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
Create a new directory for this lab ~/csci315/Labs/Lab10
Then copy all files from
~cs315/Labs/Lab10/
In your top level directory for Lab10, create a file called lab10.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 example 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.
- Update the 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 pull
- git add hexdump.h
- git add hexdump.c
- git add Makefile
- git add fdump.c
- git add [any additional files necessary]
- git commit -m “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 lab10.txt, make sure you mark the answers with problem numbers properly, e.g., Problem 2.1, etc.
- (2.1): Run your fdump program with the following parameters: filename = hexdump.c, offset=1000, size=128. Copy the first two lines of output to your lab10prelab.txt. Explain what you see.
- (2.2): Run your fdump program with the following parameters: filename = fdump, offset=500, size=128. Copy the first two lines of output to your lab10prelab.txt. 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 interpret 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 pull
- git add lab10.txt
- git commit -m “Lab 10.2 completed”
- git push
Grading Rubric
Problem 1 [15 points total]
- 1.1 [4 points] The program checks proper command arguments, exits if anything is incorrect.
- 1.2 [6 points] The program calls proper library functions to move the file to the correct location and read the specified number of bytes into a buffer.
- 1.3 [5 points] The program calls the hexdump function and produces proper output.
Problem 2 [15 points total]
- 2.1 [3 points] Explain your results properly.
- 2.2 [3 points] Explain your results properly.
- 2.3 [3 points] Make comparisons and draw reasonable conclusions.
- 2.4 [6 points] Explain the result of running file(1) on these five files.