authorgravatar for stratact@stratacter.comstratact <stratact@stratacter.com> 2019-09-18 23:56:45-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-10-19 14:04:51-04:00
loge78d3750c58d26bac0e24c40eb89c2f4796bc15c
tree085753a5bb5dbbc9b2b491ff38bf38124a3d92ab
parent6f7939a452e69b77581f5390b8075e9dfa81b03e
signaturelock-open Commit is signed but in an unrecognized format.

Use 8192 sized buffers and remove allocator parameters


10 files changed, 31 insertions(+), 56 deletions(-)

doc/docgen.zig+1-1
......@@ -51,7 +51,7 @@ pub fn main() !void {
5151 var toc = try genToc(allocator, &tokenizer);
5252
5353 try fs.makePath(allocator, tmp_dir_name);
54 defer fs.deleteTree(allocator, tmp_dir_name) catch {};
54 defer fs.deleteTree(tmp_dir_name) catch {};
5555
5656 try genHtml(allocator, &tokenizer, &toc, &buffered_out_stream.stream, zig_exe);
5757 try buffered_out_stream.flush();
lib/std/build.zig+2-2
......@@ -331,7 +331,7 @@ pub const Builder = struct {
331331 if (self.verbose) {
332332 warn("rm {}\n", full_path);
333333 }
334 fs.deleteTree(self.allocator, full_path) catch {};
334 fs.deleteTree(full_path) catch {};
335335 }
336336
337337 // TODO remove empty directories
......@@ -2671,7 +2671,7 @@ pub const RemoveDirStep = struct {
26712671 const self = @fieldParentPtr(RemoveDirStep, "step", step);
26722672
26732673 const full_path = self.builder.pathFromRoot(self.dir_path);
2674 fs.deleteTree(self.builder.allocator, full_path) catch |err| {
2674 fs.deleteTree(full_path) catch |err| {
26752675 warn("Unable to remove {}: {}\n", full_path, @errorName(err));
26762676 return err;
26772677 };
lib/std/event/fs.zig+1-1
......@@ -1312,7 +1312,7 @@ const test_tmp_dir = "std_event_fs_test";
13121312//
13131313// // TODO move this into event loop too
13141314// try os.makePath(allocator, test_tmp_dir);
1315// defer os.deleteTree(allocator, test_tmp_dir) catch {};
1315// defer os.deleteTree(test_tmp_dir) catch {};
13161316//
13171317// var loop: Loop = undefined;
13181318// try loop.initMultiThreaded(allocator);
lib/std/fs.zig+18-43
......@@ -37,6 +37,8 @@ pub const MAX_PATH_BYTES = switch (builtin.os) {
3737 else => @compileError("Unsupported OS"),
3838};
3939
40pub const MAX_BUF_BYTES: usize = 8192;
41
4042// here we replace the standard +/ with -_ so that it can be used in a file name
4143const b64_fs_encoder = base64.Base64Encoder.init("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_", base64.standard_pad_char);
4244
......@@ -371,7 +373,7 @@ const DeleteTreeError = error{
371373/// this function recursively removes its entries and then tries again.
372374/// TODO determine if we can remove the allocator requirement
373375/// https://github.com/ziglang/zig/issues/2886
374pub fn deleteTree(allocator: *Allocator, full_path: []const u8) DeleteTreeError!void {
376pub fn deleteTree(full_path: []const u8) DeleteTreeError!void {
375377 start_over: while (true) {
376378 var got_access_denied = false;
377379 // First, try deleting the item as a file. This way we don't follow sym links.
......@@ -395,7 +397,7 @@ pub fn deleteTree(allocator: *Allocator, full_path: []const u8) DeleteTreeError!
395397 => return err,
396398 }
397399 {
398 var dir = Dir.open(allocator, full_path) catch |err| switch (err) {
400 var dir = Dir.open(full_path) catch |err| switch (err) {
399401 error.NotDir => {
400402 if (got_access_denied) {
401403 return error.AccessDenied;
......@@ -424,17 +426,14 @@ pub fn deleteTree(allocator: *Allocator, full_path: []const u8) DeleteTreeError!
424426 };
425427 defer dir.close();
426428
427 var full_entry_buf = std.ArrayList(u8).init(allocator);
428 defer full_entry_buf.deinit();
429
430429 while (try dir.next()) |entry| {
431 try full_entry_buf.resize(full_path.len + entry.name.len + 1);
432 const full_entry_path = full_entry_buf.toSlice();
430 var full_entry_buf: [MAX_BUF_BYTES]u8 = undefined;
431 const full_entry_path = full_entry_buf[0..];
433432 mem.copy(u8, full_entry_path, full_path);
434433 full_entry_path[full_path.len] = path.sep;
435434 mem.copy(u8, full_entry_path[full_path.len + 1 ..], entry.name);
436435
437 try deleteTree(allocator, full_entry_path);
436 try deleteTree(full_entry_path[0..full_path.len + entry.name.len + 1]);
438437 }
439438 }
440439 return deleteDir(full_path);
......@@ -445,19 +444,18 @@ pub fn deleteTree(allocator: *Allocator, full_path: []const u8) DeleteTreeError!
445444/// files, and into the one that reads files from an open directory handle.
446445pub const Dir = struct {
447446 handle: Handle,
448 allocator: *Allocator,
449447
450448 pub const Handle = switch (builtin.os) {
451449 .macosx, .ios, .freebsd, .netbsd => struct {
452450 fd: i32,
453451 seek: i64,
454 buf: []u8,
452 buf: [MAX_BUF_BYTES]u8,
455453 index: usize,
456454 end_index: usize,
457455 },
458456 .linux => struct {
459457 fd: i32,
460 buf: []u8,
458 buf: [MAX_BUF_BYTES]u8,
461459 index: usize,
462460 end_index: usize,
463461 },
......@@ -512,9 +510,8 @@ pub const Dir = struct {
512510 /// Call close when done.
513511 /// TODO remove the allocator requirement from this API
514512 /// https://github.com/ziglang/zig/issues/2885
515 pub fn open(allocator: *Allocator, dir_path: []const u8) OpenError!Dir {
513 pub fn open(dir_path: []const u8) OpenError!Dir {
516514 return Dir{
517 .allocator = allocator,
518515 .handle = switch (builtin.os) {
519516 .windows => blk: {
520517 var find_file_data: os.windows.WIN32_FIND_DATAW = undefined;
......@@ -548,7 +545,6 @@ pub const Dir = struct {
548545 if (os.windows.is_the_target) {
549546 return os.windows.FindClose(self.handle.handle);
550547 }
551 self.allocator.free(self.handle.buf);
552548 os.close(self.handle.fd);
553549 }
554550
......@@ -579,14 +575,10 @@ pub const Dir = struct {
579575 fn nextDarwin(self: *Dir) !?Entry {
580576 start_over: while (true) {
581577 if (self.handle.index >= self.handle.end_index) {
582 if (self.handle.buf.len == 0) {
583 self.handle.buf = try self.allocator.alloc(u8, mem.page_size);
584 }
585
586578 while (true) {
587579 const rc = os.system.__getdirentries64(
588580 self.handle.fd,
589 self.handle.buf.ptr,
581 self.handle.buf[0..].ptr,
590582 self.handle.buf.len,
591583 &self.handle.seek,
592584 );
......@@ -596,10 +588,7 @@ pub const Dir = struct {
596588 os.EBADF => unreachable,
597589 os.EFAULT => unreachable,
598590 os.ENOTDIR => unreachable,
599 os.EINVAL => {
600 self.handle.buf = try self.allocator.realloc(self.handle.buf, self.handle.buf.len * 2);
601 continue;
602 },
591 os.EINVAL => unreachable,
603592 else => |err| return os.unexpectedErrno(err),
604593 }
605594 }
......@@ -666,21 +655,14 @@ pub const Dir = struct {
666655 fn nextLinux(self: *Dir) !?Entry {
667656 start_over: while (true) {
668657 if (self.handle.index >= self.handle.end_index) {
669 if (self.handle.buf.len == 0) {
670 self.handle.buf = try self.allocator.alloc(u8, mem.page_size);
671 }
672
673658 while (true) {
674 const rc = os.linux.getdents64(self.handle.fd, self.handle.buf.ptr, self.handle.buf.len);
659 const rc = os.linux.getdents64(self.handle.fd, self.handle.buf[0..].ptr, self.handle.buf.len);
675660 switch (os.linux.getErrno(rc)) {
676661 0 => {},
677662 os.EBADF => unreachable,
678663 os.EFAULT => unreachable,
679664 os.ENOTDIR => unreachable,
680 os.EINVAL => {
681 self.handle.buf = try self.allocator.realloc(self.handle.buf, self.handle.buf.len * 2);
682 continue;
683 },
665 os.EINVAL => unreachable,
684666 else => |err| return os.unexpectedErrno(err),
685667 }
686668 if (rc == 0) return null;
......@@ -720,14 +702,10 @@ pub const Dir = struct {
720702 fn nextBsd(self: *Dir) !?Entry {
721703 start_over: while (true) {
722704 if (self.handle.index >= self.handle.end_index) {
723 if (self.handle.buf.len == 0) {
724 self.handle.buf = try self.allocator.alloc(u8, mem.page_size);
725 }
726
727705 while (true) {
728706 const rc = os.system.getdirentries(
729707 self.handle.fd,
730 self.handle.buf.ptr,
708 self.handle.buf[0..].ptr,
731709 self.handle.buf.len,
732710 &self.handle.seek,
733711 );
......@@ -736,10 +714,7 @@ pub const Dir = struct {
736714 os.EBADF => unreachable,
737715 os.EFAULT => unreachable,
738716 os.ENOTDIR => unreachable,
739 os.EINVAL => {
740 self.handle.buf = try self.allocator.realloc(self.handle.buf, self.handle.buf.len * 2);
741 continue;
742 },
717 os.EINVAL => unreachable,
743718 else => |err| return os.unexpectedErrno(err),
744719 }
745720 if (rc == 0) return null;
......@@ -807,7 +782,7 @@ pub const Walker = struct {
807782 try self.name_buffer.append(base.name);
808783 if (base.kind == .Directory) {
809784 // TODO https://github.com/ziglang/zig/issues/2888
810 var new_dir = try Dir.open(self.stack.allocator, self.name_buffer.toSliceConst());
785 var new_dir = try Dir.open(self.name_buffer.toSliceConst());
811786 {
812787 errdefer new_dir.close();
813788 try self.stack.append(StackItem{
......@@ -841,7 +816,7 @@ pub const Walker = struct {
841816pub fn walkPath(allocator: *Allocator, dir_path: []const u8) !Walker {
842817 assert(!mem.endsWith(u8, dir_path, path.sep_str));
843818
844 var dir_it = try Dir.open(allocator, dir_path);
819 var dir_it = try Dir.open(dir_path);
845820 errdefer dir_it.close();
846821
847822 var name_buffer = try std.Buffer.init(allocator, dir_path);
lib/std/os/test.zig+3-3
......@@ -19,8 +19,8 @@ test "makePath, put some files in it, deleteTree" {
1919 try fs.makePath(a, "os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c");
2020 try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c" ++ fs.path.sep_str ++ "file.txt", "nonsense");
2121 try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "file2.txt", "blah");
22 try fs.deleteTree(a, "os_test_tmp");
23 if (fs.Dir.open(a, "os_test_tmp")) |dir| {
22 try fs.deleteTree("os_test_tmp");
23 if (fs.Dir.open("os_test_tmp")) |dir| {
2424 @panic("expected error");
2525 } else |err| {
2626 expect(err == error.FileNotFound);
......@@ -37,7 +37,7 @@ test "access file" {
3737
3838 try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", "");
3939 try os.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", os.F_OK);
40 try fs.deleteTree(a, "os_test_tmp");
40 try fs.deleteTree("os_test_tmp");
4141}
4242
4343fn testThreadIdFn(thread_id: *Thread.Id) void {
src-self-hosted/main.zig+1-1
......@@ -747,7 +747,7 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro
747747 )) catch |err| switch (err) {
748748 error.IsDir, error.AccessDenied => {
749749 // TODO make event based (and dir.next())
750 var dir = try fs.Dir.open(fmt.loop.allocator, file_path);
750 var dir = try fs.Dir.open(file_path);
751751 defer dir.close();
752752
753753 var group = event.Group(FmtError!void).init(fmt.loop);
src-self-hosted/stage1.zig+1-1
......@@ -283,7 +283,7 @@ fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtError!void
283283 const source_code = io.readFileAlloc(fmt.allocator, file_path) catch |err| switch (err) {
284284 error.IsDir, error.AccessDenied => {
285285 // TODO make event based (and dir.next())
286 var dir = try fs.Dir.open(fmt.allocator, file_path);
286 var dir = try fs.Dir.open(file_path);
287287 defer dir.close();
288288
289289 while (try dir.next()) |entry| {
src-self-hosted/test.zig+2-2
......@@ -56,11 +56,11 @@ pub const TestContext = struct {
5656 errdefer allocator.free(self.zig_lib_dir);
5757
5858 try std.fs.makePath(allocator, tmp_dir_name);
59 errdefer std.fs.deleteTree(allocator, tmp_dir_name) catch {};
59 errdefer std.fs.deleteTree(tmp_dir_name) catch {};
6060 }
6161
6262 fn deinit(self: *TestContext) void {
63 std.fs.deleteTree(allocator, tmp_dir_name) catch {};
63 std.fs.deleteTree(tmp_dir_name) catch {};
6464 allocator.free(self.zig_lib_dir);
6565 self.zig_compiler.deinit();
6666 self.loop.deinit();
test/cli.zig+1-1
......@@ -37,7 +37,7 @@ pub fn main() !void {
3737 testMissingOutputPath,
3838 };
3939 for (test_fns) |testFn| {
40 try fs.deleteTree(a, dir_path);
40 try fs.deleteTree(dir_path);
4141 try fs.makeDir(dir_path);
4242 try testFn(zig_exe, dir_path);
4343 }
tools/process_headers.zig+1-1
......@@ -340,7 +340,7 @@ pub fn main() !void {
340340 try dir_stack.append(target_include_dir);
341341
342342 while (dir_stack.popOrNull()) |full_dir_name| {
343 var dir = std.fs.Dir.open(allocator, full_dir_name) catch |err| switch (err) {
343 var dir = std.fs.Dir.open(full_dir_name) catch |err| switch (err) {
344344 error.FileNotFound => continue :search,
345345 error.AccessDenied => continue :search,
346346 else => return err,