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-write-error-tests.misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `atomic.Ops` fault hooks and unit tests covering `atomicWrite` failures on `ENOSPC`, `EROFS`, and `EXDEV`, as well as `makedirs` and `fsyncDir` error paths.
1 change: 1 addition & 0 deletions changelog.d/+cli-write-errors-test.misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add unit tests verifying `cli.put` and `cli.print` ignore descriptor write errors and checking `cli.versionLine` formatting.
1 change: 1 addition & 0 deletions changelog.d/+fsx-error-paths-test.misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add unit tests covering error paths for public file and directory helpers in `src/fsx.zig`.
1 change: 1 addition & 0 deletions changelog.d/+logs-error-tests.misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add unit tests covering `gzipInPlace` error paths (missing file, directory, symlink, hard link, and existing `.gz` archive) in `src/logs.zig`.
1 change: 1 addition & 0 deletions changelog.d/+proc-error-tests.misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add unit tests covering error and boundary paths for `which`, `groupAlive`, `killGroup`, `stopGroup`, and `run` in `src/proc.zig`.
41 changes: 38 additions & 3 deletions src/atomic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ pub const Ops = struct {
fchmod: *const fn (fd: sys.fd_t, mode: u32) usize = realChmod,
fchown: *const fn (fd: sys.fd_t, uid: linux.uid_t, gid: linux.gid_t) usize = realChown,
fsync: *const fn (fd: sys.fd_t) usize = realFsync,
openat: *const fn (dir: sys.fd_t, path: [*:0]const u8, flags: sys.O, mode: linux.mode_t) usize = realOpenat,
write: *const fn (fd: sys.fd_t, bytes: []const u8) usize = realWrite,
renameat: *const fn (old_dir: sys.fd_t, old_path: [*:0]const u8, new_dir: sys.fd_t, new_path: [*:0]const u8) usize = realRenameat,
geteuid: *const fn () linux.uid_t = realGeteuid,
created: *const fn (fd: sys.fd_t) void = realCreated,
};
Expand Down Expand Up @@ -66,6 +69,15 @@ fn realChown(fd: sys.fd_t, uid: linux.uid_t, gid: linux.gid_t) usize {
fn realFsync(fd: sys.fd_t) usize {
return linux.fsync(fd);
}
fn realOpenat(dir: sys.fd_t, path: [*:0]const u8, flags: sys.O, mode: linux.mode_t) usize {
return linux.openat(dir, path, flags, mode);
}
fn realWrite(fd: sys.fd_t, bytes: []const u8) usize {
return linux.write(fd, bytes.ptr, bytes.len);
}
fn realRenameat(old_dir: sys.fd_t, old_path: [*:0]const u8, new_dir: sys.fd_t, new_path: [*:0]const u8) usize {
return linux.renameat(old_dir, old_path, new_dir, new_path);
}
fn realGeteuid() linux.uid_t {
return linux.geteuid();
}
Expand All @@ -78,6 +90,29 @@ fn op(f: anytype, args: anytype) sys.Error!usize {
}
}

fn openTemp(path: []const u8, flags: sys.O, mode: linux.mode_t) sys.Error!sys.fd_t {
var zb: [sys.path_max:0]u8 = undefined;
var f = flags;
f.CLOEXEC = true;
const rc = try op(ops.openat, .{ fsx.cwd, (try sys.z(&zb, path)).ptr, f, mode });
return @intCast(rc);
}

fn writeTemp(fd: sys.fd_t, bytes: []const u8) sys.Error!void {
var off: usize = 0;
while (off < bytes.len) {
const n = try op(ops.write, .{ fd, bytes[off..] });
if (n == 0) return error.NoSpace;
off += n;
}
}

fn renameTemp(old_path: []const u8, new_path: []const u8) sys.Error!void {
var za: [sys.path_max:0]u8 = undefined;
var zb: [sys.path_max:0]u8 = undefined;
_ = try op(ops.renameat, .{ fsx.cwd, (try sys.z(&za, old_path)).ptr, fsx.cwd, (try sys.z(&zb, new_path)).ptr });
}

/// Python's OSError text for an error from one of the calls, so a message
/// reads "[Errno 13] Permission denied" as the Python tool prints it; the
/// errno comes from `sys.last_errno` (see `sys.describe`).
Expand Down Expand Up @@ -445,7 +480,7 @@ pub fn atomicWrite(a: Allocator, path: []const u8, data: []const u8, opts: Write
defer a.free(step);
if (opts.faults.fails(step)) return injected(a, why, step);
}
const fd = try sys.openat(fsx.cwd, tmp, .{ .ACCMODE = .WRONLY, .CREAT = true, .EXCL = true, .NOFOLLOW = true }, 0o600);
const fd = try openTemp(tmp, .{ .ACCMODE = .WRONLY, .CREAT = true, .EXCL = true, .NOFOLLOW = true }, 0o600);
const created = fsx.fstat(fd) catch |err| {
sys.close(fd);
// Created above with O_EXCL, so it is this call's file.
Expand All @@ -456,7 +491,7 @@ pub fn atomicWrite(a: Allocator, path: []const u8, data: []const u8, opts: Write
errdefer discard(tmp, created);
{
defer sys.close(fd);
try sys.writeAll(fd, data);
try writeTemp(fd, data);
var inner: Why = .{};
defer inner.deinit(a);
restoreMetadata(a, fd, metadata, mode, &inner) catch |err| return refuse(a, why, "cannot preserve access metadata for {s}: {s}", .{ path, if (err == error.Refused) inner.msg else describe(err) });
Expand All @@ -479,7 +514,7 @@ pub fn atomicWrite(a: Allocator, path: []const u8, data: []const u8, opts: Write
defer a.free(step);
if (opts.faults.fails(step)) return injected(a, why, step);
}
try sys.renameat(fsx.cwd, tmp, fsx.cwd, path);
try renameTemp(tmp, path);
try fsyncDir(parent);
}

Expand Down
43 changes: 43 additions & 0 deletions src/atomic_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1731,3 +1731,46 @@ test "atomicWrite frees what it allocates but a refusal's message" {
try expectAttrs(s.expected, m.getPath(s.path));
}
}

fn enospcWrite(_: sys.fd_t, _: []const u8) usize {
return err(.NOSPC);
}

fn erofsOpenat(_: sys.fd_t, _: [*:0]const u8, _: sys.O, _: linux.mode_t) usize {
return err(.ROFS);
}

fn exdevRenameat(_: sys.fd_t, _: [*:0]const u8, _: sys.fd_t, _: [*:0]const u8) usize {
return err(.XDEV);
}

test "atomicWrite returns NoSpace, ReadOnly, and CrossDevice without touching the live file" {
begin();
defer end();
const dir = tmpdir();
defer cleanup(dir);
const pt = staleModule(dir);

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.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.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.expect(!exists(pt[1]));
try testing.expectEqualStrings("old", readFile(pt[0]));

try testing.expectError(error.NotDir, atomic.makedirs(join(pt[0], "child")));
try testing.expectError(error.NotFound, atomic.fsyncDir(join(dir, "absent")));
}
13 changes: 13 additions & 0 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,16 @@ pub fn versionLine(comptime tool: []const u8, comptime version: []const u8, comp
return tool ++ " " ++ version ++ " (" ++ @tagName(builtin.cpu.arch) ++ "-" ++ @tagName(builtin.os.tag) ++ "-" ++
@tagName(builtin.abi) ++ ", " ++ @tagName(builtin.mode) ++ ", zig " ++ builtin.zig_version_string ++ ", " ++ commit ++ ")\n";
}

test "put and print ignore write errors and versionLine formats tool and version" {
put(-1, "ignored\n");
print(-1, "ignored {d}\n", .{42});

var big: [9000]u8 = undefined;
@memset(&big, 'x');
print(-1, "{s}", .{big[0..]});

const line = versionLine("demo", "1.2.3", "abc1234");
try std.testing.expect(std.mem.startsWith(u8, line, "demo 1.2.3 ("));
try std.testing.expect(std.mem.endsWith(u8, line, ", abc1234)\n"));
}
55 changes: 55 additions & 0 deletions src/fsx.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1079,3 +1079,58 @@ test "moveAcross keeps the source when publish fails before rename" {
try std.testing.expectEqualStrings("payload\n", text);
try std.testing.expectError(error.NotFound, statAt(d, "dst", false));
}

test "fsx public helpers return expected errors on bad paths and descriptors" {
const gpa = std.testing.allocator;
var cwd_buf: [sys.path_max]u8 = undefined;
const here = try realpath(cwd, ".", &cwd_buf);
var pb: [sys.path_max]u8 = undefined;
const base = try std.mem.print(&pb, "{s}/.zig-cache/zig-common-fsx-err-{d}", .{ here, linux.getpid() });
var zb: [sys.path_max:0]u8 = undefined;
_ = linux.mkdirat(cwd, (try sys.z(&zb, base)).ptr, 0o700);
const d = try openDir(cwd, base, false);
defer {
deleteTree(gpa, cwd, base) catch {};
sys.close(d);
}

const reg_fd = try sys.openat(d, "reg", .{ .ACCMODE = .WRONLY, .CREAT = true }, 0o644);
try sys.writeAll(reg_fd, "0123456789");
sys.close(reg_fd);
var dir_z: [sys.path_max:0]u8 = undefined;
_ = linux.mkdirat(d, (try sys.z(&dir_z, "sub")).ptr, 0o700);
var lnk_z: [sys.path_max:0]u8 = undefined;
_ = linux.symlinkat("reg", d, (try sys.z(&lnk_z, "lnk")).ptr);
var dlnk_z: [sys.path_max:0]u8 = undefined;
_ = linux.symlinkat("sub", d, (try sys.z(&dlnk_z, "dlnk")).ptr);

try std.testing.expectError(error.InvalidArgument, fstat(-1));
try std.testing.expectError(error.NotFound, statAt(d, "missing", false));
var out: [sys.path_max]u8 = undefined;
try std.testing.expectError(error.NotFound, realpathFd(-1, &out));
try std.testing.expectError(error.NotFound, realpath(d, "missing", &out));

try std.testing.expectError(error.NotFound, openRegular(d, "missing", false));
try std.testing.expectError(error.SymLink, openRegular(d, "lnk", false));
try std.testing.expectError(error.InvalidArgument, openRegular(d, "sub", false));

try std.testing.expectError(error.NotFound, openDir(d, "missing", false));
try std.testing.expectError(error.NotDir, openDir(d, "reg", false));
try std.testing.expectError(error.NotDir, openDir(d, "dlnk", false));

const opened = try openRegular(d, "reg", false);
defer sys.close(opened.fd);
try std.testing.expectError(error.SymLink, copyTo(opened.fd, opened.stat, d, "lnk"));
try std.testing.expectError(error.IsDir, copyTo(opened.fd, opened.stat, d, "sub"));
try std.testing.expectError(error.SymLink, appendLine(gpa, d, "lnk", "x"));
try std.testing.expectError(error.IsDir, appendLine(gpa, d, "sub", "x"));
try std.testing.expectError(error.StreamTooLong, readAll(gpa, opened.fd, 4));
try std.testing.expectError(error.NotFound, moveAcross(gpa, d, "missing", d, "dst", .{}));
try std.testing.expectError(error.NotFound, replaceContents(gpa, d, "missing", "x"));
try std.testing.expectError(error.SymLink, replaceContents(gpa, d, "lnk", "x"));
try std.testing.expectError(error.NotFound, entryType(d, .{ .name = "missing", .type = DT.UNKNOWN }));
var bad_it = try DirIter.init(gpa, -1);
defer bad_it.deinit(gpa);
try std.testing.expectError(error.InvalidArgument, bad_it.next());
try std.testing.expectError(error.NotFound, deleteTree(gpa, d, "missing"));
}
40 changes: 40 additions & 0 deletions src/logs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,43 @@ test "renameInList" {
try std.testing.expectEqualStrings("/r/a.log.gz\n/r/b.log\n", got);
try std.testing.expect((try renameInList(gpa, "/r/b.log\n", &m)) == null);
}

test "gzipInPlace refuses missing files, symlinks, directories, hard links, and existing archives" {
const gpa = std.testing.allocator;
var cwd_buf: [sys.path_max]u8 = undefined;
const here = try fsx.realpath(fsx.cwd, ".", &cwd_buf);
var pb: [sys.path_max]u8 = undefined;
const base = try std.mem.print(&pb, "{s}/.zig-cache/zig-common-logs-err-{d}", .{ here, linux.getpid() });
var zb: [sys.path_max:0]u8 = undefined;
_ = linux.mkdirat(fsx.cwd, (try sys.z(&zb, base)).ptr, 0o700);
const dir = try fsx.openDir(fsx.cwd, base, false);
defer {
fsx.deleteTree(gpa, fsx.cwd, base) catch {};
sys.close(dir);
}

const io = std.Io.Threaded.global_single_threaded.io();
try std.testing.expectError(error.NotFound, gzipInPlace(gpa, io, dir, "missing.log"));

var sub_z: [sys.path_max:0]u8 = undefined;
_ = linux.mkdirat(dir, (try sys.z(&sub_z, "sub")).ptr, 0o700);
try std.testing.expectError(error.InvalidArgument, gzipInPlace(gpa, io, dir, "sub"));

const orig = try sys.openat(dir, "orig.log", .{ .ACCMODE = .WRONLY, .CREAT = true }, 0o644);
try sys.writeAll(orig, "hello");
sys.close(orig);

var lnk_z: [sys.path_max:0]u8 = undefined;
_ = linux.symlinkat("orig.log", dir, (try sys.z(&lnk_z, "lnk.log")).ptr);
try std.testing.expectError(error.SymLink, gzipInPlace(gpa, io, dir, "lnk.log"));

var hl_z: [sys.path_max:0]u8 = undefined;
var orig_z: [sys.path_max:0]u8 = undefined;
_ = linux.linkat(dir, (try sys.z(&orig_z, "orig.log")).ptr, dir, (try sys.z(&hl_z, "hard.log")).ptr, 0);
try std.testing.expectError(error.TooManyLinks, gzipInPlace(gpa, io, dir, "orig.log"));
sys.unlinkat(dir, "hard.log", 0) catch {};

const gz = try sys.openat(dir, "orig.log.gz", .{ .ACCMODE = .WRONLY, .CREAT = true }, 0o644);
sys.close(gz);
try std.testing.expectError(error.Exists, gzipInPlace(gpa, io, dir, "orig.log"));
}
40 changes: 40 additions & 0 deletions src/proc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -704,3 +704,43 @@ test "a status lost to an ignored SIGCHLD is an error, not a kill by signal 9" {
try std.testing.expectError(error.NoChild, run(std.testing.allocator, "/bin/sh", &.{ "sh", "-c", "exit 3" }, no_env, .{}));
try std.testing.expectEqual(linux.E.CHILD, sys.last_errno);
}

test "which, groupAlive, and run cover error and boundary paths" {
const gpa = std.testing.allocator;
try std.testing.expectEqual(@as(?[]u8, null), try which(gpa, "", "/bin:/usr/bin"));
try std.testing.expectEqual(@as(?[]u8, null), try which(gpa, "a/b", "/bin:/usr/bin"));
try std.testing.expectEqual(@as(?[]u8, null), try which(gpa, "no-such-zig-common-tool", "/bin:/usr/bin"));

var cwd_buf: [sys.path_max]u8 = undefined;
const here = try fsx.realpath(fsx.cwd, ".", &cwd_buf);
var pb: [sys.path_max]u8 = undefined;
const base = try std.mem.print(&pb, "{s}/.zig-cache/zig-common-proc-err-{d}", .{ here, linux.getpid() });
var zb: [sys.path_max:0]u8 = undefined;
_ = linux.mkdirat(fsx.cwd, (try sys.z(&zb, base)).ptr, 0o700);
const dir = try fsx.openDir(fsx.cwd, base, false);
defer {
fsx.deleteTree(gpa, fsx.cwd, base) catch {};
sys.close(dir);
}
var sub_z: [sys.path_max:0]u8 = undefined;
_ = linux.mkdirat(dir, (try sys.z(&sub_z, "sub")).ptr, 0o755);
const nonexec = try sys.openat(dir, "plain", .{ .ACCMODE = .WRONLY, .CREAT = true }, 0o644);
sys.close(nonexec);
const exec = try sys.openat(dir, "tool", .{ .ACCMODE = .WRONLY, .CREAT = true }, 0o755);
sys.close(exec);

try std.testing.expectEqual(@as(?[]u8, null), try which(gpa, "sub", base));
try std.testing.expectEqual(@as(?[]u8, null), try which(gpa, "plain", base));
const found = (try which(gpa, "tool", base)).?;
defer gpa.free(found);
try std.testing.expect(std.mem.endsWith(u8, found, "/tool"));

try std.testing.expect(!groupAlive(2_000_000_000));
killGroup(0, .TERM);
stopGroup(0);

try std.testing.expectError(error.InvalidArgument, run(gpa, "/bin/\x00sh", &.{"sh"}, no_env, .{}));
try std.testing.expectError(error.InvalidArgument, run(gpa, "/bin/sh", &.{ "sh", "bad\x00arg" }, no_env, .{}));
try std.testing.expectError(error.StreamTooLong, run(gpa, "/bin/sh", &.{ "sh", "-c", "printf 12345678" }, no_env, .{ .stdout_limit = 4 }));
try std.testing.expectError(error.NotFound, run(gpa, "/no/such/zig-common-exe", &.{"x"}, no_env, .{ .exec_error = true }));
}
Loading