| ... | ... | @@ -1217,16 +1217,46 @@ test "pwrite with empty buffer" { |
| 1217 | 1217 | _ = try os.pwrite(file.handle, bytes, 0); |
| 1218 | 1218 | } |
| 1219 | 1219 | |
| 1220 | fn expectMode(dir: os.fd_t, file: []const u8, mode: os.mode_t) !void { |
| 1221 | const st = try os.fstatat(dir, file, os.AT.SYMLINK_NOFOLLOW); |
| 1222 | try expectEqual(mode, st.mode & 0b111_111_111); |
| 1223 | } |
| 1224 | |
| 1220 | 1225 | test "fchmodat smoke test" { |
| 1221 | 1226 | if (!std.fs.has_executable_bit) return error.SkipZigTest; |
| 1222 | 1227 | |
| 1223 | 1228 | var tmp = tmpDir(.{}); |
| 1224 | 1229 | defer tmp.cleanup(); |
| 1225 | 1230 | |
| 1226 | | try expectError(error.FileNotFound, os.fchmodat(tmp.dir.fd, "foo.txt", 0o666, 0)); |
| 1227 | | const fd = try os.openat(tmp.dir.fd, "foo.txt", os.O.RDWR | os.O.CREAT | os.O.EXCL, 0o666); |
| 1231 | try expectError(error.FileNotFound, os.fchmodat(tmp.dir.fd, "regfile", 0o666, 0)); |
| 1232 | const fd = try os.openat( |
| 1233 | tmp.dir.fd, |
| 1234 | "regfile", |
| 1235 | os.O.WRONLY | os.O.CREAT | os.O.EXCL | os.O.TRUNC, |
| 1236 | 0o644, |
| 1237 | ); |
| 1228 | 1238 | os.close(fd); |
| 1229 | | try os.fchmodat(tmp.dir.fd, "foo.txt", 0o755, 0); |
| 1230 | | const st = try os.fstatat(tmp.dir.fd, "foo.txt", 0); |
| 1231 | | try expectEqual(@as(os.mode_t, 0o755), st.mode & 0b111_111_111); |
| 1239 | try os.symlinkat("regfile", tmp.dir.fd, "symlink"); |
| 1240 | const sym_mode = blk: { |
| 1241 | const st = try os.fstatat(tmp.dir.fd, "symlink", os.AT.SYMLINK_NOFOLLOW); |
| 1242 | break :blk st.mode & 0b111_111_111; |
| 1243 | }; |
| 1244 | |
| 1245 | try os.fchmodat(tmp.dir.fd, "regfile", 0o640, 0); |
| 1246 | try expectMode(tmp.dir.fd, "regfile", 0o640); |
| 1247 | try os.fchmodat(tmp.dir.fd, "regfile", 0o600, os.AT.SYMLINK_NOFOLLOW); |
| 1248 | try expectMode(tmp.dir.fd, "regfile", 0o600); |
| 1249 | |
| 1250 | try os.fchmodat(tmp.dir.fd, "symlink", 0o640, 0); |
| 1251 | try expectMode(tmp.dir.fd, "regfile", 0o640); |
| 1252 | try expectMode(tmp.dir.fd, "symlink", sym_mode); |
| 1253 | |
| 1254 | var test_link = true; |
| 1255 | os.fchmodat(tmp.dir.fd, "symlink", 0o600, os.AT.SYMLINK_NOFOLLOW) catch |err| switch (err) { |
| 1256 | error.OperationNotSupported => test_link = false, |
| 1257 | else => |e| return e, |
| 1258 | }; |
| 1259 | if (test_link) |
| 1260 | try expectMode(tmp.dir.fd, "symlink", 0o600); |
| 1261 | try expectMode(tmp.dir.fd, "regfile", 0o640); |
| 1232 | 1262 | } |