Files
fsnap/SPEC.md

16 KiB

fsnap — Project Specification

1. Overview

fsnap is a small POSIX command-line utility written in C for exploring filesystem and Unix system-programming concepts.

The program recursively walks a directory tree, creates textual snapshots of filesystem metadata, reads and validates existing snapshots, compares snapshots, and verifies them against the current filesystem state.

The project is intentionally designed around low-level POSIX and C standard-library APIs rather than high-level filesystem abstractions.


2. Goals

The main goals of the project are to practice:

  • filesystem traversal;
  • file and directory metadata;
  • file descriptors;
  • standard I/O streams;
  • symbolic and hard links;
  • POSIX data types;
  • error handling with errno;
  • portable Unix programming;
  • resource management;
  • parsing textual data;
  • designing reusable C interfaces.

The implementation should remain reasonably portable across POSIX systems such as Linux and NetBSD.


3. Non-Goals

The initial version does not aim to provide:

  • filesystem monitoring;
  • real-time change detection;
  • process parallelism;
  • threads;
  • cryptographic integrity guarantees;
  • compression;
  • network synchronization;
  • platform-specific filesystem features.

These may be explored later as optional extensions.


4. Commands

4.1 scan

fsnap scan <directory>

Recursively walks the specified directory and prints every discovered filesystem entry to standard output.

Requirements

The walker must:

  • recursively enter real directories;
  • ignore . and ..;
  • not follow symbolic links;
  • process non-directory entries as leaves;
  • support filesystems where dirent.d_type is unavailable;
  • use lstat() as a fallback when d_type == DT_UNKNOWN;
  • propagate traversal and callback errors to the caller.

The traversal mechanism should be independent from the operation performed on each entry.

Conceptually:

walk(directory, operation, context)

This allows the same walker to be reused by commands such as scan and create.


5. Snapshot Creation

5.1 create

fsnap create <directory> <snapshot>

Creates a textual snapshot representing the state of the specified directory tree.

The command must reuse the generic filesystem walker.

For every discovered entry, the program obtains metadata using lstat() and serializes it into the snapshot.


5.2 Snapshot Entry

Each snapshot record should contain at least:

type
permissions
uid
gid
size
mtime
path

Example conceptual representation:

F|0644|1000|1000|125|1753999812|src/main.c
D|0755|1000|1000|512|1753999700|src
L|0777|1000|1000|8|1753999600|current

The exact serialization format is part of the project specification and should remain stable once the parser is implemented.


5.3 File Types

Filesystem entries should be represented explicitly.

Suggested identifiers:

F = regular file
D = directory
L = symbolic link
P = FIFO
S = socket
C = character device
B = block device
? = unknown or unsupported type

The filesystem walker itself does not necessarily need to distinguish all of these types.

For traversal purposes, the important distinction is:

directory
non-directory entry

The snapshot writer is responsible for determining the specific entry type.


6. Symbolic Links

Symbolic links must not be followed during traversal.

lstat() must therefore be used when metadata about an entry is required.

A symbolic link must be represented as a symbolic link rather than as the object it references.

A later version of the snapshot format should also store the link target obtained using:

readlink()

Broken symbolic links must still be representable in a snapshot.


7. Hard Links

Hard links are not a separate filesystem object type.

Two paths refer to the same filesystem object when both their:

st_dev
st_ino

values match.

Future versions of the snapshot format may store:

device ID
inode number
link count

to allow hard-link relationships to be detected.


8. Snapshot Writing

Snapshot output must use standard C I/O streams.

Relevant APIs include:

fopen()
fdopen()
fprintf()
fflush()
fclose()

All operations capable of failing must have their return values checked.

In particular, errors from:

fprintf()
fflush()
fclose()

must not be ignored.

A buffered write may succeed initially and fail only when the stream is flushed or closed.


9. Temporary Files

Snapshot generation may use a temporary file created using:

mkstemp()

mkstemp() returns an open file descriptor.

The descriptor may then be associated with a FILE * stream using:

fdopen()

Ownership rules must be respected:

mkstemp succeeds + fdopen fails
    -> close(fd)

fdopen succeeds
    -> fclose(stream)

The same descriptor must never be closed twice.

Temporary files must be removed during cleanup when no longer required.


10. Error Handling

Errors should be propagated rather than silently ignored.

The program should use:

0

for successful operations and a non-zero result for failures.

Library-style internal functions should generally return:

0

on success and:

-1

on failure where appropriate.

Diagnostic output should be written to:

stderr

while normal command output should be written to:

stdout

When cleanup operations occur after an error, the original errno should be preserved whenever possible.


11. Resource Management

