Skip to content

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

Resolve transfer manifest destination paths

Mel61305947085f
diff --git a/src/files/ops.zig b/src/files/ops.zig
index 3189793..9e8cbff 100644
--- a/src/files/ops.zig
+++ b/src/files/ops.zig
@@ -1043,6 +1043,42 @@ pub const JobRunner = struct {
slot.* = owned;
}
+ /// Resolves every manifest entry beneath one selected source root.
+ /// The update is committed only after all destination paths are allocated.
+ fn resolveManifestDestinations(self: *JobRunner, root_index: usize, root_destination: []const u8) !void {
+ _ = c.pthread_mutex_lock(&self.mutex);
+ defer _ = c.pthread_mutex_unlock(&self.mutex);
+
+ const entries = self.manifest.entries.items;
+ const resolved = try self.allocator.alloc(?[]u8, entries.len);
+ defer self.allocator.free(resolved);
+ @memset(resolved, null);
+ errdefer {
+ for (resolved) |path| {
+ if (path) |owned| self.allocator.free(owned);
+ }
+ }
+
+ for (entries, 0..) |entry, idx| {
+ if (entry.root_index != root_index) continue;
+ resolved[idx] = if (entry.relative_path.len == 0)
+ try self.allocator.dupe(u8, root_destination)
+ else
+ try std.fmt.allocPrint(self.allocator, "{s}/{s}", .{
+ root_destination,
+ entry.relative_path,
+ });
+ }
+
+ for (self.manifest.entries.items, 0..) |*entry, idx| {
+ if (resolved[idx]) |path| {
+ if (entry.destination_path) |old| self.allocator.free(old);
+ entry.destination_path = path;
+ resolved[idx] = null;
+ }
+ }
+ }
+
/// Only finished jobs expose results, so their source strings remain stable.
pub fn movedSuccessfully(self: *JobRunner, path: []const u8) bool {
_ = c.pthread_mutex_lock(&self.mutex);
@@ -1058,7 +1094,6 @@ pub const JobRunner = struct {
const name = std.fs.path.basename(src);
var target = try std.fmt.allocPrint(self.allocator, "{s}/{s}", .{ self.dest_dir, name });
defer self.allocator.free(target);
- try self.recordDestinationPath(target);
var buf: [4096]u8 = undefined;
const target_z = try std.fmt.bufPrintSentinel(&buf, "{s}", .{target}, 0);
var st: c.struct_stat = undefined;
@@ -1090,12 +1125,13 @@ pub const JobRunner = struct {
const replacement = try std.fmt.allocPrint(self.allocator, "{s}/{s}", .{ self.dest_dir, unique });
self.allocator.free(target);
target = replacement;
- try self.recordDestinationPath(target);
},
}
}
if (self.cancel_flag.load(.acquire)) return .cancelled;
if (isDescendantOrSame(src, target)) return error.CannotCopyIntoDescendant;
+ try self.resolveManifestDestinations(self.current_item_index, target);
+ try self.recordDestinationPath(target);
if (self.kind == .copy) {
try copyRecursiveTracked(self.allocator, src, target, &self.cancel_flag, self);
} else {
diff --git a/src/files/ops_tests.zig b/src/files/ops_tests.zig
index 5873ad7..8f68cdb 100644
--- a/src/files/ops_tests.zig
+++ b/src/files/ops_tests.zig
@@ -376,6 +376,22 @@ test "JobRunner copy with conflict resolution (Skip and Rename)" {
const read_content = try readTestFile(dst_item1, &orig_buf);
try std.testing.expectEqualStrings("original in dst", read_content);
+ const dst_item2 = try std.fmt.allocPrint(allocator, "{s}/item2.txt", .{dst_dir});
+ defer allocator.free(dst_item2);
+ var found_skipped = false;
+ var found_successful = false;
+ for (runner.manifest.entries.items) |entry| {
+ if (std.mem.eql(u8, entry.source_path, f1_path)) {
+ found_skipped = true;
+ try std.testing.expect(entry.destination_path == null);
+ } else if (std.mem.eql(u8, entry.source_path, f2_path)) {
+ found_successful = true;
+ try std.testing.expectEqualStrings(dst_item2, entry.destination_path.?);
+ }
+ }
+ try std.testing.expect(found_skipped);
+ try std.testing.expect(found_successful);
+
// Test 2: Conflict action = .rename
try runner.start(.copy, sources[0..1], dst_dir, .rename);
@@ -393,6 +409,74 @@ test "JobRunner copy with conflict resolution (Skip and Rename)" {
const renamed_dst = try std.fmt.allocPrint(allocator, "{s}/item1 (1).txt", .{dst_dir});
defer allocator.free(renamed_dst);
try std.testing.expect(testFileExists(renamed_dst));
+ try std.testing.expectEqual(@as(usize, 1), runner.manifest.entries.items.len);
+ try std.testing.expectEqualStrings(
+ renamed_dst,
+ runner.manifest.entries.items[0].destination_path.?,
+ );
+}
+
+test "JobRunner resolves renamed directory destinations for descendants" {
+ 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 source = try std.fmt.allocPrint(a, "{s}/source", .{tmp});
+ defer a.free(source);
+ try ops_mod.createFolder(a, tmp, "source");
+
+ const nested = try std.fmt.allocPrint(a, "{s}/nested", .{source});
+ defer a.free(nested);
+ try ops_mod.createFolder(a, source, "nested");
+
+ const file = try std.fmt.allocPrint(a, "{s}/nested/data.txt", .{source});
+ defer a.free(file);
+ try writeTestFile(file, "payload");
+
+ const destination = try std.fmt.allocPrint(a, "{s}/destination", .{tmp});
+ defer a.free(destination);
+ try ops_mod.createFolder(a, tmp, "destination");
+
+ const conflict = try std.fmt.allocPrint(a, "{s}/source", .{destination});
+ defer a.free(conflict);
+ try ops_mod.createFolder(a, destination, "source");
+
+ const runner = try ops_mod.JobRunner.init(a);
+ defer runner.deinit();
+
+ try runner.start(.copy, &.{source}, destination, .rename);
+ runner.thread.?.join();
+ runner.thread = null;
+
+ const renamed_root = try std.fmt.allocPrint(a, "{s}/source (1)", .{destination});
+ defer a.free(renamed_root);
+ const renamed_nested = try std.fmt.allocPrint(a, "{s}/nested", .{renamed_root});
+ defer a.free(renamed_nested);
+ const renamed_file = try std.fmt.allocPrint(a, "{s}/data.txt", .{renamed_nested});
+ defer a.free(renamed_file);
+
+ try std.testing.expect(testFileExists(renamed_file));
+ try std.testing.expectEqual(@as(usize, 3), runner.manifest.entries.items.len);
+
+ var found_root = false;
+ var found_nested = false;
+ var found_file = false;
+ for (runner.manifest.entries.items) |entry| {
+ if (std.mem.eql(u8, entry.source_path, source)) {
+ found_root = true;
+ try std.testing.expectEqualStrings(renamed_root, entry.destination_path.?);
+ } else if (std.mem.eql(u8, entry.source_path, nested)) {
+ found_nested = true;
+ try std.testing.expectEqualStrings(renamed_nested, entry.destination_path.?);
+ } else if (std.mem.eql(u8, entry.source_path, file)) {
+ found_file = true;
+ try std.testing.expectEqualStrings(renamed_file, entry.destination_path.?);
+ }
+ }
+ try std.testing.expect(found_root);
+ try std.testing.expect(found_nested);
+ try std.testing.expect(found_file);
}
test "JobRunner reports byte totals for recursive copies and moves" {