Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ln created by airminer96
- ------------
- -- Fields --
- ------------
- -- The original value of os.unloadAPI
- local unloadRaw
- ---------------
- -- Internals --
- ---------------
- -- Checks the fist 8 bytes of the file to see if it's a link
- local function isRawLink(path, handle)
- hb = handle.open(path,"rb")
- if handle.exists(path) and not handle.isDir(path) then
- s = {}
- for i = 1, 7 do
- b = hb.read()
- if b then
- s[i] = b
- end
- end
- hb.close()
- if string.char(unpack(s)) == "--link:" then
- return true
- end
- end
- return false
- end
- -- Quickfix for infinite looping
- local loop = 0
- -- Resolves a path
- local function resolve(path, handle)
- loop = loop + 1
- rp = fs.combine("",path)
- p = rp
- while (p ~= "") and not handle.exists(p) do
- p = p:sub(1,-1*fs.getName(p):len()-2)
- end
- if isRawLink(p, handle) and loop < 100 then
- h = handle.open(p,"r")
- l = h.readLine():sub(8)
- h.close()
- if l:sub(1,1) ~= "/" then
- l = fs.combine(p:sub(1,-1*fs.getName(p):len()-2),l)
- end
- rp = resolve(fs.combine(l,rp:sub(p:len() + 1)),handle)
- end
- loop = 0
- return rp
- end
- -- Lua shenanigans
- isLink = nil
- -- The handler passed to fsPlugins.registerHandler
- local function handler(path, handle, action)
- if action == "delete" and isLink(path) then
- return fs.combine(resolve(path:sub(1,-1*fs.getName(path):len()-2),handle),fs.getName(path)),handle
- else
- return resolve(path,handle),handle;
- end
- end
- -- The hook which overrides os.unloadAPI
- local function unload(name)
- if name == "fsPlugins" or name == "ln" then
- -- Remove handler
- fsPlugins.removeHandler(handler)
- end
- if (name == "fsPlugins" or name == "ln" or ln == nil) and os.unloadAPI == unload then
- -- Restore os.unloadAPI
- os.unloadAPI = unloadRaw
- end
- unloadRaw(name)
- end
- ----------------
- -- Public API --
- ----------------
- -- Checks if the sepcified path is a link (resolves all parent links). A handle can be optionally specified
- isLink = function(path, handle)
- if handle then
- return isRawLink(fs.combine(resolve(path:sub(1,-1*fs.getName(path):len()-2),handle),fs.getName(path)),handle)
- else
- if fsPlugins then
- fsPlugins.removeHandler(handler)
- end
- ret = isRawLink(fs.combine(resolve(path:sub(1,-1*fs.getName(path):len()-2),fs),fs.getName(path)),fs)
- if fsPlugins then
- fsPlugins.registerHandler(handler)
- end
- return ret
- end
- end
- --Creates a link file <name> pointing at the path <target>
- function createLink(target, name)
- h = fs.open(name,"w")
- h.write("--link:"..target)
- h.close()
- end
- -- os.loadAPI sequence
- if shell == nil then
- if ln then
- -- Unload API if already loaded
- os.unloadAPI("ln")
- end
- if not fsPlugins then
- -- Try to load fsPlugins API if not already loaded
- os.loadAPI("fsPlugins")
- end
- if fsPlugins then
- -- Register handler
- fsPlugins.registerHandler(handler)
- -- Override os.unloadAPI
- unloadRaw = os.unloadAPI
- os.unloadAPI = unload
- else
- if term.isColor() then
- term.setTextColor(colors.red)
- end
- print("fsPlugins API not found, driver will not be loaded")
- end
- -- Shell program sequence
- else
- tArgs = {...}
- if tArgs[1] == "load" then
- -- Load API
- os.loadAPI(shell.getRunningProgram())
- elseif tArgs[1] == "unload" then
- -- Unload API
- os.unloadAPI("ln")
- elseif tArgs[1] == "-s" and #tArgs >= 2 then
- if tArgs[3] == nil then
- tArgs[3] = fs.getName(shell.resolve(tArgs[2]))
- end
- -- Create symlink
- createLink(tArgs[2],shell.resolve(tArgs[3]))
- else
- -- Print Usages
- print("Usages:\nln help\nln load\nln unload\nln -s <target> [name]")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment