authorgravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-12-05 10:06:29 -08:00
committergravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-12-05 10:06:29 -08:00
loga425c075efb9dd9cf38cace488aa8ba8afa0a6c6
treedd65c8e0f71eb8e2173d2e461eae809dce28b041
parent625886b182cf6b8721f7fa282606572db5a6166d

util/funcs- add more


1 files changed, 56 insertions(+), 0 deletions(-)

src/util/funcs.zig+56
......@@ -53,6 +53,20 @@ pub fn does_file_exist(fpath: []const u8) !bool {
5353 return true;
5454}
5555
56pub 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
5670pub fn _join(comptime delim: []const u8, comptime xs: [][]const u8) []const u8 {
5771 var buf: []const u8 = "";
5872 for (xs) |x,i| {
......@@ -166,3 +180,45 @@ pub fn list_remove(input: [][]const u8, search: []const u8) ![][]const u8 {
166180 }
167181 return list.items;
168182}
183
184pub 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
191pub 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
205pub 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}