| ... | @@ -855,14 +855,20 @@ pub const AtomicFile = struct { | ... | @@ -855,14 +855,20 @@ pub const AtomicFile = struct { |
| 855 | const dirname = os.path.dirname(dest_path); | 855 | const dirname = os.path.dirname(dest_path); |
| 856 | | 856 | |
| 857 | var rand_buf: [12]u8 = undefined; | 857 | 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)); |
| 859 | errdefer allocator.free(tmp_path); | 862 | errdefer allocator.free(tmp_path); |
| 860 | mem.copy(u8, tmp_path[0..], dirname); | 863 | |
| 861 | tmp_path[dirname.len] = os.path.sep; | 864 | if (dirname.len != 0) { |
| | 865 | mem.copy(u8, tmp_path[0..], dirname); |
| | 866 | tmp_path[dirname.len] = os.path.sep; |
| | 867 | } |
| 862 | | 868 | |
| 863 | while (true) { | 869 | while (true) { |
| 864 | try getRandomBytes(rand_buf[0..]); | 870 | 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); |
| 866 | | 872 | |
| 867 | const file = os.File.openWriteNoClobber(allocator, tmp_path, mode) catch |err| switch (err) { | 873 | const file = os.File.openWriteNoClobber(allocator, tmp_path, mode) catch |err| switch (err) { |
| 868 | error.PathAlreadyExists => continue, | 874 | error.PathAlreadyExists => continue, |