Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/+atomic-oserror-oom.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Keep the original operating-system error when allocation fails while formatting its atomic-write diagnostic.
1 change: 1 addition & 0 deletions changelog.d/+atomic-write-oserror-text.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A failed replacement names the temporary file when the open fails, names both files when the rename fails, and names no file when the write or the flush fails.
1 change: 1 addition & 0 deletions changelog.d/+lock-acquire-filename.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A failed lock acquisition records the OSError text and whether Python names .lock, .lock.posix, the state directory, or no file.
1 change: 1 addition & 0 deletions changelog.d/+makedirs-failed-ancestor.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Report the ancestor that actually failed when recursive state-root or atomic-write directory creation stops.
1 change: 1 addition & 0 deletions changelog.d/+oserror-errno-lifetime.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Reset stale lock errno state and preserve the parent-directory errno after an atomic replacement has been renamed.
1 change: 1 addition & 0 deletions changelog.d/+rootlock-preflight-osfailure.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Record the state-root path and error text when lock acquisition rejects a non-directory or broken symbolic link.
17 changes: 6 additions & 11 deletions docs/orgmode/howto/replace-a-file-keeping-its-acls.org
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,19 @@ A reader sees the old file or the new one, never a part of either, and never the

The steps that follow use =atomicWrite=.

2. Call it with a [[zig:zig_common.atomic.Why]], which receives the reason for a refusal, and free that with =deinit=:
2. Call it with a [[zig:zig_common.atomic.Why]], which receives the reason for any failure, and free that with =deinit=:

