| ... | ... | @@ -301,35 +301,32 @@ pub fn makeDirW(dir_path: [*:0]const u16) !void { |
| 301 | 301 | /// already exists and is a directory. |
| 302 | 302 | /// This function is not atomic, and if it returns an error, the file system may |
| 303 | 303 | /// have been modified regardless. |
| 304 | | /// TODO determine if we can remove the allocator requirement from this function |
| 305 | | pub fn makePath(allocator: *Allocator, full_path: []const u8) !void { |
| 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; |
| 304 | pub fn makePath(full_path: []const u8) !void { |
| 305 | var end_index: usize = full_path.len; |
| 310 | 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 | 308 | error.PathAlreadyExists => { |
| 313 | 309 | // TODO stat the file and return an error if it's not a directory |
| 314 | 310 | // this is important because otherwise a dangling symlink |
| 315 | 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 | 314 | error.FileNotFound => { |
| 315 | if (end_index == 0) return err; |
| 319 | 316 | // march end_index backward until next path component |
| 320 | 317 | while (true) { |
| 321 | 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 | 321 | continue; |
| 325 | 322 | }, |
| 326 | 323 | else => return err, |
| 327 | 324 | }; |
| 328 | | if (end_index == resolved_path.len) return; |
| 325 | if (end_index == full_path.len) return; |
| 329 | 326 | // march end_index forward until next path component |
| 330 | 327 | while (true) { |
| 331 | 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 | } |