aboutsummaryrefslogtreecommitdiff
path: root/mirai.old/utils/kata/attachment.zig
blob: 592b2c83790e7a5ceb4a5186d1a7f0c0e0a86d44 (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
//! Attachment utilities

const afs = @import("../../fs/afs/afs.zig");
const ahci = @import("../../drivers/ahci/ahci.zig");
const attachment = @import("../../kata/attachment.zig");
const heap = @import("../../memory/heap.zig");
const kata_limits = @import("../../common/limits/kata.zig");
const kata_mod = @import("../../kata/kata.zig");
const location_util = @import("../fs/location.zig");

pub fn allocate(kata: *kata_mod.Kata) !u32 {
    var i: u32 = 3;
    while (i < kata_limits.MAX_ATTACHMENTS) : (i += 1) {
        if (kata.attachments[i] == null) {
            return i;
        }
    }
    return error.TooManyAttachments;
}

pub fn seal(kata: *kata_mod.Kata, fd: u32, fs: ?*afs.AFS(ahci.BlockDevice)) void {
    const entry = kata.attachments[fd] orelse return;

    if (entry.attachment_type == .Unit and entry.dirty) {
        if (entry.buffer) |buffer| {
            if (fs) |filesystem| {
                var full_location_buf: [512]u8 = undefined;
                const full_location = location_util.resolve(kata, entry.location[0..entry.location_len], &full_location_buf);
                filesystem.mark_unit(full_location, buffer) catch {};
            }
            heap.free(@ptrCast(buffer.ptr), buffer.len);
        }
    }

    attachment.free(entry);
    kata.attachments[fd] = null;
}