authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2020-03-05 20:15:55-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-27 13:35:59-04:00
logfdff381a5644d5e12519ca53012d7b00a33a33c4
treec67deeb0481c776676b84a9106fb1dcbe6caceb5
parent0df82889cf01757f2c19f841a5e3446a5ba70eac

fmt: Fix relative paths with . and .. on Windows

This is a band-aid fix due to NtCreateFile failing on paths with . or .. in them.

1 files changed, 13 insertions(+), 6 deletions(-)

src-self-hosted/stage2.zig+13-6
...@@ -318,11 +318,18 @@ const FmtError = error{...@@ -318,11 +318,18 @@ const FmtError = error{
318} || fs.File.OpenError;318} || fs.File.OpenError;
319319
320fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {320fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {
321 if (fmt.seen.exists(file_path)) return;321 // get the real path here to avoid Windows failing on relative file paths with . or .. in them
322 try fmt.seen.put(file_path);322 var real_path = fs.realpathAlloc(fmt.allocator, file_path) catch |err| {
323 try stderr.print("unable to open '{}': {}\n", .{ file_path, err });
324 fmt.any_error = true;
325 return;
326 };
327 defer fmt.allocator.free(real_path);
328
329 if (fmt.seen.exists(real_path)) return;
330 try fmt.seen.put(real_path);
323331
324 const max = std.math.maxInt(usize);332 const source_code = fs.cwd().readFileAlloc(fmt.allocator, real_path, self_hosted_main.max_src_size) catch |err| switch (err) {
325 const source_code = fs.cwd().readFileAlloc(fmt.allocator, file_path, max) catch |err| switch (err) {
326 error.IsDir, error.AccessDenied => {333 error.IsDir, error.AccessDenied => {
327 // TODO make event based (and dir.next())334 // TODO make event based (and dir.next())
328 var dir = try fs.cwd().openDir(file_path, .{ .iterate = true });335 var dir = try fs.cwd().openDir(file_path, .{ .iterate = true });
...@@ -332,7 +339,7 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {...@@ -332,7 +339,7 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {
332339
333 while (try dir_it.next()) |entry| {340 while (try dir_it.next()) |entry| {
334 if (entry.kind == .Directory or mem.endsWith(u8, entry.name, ".zig")) {341 if (entry.kind == .Directory or mem.endsWith(u8, entry.name, ".zig")) {
335 const full_path = try fs.path.join(fmt.allocator, &[_][]const u8{ file_path, entry.name });342 const full_path = try fs.path.join(fmt.allocator, &[_][]const u8{ real_path, entry.name });
336 try fmtPath(fmt, full_path, check_mode);343 try fmtPath(fmt, full_path, check_mode);
337 }344 }
338 }345 }
...@@ -370,7 +377,7 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {...@@ -370,7 +377,7 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {
370 fmt.any_error = true;377 fmt.any_error = true;
371 }378 }
372 } else {379 } else {
373 const baf = try io.BufferedAtomicFile.create(fmt.allocator, file_path);380 const baf = try io.BufferedAtomicFile.create(fmt.allocator, real_path);
374 defer baf.destroy();381 defer baf.destroy();
375382
376 const anything_changed = try std.zig.render(fmt.allocator, baf.stream(), tree);383 const anything_changed = try std.zig.render(fmt.allocator, baf.stream(), tree);