authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-29 17:38:50-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-29 17:38:50-04:00
logd172e3f3bbb61956d8d2202fd008b61f07eb3121
tree3ea270235a67ec36aa77634d859fbfbb74b77311
parent0c16cd2d0ed78be2d160f9c865cd0a8703348232

fix AtomicFile for relative paths

closes #1017

2 files changed, 12 insertions(+), 4 deletions(-)

std/os/index.zig+10-4
......@@ -855,14 +855,20 @@ pub const AtomicFile = struct {
855855 const dirname = os.path.dirname(dest_path);
856856
857857 var rand_buf: [12]u8 = undefined;
858 const tmp_path = try allocator.alloc(u8, dirname.len + 1 + base64.Base64Encoder.calcSize(rand_buf.len));
858
859 const dirname_component_len = if (dirname.len == 0) 0 else dirname.len + 1;
860 const tmp_path = try allocator.alloc(u8, dirname_component_len +
861 base64.Base64Encoder.calcSize(rand_buf.len));
859862 errdefer allocator.free(tmp_path);
860 mem.copy(u8, tmp_path[0..], dirname);
861 tmp_path[dirname.len] = os.path.sep;
863
864 if (dirname.len != 0) {
865 mem.copy(u8, tmp_path[0..], dirname);
866 tmp_path[dirname.len] = os.path.sep;
867 }
862868
863869 while (true) {
864870 try getRandomBytes(rand_buf[0..]);
865 b64_fs_encoder.encode(tmp_path[dirname.len + 1..], rand_buf);
871 b64_fs_encoder.encode(tmp_path[dirname_component_len..], rand_buf);
866872
867873 const file = os.File.openWriteNoClobber(allocator, tmp_path, mode) catch |err| switch (err) {
868874 error.PathAlreadyExists => continue,
std/os/path.zig+2
......@@ -647,6 +647,8 @@ fn testResolvePosix(paths: []const []const u8) []u8 {
647647 return resolvePosix(debug.global_allocator, paths) catch unreachable;
648648}
649649
650/// If the path is a file in the current directory (no directory component)
651/// then the returned slice has .len = 0.
650652pub fn dirname(path: []const u8) []const u8 {
651653 if (is_windows) {
652654 return dirnameWindows(path);