airminer96

ln

Mar 29th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.73 KB | None | 0 0
  1. -- ln created by airminer96
  2.  
  3. ------------
  4. -- Fields --
  5. ------------
  6.  
  7. -- The original value of os.unloadAPI
  8. local unloadRaw
  9.  
  10. ---------------
  11. -- Internals --
  12. ---------------
  13.  
  14. -- Checks the fist 8 bytes of the file to see if it's a link
  15. local function isRawLink(path, handle)
  16.   hb = handle.open(path,"rb")
  17.   if handle.exists(path) and not handle.isDir(path) then
  18.     s = {}
  19.     for i = 1, 7 do
  20.       b = hb.read()
  21.       if b then
  22.         s[i] = b
  23.       end
  24.     end
  25.     hb.close()
  26.     if string.char(unpack(s)) == "--link:" then
  27.       return true
  28.     end
  29.   end
  30.   return false
  31. end
  32.  
  33. -- Quickfix for infinite looping
  34. local loop = 0
  35.  
  36. -- Resolves a path
  37. local function resolve(path, handle)
  38.   loop = loop + 1
  39.   rp = fs.combine("",path)
  40.   p = rp
  41.   while (p ~= "") and not handle.exists(p) do
  42.     p = p:sub(1,-1*fs.getName(p):len()-2)
  43.   end
  44.   if isRawLink(p, handle) and loop < 100 then
  45.     h = handle.open(p,"r")
  46.     l = h.readLine():sub(8)
  47.     h.close()
  48.     if l:sub(1,1) ~= "/" then
  49.       l = fs.combine(p:sub(1,-1*fs.getName(p):len()-2),l)
  50.     end
  51.     rp = resolve(fs.combine(l,rp:sub(p:len() + 1)),handle)
  52.   end
  53.   loop = 0
  54.   return rp
  55. end
  56.  
  57. -- Lua shenanigans
  58. isLink = nil
  59.  
  60. -- The handler passed to fsPlugins.registerHandler
  61. local function handler(path, handle, action)
  62.   if action == "delete" and isLink(path) then
  63.     return fs.combine(resolve(path:sub(1,-1*fs.getName(path):len()-2),handle),fs.getName(path)),handle
  64.   else
  65.     return resolve(path,handle),handle;
  66.   end
  67. end
  68.  
  69. -- The hook which overrides os.unloadAPI
  70. local function unload(name)
  71.   if name == "fsPlugins" or name == "ln" then
  72.     -- Remove handler
  73.     fsPlugins.removeHandler(handler)
  74.   end
  75.   if (name == "fsPlugins" or name == "ln" or ln == nil) and os.unloadAPI == unload then
  76.     -- Restore os.unloadAPI
  77.     os.unloadAPI = unloadRaw
  78.   end
  79.   unloadRaw(name)
  80. end
  81.  
  82. ----------------
  83. -- Public API --
  84. ----------------
  85.  
  86. -- Checks if the sepcified path is a link (resolves all parent links). A handle can be optionally specified
  87. isLink = function(path, handle)
  88.   if handle then
  89.     return isRawLink(fs.combine(resolve(path:sub(1,-1*fs.getName(path):len()-2),handle),fs.getName(path)),handle)
  90.   else
  91.     if fsPlugins then
  92.       fsPlugins.removeHandler(handler)
  93.     end
  94.     ret = isRawLink(fs.combine(resolve(path:sub(1,-1*fs.getName(path):len()-2),fs),fs.getName(path)),fs)
  95.     if fsPlugins then
  96.       fsPlugins.registerHandler(handler)
  97.     end
  98.     return ret
  99.   end
  100. end
  101.  
  102. --Creates a link file <name> pointing at the path <target>
  103. function createLink(target, name)
  104.   h = fs.open(name,"w")
  105.   h.write("--link:"..target)
  106.   h.close()
  107. end  
  108.  
  109. -- os.loadAPI sequence
  110. if shell == nil then
  111.   if ln then
  112.     -- Unload API if already loaded
  113.     os.unloadAPI("ln")
  114.   end
  115.   if not fsPlugins then
  116.     -- Try to load fsPlugins API if not already loaded
  117.     os.loadAPI("fsPlugins")
  118.   end
  119.   if fsPlugins then
  120.     -- Register handler
  121.     fsPlugins.registerHandler(handler)
  122.     -- Override os.unloadAPI
  123.     unloadRaw = os.unloadAPI
  124.     os.unloadAPI = unload
  125.   else
  126.     if term.isColor() then
  127.       term.setTextColor(colors.red)
  128.     end
  129.     print("fsPlugins API not found, driver will not be loaded")
  130.   end
  131. -- Shell program sequence
  132. else
  133.   tArgs = {...}
  134.   if tArgs[1] == "load" then
  135.     -- Load API
  136.     os.loadAPI(shell.getRunningProgram())
  137.   elseif tArgs[1] == "unload" then
  138.     -- Unload API
  139.     os.unloadAPI("ln")
  140.   elseif tArgs[1] == "-s" and #tArgs >= 2 then
  141.     if tArgs[3] == nil then
  142.       tArgs[3] = fs.getName(shell.resolve(tArgs[2]))
  143.     end
  144.     -- Create symlink
  145.     createLink(tArgs[2],shell.resolve(tArgs[3]))
  146.   else
  147.     -- Print Usages
  148.     print("Usages:\nln help\nln load\nln unload\nln -s <target> [name]")
  149.   end
  150. end
Advertisement
Add Comment
Please, Sign In to add comment