Advertisement
nagoL2015

Raid

Apr 11th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.91 KB | None | 0 0
  1. -- [WARNING] This is RAID 0 ! --
  2. -- so if one drive fails. Everything is gone
  3. local DEBUG = true
  4. local ADEBUG =false -- I would not recommend enabling this :)
  5. local DEBUG_FILE = "raid.debug"
  6. local index_disk = "disk/"
  7. local index_file = "index.raid"
  8. local MAX_FILES = 10
  9.  
  10. -- Main Code --
  11. local index_path = index_disk..index_file
  12. local raid = {}
  13. local total = 0
  14. local disk_space = 4000000
  15. local totalSpace = 0
  16. local totalFreeSpace =0
  17. index = {} -- Huh
  18. local d = 0
  19.  
  20. function debug(str)
  21.   local fd = fs.open(DEBUG_FILE,"a")
  22.   fd.write(str)
  23.   fd.close()
  24. end
  25.  
  26. function updateArray()
  27.   for i,v in ipairs(peripheral.getNames()) do
  28.     if (peripheral.getType(v)=="drive") then
  29.       local t=true
  30.       for y,u in ipairs(raid) do
  31.         if (peripheral.wrap(v).getDiskID()==u.id or index_disk==peripheral.wrap(v).getMountPath) then
  32.           t = false
  33.         end
  34.       end
  35.       if (t and peripheral.wrap(v).isDiskPresent()) then
  36.         --BUG: Ignore empty disk drives!
  37.         d=d+1
  38.         raid[d] = {wrap=nil,freespace=0,id=0,mountPath=nil}
  39.         raid[d].wrap=peripheral.wrap(v)
  40.         raid[d].mountPath = raid[d].wrap.getMountPath()
  41.         if (raid[d].wrap.getMountPath()==nil) then
  42.           -- BUGFIX: Remove the empty disk
  43.           raid[d]={wrap=nil,freespace=0,id=0,mountPath=nil}
  44.           if (DEBUG) then print("[DBG] Removed empty disk drive from array ("..d..")") end
  45.           d = d - 1
  46.         else
  47.           raid[d].freespace=fs.getFreeSpace(raid[d].mountPath) --peripheral.call(v,"getMounthPath"))
  48.           totalFreeSpace = totalFreeSpace + raid[d].freespace
  49.           raid[d].id = raid[d].wrap.getDiskID()
  50.           totalSpace = totalSpace + disk_space
  51.         end
  52.       end
  53.     end
  54.   end
  55.   total =d
  56.   if (DEBUG) then print("[DBG] Added "..d.." to array, "..totalSpace..":"..totalFreeSpace) end
  57.   return d
  58. end
  59.  
  60. --[[ INDEX ]]--
  61. function INDEX_INIT()
  62.   -- Prepare index array and file
  63.   for i=1,MAX_FILES do
  64.     index[i] = {filename=nil,size=0,checksum=0,parts={}}
  65.     if (ADEBUG) then print("[ADBG] Init for "..i) end
  66.   end
  67. end
  68.  
  69. -- Read index file and writes to mem
  70. function INDEX_READ()
  71.   -- Reads index file and stores it in mem
  72.   local f = fs.open(index_file,"r")
  73.   if (f~=nil) then
  74.     local tdata = f.readAll()
  75.     if (ADEBUG) then debug(tdata) end
  76.       if (tdata~=nil) then
  77.         index = textutils.unserialise(tdata)
  78.       end
  79.       f.close()
  80.       return true
  81.     else
  82.     if (DEBUG) then print("[DBG] No index file found!") end
  83.     return false
  84.   end
  85. end
  86.  
  87. -- Updates the index file
  88. function INDEX_WRITE()
  89.   f = fs.open(index_file,"w")
  90.   local tdata = textutils.serialise(index)
  91.   if (tdata~=nil) then
  92.     if (ADEBUG) then debug(tdata) end
  93.     f.write(tdata)
  94.     f.close()
  95.   end
  96. end
  97.  
  98. -- Checks with checksum for dataloss
  99. function INDEX_CHECKSUM()
  100.  
  101. end
  102.  
  103. --[[ FILESYSTEM ]]--
  104. local function DISK_GETFROMID(id)
  105.   for i,v in ipairs(raid) do
  106.     if (v.id == id) then
  107.       return v
  108.     end
  109.   end
  110. end
  111.  
  112. local function FILE_WRITE(disk,filename,data,size,n)
  113.   if ADEBUG then debug("[DBG] Disk: "..disk.id.."> "..data) end
  114.   local flnm = disk.mountPath.."/"..filename.."&"..size.."&"..tostring(n)
  115.   f = fs.open(flnm,"w")
  116.   if (f~=nil) then
  117.     f.write(data)
  118.   else
  119.     return nil
  120.   end
  121.   f.close()
  122. end
  123.  
  124. local function FILE_READ(disk,filename,size,n)
  125.   if (disk==nil) then
  126.     if (DEBUG) then print("[DBG] Error: Data loss detected!") end
  127.     return nil
  128.   end
  129.   local flnm = disk.mountPath.."/"..filename.."&"..size.."&"..tostring(n)
  130.   -- ERROR: tostring(n) = nil in some!!!!
  131.   local f = fs.open(flnm,"r")
  132.   if (f==nil) then return nil end
  133.   local data = f.readAll()
  134.   f.close()
  135.   return data
  136. end
  137.  
  138. -- Adds a file
  139. function FILE_ADD(filename,data)
  140.   -- First find out if the file already exists
  141.   for i,v in ipairs(index) do
  142.     if (v.filename==filename) then
  143.       return false
  144.     end
  145.   end
  146.   -- Calculate amount of size per DISK
  147.   local size = data:len()
  148.   if (type(data)=="string") then
  149.     if (size>total) then
  150.       -- So we calculate the split size
  151.       local s = math.floor((size/total))
  152.       local ls = size-(s*(total-1)) --Size for the last disk
  153.       -- Add in index
  154.       for y,v in ipairs(index) do
  155.         if (v.filename==nil) then
  156.           -- Found a place. Now add everything in index
  157.           v.size=size
  158.           -- Split the data
  159.           local t =1
  160.           local p = 1
  161.           for i=1,total-1 do
  162.             tdata = data:sub(t,t+s-1)
  163.             t=t+s
  164.             -- Write it
  165.             -- TODO: Add check if successfull?
  166.             FILE_WRITE(raid[i],filename,tdata,s,p)
  167.             v.parts[p]={id=raid[i].id,part=p}
  168.             p = p + 1
  169.           end
  170.           tdata=data:sub(t,size)
  171.           v.parts[total]={id=raid[total].id,part=p}
  172.           FILE_WRITE(raid[total],filename,tdata,ls,p)
  173.           -- Checksum?
  174.           v.filename=filename
  175.           return true
  176.         end
  177.       end
  178.     end
  179.   end
  180.   return false
  181. end
  182.  
  183. function FILE_GET(filename)
  184.   -- First check if file exists
  185.   for i,v in ipairs(index) do
  186.     if (v.filename==filename) then
  187.       -- File found, Now get all parts!
  188.       if (DEBUG) then print("[DBG][FILE_GET] File found!") end
  189.       local tdata = ""
  190.       local size = v.size
  191.       local s = math.floor((size/total))-- Calculate size
  192.       local ls = size-(s*(total-1))
  193.       local p =1
  194.       if (DEBUG) then print("[DBG][FILE_GET] s: "..s..", ls:"..ls) end
  195.       for y,z in ipairs(v.parts) do
  196.         --BugFix: Remove last one
  197.         if (y~=#v.parts) then
  198.           local id = DISK_GETFROMID(z.id)
  199.           local tread = FILE_READ(id,filename,s,z.part)
  200.           if (tread~=nil) then
  201.             tdata = tdata..tread
  202.           end
  203.         else
  204.           local id = DISK_GETFROMID(v.parts[#v.parts].id)
  205.           local tread = FILE_READ(id,filename,ls,p)
  206.           if (tread~=nil) then
  207.             tdata = tdata..tread
  208.           end
  209.           return tdata
  210.         end
  211.         p=p+1
  212.       end
  213.       print("[FATAL] Arrays not matching!")
  214.     end
  215.   end
  216.   print("[ERROR] File not found")
  217.   return nil
  218. end
  219.  
  220. function FILE_REM(filename)
  221.  
  222. end
  223.  
  224. -- Initialize array
  225. function init()
  226.   print("Init:")
  227.   print(INDEX_INIT())
  228.   print("Index_Read: ")
  229.   print(INDEX_READ())
  230.   print("Index Array: ")
  231.   updateArray()
  232.  
  233.   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 ;)")
  234.   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..)")
  235.   print("=========FILE_GET=========")
  236.   print(FILE_GET("Test"))
  237.   print(FILE_GET("Second_file"))
  238.   print("==========================")
  239.   --totalSpace,totalSpace-totalFreeSpace
  240.   INDEX_WRITE()
  241. end
  242.  
  243. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement