Skip to content

Commit 680499b

Browse files
committed
stdlib std.os: Rename RelativePath to RelativePathWasi
1 parent f357598 commit 680499b

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

lib/std/os.zig

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,11 @@ pub const LOG = struct {
219219
pub const DEBUG = 7;
220220
};
221221

222-
pub const RelativePath = struct {
222+
/// An fd-relative file path
223+
///
224+
/// This is currently only used for WASI-specific functionality, but the concept
225+
/// is the same as the dirfd/pathname pairs in the `*at(...)` POSIX functions.
226+
pub const RelativePathWasi = struct {
223227
/// Handle to directory
224228
dir_fd: fd_t,
225229
/// Path to resource within `dir_fd`.
@@ -1425,7 +1429,7 @@ var wasi_cwd = if (builtin.os.tag == .wasi and !builtin.link_libc) struct {
14251429
// Memory buffer for storing the relative portion of the CWD
14261430
path_buffer: [MAX_PATH_BYTES]u8 = undefined,
14271431
// Current Working Directory, stored as an fd_t and a relative path
1428-
cwd: ?RelativePath = null,
1432+
cwd: ?RelativePathWasi = null,
14291433
// Preopen associated with `cwd`, if any
14301434
cwd_preopen: ?Preopen = null,
14311435
}{} else undefined;
@@ -1451,7 +1455,7 @@ pub fn initPreopensWasi(alloc: Allocator, cwd_init: ?[]const u8) !void {
14511455
const preopen = wasi_cwd.preopens.?.findContaining(.{ .Dir = cwd });
14521456
if (preopen) |po| {
14531457
wasi_cwd.cwd_preopen = po.base;
1454-
wasi_cwd.cwd = RelativePath{
1458+
wasi_cwd.cwd = RelativePathWasi{
14551459
.dir_fd = po.base.fd,
14561460
.relative_path = po.relative_path,
14571461
};
@@ -1470,13 +1474,13 @@ pub fn initPreopensWasi(alloc: Allocator, cwd_init: ?[]const u8) !void {
14701474
///
14711475
/// For absolute paths, this automatically searches among available Preopens to find
14721476
/// a match. For relative paths, it uses the "emulated" CWD.
1473-
pub fn resolvePathWasi(path: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) !RelativePath {
1474-
// Note: Due to WASI's "sandboxed" file handles, operations with this RelativePath
1477+
pub fn resolvePathWasi(path: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) !RelativePathWasi {
1478+
// Note: Due to WASI's "sandboxed" file handles, operations with this RelativePathWasi
14751479
// will fail if the relative path navigates outside of `dir_fd` using ".."
14761480
return resolvePathAndGetWasiPreopen(path, null, out_buffer);
14771481
}
14781482

1479-
fn resolvePathAndGetWasiPreopen(path: []const u8, preopen: ?*?Preopen, out_buffer: *[MAX_PATH_BYTES]u8) !RelativePath {
1483+
fn resolvePathAndGetWasiPreopen(path: []const u8, preopen: ?*?Preopen, out_buffer: *[MAX_PATH_BYTES]u8) !RelativePathWasi {
14801484
var allocator = std.heap.FixedBufferAllocator.init(out_buffer);
14811485
var alloc = allocator.allocator();
14821486

@@ -1492,7 +1496,7 @@ fn resolvePathAndGetWasiPreopen(path: []const u8, preopen: ?*?Preopen, out_buffe
14921496
const rel_path = if (path.len > fd_end + 1) path[fd_end + 1 ..] else ".";
14931497

14941498
if (preopen) |p| p.* = wasi_cwd.preopens.?.findByFd(fd);
1495-
return RelativePath{
1499+
return RelativePathWasi{
14961500
.dir_fd = fd,
14971501
.relative_path = alloc.dupe(u8, rel_path) catch return error.NameTooLong,
14981502
};
@@ -1504,7 +1508,7 @@ fn resolvePathAndGetWasiPreopen(path: []const u8, preopen: ?*?Preopen, out_buffe
15041508

15051509
if (preopen_uri) |po| {
15061510
if (preopen) |p| p.* = po.base;
1507-
return RelativePath{
1511+
return RelativePathWasi{
15081512
.dir_fd = po.base.fd,
15091513
.relative_path = po.relative_path,
15101514
};
@@ -1529,7 +1533,7 @@ fn resolvePathAndGetWasiPreopen(path: []const u8, preopen: ?*?Preopen, out_buffe
15291533
const resolved_relative_path = resolved_path[1..];
15301534

15311535
if (preopen) |p| p.* = wasi_cwd.cwd_preopen;
1532-
return RelativePath{
1536+
return RelativePathWasi{
15331537
.dir_fd = cwd.dir_fd,
15341538
.relative_path = resolved_relative_path,
15351539
};
@@ -1568,6 +1572,7 @@ pub fn openat(dir_fd: fd_t, file_path: []const u8, flags: u32, mode: mode_t) Ope
15681572
return openatZ(dir_fd, &file_path_c, flags, mode);
15691573
}
15701574

1575+
/// A struct to contain all lookup/rights flags accepted by `wasi.path_open`
15711576
const WasiOpenOptions = struct {
15721577
oflags: wasi.oflags_t,
15731578
lookup_flags: wasi.lookupflags_t,
@@ -2232,8 +2237,8 @@ pub fn linkat(
22322237
var resolve_olddir: bool = (olddir == wasi.AT.FDCWD or fs.path.isAbsolute(oldpath));
22332238
var resolve_newdir: bool = (newdir == wasi.AT.FDCWD or fs.path.isAbsolute(newpath));
22342239

2235-
var old: RelativePath = .{ .dir_fd = olddir, .relative_path = oldpath };
2236-
var new: RelativePath = .{ .dir_fd = newdir, .relative_path = newpath };
2240+
var old: RelativePathWasi = .{ .dir_fd = olddir, .relative_path = oldpath };
2241+
var new: RelativePathWasi = .{ .dir_fd = newdir, .relative_path = newpath };
22372242

22382243
// Resolve absolute or CWD-relative paths to a path within a Preopen
22392244
if (resolve_olddir or resolve_newdir) {
@@ -2257,7 +2262,7 @@ pub fn linkat(
22572262

22582263
/// WASI-only. The same as `linkat` but targeting WASI.
22592264
/// See also `linkat`.
2260-
pub fn linkatWasi(old: RelativePath, new: RelativePath, flags: i32) LinkatError!void {
2265+
pub fn linkatWasi(old: RelativePathWasi, new: RelativePathWasi, flags: i32) LinkatError!void {
22612266
var old_flags: wasi.lookupflags_t = 0;
22622267
// TODO: Why is this not defined in wasi-libc?
22632268
if (flags & linux.AT.SYMLINK_FOLLOW != 0) old_flags |= wasi.LOOKUP_SYMLINK_FOLLOW;
@@ -2545,8 +2550,8 @@ pub fn renameat(
25452550
var resolve_old: bool = (old_dir_fd == wasi.AT.FDCWD or fs.path.isAbsolute(old_path));
25462551
var resolve_new: bool = (new_dir_fd == wasi.AT.FDCWD or fs.path.isAbsolute(new_path));
25472552

2548-
var old: RelativePath = .{ .dir_fd = old_dir_fd, .relative_path = old_path };
2549-
var new: RelativePath = .{ .dir_fd = new_dir_fd, .relative_path = new_path };
2553+
var old: RelativePathWasi = .{ .dir_fd = old_dir_fd, .relative_path = old_path };
2554+
var new: RelativePathWasi = .{ .dir_fd = new_dir_fd, .relative_path = new_path };
25502555

25512556
// Resolve absolute or CWD-relative paths to a path within a Preopen
25522557
if (resolve_old or resolve_new) {
@@ -2570,7 +2575,7 @@ pub fn renameat(
25702575

25712576
/// WASI-only. Same as `renameat` expect targeting WASI.
25722577
/// See also `renameat`.
2573-
pub fn renameatWasi(old: RelativePath, new: RelativePath) RenameError!void {
2578+
pub fn renameatWasi(old: RelativePathWasi, new: RelativePathWasi) RenameError!void {
25742579
switch (wasi.path_rename(old.dir_fd, old.relative_path.ptr, old.relative_path.len, new.dir_fd, new.relative_path.ptr, new.relative_path.len)) {
25752580
.SUCCESS => return,
25762581
.ACCES => return error.AccessDenied,
@@ -4495,7 +4500,7 @@ pub fn faccessat(dirfd: fd_t, path: []const u8, mode: u32, flags: u32) AccessErr
44954500
const path_w = try windows.sliceToPrefixedFileW(path);
44964501
return faccessatW(dirfd, path_w.span().ptr, mode, flags);
44974502
} else if (builtin.os.tag == .wasi and !builtin.link_libc) {
4498-
var resolved = RelativePath{ .dir_fd = dirfd, .relative_path = path };
4503+
var resolved = RelativePathWasi{ .dir_fd = dirfd, .relative_path = path };
44994504

45004505
const file = blk: {
45014506
if (dirfd == wasi.AT.FDCWD or fs.path.isAbsolute(path)) {

0 commit comments

Comments
 (0)