| ... | @@ -258,3 +258,24 @@ pub fn renameC(self: Dir, old: []const u8, new: []const u8) !void { | ... | @@ -258,3 +258,24 @@ pub fn renameC(self: Dir, old: []const u8, new: []const u8) !void { |
| 258 | new_buf[new.len] = 0; | 258 | new_buf[new.len] = 0; |
| 259 | return rename(self, old_buf[0..old.len :0], new_buf[0..new.len :0]); | 259 | return rename(self, old_buf[0..old.len :0], new_buf[0..new.len :0]); |
| 260 | } | 260 | } |
| | 261 | |
| | 262 | pub 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 | |
| | 272 | pub 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 | }; |