fixdeletetree.zig 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. const std = @import("std");
  2. const builtin = @import("builtin");
  3. //
  4. // TODO: we should fix std library to address these issues
  5. //
  6. pub fn deleteTree(dir: std.fs.Dir, sub_path: []const u8) !void {
  7. if (builtin.os.tag != .windows) {
  8. return dir.deleteTree(sub_path);
  9. }
  10. // workaround issue on windows where it just doesn't delete things
  11. const MAX_ATTEMPTS = 10;
  12. var attempt: u8 = 0;
  13. while (true) : (attempt += 1) {
  14. if (dir.deleteTree(sub_path)) {
  15. return;
  16. } else |err| {
  17. if (attempt == MAX_ATTEMPTS) return err;
  18. switch (err) {
  19. error.FileBusy => {
  20. std.log.warn("path '{s}' is busy (attempt {}), will retry", .{ sub_path, attempt });
  21. std.time.sleep(std.time.ns_per_ms * 100); // sleep for 100 ms
  22. },
  23. else => |e| return e,
  24. }
  25. }
  26. }
  27. }
  28. pub fn deleteTreeAbsolute(dir_absolute: []const u8) !void {
  29. if (builtin.os.tag != .windows) {
  30. return std.fs.deleteTreeAbsolute(dir_absolute);
  31. }
  32. std.debug.assert(std.fs.path.isAbsolute(dir_absolute));
  33. return deleteTree(std.fs.cwd(), dir_absolute);
  34. }