commit d172e3f3bbb61956d8d2202fd008b61f07eb3121 (tree)
parent 0c16cd2d0ed78be2d160f9c865cd0a8703348232
Author: Andrew Kelley <superjoe30@gmail.com>
Date: Tue, 29 May 2018 17:38:50 -0400
fix AtomicFile for relative paths
closes #1017
Diffstat:
2 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/std/os/index.zig b/std/os/index.zig
@@ -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,
diff --git a/std/os/path.zig b/std/os/path.zig
@@ -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);