Advertisement
Alakazard12

NetworkFolderClient

Apr 6th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. local mod = peripheral.wrap("back")
  2. mod.open(418)
  3.  
  4. local dir = "net"
  5.  
  6. if not fs.exists("tmp") then
  7. fs.makeDir("tmp")
  8. end
  9.  
  10. local con = 0
  11.  
  12. local oFs = {}
  13. for i,v in pairs(fs) do
  14. oFs[i] = v
  15. end
  16.  
  17. local nFs = {}
  18.  
  19. local function transmit(mess)
  20. mod.transmit(418, os.getComputerID(), textutils.serialize(mess))
  21. local event, side, channel, repl, mess
  22. repeat
  23. event, side, channel, repl, mess = os.pullEvent("modem_message")
  24. until channel == 418 and repl == os.getComputerID()
  25. local r = textutils.unserialize(mess)
  26. if type(r) == "string" then
  27. error(mess, 0)
  28. end
  29. return r
  30. end
  31.  
  32. local function getPath(path)
  33. path = fs.combine(path, "")
  34.  
  35. if path == dir or path:sub(1, #dir + 1) == dir .. "/" then
  36. return true, fs.combine(path:sub(#dir + 1), "")
  37. end
  38. return false
  39. end
  40.  
  41. local function upload(name, path)
  42. if oFs.isDir(path) then
  43. -- Recursive stuff
  44. else
  45. local file = oFs.open(path, "rb")
  46. local dat = {}
  47. for on = 1, math.huge do
  48. if on%10000 == 0 then
  49. os.queueEvent("")
  50. coroutine.yield()
  51. end
  52. local by = file.read()
  53. if by then
  54. table.insert(dat, by)
  55. else
  56. file.close()
  57. break
  58. end
  59. end
  60.  
  61. transmit({"upf", name, dat})
  62. end
  63. end
  64.  
  65. local function downloadD(path1, path2)
  66. local dat = transmit({"downd", path1})
  67.  
  68. oFs.makeDir(path2)
  69. local check
  70. function check(pat, fi)
  71. for i,v in pairs(fi) do
  72. if v[1] == "dir" then
  73. oFs.makeDir(pat .. "/" .. i)
  74. check(pat .. "/" .. i, v[2])
  75. else
  76. local file = oFs.open(pat .. "/" .. i, "wb")
  77. for i,v in pairs(v[2]) do
  78. if i%10000 == 0 then
  79. os.queueEvent("")
  80. coroutine.yield()
  81. end
  82. file.write(v)
  83. end
  84. file.close()
  85. end
  86. end
  87. end
  88.  
  89. check(path2, dat)
  90. end
  91.  
  92. local function uploadD(path1, path2)
  93. if not fs.isDir(path1) then
  94. upload(path2, path1)
  95. return
  96. end
  97.  
  98. local files = {}
  99.  
  100. local check
  101. function check(pat, tab)
  102. for i,v in pairs(oFs.list(pat)) do
  103. if oFs.isDir(pat .. "/" .. v) then
  104. tab[v] = {"dir", {}}
  105. check(pat .. "/" .. v, tab[v][2])
  106. else
  107. local file = oFs.open(pat .. "/" .. v, "rb")
  108. tab[v] = {"file", {}}
  109. for on = 1, math.huge do
  110. if on%10000 == 0 then
  111. os.queueEvent("")
  112. coroutine.yield()
  113. end
  114. local by = file.read()
  115. if by then
  116. table.insert(tab[v][2], by)
  117. else
  118. file.close()
  119. break
  120. end
  121. end
  122. end
  123. end
  124. end
  125. check(path1, files)
  126.  
  127. transmit({"upd", path2, files})
  128. end
  129.  
  130. function nFs.copy(rpath1, rpath2)
  131. local tr1, path1 = getPath(rpath1)
  132. local tr2, path2 = getPath(rpath2)
  133.  
  134. if tr1 and not tr2 then
  135. downloadD(path1, rpath2)
  136. elseif tr2 and not tr1 then
  137. uploadD(rpath1, path2)
  138. elseif tr1 and tr2 then
  139. transmit({"copy", path1, path2})
  140. else
  141. return oFs.copy(rpath1, rpath2)
  142. end
  143. end
  144.  
  145. function nFs.move(rpath1, rpath2)
  146. local tr1, path1 = getPath(rpath1)
  147. local tr2, path2 = getPath(rpath2)
  148.  
  149. if tr1 and not tr2 then
  150. downloadD(path1, rpath2)
  151. fs.delete(rpath1)
  152. elseif tr2 and not tr1 then
  153. uploadD(rpath1, path2)
  154. oFs.delete(rpath1)
  155. elseif tr1 and tr2 then
  156. transmit({"move", path1, path2})
  157. else
  158. return oFs.move(rpath1, rpath2)
  159. end
  160. end
  161.  
  162. function nFs.open(rpath, ty)
  163. local tr, path = getPath(rpath)
  164. if tr then
  165. local name = "tmp/" .. con
  166. if not ty:find("w") then
  167. local da = transmit({"downf", path})
  168. local file = oFs.open(name, "wb")
  169. for i,v in pairs(da) do
  170. file.write(v)
  171. end
  172. file.close()
  173. con = con + 1
  174. end
  175. local hand = oFs.open(name, ty)
  176. local ocl = hand.close
  177. hand.close = function()
  178. ocl()
  179. upload(path, name)
  180. end
  181.  
  182. return hand
  183. end
  184. return oFs.open(rpath, ty)
  185. end
  186.  
  187. function nFs.delete(rpath)
  188. local tr, path = getPath(rpath)
  189. if tr then
  190. return transmit({"delete", path})
  191. end
  192. return oFs.delete(rpath)
  193. end
  194.  
  195. function nFs.getSize(rpath)
  196. local tr, path = getPath(rpath)
  197. if tr then
  198. return transmit({"getSize", path})
  199. end
  200. return oFs.getSize(rpath)
  201. end
  202.  
  203. function nFs.getFreeSpace(rpath)
  204. local tr, path = getPath(rpath)
  205. if tr then
  206. return transmit({"getFreeSpace", path})
  207. end
  208. return oFs.getFreeSpace(rpath)
  209. end
  210.  
  211. function nFs.exists(rpath)
  212. local tr, path = getPath(rpath)
  213. if tr then
  214. return transmit({"exists", path})
  215. end
  216. local r = oFs.exists(rpath)
  217. if fs.combine(rpath, "") == "net" then
  218. r = true
  219. end
  220. return r
  221. end
  222.  
  223. function nFs.isReadOnly(rpath)
  224. local tr, path = getPath(rpath)
  225. if tr then
  226. return transmit({"isReadOnly", path})
  227. end
  228. return oFs.isReadOnly(rpath)
  229. end
  230.  
  231. function nFs.list(rpath)
  232. local tr, path = getPath(rpath)
  233. if tr then
  234. return transmit({"list", path})
  235. end
  236. local r = oFs.list(rpath)
  237. if fs.combine(rpath, "") == "" then
  238. table.insert(r, "net")
  239. end
  240. return r
  241. end
  242.  
  243. function nFs.isDir(rpath)
  244. local tr, path = getPath(rpath)
  245. if tr then
  246. return transmit({"isDir", path})
  247. end
  248. local r = oFs.isDir(rpath)
  249. if fs.combine(rpath, "") == "" then
  250. r = true
  251. end
  252. return r
  253. end
  254.  
  255. function nFs.makeDir(rpath)
  256. local tr, path = getPath(rpath)
  257. if tr then
  258. return transmit({"makeDir", path})
  259. end
  260. return oFs.makeDir(rpath)
  261. end
  262.  
  263. for i,v in pairs(nFs) do
  264. fs[i] = v
  265. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement