std: update fchmodat tests

This commit is contained in:
Andrew Kelley 2025-12-11 22:35:55 -08:00
parent 68621afd2e
commit e1cf753db7
5 changed files with 243 additions and 313 deletions

View file

@ -1698,7 +1698,7 @@ pub fn addCheckFile(
return Step.CheckFile.create(b, file_source, options);
}
pub fn truncateFile(b: *Build, dest_path: []const u8) (Io.Dir.MakeError || Io.Dir.StatPathError)!void {
pub fn truncateFile(b: *Build, dest_path: []const u8) (Io.Dir.MakeError || Io.Dir.StatFileError)!void {
const io = b.graph.io;
if (b.verbose) log.info("truncate {s}", .{dest_path});
const cwd = Io.Dir.cwd();

View file

@ -1710,7 +1710,7 @@ pub const SetFilePermissionsOptions = struct {
follow_symlinks: bool = true,
};
/// Also known as "chmodat".
/// Also known as "fchmodat".
pub fn setFilePermissions(
dir: Dir,
io: Io,

View file

@ -873,7 +873,7 @@ pub fn io(k: *Kqueue) Io {
.dirMakePath = dirMakePath,
.dirMakeOpenPath = dirMakeOpenPath,
.dirStat = dirStat,
.dirStatPath = dirStatPath,
.dirStatFile = dirStatFile,
.fileStat = fileStat,
.dirAccess = dirAccess,
@ -1144,7 +1144,7 @@ fn dirStat(userdata: ?*anyopaque, dir: Dir) Dir.StatError!Dir.Stat {
_ = dir;
@panic("TODO");
}
fn dirStatPath(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, options: Dir.StatPathOptions) Dir.StatPathError!File.Stat {
fn dirStatFile(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, options: Dir.StatPathOptions) Dir.StatFileError!File.Stat {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = dir;

File diff suppressed because it is too large Load diff

View file

@ -870,67 +870,6 @@ test "pwrite with empty buffer" {
try expectEqual(rc, 0);
}
fn getFileMode(dir: posix.fd_t, path: []const u8) !posix.mode_t {
const path_z = try posix.toPosixPath(path);
const mode: posix.mode_t = if (native_os == .linux) blk: {
const stx = try linux.wrapped.statx(
dir,
&path_z,
posix.AT.SYMLINK_NOFOLLOW,
.{ .MODE = true },
);
std.debug.assert(stx.mask.MODE);
break :blk stx.mode;
} else blk: {
const st = try posix.fstatatZ(dir, &path_z, posix.AT.SYMLINK_NOFOLLOW);
break :blk st.mode;
};
return mode & 0b111_111_111;
}
fn expectMode(dir: posix.fd_t, file: []const u8, mode: posix.mode_t) !void {
const actual = try getFileMode(dir, file);
try expectEqual(mode, actual & 0b111_111_111);
}
test "fchmodat smoke test" {
if (!Io.File.Permissions.has_executable_bit) return error.SkipZigTest;
var tmp = tmpDir(.{});
defer tmp.cleanup();
try expectError(error.FileNotFound, posix.fchmodat(tmp.dir.handle, "regfile", 0o666, 0));
const fd = try posix.openat(
tmp.dir.handle,
"regfile",
.{ .ACCMODE = .WRONLY, .CREAT = true, .EXCL = true, .TRUNC = true },
0o644,
);
posix.close(fd);
try posix.symlinkat("regfile", tmp.dir.handle, "symlink");
const sym_mode = try getFileMode(tmp.dir.handle, "symlink");
try posix.fchmodat(tmp.dir.handle, "regfile", 0o640, 0);
try expectMode(tmp.dir.handle, "regfile", 0o640);
try posix.fchmodat(tmp.dir.handle, "regfile", 0o600, posix.AT.SYMLINK_NOFOLLOW);
try expectMode(tmp.dir.handle, "regfile", 0o600);
try posix.fchmodat(tmp.dir.handle, "symlink", 0o640, 0);
try expectMode(tmp.dir.handle, "regfile", 0o640);
try expectMode(tmp.dir.handle, "symlink", sym_mode);
var test_link = true;
posix.fchmodat(tmp.dir.handle, "symlink", 0o600, posix.AT.SYMLINK_NOFOLLOW) catch |err| switch (err) {
error.OperationNotSupported => test_link = false,
else => |e| return e,
};
if (test_link)
try expectMode(tmp.dir.handle, "symlink", 0o600);
try expectMode(tmp.dir.handle, "regfile", 0o640);
}
const CommonOpenFlags = packed struct {
ACCMODE: posix.ACCMODE = .RDONLY,
CREAT: bool = false,