Every acquired resource must have a clearly defined owner.

Resources include:

  • dynamically allocated memory;
  • directory streams;
  • file descriptors;
  • FILE * streams;
  • temporary files.

Every successful:

malloc()
opendir()
open()
mkstemp()
fopen()

must eventually have a corresponding cleanup operation.

Cleanup must also occur on error paths.


12. Snapshot Parser

12.1 list

fsnap list <snapshot>

Reads and validates an existing snapshot.

The main purpose of this command is to implement a robust parser for the snapshot format.

The parser should transform each textual record into an internal structure representing one snapshot entry.

Conceptually:

snapshot_entry
├── type
├── permissions
├── uid
├── gid
├── size
├── modification time
└── path

The exact C structure is intentionally left as an implementation decision.


13. Reading Snapshot Records

Records must be read without assuming an arbitrary fixed maximum line length.

The parser should therefore use dynamically sized input rather than constructs such as:

char line[1024]

POSIX facilities such as getline() are appropriate.

The program must distinguish:

  • successful read;
  • end of file;
  • I/O error;
  • malformed record.

14. Numeric Parsing

Numeric fields must be validated carefully.

The parser must detect:

  • empty numeric fields;
  • invalid characters;
  • trailing characters;
  • negative values where forbidden;
  • values outside the representable range;
  • integer overflow and underflow.

Functions such as:

strtol()
strtoul()
strtoimax()
strtoumax()

may be used with appropriate validation.

The parser must not assume that POSIX types such as:

uid_t
gid_t
off_t
time_t
ino_t

are equivalent to a specific primitive C type.


15. Record Validation

The parser must reject malformed records.

Examples include:

F|0644|1000

Missing fields.

F|hello|1000|1000|12|123|file

Invalid permissions.

X|0644|1000|1000|12|123|file

Unknown file type.

F|0644|1000|1000|-10|123|file

Invalid size for a regular file.

Errors should preferably identify the offending snapshot and line number.

Example:

snapshot.fsnap:17: invalid uid

16. Path Encoding

Unix filenames may contain almost any byte except:

NUL
/

inside a path component.

Therefore filenames may legally contain characters used by the snapshot format, including:

|
newline
backslash

Before the snapshot format is considered stable, it must define how such paths are encoded.

Possible strategies include:

Escaping

For example:

\  -> \\
|  -> \|
newline -> \n

Length-prefixed fields

Store the pathname length explicitly before its contents.

The chosen encoding must allow the parser to reconstruct the original pathname without ambiguity.


17. Snapshot Comparison

17.1 diff

Future command:

fsnap diff <old-snapshot> <new-snapshot>

Compares two snapshots and reports changes.

Possible result categories:

A = added
D = deleted
M = metadata or contents changed
P = permissions changed
L = symbolic link changed

The first implementation may load all snapshot records into memory and sort them by pathname.


18. Filesystem Verification

18.1 verify

Future command:

fsnap verify <snapshot> <directory>

Compares a stored snapshot with the current filesystem.

It should eventually distinguish conditions such as:

missing file
new file
type changed
size changed
permissions changed
owner changed
mtime changed
symbolic-link target changed
access error

Errors such as:

ENOENT
EACCES
ENOTDIR
ELOOP

must not all be interpreted as equivalent conditions.


19. Portability

The project should prefer POSIX interfaces and avoid unnecessary Linux-specific functionality.

Code should be tested on multiple Unix-like systems where practical.

Current target environments include at least:

Linux
NetBSD

The implementation must not rely on dirent.d_type always being populated.

DT_UNKNOWN must be handled correctly.


20. Build Requirements

The project should be compilable using a standard Makefile.

Recommended compiler configuration:

-std=c11
-Wall
-Wextra
-Wpedantic
-Wconversion

Headers should reside under:

include/

and compiler include paths should be configured through:

-Iinclude

Automatic header dependency generation with:

-MMD
-MP

is recommended.


21. Suggested Project Structure

fsnap/
├── Makefile
├── README.md
├── include/
│   └── fsnap.h
├── src/
│   ├── main.c
│   ├── walk.c
│   ├── scan.c
│   ├── create.c
│   ├── parser.c
│   ├── compare.c
│   └── utility.c
└── tests/

The exact module layout may evolve as responsibilities become clearer.


22. Testing

The filesystem walker and snapshot writer should be tested against trees containing:

regular files
empty files
directories
nested directories
symbolic links
broken symbolic links
hard links
FIFOs
character or block devices where available
unusual filenames
permission errors

Example test environment:

mkdir -p test/subdir

printf 'hello\n' > test/file.txt

