Advertisement
Deftry

Cassete pseudo FS(Computronics | OpenComputers)

Jun 5th, 2021
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.60 KB | None | 0 0
  1. local c = require("component")
  2. local fs = require("filesystem")
  3. local event = require("event")
  4. local tape = c.tape_drive
  5. local readed = 0
  6. local file0, file1, file2, file3, file4, file5, file6, file7, file8, file9
  7.  
  8. print("Mounting tape to \"/usr/tape\"...")
  9. local pos = tape.getPosition()
  10. tape.seek(-pos)
  11. local isTapeFS = tape.read(4)
  12. if isTapeFS=="TPFS" then
  13.   print("Tape has filesystem. So, tape can have files")
  14.  
  15.   for i=0, 9, 1 do
  16.     local file = fs.open("/usr/tape/file" .. i .. ".raw", "a")
  17.     print("Writed 4KB file to /usr/tape/file" .. i .. ".raw")
  18.     file:write(tape.read(4000))
  19.     file:close()
  20.   end
  21.   print("Writing done!")
  22.  
  23.    
  24. else
  25.   print("Filesystem error. Tape will mounted as one file")
  26.   print("Reading all data to 1 file")
  27.   tape.seek(-4)
  28.   fs.remove("/usr/tape/tape.raw")
  29.   local file = fs.open("/usr/tape/tape.raw", "a")
  30.   for i = 1, tape.getSize(),100 do
  31.     local byte = tape.read(100)
  32.     if string.byte(byte)==0 then
  33.       break
  34.     else
  35.       readed = readed + 100
  36.       file:write(byte)
  37.       print("Writed \"" .. byte .. "\". First byte: " .. string.byte(byte))
  38.     end
  39.   end
  40.   file:close()
  41.   print("Writing done!")
  42. end
  43. while true do
  44. os.execute("cls")
  45.  
  46. if isTapeFS=="TPFS" then
  47.   print("TapeFS v1 - Working with prepared cassete                          By !( Del )!")
  48.   print("-------------------------------------------------------------------------------")
  49.   print("1. Edit cassete files data                                                     ")
  50.   print("2. Apply changes, Unmount cassete and exit from program                        ")
  51.   print("                                                                               ")
  52.   print("-------------------------------------------------------------------------------")
  53.   print("Cassete size: " .. tape.getSize() / 1000 .. "KB, but can work with 10 4KB files")
  54.   print("-------------------------------------------------------------------------------")
  55.   local _, _, _, choice = event.pull("key_down")
  56.   if choice==2 then
  57.     os.execute("cls")
  58.     print("Number of file(0-9)")
  59.     local file = io.read()
  60.     os.execute("edit /usr/tape/file" .. file .. ".raw")
  61.   end
  62.   if choice==3 then
  63.     tape.seek(-900000)
  64.     tape.write(string.rep(" ", 900000))
  65.     tape.seek(-900000)
  66.     tape.seek(4)
  67.     local skip = 0
  68.     for i=0, 9, 1 do
  69.       skip = 0
  70.       local file = fs.open("/usr/tape/file" .. i .. ".raw")
  71.       local content = file:read(4000)
  72.       if not content then content = "" end
  73.       if content:len() < 4000 then
  74.         skip = 4000 - content:len()
  75.       end
  76.       file:close()
  77.       print("Writing data to file " .. i .. "... Position: " .. tape.getPosition() .. "b")
  78.       tape.write(content)
  79.       tape.seek(skip)
  80.       fs.remove("/usr/tape/file" .. i .. ".raw")
  81.       --tape.seek(4000)
  82.     end
  83.     tape.seek(-900000)
  84.     tape.write("TPFS")
  85.     os.exit()
  86.   end
  87. else
  88.   print("TapeFS v1 - Working with not prepared cassete             By !( Del )!")
  89.   print("----------------------------------------------------------------------")
  90.   print("1. Edit cassete data                                                  ")
  91.   print("2. Unmount cassete and exit from program                              ")
  92.   print("3. Make a filesystem on cassete                                       ")
  93.   print("----------------------------------------------------------------------")
  94.   print("Cassete size: " .. tape.getSize() / 1000 .. "KB                       ")
  95.   print("----------------------------------------------------------------------")
  96.   local _, _, _, choice = event.pull("key_down")
  97.   if choice==2 then
  98.     os.execute("edit /usr/tape/tape.raw")
  99.     local file = fs.open("/usr/tape/tape.raw")
  100.     local content = file:read(4000)
  101.     if not content then content = "" end
  102.     file:close()
  103.     print("Writing data...")
  104.     for i=1, readed,100 do
  105.       tape.write(string.rep(string.char(0), 100))
  106.     end
  107.     tape.seek(-9000000)
  108.     tape.write(content)
  109.   end
  110.   if choice==3 then
  111.     fs.remove("/usr/tape/tape.raw")
  112.     os.exit()
  113.   end
  114.   if choice==4 then
  115.     os.execute("cls")
  116.     print("Making a filesystem will erase ALL data from cassete. Are you sure?(Y/n)")
  117.     local confirm = io.read()
  118.     if confirm=="y" then
  119.       print("That your choice... Removing all data from cassete...")
  120.       tape.seek(-900000)
  121.       for i=1, readed, 100 do
  122.         tape.write(string.rep(string.char(0), 100))
  123.       end
  124.       tape.seek(-900000)
  125.       print("Removing complete.")
  126.       print("Writing data about filesystem")
  127.       tape.write("TPFS")
  128.       print("All done!")
  129.       os.exit()
  130.     end
  131.   end
  132. end
  133. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement