| ... | ... | @@ -146,6 +146,7 @@ pub fn iterate(self: Dir) Iterator { |
| 146 | 146 | .buf = undefined, |
| 147 | 147 | .idx = 0, |
| 148 | 148 | .len = 0, |
| 149 | .seek = 0, |
| 149 | 150 | }; |
| 150 | 151 | } |
| 151 | 152 | |
| ... | ... | @@ -154,8 +155,28 @@ pub const Iterator = struct { |
| 154 | 155 | buf: [1024]u8, |
| 155 | 156 | idx: usize, |
| 156 | 157 | len: usize, |
| 158 | seek: c_long, |
| 157 | 159 | |
| 158 | 160 | pub fn next(iter: *Iterator) !?Entry { |
| 161 | if (os == .macos) { |
| 162 | if (iter.idx == iter.len) { |
| 163 | const len = try sys.getdirentries(@intFromEnum(iter.dir.fd), &iter.buf, &iter.seek); |
| 164 | if (len == 0) return null; |
| 165 | iter.idx = 0; |
| 166 | iter.len = len; |
| 167 | } |
| 168 | const ent: *align(1) sys.struct_dirent = @ptrCast(&iter.buf[iter.idx]); |
| 169 | iter.idx += ent.reclen; |
| 170 | ent.name[ent.namlen] = 0; |
| 171 | const name = ent.name[0..ent.namlen :0]; |
| 172 | if (std.mem.eql(u8, name, ".")) return next(iter); |
| 173 | if (std.mem.eql(u8, name, "..")) return next(iter); |
| 174 | if (ent.ino == 0) return next(iter); |
| 175 | return .{ |
| 176 | .name = name, |
| 177 | .type = ent.type, |
| 178 | }; |
| 179 | } |
| 159 | 180 | if (iter.idx == iter.len) { |
| 160 | 181 | const len = try sys.getdents(@intFromEnum(iter.dir.fd), &iter.buf); |
| 161 | 182 | if (len == 0) return null; |
| ... | ... | @@ -328,3 +349,49 @@ pub const AccessMode = packed struct(c_uint) { |
| 328 | 349 | executable: bool = false, |
| 329 | 350 | _: u29 = 0, |
| 330 | 351 | }; |
| 352 | |
| 353 | pub fn realpath(self: Dir, sub_path: [:0]const u8, buf: *[sys.PATH_MAX]u8) ![:0]u8 { |
| 354 | if (std.mem.eql(u8, sub_path, ".")) { |
| 355 | if (@intFromEnum(self.fd) == sys.AT.FDCWD) { |
| 356 | return nfs.cwdpath(buf); |
| 357 | } |
| 358 | return nfs.realdpath(self.fd, buf); |
| 359 | } |
| 360 | var file = try self.openFile(sub_path, .{}); |
| 361 | defer file.close(); |
| 362 | return nfs.realdpath(file.fd, buf); |
| 363 | } |
| 364 | |
| 365 | pub fn realpathAlloc(self: Dir, allocator: std.mem.Allocator, sub_path: [:0]const u8) ![:0]u8 { |
| 366 | var buf: [sys.PATH_MAX]u8 = undefined; |
| 367 | const actual = try self.realpath(sub_path, &buf); |
| 368 | return allocator.dupeZ(u8, actual); |
| 369 | } |
| 370 | |
| 371 | pub fn deleteFile(self: Dir, sub_path: [:0]const u8) !void { |
| 372 | return sys.unlinkat(@intFromEnum(self.fd), sub_path.ptr, 0); |
| 373 | } |
| 374 | |
| 375 | pub fn deleteDir(self: Dir, sub_path: [:0]const u8) !void { |
| 376 | return sys.unlinkat(@intFromEnum(self.fd), sub_path.ptr, sys.AT.REMOVEDIR); |
| 377 | } |
| 378 | |
| 379 | pub fn deleteTree(self: Dir, sub_path: [:0]const u8) !void { |
| 380 | var dir = try self.openDir(sub_path, .{}); |
| 381 | { |
| 382 | errdefer dir.close(); |
| 383 | var iter = dir.iterate(); |
| 384 | while (try iter.next()) |entry| { |
| 385 | switch (entry.type) { |
| 386 | .DIR => { |
| 387 | try dir.deleteTree(entry.name); |
| 388 | }, |
| 389 | else => { |
| 390 | try dir.deleteFile(entry.name); |
| 391 | }, |
| 392 | } |
| 393 | } |
| 394 | dir.close(); |
| 395 | } |
| 396 | try self.deleteDir(sub_path); |
| 397 | } |