The Unix File System Interface
Goals
- Gain more practice in working with the Unix file system interface: This lab will have you work with functions to inspect various attributes of files, as you modify a program for traversing a directory tree.
- Gain more practice with the higher level file I/O interface: By now, you have used the C/Unix file I/O APIs for lower level syscalls (open(2), read(2), etc.) and for higher level streams (fopen(3), fread(3), etc.). It pays off to think critically about the differences between the two kinds of interface, so that you understand a little more about the design of the operating system.
- Understand how Unix differentiates file types: In some operating systems, a file name extension is used to designate a file type, so that both the user and the system know what type of application generated the file and/or what type of application can manipulate that file. In Unix, file name extensions are considered just part of the file name and defined by conventions. In this assignment, you will work to develop the understanding of how Unix can figure out file types in the absence of an authoritative extension or any other type of metadata.
Credits
This pre-lab assignment was created by Prof. L. Felipe Perrone. Permission to reuse this material in parts or in its entirety is granted provided that this credits note is not removed. Additional students files associated with this lab, as well as any existing solutions can be provided upon request by e-mail to perrone[at]bucknell[dot]edu
Set Up
In your git repo, create a working directory for this lab:
~/csci315/Labs/Lab10/
Copy the following directory recursively to your git repository; this will be your working directory for this lab:
~cs315/Labs/Lab10/
In your top level directory for Lab10, create a file called prelab.txt.
Problem 1 (15 points)
Create a C program called fdump.c that works as described below.
- The program must accept the following command line parameters, in the order given: filename (a char array), offset (unsigned integer), and size (unsigned integer). If the three command line parameters are not provided by the user, the program must terminate immediately with an error message and indicate the proper usage (that is, the order and the types of command line parameters expected).
- The program opens the file indicated by filename with fopen(3), moves forward the file position indicator by the number of bytes indicated by offset, and reads size bytes from filename into a buffer. (Hint: you will need to make a call to a random access library function to get to the right read location into the user-specified file.)
- Once that data is read into your program’s buffer, make it call the function hexdump provided to you in files hexdump.h and hexdump.c. The output generated will resemble the exemple below, which is similar to what is produced by the xxd Unix utility.
0000000: d4c3 b2a1 0200 0400 0000 0000 0000 0000 ................ 0000010: ffff 0000 0100 0000 47c5 a943 fd14 0300 ........G..C.... 0000020: 2a00 0000 2a00 0000 ffff ffff ffff 0001 *...*........... 0000030: 039c ffbd 0806 0001 0800 0604 0001 0001 ................ 0000040: 039c ffbd c0a8 0166 0000 0000 0000 c0a8 .......f........ 0000050: 0101 49c5 a943 5eed 0d00 4000 0000 4000 ..I..C^...@...@. 0000060: 0000 0001 039c ffbd 000c 41a1 f5da 0806 ..........A..... 0000070: 0001 0800 0604 0002 000c 41a1 f5da c0a8 ..........A..... ...
- Close the file and terminate.
- Create a Makefile to generate your fdump executable. You should compile hexdump.c separately into an object that gets linked with the compilation of fdump.c at a later point.
When you are done with this, you need to:
- cd ~/csci315/Labs/Lab10
- git add hexdump.h
- git add hexdump.c
- git add Makefile
- git add fdump.c
- git add [any additional files necessary]
- git commit -m “Pre-lab 10.1 completed”
- git push
Problem 2 (15 points)
When you are done coding and debugging, record your answers to the questions below in prelab.txt:
- (2.1): Run your fdump program with the following parameters: filename = hexdump.c, offset=1000, size=128. Explain what you see.
- (2.2): Run your fdump program with the following parameters: filename = fdump, offset=500, size=128. Explain what you see.
- (2.3): Compare the output you produced for answers (2.1) and (2.2). Looking at the hexadecimal dump on the left, in both cases, makes it clear that inside both files, you store information in binary encoding. However, the data to the right of the hexadecimal dump shows something human-readable for (2.1) and non-human readable for (2.2). In this answer, you are asked to explain why this is the case.
- (2.4): Now, run the file(1) command on the files below and do your best to interprete the results. (You can always look at the man page for file to learn what it does…)
- /usr/bin/file,
- ~cs315/Labs/Lab10/work
- ~cs315/Labs/Lab10/beauty
- Your separately compiled hexdump.o
- hexdump.h
Do some research to discover how file(1) figures out the type of content in these files. Record your findings in this answer along with citations to the sources you used.
When you are done with this, you need to:
- cd ~/csci315/Labs/Lab10
- git add prelab.txt
- git commit -m “Pre-lab 10.2 completed”
- git push