Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

Handle transfer manifest rollback and retirement

Melb149e074b1f8
diff --git a/src/files/ops.zig b/src/files/ops.zig
index 5163f24..2e35f0d 100644
--- a/src/files/ops.zig
+++ b/src/files/ops.zig
@@ -516,10 +516,32 @@ fn copyRecursiveTracked(
runner: ?*JobRunner,
) !void {
if (runner) |job| job.setManifestEntryState(job.current_item_index, src, .copying);
- try copyRecursiveTrackedImpl(allocator, src, dst, cancel, runner);
+ copyRecursiveTrackedImpl(allocator, src, dst, cancel, runner) catch |err| {
+ // Recursive directory cleanup runs before this catch. If the destination
+ // is gone, the whole copied subtree was rolled back; if it remains,
+ // cleanup failed and completed descendants may still exist.
+ if (runner) |job| {
+ if (!pathExists(allocator, dst)) {
+ job.setManifestSubtreeState(
+ job.current_item_index,
+ src,
+ if (err == error.Cancelled) .cancelled else .failed,
+ );
+ }
+ }
+ return err;
+ };
if (runner) |job| job.setManifestEntryState(job.current_item_index, src, .completed);
}
+fn pathExists(allocator: Allocator, path: []const u8) bool {
+ const path_z = allocator.dupeSentinel(u8, path, 0) catch return true;
+ defer allocator.free(path_z);
+ var st: c.struct_stat = undefined;
+ if (c.lstat(path_z, &st) == 0) return true;
+ return std.c._errno().* != c.ENOENT;
+}
+
fn copyRecursiveTrackedImpl(
allocator: Allocator,
src: []const u8,
@@ -621,11 +643,13 @@ 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 {};
+ deleteRecursive(allocator, dst) catch return error.Cancelled;
+ job.setManifestRootState(job.current_item_index, .cancelled);
return error.Cancelled;
};
if (cancel != null and cancel.?.load(.acquire)) {
- deleteRecursive(allocator, dst) catch {};
+ deleteRecursive(allocator, dst) catch return error.Cancelled;
+ if (runner) |job| job.setManifestRootState(job.current_item_index, .cancelled);
return error.Cancelled;
}
@@ -1183,6 +1207,27 @@ pub const JobRunner = struct {
}
}
+ fn setManifestSubtreeState(
+ self: *JobRunner,
+ root_index: usize,
+ source_root: []const u8,
+ state: TransferEntryState,
+ ) void {
+ _ = c.pthread_mutex_lock(&self.mutex);
+ defer _ = c.pthread_mutex_unlock(&self.mutex);
+
+ for (self.manifest.entries.items) |*entry| {
+ if (entry.root_index != root_index) continue;
+ if (std.mem.eql(u8, entry.source_path, source_root) or
+ (std.mem.startsWith(u8, entry.source_path, source_root) and
+ entry.source_path.len > source_root.len and
+ entry.source_path[source_root.len] == '/'))
+ {
+ entry.state = state;
+ }
+ }
+ }
+
fn finishManifestRoot(self: *JobRunner, root_index: usize, outcome: ItemOutcome) void {
_ = c.pthread_mutex_lock(&self.mutex);
defer _ = c.pthread_mutex_unlock(&self.mutex);
diff --git a/src/files/ops_tests.zig b/src/files/ops_tests.zig
index fc28387..79324d8 100644
--- a/src/files/ops_tests.zig
+++ b/src/files/ops_tests.zig
@@ -122,6 +122,47 @@ test "JobRunner snapshots manifest entries with independent path ownership" {
try std.testing.expectEqual(ops_mod.TransferEntryState.copying, snapshot.entries.items[0].state);
}
+test "JobRunner retires the live manifest on reuse without invalidating snapshots" {
+ const a = std.testing.allocator;
+ var tmp_buf: [256]u8 = undefined;
+ const tmp = try makeTestDir(&tmp_buf);
+ defer ops_mod.deleteRecursive(a, tmp) catch {};
+
+ const destination = try std.fmt.allocPrint(a, "{s}/destination", .{tmp});
+ defer a.free(destination);
+ try ops_mod.createFolder(a, tmp, "destination");
+
+ const first = try std.fmt.allocPrint(a, "{s}/first.txt", .{tmp});
+ defer a.free(first);
+ try writeTestFile(first, "first");
+ const second = try std.fmt.allocPrint(a, "{s}/second.txt", .{tmp});
+ defer a.free(second);
+ try writeTestFile(second, "second");
+
+ const runner = try ops_mod.JobRunner.init(a);
+ defer runner.deinit();
+
+ try runner.start(.copy, &.{first}, destination, .ask);
+ runner.thread.?.join();
+ runner.thread = null;
+ var snapshot = try runner.snapshotTransferManifest(a);
+ defer snapshot.deinit();
+ try std.testing.expectEqual(@as(usize, 1), snapshot.entries.items.len);
+ try std.testing.expectEqualStrings(first, snapshot.entries.items[0].source_path);
+ try std.testing.expectEqual(ops_mod.TransferEntryState.completed, snapshot.entries.items[0].state);
+
+ try runner.start(.copy, &.{second}, destination, .ask);
+ runner.thread.?.join();
+ runner.thread = null;
+
+ try std.testing.expectEqual(@as(usize, 1), runner.manifest.entries.items.len);
+ try std.testing.expectEqualStrings(second, runner.manifest.entries.items[0].source_path);
+ try std.testing.expectEqual(ops_mod.TransferEntryState.completed, runner.manifest.entries.items[0].state);
+ try std.testing.expectEqual(@as(usize, 1), snapshot.entries.items.len);
+ try std.testing.expectEqualStrings(first, snapshot.entries.items[0].source_path);
+ try std.testing.expectEqual(ops_mod.TransferEntryState.completed, snapshot.entries.items[0].state);
+}
+
test "JobRunner discovers recursive transfer manifest entries" {
const a = std.testing.allocator;
var tmp_buf: [256]u8 = undefined;