aboutsummaryrefslogtreecommitdiff
path: root/src/lib/vfs.zig
blob: 56834d66c7861baf3d962c9d9df813efbaade718 (plain) (blame)
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
189
190
191
192
// SPDX-FileCopyrightText: 2024 Himbeer <himbeer@disroot.org>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

const std = @import("std");

const sysexchange = @import("sysexchange.zig");

const mem = std.mem;

var root: Tree = undefined;

pub const Error = error{
    NotFound,
    RelativePathNotAllowed,
    NotADirectory,
    NoAbsoluteContainingDirectory,
    TooManyReferences,
};

// A stream is a resource that provides a shared data stream with a driver.
pub const Stream = struct {
    readFn: ?ReadFn,
    writeFn: ?WriteFn,

    pub const ReadFn = *const fn (buffer: []u8) sysexchange.Result(usize);
    pub const WriteFn = *const fn (bytes: []const u8) sysexchange.Result(usize);
};

// A file is a resource that creates a unique data stream with a driver.
pub const File = struct {
    openFn: OpenFn,
    readFn: ?ReadFn,
    writeFn: ?WriteFn,
    closeFn: ?CloseFn,

    pub const OpenFn = *allowzero const fn (pid: u16) sysexchange.Result(*anyopaque);
    pub const ReadFn = *const fn (context: *anyopaque, buffer: []u8) sysexchange.Result(usize);
    pub const WriteFn = *const fn (context: *anyopaque, bytes: []const u8) sysexchange.Result(usize);
    pub const CloseFn = *const fn (context: *anyopaque) void;
};

// A hook is a resource that invokes raw driver callbacks when interacted with.
pub const Hook = struct {
    callback: Callback,

    pub const Callback = *allowzero const fn (pid: u16, data: usize) sysexchange.Result(usize);
};

pub const MkdirOptions = packed struct(usize) {
    full: u1,
    reserved: u63,
};

// A directory hook is a resource that provides other resources via driver callbacks.
pub const DirHook = struct {
    provideFn: ProvideFn,
    findFn: FindFn,
    removeFn: RemoveFn,

    pub const ProvideFn = *allowzero const fn (inode: Inode) sysexchange.Result(void);
    pub const FindFn = *allowzero const fn (name: []const u8) ?Inode;
    pub const RemoveFn = *allowzero const fn (name: []const u8) sysexchange.Result(void);
};

pub const Resource = union(enum) {
    stream: Stream,
    file: File,
    hook: Hook,
    dir: Tree,
    dir_hook: DirHook,
};

pub const Inode = struct {
    name: []const u8,
    resource: Resource,
    refs: usize = 0,
};

pub const Node = std.DoublyLinkedList(Inode).Node;

pub const Tree = struct {
    allocator: mem.Allocator,
    nodes: std.DoublyLinkedList(Inode),

    pub fn init(allocator: mem.Allocator) Tree {
        return .{
            .allocator = allocator,
            .nodes = std.DoublyLinkedList(Inode){},
        };
    }

    pub fn find(self: *const Tree, name: []const u8) ?*Node {
        var node = self.nodes.first;
        while (node) |current_node| {
            if (current_node.next == current_node) break;

            if (mem.eql(u8, current_node.data.name, name)) return current_node;
            node = current_node.next;
        }
        return null;
    }

    pub fn provideResource(self: *Tree, inode: Inode) !void {
        var node = try self.allocator.create(Node);
        node.data = inode;
        self.nodes.append(node);
    }

    pub fn remove(self: *Tree, name: []const u8) !void {
        if (self.find(name)) |node| {
            self.nodes.remove(node);
        } else return Error.NotFound;
    }
};

pub const ResourceDescriptor = struct {
    inode: *Inode,

    pub fn init(inode: *Inode) !ResourceDescriptor {
        inode.refs = std.math.add(usize, inode.refs, 1) catch return Error.TooManyReferences;
        return .{
            .inode = inode,
        };
    }

    pub fn deinit(self: ResourceDescriptor) void {
        self.inode.refs -|= 1;
    }
};

pub const UserInfo = union(enum) {
    rd: ResourceDescriptor,
    value: sysexchange.Result(usize),
};

pub fn init(allocator: mem.Allocator) void {
    root = Tree.init(allocator);
}

pub fn find(path: []const u8) ?*Node {
    if (!std.fs.path.isAbsolutePosix(path)) return null;

    // Error set is empty.
    var it = std.fs.path.ComponentIterator(.posix, u8).init(path) catch unreachable;
    var tree = root;
    while (it.next()) |component| {
        const node = tree.find(component.name) orelse return null;

        if (mem.eql(u8, component.path, (it.last() orelse return null).path)) {
            return node;
        }
        switch (node.data.resource) {
            .dir => |dir| tree = dir,
            else => {},
        }
    }

    // Only reached if path has no components.
    return null;
}

pub fn provideResource(path: []const u8, resource: Resource) !void {
    if (!std.fs.path.isAbsolutePosix(path)) return Error.RelativePathNotAllowed;

    const dirname = std.fs.path.dirnamePosix(path) orelse return Error.NoAbsoluteContainingDirectory;
    if (find(dirname)) |node| {
        return switch (node.data.resource) {
            .dir => |dir| @constCast(&dir).provideResource(.{
                .name = std.fs.path.basenamePosix(path),
                .resource = resource,
            }),
            else => Error.NotADirectory,
        };
    } else return Error.NotFound;
}

pub fn provideResourceZ(path_c: [*:0]const u8, resource: Resource) !void {
    return provideResource(mem.sliceTo(path_c, 0), resource);
}

pub fn open(path: []const u8, pid: u16, data: usize) !UserInfo {
    const node = find(path) orelse return Error.NotFound;
    return switch (node.data.resource) {
        .hook => |hook| .{ .value = hook.callback(pid, data) },
        else => .{ .rd = try ResourceDescriptor.init(&node.data) },
    };
}

pub fn openZ(path_c: [*:0]const u8, pid: u16, data: usize) !UserInfo {
    return open(mem.sliceTo(path_c, 0), pid, data);
}