Skip to content

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

Add transfer manifest data model

Mela1291f07e403
diff --git a/src/files/ops.zig b/src/files/ops.zig
index 08d497b..3b54d36 100644
--- a/src/files/ops.zig
+++ b/src/files/ops.zig
@@ -37,6 +37,87 @@ pub const ItemOutcome = enum {
cancelled,
};
+pub const TransferEntryKind = enum {
+ file,
+ directory,
+ symlink,
+};
+
+pub const TransferEntryState = enum {
+ pending,
+ copying,
+ completed,
+ skipped,
+ failed,
+ cancelled,
+};
+
+pub const TransferEntry = struct {
+ source_path: []u8,
+ relative_path: []u8,
+ destination_path: ?[]u8 = null,
+ size: u64 = 0,
+ kind: TransferEntryKind,
+ state: TransferEntryState = .pending,
+ root_index: usize,
+};
+
+/// Owns the paths and entries describing one logical transfer.
+/// Synchronization is provided by the owning JobRunner.
+pub const TransferManifest = struct {
+ allocator: Allocator,
+ entries: std.ArrayList(TransferEntry) = .empty,
+
+ pub fn init(allocator: Allocator) TransferManifest {
+ return .{ .allocator = allocator };
+ }
+
+ pub fn append(
+ self: *TransferManifest,
+ source_path: []const u8,
+ relative_path: []const u8,
+ destination_path: ?[]const u8,
+ size: u64,
+ kind: TransferEntryKind,
+ root_index: usize,
+ ) !void {
+ const owned_source = try self.allocator.dupe(u8, source_path);
+ errdefer self.allocator.free(owned_source);
+
+ const owned_relative = try self.allocator.dupe(u8, relative_path);
+ errdefer self.allocator.free(owned_relative);
+
+ const owned_destination = if (destination_path) |path|
+ try self.allocator.dupe(u8, path)
+ else
+ null;
+ errdefer if (owned_destination) |path| self.allocator.free(path);
+
+ try self.entries.append(self.allocator, .{
+ .source_path = owned_source,
+ .relative_path = owned_relative,
+ .destination_path = owned_destination,
+ .size = size,
+ .kind = kind,
+ .root_index = root_index,
+ });
+ }
+
+ pub fn clearRetainingCapacity(self: *TransferManifest) void {
+ for (self.entries.items) |entry| {
+ self.allocator.free(entry.source_path);
+ self.allocator.free(entry.relative_path);
+ if (entry.destination_path) |path| self.allocator.free(path);
+ }
+ self.entries.clearRetainingCapacity();
+ }
+
+ pub fn deinit(self: *TransferManifest) void {
+ self.clearRetainingCapacity();
+ self.entries.deinit(self.allocator);
+ }
+};
+
pub const ItemResult = struct {
src: []const u8,
dst: ?[]const u8 = null,
@@ -522,6 +603,7 @@ pub const JobRunner = struct {
sources: std.ArrayList([]const u8) = .empty,
outcomes: std.ArrayList(ItemOutcome) = .empty,
destination_paths: std.ArrayList([]u8) = .empty,
+ manifest: TransferManifest,
dest_dir: []u8 = &[_]u8{},
member: ?[]u8 = null,
result_path: ?[]u8 = null,
@@ -558,6 +640,7 @@ pub const JobRunner = struct {
self.* = .{
.allocator = allocator,
.sources = .empty,
+ .manifest = TransferManifest.init(allocator),
};
if (c.pthread_mutex_init(&self.mutex, null) != 0) return error.MutexInitFailed;
if (c.pthread_cond_init(&self.cond, null) != 0) {
@@ -589,6 +672,7 @@ pub const JobRunner = struct {
_ = c.close(self.wake_fd);
self.clearStrings();
+ self.manifest.deinit();
for (self.temporary_dirs.items) |path| {
deleteRecursive(self.allocator, path) catch {};
self.allocator.free(path);
@@ -648,6 +732,7 @@ pub const JobRunner = struct {
errdefer _ = c.pthread_mutex_unlock(&self.mutex);
self.clearStrings();
self.state = .idle;
+ self.manifest.clearRetainingCapacity();
for (self.sources.items) |s| self.allocator.free(s);
self.sources.clearRetainingCapacity();
for (self.destination_paths.items) |path| {
diff --git a/src/files/ops_tests.zig b/src/files/ops_tests.zig
index 116e368..bd4bd82 100644
--- a/src/files/ops_tests.zig
+++ b/src/files/ops_tests.zig
@@ -39,6 +39,51 @@ pub fn testFileExists(path: []const u8) bool {
return c.stat(path_z, &st) == 0;
}
+test "transfer manifest owns entry paths and clears safely" {
+ const allocator = std.testing.allocator;
+ var manifest = ops_mod.TransferManifest.init(allocator);
+ defer manifest.deinit();
+
+ var source = [_]u8{ '/', 's', 'r', 'c' };
+ var relative = [_]u8{ 'f', 'i', 'l', 'e', '.', 't', 'x', 't' };
+ var destination = [_]u8{ '/', 'd', 's', 't' };
+
+ try manifest.append(
+ &source,
+ &relative,
+ &destination,
+ 128,
+ .file,
+ 0,
+ );
+
+ source[1] = 'x';
+ relative[0] = 'x';
+ destination[1] = 'x';
+
+ try std.testing.expectEqual(@as(usize, 1), manifest.entries.items.len);
+ const first = manifest.entries.items[0];
+ try std.testing.expectEqualStrings("/src", first.source_path);
+ try std.testing.expectEqualStrings("file.txt", first.relative_path);
+ try std.testing.expectEqualStrings("/dst", first.destination_path.?);
+ try std.testing.expectEqual(@as(u64, 128), first.size);
+ try std.testing.expectEqual(ops_mod.TransferEntryKind.file, first.kind);
+ try std.testing.expectEqual(ops_mod.TransferEntryState.pending, first.state);
+ try std.testing.expectEqual(@as(usize, 0), first.root_index);
+
+ try manifest.append("/src/folder", "folder", null, 0, .directory, 0);
+ try std.testing.expect(manifest.entries.items[1].destination_path == null);
+ try std.testing.expectEqual(ops_mod.TransferEntryKind.directory, manifest.entries.items[1].kind);
+
+ manifest.clearRetainingCapacity();
+ try std.testing.expectEqual(@as(usize, 0), manifest.entries.items.len);
+
+ try manifest.append("/another", "", null, 0, .symlink, 1);
+ try std.testing.expectEqual(@as(usize, 1), manifest.entries.items.len);
+ try std.testing.expectEqualStrings("/another", manifest.entries.items[0].source_path);
+ try std.testing.expectEqual(ops_mod.TransferEntryKind.symlink, manifest.entries.items[0].kind);
+}
+
test "copyRecursive and copySymlink" {
const allocator = std.testing.allocator;
var tmp_path_buf: [256]u8 = undefined;