Master the fundamental concepts of file systems through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksDirectories map component names to inode numbers. mkdir creates a new directory inode and links . and ..; readdir iterates entries; rmdir removes empty dirs only.
API surface:
dirent records with inode typeFor example, removing /tmp/foo succeeds only when foo contains just . and ..; a stray file inode blocks removal with ENOTEMPTY.
mkdir(), rmdir(), and readdir() are POSIX primitives that every shell's ls, mkdir -p, and file manager ultimately call, and the . and .. entries you handle here are why cd .. works on every Unix system. Forgetting to skip . and .. when counting files is a classic bug that silently inflates file counts in backup and sync tools.
Before you call the implementation done, walk failure modes on purpose. Test empty structures, single-element edge cases, maximum concurrency, and errno paths that must not crash the program. OS code usually fails in production when happy-path tests pass but invariants break under contention or memory pressure.
Keep structures small and name fields after kernel counterparts when possible. That lets you read man pages and kernel source side by side while you work. Print observable events during development; remove noisy logs once tests pass reliably.
You will implement directory create, list, and remove atop your inode tree or real syscalls. This exercise requires dot and dot-dot handling in listings.
Work with directories through the POSIX calls mkdir, rmdir, opendir/readdir/closedir and stat. Implement mkdir -p (create every missing component), list directories including the special . and .. entries every directory carries, and report the errno of every failure. Also implement rm -r, which has to delete depth-first because rmdir only removes empty directories.
As in the previous task: mkdtemp("/tmp/fsXXXXXX") (or "fsXXXXXX" in the current directory if /tmp isn't writable), chdir into it and umask(022). Paths in the input are relative to it and never contain ... Remove everything before exiting.
| Command | Behaviour | Success output |
|---|---|---|
mkdir PATH | mkdir(PATH, 0755) | mkdir PATH: ok |
mkdirp PATH | create each missing prefix in order, ignoring those that already exist as directories | mkdirp PATH: created P1, P2 or mkdirp PATH: nothing to do |
touch PATH | create an empty file (open(O_CREAT | O_WRONLY, 0644)) | touch PATH: ok |
rmdir PATH | rmdir | rmdir PATH: ok |
ls PATH | opendir + readdir | ls PATH: . .. dir/ file (N entries) |
rmr PATH | delete recursively, children before parents | rmr PATH: removed N entries |
ls lists all entries returned by readdir, sorted by byte value, with / appended to directories other than . and ... N counts them all, including . and ... rmr's count includes the directory itself.
COMMAND PATH: SYSCALL failed: ENAME for the first failing call (mkdir, rmdir, open, opendir, unlink), using the symbolic errno: EEXIST, ENOENT, ENOTDIR, ENOTEMPTY. For mkdirp, a prefix that exists but is not a directory fails with mkdir failed: EEXIST (after reporting nothing created); the prefixes it already created stay.
Input:
cLoading…
Output:
cLoading…
readdir results yourself, because their order is filesystem-dependent. Use stat (or d_type with a stat fallback) to mark directories.rmr recurses with opendir/readdir, skipping . and .., and calls unlink for files and rmdir for directories on the way back up.Hidden tests cover mkdir on an existing name and under a missing parent, mkdir through a file (ENOTDIR), mkdirp over partly existing paths and through a file, rmdir of a file, ls of a missing directory, and rmr on a deeper tree.