Schedule
Labs
Assignments
TA office hours
Topic videos
Some course notes
Extra problems
Lecture recordings
[solution]
2. Sketch the inode and directory information involved in the following output from an "ls -liR" of the directory which is inode number 10. ('R' is "recursive", and 'i' is "list inode numbers" (which appear in the left column, i.e. the numbers which are 12 through 15 below)).
That is, list the inode numbers and what data you know is in those inodes; and state the contents of the directories involved.
total 8 12 drwxr-xr-x 3 ajr users 4096 Feb 27 10:05 bar 13 drwxr-xr-x 3 ajr users 4096 Feb 27 10:05 baz ./bar: total 8 14 -rw-r--r-- 1 ajr users 20 Feb 27 10:06 gar ./baz: total 8 15 -rw-r--r-- 1 ajr users 50 Feb 27 10:06 gaz
[solution]
3.
Consider the following portion of a unix filesystem hierarchy:
The '.' and '..' entries are not indicated, of course; otherwise, all of the files below "a2" are shown.
a2.pdf, splitexpr.c, wfold.c, and wfold.1 are plain files; a2, handout, starter, soln, and splitexpr are directories.
a) Indicate the link count for each of these files:
handout:
starter:
soln:
wfold.c:
b) Suppose the user is cd'd to the handout directory and types:
ln a2.pdf example.pdfWhich file(s) will change their link count, and what will the link count(s) be now?
c) Suppose the user is cd'd to the soln directory and types:
mkdir newWhich file(s) will change their link count, and what will the link count(s) be now?
[solution]
An example which only works on "." appears on the unix (not linux) man page and is also at https://www.teach.cs.toronto.edu/~ajr/209/probs/fsys/readdir.c. One defect in that example program, which you must correct, is that it does not check whether the opendir() function call succeeds. If opendir() fails for some reason, it will return a null pointer. In this case, the reason for the failure has been stashed (in "errno"), and you should pass the directory name to perror() which will print a nice error message using this stashed error value.
Also unlike the version on the man page, your version should not work only on the current directory but should instead apply itself in turn to every directory specified on the command line. Recall that you can loop through the argv array like this:
for (i = 1; i < argc; i++) { ... argv[i] ... }
[solution]
2. Using opendir() and friends and running stat() on each file in the directory, write a program which displays the size in bytes of the largest file in a directory specified on the command-line (i.e. in argv).
[solution]
3. Write a program which, for each directory specified on the command line, recursively finds the total byte size of the files in that directory and all subdirectories. Your program will contain a function which takes as argument a directory name and returns the total number of bytes in all files and subdirectories recursively. Suppose this function is called totalbytes(). It will contain a readdir() loop which calls stat() on each file. From S_ISDIR(statbuf.st_mode) you will know whether the file is a (sub)directory. If it is a directory, and is not "." or "..", call totalbytes() recursively. Your totalbytes() should opendir() the new directory and go through the directory, possibly calling itself recursively for subdirectories. Each invocation of totalbytes() should call closedir() before returning or you will run out of file streams.
Optional cheat: To avoid having to construct the subdirectory's full path name to pass to opendir() as you descend the hierarchy, totalbytes() could begin with a chdir() (change working directory, like "cd") to the subdirectory, and end with a chdir("..") to restore the previous state. If you avail yourself of this cheat, please notice that your program no longer always works with multiple command-line arguments; that's why I call it a "cheat". (To experience the problem, use multiple arguments which are relative pathnames but do contain multiple directory levels (i.e. contain slashes).)
[solution]
4. Write a program whose argv[1] is a directory name and which finds a pair of hard-linked files within the subdirectory tree.
Example:
% mkdir glop % ln blop/foo glop/foo % findlink . ./blop/foo and ./glop/foo are links to the same file %
5. Write pwd. The algorithm is:
Improvements: