Skip to content

Commit 935bfa9

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 The below may be used for platform-specific functionality - 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 b261064 commit 935bfa9

File tree

3 files changed

+584
-0
lines changed

3 files changed

+584
-0
lines changed

lib/std/fs.zig

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

22332233
pub const ChownError = File.ChownError;
2234+
2235+
const Permissions = File.Permissions;
2236+
pub const SetPermissionsError = File.SetPermissionsError;
2237+
2238+
/// Sets permissions according to the provided `Permissions` struct.
2239+
/// This method is *NOT* available on WASI
2240+
pub fn setPermissions(self: Dir, permissions: Permissions) SetPermissionsError!void {
2241+
const file: File = .{
2242+
.handle = self.fd,
2243+
.capable_io_mode = .blocking,
2244+
};
2245+
try file.setPermissions(permissions);
2246+
}
2247+
2248+
const Metadata = File.Metadata;
2249+
pub const MetadataError = File.MetadataError;
2250+
2251+
/// Returns a `Metadata` struct, representing the permissions on the directory
2252+
pub fn metadata(self: Dir) MetadataError!Metadata {
2253+
const file: File = .{
2254+
.handle = self.fd,
2255+
.capable_io_mode = .blocking,
2256+
};
2257+
return try file.metadata();
2258+
}
22342259
};
22352260

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

0 commit comments

Comments
 (0)