File tree 2 files changed +12
-4
lines changed
2 files changed +12
-4
lines changed Original file line number Diff line number Diff line change @@ -855,14 +855,20 @@ pub const AtomicFile = struct {
855
855
const dirname = os .path .dirname (dest_path );
856
856
857
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
862
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
+ }
862
868
863
869
while (true ) {
864
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
873
const file = os .File .openWriteNoClobber (allocator , tmp_path , mode ) catch | err | switch (err ) {
868
874
error .PathAlreadyExists = > continue ,
Original file line number Diff line number Diff line change @@ -647,6 +647,8 @@ fn testResolvePosix(paths: []const []const u8) []u8 {
647
647
return resolvePosix (debug .global_allocator , paths ) catch unreachable ;
648
648
}
649
649
650
+ /// If the path is a file in the current directory (no directory component)
651
+ /// then the returned slice has .len = 0.
650
652
pub fn dirname (path : []const u8 ) []const u8 {
651
653
if (is_windows ) {
652
654
return dirnameWindows (path );
You can’t perform that action at this time.
0 commit comments