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 lines
2.2KB

  1. local unionfs = {}
  2. local function normalise(path)
  3. return table.concat(fs.segments(path),"/")
  4. end
  5. function unionfs.create(...) -- string -- table -- Returns a unionfs object of the directories specified in *...*.
  6. local paths,fids,fc = {...}, {}, 0
  7. for k,v in pairs(paths) do
  8. paths[k] = "/"..normalise(v)
  9. end
  10. local proxy = {}
  11. local function realpath(path)
  12. path = path or ""
  13. for k,v in pairs(paths) do
  14. if fs.exists(v.."/"..path) then
  15. return v.."/"..path
  16. end
  17. end
  18. return paths[1].."/"..path
  19. end
  20. function proxy.setLabel()
  21. return false
  22. end
  23. function proxy.spaceUsed()
  24. return fs.spaceUsed(paths[1])
  25. end
  26. function proxy.spaceTotal()
  27. return fs.spaceTotal(paths[1])
  28. end
  29. function proxy.isReadOnly()
  30. return fs.isReadOnly(paths[1])
  31. end
  32. function proxy.isDirectory(path)
  33. return fs.isDirectory(realpath(path))
  34. end
  35. function proxy.lastModified(path)
  36. return fs.lastModified(realpath(path))
  37. end
  38. function proxy.getLabel()
  39. return fs.getLabel(paths[1])
  40. end
  41. function proxy.exists(path)
  42. return fs.exists(realpath(path))
  43. end
  44. function proxy.remove(path)
  45. return fs.remove(realpath(path))
  46. end
  47. function proxy.size(path)
  48. return fs.size(realpath(path))
  49. end
  50. function proxy.list(path)
  51. local nt,rt = {},{}
  52. if #fs.segments(path) < 1 then
  53. for k,v in pairs(paths) do
  54. for l,m in ipairs(fs.list(v.."/"..path)) do
  55. nt[m] = true
  56. end
  57. end
  58. for k,v in pairs(nt) do
  59. rt[#rt+1] = k
  60. end
  61. table.sort(rt)
  62. return rt
  63. else
  64. return fs.list(realpath(path))
  65. end
  66. end
  67. function proxy.open(path,mode)
  68. local fh, r = fs.open(realpath(path),mode)
  69. if not fh then return fh, r end
  70. fids[fc] = fh
  71. fc = fc + 1
  72. return fc - 1
  73. end
  74. function proxy.close(fid)
  75. if not fids[fid] then
  76. return false, "file not open"
  77. end
  78. local rfh = fids[fid]
  79. fids[fid] = nil
  80. return rfh:close()
  81. end
  82. function proxy.write(fid,d)
  83. if not fids[fid] then
  84. return false, "file not open"
  85. end
  86. return fids[fid]:write(d)
  87. end
  88. function proxy.read(fid,d)
  89. if not fids[fid] then
  90. return false, "file not open"
  91. end
  92. local rb = fids[fid]:read(d)
  93. if rb == "" then rb = nil end
  94. return rb
  95. end
  96. function proxy.seek(fid,d)
  97. if not fids[fid] then
  98. return false, "file not open"
  99. end
  100. return fids[fid]:seek(d)
  101. end
  102. return proxy
  103. end
  104. return unionfs