aboutsummaryrefslogtreecommitdiff
path: root/mirai.old/fs/afs/read.zig
blob: f14a8d3ce7b81732ab637fa39c2c1b86474b3a32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//! AFS read operations

const cache = @import("cache.zig");
const cluster = @import("cluster.zig");
const compare = @import("../../utils/string/compare.zig");
const copy = @import("../../utils/mem/copy.zig");
const fs = @import("../../common/constants/fs.zig");
const int = @import("../../utils/types/int.zig");
const location = @import("location.zig");
const ptr = @import("../../utils/types/ptr.zig");
const types = @import("types.zig");

pub fn find_entry(afs: anytype, stack_cluster: u32, identity: []const u8) ?types.Entry {
    var current = stack_cluster;

    while (cluster.is_valid(current)) {
        var sector: [fs.SECTOR_SIZE]u8 align(fs.SECTOR_ALIGN) = undefined;
        if (!afs.device.read_sector(cluster.to_lba(afs, current), &sector)) {
            return null;
        }

        const entry = ptr.of(types.Entry, @intFromPtr(&sector));

        if (entry.is_end()) return null;

        if (entry.is_unit() or entry.is_stack()) {
            if (compare.equals(entry.get_identity(), identity)) {
                if (entry.is_stack()) {
                    cache.store(afs, entry.first_cluster, stack_cluster);
                }
                return entry.*;
            }
        }

        current = cluster.get_next(afs, current) catch return null;
    }

    return null;
}

pub fn view_unit(afs: anytype, entry: types.Entry, buffer: []u8) !usize {
    var current = entry.first_cluster;
    var viewed: usize = 0;

    while (cluster.is_valid(current)) {
        var sector: [fs.SECTOR_SIZE]u8 align(fs.SECTOR_ALIGN) = undefined;
        if (!afs.device.read_sector(cluster.to_lba(afs, current), &sector)) {
            return error.ReadFailed;
        }

        const to_copy = @min(fs.SECTOR_SIZE, entry.size - viewed);
        const end = @min(viewed + to_copy, buffer.len);
        copy.bytes(buffer[viewed..end], sector[0 .. end - viewed]);

        viewed += to_copy;
        if (viewed >= entry.size) break;

        current = try cluster.get_next(afs, current);
    }

    return viewed;
}

pub fn list_stack(afs: anytype, stack_cluster: u32, items: []types.StackItem) !usize {
    var current = stack_cluster;
    var count: usize = 0;

    while (cluster.is_valid(current)) {
        var sector: [fs.SECTOR_SIZE]u8 align(fs.SECTOR_ALIGN) = undefined;
        if (!afs.device.read_sector(cluster.to_lba(afs, current), &sector)) {
            return error.ReadFailed;
        }

        const entry = ptr.of(types.Entry, @intFromPtr(&sector));

        if (entry.is_end()) return count;

        if (entry.is_unit() or entry.is_stack()) {
            if (count >= items.len) return count;

            copy.bytes(items[count].identity[0..entry.name_len], entry.get_identity());
            items[count].identity_len = entry.name_len;
            items[count].is_stack = entry.is_stack();

            copy.bytes(items[count].owner_name[0..entry.owner_name_len], entry.get_owner());
            items[count].owner_name_len = entry.owner_name_len;
            items[count].permission_type = entry.permission_type;
            items[count].modified_time = entry.modified_time;

            items[count].size = if (entry.is_stack())
                int.u32_of(calculate_stack_size(afs, entry.first_cluster) catch 0)
            else
                int.u32_of(entry.size);

            count += 1;
        }

        current = cluster.get_next(afs, current) catch return count;
    }

    return count;
}

pub fn calculate_stack_size(afs: anytype, stack_cluster: u32) !u64 {
    var current = stack_cluster;
    var total: u64 = 0;

    while (cluster.is_valid(current)) {
        var sector: [fs.SECTOR_SIZE]u8 align(fs.SECTOR_ALIGN) = undefined;
        if (!afs.device.read_sector(cluster.to_lba(afs, current), &sector)) {
            return error.ReadFailed;
        }

        const entry = ptr.of(types.Entry, @intFromPtr(&sector));

        if (entry.is_end()) return total;

        if (entry.is_unit()) {
            total += entry.size;
        } else if (entry.is_stack()) {
            total += calculate_stack_size(afs, entry.first_cluster) catch 0;
        }

        current = cluster.get_next(afs, current) catch return total;
    }

    return total;
}

pub fn get_unit_size(afs: anytype, loc: []const u8) !u64 {
    if (loc.len == 0) return error.InvalidLocation;

    var current = afs.root_cluster;
    var start: usize = location.skip_root(loc);
    var i: usize = start;

    while (i <= loc.len) : (i += 1) {
        const is_end = (i == loc.len);
        const is_slash = !is_end and loc[i] == '/';

        if (is_slash or is_end) {
            if (i > start) {
                const component = loc[start..i];
                const entry = find_entry(afs, current, component) orelse return error.NotFound;

                if (is_end) {
                    if (!entry.is_unit()) return error.NotAUnit;
                    return entry.size;
                } else {
                    if (!entry.is_stack()) return error.NotAStack;
                    current = entry.first_cluster;
                }
            }
            start = i + 1;
        }
    }

    return error.InvalidLocation;
}

pub fn view_unit_at(afs: anytype, loc: []const u8, buffer: []u8) !usize {
    var current = afs.root_cluster;
    var start: usize = location.skip_root(loc);
    var i: usize = start;

    while (i <= loc.len) : (i += 1) {
        const is_end = (i == loc.len);
        const is_slash = !is_end and loc[i] == '/';

        if (is_slash or is_end) {
            if (i > start) {
                const component = loc[start..i];
                const entry = find_entry(afs, current, component) orelse return error.NotFound;

                if (is_end) {
                    if (!entry.is_unit()) return error.NotAUnit;
                    return view_unit(afs, entry, buffer);
                } else {
                    if (!entry.is_stack()) return error.NotAStack;
                    current = entry.first_cluster;
                }
            }
            start = i + 1;
        }
    }

    return error.InvalidLocation;
}