Skip to content

Write files safely everywhere: one robust temp-file + rename (+ optional fsync) helper in libntech #30

Open

Description

@djbclark

Not scheduled; recorded for later.

CFEngine writes files in several ad-hoc ways with uneven safety. Found while reviewing --simulate-json (#27):

Writer Temp file fsync Rename into place
copy_from (libpromises/files_copy.c) – no (do_sync = false) –
edit_line save (libpromises/item_lib.c, SaveItemListAsFile) fixed name <file>.cfedited, fopen overwrites whatever is there no yes
lock DB backup (libpromises/locks.c) fixed name, O_TRUNC yes (failure is only a warning) yes
--simulate-json (#27, cf-agent/simulate_mode.c) unique name + O_EXCL, never overwrites no yes

With a fixed temp name, anything already at <file>.cfedited gets overwritten. That includes a symlink planted there, since fopen follows it. Without fsync, a power loss can leave an empty or truncated file behind.

To do:

  1. Search more deeply for this pattern before designing anything. The table comes from a quick grep for rename( and fsync(. Also look for fopen(..., "w") / O_TRUNC writes of state and config files, FileWriteOver(), CopyRegularFileDisk*(), WriteJson*-style state writers, and the other daemons (cf-serverd, cf-execd, cf-hub if present).
  2. Add one robust helper to libntech, e.g. FileWriteAtomic(path, data, len, mode, flags):
    a. a unique temp name in the same directory, created with O_EXCL;
    b. a full write;
    c. an optional fsync of the file, plus of the directory after the rename;
    d. rename() into place;
    e. the temp file removed on every failure path;
    f. a clear policy on whether to keep the original owner and mode.
  3. Call it from every writer found, one small upstream-sized PR per call site or per theme.

Upstream constraints apply (~/src/cfengine-all/AGENTS.md): small PRs, and prose proportional to code.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions