authorgravatar for admin@15318.deCassidy Dingenskirchen <admin@15318.de> 2020-06-16 19:07:55+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-18 20:41:40-04:00
logb30642a86ae59f9cfe73c8a9ed6e79c3cb5aaa36
tree093cf7ea9394b560f23dd66ac08ba5c3af44bcaf
parentbd17a373cc20c919be9fa279a4420033e959ca92

Fix zig fmt clobbering a file's mode


1 files changed, 9 insertions(+), 2 deletions(-)

src-self-hosted/main.zig+9-2
...@@ -684,7 +684,7 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {...@@ -684,7 +684,7 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {
684 if (fmt.seen.exists(real_path)) return;684 if (fmt.seen.exists(real_path)) return;
685 try fmt.seen.put(real_path);685 try fmt.seen.put(real_path);
686686
687 const source_code = fs.cwd().readFileAlloc(fmt.gpa, real_path, max_src_size) catch |err| switch (err) {687 const source_file = fs.cwd().openFile(real_path, .{}) catch |err| switch (err) {
688 error.IsDir, error.AccessDenied => {688 error.IsDir, error.AccessDenied => {
689 var dir = try fs.cwd().openDir(file_path, .{ .iterate = true });689 var dir = try fs.cwd().openDir(file_path, .{ .iterate = true });
690 defer dir.close();690 defer dir.close();
...@@ -705,6 +705,13 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {...@@ -705,6 +705,13 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {
705 return;705 return;
706 },706 },
707 };707 };
708 defer source_file.close();
709
710 const source_code = source_file.reader().readAllAlloc(fmt.gpa, max_src_size) catch |err| {
711 std.debug.warn("unable to read '{}': {}\n", .{ file_path, err });
712 fmt.any_error = true;
713 return;
714 };
708 defer fmt.gpa.free(source_code);715 defer fmt.gpa.free(source_code);
709716
710 const tree = std.zig.parse(fmt.gpa, source_code) catch |err| {717 const tree = std.zig.parse(fmt.gpa, source_code) catch |err| {
...@@ -729,7 +736,7 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {...@@ -729,7 +736,7 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {
729 fmt.any_error = true;736 fmt.any_error = true;
730 }737 }
731 } else {738 } else {
732 const baf = try io.BufferedAtomicFile.create(fmt.gpa, fs.cwd(), real_path, .{});739 const baf = try io.BufferedAtomicFile.create(fmt.gpa, fs.cwd(), real_path, .{ .mode = try source_file.mode() });
733 defer baf.destroy();740 defer baf.destroy();
734741
735 const anything_changed = try std.zig.render(fmt.gpa, baf.stream(), tree);742 const anything_changed = try std.zig.render(fmt.gpa, baf.stream(), tree);