Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- [WARNING] This is RAID 0 ! --
- -- so if one drive fails. Everything is gone
- local DEBUG = true
- local ADEBUG =false -- I would not recommend enabling this :)
- local DEBUG_FILE = "raid.debug"
- local index_disk = "disk/"
- local index_file = "index.raid"
- local MAX_FILES = 10
- -- Main Code --
- local index_path = index_disk..index_file
- local raid = {}
- local total = 0
- local disk_space = 4000000
- local totalSpace = 0
- local totalFreeSpace =0
- index = {} -- Huh
- local d = 0
- function debug(str)
- local fd = fs.open(DEBUG_FILE,"a")
- fd.write(str)
- fd.close()
- end
- function updateArray()
- for i,v in ipairs(peripheral.getNames()) do
- if (peripheral.getType(v)=="drive") then
- local t=true
- for y,u in ipairs(raid) do
- if (peripheral.wrap(v).getDiskID()==u.id or index_disk==peripheral.wrap(v).getMountPath) then
- t = false
- end
- end
- if (t and peripheral.wrap(v).isDiskPresent()) then
- --BUG: Ignore empty disk drives!
- d=d+1
- raid[d] = {wrap=nil,freespace=0,id=0,mountPath=nil}
- raid[d].wrap=peripheral.wrap(v)
- raid[d].mountPath = raid[d].wrap.getMountPath()
- if (raid[d].wrap.getMountPath()==nil) then
- -- BUGFIX: Remove the empty disk
- raid[d]={wrap=nil,freespace=0,id=0,mountPath=nil}
- if (DEBUG) then print("[DBG] Removed empty disk drive from array ("..d..")") end
- d = d - 1
- else
- raid[d].freespace=fs.getFreeSpace(raid[d].mountPath) --peripheral.call(v,"getMounthPath"))
- totalFreeSpace = totalFreeSpace + raid[d].freespace
- raid[d].id = raid[d].wrap.getDiskID()
- totalSpace = totalSpace + disk_space
- end
- end
- end
- end
- total =d
- if (DEBUG) then print("[DBG] Added "..d.." to array, "..totalSpace..":"..totalFreeSpace) end
- return d
- end
- --[[ INDEX ]]--
- function INDEX_INIT()
- -- Prepare index array and file
- for i=1,MAX_FILES do
- index[i] = {filename=nil,size=0,checksum=0,parts={}}
- if (ADEBUG) then print("[ADBG] Init for "..i) end
- end
- end
- -- Read index file and writes to mem
- function INDEX_READ()
- -- Reads index file and stores it in mem
- local f = fs.open(index_file,"r")
- if (f~=nil) then
- local tdata = f.readAll()
- if (ADEBUG) then debug(tdata) end
- if (tdata~=nil) then
- index = textutils.unserialise(tdata)
- end
- f.close()
- return true
- else
- if (DEBUG) then print("[DBG] No index file found!") end
- return false
- end
- end
- -- Updates the index file
- function INDEX_WRITE()
- f = fs.open(index_file,"w")
- local tdata = textutils.serialise(index)
- if (tdata~=nil) then
- if (ADEBUG) then debug(tdata) end
- f.write(tdata)
- f.close()
- end
- end
- -- Checks with checksum for dataloss
- function INDEX_CHECKSUM()
- end
- --[[ FILESYSTEM ]]--
- local function DISK_GETFROMID(id)
- for i,v in ipairs(raid) do
- if (v.id == id) then
- return v
- end
- end
- end
- local function FILE_WRITE(disk,filename,data,size,n)
- if ADEBUG then debug("[DBG] Disk: "..disk.id.."> "..data) end
- local flnm = disk.mountPath.."/"..filename.."&"..size.."&"..tostring(n)
- f = fs.open(flnm,"w")
- if (f~=nil) then
- f.write(data)
- else
- return nil
- end
- f.close()
- end
- local function FILE_READ(disk,filename,size,n)
- if (disk==nil) then
- if (DEBUG) then print("[DBG] Error: Data loss detected!") end
- return nil
- end
- local flnm = disk.mountPath.."/"..filename.."&"..size.."&"..tostring(n)
- -- ERROR: tostring(n) = nil in some!!!!
- local f = fs.open(flnm,"r")
- if (f==nil) then return nil end
- local data = f.readAll()
- f.close()
- return data
- end
- -- Adds a file
- function FILE_ADD(filename,data)
- -- First find out if the file already exists
- for i,v in ipairs(index) do
- if (v.filename==filename) then
- return false
- end
- end
- -- Calculate amount of size per DISK
- local size = data:len()
- if (type(data)=="string") then
- if (size>total) then
- -- So we calculate the split size
- local s = math.floor((size/total))
- local ls = size-(s*(total-1)) --Size for the last disk
- -- Add in index
- for y,v in ipairs(index) do
- if (v.filename==nil) then
- -- Found a place. Now add everything in index
- v.size=size
- -- Split the data
- local t =1
- local p = 1
- for i=1,total-1 do
- tdata = data:sub(t,t+s-1)
- t=t+s
- -- Write it
- -- TODO: Add check if successfull?
- FILE_WRITE(raid[i],filename,tdata,s,p)
- v.parts[p]={id=raid[i].id,part=p}
- p = p + 1
- end
- tdata=data:sub(t,size)
- v.parts[total]={id=raid[total].id,part=p}
- FILE_WRITE(raid[total],filename,tdata,ls,p)
- -- Checksum?
- v.filename=filename
- return true
- end
- end
- end
- end
- return false
- end
- function FILE_GET(filename)
- -- First check if file exists
- for i,v in ipairs(index) do
- if (v.filename==filename) then
- -- File found, Now get all parts!
- if (DEBUG) then print("[DBG][FILE_GET] File found!") end
- local tdata = ""
- local size = v.size
- local s = math.floor((size/total))-- Calculate size
- local ls = size-(s*(total-1))
- local p =1
- if (DEBUG) then print("[DBG][FILE_GET] s: "..s..", ls:"..ls) end
- for y,z in ipairs(v.parts) do
- --BugFix: Remove last one
- if (y~=#v.parts) then
- local id = DISK_GETFROMID(z.id)
- local tread = FILE_READ(id,filename,s,z.part)
- if (tread~=nil) then
- tdata = tdata..tread
- end
- else
- local id = DISK_GETFROMID(v.parts[#v.parts].id)
- local tread = FILE_READ(id,filename,ls,p)
- if (tread~=nil) then
- tdata = tdata..tread
- end
- return tdata
- end
- p=p+1
- end
- print("[FATAL] Arrays not matching!")
- end
- end
- print("[ERROR] File not found")
- return nil
- end
- function FILE_REM(filename)
- end
- -- Initialize array
- function init()
- print("Init:")
- print(INDEX_INIT())
- print("Index_Read: ")
- print(INDEX_READ())
- print("Index Array: ")
- updateArray()
- FILE_ADD("Test","Hello world. Isn't it a nice day? And A GIANT Array? Oh yea I know this is a lot of text. But I need it for my tests. Since I need to go over 200 characters :O IKR?! Yea. So... How are you? I'm good. Just lot's of text ;)")
- FILE_ADD("Second_file","RANDOMDATA WE NEED TO FILL THIS WITH A LOT OF SHIT. UHM... bla bla bla bla bla bla bla bla bla bla bla bla oh.. right. This isn't enough data to store (split iot over all disks..)")
- print("=========FILE_GET=========")
- print(FILE_GET("Test"))
- print(FILE_GET("Second_file"))
- print("==========================")
- --totalSpace,totalSpace-totalFreeSpace
- INDEX_WRITE()
- end
- init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement