RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
Files: add inline Unix permission toggles
Meld529b892d70d
diff --git a/src/files/app.zig b/src/files/app.zigindex eeff325..06e43f0 100644--- a/src/files/app.zig+++ b/src/files/app.zig@@ -908,6 +908,9 @@ pub const renderTorrentSidebarTip = app_torrents.renderTorrentSidebarTip;pub const activateSelected = app_opening.activateSelected;// app_pointer.zig+ pub const permissionHoverBase = app_pointer.permissionHoverBase;+ pub const permissionHit = app_pointer.permissionHit;+ pub const toggleInlinePermission = app_pointer.toggleInlinePermission;pub const hoverKey = app_pointer.hoverKey;pub const handleMotion = app_pointer.handleMotion;pub const cancelDrag = app_pointer.cancelDrag;@@ -1005,6 +1008,7 @@ pub const renderTorrentSidebarTip = app_torrents.renderTorrentSidebarTip;pub const renderToolbarHover = app_paint.renderToolbarHover;pub const renderGitViewToggle = app_paint.renderGitViewToggle;pub const render = app_paint.render;+ pub const renderPermissionTip = app_paint.renderPermissionTip;pub const renderButton = app_paint.renderButton;pub const fieldFontSize = app_paint.fieldFontSize;pub const renderLocationBar = app_paint.renderLocationBar;diff --git a/src/files/app_hover.zig b/src/files/app_hover.zigindex ca33140..8dada3c 100644--- a/src/files/app_hover.zig+++ b/src/files/app_hover.zig@@ -20,6 +20,7 @@ pub fn hoverBounds(self: *App, key: usize) ?header.Rect {const card = self.sidebarLayout().cards[key - 940];return self.sidebarHeaderBounds(card);}+ if (key >= App.permissionHoverBase and key < App.permissionHoverBase + self.items.items.len * 9) return null;if (key >= std.math.maxInt(usize) - 4096) return null;if (key >= 900 and key < 904) return .{ .x = 0, .y = 0, .w = @floatFromInt(self.w), .h = @floatFromInt(self.h) };if (key == 0) return .{ .x = 0, .y = 0, .w = 0, .h = 0 };diff --git a/src/files/app_items.zig b/src/files/app_items.zigindex eed5835..59fe01c 100644--- a/src/files/app_items.zig+++ b/src/files/app_items.zig@@ -240,8 +240,36 @@ pub fn renderFileItems(self: *App, cr: *c.cairo_t, viewport_h: i32) void {const timestamp = if (item.transfer_stub) "—" else @import("settings.zig").dateTime(if (self.isRecent()) item.opened else item.mtime, &buf);drawEllipsis(cr, timestamp, mt.x + 4, y + baseline, mt.w - 8, shell_ui.textSize(), false);const permissions = self.listColumn(4);- const mode = formatPermissions(item.mode);- drawEllipsis(cr, if (item.transfer_stub) "—" else if (item.permissions_text.len > 0) item.permissions_text else &mode, permissions.x + 4, y + baseline, permissions.w - 8, shell_ui.textSize(), false);+ if (item.transfer_stub or item.transfer_ghost) {+ drawEllipsis(cr, "—", permissions.x + 4, y + baseline, permissions.w - 8, shell_ui.textSize(), false);+ } else {+ const mode = formatPermissions(item.mode);+ const text_size = shell_ui.textSize();+ setSource(cr, ui_theme.global.window_fg);+ drawText(cr, mode[0..1], permissions.x + 4, y + baseline, text_size, false);++ // Each permission has its own fixed hit cell. The gaps between+ // triplets visually separate Owner, Group and Others.+ const permission_start = permissions.x + 18;+ for (0..9) |bit| {+ const offset: f64 = @floatFromInt(bit * 10 + (bit / 3) * 4);+ const glyph_x = permission_start + offset;+ const glyph = mode[bit + 1 .. bit + 2];+ const glyph_width = measureText(cr, glyph, text_size, false);+ const hovered = self.hoverKey() == App.permissionHoverBase + idx * 9 + bit;+ setSource(cr, if (hovered) accent else ui_theme.global.window_fg);+ drawText(cr, glyph, glyph_x + (10 - glyph_width) / 2, y + baseline, text_size, false);+ }++ if (item.permissions_text.len > 10) {+ const owner_x = permission_start + 106;+ const owner_width = permissions.x + permissions.w - owner_x - 4;+ if (owner_width > 0) {+ setSource(cr, ui_theme.global.window_fg);+ drawEllipsis(cr, item.permissions_text[10..], owner_x, y + baseline, owner_width, text_size, false);+ }+ }+ }} else if (self.torrentForItem(idx) != null) {// The transfer accessory occupies the grid card's detail line.} else {diff --git a/src/files/app_paint.zig b/src/files/app_paint.zigindex 9657d4a..851c246 100644--- a/src/files/app_paint.zig+++ b/src/files/app_paint.zig@@ -27,6 +27,7 @@ const drawEllipsis = @import("app_draw.zig").drawEllipsis;const drawText = @import("app_draw.zig").drawText;const fontMetrics = @import("app_draw.zig").fontMetrics;const measureText = @import("app_draw.zig").measureText;+const roundedRect = @import("app_draw.zig").roundedRect;pub fn renderHeader(self: *App, cr: *c.cairo_t) void {const width: f64 = @floatFromInt(self.w);@@ -212,6 +213,7 @@ pub fn render(self: *App, cr: *c.cairo_t) void {self.renderTerminal(cr);if (!self.audio_bar.playlist_open and !self.transfer_view.active and self.menu == null and self.dialog == null and self.selection_band == null) self.renderFilenameTip(cr);+ self.renderPermissionTip(cr);if (self.menu != null) self.renderMenu(cr);if (self.menu == null and self.dialog == null) {self.renderFileOpsTip(cr);@@ -227,6 +229,32 @@ pub fn render(self: *App, cr: *c.cairo_t) void {}}+pub fn renderPermissionTip(self: *App, cr: *c.cairo_t) void {+ const hit = self.permissionHit(self.mouse_x, self.mouse_y) orelse return;+ const item = self.items.items[hit.index];+ const groups = [_][]const u8{ tr("Owner"), tr("Group"), tr("Others") };+ const actions = [_][]const u8{ tr("Read"), tr("Write"), tr("Execute") };+ const state = if (item.mode & (@as(@TypeOf(item.mode), 1) << @as(std.math.Log2Int(@TypeOf(item.mode)), @intCast(8 - (hit.bit % 3) * 3 - hit.bit / 3))) != 0)+ tr("Enabled")+ else+ tr("Disabled");++ var buf: [128]u8 = undefined;+ const label = std.fmt.bufPrint(&buf, "{s} · {s}: {s}", .{+ groups[hit.bit / 3],+ actions[hit.bit % 3],+ state,+ }) catch return;+ const width = measureText(cr, label, shell_ui.textSize(), false) + 16;+ const x = std.math.clamp(self.mouse_x + 12, 8, @max(8, @as(f64, @floatFromInt(self.w)) - width - 8));+ const y = std.math.clamp(self.mouse_y - 34, 8, @max(8, @as(f64, @floatFromInt(self.h - self.footerHeight())) - 30));+ roundedRect(cr, x, y, width, 26, 5);+ setSource(cr, ui_theme.global.app_divider);+ c.cairo_fill(cr);+ setSource(cr, ui_theme.global.window_fg);+ drawText(cr, label, x + 8, y + 18, shell_ui.textSize(), false);+}+pub fn renderButton(self: *App, cr: *c.cairo_t, x: f64, y: f64, w: f64, h: f64, label_text: []const u8, enabled: bool, target_id: usize) void {const hovered = enabled and if (self.dialog_surface.hovered) |target|target.id == target_id@@ -288,6 +316,7 @@ pub fn pointerCursor(self: *App, x: f64, y: f64) [*:0]const u8 {if (self.overSidebarDivider(x, y)) return "col-resize";if (self.chooser != null and self.chooserRect(0).contains(x, y)) return "text";if (self.focus != .location_bar and !self.search_visible and header.field(self.w).contains(x, y)) return "pointer";+ if (self.permissionHit(x, y) != null) return "pointer";if (@import("app_audio_bar.zig").hit(self, x, y) != null or self.inlineTorrentHit(x, y) != null or self.audioButtonHit(x, y) != null) return "pointer";if (self.singleClickIconAt(x, y) != null) return "pointer";return if (header.field(self.w).contains(x, y)) "text" else "default";diff --git a/src/files/app_pointer.zig b/src/files/app_pointer.zigindex 216b3c3..34aaa8a 100644--- a/src/files/app_pointer.zig+++ b/src/files/app_pointer.zig@@ -16,6 +16,74 @@ const setSource = @import("app_draw.zig").setSource;const gitBranchRowCount = App.gitBranchRowCount;const gitBranchRow = App.gitBranchRow;+pub const permissionHoverBase: usize = 3_000_000;++pub const PermissionHit = struct {+ index: usize,+ bit: usize,+};++fn permissionsAvailable(self: *App) bool {+ return self.list_view and+ !self.transfer_view.active and+ self.repository() == null and+ self.chooser == null and+ self.dialog == null and+ self.menu == null and+ self.history.archive_len == 0 and+ !@import("app_trash.zig").inTrash(self) and+ self.columnLabels().len > 4;+}++pub fn permissionRect(self: *App, index: usize, bit: usize) ?header.Rect {+ if (!permissionsAvailable(self) or bit >= 9 or index >= self.items.items.len) return null;+ const item = self.items.items[index];+ if (item.transfer_stub or item.transfer_ghost or item.missing or item.is_symlink or item.is_broken) return null;++ const column = self.listColumn(4);+ const row: i32 = @intCast(index);+ const row_y: f64 = @floatFromInt(+ self.viewTop() + self.itemInset() ++ @divTrunc(row, self.columns()) * self.rowHeight() - self.scroll_y,+ );+ const start_x = column.x + 18;+ const offset: f64 = @floatFromInt(bit * 10 + (bit / 3) * 4);+ return .{+ .x = start_x + offset,+ .y = row_y + self.listBaseline() - 17,+ .w = 10,+ .h = 22,+ };+}++pub fn permissionHit(self: *App, x: f64, y: f64) ?PermissionHit {+ if (!permissionsAvailable(self)) return null;+ const index = self.itemAt(x, y) orelse return null;+ for (0..9) |bit| {+ if (permissionRect(self, index, bit)) |rect| {+ if (rect.contains(x, y)) return .{ .index = index, .bit = bit };+ }+ }+ return null;+}++pub fn toggleInlinePermission(self: *App, hit: PermissionHit) void {+ if (hit.index >= self.items.items.len or hit.bit >= 9) return;+ const path = self.items.items[hit.index].path;+ const mode = properties.togglePathPermission(path, hit.bit) catch |err| {+ const message = if (err == error.NotOwner)+ @import("i18n").tr("Only the owner can change permissions")+ else+ @import("i18n").tr("Could not change permissions");+ self.setStatusNotice(message, true);+ return;+ };++ // Update the visible snapshot in place. Selection and scroll stay intact.+ self.items.items[hit.index].mode = mode;+ self.invalidate();+}+// Input handlerspub fn hoverKey(self: *App) usize {if (self.dialog) |dlg| if (dlg.kind == .transfers) return self.transfersHover();@@ -57,6 +125,7 @@ pub fn hoverKey(self: *App) usize {if (self.column_surface.hovered) |target| return @intCast(target.id);}if (self.sidebar_surface.hovered) |target| return @intCast(target.id);+ if (self.permissionHit(x, y)) |hit| return permissionHoverBase + hit.index * 9 + hit.bit;if (self.inlineTorrentHit(x, y)) |hit| return std.math.maxInt(usize) - 3000 - hit.index * 2 - @intFromBool(hit.toggle);if (self.audioButtonHit(x, y)) |index| return @import("app_audio.zig").audioHoverKey(index);if (self.item_surface.hovered) |target| {@@ -604,6 +673,12 @@ pub fn handleButton(self: *App, button: u32, pressed: bool) void {// File view areaif (y >= @as(f64, @floatFromInt(self.contentTop())) and y < @as(f64, @floatFromInt(self.contentBottom())) and x < @as(f64, @floatFromInt(self.w - self.scrollbarGutter()))) {self.focus = .file_view;+ if (button == 0x110) {+ if (self.permissionHit(x, y)) |hit| {+ self.toggleInlinePermission(hit);+ return;+ }+ }if (self.itemAt(x, y)) |idx| {if (button == 0x110) {if (self.sizeButtonRect(idx)) |rect| {diff --git a/src/files/properties.zig b/src/files/properties.zigindex 1b07066..8f91a53 100644--- a/src/files/properties.zig+++ b/src/files/properties.zig@@ -256,7 +256,81 @@ pub const State = struct {};fn permissionBit(index: usize) c.mode_t {- return @as(c.mode_t, 1) << @as(std.math.Log2Int(c.mode_t), @intCast(8 - (index % 3) * 3 - index / 3));+ return @as(c.mode_t, 1) << @as(std.math.Log2Int(c.mode_t), @intCast(8 - index));+}++fn toggledPermissions(mode: c.mode_t, index: usize) !c.mode_t {+ if (index >= 9) return error.InvalidPermission;+ return (mode & 0o7777) ^ permissionBit(index);+}++test "permission characters map to their corresponding Unix mode bits" {+ const expected = [_]c.mode_t{+ 0o400, 0o200, 0o100,+ 0o040, 0o020, 0o010,+ 0o004, 0o002, 0o001,+ };++ for (expected, 0..) |bit, index| {+ try std.testing.expectEqual(bit, permissionBit(index));+ }+}++test "permission toggles enable and disable only the selected bit" {+ const initial: c.mode_t = 0o644;++ for (0..9) |index| {+ const bit = permissionBit(index);+ const enabled = try toggledPermissions(initial, index);+ try std.testing.expectEqual(initial ^ bit, enabled);++ const disabled = try toggledPermissions(enabled, index);+ try std.testing.expectEqual(initial, disabled);+ }+}++test "permission toggles preserve special mode bits" {+ const initial: c.mode_t = 0o7644;++ for (0..9) |index| {+ const result = try toggledPermissions(initial, index);+ try std.testing.expectEqual(@as(c.mode_t, 0o7000), result & 0o7000);+ try std.testing.expectEqual(initial ^ permissionBit(index), result);+ }+}++test "permission toggles reject invalid indices" {+ try std.testing.expectError(+ error.InvalidPermission,+ toggledPermissions(0o644, 9),+ );+}++/// Toggle one permission bit on the inode currently named by `path`.+/// O_PATH pins the inode, and the proc-fd path keeps chmod from following a+/// replacement symlink if the directory entry changes after it is opened.+pub fn togglePathPermission(path: []const u8, index: usize) !c.mode_t {+ const allocator = std.heap.page_allocator;+ const owned = try allocator.dupeSentinel(u8, path, 0);+ defer allocator.free(owned);++ const fd = c.open(owned, c.O_PATH | c.O_NOFOLLOW | c.O_CLOEXEC);+ if (fd < 0) return error.OpenFailed;+ defer _ = c.close(fd);++ var st: c.struct_stat = undefined;+ if (c.fstat(fd, &st) != 0) return error.StatFailed;+ if (st.st_mode & c.S_IFMT == c.S_IFLNK) return error.Symlink;+ if (c.geteuid() != 0 and c.geteuid() != st.st_uid) return error.NotOwner;++ var buf: [64]u8 = undefined;+ const pinned = std.fmt.bufPrintSentinel(&buf, "/proc/self/fd/{d}", .{fd}, 0) catch unreachable;+ const new_mode = try toggledPermissions(st.st_mode, index);+ if (c.chmod(pinned, new_mode) != 0) {+ return error.ChmodFailed;+ }+ if (c.fstat(fd, &st) != 0) return error.StatFailed;+ return st.st_mode;}fn date(timestamp: ?i64, buf: []u8) []const u8 {