Skip to content

Commit 18c9535

Browse files
committed
Rename os.fdPath to os.getFdPath and make it pub
Also, ensure the docs clearly state that `os.getFdPath` and `os.realpathat` are *not* supported on all hosts.
1 parent 56978f8 commit 18c9535

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

lib/std/fs.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,9 @@ pub const Dir = struct {
931931
return self.openDir(sub_path, open_dir_options);
932932
}
933933

934-
/// This function behaves differently on different hosts.
934+
/// This function behaves differently on different hosts, and since it uses `std.os.getFdPath`
935+
/// underneath, it is not universally supported on all hosts.
936+
/// Currently supported hosts are: Linux, macOS, Windows, and WASI.
935937
/// In WASI, returns the canonicalized relative pathname of `pathname` relative
936938
/// to this `Dir`. If `pathname` is absolute, or an attempt is made at
937939
/// escaping beyond this `Dir`, return `error.AccessDenied`.

lib/std/os.zig

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3985,7 +3985,7 @@ pub const realpathC = @compileError("deprecated: renamed realpathZ");
39853985
/// This function is very host-specific and is not universally supported by all hosts.
39863986
/// For example, while it generally works on Linux, macOS or Windows, it is unsupported
39873987
/// on FreeBSD, or WASI.
3988-
fn fdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
3988+
fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
39893989
switch (builtin.os.tag) {
39903990
.windows => {
39913991
var wide_buf: [windows.PATH_MAX_WIDE]u16 = undefined;
@@ -4037,7 +4037,7 @@ pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealP
40374037
};
40384038
defer close(fd);
40394039

4040-
return fdPath(fd, out_buffer);
4040+
return getFdPath(fd, out_buffer);
40414041
}
40424042
const result_path = std.c.realpath(pathname, out_buffer) orelse switch (std.c._errno().*) {
40434043
EINVAL => unreachable,
@@ -4069,14 +4069,19 @@ pub fn realpathW(pathname: [*:0]const u16, out_buffer: *[MAX_PATH_BYTES]u8) Real
40694069
);
40704070
defer windows.CloseHandle(h_file);
40714071

4072-
return fdPath(h_file, out_buffer);
4072+
return getFdPath(h_file, out_buffer);
40734073
}
40744074

4075-
/// This function will depend differently on different hosts.
4076-
/// In WASI, it calls `realpathatWasi` which observes WASI capability-oriented
4077-
/// security model and returns `error.AccessDenied` if the `pathname` is absolute
4078-
/// or an attempt is made at escaping beyond the input `fd` file descriptor.
4079-
/// On all other hosts, this function is similar to `realpath`, however, returns
4075+
/// This function will depend differently on different hosts. Since underneath it
4076+
/// uses `getFdPath` (with the exception of WASI) to query the host for a true
4077+
/// canonical path to handle `fd`, this function, much like `getFdPath` is not universally
4078+
/// supported on all hosts.
4079+
/// Currently, the supported hosts are: Linux, macOS, Windows, and WASI.
4080+
/// In WASI, it does not issue any `getFdPath`-like calls. Instead, it canonicalizes
4081+
/// the input path `pathname` upto the handle `fd` while observing WASI capability-oriented
4082+
/// security model. If `pathname` is absolute, return `error.AbsolutePath`. See `realpathatWasi`
4083+
/// for more information on how it works in WASI.
4084+
/// On all other supported hosts, this function is similar to `realpath`, however, returns
40804085
/// the canonicalized absolute pathname of a `pathname` relative to a file descriptor
40814086
/// `fd`. If `pathname` is an absolute path, ignores `fd` and reverts to calling
40824087
/// `realpath` on the `pathname` argument. In particular, on Unix, if `fd` was
@@ -4119,7 +4124,7 @@ pub fn realpathatZ(fd: fd_t, pathname: [*:0]const u8, out_buffer: *[MAX_PATH_BYT
41194124
};
41204125
};
41214126
} else {
4122-
fd_path = try fdPath(fd, &buffer);
4127+
fd_path = try getFdPath(fd, &buffer);
41234128
}
41244129

41254130
const total_len = fd_path.len + mem.lenZ(pathname) + 1; // +1 to account for path separator
@@ -4145,7 +4150,7 @@ pub fn realpathatW(fd: fd_t, pathname: [*:0]const u16, out_buffer: *[MAX_PATH_BY
41454150
}
41464151

41474152
var buffer: [MAX_PATH_BYTES]u8 = undefined;
4148-
const fd_path = try fdPath(fd, &buffer);
4153+
const fd_path = try getFdPath(fd, &buffer);
41494154

41504155
// Convert UTF16LE to UTF8
41514156
var pathname_u8: [MAX_PATH_BYTES]u8 = undefined;

0 commit comments

Comments
 (0)