Isn't it weird that language stdlibs don't have support for atomically replacing files? Why is there no C++ or Rust stdlib feature which, on UNIX, implements the "create temporary file on the same volume, write to that file, rename into place" pattern

It's not like it's an incredibly difficult piece to write by hand, but there are subtle gotchas. Given that it ought to be the default way programs replace e.g state files, it ought to be supported by the stdlib, no?

#programming #linux #unix

@mort

It's an accident of history, really.

The whole idea of atomically updating critical files so that a running system never sees them in a partly written state only really took hold in the 1990s.

Whereas editing configuration files with text editors that truncated and rewrote in place was theretofore generally the norm, and one had to have extra tools like vipw to make atomicity guarantees.

C standard libraries had stabilized the decade before. And the C++ libraries effectively took the C libraries as templates for what functionality to offer.

The rather sad part of this is that Microsoft had System.IO.File::Replace() in the .NET library, based upon the ReplaceFile() Win32 function, years ago.

https://learn.microsoft.com/en-gb/dotnet/api/system.io.file.replace

https://learn.microsoft.com/en-gb/windows/win32/api/winbase/nf-winbase-replacefilew

#CLanguage #Unix #ComputerHistory #CPlusPlus #DotNet #Win32 #Unix

File.Replace Method (System.IO)

Replaces the contents of a specified file with the contents of another file, deleting the original file and creating a backup of the replaced file.