2024-04-20 05:49:42 -08:00
|
|
|
// SPDX-FileCopyrightText: Yorhel <projects@yorhel.nl>
|
2021-07-18 01:36:05 -08:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
2021-04-29 02:48:45 -08:00
|
|
|
const std = @import("std");
|
|
|
|
|
const main = @import("main.zig");
|
2021-05-29 03:18:21 -08:00
|
|
|
const ui = @import("ui.zig");
|
2021-07-19 05:28:11 -08:00
|
|
|
const util = @import("util.zig");
|
2021-04-29 02:48:45 -08:00
|
|
|
|
2021-07-19 05:28:11 -08:00
|
|
|
pub const EType = enum(u2) { dir, link, file };
|
2021-04-29 02:48:45 -08:00
|
|
|
|
2022-11-02 02:28:43 -08:00
|
|
|
// Type for the Entry.Packed.blocks field. Smaller than a u64 to make room for flags.
|
2024-07-14 06:21:59 -08:00
|
|
|
pub const Blocks = u61;
|
2021-07-13 03:33:38 -08:00
|
|
|
|
2021-04-29 02:48:45 -08:00
|
|
|
// Memory layout:
|
2021-07-16 09:13:02 -08:00
|
|
|
// (Ext +) Dir + name
|
|
|
|
|
// or: (Ext +) Link + name
|
|
|
|
|
// or: (Ext +) File + name
|
2021-04-29 02:48:45 -08:00
|
|
|
//
|
|
|
|
|
// Entry is always the first part of Dir, Link and File, so a pointer cast to
|
2021-07-16 09:13:02 -08:00
|
|
|
// *Entry is always safe and an *Entry can be casted to the full type. The Ext
|
|
|
|
|
// struct, if present, is placed before the *Entry pointer.
|
|
|
|
|
// These are all packed structs and hence do not have any alignment, which is
|
|
|
|
|
// great for saving memory but perhaps not very great for code size or
|
|
|
|
|
// performance.
|
2022-11-02 02:28:43 -08:00
|
|
|
pub const Entry = extern struct {
|
|
|
|
|
pack: Packed align(1),
|
2022-11-02 05:39:05 -08:00
|
|
|
size: u64 align(1) = 0,
|
|
|
|
|
next: ?*Entry align(1) = null,
|
2022-11-02 02:28:43 -08:00
|
|
|
|
|
|
|
|
pub const Packed = packed struct(u64) {
|
|
|
|
|
etype: EType,
|
|
|
|
|
isext: bool,
|
2022-11-02 05:39:05 -08:00
|
|
|
blocks: Blocks = 0, // 512-byte blocks
|
2022-11-02 02:28:43 -08:00
|
|
|
};
|
2021-04-29 02:48:45 -08:00
|
|
|
|
|
|
|
|
const Self = @This();
|
|
|
|
|
|
|
|
|
|
pub fn dir(self: *Self) ?*Dir {
|
2023-04-09 07:45:11 -08:00
|
|
|
return if (self.pack.etype == .dir) @ptrCast(self) else null;
|
2021-04-29 02:48:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn link(self: *Self) ?*Link {
|
2023-04-09 07:45:11 -08:00
|
|
|
return if (self.pack.etype == .link) @ptrCast(self) else null;
|
2021-04-29 02:48:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn file(self: *Self) ?*File {
|
2023-04-09 07:45:11 -08:00
|
|
|
return if (self.pack.etype == .file) @ptrCast(self) else null;
|
2021-04-29 02:48:45 -08:00
|
|
|
}
|
|
|
|
|
|
2021-06-11 03:05:39 -08:00
|
|
|
// Whether this entry should be displayed as a "directory".
|
|
|
|
|
// Some dirs are actually represented in this data model as a File for efficiency.
|
|
|
|
|
pub fn isDirectory(self: *Self) bool {
|
2022-11-02 02:28:43 -08:00
|
|
|
return if (self.file()) |f| f.pack.other_fs or f.pack.kernfs else self.pack.etype == .dir;
|
2021-06-11 03:05:39 -08:00
|
|
|
}
|
|
|
|
|
|
2021-04-29 02:48:45 -08:00
|
|
|
pub fn name(self: *const Self) [:0]const u8 {
|
2023-04-09 07:45:11 -08:00
|
|
|
const self_name = switch (self.pack.etype) {
|
|
|
|
|
.dir => &@as(*const Dir, @ptrCast(self)).name,
|
|
|
|
|
.link => &@as(*const Link, @ptrCast(self)).name,
|
|
|
|
|
.file => &@as(*const File, @ptrCast(self)).name,
|
2022-11-02 05:39:05 -08:00
|
|
|
};
|
2023-04-09 07:45:11 -08:00
|
|
|
const name_ptr: [*:0]const u8 = @ptrCast(self_name);
|
|
|
|
|
return std.mem.sliceTo(name_ptr, 0);
|
2021-04-29 02:48:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn ext(self: *Self) ?*Ext {
|
2022-11-02 02:28:43 -08:00
|
|
|
if (!self.pack.isext) return null;
|
2023-04-09 07:45:11 -08:00
|
|
|
return @ptrCast(@as([*]Ext, @ptrCast(self)) - 1);
|
2021-04-29 02:48:45 -08:00
|
|
|
}
|
|
|
|
|
|
2024-07-12 02:31:57 -08:00
|
|
|
fn alloc(comptime T: type, allocator: std.mem.Allocator, etype: EType, isext: bool, ename: []const u8) *Entry {
|
2022-11-02 05:39:05 -08:00
|
|
|
const size = (if (isext) @as(usize, @sizeOf(Ext)) else 0) + @sizeOf(T) + ename.len + 1;
|
|
|
|
|
var ptr = blk: while (true) {
|
|
|
|
|
if (allocator.allocWithOptions(u8, size, 1, null)) |p| break :blk p
|
|
|
|
|
else |_| {}
|
|
|
|
|
ui.oom();
|
|
|
|
|
};
|
|
|
|
|
if (isext) {
|
2023-04-09 07:45:11 -08:00
|
|
|
@as(*Ext, @ptrCast(ptr)).* = .{};
|
2022-11-02 05:39:05 -08:00
|
|
|
ptr = ptr[@sizeOf(Ext)..];
|
|
|
|
|
}
|
2023-04-09 07:45:11 -08:00
|
|
|
const e: *T = @ptrCast(ptr);
|
2022-11-02 05:39:05 -08:00
|
|
|
e.* = .{ .entry = .{ .pack = .{ .etype = etype, .isext = isext } } };
|
2023-04-09 07:45:11 -08:00
|
|
|
const n = @as([*]u8, @ptrCast(&e.name))[0..ename.len+1];
|
|
|
|
|
@memcpy(n[0..ename.len], ename);
|
2022-11-02 05:39:05 -08:00
|
|
|
n[ename.len] = 0;
|
|
|
|
|
return &e.entry;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-12 02:31:57 -08:00
|
|
|
pub fn create(allocator: std.mem.Allocator, etype: EType, isext: bool, ename: []const u8) *Entry {
|
2022-11-02 05:39:05 -08:00
|
|
|
return switch (etype) {
|
2024-07-12 02:31:57 -08:00
|
|
|
.dir => alloc(Dir, allocator, etype, isext, ename),
|
|
|
|
|
.file => alloc(File, allocator, etype, isext, ename),
|
|
|
|
|
.link => alloc(Link, allocator, etype, isext, ename),
|
2021-05-29 03:18:21 -08:00
|
|
|
};
|
2021-04-29 02:48:45 -08:00
|
|
|
}
|
|
|
|
|
|
2023-12-05 02:03:39 -09:00
|
|
|
fn hasErr(self: *Self) bool {
|
|
|
|
|
return
|
|
|
|
|
if (self.file()) |f| f.pack.err
|
|
|
|
|
else if (self.dir()) |d| d.pack.err or d.pack.suberr
|
|
|
|
|
else false;
|
2021-04-29 02:48:45 -08:00
|
|
|
}
|
|
|
|
|
|
2024-07-14 09:57:32 -08:00
|
|
|
fn removeLinks(self: *Entry) void {
|
|
|
|
|
if (self.dir()) |d| {
|
|
|
|
|
var it = d.sub;
|
|
|
|
|
while (it) |e| : (it = e.next) e.removeLinks();
|
|
|
|
|
}
|
|
|
|
|
if (self.link()) |l| l.removeLink();
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-14 06:21:59 -08:00
|
|
|
fn zeroStatsRec(self: *Entry) void {
|
|
|
|
|
self.pack.blocks = 0;
|
|
|
|
|
self.size = 0;
|
|
|
|
|
if (self.file()) |f| f.pack = .{};
|
|
|
|
|
if (self.dir()) |d| {
|
|
|
|
|
d.items = 0;
|
|
|
|
|
d.pack.err = false;
|
|
|
|
|
d.pack.suberr = false;
|
|
|
|
|
var it = d.sub;
|
2024-07-14 09:57:32 -08:00
|
|
|
while (it) |e| : (it = e.next) e.zeroStatsRec();
|
2021-07-13 03:33:38 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-14 06:21:59 -08:00
|
|
|
// Recursively set stats and those of sub-items to zero and removes counts
|
|
|
|
|
// from parent directories; as if this item does not exist in the tree.
|
|
|
|
|
// XXX: Does not update the 'suberr' flag of parent directories, make sure
|
|
|
|
|
// to call updateSubErr() afterwards.
|
|
|
|
|
pub fn zeroStats(self: *Entry, parent: ?*Dir) void {
|
2024-07-14 09:57:32 -08:00
|
|
|
self.removeLinks();
|
2024-07-14 06:21:59 -08:00
|
|
|
|
|
|
|
|
var it = parent;
|
|
|
|
|
while (it) |p| : (it = p.parent) {
|
|
|
|
|
p.entry.pack.blocks -|= self.pack.blocks;
|
|
|
|
|
p.entry.size -|= self.size;
|
|
|
|
|
p.items -|= 1 + (if (self.dir()) |d| d.items else 0);
|
2021-07-13 03:33:38 -08:00
|
|
|
}
|
2024-07-14 06:21:59 -08:00
|
|
|
self.zeroStatsRec();
|
2021-06-01 03:00:54 -08:00
|
|
|
}
|
2021-04-29 02:48:45 -08:00
|
|
|
};
|
|
|
|
|
|
2022-11-02 02:28:43 -08:00
|
|
|
const DevId = u30; // Can be reduced to make room for more flags in Dir.Packed.
|
2021-04-29 02:48:45 -08:00
|
|
|
|
2022-11-02 02:28:43 -08:00
|
|
|
pub const Dir = extern struct {
|
2021-04-29 02:48:45 -08:00
|
|
|
entry: Entry,
|
|
|
|
|
|
2022-11-02 05:39:05 -08:00
|
|
|
sub: ?*Entry align(1) = null,
|
|
|
|
|
parent: ?*Dir align(1) = null,
|
2021-04-29 02:48:45 -08:00
|
|
|
|
2021-05-06 09:15:47 -08:00
|
|
|
// entry.{blocks,size}: Total size of all unique files + dirs. Non-shared hardlinks are counted only once.
|
2021-04-29 02:48:45 -08:00
|
|
|
// (i.e. the space you'll need if you created a filesystem with only this dir)
|
|
|
|
|
// shared_*: Unique hardlinks that still have references outside of this directory.
|
|
|
|
|
// (i.e. the space you won't reclaim by deleting this dir)
|
2021-05-06 09:15:47 -08:00
|
|
|
// (space reclaimed by deleting a dir =~ entry. - shared_)
|
2022-11-02 05:39:05 -08:00
|
|
|
shared_blocks: u64 align(1) = 0,
|
|
|
|
|
shared_size: u64 align(1) = 0,
|
|
|
|
|
items: u32 align(1) = 0,
|
2021-04-29 02:48:45 -08:00
|
|
|
|
2022-11-02 05:39:05 -08:00
|
|
|
pack: Packed align(1) = .{},
|
2021-04-29 02:48:45 -08:00
|
|
|
|
2021-07-19 05:28:11 -08:00
|
|
|
// Only used to find the @offsetOff, the name is written at this point as a 0-terminated string.
|
2021-04-29 02:48:45 -08:00
|
|
|
// (Old C habits die hard)
|
2022-11-02 05:39:05 -08:00
|
|
|
name: [0]u8 = undefined,
|
2022-11-02 02:28:43 -08:00
|
|
|
|
|
|
|
|
pub const Packed = packed struct {
|
|
|
|
|
// Indexes into the global 'devices.list' array
|
2022-11-02 05:39:05 -08:00
|
|
|
dev: DevId = 0,
|
|
|
|
|
err: bool = false,
|
|
|
|
|
suberr: bool = false,
|
2022-11-02 02:28:43 -08:00
|
|
|
};
|
2021-07-26 04:03:08 -08:00
|
|
|
|
|
|
|
|
pub fn fmtPath(self: *const @This(), withRoot: bool, out: *std.ArrayList(u8)) void {
|
2021-07-28 10:08:54 -08:00
|
|
|
if (!withRoot and self.parent == null) return;
|
2021-07-26 04:03:08 -08:00
|
|
|
var components = std.ArrayList([:0]const u8).init(main.allocator);
|
|
|
|
|
defer components.deinit();
|
|
|
|
|
var it: ?*const @This() = self;
|
|
|
|
|
while (it) |e| : (it = e.parent)
|
2021-07-28 10:08:54 -08:00
|
|
|
if (withRoot or e.parent != null)
|
2021-07-26 04:03:08 -08:00
|
|
|
components.append(e.entry.name()) catch unreachable;
|
|
|
|
|
|
|
|
|
|
var i: usize = components.items.len-1;
|
|
|
|
|
while (true) {
|
2021-10-06 04:49:40 -08:00
|
|
|
if (i != components.items.len-1 and !(out.items.len != 0 and out.items[out.items.len-1] == '/')) out.append('/') catch unreachable;
|
2021-07-26 04:03:08 -08:00
|
|
|
out.appendSlice(components.items[i]) catch unreachable;
|
|
|
|
|
if (i == 0) break;
|
|
|
|
|
i -= 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-05 02:03:39 -09:00
|
|
|
|
|
|
|
|
// Only updates the suberr of this Dir, assumes child dirs have already
|
|
|
|
|
// been updated and does not propagate to parents.
|
|
|
|
|
pub fn updateSubErr(self: *@This()) void {
|
|
|
|
|
self.pack.suberr = false;
|
|
|
|
|
var sub = self.sub;
|
|
|
|
|
while (sub) |e| : (sub = e.next) {
|
|
|
|
|
if (e.hasErr()) {
|
|
|
|
|
self.pack.suberr = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-29 02:48:45 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// File that's been hardlinked (i.e. nlink > 1)
|
2022-11-02 02:28:43 -08:00
|
|
|
pub const Link = extern struct {
|
2021-04-29 02:48:45 -08:00
|
|
|
entry: Entry,
|
2022-11-02 05:39:05 -08:00
|
|
|
parent: *Dir align(1) = undefined,
|
2024-07-14 09:57:32 -08:00
|
|
|
next: *Link align(1) = undefined, // circular linked list of all *Link nodes with the same dev,ino.
|
|
|
|
|
prev: *Link align(1) = undefined,
|
2021-04-29 02:48:45 -08:00
|
|
|
// dev is inherited from the parent Dir
|
2022-11-02 05:39:05 -08:00
|
|
|
ino: u64 align(1) = undefined,
|
2024-07-17 04:04:17 -08:00
|
|
|
pack: Pack align(1) = .{},
|
2022-11-02 05:39:05 -08:00
|
|
|
name: [0]u8 = undefined,
|
2021-07-28 00:29:15 -08:00
|
|
|
|
2024-07-17 04:04:17 -08:00
|
|
|
const Pack = packed struct(u32) {
|
|
|
|
|
// Whether this Inode is counted towards the parent directories.
|
|
|
|
|
// Is kept synchronized between all Link nodes with the same dev/ino.
|
|
|
|
|
counted: bool = false,
|
|
|
|
|
// Number of links for this inode. When set to '0', we don't know the
|
|
|
|
|
// actual nlink count; which happens for old JSON dumps.
|
|
|
|
|
nlink: u31 = undefined,
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-28 00:29:15 -08:00
|
|
|
// Return value should be freed with main.allocator.
|
2022-11-02 05:51:22 -08:00
|
|
|
pub fn path(self: *const @This(), withRoot: bool) [:0]const u8 {
|
2021-07-28 00:29:15 -08:00
|
|
|
var out = std.ArrayList(u8).init(main.allocator);
|
|
|
|
|
self.parent.fmtPath(withRoot, &out);
|
|
|
|
|
out.append('/') catch unreachable;
|
|
|
|
|
out.appendSlice(self.entry.name()) catch unreachable;
|
|
|
|
|
return out.toOwnedSliceSentinel(0) catch unreachable;
|
|
|
|
|
}
|
2024-07-14 09:57:32 -08:00
|
|
|
|
|
|
|
|
// Add this link to the inodes map and mark it as 'uncounted'.
|
2024-07-17 04:04:17 -08:00
|
|
|
pub fn addLink(l: *@This()) void {
|
|
|
|
|
const d = inodes.map.getOrPut(l) catch unreachable;
|
2024-07-14 09:57:32 -08:00
|
|
|
if (!d.found_existing) {
|
|
|
|
|
l.next = l;
|
|
|
|
|
l.prev = l;
|
|
|
|
|
} else {
|
2024-07-28 00:34:02 -08:00
|
|
|
inodes.setStats(d.key_ptr.*, false);
|
2024-07-14 09:57:32 -08:00
|
|
|
l.next = d.key_ptr.*;
|
|
|
|
|
l.prev = d.key_ptr.*.prev;
|
|
|
|
|
l.next.prev = l;
|
|
|
|
|
l.prev.next = l;
|
|
|
|
|
}
|
|
|
|
|
inodes.addUncounted(l);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove this link from the inodes map and remove its stats from parent directories.
|
|
|
|
|
fn removeLink(l: *@This()) void {
|
2024-07-17 04:04:17 -08:00
|
|
|
inodes.setStats(l, false);
|
2024-07-14 09:57:32 -08:00
|
|
|
const entry = inodes.map.getEntry(l) orelse return;
|
|
|
|
|
if (l.next == l) {
|
|
|
|
|
_ = inodes.map.remove(l);
|
|
|
|
|
_ = inodes.uncounted.remove(l);
|
|
|
|
|
} else {
|
|
|
|
|
// XXX: If this link is actually removed from the filesystem, then
|
|
|
|
|
// the nlink count of the existing links should be updated to
|
|
|
|
|
// reflect that. But we can't do that here, because this function
|
|
|
|
|
// is also called before doing a filesystem refresh - in which case
|
|
|
|
|
// the nlink count likely won't change. Best we can hope for is
|
|
|
|
|
// that a refresh will encounter another link to the same inode and
|
|
|
|
|
// trigger an nlink change.
|
|
|
|
|
if (entry.key_ptr.* == l)
|
|
|
|
|
entry.key_ptr.* = l.next;
|
|
|
|
|
inodes.addUncounted(l.next);
|
|
|
|
|
l.next.prev = l.prev;
|
|
|
|
|
l.prev.next = l.next;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-29 02:48:45 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Anything that's not an (indexed) directory or hardlink. Excluded directories are also "Files".
|
2022-11-02 02:28:43 -08:00
|
|
|
pub const File = extern struct {
|
2021-04-29 02:48:45 -08:00
|
|
|
entry: Entry,
|
2022-11-02 05:39:05 -08:00
|
|
|
pack: Packed = .{},
|
|
|
|
|
name: [0]u8 = undefined,
|
2022-11-02 02:28:43 -08:00
|
|
|
|
|
|
|
|
pub const Packed = packed struct(u8) {
|
|
|
|
|
err: bool = false,
|
|
|
|
|
excluded: bool = false,
|
|
|
|
|
other_fs: bool = false,
|
|
|
|
|
kernfs: bool = false,
|
|
|
|
|
notreg: bool = false,
|
|
|
|
|
_pad: u3 = 0, // Make this struct "ABI sized" to allow inclusion in an extern struct
|
|
|
|
|
};
|
2021-04-29 02:48:45 -08:00
|
|
|
};
|
|
|
|
|
|
2022-11-02 02:28:43 -08:00
|
|
|
pub const Ext = extern struct {
|
|
|
|
|
mtime: u64 align(1) = 0,
|
|
|
|
|
uid: u32 align(1) = 0,
|
|
|
|
|
gid: u32 align(1) = 0,
|
|
|
|
|
mode: u16 align(1) = 0,
|
2021-04-29 02:48:45 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2021-07-28 00:29:15 -08:00
|
|
|
// List of st_dev entries. Those are typically 64bits, but that's quite a waste
|
|
|
|
|
// of space when a typical scan won't cover many unique devices.
|
2021-05-29 09:22:00 -08:00
|
|
|
pub const devices = struct {
|
2024-07-12 02:31:57 -08:00
|
|
|
var lock = std.Thread.Mutex{};
|
2021-07-28 00:29:15 -08:00
|
|
|
// id -> dev
|
|
|
|
|
pub var list = std.ArrayList(u64).init(main.allocator);
|
2021-06-07 00:57:24 -08:00
|
|
|
// dev -> id
|
2021-07-28 00:29:15 -08:00
|
|
|
var lookup = std.AutoHashMap(u64, DevId).init(main.allocator);
|
2021-05-29 09:22:00 -08:00
|
|
|
|
|
|
|
|
pub fn getId(dev: u64) DevId {
|
2024-07-12 02:31:57 -08:00
|
|
|
lock.lock();
|
|
|
|
|
defer lock.unlock();
|
2023-11-19 23:40:20 -09:00
|
|
|
const d = lookup.getOrPut(dev) catch unreachable;
|
2021-05-29 09:22:00 -08:00
|
|
|
if (!d.found_existing) {
|
2023-04-09 07:45:11 -08:00
|
|
|
d.value_ptr.* = @as(DevId, @intCast(list.items.len));
|
2021-07-28 00:29:15 -08:00
|
|
|
list.append(dev) catch unreachable;
|
2021-05-29 09:22:00 -08:00
|
|
|
}
|
2021-06-07 00:57:24 -08:00
|
|
|
return d.value_ptr.*;
|
2021-04-29 02:48:45 -08:00
|
|
|
}
|
2021-05-29 09:22:00 -08:00
|
|
|
};
|
2021-04-29 02:48:45 -08:00
|
|
|
|
2021-06-01 03:00:54 -08:00
|
|
|
|
2021-07-28 00:29:15 -08:00
|
|
|
// Lookup table for ino -> *Link entries, used for hard link counting.
|
|
|
|
|
pub const inodes = struct {
|
|
|
|
|
// Keys are hashed by their (dev,ino), the *Link points to an arbitrary
|
|
|
|
|
// node in the list. Link entries with the same dev/ino are part of a
|
|
|
|
|
// circular linked list, so you can iterate through all of them with this
|
|
|
|
|
// single pointer.
|
2024-07-17 04:04:17 -08:00
|
|
|
const Map = std.HashMap(*Link, void, HashContext, 80);
|
2021-07-28 00:29:15 -08:00
|
|
|
pub var map = Map.init(main.allocator);
|
|
|
|
|
|
2021-07-28 10:12:50 -08:00
|
|
|
// List of nodes in 'map' with !counted, to speed up addAllStats().
|
|
|
|
|
// If this list grows large relative to the number of nodes in 'map', then
|
|
|
|
|
// this list is cleared and uncounted_full is set instead, so that
|
|
|
|
|
// addAllStats() will do a full iteration over 'map'.
|
|
|
|
|
var uncounted = std.HashMap(*Link, void, HashContext, 80).init(main.allocator);
|
|
|
|
|
var uncounted_full = true; // start with true for the initial scan
|
|
|
|
|
|
2024-07-14 09:57:32 -08:00
|
|
|
pub var lock = std.Thread.Mutex{};
|
|
|
|
|
|
2021-06-07 00:57:24 -08:00
|
|
|
const HashContext = struct {
|
2021-07-28 00:29:15 -08:00
|
|
|
pub fn hash(_: @This(), l: *Link) u64 {
|
2021-06-01 03:00:54 -08:00
|
|
|
var h = std.hash.Wyhash.init(0);
|
2022-11-02 02:28:43 -08:00
|
|
|
h.update(std.mem.asBytes(&@as(u32, l.parent.pack.dev)));
|
2021-07-28 00:29:15 -08:00
|
|
|
h.update(std.mem.asBytes(&l.ino));
|
2021-06-01 03:00:54 -08:00
|
|
|
return h.final();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-28 00:29:15 -08:00
|
|
|
pub fn eql(_: @This(), a: *Link, b: *Link) bool {
|
2022-11-02 02:28:43 -08:00
|
|
|
return a.ino == b.ino and a.parent.pack.dev == b.parent.pack.dev;
|
2021-06-01 03:00:54 -08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-28 10:12:50 -08:00
|
|
|
fn addUncounted(l: *Link) void {
|
|
|
|
|
if (uncounted_full) return;
|
|
|
|
|
if (uncounted.count() > map.count()/8) {
|
|
|
|
|
uncounted.clearAndFree();
|
|
|
|
|
uncounted_full = true;
|
|
|
|
|
} else
|
|
|
|
|
(uncounted.getOrPut(l) catch unreachable).key_ptr.* = l;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-28 00:29:15 -08:00
|
|
|
// Add/remove this inode from the parent Dir sizes. When removing stats,
|
|
|
|
|
// the list of *Links and their sizes and counts must be in the exact same
|
|
|
|
|
// state as when the stats were added. Hence, any modification to the Link
|
|
|
|
|
// state should be preceded by a setStats(.., false).
|
2024-07-17 04:04:17 -08:00
|
|
|
fn setStats(l: *Link, add: bool) void {
|
|
|
|
|
if (l.pack.counted == add) return;
|
2021-07-28 00:29:15 -08:00
|
|
|
|
|
|
|
|
var nlink: u31 = 0;
|
2024-07-17 04:04:17 -08:00
|
|
|
var inconsistent = false;
|
2021-07-28 00:29:15 -08:00
|
|
|
var dirs = std.AutoHashMap(*Dir, u32).init(main.allocator);
|
|
|
|
|
defer dirs.deinit();
|
2024-07-17 04:04:17 -08:00
|
|
|
var it = l;
|
2021-07-28 00:29:15 -08:00
|
|
|
while (true) {
|
2024-07-17 04:04:17 -08:00
|
|
|
it.pack.counted = add;
|
2024-07-14 06:21:59 -08:00
|
|
|
nlink += 1;
|
2024-07-17 04:04:17 -08:00
|
|
|
if (it.pack.nlink != l.pack.nlink) inconsistent = true;
|
2024-07-14 06:21:59 -08:00
|
|
|
var parent: ?*Dir = it.parent;
|
|
|
|
|
while (parent) |p| : (parent = p.parent) {
|
|
|
|
|
const de = dirs.getOrPut(p) catch unreachable;
|
|
|
|
|
if (de.found_existing) de.value_ptr.* += 1
|
|
|
|
|
else de.value_ptr.* = 1;
|
2021-06-01 03:00:54 -08:00
|
|
|
}
|
2021-07-28 00:29:15 -08:00
|
|
|
it = it.next;
|
2024-07-17 04:04:17 -08:00
|
|
|
if (it == l)
|
2021-07-28 00:29:15 -08:00
|
|
|
break;
|
2021-06-01 03:00:54 -08:00
|
|
|
}
|
2021-07-06 08:28:35 -08:00
|
|
|
|
2024-07-17 04:04:17 -08:00
|
|
|
// There's not many sensible things we can do when we encounter
|
|
|
|
|
// inconsistent nlink counts. Current approach is to use the number of
|
|
|
|
|
// times we've seen this link in our tree as fallback for when the
|
|
|
|
|
// nlink counts aren't matching. May want to add a warning of some
|
|
|
|
|
// sorts to the UI at some point.
|
|
|
|
|
if (!inconsistent and l.pack.nlink >= nlink) nlink = l.pack.nlink;
|
|
|
|
|
|
|
|
|
|
// XXX: We're also not testing for inconsistent entry sizes, instead
|
|
|
|
|
// using the given 'l' size for all Links. Might warrant a warning as
|
|
|
|
|
// well.
|
2021-07-28 00:29:15 -08:00
|
|
|
|
|
|
|
|
var dir_iter = dirs.iterator();
|
|
|
|
|
if (add) {
|
|
|
|
|
while (dir_iter.next()) |de| {
|
2024-07-17 04:04:17 -08:00
|
|
|
de.key_ptr.*.entry.pack.blocks +|= l.entry.pack.blocks;
|
|
|
|
|
de.key_ptr.*.entry.size +|= l.entry.size;
|
2021-07-28 00:29:15 -08:00
|
|
|
if (de.value_ptr.* < nlink) {
|
2024-07-17 04:04:17 -08:00
|
|
|
de.key_ptr.*.shared_blocks +|= l.entry.pack.blocks;
|
|
|
|
|
de.key_ptr.*.shared_size +|= l.entry.size;
|
2021-07-28 00:29:15 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
while (dir_iter.next()) |de| {
|
2024-07-17 04:04:17 -08:00
|
|
|
de.key_ptr.*.entry.pack.blocks -|= l.entry.pack.blocks;
|
|
|
|
|
de.key_ptr.*.entry.size -|= l.entry.size;
|
2021-07-28 00:29:15 -08:00
|
|
|
if (de.value_ptr.* < nlink) {
|
2024-07-17 04:04:17 -08:00
|
|
|
de.key_ptr.*.shared_blocks -|= l.entry.pack.blocks;
|
|
|
|
|
de.key_ptr.*.shared_size -|= l.entry.size;
|
2021-07-28 00:29:15 -08:00
|
|
|
}
|
2021-07-06 08:28:35 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-28 00:29:15 -08:00
|
|
|
pub fn addAllStats() void {
|
2021-07-28 10:12:50 -08:00
|
|
|
if (uncounted_full) {
|
2024-07-17 04:04:17 -08:00
|
|
|
var it = map.keyIterator();
|
|
|
|
|
while (it.next()) |e| setStats(e.*, true);
|
2021-07-28 10:12:50 -08:00
|
|
|
} else {
|
2024-07-17 04:04:17 -08:00
|
|
|
var it = uncounted.keyIterator();
|
|
|
|
|
while (it.next()) |u| if (map.getKey(u.*)) |e| setStats(e, true);
|
2021-07-28 10:12:50 -08:00
|
|
|
}
|
|
|
|
|
uncounted_full = false;
|
|
|
|
|
if (uncounted.count() > 0)
|
|
|
|
|
uncounted.clearAndFree();
|
2021-07-06 08:28:35 -08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2021-07-28 00:29:15 -08:00
|
|
|
pub var root: *Dir = undefined;
|
|
|
|
|
|
|
|
|
|
|
2021-04-29 02:48:45 -08:00
|
|
|
test "entry" {
|
2024-07-12 02:31:57 -08:00
|
|
|
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
|
|
|
|
defer arena.deinit();
|
|
|
|
|
var e = Entry.create(arena.allocator(), .file, false, "hello");
|
2022-11-02 02:28:43 -08:00
|
|
|
try std.testing.expectEqual(e.pack.etype, .file);
|
|
|
|
|
try std.testing.expect(!e.pack.isext);
|
2022-08-08 08:23:45 -08:00
|
|
|
try std.testing.expectEqualStrings(e.name(), "hello");
|
2021-04-29 02:48:45 -08:00
|
|
|
}
|