Skip to content

Commit c6dcaa9

Browse files
committed
std.fs: Implement cross-platform metadata API
Implements a cross-platform metadata API, aiming to reduce unnecessary Unix-dependence of the `std.fs` api. Presently, all OSes beside Windows are treated as Unix; this is likely the best way to treat things by default, instead of explicitly listing each Unix-like OS. Platform-specific operations are not provided by `File.Metadata`, and instead are to be accessed from `File.Metadata.inner`. Adds: - File.setPermissions() : Sets permission of a file according to a `Permissions` struct (not available on WASI) - File.Permissions : A cross-platform representation of file permissions - Permissions.readOnly() : Returns whether the file is read-only - Permissions.setReadOnly() : Sets whether the file is read-only - Permissions.unixSet() : Sets permissions for a class (UNIX-only) - Permissions.unixGet() : Checks a permission for a class (UNIX-only) - Permissions.unixNew() : Returns a new Permissions struct to represent the passed mode (UNIX-only) - File.Metadata : A cross-platform representation of file metadata - Metadata.size() : Returns the size of a file - Metadata.permissions() : Returns a `Permissions` struct, representing permissions on the file - Metadata.kind() : Returns the `Kind` of the file - Metadata.accessed() : Returns the time the file was last accessed - Metadata.modified() : Returns the time the file was last modified - Metadata.created() : Returns the time the file was created (this is an optional, as the underlying filesystem, or OS may not support this) Methods of `File.Metadata` are also available for the below, so I won't repeat myself - File.MetadataUnix : The internal implementation of `File.Metadata` on Unices - File.MetadataLinux : The internal implementation of `File.Metadata` on Linux - File.MetadataWindows : The implementation of `File.Metadata` on Windows
1 parent c30173f commit c6dcaa9

File tree

3 files changed

+555
-0
lines changed

3 files changed

+555
-0
lines changed

lib/std/fs.zig

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,6 +2210,31 @@ pub const Dir = struct {
22102210
}
22112211

22122212
pub const ChownError = File.ChownError;
2213+
2214+
const Permissions = File.Permissions;
2215+
pub const SetPermissionsError = File.SetPermissionsError;
2216+
2217+
/// Sets permissions according to the provided `Permissions` struct.
2218+
/// This method is *NOT* available on WASI
2219+
pub fn setPermissions(self: Dir, permissions: Permissions) SetPermissionsError!void {
2220+
const file: File = .{
2221+
.handle = self.fd,
2222+
.capable_io_mode = .blocking,
2223+
};
2224+
try file.setPermissions(permissions);
2225+
}
2226+
2227+
const Metadata = File.Metadata;
2228+
pub const MetadataError = File.MetadataError;
2229+
2230+
/// Returns a `Metadata` struct, representing the permissions on the directory
2231+
pub fn metadata(self: Dir) MetadataError!Metadata {
2232+
const file: File = .{
2233+
.handle = self.fd,
2234+
.capable_io_mode = .blocking,
2235+
};
2236+
return try file.metadata();
2237+
}
22132238
};
22142239

22152240
/// Returns a handle to the current working directory. It is not opened with iteration capability.

0 commit comments

Comments
 (0)