This is an individual assignment; your code must be your own.
FINAL UPDATE: It seems that the fix for the memory leak in mv was incorrect. Please see the blue update for the correct fix (basically place the return; statement back in and add two free commands). Because this is a bug that was in the project spec, error checking will no longer be tested on mv.
FINAL FINAL UPDATE: It seems that the mv spec for moving to directories is bad (can you see why? You can overwrite files!). Consequently we are not testing the mv with a path#5 path argument.
Read the entire project description before beginning this assignment. While you may be able to understand the expected behavior of the project from the examples, you must refer to this document to ensure that you handle all commands correctly (especially corner cases). Much of this project involves conforming to specifications.
Update September 14 at 10:45pm: Comments in the base directory.c (relating to the locateItem() and resolvePath() ) have been updated to correct typos and to improve clarity about expected behavior. Furthermore, the path and softlink sections of this document have been updated to further clarify how paths work (and provide more examples). Requirements involving errors have been explained further, and requirements about modifications to directory.c are slightly looser. Finally, as per a student's recommendation, I added a "to-do" summary. No nontrivial changes in the project behavioral requirements have been made, however. All added text for this update is written in RED.
This project is intended to give you substantial practice with C pointers, linked structures, C strings, bit masks, and the use of malloc and free.
Most of you have had experience working with a UNIX-like operating system. Such operating systems provide a tree-structured file system and operations for navigating around it. In this project, you will work with a command interpreter, implementing commands that create, delete, and traverse nodes in a tree that vaguely simulates a UNIX directory.1
As you complete the commands, be sure that you consult this project specification - and only this project specification - to determine what correct behavior is. Most of the commands you will implement behave somewhat differently than UNIX shells do.
Bit | Attribute Name | Description |
0 | READABLE | Only applies to text files. If this bit is high, the file may be read from; if low, the file cannot be read. |
1 | WRITABLE | Only applies to text files. If this bit is high, the file may be written to or deleted; if low, the file cannot be written to or deleted. |
14 | SOFTLINK | If this bit is high, this node is a softlink. |
15 | DIRECTORY | If this bit is high, this node is a directory. |
A sample directory (without softlinks):
Its implementation (NOTE: The 0 and 1 indicates bit 15 of attributes being low or high, respectively):
command |
# of arguments |
Behavior |
SoftLink considerations | Errors to output |
pwd |
none |
Prints the full path name of the working directory. What is printed out must be a valid absolute path. The path must NOT end with a "/" character. ex:/dir1/dir2 |
If the user cd's into a softlink (call it LINK) while
in some working directory (call it x), pwd should display: x/LINK That is softlinks in the path that is printed out should not show the resolved path. |
N/A |
cd |
1 (path) |
Changes the working directory to the path (absolute or relative) to that of the argument. Recall that ".." from the root directory is still the root directory. |
If path refers to a softlink, the working directory must become the target of the softlink. However, as noted above, pwd will list the softlink's name, not the target's. | no_such_file: -path does not refer to an existing node. not_a_directory: -path refers to a valid node, but that node (or the node's target) is a text file. |
mkdir |
1 (path) |
Creates a directory with the suffix of path inside the directory specified by the path with the suffix removed. This can create files inside other directories; mkdir dir1/dir2 is valid - and will create dir2 - if and only if dir1 already exists. The new directory's READABLE and WRITABLE bit attributes are irrelevant, but because the ls command must show both being set, you may want to set both bits to be true. NOTE: mkdir dir1/dir2/ is technically always invalid (since it would create the directory "" inside dir1/dir2). However we are not testing this behavior. |
No special consideration, other than path resolving. | no_such_file: -path implies that a directory should be created inside a non-existant directory. file_exists: -directory exists |
ls |
0 or 1 (wildcard_exp) |
If given without arguments, prints the names of files in the working directory in alphabetical order. If given an argument, the argument is treated as a "wildcard_exp". A wildcard_exp is much like an ordinary filename, except it is allowed to have the character '?' in it. If this type of argument is used, the files within the working directory should be listed in alphabetical order, provided that their name matches wildcard_exp. A file name matches wildcard_exp if it is the same length and character i (0<=i<length) of the filename is the same as character i of the wildcard_exp OR character i of the wildcard_exp is a '?'. Thus, if we have the wildcard_exp of "fo?", the files "foo" and "fos" match, but "fun" does not. A text file is printed as follows:-rw filename Note that r and w refer to readable and writable respectively. So if the file is readable, but not writable, the following should be printed: -r- filename A directory is printed as follows: drw directoryname (The drw is always present when showing directories. One is not allowed to change the READABLE and WRITABLE attributes of directories.) See the next column for how softlinks should be printed. If no files by wildcard_exp are matched the error "no_such_file" must be printed. Only print this error when a wildcard_exp is given in the arguments. Note: This is the only function which cannot accept path arguments. dirmain.c will filter these out. NOTE2: It may be easier to keep directories' entryLists always in alphabetical order than to sort lists every time ls is invoked. Note3: The alphabetical ordering used by strcmp may be non-intuitive. Specifically any capital letter comes before a lower case one. Example: a proper sorting is "A", "B", "a", "b". |
Softlinks are printed as follows: atr softlink -> target Replace atr with the attributes that the target possesses (if the target is a softlink, resolve targets until a file is found). For instance, drw softlink -> directory --w softlink -> /afile If the link cannot be resolved, print INV softlink -> /afile |
no_such_file: -No file matched the wildcard expression |
rmdir |
1 (path) |
Removes the directory specified by path. It is an error if the directory is not present. |
Do not remove the softlink or the softlink's target. It is an error to execute this command on a softlink. | no_such_file: -path does not refer to an existing node. not_a_directory: -path refers to a valid node, but that node is a text file or softlink. directory_not_empty: -The directory contains files |
rm |
1 (path) |
Removes the text file specified by path. It is an error if the text/softlink file is not present. |
Remove the softlink, NOT the softlink's target. Remove the softlink regardless of what its target is. | no_such_file: -path does not refer to an existing node. is_a_directory: -path refers to a valid node, but that node is a directory operation_not_permitted: -text file is not writable |
create |
1 (path) |
Creates a "text file" with the suffix of path inside the directory specified by the path with the suffix removed. It then reads the contents of the file from standard input. The working directory must not already contain a file with the given name. Standard input is indicated as completed by two consecutive carriage returns ('\n'). The first carriage return will go in the text file; the second will not. Do not assume any bound on the size of input. The newly created file will be both READABLE and WRITABLE.e.x. create TAs/Sameer creates a file named "Sameer" in the directory TAs if and only if the TAs directory exists. NOTE: create TAs/Sameer/ is technically always invalid (since it would create the file "" inside TAs/Sameer). However we are not testing this behavior. |
N/A | no_such_file: -path implies that the file should be created inside a non-existant directory. file_exists: -node with the same name already exists. |
append |
1 (path) |
Appends to the contents of the text file specified by path with data read from standard input. Standard input is indicated as completed by two consecutive carriage returns ('\n'). The first carriage return will go in the text file; the second will not. Do not assume any bound on the size of input. |
If the path refers to a softlink, append to the target of the softlink. |
no_such_file: -path refers to a nonexistent file is_a_directory: -path refers to a directory operation_not_permitted: -text file is not writable |
cat |
≥ 1 (path) |
Prints the contents of the text files named by the path arguments. NOTE: You will be implementing a function that only takes 1 argument (see the code in dirmain.c). The behavior when an error occurs with 2+ arguments is undefined; we will only be testing cat with 1 argument. |
If the path refers to a softlink, read from the target of the softlink. | no_such_file: -path refers to a nonexistent file is_a_directory: -path refers to a directory operation_not_permitted: -text file is not readable. |
ln |
2 (path target, ansolute path linkname) |
Creates a softlink named the suffix of "linkname" (same path rules as create) with a shortcutReference of "target". In particular, the target should not be validated or resolved (other than checking that it is an absolute path): It is allowed to create an invalid link. The new softlink's READABLE and WRITABLE bit attributes are irrelevant, but because rm be able to delete the file, you may want to set at least WRITABLE to true. (NOTE: The code we gave you sets READABLE and WRITABLE both to true.) |
Other than path resolving, none. | no_such_file: -path implies that the link should be created inside a non-existent directory. file_exists: -node with the same name already exists. links_must_be_absolute: -linkname is a relative path. (Note: As indicated in the "experts" section, you are allowed to add relative softlink support - consequently we will not be testing this error) |
mv | 2 (path source, path destination) | There are two possible behaviors depending on the format of destination: If destination is a path#5 (you can optionally use this behavior if the path suffix is "." or ".."): -Moves the text file OR softlink referenced by source into the directory referred to by the destination. Otherwise: -Moves the text file OR softlink referenced by source into the directory referred to by the path with its suffix stripped. Then it renames the file to be the suffix of the path. |
If the source path refers to a softlink, move the softlink itself. Do not update the softlink's shortcutReference variable (the string or the actual target entryNode). If the destination is a softlink to a directory, move to the file to that directory. |
no_such_file: -path refers to a nonexistent file file_exists: -the destination already exists (only possible if the destination is not a path #5) is_a_directory: -source refers to a directory (Note: As indicated in the "experts" section, you are allowed to add directory moving functionality - consequently we will not be testing this error) |
setread | 2 (int, path) | Set the READABLE attribute bit of the "text file" referred to by path to be the integer argument (which will only be 1 or 0). |
If the path refers to a softlink, do this operation on the target of the softlink (again, this is recursive) | no_such_file: -path refers to a nonexistent file is_a_directory: -path refers to a directory |
setwrite | 2 (int, path) | Set the WRITABLE attribute bit of the "text file" referred to by path to be the integer argument (which will only be 1 or 0). |
If the path refers to a softlink, do this operation on the target of the softlink (again, this is recursive) | no_such_file: -path refers to a nonexistent file is_a_directory: -path refers to a directory |
Table of commands to be supported by the project 1 command interpreter |
---|
Additionally, you must initialize the file system. Set root to be a pointer to a directory entryNode with name = "/", no siblings, and no children.
Note: What this project is NOT is doing syntax checking. dirmain.c ensures that all path inputs are valid path names - and that numeric arguments are within the proper range. You simply do "semantic checking" (e.g. does this path refer to a valid node?)UPDATE: You are permitted to modify
the entryNode structure (or even not use it), provided that you email the
Project TA in charge explaining what you desire doing and why you feel that
change is advantageous (there must be a significant speed or space advantage) -
and the TA must approve your change. This requirement is necessary
because the autograder will be tracking your program's memory usage (to verify
that memory leaks to not exist); If your data structures change, your
program's potentially different memory usage will need to be accommodated.
Submit your solution with the submit
proj1 command.
You must include directory.c,
which contains your modifications to directory.c, as well as a README file, which should
discuss design decisions you made, explain your choice of data
structures, and if you like, provide brief comments on any problems you
encountered and how you overcame them. ALSO BE SURE TO NOTE WHETHER YOUR PROGRAM SUPPORTS SOFTLINKS!
1. An actual UNIX file system is organized somewhat differently; see The UNIX Programming Environment , by Brian Kernighan and Rob Pike (Prentice-Hall, 1984), for further information.
2. See UNIX Unleashed, System Administrator's Edition to view information about how file attributes actually work in UNIX-like operating systems
3. In case you were wondering, this is how the bash shell works. The tcsh shell (the default on the instructional machines), however, will make the working directory "d" after the cd.. Other shells probably even have different behavior. Everyone loves UNIX's amazing standardization.
4. Strictly speaking, since this is the first time this project has ever been given, no mistake can yet be called "common" and no question could have been asked "frequently".