Skip to content

Commit

Permalink
fix AtomicFile for relative paths
Browse files Browse the repository at this point in the history
closes #1017
  • Loading branch information
andrewrk committed May 29, 2018
1 parent 0c16cd2 commit d172e3f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 10 additions & 4 deletions std/os/index.zig
Expand Up @@ -855,14 +855,20 @@ pub const AtomicFile = struct {
const dirname = os.path.dirname(dest_path);

var rand_buf: [12]u8 = undefined;
const tmp_path = try allocator.alloc(u8, dirname.len + 1 + base64.Base64Encoder.calcSize(rand_buf.len));

const dirname_component_len = if (dirname.len == 0) 0 else dirname.len + 1;
const tmp_path = try allocator.alloc(u8, dirname_component_len +
base64.Base64Encoder.calcSize(rand_buf.len));
errdefer allocator.free(tmp_path);
mem.copy(u8, tmp_path[0..], dirname);
tmp_path[dirname.len] = os.path.sep;

if (dirname.len != 0) {
mem.copy(u8, tmp_path[0..], dirname);
tmp_path[dirname.len] = os.path.sep;
}

while (true) {
try getRandomBytes(rand_buf[0..]);
b64_fs_encoder.encode(tmp_path[dirname.len + 1..], rand_buf);
b64_fs_encoder.encode(tmp_path[dirname_component_len..], rand_buf);

const file = os.File.openWriteNoClobber(allocator, tmp_path, mode) catch |err| switch (err) {
error.PathAlreadyExists => continue,
Expand Down
2 changes: 2 additions & 0 deletions std/os/path.zig
Expand Up @@ -647,6 +647,8 @@ fn testResolvePosix(paths: []const []const u8) []u8 {
return resolvePosix(debug.global_allocator, paths) catch unreachable;
}

/// If the path is a file in the current directory (no directory component)
/// then the returned slice has .len = 0.
pub fn dirname(path: []const u8) []const u8 {
if (is_windows) {
return dirnameWindows(path);
Expand Down

0 comments on commit d172e3f

Please sign in to comment.