aboutsummaryrefslogtreecommitdiff
path: root/src/lib/vfs.zig
blob: abc2f8cf34d37d1deb4d5ae0dd23f7c18b46ca68 (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
// SPDX-FileCopyrightText: 2024 Himbeer <himbeer@disroot.org>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

const std = @import("std");

var root = undefined;

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

// A stream is a resource that provides a shared data stream with a driver.
pub const Stream = struct {};

// A file is a resource that creates a unique data stream with a driver.
pub const File = struct {};

// A hook is a resource that invokes raw driver callbacks when interacted with.
pub const Hook = struct {};

// A directory hook is a resource that provides other resources via driver callbacks.
pub const DirHook = struct {};

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

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

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

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

    pub fn init(allocator: std.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 orelse return null;
        while (node) |current_node| {
            if (current_node.next == current_node) break;

            if (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 NotFound;
    }
};

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

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

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

        if (component == it.last()) return node;
        if (node.data.resource == .dir) |dir| {
            tree = dir;
        }
    }

    // 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);
    if (find(dirname)) |node| {
        if (node.data.resource == .dir) |dir| {
            return dir.provideResource(.{
                .name = std.fs.path.basenamePosix(path),
                .resource = resource,
            });
        } else return Error.NotADirectory;
    } else return Error.NotFound;
}