Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vdrives = {} --The global table for mounted images
- invalidNames = { --this is basically a compatibility hack for other programs
- ".",
- "",
- }
- oldFS = {
- open = fs.open,
- delete = fs.delete,
- makeDir = fs.makeDir,
- list = fs.list,
- exists = fs.exists,
- isDir = fs.isDir,
- getDrive = fs.getDrive,
- move = fs.move,
- copy = fs.copy,
- isReadOnly = fs.isReadOnly,
- combine = fs.combine,
- getDir = fs.getDir,
- getSize = fs.getSize,
- }
- fs.isReadOnly = function(path)
- if isVirtual(file) then -- is virtual
- for dname,drive in pairs(vdrives) do
- for fname,file in pairs(drive.files) do
- name = "//" .. dname .. "/" .. fname
- if name == path then
- return file.isReadOnly
- end
- end
- name = "//" .. dname
- if name == path then
- return false
- end
- end
- error("File doesn't exist")
- else
- return oldFS.isReadOnly(path)
- end
- end
- fs.open = function(file, mode)
- if isVirtual(file) then -- is virtual
- local a = string.find(file:sub(3, -1), "/")
- if fs.exists(file) == false or mode:sub(1, 1) == "w" then
- vdrives[file:sub(3, a + 1)].files[file:sub(a + 3, -1)] = {
- data = '',
- isReadOnly = false,
- isDir = false
- }
- end
- fobj = { position = 0 }
- fobj.data = vdrives[file:sub(3, a + 1)].files[file:sub(a + 3, -1)].data --doesn't work when writing
- if string.sub(mode, 1, 1) == "r" then
- if string.sub(mode, 2, 2) == "b" then
- fobj.read = function()
- fobj.position = fobj.position + 1
- local x = #fobj.data < realPos and nil or tonumber(fobj.data:sub(fobj.position, fobj.position + 1), 16)
- fobj.position = fobj.position + 2
- return x
- end
- else
- fobj.readLine = function()
- if fobj.position > #fobj.data then return nil end
- local line = ""
- fobj.position = fobj.position + 1
- for i = fobj.position, #fobj.data, 2 do
- local nByte = tonumber(fobj.data:sub(i, i + 1), 16)
- if nByte == 10 then
- fobj.position = i + 1
- return line -- .. "\n"
- else
- line = line .. string.char(nByte)
- end
- fobj.position = fobj.position + 2
- end
- fobj.position = fobj.position + 2
- return line
- end
- fobj.readAll = function()
- if fobj.position > #fobj.data then return nil end
- local text = ""
- fobj.position = fobj.position + 1
- for i = fobj.position, #fobj.data, 2 do
- local nByte = tonumber(fobj.data:sub(i, i + 1), 16)
- text = text .. string.char(nByte)
- end
- fobj.position = #fobj.data + 1
- return text
- end
- fobj.readRaw = function()
- return fobj.data
- end
- end
- elseif (string.sub(mode, 1, 1) == "w") or (string.sub(mode, 1, 1) == "a") then
- if string.sub(mode, 2, 2) == "b" then
- fobj.write = function(b)
- vdrives[file:sub(3, a + 1)].files[file:sub(a + 3, #file)].data = vdrives[file:sub(3, a + 1)].files[file:sub(a + 3, #file)].data .. string.format('%02x', b)
- end
- fobj.writeRaw = function(w)
- vdrives[file:sub(3, a + 1)].files[file:sub(a + 3, #file)].data = w
- end
- else
- fobj.write = function(s)
- for i = 1, #s do
- vdrives[string.sub(file, 3, a + 1)].files[string.sub(file, a + 3, #file)].data = vdrives[string.sub(file, 3, a + 1)].files[string.sub(file, a + 3, #file)].data .. string.format('%02x', string.byte(string.sub(s, i, i)))
- end
- end
- fobj.writeLine = function(s)
- fobj.write(s .. '\n')
- end
- end
- end
- fobj.close = function()
- --nothing needs to be done, object just needs to be cleared
- fobj = nil
- end
- return fobj
- else -- is real
- return oldFS.open(file, mode)
- end
- end
- fs.isDir = function(path)
- if isVirtual(path) then
- for dname,drive in pairs(vdrives) do
- name = "//" .. dname
- name2 = "//" .. dname .. "/"
- if name == path or name2 == path then
- return true
- end
- for fname,file in pairs(drive.files) do
- name = "//" .. dname .. "/" .. fname
- if name == path then
- return file.isDir
- end
- dirname = "//" .. dname .. "/" .. fname .. "/"
- if dirname == path then
- return file.isDir
- end
- end
- end
- error("File doesn't exist")
- else
- return oldFS.isDir(path)
- end
- end
- fs.exists = function(path)
- if isVirtual(path) then
- for dname,drive in pairs(vdrives) do
- for fname,file in pairs(drive.files) do
- name = "//" .. dname .. "/" .. fname
- if name == path then
- return true
- end
- end
- name = "//" .. dname
- name2 = "//" .. dname .. "/"
- if name == path or name2 == path then
- return true
- end
- end
- return false
- else
- return oldFS.exists(path)
- end
- end
- fs.combine = function(basePath, localPath)
- if isVirtual(localPath) then
- return localPath
- elseif isVirtual(basePath) then
- return basePath .. "/" .. localPath
- else
- return oldFS.combine(basePath, localPath)
- end
- end
- fs.list = function(path)
- if isVirtual(path) then
- tList = {}
- for dname,drive in pairs(vdrives) do
- for fname,file in pairs(drive.files) do
- name = "//" .. dname .. "/" .. fname
- local a, b = string.find(name, path)
- if string.find(name, path) ~= nil then
- if string.find(string.sub(name, b + 2, -1), "/") == nil and string.sub(name, b + 2, -1) ~= nil and string.gsub(string.sub(name, b + 2, -1), " ", "") ~= "" then --lots of methods to prevent invalid entries
- table.insert(tList, string.sub(name, b + 2, -1))
- end
- end
- end
- end
- return tList
- else
- return oldFS.list(path)
- end
- end
- fs.delete = function(path)
- if isVirtual(path) then
- if fs.isReadOnly(path) == false then
- if fs.isDir(path) then
- local tList = fs.list(path)
- if #tList > 0 then
- for i = 1, #tList do
- if tList[i] ~= "" and tList[i] ~= nil then
- fs.delete(path .. "/" .. fs.getName(tList[i]))
- end
- end
- end
- end
- local tryDelete = function()
- vdrives[getDriveName(path)].files[getFileName(path)] = nil
- if fs.exists(path) then
- error("Error deleting " .. path)
- else
- return true
- end
- end
- ok = pcall(tryDelete)
- if ok == false then
- print("Error deleting " .. path)
- end
- else
- error("Path is read-only")
- end
- else
- return oldFS.delete(path)
- end
- end
- fs.makeDir = function(path)
- if isVirtual(path) then -- is virtual
- vdrives[getDriveName(path)].files[getFileName(path)] = {
- data = '',
- isReadOnly = false,
- isDir = true
- }
- else
- return oldFS.makeDir(path)
- end
- end
- fs.getDrive = function(path)
- if isVirtual(path) then -- is virtual
- return "vfs"
- else
- return oldFS.getDrive(path)
- end
- end
- fs.copy = function(fromPath, toPath)
- if isVirtual(fromPath) or isVirtual(toPath) then
- if fs.exists(fs.getDir(toPath)) == false then
- fs.makeDir(fs.getDir(toPath))
- end
- if fs.isDir(fromPath) then
- local tList = fs.list(fromPath)
- if #tList > 0 then
- for i = 1, #tList do
- if tList[i] ~= "" and tList[i] ~= nil then
- fs.copy(fromPath .. "/" .. fs.getName(tList[i]), toPath .. "/" .. fs.getName(tList[i]))
- end
- end
- end
- if fs.exists(toPath) == false then
- fs.makeDir(toPath)
- end
- else
- if fs.exists(toPath) then
- error("File already exists")
- elseif fs.exists(fromPath) == false then
- error("File does not exist")
- end
- if isVirtual(fromPath) and isVirtual(toPath) == false then --copying from memory to disk
- local f = fs.open(toPath, "wb")
- local r = fs.open(fromPath, 'rb')
- for b in r.read() do
- f.write(b)
- end
- f.close()
- elseif isVirtual(fromPath) == false and isVirtual(toPath) then --copying from disk to memory
- vdrives[getDriveName(toPath)].files[getFileName(toPath)] = {
- data = '',
- isReadOnly = false,
- isDir = false
- }
- local f = fs.open(toPath, 'wb')
- local r = fs.open(fromPath, "rb")
- for b in r.read() do
- f.write(b)
- end
- f.close()
- else --copying from memory to memory
- vdrives[getDriveName(toPath)].files[getFileName(toPath)] = {
- data = '',
- isReadOnly = false,
- isDir = false
- }
- vdrives[getDriveName(toPath)].files[getFileName(toPath)].data = vdrives[getDriveName(fromPath)].files[getFileName(fromPath)].data
- end
- end
- else
- return oldFS.copy(fromPath, toPath) --copying from disk to disk
- end
- end
- fs.move = function(fromPath, toPath)
- fs.copy(fromPath, toPath)
- fs.delete(fromPath)
- end
- fs.getDir = function(path)
- if isVirtual(path) then
- return "//" .. oldFS.getDir(path)
- else
- return oldFS.getDir(path)
- end
- end
- mount = function(image) --add image to vdrives
- if isValidName(image.name) == false then return end
- vdrives[image.name] = image
- end
- unmount = function(name) --remove image from vdrives
- if isValidName(name) == false then return end
- vdrives[name] = nil
- end
- getMountedImage = function(name) --returns a loaded image
- return vdrives[name]
- end
- listMountedImages = function() --returns a table of names of mounted images
- tList = {}
- for dname, drive in pairs(vdrives) do
- table.insert(tList, dname)
- end
- return tList
- end
- createImage = function(_name) --returns a blank image
- return {
- name = _name,
- files = {}
- }
- end
- exportImage = function(name) --hdd to image
- if isValidName(name) == false then return end
- image = createImage(name)
- local i = 0
- local wildcard = ""
- repeat
- i = 0
- wildcard = wildcard .. "*/"
- t = fs.find(wildcard)
- for k = 1, #t do
- local a, b = string.find(t[k], "rom")
- if a ~= 1 then
- i = i + 1
- image.files[t[k]] = {
- data = '',
- isDir = false,
- isReadOnly = false
- }
- image.files[t[k]].isReadOnly = fs.isReadOnly(t[k])
- image.files[t[k]].isDir = fs.isDir(t[k])
- if fs.isDir(t[k]) == false then
- f = fs.open(t[k], "rb")
- s = 0
- repeat
- s = f.read()
- if s ~= nil then
- image.files[t[k]].data = image.files[t[k]].data .. string.format('%02x', s)
- end
- until s == nil
- end
- end
- end
- until i == 0
- return image
- end
- importImage = function(image, append) --replace hdd with image
- if not append then format() end
- for fname, file in ipairs(image.files) do
- if file.isDir then
- fs.makeDir(fname)
- else
- local f = fs.open(fname, "wb")
- for i = 1, #file.data, 2 do
- local nByte = tonumber(fobj.data:sub(i, i + 1), 16)
- f.write(nByte)
- end
- end
- end
- end
- format = function() --delete everything on the HDD
- shell.run("rm *")
- --[[ local i = 0
- local wildcard = ""
- repeat
- i = 0
- wildcard = wildcard .. "*/"
- t = fs.find(wildcard)
- for k = 1, #t do
- i = i + 1
- local a, b = string.find(t[k], "rom")
- if a ~= 1 then
- local tryDelete = function()
- fs.delete(path)
- if fs.exists(path) then
- error("Error deleting " .. path)
- else
- return true
- end
- end
- ok = pcall(tryDelete)
- if ok == false then
- print("Error deleting " .. path)
- end
- end
- end
- until i == 0 ]]-- legacy method incase rm * breaks
- end
- isVirtual = function(path)
- --legacy methods
- --return string.find(file, "//") ~= nil - caused error on using rom programs
- --return string.sub(path, 2, 2) == "/" - will allow invalid paths/names
- if path == nil or isValidName(path) == false then return false end
- for dname,drive in pairs(vdrives) do
- --explicitly exists
- for fname,file in pairs(drive.files) do
- local name = "//" .. dname .. "/" .. fname
- if name == path then
- return true
- end
- end
- local name = "//" .. dname
- local name2 = "//" .. dname .. "/"
- if name == path or name2 == path then
- return true
- end
- --can exist
- if string.sub(path, 3, #name) == dname then
- return isValidName(string.sub(path, #name + 2, #path))
- end
- end
- return false
- end
- isValidName = function(name)
- for i = 1, #invalidNames do
- if invalidNames[i] == name then
- return false
- end
- end
- return true
- end
- getDriveName = function(path)
- local f = string.sub(path, 3, #path)
- local pos = string.find(f, "/")
- return string.sub(f, 1, pos - 1)
- end
- getFileName = function(path)
- local f = string.sub(path, 3, #path)
- local pos = string.find(f, "/")
- return string.sub(f, pos + 1, #path)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement