Skip to content
Merged
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/+create-private-close.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`state.createPrivateAt` returns a failed close of the file it creates as `sys.Error`, with that errno in `sys.last_errno`. The created name is left in place, including when the close fails. Before this change the close was ignored, so a write that a network file system (NFS) reported lost at the close was returned as success. A close that `EINTR` interrupts is not retried and is not an error, as `sys.result` treats `EINTR`. After an earlier error, the cleanup close ignores its result and keeps that error's errno. The close goes through `state.write_ops.close`, the same replaceable call `state.writePrivate` uses.
27 changes: 23 additions & 4 deletions src/state.zig
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,30 @@ pub fn readPrivate(gpa: Allocator, dir: fsx.fd_t, name: []const u8, limit: usize
/// umask cannot bite it. The name is created and closed in one step;
/// nothing is renamed, so another name's file is never touched. A
/// symbolic link at the name is refused without being followed, as
/// `openPrivate` refuses one.
/// `openPrivate` refuses one. The descriptor is closed once on every
/// path. A failed close is returned with
/// `sys.last_errno` set to its errno. The created name is left in place
/// on a failed close, and on a failed fchmod or write: nothing unlinks
/// it. A write lost on NFS may be reported only by the close. A close
/// that EINTR interrupts is not retried, as Linux has freed the
/// descriptor, and is not an error, as `sys.result` treats EINTR. A
/// close that only releases the descriptor after an earlier error
/// ignores its result, so `sys.last_errno` stays that error's errno.
pub fn createPrivateAt(dir: fsx.fd_t, name: []const u8, bytes: []const u8, mode: u32) Error!void {
const fd = try sys.openat(dir, name, .{ .ACCMODE = .WRONLY, .CREAT = true, .EXCL = true, .NOFOLLOW = true, .NONBLOCK = true }, 0o600);
defer sys.close(fd);
var open = true;
defer {
if (open) {
// The error being returned keeps its errno.
const saved = sys.last_errno;
defer sys.last_errno = saved;
_ = write_ops.close(fd);
}
}
try sys.fchmod(fd, mode);
try sys.writeAll(fd, bytes);
open = false;
_ = try sys.result(write_ops.close(fd));
}

/// Replace `dir/name` with `bytes`: written to `.NAME.PID.RANDOM.tmp` with
Expand Down Expand Up @@ -235,8 +253,9 @@ pub fn writePrivate(gpa: Allocator, dir: fsx.fd_t, name: []const u8, bytes: []co
done = true;
}

/// The calls `writePrivate` makes on the temporary file, as raw
/// system-call results. Replaceable in the same builds as `lock_ops`;
/// Raw results of the calls `writePrivate` makes on its temporary file
/// (`fsync` and `close`) and of the close `createPrivateAt` checks on
/// the file it creates. Replaceable in the same builds as `lock_ops`;
/// constant real calls in any other build.
pub const WriteOps = struct {
fsync: *const fn (fd: fsx.fd_t) usize = realFsync,
Expand Down
37 changes: 37 additions & 0 deletions src/state_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -689,3 +689,40 @@ test "writePrivate closes the written file once when it succeeds" {
try expectContents(gpa, dir, "c.json", "new\n");
try expectNoTemporary(gpa, dir);
}

test "a failed close of the file createPrivateAt creates is returned" {
const gpa = testing.allocator;
const root = try scratchRoot(gpa);
defer removeRoot(gpa, root);
defer state.write_ops.* = .{};
const dir = try fsx.openDir(fsx.cwd, root, false);
defer sys.close(dir);
const before = try test_support.fdCount(gpa);
WriteHooks.set(.SUCCESS, .IO);
sys.last_errno = .SUCCESS;
const created = state.createPrivateAt(dir, "new", "bytes\n", 0o640);
state.write_ops.* = .{};
try testing.expectError(error.Unexpected, created);
try testing.expectEqual(linux.E.IO, sys.last_errno);
try testing.expectEqual(@as(usize, 1), WriteHooks.closes);
try testing.expectEqual(before, try test_support.fdCount(gpa));
try expectContents(gpa, dir, "new", "bytes\n");
try testing.expectEqual(@as(u32, 0o640), (try fsx.statAt(dir, "new", false)).perm());
}

test "a close that EINTR interrupts is success for the file createPrivateAt creates" {
const gpa = testing.allocator;
const root = try scratchRoot(gpa);
defer removeRoot(gpa, root);
defer state.write_ops.* = .{};
const dir = try fsx.openDir(fsx.cwd, root, false);
defer sys.close(dir);
const before = try test_support.fdCount(gpa);
WriteHooks.set(.SUCCESS, .INTR);
try state.createPrivateAt(dir, "new", "bytes\n", 0o640);
state.write_ops.* = .{};
try testing.expectEqual(@as(usize, 1), WriteHooks.closes);
try testing.expectEqual(before, try test_support.fdCount(gpa));
try expectContents(gpa, dir, "new", "bytes\n");
try testing.expectEqual(@as(u32, 0o640), (try fsx.statAt(dir, "new", false)).perm());
}
Loading