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)
Since #1248, the POSIX implementation of
handleFileForWritingWithOwnerPermissionopens the target file withO_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
0600permissions in the target directory, write the contents, thenrename(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, andwriteSecrets.Originally posted by @carbolymer in #1248 (comment)