Advertisement
awsumben13

Filesystem Redirection

Aug 26th, 2015
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.52 KB | None | 0 0
  1. local fs = fs
  2.  
  3. local function readpath( path )
  4.     if fs.isDir( path ) then
  5.         local t = {}
  6.         local files = fs.list( path )
  7.         for i = 1, #files do
  8.             t[files[i]] = readpath( path .. "/" .. files[i] )
  9.         end
  10.         return t
  11.     else
  12.         local h = fs.open( path, "r" )
  13.         if h then
  14.             local content = h.readAll()
  15.             h.close()
  16.             return content
  17.         end
  18.     end
  19. end
  20.  
  21. local function writepath( path, data )
  22.     if type( data ) == "table" then
  23.         fs.makeDir( path )
  24.         for k, v in pairs( data ) do
  25.             writepath( path .. "/" .. k, v )
  26.         end
  27.     elseif type( data ) == "string" then
  28.         local h = fs.open( path, "w" )
  29.         if h then
  30.             h.write( data )
  31.             h.close()
  32.         end
  33.     end
  34. end
  35.  
  36. function redirect( path )
  37.     path = fmtpath( path )
  38.     local device = {}
  39.  
  40.     local clones = { "combine", "getName", "getDir", "getDrive" }
  41.     for i = 1, #clones do
  42.         device[clones[i]] = fs[clones[i]]
  43.     end
  44.     local single = { "exists", "isDir", "makeDir", "delete", "list", "isReadOnly", "getFreeSpace", "getSize", "open" }
  45.     for i = 1, #single do
  46.         device[single[i]] = function( file, ... )
  47.             return fs[single[i]]( path .. "/" .. file, ... )
  48.         end
  49.     end
  50.  
  51.     function device.copy( p1, p2 )
  52.         if fs.exists( path .. "/" .. p2 ) then
  53.             return error "path exists"
  54.         end
  55.         writepath( path .. "/" .. p2, readpath( path .. "/" .. p1 ) )
  56.     end
  57.     function device.move( p1, p2 )
  58.         if fs.exists( path .. "/" .. p2 ) then
  59.             return error "path exists"
  60.         end
  61.         writepath( path .. "/" .. p2, readpath( path .. "/" .. p1 ) )
  62.         fs.delete( path .. "/" .. p1 )
  63.     end
  64.     return device
  65. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement