Skip to content

Make POSIX owner-permission file writes atomic via temp file + rename #1254

Description

@palas

Since #1248, the POSIX implementation of handleFileForWritingWithOwnerPermission opens the target file with O_TRUNC, so overwriting a file with shorter content no longer leaves stale trailing bytes. However, the write is still not atomic: if it fails midway (crash, full disk, …), the previous file contents are already destroyed and a truncated file is left behind.

The Windows implementation already avoids this by writing to a freshly created temporary file and renaming it over the target. Doing the same on POSIX (creating a temp file with 0600 permissions in the target directory, write the contents, then rename(2)) would keep the old file intact until the new one is complete.

The currently documented behaviour that pre-existing files keep their permission bits would be lost, since the replacement file would always end up 0600. The haddocks (e.g. writeFileTextEnvelopeWithOwnerPermissions) will need updating to match.

Affected functions: writeFileTextEnvelopeWithOwnerPermissions, writeByteStringFileWithOwnerPermissions, writeLazyByteStringFileWithOwnerPermissions, writeTextFileWithOwnerPermissions, and writeSecrets.

Originally posted by @carbolymer in #1248 (comment)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions