authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-04-25 21:11:00 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-04-25 21:11:00 -07:00
log20cbfd30d4ad8b134e39dc012d9919f55cb905a7
treec35cb8c872da461677433e22ba968a03a7fbe947
parentbbe0292b79b033440cc5311ac6bafafda2b1b771
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

add Dir.createFile()


1 files changed, 21 insertions(+), 0 deletions(-)

Dir.zig+21
......@@ -258,3 +258,24 @@ pub fn renameC(self: Dir, old: []const u8, new: []const u8) !void {
258258 new_buf[new.len] = 0;
259259 return rename(self, old_buf[0..old.len :0], new_buf[0..new.len :0]);
260260}
261
262pub fn createFile(self: Dir, sub_path: [:0]const u8, flags: CreateFlags) !File {
263 var oflag: c_int = 0;
264 oflag |= if (flags.read) sys.O.RDWR else sys.O.WRONLY;
265 oflag |= sys.O.CREAT;
266 if (flags.truncate) oflag |= sys.O.TRUNC;
267 if (flags.exclusive) oflag |= sys.O.EXCL;
268 oflag |= sys.O.CLOEXEC;
269 return .{ .fd = @enumFromInt(try sys.openat(@intFromEnum(self.fd), sub_path.ptr, oflag)) };
270}
271
272pub const CreateFlags = packed struct {
273 /// Whether the file will be created with read access.
274 read: bool = false,
275 /// If the file already exists, and is a regular file, and the access mode allows writing, it will be truncated to length 0.
276 truncate: bool = true,
277 /// Ensures that this open call creates the file, otherwise causes `error.PathAlreadyExists` to be returned.
278 exclusive: bool = false,
279 /// The file system mode the file will be created with.
280 mode: File.Mode = 0o666,
281};