Dir.atomicFile replacement?

Porting some code away from fs.* i noticed that fs.Dir.atomicFile seems to have no equivalent in the new api?

Is there any way to replicate this with the new API without creating a temporary file with known_directories or similar?

1 Like

It’s now called std.Io.Dir.createFileAtomic, and you use it something like this:

var af = try dir.createFileAtomic(io, path, .{ .replace = true });
defer af.deinit(io);

var buf: [4096]u8 = undefined;
var writer = af.file.writer(io, &buf);

// do stuff with &writer.interface
try writer.interface.flush();
try af.replace(io);

If you know that the destination file shouldn’t exist yet, then you can use the “link” strategy instead of replace (see the docs for details)

3 Likes