authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-12-16 01:49:06-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-01 17:51:20-07:00
logd71e6273b6acd766eea6c37b8f53ae03cb2bd9f6
treed9b8669290ec29705025fabce828ab927a8fb337
parent971593647205da15f4174ddd7e4039b1c2756ccc

std.fs.Dir.atomicFile: provide an option for make path

It's useful for the API to support creating the parent directory.

1 files changed, 11 insertions(+), 5 deletions(-)

lib/std/fs/Dir.zig+11-5
......@@ -2416,15 +2416,21 @@ fn copy_file(fd_in: posix.fd_t, fd_out: posix.fd_t, maybe_size: ?u64) CopyFileRa
24162416
24172417pub const AtomicFileOptions = struct {
24182418 mode: File.Mode = File.default_mode,
2419 make_path: bool = false,
24192420};
24202421
2421/// Directly access the `.file` field, and then call `AtomicFile.finish`
2422/// to atomically replace `dest_path` with contents.
2423/// Always call `AtomicFile.deinit` to clean up, regardless of whether `AtomicFile.finish` succeeded.
2424/// `dest_path` must remain valid until `AtomicFile.deinit` is called.
2422/// Directly access the `.file` field, and then call `AtomicFile.finish` to
2423/// atomically replace `dest_path` with contents.
2424/// Always call `AtomicFile.deinit` to clean up, regardless of whether
2425/// `AtomicFile.finish` succeeded. `dest_path` must remain valid until
2426/// `AtomicFile.deinit` is called.
24252427pub fn atomicFile(self: Dir, dest_path: []const u8, options: AtomicFileOptions) !AtomicFile {
24262428 if (fs.path.dirname(dest_path)) |dirname| {
2427 const dir = try self.openDir(dirname, .{});
2429 const dir = if (options.make_path)
2430 try self.makeOpenPath(dirname, .{})
2431 else
2432 try self.openDir(dirname, .{});
2433
24282434 return AtomicFile.init(fs.path.basename(dest_path), options.mode, dir, true);
24292435 } else {
24302436 return AtomicFile.init(dest_path, options.mode, self, false);