authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2023-08-07 23:49:53-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-08-10 13:25:14-07:00
loga190582b26a82f96da7488eff37e9676cdb937bc
tree50931e3921a66fa1bc9eb8f55029be7d77b558ba
parent901457d173467c71b681a8c69f4b77c94d516da7

fs.Dir.realpathW: Reduce the number of OpenFile calls for directories

`.filter = .any` can be used here to remove an unnecessary extra OpenFile call when getting the path of a directory

2 files changed, 14 insertions(+), 15 deletions(-)

lib/std/fs.zig+1-11
...@@ -1568,18 +1568,8 @@ pub const Dir = struct {...@@ -1568,18 +1568,8 @@ pub const Dir = struct {
1568 .share_access = share_access,1568 .share_access = share_access,
1569 .creation = creation,1569 .creation = creation,
1570 .io_mode = .blocking,1570 .io_mode = .blocking,
1571 .filter = .any,
1571 }) catch |err| switch (err) {1572 }) catch |err| switch (err) {
1572 error.IsDir => break :blk w.OpenFile(pathname, .{
1573 .dir = self.fd,
1574 .access_mask = access_mask,
1575 .share_access = share_access,
1576 .creation = creation,
1577 .io_mode = .blocking,
1578 .filter = .dir_only,
1579 }) catch |er| switch (er) {
1580 error.WouldBlock => unreachable,
1581 else => |e2| return e2,
1582 },
1583 error.WouldBlock => unreachable,1573 error.WouldBlock => unreachable,
1584 else => |e| return e,1574 else => |e| return e,
1585 };1575 };
lib/std/fs/test.zig+13-4
...@@ -341,6 +341,8 @@ test "Dir.realpath smoke test" {...@@ -341,6 +341,8 @@ test "Dir.realpath smoke test" {
341 // with a sharing violation.341 // with a sharing violation.
342 file.close();342 file.close();
343343
344 try tmp_dir.dir.makeDir("test_dir");
345
344 var arena = ArenaAllocator.init(testing.allocator);346 var arena = ArenaAllocator.init(testing.allocator);
345 defer arena.deinit();347 defer arena.deinit();
346 const allocator = arena.allocator();348 const allocator = arena.allocator();
...@@ -353,18 +355,25 @@ test "Dir.realpath smoke test" {...@@ -353,18 +355,25 @@ test "Dir.realpath smoke test" {
353 // First, test non-alloc version355 // First, test non-alloc version
354 {356 {
355 var buf1: [fs.MAX_PATH_BYTES]u8 = undefined;357 var buf1: [fs.MAX_PATH_BYTES]u8 = undefined;
358
356 const file_path = try tmp_dir.dir.realpath("test_file", buf1[0..]);359 const file_path = try tmp_dir.dir.realpath("test_file", buf1[0..]);
357 const expected_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "test_file" });360 const expected_file_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "test_file" });
361 try testing.expectEqualStrings(expected_file_path, file_path);
358362
359 try testing.expect(mem.eql(u8, file_path, expected_path));363 const dir_path = try tmp_dir.dir.realpath("test_dir", buf1[0..]);
364 const expected_dir_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "test_dir" });
365 try testing.expectEqualStrings(expected_dir_path, dir_path);
360 }366 }
361367
362 // Next, test alloc version368 // Next, test alloc version
363 {369 {
364 const file_path = try tmp_dir.dir.realpathAlloc(allocator, "test_file");370 const file_path = try tmp_dir.dir.realpathAlloc(allocator, "test_file");
365 const expected_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "test_file" });371 const expected_file_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "test_file" });
372 try testing.expectEqualStrings(expected_file_path, file_path);
366373
367 try testing.expect(mem.eql(u8, file_path, expected_path));374 const dir_path = try tmp_dir.dir.realpathAlloc(allocator, "test_dir");
375 const expected_dir_path = try fs.path.join(allocator, &[_][]const u8{ base_path, "test_dir" });
376 try testing.expectEqualStrings(expected_dir_path, dir_path);
368 }377 }
369}378}
370379