Skip to content

Commit d172e3f

Browse files
committed
fix AtomicFile for relative paths
closes #1017
1 parent 0c16cd2 commit d172e3f

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

std/os/index.zig

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
650652
pub fn dirname(path: []const u8) []const u8 {
651653
if (is_windows) {
652654
return dirnameWindows(path);

0 commit comments

Comments
 (0)