feature. See also
. The project being documented here (as the example) is the Zig library itself.
Dispatch.dirCreateFile
fn dirCreateFile(
userdata: ?*anyopaque,
dir: Dir,
sub_path: []const u8,
flags: Dir.CreateFileOptions,
) File.OpenError!File
File
Code
fn dirCreateFile(
userdata: ?*anyopaque,
dir: Dir,
sub_path: []const u8,
flags: Dir.CreateFileOptions,
) File.OpenError!File {
const ev: *Evented = @ptrCast(@alignCast(userdata));
_ = ev;
var path_buffer: [c.PATH_MAX]u8 = undefined;
const sub_path_posix = try pathToPosix(sub_path, &path_buffer);
const os_flags: c.O = .{
.ACCMODE = if (flags.read) .RDWR else .WRONLY,
.NONBLOCK = flags.lock == .none or flags.lock_nonblocking,
.SHLOCK = flags.lock == .shared,
.EXLOCK = flags.lock == .exclusive,
.CREAT = true,
.TRUNC = flags.truncate,
.EXCL = flags.exclusive,
.CLOEXEC = true,
};
const fd: c.fd_t = while (true) {
const rc = c.openat(dir.handle, sub_path_posix, os_flags, flags.permissions.toMode());
switch (c.errno(rc)) {
.SUCCESS => break @intCast(rc),
.INTR => {},
.FAULT => |err| return errnoBug(err),
.INVAL => return error.BadPathName,
.BADF => |err| return errnoBug(err),
.ACCES => return error.AccessDenied,
.FBIG => return error.FileTooBig,
.OVERFLOW => return error.FileTooBig,
.ISDIR => return error.IsDir,
.LOOP => return error.SymLinkLoop,
.MFILE => return error.ProcessFdQuotaExceeded,
.NAMETOOLONG => return error.NameTooLong,
.NFILE => return error.SystemFdQuotaExceeded,
.NODEV => return error.NoDevice,
.NOENT => return error.FileNotFound,
.NOMEM => return error.SystemResources,
.NOSPC => return error.NoSpaceLeft,
.NOTDIR => return error.NotDir,
.PERM => return error.PermissionDenied,
.EXIST => return error.PathAlreadyExists,
.BUSY => return error.DeviceBusy,
.OPNOTSUPP => return error.FileLocksUnsupported,
.AGAIN => return error.WouldBlock,
.TXTBSY => return error.FileBusy,
.ROFS => return error.ReadOnlyFileSystem,
.NXIO => return error.NoDevice,
.ILSEQ => return error.BadPathName,
else => |err| return unexpectedErrno(err),
}
};
errdefer closeFd(fd);
return .{
.handle = fd,
.flags = .{ .nonblocking = os_flags.NONBLOCK },
};
}