Operating system for OpenComputers
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

121 satır
2.8KB

  1. fs = {}
  2. fs.mounts = {}
  3. -- basics
  4. function fs.segments(path) -- splits *path* on each /
  5. local segments = {}
  6. for segment in path:gmatch("[^/]+") do
  7. segments[#segments+1] = segment
  8. end
  9. return segments
  10. end
  11. function fs.resolve(path) -- resolves *path* to a specific filesystem mount and path
  12. if not path or path == "." then path = os.getenv("PWD") end
  13. if path:sub(1,1) ~= "/" then path=(os.getenv("PWD") or "").."/"..path end
  14. local segments, rpath = fs.segments(path), "/"
  15. for i = 2, #segments do
  16. rpath = rpath .. segments[i] .. "/"
  17. end
  18. rpath = rpath:match("(.+)/") or rpath
  19. return segments[1] or "root",rpath
  20. end
  21. -- generate some simple functions
  22. for k,v in pairs({"makeDirectory","exists","isDirectory","list","lastModified","remove","size","spaceUsed","isReadOnly","getLabel"}) do
  23. fs[v] = function(path)
  24. local fsi,path = fs.resolve(path)
  25. return fs.mounts[fsi][v](path)
  26. end
  27. end
  28. local function fread(self,length)
  29. if length == "*a" then
  30. length = math.huge
  31. end
  32. if type(length) == "number" then
  33. local rstr, lstr = "", ""
  34. repeat
  35. lstr = fs.mounts[self.fs].read(self.fid,math.min(2^16,length-rstr:len())) or ""
  36. rstr = rstr .. lstr
  37. until rstr:len() == length or lstr == ""
  38. return rstr
  39. end
  40. return fs.mounts[self.fs].read(self.fid,length)
  41. end
  42. local function fwrite(self,data)
  43. fs.mounts[self.fs].write(self.fid,data)
  44. end
  45. local function fclose(self)
  46. fs.mounts[self.fs].close(self.fid)
  47. end
  48. function fs.open(path,mode) -- opens file *path* with mode *mode*
  49. mode = mode or "rb"
  50. local fsi,path = fs.resolve(path)
  51. if not fs.mounts[fsi] then return false end
  52. local fid = fs.mounts[fsi].open(path,mode)
  53. if fid then
  54. local fobj = {["fs"]=fsi,["fid"]=fid,["close"]=fclose}
  55. if mode:find("r") then
  56. fobj.read = fread
  57. end
  58. if mode:find("w") then
  59. fobj.write = fwrite
  60. end
  61. return fobj
  62. end
  63. return false
  64. end
  65. function fs.copy(from,to) -- copies a file from *from* to *to*
  66. local of = fs.open(from,"rb")
  67. local df = fs.open(to,"wb")
  68. if not of or not df then
  69. return false
  70. end
  71. df:write(of:read("*a"))
  72. df:close()
  73. of:close()
  74. end
  75. function fs.rename(from,to) -- moves file *from* to *to*
  76. local ofsi, opath = fs.resolve(from)
  77. local dfsi, dpath = fs.resolve(to)
  78. if ofsi == dfsi then
  79. fs.mounts[ofsi].rename(opath,dpath)
  80. return true
  81. end
  82. fs.copy(from,to)
  83. fs.remove(from)
  84. return true
  85. end
  86. fs.mounts.temp = component.proxy(computer.tmpAddress())
  87. if computer.getBootAddress then
  88. fs.mounts.boot = component.proxy(computer.getBootAddress())
  89. end
  90. for addr, _ in component.list("filesystem") do
  91. fs.mounts[addr:sub(1,3)] = component.proxy(addr)
  92. end
  93. local function rf()
  94. return false
  95. end
  96. fs.mounts.root = {}
  97. for k,v in pairs(fs.mounts.temp) do
  98. fs.mounts.root[k] = rf
  99. end
  100. function fs.mounts.root.list()
  101. local t = {}
  102. for k,v in pairs(fs.mounts) do
  103. t[#t+1] = k
  104. end
  105. t.n = #t
  106. return t
  107. end
  108. function fs.mounts.root.isReadOnly()
  109. return true
  110. end