Files
HyperionOS/Src/Hyperion-kernel/data/lib/modules/hyperion/12_tmpfs.kmod
T

358 lines
9.1 KiB
Plaintext

--:Minify:--
local kernel = ...
local os = os
if not kernel.config.tmpToDisk then
local proxy = {}
local data = {}
local timestamps = {}
local now = function() return kernel.EFI:getEpochMs() end
local function touch(path, created)
local time = now()
timestamps[path] = timestamps[path] or {
created = created or time,
modified = time,
}
timestamps[path].modified = time
end
local function getTimestamp(path)
if not timestamps[path] then
local time = now()
timestamps[path] = {
created = time,
modified = time,
}
end
return timestamps[path]
end
proxy.address = "tmpfs0000"
proxy.isvirt = true
proxy.isReadOnly = function()
return false
end
proxy.spaceUsed = function()
local function size(step)
local total = 0
for _, value in pairs(step) do
if type(value) == "string" then
total = total + #value
elseif type(value) == "table" then
total = total + size(value)
end
end
return total
end
return size(data)
end
proxy.spaceTotal = function()
return math.huge
end
proxy.makeDirectory = function(_, path)
local steps = kernel.vfs.splitPath(path)
local step = data
local current = ""
for i = 1, #steps do
current = current == "" and steps[i]
or current .. "/" .. steps[i]
if not step[steps[i]] then
step[steps[i]] = {}
touch(current)
elseif type(step[steps[i]]) ~= "table" then
error("ENOTDIR")
end
step = step[steps[i]]
end
end
proxy.remove = function(_, path)
local steps = kernel.vfs.splitPath(path)
if #steps == 0 then
error("EACCES")
end
local step = data
local current = ""
for i = 1, #steps - 1 do
current = current == "" and steps[i]
or current .. "/" .. steps[i]
step = step[steps[i]]
if not step then
error("ENOENT")
end
if type(step) ~= "table" then
error("ENOTDIR")
end
end
local name = steps[#steps]
if step[name] == nil then
error("ENOENT")
end
local function clearTimestamps(value, pathPrefix)
timestamps[pathPrefix] = nil
if type(value) == "table" then
for key, child in pairs(value) do
clearTimestamps(child, pathPrefix .. "/" .. key)
end
end
end
clearTimestamps(step[name], path)
step[name] = nil
end
proxy.setLabel = function(_, label)
end
proxy.getLabel = function()
return "tmpfs"
end
proxy.attributes = function(_, path)
local steps = kernel.vfs.splitPath(path)
local step = data
local current = ""
for i = 1, #steps do
current = current == "" and steps[i]
or current .. "/" .. steps[i]
step = step[steps[i]]
if step == nil then
error("ENOENT")
end
end
local time = getTimestamp(current)
return {
size = type(step) == "string" and #step or 0,
modified = time.modified,
created = time.created,
}
end
function proxy:open(path, mode)
local steps = kernel.vfs.splitPath(path)
if #steps == 0 then
error("EISDIR")
end
local step = data
local current = ""
for i = 1, #steps - 1 do
current = current == "" and steps[i]
or current .. "/" .. steps[i]
if not step[steps[i]] then
if mode == "w" then
step[steps[i]] = {}
touch(current)
else
error("ENOENT")
end
elseif type(step[steps[i]]) ~= "table" then
error("ENOTDIR")
end
step = step[steps[i]]
end
local filename = steps[#steps]
local filepath = current == "" and filename
or current .. "/" .. filename
if mode == "r" then
if type(step[filename]) ~= "string" then
if type(step[filename]) == "table" then
error("EISDIR")
end
error("ENOENT")
end
local content = step[filename]
local pos = 1
return {
read = function(amount)
amount = amount or #content
local chunk = content:sub(pos, pos + amount - 1)
pos = pos + #chunk
if chunk == "" then
return nil
end
return chunk
end,
close = function()
end,
}
elseif mode == "w" then
if type(step[filename]) == "table" then
error("EISDIR")
end
local buf = {}
local closed = false
return {
write = function(str)
if closed then
error("EBADF")
end
buf[#buf + 1] = tostring(str)
end,
close = function()
if closed then
return
end
closed = true
local existed = type(step[filename]) == "string"
step[filename] = table.concat(buf)
if not existed then
local time = now()
timestamps[filepath] = {
created = time,
modified = time,
}
else
touch(filepath)
end
end,
}
elseif mode == "a" then
if type(step[filename]) == "table" then
error("EISDIR")
end
local buf = {
type(step[filename]) == "string" and step[filename] or ""
}
local closed = false
return {
write = function(str)
if closed then
error("EBADF")
end
buf[#buf + 1] = tostring(str)
end,
close = function()
if closed then
return
end
closed = true
local existed = type(step[filename]) == "string"
step[filename] = table.concat(buf)
if not existed then
local time = now()
timestamps[filepath] = {
created = time,
modified = time,
}
else
touch(filepath)
end
end,
}
else
error("EACCES")
end
end
function proxy:type(path)
local steps = kernel.vfs.splitPath(path)
local step = data
if #steps == 0 then
return "directory"
end
for i = 1, #steps do
if type(step) ~= "table" then
return false
end
step = step[steps[i]]
if step == nil then
return false
end
end
if type(step) == "table" then
return "directory"
end
if type(step) == "string" then
return "file"
end
end
function proxy:list(path)
local steps = kernel.vfs.splitPath(path)
local step = data
local current = ""
for i = 1, #steps do
current = current == "" and steps[i]
or current .. "/" .. steps[i]
if type(step) ~= "table" then
error("ENOTDIR")
end
step = step[steps[i]]
if step == nil then
error("ENOENT")
end
end
if type(step) ~= "table" then
error("ENOTDIR")
end
local keys = {}
for k in pairs(step) do
keys[#keys + 1] = k
end
return keys
end
function proxy:fileExists(path)
local t = self:type(path)
return t == "file" or t == "directory"
end
kernel.disks["tmpfs0000"] = proxy
kernel.tmpfs={data=data,proxy=proxy}
else
kernel.log("tmpToDisk enabled, skipping tmpfs module")
kernel.log("tmpfs0000 will passthrough to /tmp on the disk")
kernel.runWhenLoaded(function()
kernel.vfs.remove("/tmp")
kernel.vfs.mkdir("/tmp")
end)
end