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 worksWriting FAT12 means updating directory entries, allocating FAT clusters, and preserving boot sector invariants. Cluster chains are singly linked through 12-bit entries packed two per byte triplet in the FAT.
Create file workflow:
0xFF8.. 0xFFF EOC markersFor example, a 3-cluster file might use clusters 2 → 3 → 4 with FAT entries linking forward.
Every field you pack here , the space-padded 8.3 filename, the 0xFFF end-of-chain marker , matches exactly what tools like mkfs.fat and fsck.fat validate, and what real DOS and early Windows machines wrote to floppies for two decades. Forgetting to write the FAT back to disk after updating it in memory is a classic bug that silently corrupts the whole filesystem on the next mount.
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 create, write, and close that updates FAT and directory on disk image. The task asks you to verify round-trip by re-reading with your FAT12 reader.
Write to a FAT12 filesystem: format an image, then create, extend and delete files. Every change has to be made in the on-disk structures: allocate free clusters from the FAT and link them into a chain ending in 0xFFF, pack the 12-bit entries into both FAT copies, write the data into the clusters, and fill in 32-byte directory entries with space-padded 8.3 names. Report "disk full" and "directory full" before changing anything.
format)512-byte sectors, 1 sector per cluster, 1 reserved (boot) sector, 2 FAT copies of spf sectors each, then the root directory (ROOT_ENTRIES × 32 bytes, a multiple of 512), then the data area. spf is the smallest number of sectors such that the FAT holds an entry for every data cluster plus entries 0 and 1: (clusters + 2) × 3 / 2 ≤ spf × 512, where clusters = TOTAL − 1 − 2·spf − ROOT_ENTRIES·32/512. FAT entries 0 and 1 are 0xFF8 and 0xFFF. The boot sector gets the usual BPB fields (as in the reader task).
FAT12 entry n occupies the 16-bit little-endian word at byte ⌊3n/2⌋ of each FAT: the low 12 bits if n is even, the high 12 bits if odd. Writing one entry must preserve the neighbouring entry's nibble.
| Command | Effect | Output |
|---|---|---|
format TOTAL ROOT_ENTRIES LABEL | build an empty image | format: 24 sectors, fat 1 sector x2, root 16 entries, 20 data clusters |
write NAME TEXT... | create a file with the rest of the line (after one space) as content | write NAME: 13 bytes, clusters 2 |
fill NAME CHAR COUNT | create a file of COUNT copies of CHAR | fill NAME: 700 bytes, clusters 3 4 |
append NAME TEXT... | extend a file, filling the last cluster before allocating more | append NAME: 23 bytes (+10), clusters 2 |
rm NAME | free the chain, mark the entry 0xE5 | rm NAME: freed 2 clusters |
cat NAME | print the content | cat NAME: "..." |
ls | every live entry in directory order | NAME SIZE bytes, clusters 2 5 (name in 12 columns; no clusters for an empty file), then N files, F free clusters |
fat N | the first N FAT entries, then the bytes holding them, then a copy check | fat[0..5]: ff8 fff fff 004 fff 000, bytes: f8 ff … (⌈3N/2⌉ bytes), copies match |
0x00 or 0xE5.COMMAND NAME: invalid name.already exists (write/fill), not found (append/rm/cat), directory full, and disk full (need K clusters, F free), all as COMMAND NAME: MESSAGE. Checks run in the order: invalid name, already exists / not found, directory full, disk full.Input:
cLoading…
Output:
cLoading…
get_fat/set_fat on it; set_fat updates both copies.Hidden tests cover deleting a file and reusing its directory slot and clusters (producing a fragmented chain), appending to an empty file, a full disk, a full root directory, an existing name and invalid names.