Operating system for OpenComputers
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

65 lines
1.7KB

  1. iofs = {}
  2. iofs.files = {}
  3. iofs.fds = {}
  4. iofs.nextfd = 0
  5. iofs.component = {}
  6. local function rfalse()
  7. return false
  8. end
  9. function iofs.component.getLabel()
  10. return "iofs"
  11. end
  12. iofs.component.spaceUsed, iofs.component.spaceTotal, iofs.component.isReadOnly, iofs.component.isDirectory,iofs.component.size, iofs.component.setLabel = function() return computer.totalMemory()-computer.freeMemory() end, computer.totalMemory, rfalse, rfalse, rfalse, rfalse
  13. function iofs.component.exists(fname)
  14. return iofs.files[fname] ~= nil
  15. end
  16. function iofs.component.list()
  17. local t = {}
  18. for k,v in pairs(iofs.files) do
  19. t[#t+1] = k
  20. end
  21. return t
  22. end
  23. function iofs.component.open(fname, mode)
  24. fname=fname:gsub("/","")
  25. if iofs.files[fname] then
  26. local r,w,c,s = iofs.files[fname](mode)
  27. iofs.fds[iofs.nextfd] = {["read"]=r or rfalse,["write"]=w or rfalse,["seek"]=s or rfalse,["close"]=c or rfalse}
  28. iofs.nextfd = iofs.nextfd + 1
  29. return iofs.nextfd - 1
  30. end
  31. return false
  32. end
  33. function iofs.component.read(fd,count)
  34. if iofs.fds[fd] then
  35. return iofs.fds[fd].read(count)
  36. end
  37. end
  38. function iofs.component.write(fd,data)
  39. if iofs.fds[fd] then
  40. return iofs.fds[fd].write(data)
  41. end
  42. end
  43. function iofs.component.close(fd)
  44. if iofs.fds[fd] then
  45. iofs.fds[fd].close()
  46. end
  47. iofs.fds[fd] = nil
  48. end
  49. function iofs.component.seek(fd,...)
  50. if iofs.fds[fd] then
  51. return iofs.fds[fd].seek(...)
  52. end
  53. end
  54. function iofs.register(fname,fopen) -- Register a new iofs node with the name *fname* that will run the function *fopen* when opened. This function should return a function for read, a function for write, and a function for close, in that order.
  55. iofs.files[fname] = fopen
  56. end
  57. fs.mounts.iofs = iofs.component