Phase 4A transactional filesystem contract¶
Status: implementation contract
Rollback checkpoint:
fc50585b280dd9bb76c7671f1932a6d80bc4f06eScope: internal, non-cryptographic filesystem mechanics only
Responsibility and scope¶
cryptography_suite.streaming.atomic supplies an internal
TransactionalSink implementation. It accepts bounded bytes, keeps them
under an operation-owned staging name, and publishes them only on an explicit
commit. It is not exported by the package root or by
cryptography_suite.streaming.
The sink does not encrypt, decrypt, authenticate, hash caller data, invoke a
provider, evaluate policy, parse an envelope, run a CLI, or delete a caller’s
source. Protector remains fail-closed.
Trusted root and untrusted relative destination¶
The caller supplies:
an explicit absolute trusted output root that already exists; and
an untrusted relative destination below that root.
The implementation never obtains either value from the current working
directory or environment. The root is opened without following a final link.
It must be a directory owned by the effective caller and must not permit
group/other writes on POSIX. Windows requires a local NTFS root and retains
non-reparse directory handles while the transaction is active. “Local NTFS”
is decided from the volume that actually contains the root: the volume path
is resolved with GetVolumePathNameW (so a mount point anywhere above the root
is honored), its drive type must be fixed or removable (remote, RAM-disk,
optical, and unknown drives are refused), and the filesystem name and the
persistent-ACL flag are read through GetVolumeInformationByHandleW on a
handle to the root itself.
The relative path grammar rejects empty paths, absolute/anchored/drive/UNC
paths, NUL, . and .., empty components, repeated separators, alternate
separators, and platform-reserved or invalid names. Parent directories must
already exist. No parent is created.
POSIX traverses each parent with directory-relative open, O_DIRECTORY, and
O_NOFOLLOW. Windows opens and retains each parent using CreateFileW with
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT and rejects every
reparse point. Lexical validation is only the first check; it is not described
as a race-proof containment mechanism.
POSIX passes Unicode strings to the native filesystem unchanged. macOS
filesystem normalization aliases remain filesystem-defined. Windows rejects
trailing spaces/dots, alternate data stream syntax, invalid Win32 filename
characters, and every reserved device basename with or without an extension:
CON, PRN, AUX, NUL, CONIN$, CONOUT$, CLOCK$, COM0–COM9,
LPT0–LPT9, and the superscript forms COM¹–COM³ and LPT¹–LPT³.
Spaces before the extension are ignored when matching, so NUL .txt is
rejected. Windows case comparison and normalization remain filesystem-defined.
Link and file-identity rules¶
Destination inspection never follows a final symlink or reparse point. An
existing destination must be a regular file: directories, FIFOs, sockets,
devices, and (on Windows) any handle whose GetFileType is not a disk file
are rejected, as is a file whose link count exceeds one. On POSIX the retained
overwrite handle is opened with O_NONBLOCK | O_NOCTTY and its type is
re-verified through fstat, so a FIFO or device that races into the name
cannot block or acquire a controlling terminal. When a
caller supplies an already-open source descriptor, the sink records its native
file identity and compares that identity with the destination during
construction and immediately before publication. The source handle remains
caller-owned and is never closed or deleted.
POSIX identity is device plus inode from fstat/directory-relative stat.
Windows identity is volume serial plus file index from
GetFileInformationByHandle. These values never enter exception text, repr,
logs, or public metadata.
For an existing overwrite destination, construction also retains an open destination handle. Commit compares the retained object with the immediately reinspected name before publication. On POSIX the retained handle prevents inode reuse from making a delete-and-recreate race look unchanged. Windows opens the retained target with delete sharing and uses POSIX-semantics replace flags so the handle can remain open through atomic replacement.
Temporary ownership and permissions¶
Staging occurs exclusively in the destination’s existing parent. A random nonsecret name is created with an exclusive create primitive and contains no source name, destination name, tenant, context, key, or data-derived value. The retained open handle and native identity establish ownership.
POSIX creates with mode 0600, applies fchmod(0600), and verifies the mode.
Windows creates with a protected DACL granting full control only through the
owner-rights SID and verifies the resulting DACL. A platform/filesystem that
cannot establish the permission property fails closed.
Cleanup reopens or stats the staging name without following links and compares its identity with the retained identity. An identity mismatch is never unlinked.
Bounds and writes¶
The immutable internal options object represents a future policy decision. It contains maximum write size, maximum total bytes, policy overwrite authority, and operation overwrite intent. It is not a public policy schema. Boolean values are rejected where integer bounds are required, caller bounds cannot exceed hard implementation ceilings, and overwrite defaults to false.
write accepts exact bytes. Empty bytes is an accepted no-op. Each chunk
and the checked cumulative total are validated before writing; a rejected
bound writes nothing and leaves the sink open. Interrupted and partial system
writes are retried until complete. Any other failure once writing has begun,
including an unexpected exception or an interrupt such as KeyboardInterrupt,
makes the transaction non-committable but leaves deterministic abort
available.
Publication¶
Commit is permitted once from the open state:
flush the retained file handle with the platform file-durability primitive;
revalidate staging identity;
revalidate destination type, link count, and optional source identity;
atomically publish within the already-open parent;
perform the platform directory/rename durability barrier; and
close owned handles.
No copy-and-delete or cross-filesystem fallback exists.
No overwrite¶
No overwrite is the default. POSIX creates the destination atomically with
linkat semantics and then removes only the owned staging name. Windows uses
handle-based SetFileInformationByHandle(FileRenameInfoEx) without the
replace flag. A concurrent destination causes a stable OUTPUT_EXISTS
failure. Exactly one no-overwrite publisher can win.
Overwrite¶
Replacement is enabled only when both the future-policy authority flag and the
operation-intent flag are true. An absent destination still uses the
no-overwrite primitive so a newly appearing object is not replaced without
inspection. A safely inspected existing destination is replaced atomically:
POSIX uses same-directory replace; Windows uses handle-based rename with
FILE_RENAME_FLAG_REPLACE_IF_EXISTS | FILE_RENAME_FLAG_POSIX_SEMANTICS.
The staged permission is the final permission; source or old-destination mode is not inherited.
Durability and ambiguous outcomes¶
POSIX performs file fsync, publication, staging-name cleanup, and containing
directory fsync. On macOS, where fsync only hands data to the drive, the
file barrier adds fcntl(F_FULLFSYNC) and the post-publication barrier issues
F_FULLFSYNC after the directory fsync to drain the drive cache; a
filesystem that refuses F_FULLFSYNC fails closed with the uncertain or
not-published outcome. Windows performs FlushFileBuffers before publication and
uses a write-through file handle plus a post-rename FlushFileBuffers metadata
barrier on local NTFS. Windows does not claim a portable POSIX directory-fsync
API.
Internal outcomes are:
NOT_PUBLISHED;PUBLISHED;PUBLISHED_DURABILITY_UNCERTAIN; andCLEANUP_INCOMPLETE.
A file-sync or promotion failure before publication leaves the old destination unchanged and permits abort. A failure of the post-publication durability barrier raises a typed uncertain error, keeps the destination, and prevents abort from claiming an aborted result. A failure removing a post-link staging name reports cleanup incomplete and never removes the destination.
Abort¶
Abort is idempotent. Before publication it closes the staging handle, verifies the staging identity, and removes only that owned name. It never removes the destination or source. After commit it is a no-op with respect to the destination. After an uncertain or cleanup-incomplete publication it may retry cleanup of a distinct owned staging name but never changes the outcome to aborted.
No destructor is relied upon for cleanup or correctness.
Errors and redaction¶
Internal filesystem errors use existing stable codes IO_FAILED,
OUTPUT_EXISTS, LIMIT_EXCEEDED, and INTERNAL_ERROR. Messages and reprs
contain no trusted root, relative destination, temporary name, source or
destination identity, staged bytes, or raw operating-system exception text.
Every operating-system failure inside the facade, including fstat, fsync,
and fchmod during construction, is converted to a typed error; an unexpected
exception during commit is converted to AtomicSinkError with the outcome
that matches whether promotion had happened. Errors survive pickle and
copy with their code, state, and outcome intact, so they can cross process
boundaries.
State diagram¶
stateDiagram-v2
[*] --> OPEN
OPEN --> OPEN: bounded write
OPEN --> FAILED: write or pre-publication commit failure
OPEN --> PUBLISHING: commit
PUBLISHING --> COMMITTED: publication and durability complete
PUBLISHING --> PUBLICATION_UNCERTAIN: post-publication durability failure
PUBLISHING --> CLEANUP_INCOMPLETE: post-publication cleanup/close failure
OPEN --> ABORTED: abort and owned cleanup
FAILED --> ABORTED: abort and owned cleanup
OPEN --> CLEANUP_INCOMPLETE: abort cleanup failure
FAILED --> CLEANUP_INCOMPLETE: abort cleanup failure
Unsupported behavior¶
Relative-path roots, missing parents, writable-by-other POSIX parents, remote or non-NTFS Windows roots, reparse/link parents, unverifiable owner-only permissions, unavailable atomic publication, and unavailable durability barriers fail closed. Network shares, cross-filesystem publication, backups, automatic source deletion, stale-temp scanning, and recovery scanning are not implemented.