Skip to content

stdlib: Use "RelativePath" struct for *at() POSIX functions in std.os #11042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
topolarity opened this issue Mar 3, 2022 · 0 comments
Open
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@topolarity
Copy link
Contributor

Publishing this at @kubkon 's recommendation in #11021 (comment)

That PR introduced std.os.RelativePathWasi, which looks like this:

/// An fd-relative file path
pub const RelativePathWasi = struct {
    /// Handle to directory
    dir_fd: fd_t,
    /// Path to resource within `dir_fd`.
    relative_path: []const u8,
};

Although this only used in the WASI-specific internal functions right now, it seems natural for all of the (slightly) more modern *at() POSIX functions: openat, renameat, symlinkat, etc.

The idea would be to change these functions to accept RelativePath for fd_t-relative path arguments:

// Proposed
fn openat(file: RelativePath, flags: u32, mode: mode_t) OpenError!fd_t
fn symlinkat(target: []const u8, symlink: RelativePath) SymLinkError!void
fn renameat(old: RelativePath, new: RelativePath) RenameError!void

// Current
fn openat(dir_fd: fd_t, file_path: []const u8, flags: u32, mode: mode_t) OpenError!fd_t
fn symlinkat(target_path: []const u8, newdirfd: fd_t, sym_link_path: []const u8) SymLinkError!void
fn renameat(old_dir_fd: fd_t, old_path: []const u8, new_dir_fd: fd_t, new_path: []const u8) RenameError!void

This comes with some other small advantages:

  • POSIX-style functions can be made to be generic over fd_t/[]const u8/RelativePath parameters, if desired
  • It matches the cwd().openFile() style in the "std.fs" interface well - e.g. cwd().getRelativePath(...) could give you a RelativePath for use with the POSIX-style std.os functions
@kprotty kprotty added standard library This issue involves writing Zig code for the standard library. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. labels Mar 21, 2022
@Vexu Vexu added this to the 0.11.0 milestone Mar 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

No branches or pull requests

3 participants