aboutsummaryrefslogtreecommitdiff
path: root/mirai.old/utils/fs/location.zig
blob: a0a75add59afdfb9d342c7be7e7bf61d2814d263 (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
//! Location utilities

const afs = @import("../../fs/afs/afs.zig");
const ahci = @import("../../drivers/ahci/ahci.zig");
const compare = @import("../string/compare.zig");
const kata_mod = @import("../../kata/kata.zig");
const fs_limits = @import("../../common/limits/fs.zig");

const DEVICE_PREFIX = "/system/devices/";

pub fn resolve_to_cluster(
    fs: *afs.AFS(ahci.BlockDevice),
    location: []const u8,
    current_cluster: u64,
) ?u32 {
    const current: u32 = if (current_cluster == 0) fs.root_cluster else @as(u32, @intCast(current_cluster));

    if (location.len == 0) return current;

    if (location.len == 1 and location[0] == '^') {
        return fs.get_parent_cluster(current) orelse fs.root_cluster;
    }

    var cluster: u32 = undefined;
    var start: usize = 0;

    if (location[0] == '/') {
        cluster = fs.root_cluster;
        start = 1;
    } else {
        cluster = current;
    }

    if (start >= location.len) return cluster;

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

        if (is_slash or is_end) {
            if (i > start) {
                const component = location[start..i];

                if (compare.equals(component, "^")) {
                    cluster = fs.get_parent_cluster(cluster) orelse fs.root_cluster;
                } else {
                    const entry = fs.find_entry(cluster, component) orelse return null;
                    if (!entry.is_stack()) return null;
                    cluster = entry.first_cluster;
                }
            }
            start = i + 1;
        }
    }

    return cluster;
}

pub const Canonical = struct {
    buf: [fs_limits.MAX_LOCATION_LENGTH]u8,
    len: usize,

    pub fn slice(self: *const Canonical) []const u8 {
        return self.buf[0..self.len];
    }
};

pub fn canonicalize(current: []const u8, location: []const u8) Canonical {
    var result: Canonical = .{ .buf = undefined, .len = 0 };

    if (location.len == 1 and location[0] == '^') {
        @memcpy(result.buf[0..current.len], current);
        result.len = current.len;
        strip_last(&result);
        return result;
    }

    if (location.len > 0 and location[0] == '/') {
        result.buf[0] = '/';
        result.len = 1;
        process_components(&result, location[1..]);
        return result;
    }

    @memcpy(result.buf[0..current.len], current);
    result.len = current.len;
    process_components(&result, location);

    return result;
}

fn strip_last(result: *Canonical) void {
    if (result.len > 1) {
        result.len -= 1;
        while (result.len > 1 and result.buf[result.len - 1] != '/') {
            result.len -= 1;
        }
        if (result.len > 1) result.len -= 1;
    }
    if (result.len == 0) {
        result.buf[0] = '/';
        result.len = 1;
    }
}

fn process_components(result: *Canonical, location: []const u8) void {
    var i: usize = 0;
    while (i < location.len) {
        while (i < location.len and location[i] == '/') : (i += 1) {}
        if (i >= location.len) break;

        const start = i;
        while (i < location.len and location[i] != '/') : (i += 1) {}
        const component = location[start..i];

        if (compare.equals(component, "^")) {
            strip_last(result);
        } else {
            if (result.len > 1) {
                result.buf[result.len] = '/';
                result.len += 1;
            }
            for (component) |c| {
                if (result.len < fs_limits.MAX_LOCATION_LENGTH) {
                    result.buf[result.len] = c;
                    result.len += 1;
                }
            }
        }
    }
}

pub fn resolve(kata: *kata_mod.Kata, location: []const u8, buffer: []u8) []const u8 {
    if (location.len > 0 and location[0] == '/') {
        @memcpy(buffer[0..location.len], location);
        return buffer[0..location.len];
    }

    const cwd = kata.current_location[0..kata.current_location_len];
    var len: usize = cwd.len;

    @memcpy(buffer[0..cwd.len], cwd);

    if (cwd[cwd.len - 1] != '/') {
        buffer[len] = '/';
        len += 1;
    }

    @memcpy(buffer[len .. len + location.len], location);
    len += location.len;

    return buffer[0..len];
}

pub fn is_device(location: []const u8) bool {
    return compare.starts_with(location, DEVICE_PREFIX);
}

pub fn device_name(location: []const u8) []const u8 {
    return location[DEVICE_PREFIX.len..];
}