ln test/file.txt test/hardlink.txt

ln -s file.txt test/link.txt

ln -s missing.txt test/broken-link

mkfifo test/pipe

The walker must never recursively follow link.txt, even if a symbolic link points to a directory.


23. Development Stages

The project is divided into incremental stages.

Stage 1 — Filesystem Walker

Implement:

fsnap scan

Goals:

  • directory traversal;
  • dirent;
  • d_type;
  • DT_UNKNOWN;
  • lstat();
  • callback-based walker.

Status: implemented.

Stage 2 — Snapshot Writer

Implement:

fsnap create

Goals:

  • filesystem metadata;
  • struct stat;
  • standard I/O;
  • serialization;
  • temporary files;
  • robust cleanup.

Status: implemented.

Stage 3 — Snapshot Parser

Implement:

fsnap list

Goals:

  • getline();
  • parsing;
  • numeric conversion;
  • input validation;
  • dynamic memory;
  • snapshot entry representation.

Status: next development stage.

Stage 4 — Snapshot Comparison

Implement:

fsnap diff

Goals:

  • collections of records;
  • sorting;
  • comparison;
  • filesystem identity and metadata semantics.

Stage 5 — Filesystem Verification

Implement:

fsnap verify

Goals:

  • compare persisted metadata against the live filesystem;
  • classify changes;
  • handle filesystem errors correctly.

24. Optional Extensions

Once the basic project is complete, possible extensions include:

Content hashing

Calculate checksums or hashes for regular files.

This introduces additional practice with:

open()
read()
close()

and contrasts raw file I/O with standard I/O.

Sparse-file detection

Compare logical file size with allocated blocks using fields such as:

st_size
st_blocks

Track:

st_dev
st_ino

to identify multiple paths referring to the same inode.

Snapshot format versioning

Add a header such as:

FSNAP|1

to allow the on-disk format to evolve without making older snapshots impossible to recognize.

Atomic snapshot replacement

Write the completed snapshot to a temporary file and atomically replace the destination where the target filesystem permits it.

Process-based hashing

After studying Unix process management, file hashing may be distributed across worker processes.

Signal handling

After studying Unix signals, long-running snapshot creation could respond cleanly to interruption and remove incomplete temporary files.


25. Learning Objectives

By the end of the project, the implementation should demonstrate practical understanding of concepts covered by the early chapters of Advanced Programming in the UNIX Environment, particularly:

  • Unix filesystem semantics;
  • file descriptors;
  • file metadata;
  • directories;
  • inode-related concepts;
  • symbolic links;
  • standard I/O;
  • buffering;
  • error handling;
  • POSIX types;
  • portability;
  • resource ownership.

The project should favor correctness, explicit error handling, and understanding of Unix behavior over minimizing the amount of code.


26. Implemented Snapshot Format (Version 1)

New snapshots start with the exact header FSNAP|1, followed by records:

type|permissions|uid|gid|size|mtime|target|fingerprint|device|inode|links|path

device, inode, and links hold decimal st_dev, st_ino, and st_nlink converted through uintmax_t. A link count must be positive. A matching (device, inode) pair identifies names for the same filesystem object. Both target and path use the escaping defined in section 16. The fingerprint is CRC32 of regular-file bytes or of a symlink's unescaped target string, and zero for other entry types. An empty versioned snapshot contains only the header.

Readers retain support for the legacy headerless nine-field format (the first eight fields above followed by path), including a zero-byte file. Unknown versions and mixed layouts must fail, with physical line numbers in diagnostics. Identity fields are compared only when present in both records. verify reports non-symlink identity changes with I and link-count changes with H.

Readers consume a single stream through EOF with dynamic record storage. They must not silently accept only an initially counted prefix. Changes to a regular snapshot's size, mtime, or ctime during reading fail with EAGAIN and a diagnostic. This detects ordinary concurrent writes; it does not replace filesystem locking.

create publishes a fully flushed, synced, and closed temporary file with rename. By default the temporary file is beside the destination. A nonempty TMPDIR overrides the working directory. Only EXDEV allows a copy fallback: copy into another private temporary file beside the destination, sync and close it, then rename. Never truncate the published destination. Destination symlinks and non-regular files are rejected; output files have mode 0600. This provides atomic publication, not a promise of power-loss durability for the directory.

Public exit status is 0 for complete success, 1 for failure, and 2 for completion with skipped entries. Unreadable child directories and unreadable regular files produce diagnostics and status 2; unreadable file records are omitted rather than assigned a fabricated checksum. An unreadable root fails without publishing. verify returns 1 for findings, or 2 if its traversal was incomplete and found no differences.