|
|
|
|
@@ -1665,7 +1665,9 @@ pub const UnlinkError = error{
|
|
|
|
|
/// Delete a name and possibly the file it refers to.
|
|
|
|
|
/// See also `unlinkC`.
|
|
|
|
|
pub fn unlink(file_path: []const u8) UnlinkError!void {
|
|
|
|
|
if (builtin.os.tag == .windows) {
|
|
|
|
|
if (builtin.os.tag == .wasi) {
|
|
|
|
|
@compileError("unlink is not supported in WASI; use unlinkat instead");
|
|
|
|
|
} else if (builtin.os.tag == .windows) {
|
|
|
|
|
const file_path_w = try windows.sliceToPrefixedFileW(file_path);
|
|
|
|
|
return windows.DeleteFileW(file_path_w.span().ptr);
|
|
|
|
|
} else {
|
|
|
|
|
@@ -1722,6 +1724,8 @@ pub fn unlinkat(dirfd: fd_t, file_path: []const u8, flags: u32) UnlinkatError!vo
|
|
|
|
|
|
|
|
|
|
pub const unlinkatC = @compileError("deprecated: renamed to unlinkatZ");
|
|
|
|
|
|
|
|
|
|
/// WASI-only. Same as `unlinkat` but targeting WASI.
|
|
|
|
|
/// See also `unlinkat`.
|
|
|
|
|
pub fn unlinkatWasi(dirfd: fd_t, file_path: []const u8, flags: u32) UnlinkatError!void {
|
|
|
|
|
const remove_dir = (flags & AT_REMOVEDIR) != 0;
|
|
|
|
|
const res = if (remove_dir)
|
|
|
|
|
@@ -1868,7 +1872,9 @@ const RenameError = error{
|
|
|
|
|
|
|
|
|
|
/// Change the name or location of a file.
|
|
|
|
|
pub fn rename(old_path: []const u8, new_path: []const u8) RenameError!void {
|
|
|
|
|
if (builtin.os.tag == .windows) {
|
|
|
|
|
if (builtin.os.tag == .wasi) {
|
|
|
|
|
@compileError("rename is not supported in WASI; use renameat instead");
|
|
|
|
|
} else if (builtin.os.tag == .windows) {
|
|
|
|
|
const old_path_w = try windows.sliceToPrefixedFileW(old_path);
|
|
|
|
|
const new_path_w = try windows.sliceToPrefixedFileW(new_path);
|
|
|
|
|
return renameW(old_path_w.span().ptr, new_path_w.span().ptr);
|
|
|
|
|
@@ -1939,7 +1945,8 @@ pub fn renameat(
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Same as `renameat` expect only WASI.
|
|
|
|
|
/// WASI-only. Same as `renameat` expect targeting WASI.
|
|
|
|
|
/// See also `renameat`.
|
|
|
|
|
pub fn renameatWasi(old_dir_fd: fd_t, old_path: []const u8, new_dir_fd: fd_t, new_path: []const u8) RenameError!void {
|
|
|
|
|
switch (wasi.path_rename(old_dir_fd, old_path.ptr, old_path.len, new_dir_fd, new_path.ptr, new_path.len)) {
|
|
|
|
|
wasi.ESUCCESS => return,
|
|
|
|
|
@@ -2144,7 +2151,9 @@ pub fn mkdiratW(dir_fd: fd_t, sub_path_w: [*:0]const u16, mode: u32) MakeDirErro
|
|
|
|
|
/// Create a directory.
|
|
|
|
|
/// `mode` is ignored on Windows.
|
|
|
|
|
pub fn mkdir(dir_path: []const u8, mode: u32) MakeDirError!void {
|
|
|
|
|
if (builtin.os.tag == .windows) {
|
|
|
|
|
if (builtin.os.tag == .wasi) {
|
|
|
|
|
@compileError("mkdir is not supported in WASI; use mkdirat instead");
|
|
|
|
|
} else if (builtin.os.tag == .windows) {
|
|
|
|
|
const sub_dir_handle = try windows.CreateDirectory(null, dir_path, null);
|
|
|
|
|
windows.CloseHandle(sub_dir_handle);
|
|
|
|
|
return;
|
|
|
|
|
@@ -2197,7 +2206,9 @@ pub const DeleteDirError = error{
|
|
|
|
|
|
|
|
|
|
/// Deletes an empty directory.
|
|
|
|
|
pub fn rmdir(dir_path: []const u8) DeleteDirError!void {
|
|
|
|
|
if (builtin.os.tag == .windows) {
|
|
|
|
|
if (builtin.os.tag == .wasi) {
|
|
|
|
|
@compileError("rmdir is not supported in WASI; use unlinkat instead");
|
|
|
|
|
} else if (builtin.os.tag == .windows) {
|
|
|
|
|
const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);
|
|
|
|
|
return windows.RemoveDirectoryW(dir_path_w.span().ptr);
|
|
|
|
|
} else {
|
|
|
|
|
@@ -2246,7 +2257,9 @@ pub const ChangeCurDirError = error{
|
|
|
|
|
/// Changes the current working directory of the calling process.
|
|
|
|
|
/// `dir_path` is recommended to be a UTF-8 encoded string.
|
|
|
|
|
pub fn chdir(dir_path: []const u8) ChangeCurDirError!void {
|
|
|
|
|
if (builtin.os.tag == .windows) {
|
|
|
|
|
if (builtin.os.tag == .wasi) {
|
|
|
|
|
@compileError("chdir is not supported in WASI");
|
|
|
|
|
} else if (builtin.os.tag == .windows) {
|
|
|
|
|
const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);
|
|
|
|
|
@compileError("TODO implement chdir for Windows");
|
|
|
|
|
} else {
|
|
|
|
|
@@ -2310,9 +2323,11 @@ pub const ReadLinkError = error{
|
|
|
|
|
/// Read value of a symbolic link.
|
|
|
|
|
/// The return value is a slice of `out_buffer` from index 0.
|
|
|
|
|
pub fn readlink(file_path: []const u8, out_buffer: []u8) ReadLinkError![]u8 {
|
|
|
|
|
if (builtin.os.tag == .windows) {
|
|
|
|
|
if (builtin.os.tag == .wasi) {
|
|
|
|
|
@compileError("readlink is not supported in WASI; use readlinkat instead");
|
|
|
|
|
} else if (builtin.os.tag == .windows) {
|
|
|
|
|
const file_path_w = try windows.sliceToPrefixedFileW(file_path);
|
|
|
|
|
@compileError("TODO implement readlink for Windows");
|
|
|
|
|
return readlinkW(file_path_w.span().ptr, out_buffer);
|
|
|
|
|
} else {
|
|
|
|
|
const file_path_c = try toPosixPath(file_path);
|
|
|
|
|
return readlinkZ(&file_path_c, out_buffer);
|
|
|
|
|
@@ -2321,11 +2336,17 @@ pub fn readlink(file_path: []const u8, out_buffer: []u8) ReadLinkError![]u8 {
|
|
|
|
|
|
|
|
|
|
pub const readlinkC = @compileError("deprecated: renamed to readlinkZ");
|
|
|
|
|
|
|
|
|
|
/// Windows-only. Same as `readlink` expecte `file_path` is null-terminated, WTF16 encoded.
|
|
|
|
|
/// Seel also `readlinkZ`.
|
|
|
|
|
pub fn readlinkW(file_path: [*:0]const u16, out_buffer: []u8) ReadLinkError![]u8 {
|
|
|
|
|
@compileError("TODO implement readlink for Windows");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Same as `readlink` except `file_path` is null-terminated.
|
|
|
|
|
pub fn readlinkZ(file_path: [*:0]const u8, out_buffer: []u8) ReadLinkError![]u8 {
|
|
|
|
|
if (builtin.os.tag == .windows) {
|
|
|
|
|
const file_path_w = try windows.cStrToPrefixedFileW(file_path);
|
|
|
|
|
@compileError("TODO implement readlink for Windows");
|
|
|
|
|
return readlinkW(file_path_w.span().ptr, out_buffer);
|
|
|
|
|
}
|
|
|
|
|
const rc = system.readlink(file_path, out_buffer.ptr, out_buffer.len);
|
|
|
|
|
switch (errno(rc)) {
|
|
|
|
|
@@ -3055,6 +3076,7 @@ pub const FStatError = error{
|
|
|
|
|
AccessDenied,
|
|
|
|
|
} || UnexpectedError;
|
|
|
|
|
|
|
|
|
|
/// Return information about a file descriptor.
|
|
|
|
|
pub fn fstat(fd: fd_t) FStatError!Stat {
|
|
|
|
|
if (builtin.os.tag == .wasi) {
|
|
|
|
|
var stat: wasi.filestat_t = undefined;
|
|
|
|
|
@@ -3067,6 +3089,9 @@ pub fn fstat(fd: fd_t) FStatError!Stat {
|
|
|
|
|
else => |err| return unexpectedErrno(err),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (builtin.os.tag == .windows) {
|
|
|
|
|
@compileError("fstat is not yet implemented on Windows");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var stat: Stat = undefined;
|
|
|
|
|
switch (errno(system.fstat(fd, &stat))) {
|
|
|
|
|
@@ -3081,13 +3106,42 @@ pub fn fstat(fd: fd_t) FStatError!Stat {
|
|
|
|
|
|
|
|
|
|
pub const FStatAtError = FStatError || error{ NameTooLong, FileNotFound };
|
|
|
|
|
|
|
|
|
|
/// Similar to `fstat`, but returns stat of a resource pointed to by `pathname`
|
|
|
|
|
/// which is relative to `dirfd` handle.
|
|
|
|
|
/// See also `fstatatZ` and `fstatatWasi`.
|
|
|
|
|
pub fn fstatat(dirfd: fd_t, pathname: []const u8, flags: u32) FStatAtError!Stat {
|
|
|
|
|
const pathname_c = try toPosixPath(pathname);
|
|
|
|
|
return fstatatZ(dirfd, &pathname_c, flags);
|
|
|
|
|
if (builtin.os.tag == .wasi) {
|
|
|
|
|
return fstatatWasi(dirfd, pathname, flags);
|
|
|
|
|
} else if (builtin.os.tag == .windows) {
|
|
|
|
|
@compileError("fstatat is not yet implemented on Windows");
|
|
|
|
|
} else {
|
|
|
|
|
const pathname_c = try toPosixPath(pathname);
|
|
|
|
|
return fstatatZ(dirfd, &pathname_c, flags);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub const fstatatC = @compileError("deprecated: renamed to fstatatZ");
|
|
|
|
|
|
|
|
|
|
/// WASI-only. Same as `fstatat` but targeting WASI.
|
|
|
|
|
/// See also `fstatat`.
|
|
|
|
|
pub fn fstatatWasi(dirfd: fd_t, pathname: []const u8, flags: u32) FStatAtError!Stat {
|
|
|
|
|
var stat: wasi.filestat_t = undefined;
|
|
|
|
|
switch (wasi.path_filestat_get(dirfd, flags, pathname.ptr, pathname.len, &stat)) {
|
|
|
|
|
wasi.ESUCCESS => return Stat.fromFilestat(stat),
|
|
|
|
|
wasi.EINVAL => unreachable,
|
|
|
|
|
wasi.EBADF => unreachable, // Always a race condition.
|
|
|
|
|
wasi.ENOMEM => return error.SystemResources,
|
|
|
|
|
wasi.EACCES => return error.AccessDenied,
|
|
|
|
|
wasi.EFAULT => unreachable,
|
|
|
|
|
wasi.ENAMETOOLONG => return error.NameTooLong,
|
|
|
|
|
wasi.ENOENT => return error.FileNotFound,
|
|
|
|
|
wasi.ENOTDIR => return error.FileNotFound,
|
|
|
|
|
else => |err| return unexpectedErrno(err),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Same as `fstatat` but `pathname` is null-terminated.
|
|
|
|
|
/// See also `fstatat`.
|
|
|
|
|
pub fn fstatatZ(dirfd: fd_t, pathname: [*:0]const u8, flags: u32) FStatAtError!Stat {
|
|
|
|
|
var stat: Stat = undefined;
|
|
|
|
|
switch (errno(system.fstatat(dirfd, pathname, &stat, flags))) {
|
|
|
|
|
|