authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-18 14:36:50-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-23 22:15:10-08:00
logd8b1cc953eef07079968093babcd1ce3a2020c73
tree35ee87c2c853b19abbd0b36592eebaa395b08a20
parente205b13ffbfe0e57e39bb324799785450c9a1da5

std.Io.Dir.renameAbsolute: consistent parameter order


3 files changed, 52 insertions(+), 66 deletions(-)

lib/std/Io/Dir.zig+1-1
......@@ -982,7 +982,7 @@ pub fn rename(
982982 return io.vtable.dirRename(io.userdata, old_dir, old_sub_path, new_dir, new_sub_path);
983983}
984984
985pub fn renameAbsolute(io: Io, old_path: []const u8, new_path: []const u8) RenameError!void {
985pub fn renameAbsolute(old_path: []const u8, new_path: []const u8, io: Io) RenameError!void {
986986 assert(path.isAbsolute(old_path));
987987 assert(path.isAbsolute(new_path));
988988 const my_cwd = cwd();
lib/std/fs/test.zig+49-63
......@@ -251,7 +251,7 @@ test "Dir.readLink on non-symlinks" {
251251
252252fn testReadLink(io: Io, dir: Dir, target_path: []const u8, symlink_path: []const u8) !void {
253253 var buffer: [Dir.max_path_bytes]u8 = undefined;
254 const actual = try dir.readLink(io, symlink_path, &buffer);
254 const actual = buffer[0..try dir.readLink(io, symlink_path, &buffer)];
255255 try expectEqualStrings(target_path, actual);
256256}
257257
......@@ -289,7 +289,7 @@ test "File.stat on a File that is a symlink returns Kind.sym_link" {
289289
290290 try setupSymlink(io, ctx.dir, dir_target_path, "symlink", .{ .is_directory = true });
291291
292 var symlink: Dir = try ctx.dir.openDir("symlink", .{ .follow_symlinks = false });
292 var symlink: Dir = try ctx.dir.openDir(io, "symlink", .{ .follow_symlinks = false });
293293 defer symlink.close(io);
294294
295295 const stat = try symlink.stat(io);
......@@ -807,7 +807,7 @@ test "directory operations on files" {
807807 try expectError(error.NotDir, ctx.dir.deleteDir(io, test_file_name));
808808
809809 if (ctx.path_type == .absolute and comptime PathType.absolute.isSupported(builtin.os)) {
810 try expectError(error.PathAlreadyExists, Dir.makeDirAbsolute(io, test_file_name));
810 try expectError(error.PathAlreadyExists, Dir.makeDirAbsolute(io, test_file_name, .default_dir));
811811 try expectError(error.NotDir, Dir.deleteDirAbsolute(io, test_file_name));
812812 }
813813
......@@ -1104,9 +1104,9 @@ test "renameAbsolute" {
11041104 const base_path = try tmp_dir.dir.realPathAlloc(io, ".", allocator);
11051105
11061106 try expectError(error.FileNotFound, Dir.renameAbsolute(
1107 io,
11081107 try Dir.path.join(allocator, &.{ base_path, "missing_file_name" }),
11091108 try Dir.path.join(allocator, &.{ base_path, "something_else" }),
1109 io,
11101110 ));
11111111
11121112 // Renaming files
......@@ -1115,9 +1115,9 @@ test "renameAbsolute" {
11151115 var file = try tmp_dir.dir.createFile(io, test_file_name, .{ .read = true });
11161116 file.close(io);
11171117 try Dir.renameAbsolute(
1118 io,
11191118 try Dir.path.join(allocator, &.{ base_path, test_file_name }),
11201119 try Dir.path.join(allocator, &.{ base_path, renamed_test_file_name }),
1120 io,
11211121 );
11221122
11231123 // ensure the file was renamed
......@@ -1132,9 +1132,9 @@ test "renameAbsolute" {
11321132 const renamed_test_dir_name = "test_dir_renamed";
11331133 try tmp_dir.dir.makeDir(io, test_dir_name, .default_dir);
11341134 try Dir.renameAbsolute(
1135 io,
11361135 try Dir.path.join(allocator, &.{ base_path, test_dir_name }),
11371136 try Dir.path.join(allocator, &.{ base_path, renamed_test_dir_name }),
1137 io,
11381138 );
11391139
11401140 // ensure the directory was renamed
......@@ -1430,7 +1430,7 @@ test "writev, readv" {
14301430 var src_file = try tmp.dir.createFile(io, "test.txt", .{ .read = true });
14311431 defer src_file.close(io);
14321432
1433 var writer = src_file.writerStreaming(&.{});
1433 var writer = src_file.writerStreaming(io, &.{});
14341434
14351435 try writer.interface.writeVecAll(&write_vecs);
14361436 try writer.interface.flush();
......@@ -1590,10 +1590,10 @@ test "copyFile" {
15901590 try ctx.dir.writeFile(io, .{ .sub_path = src_file, .data = data });
15911591 defer ctx.dir.deleteFile(io, src_file) catch {};
15921592
1593 try ctx.dir.copyFile(src_file, ctx.dir, dest_file, .{});
1593 try ctx.dir.copyFile(src_file, ctx.dir, dest_file, io, .{});
15941594 defer ctx.dir.deleteFile(io, dest_file) catch {};
15951595
1596 try ctx.dir.copyFile(src_file, ctx.dir, dest_file2, .{ .override_mode = File.default_mode });
1596 try ctx.dir.copyFile(src_file, ctx.dir, dest_file2, io, .{ .override_mode = File.default_mode });
15971597 defer ctx.dir.deleteFile(io, dest_file2) catch {};
15981598
15991599 try expectFileContents(io, ctx.dir, dest_file, data);
......@@ -1968,7 +1968,7 @@ test "'.' and '..' in Dir functions" {
19681968 created_file.close(io);
19691969 try ctx.dir.access(io, file_path, .{});
19701970
1971 try ctx.dir.copyFile(file_path, ctx.dir, copy_path, .{});
1971 try ctx.dir.copyFile(file_path, ctx.dir, copy_path, io, .{});
19721972 try ctx.dir.rename(copy_path, ctx.dir, rename_path, io);
19731973 const renamed_file = try ctx.dir.openFile(io, rename_path, .{});
19741974 renamed_file.close(io);
......@@ -2000,7 +2000,7 @@ test "'.' and '..' in absolute functions" {
20002000 const base_path = try tmp.dir.realPathAlloc(io, ".", allocator);
20012001
20022002 const subdir_path = try Dir.path.join(allocator, &.{ base_path, "./subdir" });
2003 try Dir.makeDirAbsolute(io, subdir_path);
2003 try Dir.makeDirAbsolute(io, subdir_path, .default_dir);
20042004 try Dir.accessAbsolute(io, subdir_path, .{});
20052005 var created_subdir = try Dir.openDirAbsolute(io, subdir_path, .{});
20062006 created_subdir.close(io);
......@@ -2011,10 +2011,10 @@ test "'.' and '..' in absolute functions" {
20112011 try Dir.accessAbsolute(io, created_file_path, .{});
20122012
20132013 const copied_file_path = try Dir.path.join(allocator, &.{ subdir_path, "../copy" });
2014 try Dir.copyFileAbsolute(io, created_file_path, copied_file_path, .{});
2014 try Dir.copyFileAbsolute(created_file_path, copied_file_path, io, .{});
20152015 const renamed_file_path = try Dir.path.join(allocator, &.{ subdir_path, "../rename" });
2016 try Dir.renameAbsolute(io, copied_file_path, renamed_file_path);
2017 const renamed_file = try Dir.openFileAbsolute(renamed_file_path, .{});
2016 try Dir.renameAbsolute(copied_file_path, renamed_file_path, io);
2017 const renamed_file = try Dir.openFileAbsolute(io, renamed_file_path, .{});
20182018 renamed_file.close(io);
20192019 try Dir.deleteFileAbsolute(io, renamed_file_path);
20202020
......@@ -2044,7 +2044,7 @@ test "chmod" {
20442044 try expectEqual(0o700, (try dir.stat(io)).permissions.toMode() & 0o7777);
20452045}
20462046
2047test "chown" {
2047test "change ownership" {
20482048 if (native_os == .windows or native_os == .wasi)
20492049 return error.SkipZigTest;
20502050
......@@ -2055,13 +2055,13 @@ test "chown" {
20552055
20562056 const file = try tmp.dir.createFile(io, "test_file", .{});
20572057 defer file.close(io);
2058 try file.chown(null, null);
2058 try file.setOwner(io, null, null);
20592059
20602060 try tmp.dir.makeDir(io, "test_dir", .default_dir);
20612061
20622062 var dir = try tmp.dir.openDir(io, "test_dir", .{ .iterate = true });
20632063 defer dir.close(io);
2064 try dir.chown(null, null);
2064 try dir.setOwner(io, null, null);
20652065}
20662066
20672067test "invalid UTF-8/WTF-8 paths" {
......@@ -2116,7 +2116,7 @@ test "invalid UTF-8/WTF-8 paths" {
21162116
21172117 var dir = ctx.dir;
21182118 try expectError(expected_err, dir.updateFile(io, invalid_path, dir, invalid_path, .{}));
2119 try expectError(expected_err, ctx.dir.copyFile(invalid_path, ctx.dir, invalid_path, .{}));
2119 try expectError(expected_err, ctx.dir.copyFile(invalid_path, ctx.dir, invalid_path, io, .{}));
21202120
21212121 try expectError(expected_err, ctx.dir.statFile(invalid_path));
21222122
......@@ -2128,12 +2128,12 @@ test "invalid UTF-8/WTF-8 paths" {
21282128 try expectError(expected_err, Dir.rename(ctx.dir, invalid_path, ctx.dir, invalid_path, io));
21292129
21302130 if (native_os != .wasi and ctx.path_type != .relative) {
2131 try expectError(expected_err, Dir.copyFileAbsolute(invalid_path, invalid_path, .{}));
2132 try expectError(expected_err, Dir.makeDirAbsolute(invalid_path));
2131 try expectError(expected_err, Dir.copyFileAbsolute(invalid_path, invalid_path, io, .{}));
2132 try expectError(expected_err, Dir.makeDirAbsolute(io, invalid_path, .default_dir));
21332133 try expectError(expected_err, Dir.deleteDirAbsolute(invalid_path));
2134 try expectError(expected_err, Dir.renameAbsolute(invalid_path, invalid_path));
2134 try expectError(expected_err, Dir.renameAbsolute(invalid_path, invalid_path, io));
21352135 try expectError(expected_err, Dir.openDirAbsolute(io, invalid_path, .{}));
2136 try expectError(expected_err, Dir.openFileAbsolute(invalid_path, .{}));
2136 try expectError(expected_err, Dir.openFileAbsolute(io, invalid_path, .{}));
21372137 try expectError(expected_err, Dir.accessAbsolute(invalid_path, .{}));
21382138 try expectError(expected_err, Dir.createFileAbsolute(invalid_path, .{}));
21392139 try expectError(expected_err, Dir.deleteFileAbsolute(invalid_path));
......@@ -2157,7 +2157,7 @@ test "read file non vectored" {
21572157 const file = try tmp_dir.dir.createFile(io, "input.txt", .{ .read = true });
21582158 defer file.close(io);
21592159 {
2160 var file_writer: File.Writer = .init(file, &.{});
2160 var file_writer: File.Writer = .init(file, io, &.{});
21612161 try file_writer.interface.writeAll(contents);
21622162 try file_writer.interface.flush();
21632163 }
......@@ -2189,7 +2189,7 @@ test "seek keeping partial buffer" {
21892189 const file = try tmp_dir.dir.createFile(io, "input.txt", .{ .read = true });
21902190 defer file.close(io);
21912191 {
2192 var file_writer: File.Writer = .init(file, &.{});
2192 var file_writer: File.Writer = .init(file, io, &.{});
21932193 try file_writer.interface.writeAll(contents);
21942194 try file_writer.interface.flush();
21952195 }
......@@ -2251,7 +2251,7 @@ test "seekTo flushes buffered data" {
22512251 defer file.close(io);
22522252 {
22532253 var buf: [16]u8 = undefined;
2254 var file_writer = file.writer(io, file, &buf);
2254 var file_writer = file.writer(io, &buf);
22552255
22562256 try file_writer.interface.writeAll(contents);
22572257 try file_writer.seekTo(8);
......@@ -2285,7 +2285,7 @@ test "File.Writer sendfile with buffered contents" {
22852285 try in_r.interface.fill(2);
22862286
22872287 var out_buf: [1]u8 = undefined;
2288 var out_w = out.writerStreaming(&out_buf);
2288 var out_w = out.writerStreaming(io, &out_buf);
22892289 try out_w.interface.writeByte('a');
22902290 try expectEqual(3, try out_w.interface.sendFileAll(&in_r, .unlimited));
22912291 try out_w.interface.flush();
......@@ -2325,16 +2325,17 @@ test "readlinkat" {
23252325 try tmp.dir.writeFile(io, .{ .sub_path = "file.txt", .data = "nonsense" });
23262326
23272327 // create a symbolic link
2328 tmp.dir.symLink("file.txt", "link", .{}) catch |err| switch (err) {
2328 tmp.dir.symLink(io, "file.txt", "link", .{}) catch |err| switch (err) {
23292329 error.AccessDenied => {
23302330 // Symlink requires admin privileges on windows, so this test can legitimately fail.
23312331 if (native_os == .windows) return error.SkipZigTest;
23322332 },
2333 else => |e| return e,
23332334 };
23342335
23352336 // read the link
23362337 var buffer: [Dir.max_path_bytes]u8 = undefined;
2337 const read_link = try tmp.dir.readLink(io, "link", &buffer);
2338 const read_link = buffer[0..try tmp.dir.readLink(io, "link", &buffer)];
23382339 try expectEqualStrings("file.txt", read_link);
23392340}
23402341
......@@ -2351,7 +2352,7 @@ test "fchmodat smoke test" {
23512352 var tmp = tmpDir(.{});
23522353 defer tmp.cleanup();
23532354
2354 try expectError(error.FileNotFound, tmp.dir.setPermissions(io, "regfile", 0o666, .{}));
2355 try expectError(error.FileNotFound, tmp.dir.setFilePermissions(io, "regfile", .fromMode(0o666), .{}));
23552356 const file = try tmp.dir.createFile(io, "regfile", .{
23562357 .exclusive = true,
23572358 .permissions = .fromMode(0o644),
......@@ -2384,15 +2385,15 @@ test "fchmodat smoke test" {
23842385 error.OperationNotSupported => test_link = false,
23852386 else => |e| return e,
23862387 };
2387 if (test_link)
2388 try expectMode(tmp.dir.handle, "symlink", 0o600);
2389 try expectMode(tmp.dir.handle, "regfile", 0o640);
2388 if (test_link) try expectMode(io, tmp.dir, "symlink", .fromMode(0o600));
2389 try expectMode(io, tmp.dir, "regfile", .fromMode(0o640));
23902390}
23912391
23922392fn expectMode(io: Io, dir: Dir, file: []const u8, permissions: File.Permissions) !void {
23932393 const mode = permissions.toMode();
23942394 const st = try dir.statFile(io, file, .{ .follow_symlinks = false });
2395 try expectEqual(mode, st.mode & 0b111_111_111);
2395 const found_mode = st.permissions.toMode();
2396 try expectEqual(mode, found_mode & 0b111_111_111);
23962397}
23972398
23982399test "isatty" {
......@@ -2417,7 +2418,7 @@ test "read positional empty buffer" {
24172418 defer file.close(io);
24182419
24192420 var buffer: [0]u8 = undefined;
2420 try expectEqual(0, try file.readPositional(io, &buffer, 0));
2421 try expectEqual(0, try file.readPositional(io, &.{&buffer}, 0));
24212422}
24222423
24232424test "write streaming empty buffer" {
......@@ -2429,8 +2430,8 @@ test "write streaming empty buffer" {
24292430 var file = try tmp.dir.createFile(io, "write_empty", .{});
24302431 defer file.close(io);
24312432
2432 var buffer: [0]u8 = &.{};
2433 try expectEqual(0, try file.writeStreaming(io, &buffer));
2433 const buffer: [0]u8 = .{};
2434 try file.writeStreamingAll(io, &buffer);
24342435}
24352436
24362437test "write positional empty buffer" {
......@@ -2442,8 +2443,8 @@ test "write positional empty buffer" {
24422443 var file = try tmp.dir.createFile(io, "pwrite_empty", .{});
24432444 defer file.close(io);
24442445
2445 var buffer: [0]u8 = &.{};
2446 try expectEqual(0, try file.writePositional(io, &buffer, 0));
2446 const buffer: [0]u8 = .{};
2447 try expectEqual(0, try file.writePositional(io, &.{&buffer}, 0));
24472448}
24482449
24492450test "access smoke test" {
......@@ -2452,53 +2453,38 @@ test "access smoke test" {
24522453 if (native_os == .openbsd) return error.SkipZigTest;
24532454
24542455 const io = testing.io;
2455 const gpa = testing.allocator;
24562456
24572457 var tmp = tmpDir(.{});
24582458 defer tmp.cleanup();
24592459
2460 const base_path = try tmp.dir.realPathAlloc(io, ".", gpa);
2461 defer gpa.free(base_path);
2462
24632460 {
24642461 // Create some file using `open`.
2465 const file_path = try Dir.path.join(gpa, &.{ base_path, "some_file" });
2466 defer gpa.free(file_path);
2467 const file = Dir.cwd().createFile(io, file_path, .{ .read = true, .exclusive = true });
2462 const file = try tmp.dir.createFile(io, "some_file", .{ .read = true, .exclusive = true });
24682463 file.close(io);
24692464 }
24702465
24712466 {
24722467 // Try to access() the file
2473 const file_path = try Dir.path.join(gpa, &.{ base_path, "some_file" });
2474 defer gpa.free(file_path);
24752468 if (native_os == .windows) {
2476 try Dir.cwd().access(io, file_path, .{});
2469 try tmp.dir.access(io, "some_file", .{});
24772470 } else {
2478 try Dir.cwd().access(io, file_path, .{ .read = true, .write = true });
2471 try tmp.dir.access(io, "some_file", .{ .read = true, .write = true });
24792472 }
24802473 }
24812474
24822475 {
24832476 // Try to access() a non-existent file - should fail with error.FileNotFound
2484 const file_path = try Dir.path.join(gpa, &.{ base_path, "some_other_file" });
2485 defer gpa.free(file_path);
2486 try expectError(error.FileNotFound, Dir.cwd().access(io, file_path, .{}));
2477 try expectError(error.FileNotFound, tmp.dir.access(io, "some_other_file", .{}));
24872478 }
24882479
24892480 {
24902481 // Create some directory
2491 const file_path = try Dir.path.join(gpa, &.{ base_path, "some_dir" });
2492 defer gpa.free(file_path);
2493 try Dir.makeDir(io, file_path, .default_file);
2482 try tmp.dir.makeDir(io, "some_dir", .default_file);
24942483 }
24952484
24962485 {
24972486 // Try to access() the directory
2498 const file_path = try Dir.path.join(gpa, &.{ base_path, "some_dir" });
2499 defer gpa.free(file_path);
2500
2501 try Dir.access(io, file_path, .{});
2487 try tmp.dir.access(io, "some_dir", .{});
25022488 }
25032489}
25042490
......@@ -2552,12 +2538,12 @@ test "open smoke test" {
25522538 try tmp.dir.makeDir(io, "some_dir", .default_dir);
25532539
25542540 {
2555 const dir = try tmp.dir.openDir("some_dir", .{});
2541 const dir = try tmp.dir.openDir(io, "some_dir", .{});
25562542 dir.close(io);
25572543 }
25582544
25592545 // Try opening as file which should fail.
2560 try expectError(error.IsDir, tmp.dir.openFile("some_dir", .{}));
2546 try expectError(error.IsDir, tmp.dir.openFile(io, "some_dir", .{}));
25612547}
25622548
25632549test "hard link with different directories" {
......@@ -2575,7 +2561,7 @@ test "hard link with different directories" {
25752561 try tmp.dir.writeFile(io, .{ .sub_path = target_name, .data = "example" });
25762562
25772563 // Test 1: link from file in subdir back up to target in parent directory
2578 tmp.dir.hardLink(target_name, subdir, link_name, 0) catch |err| switch (err) {
2564 tmp.dir.hardLink(target_name, subdir, link_name, io, .{}) catch |err| switch (err) {
25792565 error.OperationUnsupported => return error.SkipZigTest,
25802566 else => |e| return e,
25812567 };
......@@ -2596,7 +2582,7 @@ test "hard link with different directories" {
25962582 }
25972583
25982584 // Test 2: remove link
2599 try subdir.deleteFile(io, link_name, .{});
2585 try subdir.deleteFile(io, link_name);
26002586 const e_stat = try efd.stat(io);
26012587 try expectEqual(1, e_stat.nlink);
26022588}
lib/std/posix/test.zig+2-2
......@@ -518,7 +518,7 @@ test "rename smoke test" {
518518 // Rename the file
519519 const new_file_path = try Dir.path.join(gpa, &.{ base_path, "some_other_file" });
520520 defer gpa.free(new_file_path);
521 try Io.Dir.renameAbsolute(io, file_path, new_file_path);
521 try Io.Dir.renameAbsolute(file_path, new_file_path, io);
522522 }
523523
524524 {
......@@ -545,7 +545,7 @@ test "rename smoke test" {
545545 // Rename the directory
546546 const new_file_path = try Dir.path.join(gpa, &.{ base_path, "some_other_dir" });
547547 defer gpa.free(new_file_path);
548 try Io.Dir.renameAbsolute(io, file_path, new_file_path);
548 try Io.Dir.renameAbsolute(file_path, new_file_path, io);
549549 }
550550
551551 {