CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 7 of 7 · 3 min read

Exercises

Easy

Easy 1: Link count of a directory (20 minutes)

A directory has 3 subdirectories and 10 ordinary files. What is its link count?

Done when: 5.

Hint

2 plus the number of subdirectories. Ordinary files do not count.

Easy 2: Cold lookup (20 minutes)

List the reads for a cold lookup of /home/ann/notes.txt when each directory costs one inode read and one data read, and the file costs one inode read.

Done when: 7 reads, named in order.

Hint

each directory on the way costs an inode read and a data read, and the last name only an inode read.

Easy 3: Names left (20 minutes)

A file has 1 link. Two hard links are added and one name is removed. What is the link count, and is the data still there?

Done when: 2, and yes.

Hint

one link plus two minus one.

Medium

Medium 1: Symlink size (45 minutes)

Create a symlink to ../shared/data/report.txt and read its size with lstat or ls -l.

Done when: 25 bytes, and a sentence that says why the size equals the length of the path.

Hint

use ln -s ../shared/data/report.txt x and ls -l x.

Medium 2: Run the program (45 minutes)

Compile the program of section 3 and run it in a directory on an ext4 or similar file system. Change it so that it also creates a hard link to the symbolic link, and report the link counts.

Done when: the output of your run and the new counts with a sentence on what link of a symlink does on your system.

Hint

link may or may not follow a symlink, depending on the system. Report what you see.

Medium 3: Predict and check (45 minutes)

Predict the link counts of t, t/a, t/a/b and t/c after mkdir -p t/a/b t/c. Then check with stat -c '%h %n'.

Done when: a prediction of 4, 3, 2 and 2, and the checked output.

Hint

a directory's count is 2 plus its subdirectories.

Hard

Hard 1: Find all names (90 minutes)

Write a program that takes a file name, finds its inode number and device, and then walks a directory tree with nftw and prints every path with the same inode and device.

Done when: a test with three hard links prints three paths, and a symlink to the file is not printed.

Hint

compare st_ino and st_dev from lstat. Skip symlinks by checking S_ISLNK.

Hard 2: Dangling links (90 minutes)

Write a program that walks a tree and prints every symbolic link whose target does not exist. Use lstat and stat.

Done when: a test tree with two dangling links and one good link prints exactly the two.

Hint

lstat says it is a link and stat fails with ENOENT when it dangles.

Hard 3: Deleted but open (90 minutes)

Write a program that creates a file, writes 1 MiB, unlinks it while it is still open, and then reads the data back from the open descriptor. Print st_nlink from fstat before and after the unlink.

Done when: a link count of 1 then 0, and the 1 MiB is read back correctly, with a sentence on when the blocks will be freed.

Hint

fstat on the descriptor still works after the unlink.