CSE 4105 System Programming · Lab 12

Files and the VFS · File Systems on Disk

myls (your own ls -li); ext2cat (read a file from an ext2 disk image).

Tasks

The same steps as the lab slide. Do them on a Linux PC with the files from the zip; the tools below help you check your thinking first.

Lab 12.1 · Files and the VFS

  1. TODO 1 in myls_starter.c: opendir, then readdir until NULL; build "dir/name" with snprintf
  2. TODO 2: lstat the path; print inode, mode, links, owner (getpwuid), size, name
  3. TODO 3: mode_string: the type letter, then rwx three times from the bits 0400 … 0001
  4. TODO 4: for a symlink, readlink and print " -> target" (add the '\0' yourself)
  5. Compare with ls -lia on your home directory, /dev and /proc/self/fd

./myls proj (reference; real)

  959483 drwxr-xr-x  2 root   4096 src
  959482 drwxr-xr-x  4 root   4096 .
  959486 -rw-r--r--  1 root      6 notes.txt
  959356 drwxr-xr-x  4 root   4096 ..
  959484 drwxr-xr-x  2 root   4096 docs
  959487 lrwxrwxrwx  1 root      9 readme
                       -> notes.txt
$ ls -lia proj
959482 drwxr-xr-x 4 root root 4096 .
959356 drwxr-xr-x 4 root root 4096 ..
959484 drwxr-xr-x 2 root root 4096 docs
959486 -rw-r--r-- 1 root root    6 notes.txt
959487 lrwxrwxrwx 1 root root    9 readme -> …

lstat, not stat: stat follows a symlink and describes the target; for readme -> notes.txt you would print a regular file and never see the "l".

Lab 12.2 · File Systems on Disk

  1. ./mkimage.sh (or use the disk.img in the zip); look at it with dumpe2fs and debugfs -R "ls -l /"
  2. TODO 1: read the superblock at byte 1024; check 0xEF53; bs = 1024 << s_log_block_size
  3. TODO 2: read_inode: group and index, the group descriptor, then the inode
  4. TODO 3: walk a directory block with rec_len; compare names with memcmp
  5. TODO 4: print i_size bytes from the direct blocks. Test /hello.txt, /docs/notes.txt

reference, real

$ ./ext2cat disk.img /docs
block size 1024, 1024 inodes per group, …
  docs  in inode 2 -> inode 12
inode    12  rec_len   12  .
inode     2  rec_len   12  ..
inode    13  rec_len   16  big.txt
inode    14  rec_len   20  notes.txt
inode    15  rec_len   20  numbers.txt
inode    16  rec_len  944  readme
$ ./ext2cat disk.img /docs/notes.txt
Lab 12.2 notes
line two
$ ./ext2cat disk.img /docs/numbers.txt
block 12 needs an indirect block: Homework 12.2

Check every step with the tools: debugfs -R "stat /hello.txt" disk.img prints the inode you should find; od -A d -t x1 -j BYTE -N 64 disk.img shows the raw bytes.

Try it here

These run in your browser, with no server and no internet. They are built from the same reference programs as the lab, so their output matches what your finished C program should print. They do not replace the real lab: the kernel, gcc and your own code are the point.

Read the files

Every text file from lab12_files.zip. Read here, then download the zip to work on them.

    ← Lab 11All lecturesLab 13 →