#+begin_src zig
var why: common.atomic.Why = .{};
defer why.deinit(gpa);
common.atomic.atomicWrite(gpa, path, bytes, .{}, &why) catch |err| switch (err) {
error.Refused => {
common.cli.err("tool: {s}\n", .{why.msg});
return 1;
},
else => |e| {
common.cli.err("tool: {s}: {s}\n", .{ path, common.atomic.describe(e) });
return 1;
},
common.atomic.atomicWrite(gpa, path, bytes, .{}, &why) catch |err| {
const message = if (why.msg.len > 0) why.msg else common.atomic.describe(err);
common.cli.err("tool: {s}\n", .{message});
return 1;
};
#+end_src

[[zig:zig_common.atomic.describe]] words any other error as Python's =OSError= does, such as =[Errno 28] No space left on device=.
=why.msg= includes the filename or filenames of an operating-system error. If a failure happened before it could be filled, [[zig:zig_common.atomic.describe]] supplies text such as =[Errno 12] Cannot allocate memory=.

3. To give the new file the access of another file, such as a file prepared beside the live one it is to replace, name that file in [[zig:zig_common.atomic.WriteOptions]]:

Expand Down
227 changes: 203 additions & 24 deletions src/atomic.zig

Large diffs are not rendered by default.

109 changes: 106 additions & 3 deletions src/atomic_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ fn touch(fd: sys.fd_t) void {

fn write(path: []const u8, data: []const u8, opts: atomic.WriteOptions) atomic.Error!void {
var why: atomic.Why = .{};
defer why.deinit(A);
atomic.atomicWrite(A, path, data, opts, &why) catch |e| {
last_why = why.msg;
last_why = A.dupe(u8, why.msg) catch "out of memory";
return e;
};
}
Expand Down Expand Up @@ -1716,16 +1717,17 @@ test "atomicWrite frees what it allocates but a refusal's message" {
const s = scenario(&m);
defer cleanup(s.dir);
var why: atomic.Why = .{};
defer why.deinit(gpa);
try atomic.atomicWrite(gpa, s.path, "new", replace, &why);
try atomic.atomicWrite(gpa, join(s.dir, "copy.lua"), "copy", .{ .metadata_from = s.path }, &why);
try testing.expectEqualStrings("", why.msg);
try testing.expectError(error.Refused, atomic.atomicWrite(gpa, s.path, "x", .{ .step = "replace", .faults = .{ .fail_fn = failFsync } }, &why));
gpa.free(why.msg);
why.deinit(gpa);
target = s.path;
hook_errno = .STALE;
m.hooks.get = hFail;
try testing.expectError(error.Refused, atomic.atomicWrite(gpa, s.path, "x", replace, &why));
gpa.free(why.msg);
why.deinit(gpa);
reset(&m);
try testing.expectEqualStrings("new", readFile(s.path));
try expectAttrs(s.expected, m.getPath(s.path));
Expand All @@ -1744,6 +1746,104 @@ fn exdevRenameat(_: sys.fd_t, _: [*:0]const u8, _: sys.fd_t, _: [*:0]const u8) u
return err(.XDEV);
}

fn eioFsync(_: sys.fd_t) usize {
return err(.IO);
}

fn closedOpenat(dir: sys.fd_t, path: [*:0]const u8, flags: sys.O, mode: linux.mode_t) usize {
const rc = linux.openat(dir, path, flags, mode);
if (linux.errno(rc) == .SUCCESS) sys.close(@intCast(rc));
return rc;
}

var stale_path: []const u8 = "";
var parent_path: []const u8 = "";
var moved_parent_path: []const u8 = "";

fn replaceStaleWithDirectory() linux.uid_t {
sys.unlinkat(fsx.cwd, stale_path, 0) catch unreachable;
var zb: [sys.path_max:0]u8 = undefined;
_ = linux.mkdirat(fsx.cwd, (sys.z(&zb, stale_path) catch unreachable).ptr, 0o700);
return linux.geteuid();
}

fn renameThenUseProcParent(old_dir: sys.fd_t, old_path: [*:0]const u8, new_dir: sys.fd_t, new_path: [*:0]const u8) usize {
const rc = linux.renameat(old_dir, old_path, new_dir, new_path);
if (linux.errno(rc) != .SUCCESS) return rc;
_ = linux.renameat(fsx.cwd, z(parent_path).ptr, fsx.cwd, z(moved_parent_path).ptr);
_ = linux.symlinkat("/proc", fsx.cwd, z(parent_path).ptr);
return rc;
}

fn restoreProcParent() void {
_ = linux.unlinkat(fsx.cwd, z(parent_path).ptr, 0);
_ = linux.renameat(fsx.cwd, z(moved_parent_path).ptr, fsx.cwd, z(parent_path).ptr);
}

test "OSError messages cover write, fsync, fstat, and stale unlink failures" {
begin();
defer end();
const dir = tmpdir();
defer cleanup(dir);
const path = join(dir, "fresh");
const tmp = join(dir, ".fresh.new");

atomic.ops.write = enospcWrite;
try testing.expectError(error.NoSpace, write(path, "new", replace));
try testing.expectEqualStrings("[Errno 28] No space left on device", last_why);

atomic.ops.* = .{};
atomic.ops.fsync = eioFsync;
try testing.expectError(error.Unexpected, write(path, "new", replace));
try testing.expectEqualStrings("[Errno 5] Input/output error", last_why);

atomic.ops.* = .{};
atomic.ops.openat = closedOpenat;
try testing.expectError(error.InvalidArgument, write(path, "new", replace));
try testing.expectEqualStrings("[Errno 9] Bad file descriptor", last_why);

atomic.ops.* = .{};
writeFile(tmp, "stale");
stale_path = tmp;
atomic.ops.geteuid = replaceStaleWithDirectory;
try testing.expectError(error.IsDir, write(path, "new", replace));
try testing.expectEqualStrings(try A.print("[Errno 21] Is a directory: '{s}'", .{tmp}), last_why);
}

test "a parent fsync failure carries no filename and keeps its errno" {
begin();
defer end();
const dir = tmpdir();
defer cleanup(dir);
parent_path = dir;
moved_parent_path = A.print("{s}.moved", .{dir}) catch unreachable;
const path = join(dir, "fresh");
atomic.ops.renameat = renameThenUseProcParent;
const result = write(path, "new", replace);
const kept_errno = sys.last_errno;
restoreProcParent();
try testing.expectError(error.InvalidArgument, result);
try testing.expectEqual(linux.E.INVAL, kept_errno);
try testing.expectEqualStrings("[Errno 22] Invalid argument", last_why);
try testing.expectEqualStrings("new", readFile(path));
}

test "a sys-error message can be released with a general-purpose allocator" {
begin();
defer end();
const gpa = testing.allocator;
const dir = tmpdir();
defer cleanup(dir);
const path = join(dir, "fresh");
atomic.ops.write = enospcWrite;
var why: atomic.Why = .{};
defer why.deinit(gpa);
try testing.expectError(error.NoSpace, atomic.atomicWrite(gpa, path, "new", replace, &why));
try testing.expectEqualStrings("[Errno 28] No space left on device", why.msg);
why.deinit(gpa);
try testing.expectEqualStrings("", why.msg);
}

test "atomicWrite returns NoSpace, ReadOnly, and CrossDevice without touching the live file" {
begin();
defer end();
Expand All @@ -1754,20 +1854,23 @@ test "atomicWrite returns NoSpace, ReadOnly, and CrossDevice without touching th
atomic.ops.write = enospcWrite;
try testing.expectError(error.NoSpace, write(pt[0], "new", replace));
try testing.expectEqualStrings("[Errno 28] No space left on device", atomic.describe(error.NoSpace));
try testing.expectEqualStrings("[Errno 28] No space left on device", last_why);
try testing.expect(!exists(pt[1]));
try testing.expectEqualStrings("old", readFile(pt[0]));

atomic.ops.* = .{};
atomic.ops.openat = erofsOpenat;
try testing.expectError(error.ReadOnly, write(pt[0], "new", replace));
try testing.expectEqualStrings("[Errno 30] Read-only file system", atomic.describe(error.ReadOnly));
try testing.expectEqualStrings(try A.print("[Errno 30] Read-only file system: '{s}'", .{pt[1]}), last_why);
try testing.expect(!exists(pt[1]));
try testing.expectEqualStrings("old", readFile(pt[0]));

atomic.ops.* = .{};
atomic.ops.renameat = exdevRenameat;
try testing.expectError(error.CrossDevice, write(pt[0], "new", replace));
try testing.expectEqualStrings("[Errno 18] Invalid cross-device link", atomic.describe(error.CrossDevice));
try testing.expectEqualStrings(try A.print("[Errno 18] Invalid cross-device link: '{s}' -> '{s}'", .{ pt[1], pt[0] }), last_why);
try testing.expect(!exists(pt[1]));
try testing.expectEqualStrings("old", readFile(pt[0]));

Expand Down
Loading
Loading