From 3245d68be8b3ba7df5b615386880fdd50671852f Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Sat, 21 Nov 2026 14:45:55 +0000 Subject: [PATCH 1/3] test: trace the renames and directory syncs of moveAcross The moveAcross tests could watch named steps and inject a failure at one, but they could not see which directory a sync flushed. A test that moves to a nested name therefore passed whether the code synced the root descriptor or the parent that gained the entry. MoveFaults now takes a trace function. moveAcross reports each rename and each directory sync it completes through that function. A sync reports the open descriptor, so a test can stat it while the trace runs. Without a trace function the calls are the same as before. A unit test records both paths for a move between two flat names in one directory. The rename path makes one rename and one sync of that directory. The copy path makes one rename and two syncs of it, one after the publish and one after the source is removed. --- changelog.d/+moveacross-call-trace.misc.md | 1 + src/fsx.zig | 120 +++++++++++++++++++-- 2 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 changelog.d/+moveacross-call-trace.misc.md diff --git a/changelog.d/+moveacross-call-trace.misc.md b/changelog.d/+moveacross-call-trace.misc.md new file mode 100644 index 0000000..07c4714 --- /dev/null +++ b/changelog.d/+moveacross-call-trace.misc.md @@ -0,0 +1 @@ +`fsx` unit tests trace the renames and directory syncs that `moveAcross` makes. diff --git a/src/fsx.zig b/src/fsx.zig index 3a98713..f9cfed4 100644 --- a/src/fsx.zig +++ b/src/fsx.zig @@ -412,14 +412,32 @@ const MoveFaults = struct { force_cross_device_rename: bool = false, reach_fn: ?*const fn (?*anyopaque, []const u8) void = null, fail_fn: ?*const fn (?*anyopaque, []const u8) ?Error = null, + trace_fn: ?*const fn (?*anyopaque, MoveCall) void = null, ctx: ?*anyopaque = null, }; +/// A rename or directory sync that moveAcross completed; the descriptor of +/// a sync is open only while the trace runs. +const MoveCall = union(enum) { + rename, + fsync: fd_t, +}; + fn moveFaultsStep(faults: MoveFaults, step: []const u8) Error!void { if (faults.reach_fn) |f| f(faults.ctx, step); if (faults.fail_fn) |f| if (f(faults.ctx, step)) |err| return err; } +fn moveRename(faults: MoveFaults, old_dir: fd_t, old_name: []const u8, new_dir: fd_t, new_name: []const u8) Error!void { + try sys.renameat(old_dir, old_name, new_dir, new_name); + if (faults.trace_fn) |f| f(faults.ctx, .rename); +} + +fn moveFsync(faults: MoveFaults, dir: fd_t) Error!void { + try sys.fsync(dir); + if (faults.trace_fn) |f| f(faults.ctx, .{ .fsync = dir }); +} + fn tmpName(base: []const u8, out: *[sys.path_max]u8) Error![]const u8 { var rnd: [4]u8 = undefined; _ = try sys.call(linux.getrandom, .{ &rnd, rnd.len, 0 }); @@ -543,24 +561,24 @@ fn moveAcrossCopy(gpa: Allocator, src_dir: fd_t, src_name: []const u8, dst_dir: } try moveFaultsStep(faults, "move.publish"); - try sys.renameat(dst_dir, tmp, dst_dir, dst_name); + try moveRename(faults, dst_dir, tmp, dst_dir, dst_name); published = true; - try sys.fsync(dst_dir); + try moveFsync(faults, dst_dir); try moveFaultsStep(faults, "move.remove-source"); if (src.isDir()) { try deleteTree(gpa, src_dir, src_name); } else { try sys.unlinkat(src_dir, src_name, 0); } - try sys.fsync(src_dir); + try moveFsync(faults, src_dir); } fn moveAcrossInternal(gpa: Allocator, src_dir: fd_t, src_name: []const u8, dst_dir: fd_t, dst_name: []const u8, opts: MoveAcrossOptions, faults: MoveFaults) (Error || Allocator.Error)!void { try moveFaultsStep(faults, "move.rename"); if (!faults.force_cross_device_rename) { - if (sys.renameat(src_dir, src_name, dst_dir, dst_name)) |_| { - try sys.fsync(dst_dir); - if (src_dir != dst_dir) try sys.fsync(src_dir); + if (moveRename(faults, src_dir, src_name, dst_dir, dst_name)) |_| { + try moveFsync(faults, dst_dir); + if (src_dir != dst_dir) try moveFsync(faults, src_dir); return; } else |err| switch (err) { error.CrossDevice => {}, @@ -1631,6 +1649,96 @@ const MoveProbe = struct { } }; +/// The renames and directory syncs one moveAcross call makes, in order. A +/// sync is kept as the device and inode of the directory it flushed. +const MoveTrace = struct { + calls: [16]Call = undefined, + len: usize = 0, + + const Call = union(enum) { + rename, + fsync: struct { dev: u64, ino: u64 }, + }; + + fn record(ctx: ?*anyopaque, call: MoveCall) void { + const self: *MoveTrace = @ptrCast(@alignCast(ctx.?)); + if (self.len == self.calls.len) return; + self.calls[self.len] = switch (call) { + .rename => .rename, + .fsync => |fd| if (fstat(fd)) |st| .{ .fsync = .{ .dev = st.dev, .ino = st.ino } } else |_| .{ .fsync = .{ .dev = 0, .ino = 0 } }, + }; + self.len += 1; + } + + fn faults(self: *MoveTrace, cross_device: bool) MoveFaults { + return .{ .force_cross_device_rename = cross_device, .trace_fn = record, .ctx = self }; + } + + fn sync(dir: Stat) Call { + return .{ .fsync = .{ .dev = dir.dev, .ino = dir.ino } }; + } + + fn expectCalls(self: *const MoveTrace, want: []const Call) !void { + try std.testing.expectEqualSlices(Call, want, self.calls[0..self.len]); + } +}; + +/// Scratch directory under `.zig-cache` for the moveAcross trace tests. +const MoveScratch = struct { + gpa: Allocator, + base: []u8, + d: fd_t, + + fn init(gpa: Allocator, tag: []const u8) !MoveScratch { + var cwd_buf: [sys.path_max]u8 = undefined; + const here = try realpath(cwd, ".", &cwd_buf); + const base = try std.fmt.allocPrint(gpa, "{s}/.zig-cache/zig-common-{s}-{d}", .{ here, tag, linux.getpid() }); + errdefer gpa.free(base); + var zb: [sys.path_max:0]u8 = undefined; + _ = linux.mkdirat(cwd, (try sys.z(&zb, base)).ptr, 0o700); + return .{ .gpa = gpa, .base = base, .d = try openDir(cwd, base, false) }; + } + + fn deinit(self: MoveScratch) void { + deleteTree(self.gpa, cwd, self.base) catch {}; + sys.close(self.d); + self.gpa.free(self.base); + } + + fn put(self: MoveScratch, name: []const u8) !void { + const fd = try sys.openat(self.d, name, .{ .ACCMODE = .WRONLY, .CREAT = true, .EXCL = true }, 0o644); + defer sys.close(fd); + try sys.writeAll(fd, "moved\n"); + } + + fn expectMoved(self: MoveScratch, src: []const u8, dst: []const u8) !void { + try std.testing.expectError(error.NotFound, statAt(self.d, src, false)); + const got = try openRegular(self.d, dst, false); + defer sys.close(got.fd); + const text = try readAll(self.gpa, got.fd, 64); + defer self.gpa.free(text); + try std.testing.expectEqualStrings("moved\n", text); + } +}; + +test "moveAcross traces the rename and the directory syncs that publish it" { + const gpa = std.testing.allocator; + const s = try MoveScratch.init(gpa, "move-trace"); + defer s.deinit(); + const root = try fstat(s.d); + + try s.put("src"); + var renamed = MoveTrace{}; + try moveAcrossInternal(gpa, s.d, "src", s.d, "dst", .{}, renamed.faults(false)); + try s.expectMoved("src", "dst"); + try renamed.expectCalls(&.{ .rename, MoveTrace.sync(root) }); + + var copied = MoveTrace{}; + try moveAcrossInternal(gpa, s.d, "dst", s.d, "src", .{}, copied.faults(true)); + try s.expectMoved("dst", "src"); + try copied.expectCalls(&.{ .rename, MoveTrace.sync(root), MoveTrace.sync(root) }); +} + test "moveAcross falls back on EXDEV and removes the source after publish" { const gpa = std.testing.allocator; var cwd_buf: [sys.path_max]u8 = undefined; From fc63e598705160f81513b2a279a65e4d8824037b Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Sat, 21 Nov 2026 14:46:22 +0000 Subject: [PATCH 2/3] fix: sync nested moveAcross destination parent moveAcross flushed the root destination descriptor after publishing, so a nested destination left the directory that receives the new entry unsynced. The temporary name already follows the destination parent through the shared helper. Both the rename path and the copy path now open the parent of the destination name and flush it, as copyTo does. The sync goes through the moveAcross trace, so a test can see which directory it reached. A unit test moves a file to a name under a subdirectory, once by rename and once through the forced copy path. Each trace must show a sync of that subdirectory after the last rename. With the root descriptor synced instead, both checks fail. --- ...+moveacross-nested-target-parent.bugfix.md | 1 + src/fsx.zig | 49 ++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 changelog.d/+moveacross-nested-target-parent.bugfix.md diff --git a/changelog.d/+moveacross-nested-target-parent.bugfix.md b/changelog.d/+moveacross-nested-target-parent.bugfix.md new file mode 100644 index 0000000..5706237 --- /dev/null +++ b/changelog.d/+moveacross-nested-target-parent.bugfix.md @@ -0,0 +1 @@ +`fsx.moveAcross` syncs the destination parent directory for nested targets. diff --git a/src/fsx.zig b/src/fsx.zig index f9cfed4..32469af 100644 --- a/src/fsx.zig +++ b/src/fsx.zig @@ -438,6 +438,13 @@ fn moveFsync(faults: MoveFaults, dir: fd_t) Error!void { if (faults.trace_fn) |f| f(faults.ctx, .{ .fsync = dir }); } +/// `fsyncNameParent` for moveAcross, reported to its trace. +fn moveFsyncNameParent(faults: MoveFaults, dir: fd_t, name: []const u8) Error!void { + const parent_dir = try openDir(dir, std.fs.path.dirname(name) orelse ".", true); + defer sys.close(parent_dir); + try moveFsync(faults, parent_dir); +} + fn tmpName(base: []const u8, out: *[sys.path_max]u8) Error![]const u8 { var rnd: [4]u8 = undefined; _ = try sys.call(linux.getrandom, .{ &rnd, rnd.len, 0 }); @@ -563,7 +570,7 @@ fn moveAcrossCopy(gpa: Allocator, src_dir: fd_t, src_name: []const u8, dst_dir: try moveFaultsStep(faults, "move.publish"); try moveRename(faults, dst_dir, tmp, dst_dir, dst_name); published = true; - try moveFsync(faults, dst_dir); + try moveFsyncNameParent(faults, dst_dir, dst_name); try moveFaultsStep(faults, "move.remove-source"); if (src.isDir()) { try deleteTree(gpa, src_dir, src_name); @@ -577,7 +584,7 @@ fn moveAcrossInternal(gpa: Allocator, src_dir: fd_t, src_name: []const u8, dst_d try moveFaultsStep(faults, "move.rename"); if (!faults.force_cross_device_rename) { if (moveRename(faults, src_dir, src_name, dst_dir, dst_name)) |_| { - try moveFsync(faults, dst_dir); + try moveFsyncNameParent(faults, dst_dir, dst_name); if (src_dir != dst_dir) try moveFsync(faults, src_dir); return; } else |err| switch (err) { @@ -1681,6 +1688,19 @@ const MoveTrace = struct { fn expectCalls(self: *const MoveTrace, want: []const Call) !void { try std.testing.expectEqualSlices(Call, want, self.calls[0..self.len]); } + + /// Whether `dir` is synced after the last rename. + fn syncedAfterPublish(self: *const MoveTrace, dir: Stat) bool { + var i = self.len; + while (i > 0) { + i -= 1; + switch (self.calls[i]) { + .rename => return false, + .fsync => |s| if (s.dev == dir.dev and s.ino == dir.ino) return true, + } + } + return false; + } }; /// Scratch directory under `.zig-cache` for the moveAcross trace tests. @@ -1705,6 +1725,12 @@ const MoveScratch = struct { self.gpa.free(self.base); } + fn mkdir(self: MoveScratch, name: []const u8) !Stat { + var zb: [sys.path_max:0]u8 = undefined; + _ = try sys.call(linux.mkdirat, .{ self.d, (try sys.z(&zb, name)).ptr, @as(linux.mode_t, 0o700) }); + return statAt(self.d, name, false); + } + fn put(self: MoveScratch, name: []const u8) !void { const fd = try sys.openat(self.d, name, .{ .ACCMODE = .WRONLY, .CREAT = true, .EXCL = true }, 0o644); defer sys.close(fd); @@ -1739,6 +1765,25 @@ test "moveAcross traces the rename and the directory syncs that publish it" { try copied.expectCalls(&.{ .rename, MoveTrace.sync(root), MoveTrace.sync(root) }); } +test "moveAcross syncs the parent that receives a nested destination" { + const gpa = std.testing.allocator; + const s = try MoveScratch.init(gpa, "move-nested-dst"); + defer s.deinit(); + const sub = try s.mkdir("sub"); + + try s.put("src"); + var renamed = MoveTrace{}; + try moveAcrossInternal(gpa, s.d, "src", s.d, "sub/renamed", .{}, renamed.faults(false)); + try s.expectMoved("src", "sub/renamed"); + try std.testing.expect(renamed.syncedAfterPublish(sub)); + + try s.put("src"); + var copied = MoveTrace{}; + try moveAcrossInternal(gpa, s.d, "src", s.d, "sub/copied", .{}, copied.faults(true)); + try s.expectMoved("src", "sub/copied"); + try std.testing.expect(copied.syncedAfterPublish(sub)); +} + test "moveAcross falls back on EXDEV and removes the source after publish" { const gpa = std.testing.allocator; var cwd_buf: [sys.path_max]u8 = undefined; From 9732e4fb4f33b8e8c64d3df1603089896a700576 Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Sat, 21 Nov 2026 14:46:55 +0000 Subject: [PATCH 3/3] fix: sync the moveAcross source parent beside a nested destination The rename path skipped the source sync when both names came from one descriptor. That was safe while the destination sync flushed the same descriptor. The destination sync now flushes the parent of a nested name. A move from dir/src to dir/sub/dst therefore flushed only dir/sub, and the removal of src from dir was never synced. The copy path synced the source descriptor after the unlink, which is the wrong directory for a nested source name such as a/src. Both paths now sync the parent of the source name. The rename path skips it only when the two names share a descriptor and the same parent path, since one sync then covers both entries. The copy path always syncs it, because the unlink comes after the publish sync. A unit test moves a flat source and a nested source to a nested destination by rename. It then moves a nested source through the forced copy path. Each trace must show syncs of the destination parent and the source parent after the last rename. A move between two names in one subdirectory must make one rename and one sync of that subdirectory. --- .../+moveacross-source-parent.bugfix.md | 1 + src/fsx.zig | 47 ++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 changelog.d/+moveacross-source-parent.bugfix.md diff --git a/changelog.d/+moveacross-source-parent.bugfix.md b/changelog.d/+moveacross-source-parent.bugfix.md new file mode 100644 index 0000000..fc21013 --- /dev/null +++ b/changelog.d/+moveacross-source-parent.bugfix.md @@ -0,0 +1 @@ +`fsx.moveAcross` syncs the parent directory of the source name, also when one descriptor holds both names and the destination is nested. diff --git a/src/fsx.zig b/src/fsx.zig index 32469af..ba38607 100644 --- a/src/fsx.zig +++ b/src/fsx.zig @@ -445,6 +445,14 @@ fn moveFsyncNameParent(faults: MoveFaults, dir: fd_t, name: []const u8) Error!vo try moveFsync(faults, parent_dir); } +/// Whether `a_dir/a_name` and `b_dir/b_name` name entries of the same +/// directory by the same path. False does not mean the directories differ. +fn sameNameParent(a_dir: fd_t, a_name: []const u8, b_dir: fd_t, b_name: []const u8) bool { + const a = std.fs.path.dirname(a_name) orelse "."; + const b = std.fs.path.dirname(b_name) orelse "."; + return a_dir == b_dir and std.mem.eql(u8, a, b); +} + fn tmpName(base: []const u8, out: *[sys.path_max]u8) Error![]const u8 { var rnd: [4]u8 = undefined; _ = try sys.call(linux.getrandom, .{ &rnd, rnd.len, 0 }); @@ -577,7 +585,7 @@ fn moveAcrossCopy(gpa: Allocator, src_dir: fd_t, src_name: []const u8, dst_dir: } else { try sys.unlinkat(src_dir, src_name, 0); } - try moveFsync(faults, src_dir); + try moveFsyncNameParent(faults, src_dir, src_name); } fn moveAcrossInternal(gpa: Allocator, src_dir: fd_t, src_name: []const u8, dst_dir: fd_t, dst_name: []const u8, opts: MoveAcrossOptions, faults: MoveFaults) (Error || Allocator.Error)!void { @@ -585,7 +593,7 @@ fn moveAcrossInternal(gpa: Allocator, src_dir: fd_t, src_name: []const u8, dst_d if (!faults.force_cross_device_rename) { if (moveRename(faults, src_dir, src_name, dst_dir, dst_name)) |_| { try moveFsyncNameParent(faults, dst_dir, dst_name); - if (src_dir != dst_dir) try moveFsync(faults, src_dir); + if (!sameNameParent(src_dir, src_name, dst_dir, dst_name)) try moveFsyncNameParent(faults, src_dir, src_name); return; } else |err| switch (err) { error.CrossDevice => {}, @@ -1784,6 +1792,41 @@ test "moveAcross syncs the parent that receives a nested destination" { try std.testing.expect(copied.syncedAfterPublish(sub)); } +test "moveAcross syncs the source parent beside a nested destination" { + const gpa = std.testing.allocator; + const s = try MoveScratch.init(gpa, "move-nested-src"); + defer s.deinit(); + const root = try fstat(s.d); + const sub = try s.mkdir("sub"); + const a = try s.mkdir("a"); + + try s.put("src"); + var flat = MoveTrace{}; + try moveAcrossInternal(gpa, s.d, "src", s.d, "sub/flat", .{}, flat.faults(false)); + try s.expectMoved("src", "sub/flat"); + try std.testing.expect(flat.syncedAfterPublish(sub)); + try std.testing.expect(flat.syncedAfterPublish(root)); + + try s.put("a/src"); + var renamed = MoveTrace{}; + try moveAcrossInternal(gpa, s.d, "a/src", s.d, "sub/renamed", .{}, renamed.faults(false)); + try s.expectMoved("a/src", "sub/renamed"); + try std.testing.expect(renamed.syncedAfterPublish(sub)); + try std.testing.expect(renamed.syncedAfterPublish(a)); + + try s.put("a/src"); + var copied = MoveTrace{}; + try moveAcrossInternal(gpa, s.d, "a/src", s.d, "sub/copied", .{}, copied.faults(true)); + try s.expectMoved("a/src", "sub/copied"); + try std.testing.expect(copied.syncedAfterPublish(sub)); + try std.testing.expect(copied.syncedAfterPublish(a)); + + var beside = MoveTrace{}; + try moveAcrossInternal(gpa, s.d, "sub/copied", s.d, "sub/beside", .{}, beside.faults(false)); + try s.expectMoved("sub/copied", "sub/beside"); + try beside.expectCalls(&.{ .rename, MoveTrace.sync(sub) }); +} + test "moveAcross falls back on EXDEV and removes the source after publish" { const gpa = std.testing.allocator; var cwd_buf: [sys.path_max]u8 = undefined;