| ... | @@ -301,35 +301,32 @@ pub fn makeDirW(dir_path: [*:0]const u16) !void { | ... | @@ -301,35 +301,32 @@ pub fn makeDirW(dir_path: [*:0]const u16) !void { |
| 301 | /// already exists and is a directory. | 301 | /// already exists and is a directory. |
| 302 | /// This function is not atomic, and if it returns an error, the file system may | 302 | /// This function is not atomic, and if it returns an error, the file system may |
| 303 | /// have been modified regardless. | 303 | /// have been modified regardless. |
| 304 | /// TODO determine if we can remove the allocator requirement from this function | 304 | pub fn makePath(full_path: []const u8) !void { |
| 305 | pub fn makePath(allocator: *Allocator, full_path: []const u8) !void { | 305 | var end_index: usize = full_path.len; |
| 306 | const resolved_path = try path.resolve(allocator, &[_][]const u8{full_path}); | | |
| 307 | defer allocator.free(resolved_path); | | |
| 308 | | | |
| 309 | var end_index: usize = resolved_path.len; | | |
| 310 | while (true) { | 306 | while (true) { |
| 311 | makeDir(resolved_path[0..end_index]) catch |err| switch (err) { | 307 | cwd().makeDir(full_path[0..end_index]) catch |err| switch (err) { |
| 312 | error.PathAlreadyExists => { | 308 | error.PathAlreadyExists => { |
| 313 | // TODO stat the file and return an error if it's not a directory | 309 | // TODO stat the file and return an error if it's not a directory |
| 314 | // this is important because otherwise a dangling symlink | 310 | // this is important because otherwise a dangling symlink |
| 315 | // could cause an infinite loop | 311 | // could cause an infinite loop |
| 316 | if (end_index == resolved_path.len) return; | 312 | if (end_index == full_path.len) return; |
| 317 | }, | 313 | }, |
| 318 | error.FileNotFound => { | 314 | error.FileNotFound => { |
| | 315 | if (end_index == 0) return err; |
| 319 | // march end_index backward until next path component | 316 | // march end_index backward until next path component |
| 320 | while (true) { | 317 | while (true) { |
| 321 | end_index -= 1; | 318 | end_index -= 1; |
| 322 | if (path.isSep(resolved_path[end_index])) break; | 319 | if (path.isSep(full_path[end_index])) break; |
| 323 | } | 320 | } |
| 324 | continue; | 321 | continue; |
| 325 | }, | 322 | }, |
| 326 | else => return err, | 323 | else => return err, |
| 327 | }; | 324 | }; |
| 328 | if (end_index == resolved_path.len) return; | 325 | if (end_index == full_path.len) return; |
| 329 | // march end_index forward until next path component | 326 | // march end_index forward until next path component |
| 330 | while (true) { | 327 | while (true) { |
| 331 | end_index += 1; | 328 | end_index += 1; |
| 332 | if (end_index == resolved_path.len or path.isSep(resolved_path[end_index])) break; | 329 | if (end_index == full_path.len or path.isSep(full_path[end_index])) break; |
| 333 | } | 330 | } |
| 334 | } | 331 | } |
| 335 | } | 332 | } |