aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCassidy Dingenskirchen <admin@15318.de>2020-06-16 19:07:55 +0200
committerAndrew Kelley <andrew@ziglang.org>2020-06-18 20:41:40 -0400
commitb30642a86ae59f9cfe73c8a9ed6e79c3cb5aaa36 (patch)
tree093cf7ea9394b560f23dd66ac08ba5c3af44bcaf
parentbd17a373cc20c919be9fa279a4420033e959ca92 (diff)
downloadzig-b30642a86ae59f9cfe73c8a9ed6e79c3cb5aaa36.tar.gz
zig-b30642a86ae59f9cfe73c8a9ed6e79c3cb5aaa36.zip
Fix zig fmt clobbering a file's mode
-rw-r--r--src-self-hosted/main.zig11
1 files changed, 9 insertions, 2 deletions
diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig
index eda9dcfc31..4db98c60dc 100644
--- a/src-self-hosted/main.zig
+++ b/src-self-hosted/main.zig
@@ -684,7 +684,7 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {
if (fmt.seen.exists(real_path)) return;
try fmt.seen.put(real_path);
- const source_code = fs.cwd().readFileAlloc(fmt.gpa, real_path, max_src_size) catch |err| switch (err) {
+ const source_file = fs.cwd().openFile(real_path, .{}) catch |err| switch (err) {
error.IsDir, error.AccessDenied => {
var dir = try fs.cwd().openDir(file_path, .{ .iterate = true });
defer dir.close();
@@ -705,6 +705,13 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {
return;
},
};
+ defer source_file.close();
+
+ const source_code = source_file.reader().readAllAlloc(fmt.gpa, max_src_size) catch |err| {
+ std.debug.warn("unable to read '{}': {}\n", .{ file_path, err });
+ fmt.any_error = true;
+ return;
+ };
defer fmt.gpa.free(source_code);
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 {
fmt.any_error = true;
}
} else {
- const baf = try io.BufferedAtomicFile.create(fmt.gpa, fs.cwd(), real_path, .{});
+ const baf = try io.BufferedAtomicFile.create(fmt.gpa, fs.cwd(), real_path, .{ .mode = try source_file.mode() });
defer baf.destroy();
const anything_changed = try std.zig.render(fmt.gpa, baf.stream(), tree);