Skip to content

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

Discover recursive transfer manifest entries

Melcac9fae35fc8
diff --git a/src/files/ops.zig b/src/files/ops.zig
index 3b54d36..3189793 100644
--- a/src/files/ops.zig
+++ b/src/files/ops.zig
@@ -318,6 +318,8 @@ fn measureBytes(
path: []const u8,
cancel: ?*std.atomic.Value(bool),
runner: ?*JobRunner,
+ root_index: usize,
+ relative_path: []const u8,
) !u64 {
if (runner) |job| if (!job.waitIfResumed(cancel)) return error.Cancelled;
if (cancel != null and cancel.?.load(.acquire)) return error.Cancelled;
@@ -329,12 +331,27 @@ fn measureBytes(
if (c.lstat(path_z, &st) != 0) return error.StatFailed;
const fmt = st.st_mode & c.S_IFMT;
- if (fmt == c.S_IFLNK) return 0;
- if (fmt == c.S_IFREG) {
+ const kind: TransferEntryKind = if (fmt == c.S_IFLNK)
+ .symlink
+ else if (fmt == c.S_IFREG)
+ .file
+ else if (fmt == c.S_IFDIR)
+ .directory
+ else
+ return error.UnsupportedFileType;
+
+ const size: u64 = if (kind == .file) blk: {
if (st.st_size < 0) return error.InvalidFileSize;
- return @intCast(st.st_size);
+ break :blk @intCast(st.st_size);
+ } else 0;
+
+ if (runner) |job| {
+ _ = c.pthread_mutex_lock(&job.mutex);
+ defer _ = c.pthread_mutex_unlock(&job.mutex);
+ try job.manifest.append(path, relative_path, null, size, kind, root_index);
}
- if (fmt != c.S_IFDIR) return error.UnsupportedFileType;
+
+ if (kind == .symlink or kind == .file) return size;
const dir = c.opendir(path_z) orelse return error.OpenDirectoryFailed;
defer _ = c.closedir(dir);
@@ -354,7 +371,12 @@ 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, runner);
+ const child_relative = if (relative_path.len == 0)
+ try allocator.dupe(u8, name)
+ else
+ try std.fmt.allocPrint(allocator, "{s}/{s}", .{ relative_path, name });
+ defer allocator.free(child_relative);
+ const child_bytes = try measureBytes(allocator, child, cancel, runner, root_index, child_relative);
if (child_bytes > std.math.maxInt(u64) - total) return error.SizeOverflow;
total += child_bytes;
}
@@ -1132,7 +1154,10 @@ pub const JobRunner = struct {
known = false;
break;
}
- const bytes = measureBytes(self.allocator, src, &self.cancel_flag, self) catch {
+ const bytes = measureBytes(self.allocator, src, &self.cancel_flag, self, idx, "") catch {
+ _ = c.pthread_mutex_lock(&self.mutex);
+ self.manifest.clearRetainingCapacity();
+ _ = c.pthread_mutex_unlock(&self.mutex);
known = false;
break;
};
@@ -1143,6 +1168,11 @@ pub const JobRunner = struct {
}
total += bytes;
}
+ if (self.cancel_flag.load(.acquire)) {
+ _ = c.pthread_mutex_lock(&self.mutex);
+ self.manifest.clearRetainingCapacity();
+ _ = c.pthread_mutex_unlock(&self.mutex);
+ }
_ = c.pthread_mutex_lock(&self.mutex);
self.total_bytes = total;
self.total_bytes_known = known;
diff --git a/src/files/ops_tests.zig b/src/files/ops_tests.zig
index bd4bd82..5873ad7 100644
--- a/src/files/ops_tests.zig
+++ b/src/files/ops_tests.zig
@@ -84,6 +84,104 @@ test "transfer manifest owns entry paths and clears safely" {
try std.testing.expectEqual(ops_mod.TransferEntryKind.symlink, manifest.entries.items[0].kind);
}
+test "JobRunner discovers recursive transfer manifest entries" {
+ 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 first = try std.fmt.allocPrint(a, "{s}/first.txt", .{source});
+ defer a.free(first);
+ try writeTestFile(first, "hello");
+
+ const second = try std.fmt.allocPrint(a, "{s}/second.txt", .{nested});
+ defer a.free(second);
+ try writeTestFile(second, "world!");
+
+ const link = try std.fmt.allocPrint(a, "{s}/link", .{nested});
+ defer a.free(link);
+ const link_z = try a.dupeSentinel(u8, link, 0);
+ defer a.free(link_z);
+ try std.testing.expectEqual(@as(c_int, 0), c.symlink("../first.txt", link_z));
+
+ const second_root = try std.fmt.allocPrint(a, "{s}/standalone.txt", .{tmp});
+ defer a.free(second_root);
+ try writeTestFile(second_root, "another");
+
+ const destination = try std.fmt.allocPrint(a, "{s}/destination", .{tmp});
+ defer a.free(destination);
+ try ops_mod.createFolder(a, tmp, "destination");
+
+ const runner = try ops_mod.JobRunner.init(a);
+ defer runner.deinit();
+
+ try runner.start(.copy, &.{ source, second_root }, destination, .ask);
+ runner.thread.?.join();
+ runner.thread = null;
+
+ try std.testing.expectEqual(@as(usize, 6), runner.manifest.entries.items.len);
+
+ var found_root = false;
+ var found_first = false;
+ var found_nested = false;
+ var found_second = false;
+ var found_link = false;
+ var found_second_root = false;
+
+ for (runner.manifest.entries.items) |entry| {
+ if (std.mem.eql(u8, entry.source_path, source)) {
+ found_root = true;
+ try std.testing.expectEqualStrings("", entry.relative_path);
+ try std.testing.expectEqual(ops_mod.TransferEntryKind.directory, entry.kind);
+ try std.testing.expectEqual(@as(usize, 0), entry.root_index);
+ } else if (std.mem.eql(u8, entry.source_path, first)) {
+ found_first = true;
+ try std.testing.expectEqualStrings("first.txt", entry.relative_path);
+ try std.testing.expectEqual(ops_mod.TransferEntryKind.file, entry.kind);
+ try std.testing.expectEqual(@as(u64, 5), entry.size);
+ try std.testing.expectEqual(@as(usize, 0), entry.root_index);
+ } else if (std.mem.eql(u8, entry.source_path, nested)) {
+ found_nested = true;
+ try std.testing.expectEqualStrings("nested", entry.relative_path);
+ try std.testing.expectEqual(ops_mod.TransferEntryKind.directory, entry.kind);
+ try std.testing.expectEqual(@as(usize, 0), entry.root_index);
+ } else if (std.mem.eql(u8, entry.source_path, second)) {
+ found_second = true;
+ try std.testing.expectEqualStrings("nested/second.txt", entry.relative_path);
+ try std.testing.expectEqual(ops_mod.TransferEntryKind.file, entry.kind);
+ try std.testing.expectEqual(@as(u64, 6), entry.size);
+ try std.testing.expectEqual(@as(usize, 0), entry.root_index);
+ } else if (std.mem.eql(u8, entry.source_path, link)) {
+ found_link = true;
+ try std.testing.expectEqualStrings("nested/link", entry.relative_path);
+ try std.testing.expectEqual(ops_mod.TransferEntryKind.symlink, entry.kind);
+ try std.testing.expectEqual(@as(u64, 0), entry.size);
+ try std.testing.expectEqual(@as(usize, 0), entry.root_index);
+ } else if (std.mem.eql(u8, entry.source_path, second_root)) {
+ found_second_root = true;
+ try std.testing.expectEqualStrings("", entry.relative_path);
+ try std.testing.expectEqual(ops_mod.TransferEntryKind.file, entry.kind);
+ try std.testing.expectEqual(@as(u64, 7), entry.size);
+ try std.testing.expectEqual(@as(usize, 1), entry.root_index);
+ }
+ }
+
+ try std.testing.expect(found_root);
+ try std.testing.expect(found_first);
+ try std.testing.expect(found_nested);
+ try std.testing.expect(found_second);
+ try std.testing.expect(found_link);
+ try std.testing.expect(found_second_root);
+}
+
test "copyRecursive and copySymlink" {
const allocator = std.testing.allocator;
var tmp_path_buf: [256]u8 = undefined;