| ... | ... | @@ -53,6 +53,20 @@ pub fn does_file_exist(fpath: []const u8) !bool { |
| 53 | 53 | return true; |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | pub fn does_folder_exist(fpath: []const u8) !bool { |
| 57 | const abs_path = std.fs.realpathAlloc(gpa, fpath) catch |e| switch (e) { |
| 58 | error.FileNotFound => return false, |
| 59 | else => return e, |
| 60 | }; |
| 61 | const file = try std.fs.openFileAbsolute(abs_path, .{}); |
| 62 | defer file.close(); |
| 63 | const s = try file.stat(); |
| 64 | if (s.kind != .Directory) { |
| 65 | return false; |
| 66 | } |
| 67 | return true; |
| 68 | } |
| 69 | |
| 56 | 70 | pub fn _join(comptime delim: []const u8, comptime xs: [][]const u8) []const u8 { |
| 57 | 71 | var buf: []const u8 = ""; |
| 58 | 72 | for (xs) |x,i| { |
| ... | ... | @@ -166,3 +180,45 @@ pub fn list_remove(input: [][]const u8, search: []const u8) ![][]const u8 { |
| 166 | 180 | } |
| 167 | 181 | return list.items; |
| 168 | 182 | } |
| 183 | |
| 184 | pub fn last(in: [][]const u8) ![]const u8 { |
| 185 | if (in.len == 0) { |
| 186 | return error.EmptyArray; |
| 187 | } |
| 188 | return in[in.len - 1]; |
| 189 | } |
| 190 | |
| 191 | pub fn mkdir_all(dpath: []const u8) anyerror!void { |
| 192 | const d = if (dpath[dpath.len-1] == std.fs.path.sep) dpath[0..dpath.len-1] else dpath; |
| 193 | const ps = std.fs.path.sep_str; |
| 194 | const e = dpath[0..std.mem.lastIndexOf(u8, dpath, ps).?]; |
| 195 | if (std.mem.indexOf(u8, e, ps)) |_| {} else { |
| 196 | return; |
| 197 | } |
| 198 | try mkdir_all(e); |
| 199 | if (!try does_folder_exist(d)) { |
| 200 | std.log.debug("mkdir_all: {} doesnt exist, making", .{d}); |
| 201 | try std.fs.makeDirAbsolute(d); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | pub fn rm_recv(path: []const u8) anyerror!void { |
| 206 | const abs_path = std.fs.realpathAlloc(gpa, path) catch |e| switch (e) { |
| 207 | error.FileNotFound => return, |
| 208 | else => return e, |
| 209 | }; |
| 210 | const file = try std.fs.openFileAbsolute(abs_path, .{}); |
| 211 | defer file.close(); |
| 212 | const s = try file.stat(); |
| 213 | if (s.kind == .Directory) { |
| 214 | const dir = std.fs.cwd().openDir(abs_path, .{ .iterate=true, }) catch unreachable; |
| 215 | var iter = dir.iterate(); |
| 216 | while (try iter.next()) |item| { |
| 217 | try rm_recv(try std.fs.path.join(gpa, &[_][]const u8{abs_path, item.name})); |
| 218 | } |
| 219 | try std.fs.deleteDirAbsolute(abs_path); |
| 220 | } |
| 221 | else { |
| 222 | try std.fs.deleteFileAbsolute(abs_path); |
| 223 | } |
| 224 | } |