RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
Add pause and resume to file operations
Mel9cc8932924d0
diff --git a/src/files/app.zig b/src/files/app.zigindex c5ef6ee..f7cd23a 100644--- a/src/files/app.zig+++ b/src/files/app.zig@@ -272,6 +272,7 @@ pub const App = struct {extract_picker_bytes: std.ArrayList(u8) = .empty,job_cut_revision: ?usize = null,job_progress: usize = std.math.maxInt(usize),+ job_transferred_bytes: u64 = 0,job_state: ops_mod.JobState = .idle,status_notice: ?[]u8 = null,status_notice_until: i64 = 0,@@ -789,6 +790,10 @@ pub const App = struct {pub const drawPackPlaceIcon = app_sidebar_paint.drawPackPlaceIcon;pub const renderSidebar = app_sidebar_paint.renderSidebar;pub const renderPinDrop = app_sidebar_paint.renderPinDrop;+ pub const fileOpsSidebarHeight = app_sidebar_paint.fileOpsSidebarHeight;+ pub const fileOpsSidebarHit = app_sidebar_paint.fileOpsSidebarHit;+ pub const fileOpsSidebarClick = app_sidebar_paint.fileOpsSidebarClick;+ pub const renderFileOpsSidebar = app_sidebar_paint.renderFileOpsSidebar;// app_sidebar_paint.zigpub const sidebarExtent = app_sidebar_paint.sidebarExtent;diff --git a/src/files/app_git_sidebar.zig b/src/files/app_git_sidebar.zigindex 8f2becc..c99f412 100644--- a/src/files/app_git_sidebar.zig+++ b/src/files/app_git_sidebar.zig@@ -51,7 +51,7 @@ pub fn gitCardHeight(self: *App) f64 {}pub fn gitCard(self: *App) header.Rect {- return .{ .x = 10, .y = @floatFromInt(self.pinRowY(self.pins.items.items.len) + 44 + self.deviceBlock() + self.torrentSidebarHeight()), .w = @floatFromInt(self.sidebarWidth() - 20 - self.scrollbarGutter()), .h = self.gitCardHeight() };+ return .{ .x = 10, .y = @floatFromInt(self.pinRowY(self.pins.items.items.len) + 44 + self.deviceBlock() + self.torrentSidebarHeight() + self.fileOpsSidebarHeight()), .w = @floatFromInt(self.sidebarWidth() - 20 - self.scrollbarGutter()), .h = self.gitCardHeight() };}pub fn renderGitSidebar(self: *App, cr: *c.cairo_t, content_width: f64) void {diff --git a/src/files/app_listing.zig b/src/files/app_listing.zigindex 3cbc8ba..2623e4a 100644--- a/src/files/app_listing.zig+++ b/src/files/app_listing.zig@@ -195,12 +195,15 @@ pub fn sync(self: *App) void {}} else self.refresh();}- } else if (progress.state == .running or progress.state == .waiting_conflict) {+ } else if (progress.state == .running or progress.state == .paused or progress.state == .waiting_conflict) {self.job_was_running = true;- if (self.job_progress != progress.processed_items or self.job_state != progress.state) self.invalidate();+ if (self.job_progress != progress.processed_items or+ self.job_state != progress.state or+ self.job_transferred_bytes != progress.transferred_bytes) self.invalidate();}self.job_progress = progress.processed_items;self.job_state = progress.state;+ self.job_transferred_bytes = progress.transferred_bytes;}if (self.status_notice != null and nowMs() >= self.status_notice_until) {self.allocator.free(self.status_notice.?);diff --git a/src/files/app_pointer.zig b/src/files/app_pointer.zigindex fe61175..a8ee0fa 100644--- a/src/files/app_pointer.zig+++ b/src/files/app_pointer.zig@@ -19,6 +19,7 @@ const gitBranchRow = App.gitBranchRow;// Input handlerspub fn hoverKey(self: *App) usize {if (self.dialog) |dlg| if (dlg.kind == .transfers) return self.transfersHover();+ if (self.dialog == null and self.fileOpsSidebarHit(self.mouse_x, self.mouse_y)) return std.math.maxInt(usize) - 2002;if (self.dialog == null and self.torrentSidebarHit(self.mouse_x, self.mouse_y)) return std.math.maxInt(usize) - 2001;const x = self.mouse_x;const y = self.mouse_y;@@ -502,6 +503,7 @@ pub fn handleButton(self: *App, button: u32, pressed: bool) void {return;}}+ if (self.fileOpsSidebarClick(x, y)) return;if (self.torrentSidebarHit(x, y)) {self.openTransfers();return;diff --git a/src/files/app_sidebar_paint.zig b/src/files/app_sidebar_paint.zigindex 5dd967d..dd1d6bd 100644--- a/src/files/app_sidebar_paint.zig+++ b/src/files/app_sidebar_paint.zig@@ -10,6 +10,7 @@ const recent = @import("recent.zig");const icons = @import("../icon_cache.zig");const shell_ui = @import("ui").cairo;const ui_theme = @import("ui").theme;+const button_widget = @import("ui").widgets.button;const pins_mod = @import("pins.zig");const places = App.places;const places_start = App.places_start;@@ -148,6 +149,7 @@ pub fn renderSidebar(self: *App, cr: *c.cairo_t) void {c.cairo_paint(cr);c.cairo_set_operator(cr, c.CAIRO_OPERATOR_OVER);self.renderTorrentSidebar(cr);+ self.renderFileOpsSidebar(cr);self.renderGitSidebar(cr, content_width);// Under the labels: the glide passes over the PLACES heading.const glide = self.nav_hover.frame;@@ -244,7 +246,111 @@ pub fn renderPinDrop(self: *App, cr: *c.cairo_t) void {// Bottom of the sidebar content in scrolled coordinates: the Git card is the last section.pub fn sidebarExtent(self: *App) i32 {- return self.pinRowY(self.pins.items.items.len) + self.sidebar_scroll + 8 + self.deviceBlock() + self.gitSidebarHeight() + self.torrentSidebarHeight();+ return self.pinRowY(self.pins.items.items.len) + self.sidebar_scroll + 8 + self.deviceBlock() ++ self.torrentSidebarHeight() + self.fileOpsSidebarHeight() + self.gitSidebarHeight();+}++fn fileOpsBox(self: *App) shell_ui.Rect {+ return .{+ .x = 12,+ .y = @floatFromInt(self.pinRowY(self.pins.items.items.len) + 40 + self.deviceBlock() + self.torrentSidebarHeight()),+ .w = @floatFromInt(@max(0, self.sidebarWidth() - 24 - self.scrollbarGutter())),+ .h = 104,+ };+}++pub fn fileOpsSidebarHeight(self: *App) i32 {+ const runner = self.job_runner orelse return 0;+ const state = runner.pollProgress().state;+ return if (state == .running or state == .paused or state == .waiting_conflict) 112 else 0;+}++pub fn fileOpsSidebarHit(self: *App, x: f64, y: f64) bool {+ if (self.fileOpsSidebarHeight() == 0 or self.chooser != null or self.dialog != null) return false;+ if (self.sidebarWidth() == 0 or y < @as(f64, @floatFromInt(self.contentTop())) or+ y >= @as(f64, @floatFromInt(self.h - self.sidebarFooter()))) return false;+ const area = fileOpsBox(self);+ return x >= area.x and x < area.x + area.w and y >= area.y and y < area.y + area.h;+}++pub fn fileOpsSidebarClick(self: *App, x: f64, y: f64) bool {+ if (!self.fileOpsSidebarHit(x, y)) return false;+ const runner = self.job_runner orelse return true;+ const progress = runner.pollProgress();+ const area = fileOpsBox(self);+ const local_y = y - area.y;+ const cancel_left = area.x + area.w - 64;+ if (local_y >= 68 and local_y <= 98 and x >= cancel_left and x <= area.x + area.w - 8) {+ runner.cancel();+ } else if ((progress.kind == .copy or progress.kind == .move) and+ local_y >= 68 and local_y <= 98 and x >= area.x + area.w - 124 and x < cancel_left - 4)+ {+ if (progress.state == .paused) {+ runner.resumeJob();+ } else if (progress.state == .running) {+ runner.pause();+ }+ }+ self.invalidate();+ return true;+}++pub fn renderFileOpsSidebar(self: *App, cr: *c.cairo_t) void {+ const runner = self.job_runner orelse return;+ const progress = runner.pollProgress();+ if (progress.state != .running and progress.state != .paused and progress.state != .waiting_conflict) return;+ const area = fileOpsBox(self);+ if (area.w < 120) return;++ const palette = ui_theme.shellPalette();+ drawText(cr, tr("FILE OPERATIONS"), 24, area.y + 18, shell_ui.headingSize(), true);++ const fraction: f64 = if (progress.total_bytes_known and progress.total_bytes > 0)+ @min(1.0, @as(f64, @floatFromInt(progress.transferred_bytes)) / @as(f64, @floatFromInt(progress.total_bytes)))+ else+ 0;+ shell_ui.drawProgressRing(cr, .{ .x = area.x + 12, .y = area.y + 30, .w = 28, .h = 28 }, fraction, palette.accent);++ var percent_buf: [32]u8 = undefined;+ const percent: u64 = @intFromFloat(fraction * 100);+ const status = if (progress.state == .paused)+ tr("Paused")+ else if (progress.state == .waiting_conflict)+ tr("Waiting for decision")+ else if (progress.measuring)+ tr("Measuring…")+ else if (progress.total_bytes_known)+ std.fmt.bufPrint(&percent_buf, "{d}%", .{percent}) catch ""+ else+ tr("Working…");+ drawText(cr, status, area.x + 48, area.y + 43, shell_ui.statusSize(), false);+ drawEllipsis(cr, if (progress.current_name.len > 0) progress.current_name else tr("Preparing…"),+ area.x + 48, area.y + 61, area.w - 58, shell_ui.statusSize(), false);++ const can_pause = (progress.kind == .copy or progress.kind == .move) and+ (progress.state == .running or progress.state == .paused);+ if (can_pause) {+ const rect = shell_ui.Rect{ .x = area.w - 120, .y = 68, .w = 52, .h = 26 };+ var layer = shell_ui.Layer.begin(cr, .{ .x = area.x + rect.x, .y = area.y + rect.y, .w = rect.w, .h = rect.h }) orelse return;+ defer layer.finish();+ const hovered = rect.x + area.x <= self.mouse_x and self.mouse_x <= rect.x + area.x + rect.w and+ rect.y + area.y <= self.mouse_y and self.mouse_y <= rect.y + area.y + rect.h;+ button_widget.paint(&layer.renderer, layer.local(), .{+ .label = if (progress.state == .paused) tr("Resume") else tr("Pause"),+ .variant = .ghost,+ .size = .sm,+ }, .{ .pointer = if (hovered) .hover else .idle });+ }+ const cancel_rect = shell_ui.Rect{ .x = area.w - 64, .y = 68, .w = 56, .h = 26 };+ var cancel_layer = shell_ui.Layer.begin(cr, .{ .x = area.x + cancel_rect.x, .y = area.y + cancel_rect.y, .w = cancel_rect.w, .h = cancel_rect.h }) orelse return;+ defer cancel_layer.finish();+ const cancel_hovered = cancel_rect.x + area.x <= self.mouse_x and self.mouse_x <= cancel_rect.x + area.x + cancel_rect.w and+ cancel_rect.y + area.y <= self.mouse_y and self.mouse_y <= cancel_rect.y + area.y + cancel_rect.h;+ button_widget.paint(&cancel_layer.renderer, cancel_layer.local(), .{+ .label = tr("Cancel"),+ .variant = .ghost,+ .size = .sm,+ }, .{ .pointer = if (cancel_hovered) .hover else .idle });}pub fn renderSidebarScrollbar(self: *App, cr: *c.cairo_t) void {diff --git a/src/files/ops.zig b/src/files/ops.zigindex 1d48608..c8f42d6 100644--- a/src/files/ops.zig+++ b/src/files/ops.zig@@ -16,6 +16,7 @@ pub const ConflictAction = enum {pub const JobState = enum {idle,running,+ paused,waiting_conflict,finished,};@@ -43,6 +44,7 @@ pub const ItemResult = struct {pub const ProgressInfo = struct {state: JobState,+ kind: OpKind = .copy,total_items: usize = 0,processed_items: usize = 0,total_bytes: u64 = 0,@@ -228,7 +230,9 @@ fn measureBytes(allocator: Allocator,path: []const u8,cancel: ?*std.atomic.Value(bool),+ runner: ?*JobRunner,) !u64 {+ if (runner) |job| if (!job.waitIfResumed(cancel)) return error.Cancelled;if (cancel != null and cancel.?.load(.acquire)) return error.Cancelled;const path_z = try allocator.dupeSentinel(u8, path, 0);@@ -250,6 +254,7 @@ fn measureBytes(var total: u64 = 0;while (true) {+ if (runner) |job| if (!job.waitIfResumed(cancel)) return error.Cancelled;if (cancel != null and cancel.?.load(.acquire)) return error.Cancelled;std.c._errno().* = 0;@@ -262,7 +267,7 @@ fn measureBytes(const child = try std.fmt.allocPrint(allocator, "{s}/{s}", .{ path, name });defer allocator.free(child);- const child_bytes = try measureBytes(allocator, child, cancel);+ const child_bytes = try measureBytes(allocator, child, cancel, runner);if (child_bytes > std.math.maxInt(u64) - total) return error.SizeOverflow;total += child_bytes;}@@ -299,6 +304,10 @@ fn copyFileContentsTracked(var buf: [65536]u8 = undefined;while (true) {+ if (runner) |job| if (!job.waitIfResumed(cancel)) {+ _ = c.unlink(dst_z);+ return error.Cancelled;+ };if (cancel != null and cancel.?.load(.acquire)) {_ = c.unlink(dst_z);return error.Cancelled;@@ -350,6 +359,7 @@ fn copyRecursiveTracked(cancel: ?*std.atomic.Value(bool),runner: ?*JobRunner,) !void {+ if (runner) |job| if (!job.waitIfResumed(cancel)) return error.Cancelled;if (cancel != null and cancel.?.load(.acquire)) return error.Cancelled;const src_z = try allocator.dupeSentinel(u8, src, 0);@@ -442,6 +452,10 @@ fn crossDeviceMoveTracked(// Copy cleans up only destinations it created; a raced existing target is preserved.try copyRecursiveTracked(allocator, src, dst, cancel, runner);+ if (runner) |job| if (!job.waitIfResumed(cancel)) {+ deleteRecursive(allocator, dst) catch {};+ return error.Cancelled;+ };if (cancel != null and cancel.?.load(.acquire)) {deleteRecursive(allocator, dst) catch {};return error.Cancelled;@@ -463,6 +477,7 @@ fn moveItemTracked(runner: ?*JobRunner,source_bytes: u64,) !void {+ if (runner) |job| if (!job.waitIfResumed(cancel)) return error.Cancelled;if (isDescendantOrSame(src, dst)) return error.CannotMoveIntoDescendant;if (std.mem.eql(u8, src, dst)) return;@@ -486,6 +501,7 @@ pub const JobRunner = struct {thread: ?std.Thread = null,wake_fd: c_int = -1,cancel_flag: std.atomic.Value(bool) = .init(false),+ pause_requested: bool = false,// Protected by mutexstate: JobState = .idle,@@ -597,7 +613,7 @@ pub const JobRunner = struct {fn startImpl(self: *JobRunner, kind: OpKind, sources: []const []const u8, dest_dir: []const u8, default_conflict: ConflictAction, member: ?[]const u8) !void {const progress = self.pollProgress();- if (progress.state == .running or progress.state == .waiting_conflict) return error.Busy;+ if (progress.state == .running or progress.state == .paused or progress.state == .waiting_conflict) return error.Busy;if (self.thread) |t| {t.join();self.thread = null;@@ -625,6 +641,7 @@ pub const JobRunner = struct {self.default_conflict = default_conflict;self.state = .running;self.cancel_flag.store(false, .release);+ self.pause_requested = false;self.total_items = self.sources.items.len;self.processed_items = 0;@@ -650,11 +667,50 @@ pub const JobRunner = struct {pub fn cancel(self: *JobRunner) void {self.cancel_flag.store(true, .release);_ = c.pthread_mutex_lock(&self.mutex);+ self.pause_requested = false;if (self.state == .waiting_conflict) {self.conflict_action = .cancel;- _ = c.pthread_cond_signal(&self.cond);}+ _ = c.pthread_cond_broadcast(&self.cond);+ _ = c.pthread_mutex_unlock(&self.mutex);+ }++ /// Request a cooperative pause. The worker acknowledges it at its next+ /// safe point, after the current file chunk or directory entry.+ pub fn pause(self: *JobRunner) void {+ _ = c.pthread_mutex_lock(&self.mutex);+ if (self.state == .running and (self.kind == .copy or self.kind == .move)) {+ self.pause_requested = true;+ self.notify();+ }+ _ = c.pthread_mutex_unlock(&self.mutex);+ }++ pub fn resumeJob(self: *JobRunner) void {+ _ = c.pthread_mutex_lock(&self.mutex);+ if (self.state == .paused) {+ self.pause_requested = false;+ self.state = .running;+ _ = c.pthread_cond_broadcast(&self.cond);+ self.notify();+ }+ _ = c.pthread_mutex_unlock(&self.mutex);+ }++ /// Called only by the worker. A paused state means the worker has reached+ /// a safe point and is no longer doing filesystem work.+ fn waitIfResumed(self: *JobRunner, cancel_flag: ?*std.atomic.Value(bool)) bool {+ _ = c.pthread_mutex_lock(&self.mutex);+ while (self.pause_requested and (cancel_flag == null or !cancel_flag.?.load(.acquire))) {+ if (self.state != .paused) {+ self.state = .paused;+ self.notify();+ }+ _ = c.pthread_cond_wait(&self.cond, &self.mutex);+ }+ const should_continue = cancel_flag == null or !cancel_flag.?.load(.acquire);_ = c.pthread_mutex_unlock(&self.mutex);+ return should_continue;}pub fn resolveConflict(self: *JobRunner, action: ConflictAction) void {@@ -673,6 +729,7 @@ pub const JobRunner = struct {return .{.state = self.state,+ .kind = self.kind,.total_items = self.total_items,.processed_items = self.processed_items,.total_bytes = self.total_bytes,@@ -811,7 +868,7 @@ pub const JobRunner = struct {known = false;break;}- const bytes = measureBytes(self.allocator, src, &self.cancel_flag) catch {+ const bytes = measureBytes(self.allocator, src, &self.cancel_flag, self) catch {known = false;break;};diff --git a/src/files/ops_tests.zig b/src/files/ops_tests.zigindex 79a219b..4c293ba 100644--- a/src/files/ops_tests.zig+++ b/src/files/ops_tests.zig@@ -474,6 +474,66 @@ test "cancelling an active copy removes partial output and preserves source" {try std.testing.expect(!testFileExists(dst));}+test "paused copy stops at a safe point and resumes" {+ const a = std.testing.allocator;+ var tmp_buf: [256]u8 = undefined;+ const tmp = try makeTestDir(&tmp_buf);+ defer ops_mod.deleteRecursive(a, tmp) catch {};+ try ops_mod.createFolder(a, tmp, "dest");+ const dest = try std.fmt.allocPrint(a, "{s}/dest", .{tmp});+ defer a.free(dest);+ const src = try std.fmt.allocPrintSentinel(a, "{s}/large", .{tmp}, 0);+ defer a.free(src);+ const fd = c.open(src, c.O_CREAT | c.O_WRONLY | c.O_CLOEXEC, @as(c_uint, 0o600));+ try std.testing.expect(fd >= 0);+ defer _ = c.close(fd);+ try std.testing.expect(c.ftruncate(fd, 512 * 1024 * 1024) == 0);++ const runner = try ops_mod.JobRunner.init(a);+ defer runner.deinit();+ try runner.start(.copy, &.{src}, dest, .ask);++ var started = false;+ for (0..1000) |_| {+ if (runner.pollProgress().transferred_bytes > 0) {+ started = true;+ break;+ }+ _ = c.usleep(1000);+ }+ try std.testing.expect(started);++ runner.pause();+ var paused = false;+ for (0..1000) |_| {+ if (runner.pollProgress().state == .paused) {+ paused = true;+ break;+ }+ _ = c.usleep(1000);+ }+ try std.testing.expect(paused);+ const paused_bytes = runner.pollProgress().transferred_bytes;+ _ = c.usleep(20 * 1000);+ try std.testing.expectEqual(paused_bytes, runner.pollProgress().transferred_bytes);++ runner.resumeJob();+ var resumed = false;+ for (0..1000) |_| {+ if (runner.pollProgress().state == .running) {+ resumed = true;+ break;+ }+ _ = c.usleep(1000);+ }+ try std.testing.expect(resumed);+ runner.cancel();+ runner.thread.?.join();+ runner.thread = null;+ try std.testing.expect(runner.pollProgress().cancelled);+ try std.testing.expect(testFileExists(src));+}+test "failed copy and cross-device fallback never remove an existing destination" {const a = std.testing.allocator;var tmp_buf: [256]u8 = undefined;