From 20cbfd30d4ad8b134e39dc012d9919f55cb905a7 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Sat, 25 Apr 2026 21:11:00 -0700 Subject: [PATCH] add Dir.createFile() --- Dir.zig | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Dir.zig b/Dir.zig index 3b6084980a2a61ab14510b723cde155a77db937d..40ed994fdf3ae22704d6da8d6766ca18a25145d8 100644 --- a/Dir.zig +++ b/Dir.zig @@ -258,3 +258,24 @@ pub fn renameC(self: Dir, old: []const u8, new: []const u8) !void { new_buf[new.len] = 0; return rename(self, old_buf[0..old.len :0], new_buf[0..new.len :0]); } + +pub fn createFile(self: Dir, sub_path: [:0]const u8, flags: CreateFlags) !File { + var oflag: c_int = 0; + oflag |= if (flags.read) sys.O.RDWR else sys.O.WRONLY; + oflag |= sys.O.CREAT; + if (flags.truncate) oflag |= sys.O.TRUNC; + if (flags.exclusive) oflag |= sys.O.EXCL; + oflag |= sys.O.CLOEXEC; + return .{ .fd = @enumFromInt(try sys.openat(@intFromEnum(self.fd), sub_path.ptr, oflag)) }; +} + +pub const CreateFlags = packed struct { + /// Whether the file will be created with read access. + read: bool = false, + /// If the file already exists, and is a regular file, and the access mode allows writing, it will be truncated to length 0. + truncate: bool = true, + /// Ensures that this open call creates the file, otherwise causes `error.PathAlreadyExists` to be returned. + exclusive: bool = false, + /// The file system mode the file will be created with. + mode: File.Mode = 0o666, +}; -- 2.54.0