t8rt0t

filegrabber.lua

May 14th, 2024 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.87 KB | None | 0 0
  1. --Downloads files from a computer to a disk
  2. --Put in startup.lua for the disk
  3. --Fetching all drives which can stores data
  4. local function getMounts(oldMounts)
  5.     if not peripheral.find("drive") then error("No connected disk drive") end
  6.     local drives = {peripheral.find("drive",function(a,b) return b.hasData end)}
  7.     local mounts = {}
  8.     local freeSpace,maxSize = 0,0
  9.     for idx,drive in ipairs(drives) do
  10.         mounts[idx] = drive.getMountPath()
  11.         freeSpace = freeSpace + fs.getFreeSpace(mounts[idx])
  12.         maxSize = math.max(maxSize,fs.getCapacity(mounts[idx]))
  13.         if type(oldMounts) == "table" then
  14.             local isOldMount = false
  15.             for k,v in pairs(oldMounts) do
  16.                 if v == mounts[idx] then
  17.                     isOldMount = true
  18.                 end
  19.             end
  20.             if not isOldMount then fs.delete(fs.combine(mounts[idx],""..os.getComputerID())) end
  21.         end
  22.     end
  23.     return mounts,freeSpace,maxSize
  24. end
  25.  
  26. local _,__,maxSize = getMounts()
  27.  
  28. local function getFilesToCopy(dir,paths)
  29.     local size = 0
  30.     if type(paths) ~= "table" then paths = {} end
  31.     if type(dir) ~= "string" or not fs.isDir(dir) then dir = "/" end
  32.     for k,v in pairs(fs.list(dir)) do
  33.         local path = fs.combine(dir,v)
  34.         if fs.getDrive(path) == "hdd" then
  35.             if fs.isDir(path) then
  36.                 local _,dirSize = getFilesToCopy(path,paths)
  37.                 size = size + dirSize
  38.             elseif fs.getSize(path) < maxSize then
  39.                 table.insert(paths,path)
  40.                 size = size + fs.getSize(path)
  41.             end
  42.         end
  43.     end
  44.     return paths,size
  45. end
  46.  
  47. local function copyFiles(files,mounts)
  48.     if type(mounts) ~= "table" or #mounts <= 0 then return #files <= 0,files end
  49.     local mount = table.remove(mounts)
  50.     while #files > 0 do
  51.         local file = table.remove(files)
  52.         if fs.getFreeSpace(mount) < fs.getSize(file) then
  53.             table.insert(files,file)
  54.             return copyFiles(files,mounts)
  55.         else
  56.             fs.copy(file,fs.combine(mount,""..os.getComputerID(),file))
  57.         end
  58.     end
  59.     return true,files
  60. end
  61.  
  62. local success = false
  63. local mounts,freeSpace = getMounts()
  64. local files,size = getFilesToCopy()
  65. local numFiles = #files
  66.  
  67. repeat
  68.     success,files = copyFiles(files,mounts)
  69.     if not success then
  70.         term.clear()
  71.         term.setCursorPos(1,1)
  72.         print(#files,"files have yet to be downloaded. Please add disk space in order to download more files.")
  73.         print("Press [space] in order to continue downloading, once you have added more storage.")
  74.         local key
  75.         repeat
  76.             _,key = os.pullEvent("key")
  77.         until key == keys.space
  78.         mounts,freeSpace = getMounts(mounts)
  79.     end
  80. until success
  81.  
  82. print(numFiles,"files were downloaded!")
  83. print(size,"bytes of data was transferred")
Advertisement
Add Comment
Please, Sign In to add comment