init
Note that the Dir.atomicFile API may be more handy than this lower-level function.
Function parameters
Parameters
Note that the `Dir.atomicFile` API may be more handy than this lower-level function.
Functions
- init
- Note that the `Dir.atomicFile` API may be more handy than this lower-level function.
- deinit
- Always call deinit, even after a successful finish().
- renameIntoPlace
- On Windows, this function introduces a period of time where some file
- finish
- Combination of `flush` followed by `renameIntoPlace`.
Error sets in this namespace
Error Sets
Source
Implementation
pub fn init(
dest_basename: []const u8,
mode: File.Mode,
dir: Dir,
close_dir_on_deinit: bool,
write_buffer: []u8,
) InitError!AtomicFile {
while (true) {
const random_integer = std.crypto.random.int(u64);
const tmp_sub_path = std.fmt.hex(random_integer);
const file = dir.createFile(&tmp_sub_path, .{ .mode = mode, .exclusive = true }) catch |err| switch (err) {
error.PathAlreadyExists => continue,
else => |e| return e,
};
return .{
.file_writer = file.writer(write_buffer),
.random_integer = random_integer,
.dest_basename = dest_basename,
.file_open = true,
.file_exists = true,
.close_dir_on_deinit = close_dir_on_deinit,
.dir = dir,
};
}
}