Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Downloads files from a computer to a disk
- --Put in startup.lua for the disk
- --Fetching all drives which can stores data
- local function getMounts(oldMounts)
- if not peripheral.find("drive") then error("No connected disk drive") end
- local drives = {peripheral.find("drive",function(a,b) return b.hasData end)}
- local mounts = {}
- local freeSpace,maxSize = 0,0
- for idx,drive in ipairs(drives) do
- mounts[idx] = drive.getMountPath()
- freeSpace = freeSpace + fs.getFreeSpace(mounts[idx])
- maxSize = math.max(maxSize,fs.getCapacity(mounts[idx]))
- if type(oldMounts) == "table" then
- local isOldMount = false
- for k,v in pairs(oldMounts) do
- if v == mounts[idx] then
- isOldMount = true
- end
- end
- if not isOldMount then fs.delete(fs.combine(mounts[idx],""..os.getComputerID())) end
- end
- end
- return mounts,freeSpace,maxSize
- end
- local _,__,maxSize = getMounts()
- local function getFilesToCopy(dir,paths)
- local size = 0
- if type(paths) ~= "table" then paths = {} end
- if type(dir) ~= "string" or not fs.isDir(dir) then dir = "/" end
- for k,v in pairs(fs.list(dir)) do
- local path = fs.combine(dir,v)
- if fs.getDrive(path) == "hdd" then
- if fs.isDir(path) then
- local _,dirSize = getFilesToCopy(path,paths)
- size = size + dirSize
- elseif fs.getSize(path) < maxSize then
- table.insert(paths,path)
- size = size + fs.getSize(path)
- end
- end
- end
- return paths,size
- end
- local function copyFiles(files,mounts)
- if type(mounts) ~= "table" or #mounts <= 0 then return #files <= 0,files end
- local mount = table.remove(mounts)
- while #files > 0 do
- local file = table.remove(files)
- if fs.getFreeSpace(mount) < fs.getSize(file) then
- table.insert(files,file)
- return copyFiles(files,mounts)
- else
- fs.copy(file,fs.combine(mount,""..os.getComputerID(),file))
- end
- end
- return true,files
- end
- local success = false
- local mounts,freeSpace = getMounts()
- local files,size = getFilesToCopy()
- local numFiles = #files
- repeat
- success,files = copyFiles(files,mounts)
- if not success then
- term.clear()
- term.setCursorPos(1,1)
- print(#files,"files have yet to be downloaded. Please add disk space in order to download more files.")
- print("Press [space] in order to continue downloading, once you have added more storage.")
- local key
- repeat
- _,key = os.pullEvent("key")
- until key == keys.space
- mounts,freeSpace = getMounts(mounts)
- end
- until success
- print(numFiles,"files were downloaded!")
- print(size,"bytes of data was transferred")
Advertisement
Add Comment
Please, Sign In to add comment