struct PermissionsUnix [src]

Fields

mode: Mode

Members

Source

pub const PermissionsUnix = struct { mode: Mode, const Self = @This(); /// Returns `true` if permissions represent an unwritable file. /// `true` is returned only if no class has write permissions. pub fn readOnly(self: Self) bool { return self.mode & 0o222 == 0; } /// Sets whether write permissions are provided. /// This affects *all* classes. If this is undesired, use `unixSet`. /// This method *DOES NOT* set permissions on the filesystem: use `File.setPermissions(permissions)` pub fn setReadOnly(self: *Self, read_only: bool) void { if (read_only) { self.mode &= ~@as(Mode, 0o222); } else { self.mode |= @as(Mode, 0o222); } } pub const Class = enum(u2) { user = 2, group = 1, other = 0, }; pub const Permission = enum(u3) { read = 0o4, write = 0o2, execute = 0o1, }; /// Returns `true` if the chosen class has the selected permission. /// This method is only available on Unix platforms. pub fn unixHas(self: Self, class: Class, permission: Permission) bool { const mask = @as(Mode, @intFromEnum(permission)) << @as(u3, @intFromEnum(class)) * 3; return self.mode & mask != 0; } /// Sets the permissions for the chosen class. Any permissions set to `null` are left unchanged. /// This method *DOES NOT* set permissions on the filesystem: use `File.setPermissions(permissions)` pub fn unixSet(self: *Self, class: Class, permissions: struct { read: ?bool = null, write: ?bool = null, execute: ?bool = null, }) void { const shift = @as(u3, @intFromEnum(class)) * 3; if (permissions.read) |r| { if (r) { self.mode |= @as(Mode, 0o4) << shift; } else { self.mode &= ~(@as(Mode, 0o4) << shift); } } if (permissions.write) |w| { if (w) { self.mode |= @as(Mode, 0o2) << shift; } else { self.mode &= ~(@as(Mode, 0o2) << shift); } } if (permissions.execute) |x| { if (x) { self.mode |= @as(Mode, 0o1) << shift; } else { self.mode &= ~(@as(Mode, 0o1) << shift); } } } /// Returns a `Permissions` struct representing the permissions from the passed mode. pub fn unixNew(new_mode: Mode) Self { return Self{ .mode = new_mode, }; } }