Operating system for OpenComputers
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

107 рядки
2.0KB

  1. local unionfs = {}
  2. local function normalise(path)
  3. return table.concat(fs.segments(path),"/")
  4. end
  5. function unionfs.create(...)
  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.spaceUsed()
  21. return fs.spaceUsed(paths[1])
  22. end
  23. function proxy.spaceTotal()
  24. return fs.spaceTotal(paths[1])
  25. end
  26. function proxy.isReadOnly()
  27. return fs.isReadOnly(paths[1])
  28. end
  29. function proxy.isDirectory(path)
  30. return fs.isDirectory(realpath(path))
  31. end
  32. function proxy.lastModified(path)
  33. return fs.lastModified(realpath(path))
  34. end
  35. function proxy.exists(path)
  36. return fs.exists(realpath(path))
  37. end
  38. function proxy.remove(path)
  39. return fs.remove(realpath(path))
  40. end
  41. function proxy.size(path)
  42. return fs.size(realpath(path))
  43. end
  44. function proxy.list(path)
  45. local nt,rt = {},{}
  46. if #fs.segments(path) < 1 then
  47. for k,v in pairs(paths) do
  48. print(v.."/"..path)
  49. for l,m in ipairs(fs.list(v.."/"..path)) do
  50. nt[m] = true
  51. end
  52. end
  53. for k,v in pairs(nt) do
  54. rt[#rt+1] = k
  55. end
  56. table.sort(rt)
  57. return rt
  58. else
  59. return fs.list(realpath(path))
  60. end
  61. end
  62. function proxy.open(path,mode)
  63. local fh, r = fs.open(realpath(path),mode)
  64. if not fh then return fh, r end
  65. fids[fc] = fh
  66. fc = fc + 1
  67. return fc - 1
  68. end
  69. function proxy.close(fid)
  70. if not fids[fid] then
  71. return false, "file not open"
  72. end
  73. local rfh = fids[fid]
  74. fids[fid] = nil
  75. return rfh:close()
  76. end
  77. function proxy.write(fid,d)
  78. if not fids[fid] then
  79. return false, "file not open"
  80. end
  81. return fids[fid]:write(d)
  82. end
  83. function proxy.read(fid,d)
  84. if not fids[fid] then
  85. return false, "file not open"
  86. end
  87. return fids[fid]:read(d)
  88. end
  89. function proxy.seek(fid,d)
  90. if not fids[fid] then
  91. return false, "file not open"
  92. end
  93. return fids[fid]:seek(d)
  94. end
  95. return proxy
  96. end
  97. return unionfs