Operating system for OpenComputers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 line
2.4KB

  1. local mtar = require "libmtar"
  2. local w, lz16 = pcall(require, "liblz16")
  3. if not w then lz16 = nil end
  4. pkgfs = {}
  5. local findex = {}
  6. local handles = {}
  7. local hc = 0
  8. local function rfalse()
  9. return false
  10. end
  11. local function rzero()
  12. return 0
  13. end
  14. pkgfs.component = {seek = rfalse, makeDirectory = rfalse, write = rfalse, rename = rfalse, setlabel = rfalse, spaceUsed = rzero, spaceTotal = rzero, lastModified = rzero}
  15. local function fopen(path,comp)
  16. local f
  17. if comp and lz16 then
  18. f = lz16.open(path,"rb")
  19. else
  20. f = io.open(path,"rb")
  21. end
  22. return f
  23. end
  24. local function fnormalize(s)
  25. return table.concat(fs.segments(s),"/")
  26. end
  27. function pkgfs.component.list(path)
  28. path = fnormalize(path).."/"
  29. local ft,rt = {},{}
  30. for k,v in pairs(findex) do
  31. k="/"..k
  32. if k:match(path.."([^/]+)/.+") then
  33. ft[k:match(path.."([^/]+)/.+").."/"] = true
  34. elseif k:match(path.."([^/]+)") then
  35. ft[k:match(path.."([^/]+)")] = true
  36. end
  37. end
  38. for k,v in pairs(ft) do
  39. rt[#rt+1] = k
  40. end
  41. return rt
  42. end
  43. function pkgfs.component.isDirectory(path)
  44. path = fnormalize(path).."/"
  45. for k,v in pairs(findex) do
  46. k="/"..k
  47. if k:match(path.."([^/]+)/.+") then
  48. return true
  49. end
  50. end
  51. return false
  52. end
  53. function pkgfs.component.size(path)
  54. path=fnormalize(path)
  55. if not findex[path] then return false end
  56. local f = fopen(findex[path][1], findex[path][2])
  57. for fname, read, fsize in mtar.iter(f) do
  58. if fname == path then
  59. return fsize
  60. end
  61. end
  62. return false
  63. end
  64. function pkgfs.component.open(path,mode)
  65. path=fnormalize(path)
  66. if mode:find("w") or mode:find("a") or not findex[path] then
  67. return false
  68. end
  69. local f = fopen(findex[path][1],findex[path][2])
  70. for fname,read,fsize in mtar.iter(f) do
  71. if fname == path then
  72. hc = hc + 1
  73. handles[hc] = {read, f}
  74. return hc
  75. end
  76. end
  77. end
  78. function pkgfs.component.read(handle, n)
  79. if not handles[handle] then return false end
  80. local rv = handles[handle][1](n)
  81. if not rv then return nil end
  82. if rv:len() < 1 then return nil end
  83. return rv
  84. end
  85. function pkgfs.component.close(handle)
  86. if not handles[handle] then return false end
  87. handles[handle][2]:close()
  88. handles[handle] = nil
  89. return true
  90. end
  91. function pkgfs.add(fname,comp)
  92. if fname:sub(1,1) ~= "/" then
  93. fname = "/"..fnormalize(os.getenv("PWD").."/"..fname)
  94. end
  95. local f = fopen(fname,comp)
  96. if not fname then error("unable to open file") end
  97. print(fname)
  98. for name, read, fsize in mtar.iter(f) do
  99. findex[fnormalize(name)] = {fname,comp}
  100. end
  101. f:close()
  102. end
  103. return pkgfs