2019-11-08 12:42:49 +11:00
|
|
|
do
|
2018-11-03 03:05:41 +11:00
|
|
|
fs = {}
|
2019-11-08 12:42:49 +11:00
|
|
|
local fsmounts = {}
|
2018-11-03 03:05:41 +11:00
|
|
|
|
|
|
|
-- basics
|
2020-05-12 17:55:05 +10:00
|
|
|
function fs.segments(path) -- string -- table -- Splits *path* on each /
|
2018-11-03 03:05:41 +11:00
|
|
|
local segments = {}
|
|
|
|
for segment in path:gmatch("[^/]+") do
|
|
|
|
segments[#segments+1] = segment
|
|
|
|
end
|
|
|
|
return segments
|
|
|
|
end
|
2020-05-12 17:55:05 +10:00
|
|
|
function fs.resolve(path) -- string -- string string -- Resolves *path* to a specific filesystem mount and path
|
2019-01-02 16:40:55 +11: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
|
2019-11-08 12:42:49 +11:00
|
|
|
local segments, rpath, rfs= fs.segments(path)
|
|
|
|
local rc = #segments
|
|
|
|
for i = #segments, 1, -1 do
|
|
|
|
if fsmounts[table.concat(segments, "/", 1, i)] ~= nil then
|
|
|
|
return table.concat(segments, "/", 1, i), table.concat(segments, "/", i+1)
|
|
|
|
end
|
2018-11-03 03:05:41 +11:00
|
|
|
end
|
2019-11-08 12:42:49 +11:00
|
|
|
return "/", table.concat(segments,"/")
|
2018-11-03 03:05:41 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
-- generate some simple functions
|
2019-11-08 21:01:01 +11:00
|
|
|
for k,v in pairs({"makeDirectory","exists","isDirectory","list","lastModified","remove","size","spaceUsed","spaceTotal","isReadOnly","getLabel"}) do
|
2018-11-03 03:05:41 +11:00
|
|
|
fs[v] = function(path)
|
|
|
|
local fsi,path = fs.resolve(path)
|
2019-11-08 12:42:49 +11:00
|
|
|
return fsmounts[fsi][v](path)
|
2018-11-03 03:05:41 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function fread(self,length)
|
2019-11-08 12:42:49 +11:00
|
|
|
return fsmounts[self.fs].read(self.fid,length)
|
2018-11-03 03:05:41 +11:00
|
|
|
end
|
|
|
|
local function fwrite(self,data)
|
2020-03-16 17:30:22 +11:00
|
|
|
return fsmounts[self.fs].write(self.fid,data)
|
2018-11-03 03:05:41 +11:00
|
|
|
end
|
2019-07-18 13:49:34 +10:00
|
|
|
local function fseek(self,dist)
|
2020-03-16 17:30:22 +11:00
|
|
|
return fsmounts[self.fs].seek(self.fid,dist)
|
2019-07-18 13:49:34 +10:00
|
|
|
end
|
2018-11-03 03:05:41 +11:00
|
|
|
local function fclose(self)
|
2020-03-16 17:30:22 +11:00
|
|
|
return fsmounts[self.fs].close(self.fid)
|
2018-11-03 03:05:41 +11:00
|
|
|
end
|
|
|
|
|
2020-05-12 17:55:05 +10:00
|
|
|
function fs.open(path,mode) -- string string -- table -- Opens file *path* with mode *mode*, returning a file object.
|
2018-11-03 03:05:41 +11:00
|
|
|
mode = mode or "rb"
|
|
|
|
local fsi,path = fs.resolve(path)
|
2019-11-08 12:42:49 +11:00
|
|
|
if not fsmounts[fsi] then return false end
|
|
|
|
local fid = fsmounts[fsi].open(path,mode)
|
2018-11-03 03:05:41 +11:00
|
|
|
if fid then
|
2019-07-18 13:49:34 +10:00
|
|
|
local fobj = {["fs"]=fsi,["fid"]=fid,["seek"]=fseek,["close"]=fclose}
|
2019-07-14 20:52:56 +10:00
|
|
|
if mode:find("r") then
|
2018-11-03 03:05:41 +11:00
|
|
|
fobj.read = fread
|
2019-07-14 20:52:56 +10:00
|
|
|
end
|
|
|
|
if mode:find("w") then
|
2018-11-03 03:05:41 +11:00
|
|
|
fobj.write = fwrite
|
|
|
|
end
|
|
|
|
return fobj
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2020-05-12 17:55:05 +10:00
|
|
|
function fs.copy(from,to) -- string string -- boolean -- copies a file from *from* to *to*
|
2018-11-03 03:05:41 +11:00
|
|
|
local of = fs.open(from,"rb")
|
|
|
|
local df = fs.open(to,"wb")
|
|
|
|
if not of or not df then
|
|
|
|
return false
|
|
|
|
end
|
2020-06-06 12:54:10 +10:00
|
|
|
local tmp
|
|
|
|
repeat
|
|
|
|
tmp = of:read(2048)
|
|
|
|
df:write(tmp or "")
|
|
|
|
until not tmp
|
2018-11-03 03:05:41 +11:00
|
|
|
df:close()
|
|
|
|
of:close()
|
2020-05-12 17:55:05 +10:00
|
|
|
return true
|
2018-11-03 03:05:41 +11:00
|
|
|
end
|
|
|
|
|
2020-05-12 17:55:05 +10:00
|
|
|
function fs.rename(from,to) -- string string -- boolean -- Moves file *from* to *to*
|
2018-11-03 03:05:41 +11:00
|
|
|
local ofsi, opath = fs.resolve(from)
|
|
|
|
local dfsi, dpath = fs.resolve(to)
|
|
|
|
if ofsi == dfsi then
|
2019-11-08 12:42:49 +11:00
|
|
|
fsmounts[ofsi].rename(opath,dpath)
|
2018-11-03 03:05:41 +11:00
|
|
|
return true
|
|
|
|
end
|
2020-05-12 17:55:05 +10:00
|
|
|
if not fs.copy(from,to) then return false end
|
|
|
|
if not fs.remove(from) then return false end
|
2018-11-03 03:05:41 +11:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2020-05-12 17:55:05 +10:00
|
|
|
function fs.mount(path,proxy) -- string table -- boolean -- Mounts the filesystem *proxy* to the mount point *path* if it is a directory. BYO proxy.
|
2019-11-09 15:56:25 +11:00
|
|
|
if fs.isDirectory(path) and not fsmounts[table.concat(fs.segments(path),"/")] then
|
2019-11-08 12:42:49 +11:00
|
|
|
fsmounts[table.concat(fs.segments(path),"/")] = proxy
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false, "path is not a directory"
|
|
|
|
end
|
2020-05-12 17:55:05 +10:00
|
|
|
function fs.umount(path) -- string -- -- Unmounts filesystem from *path*.
|
2019-11-09 15:56:25 +11:00
|
|
|
local fsi,_ = fs.resolve(path)
|
|
|
|
fsmounts[fsi] = nil
|
|
|
|
end
|
2018-11-03 03:05:41 +11:00
|
|
|
|
2020-05-12 17:55:05 +10:00
|
|
|
function fs.mounts() -- -- table -- Returns a table containing the mount points of all mounted filesystems
|
2019-11-08 21:01:01 +11:00
|
|
|
local rt = {}
|
|
|
|
for k,v in pairs(fsmounts) do
|
|
|
|
rt[#rt+1] = k,v.address or "unknown"
|
|
|
|
end
|
|
|
|
return rt
|
|
|
|
end
|
|
|
|
|
2020-05-12 17:55:05 +10:00
|
|
|
function fs.address(path) -- string -- string -- Returns the address of the filesystem at a given path, if applicable; do not expect a sensical response
|
2019-11-08 21:01:01 +11:00
|
|
|
local fsi,_ = fs.resolve(path)
|
|
|
|
return fsmounts[fsi].address
|
|
|
|
end
|
2020-05-12 17:55:05 +10:00
|
|
|
function fs.type(path) -- string -- string -- Returns the component type of the filesystem at a given path, if applicable
|
2019-11-08 21:01:01 +11:00
|
|
|
local fsi,_ = fs.resolve(path)
|
2020-03-26 17:25:36 +11:00
|
|
|
return fsmounts[fsi].type or "filesystem"
|
2019-11-08 21:01:01 +11:00
|
|
|
end
|
|
|
|
|
2019-11-08 12:42:49 +11:00
|
|
|
fsmounts["/"] = component.proxy(computer.tmpAddress())
|
|
|
|
fs.makeDirectory("temp")
|
2018-11-03 03:05:41 +11:00
|
|
|
if computer.getBootAddress then
|
2019-11-08 12:42:49 +11:00
|
|
|
fs.makeDirectory("boot")
|
|
|
|
fs.mount("boot",component.proxy(computer.getBootAddress()))
|
2018-11-03 03:05:41 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|