Lab: File System Utilities

CSC 213 - Operating Systems and Parallel Algorithms - Weinman



Summary:
You will work at the system level to rebuild common command line file utilities.
Assigned:
Tuesday 4 December
Due:
10:30 PM Monday 10 December
Objectives:
 
Collaboration:
You will complete this lab in teams as assigned by the instructor.

Exercises

Preliminaries

Do this laboratory on any MathLAN workstation. Copy the starter files to somewhere in your home directory.
cp -R ~weinman/public_html/courses/CSC213/2012F/labs/code/filesystems ~/somewhere/

Part A: Improving Tanenbaum's file copy

  1. Review the file mycp.c. Compile it using make and try using it as you would use the standard cp(1) program.
  2. You may have noticed that the program's output is the same-nothing-regardless of whether the copy is successful. Wherever the code exits with an error status, modify the code to use perror(3) whenever possible, or fprintf(3) otherwise, to print a friendly error message.
  3. Rather than always creating the new file with the protection mode 0700, use fstat(2) to obtain the protection mode (i.e., file permissions) of the original file.
    Note that creat(2) automatically applies the process's umask to the protection mode.
  4. You may have also noticed that mycp.c does not let you copy a file to a different directory except by typing the path and filename for the new file:
    ./mycp mycp ../mycp
  5. Modify mycp.c so that you can specify just a directory for the destination:
    ./mycp mycp ..
    ls ../mycp
    ../mycp
    Hint:
    If creating the destination file (out_fd) fails, then check the errno system variable. If it is equal to EISDIR, indicating the destination path names not a file but a directory, then do the following instead of immediately exiting with an error.

Part B: More file system utilities

  1. Write a program myrm.c, which takes as its sole command-line argument the name of a file to remove. Use unlink(2) to remove the file. In this and all of the programs you write for this lab, be sure to report errors to the user as appropriate. Add myrm to the PROGRAMS variable of the Makefile.
  2. Write a program mytouch.c, which will be similar to the touch(1) program. Add mytouch to the PROGRAMS variable of the Makefile.
    ls -l
    total 32
    -rw------- 1 davisjan mathfac    6 2010-10-08 21:36 foo
    -rwx------ 1 davisjan mathfac  217 2010-10-08 20:02 Makefile
    -rwx------ 1 davisjan mathfac 7867 2010-10-08 21:28 mycp
    -rw------- 1 davisjan mathfac 1611 2010-10-08 21:01 mycp.c
    -rwx------ 1 davisjan mathfac 7964 2010-10-08 21:35 mytouch
    -rw------- 1 davisjan mathfac  756 2010-10-08 21:35 mytouch.c
    cat foo
    blah!
    touch foo
    cat foo
    blah!
    touch bar
    cat bar
    ls -l
    total 32
    -rw------- 1 davisjan mathfac    0 2010-10-08 21:37 bar
    -rw------- 1 davisjan mathfac    6 2010-10-08 21:37 foo
    -rwx------ 1 davisjan mathfac  217 2010-10-08 20:02 Makefile
    -rwx------ 1 davisjan mathfac 7867 2010-10-08 21:28 mycp
    -rw------- 1 davisjan mathfac 1611 2010-10-08 21:01 mycp.c
    -rwx------ 1 davisjan mathfac 7964 2010-10-08 21:35 mytouch
    -rw------- 1 davisjan mathfac  756 2010-10-08 21:35 mytouch.c
    $
    Here's the basic algorithm:
  3. Consider how to implement a version of the mv(1) program, which moves or renames a file. There are three different approaches: (1) copy the file and then use unlink(2) to delete the original; (2) use link(2) to create a hard link from the destination path to the original file, and then use unlink(2) to delete the original; (3) use rename(2) to rename the file.
    1. What are the advantages and disadvantages of these three approaches? Here are three issues you should consider: performance, system crashes, and renaming across the mounted file systems in a virtual file system.
    2. Using whichever approach you prefer, write a program mymv.c, which will be similar to the mv(1) program. As in mycp.c, take as command-line arguments a original file name followed by a destination path, which is either a file or a directory. Add mymv to the PROGRAMS variable of the Makefile.
  4. Write a program myls.c, which will be a simple version of the ls(1) program that lists the names of the files in a directory, along with each file's size (in bytes).
    Add myls to the PROGRAMS variable of the Makefile.

What to turn in

Acknowledgment

This assignment was developed by Janet Davis, where she credits "the file copy program from Tanenbaum's Modern Operating Systems, 3/e."