1
1
mirror of https://git.shadowkat.net/izaya/OC-PsychOS2.git synced 2024-11-14 08:50:00 -05:00
OC-PsychOS2/module/fs.lua

124 lines
2.9 KiB
Lua
Raw Normal View History

2018-11-02 12:05:41 -04:00
fs = {}
fs.mounts = {}
-- basics
2019-01-08 02:00:11 -05:00
function fs.segments(path) -- splits *path* on each /
2018-11-02 12:05:41 -04:00
local segments = {}
for segment in path:gmatch("[^/]+") do
segments[#segments+1] = segment
end
return segments
end
2019-01-08 02:00:11 -05:00
function fs.resolve(path) -- resolves *path* to a specific filesystem mount and path
2019-01-02 00:40:55 -05:00
if not path or path == "." then path = os.getenv("PWD") end
if path:sub(1,1) ~= "/" then path=(os.getenv("PWD") or "").."/"..path end
2018-11-02 12:05:41 -04:00
local segments, rpath = fs.segments(path), "/"
for i = 2, #segments do
rpath = rpath .. segments[i] .. "/"
end
rpath = rpath:match("(.+)/") or rpath
return segments[1] or "root",rpath
end
-- generate some simple functions
for k,v in pairs({"makeDirectory","exists","isDirectory","list","lastModified","remove","size","spaceUsed","isReadOnly","getLabel"}) do
fs[v] = function(path)
local fsi,path = fs.resolve(path)
return fs.mounts[fsi][v](path)
end
end
local function fread(self,length)
if length == "*a" then
length = math.huge
end
2019-07-14 06:52:56 -04:00
if type(length) == "number" then
local rstr, lstr = "", ""
repeat
lstr = fs.mounts[self.fs].read(self.fid,math.min(2^16,length-rstr:len())) or ""
rstr = rstr .. lstr
until rstr:len() == length or lstr == ""
return rstr
end
return fs.mounts[self.fs].read(self.fid,length)
2018-11-02 12:05:41 -04:00
end
local function fwrite(self,data)
fs.mounts[self.fs].write(self.fid,data)
end
local function fseek(self,dist)
fs.mounts[self.fs].seek(self.fid,dist)
end
2018-11-02 12:05:41 -04:00
local function fclose(self)
fs.mounts[self.fs].close(self.fid)
end
2019-01-08 02:00:11 -05:00
function fs.open(path,mode) -- opens file *path* with mode *mode*
2018-11-02 12:05:41 -04:00
mode = mode or "rb"
local fsi,path = fs.resolve(path)
if not fs.mounts[fsi] then return false end
local fid = fs.mounts[fsi].open(path,mode)
if fid then
local fobj = {["fs"]=fsi,["fid"]=fid,["seek"]=fseek,["close"]=fclose}
2019-07-14 06:52:56 -04:00
if mode:find("r") then
2018-11-02 12:05:41 -04:00
fobj.read = fread
2019-07-14 06:52:56 -04:00
end
if mode:find("w") then
2018-11-02 12:05:41 -04:00
fobj.write = fwrite
end
return fobj
end
return false
end
2019-01-08 02:00:11 -05:00
function fs.copy(from,to) -- copies a file from *from* to *to*
2018-11-02 12:05:41 -04:00
local of = fs.open(from,"rb")
local df = fs.open(to,"wb")
if not of or not df then
return false
end
df:write(of:read("*a"))
df:close()
of:close()
end
2019-01-08 02:00:11 -05:00
function fs.rename(from,to) -- moves file *from* to *to*
2018-11-02 12:05:41 -04:00
local ofsi, opath = fs.resolve(from)
local dfsi, dpath = fs.resolve(to)
if ofsi == dfsi then
fs.mounts[ofsi].rename(opath,dpath)
return true
end
fs.copy(from,to)
fs.remove(from)
return true
end
fs.mounts.temp = component.proxy(computer.tmpAddress())
if computer.getBootAddress then
fs.mounts.boot = component.proxy(computer.getBootAddress())
end
for addr, _ in component.list("filesystem") do
fs.mounts[addr:sub(1,3)] = component.proxy(addr)
end
local function rf()
return false
end
fs.mounts.root = {}
for k,v in pairs(fs.mounts.temp) do
fs.mounts.root[k] = rf
end
function fs.mounts.root.list()
local t = {}
for k,v in pairs(fs.mounts) do
t[#t+1] = k
end
t.n = #t
return t
end
function fs.mounts.root.isReadOnly()
return true
end