mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 14:23:09 +00:00
std.fs.Dir.atomicFile: provide an option for make path
It's useful for the API to support creating the parent directory.
This commit is contained in:
parent
9715936472
commit
d71e6273b6
@ -2416,15 +2416,21 @@ fn copy_file(fd_in: posix.fd_t, fd_out: posix.fd_t, maybe_size: ?u64) CopyFileRa
|
|||||||
|
|
||||||
pub const AtomicFileOptions = struct {
|
pub const AtomicFileOptions = struct {
|
||||||
mode: File.Mode = File.default_mode,
|
mode: File.Mode = File.default_mode,
|
||||||
|
make_path: bool = false,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Directly access the `.file` field, and then call `AtomicFile.finish`
|
/// Directly access the `.file` field, and then call `AtomicFile.finish` to
|
||||||
/// to atomically replace `dest_path` with contents.
|
/// atomically replace `dest_path` with contents.
|
||||||
/// Always call `AtomicFile.deinit` to clean up, regardless of whether `AtomicFile.finish` succeeded.
|
/// Always call `AtomicFile.deinit` to clean up, regardless of whether
|
||||||
/// `dest_path` must remain valid until `AtomicFile.deinit` is called.
|
/// `AtomicFile.finish` succeeded. `dest_path` must remain valid until
|
||||||
|
/// `AtomicFile.deinit` is called.
|
||||||
pub fn atomicFile(self: Dir, dest_path: []const u8, options: AtomicFileOptions) !AtomicFile {
|
pub fn atomicFile(self: Dir, dest_path: []const u8, options: AtomicFileOptions) !AtomicFile {
|
||||||
if (fs.path.dirname(dest_path)) |dirname| {
|
if (fs.path.dirname(dest_path)) |dirname| {
|
||||||
const dir = try self.openDir(dirname, .{});
|
const dir = if (options.make_path)
|
||||||
|
try self.makeOpenPath(dirname, .{})
|
||||||
|
else
|
||||||
|
try self.openDir(dirname, .{});
|
||||||
|
|
||||||
return AtomicFile.init(fs.path.basename(dest_path), options.mode, dir, true);
|
return AtomicFile.init(fs.path.basename(dest_path), options.mode, dir, true);
|
||||||
} else {
|
} else {
|
||||||
return AtomicFile.init(dest_path, options.mode, self, false);
|
return AtomicFile.init(dest_path, options.mode, self, false);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user