Guest User

Untitled

a guest
Mar 20th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. local sfs={}
  2. for k,v in pairs(fs) do
  3. sfs[k]=v
  4. end
  5. function sfs.isReadOnly(file) -- AVR compatability
  6. local r,d=pcall(fs.isReadOnly,file)
  7. if not r then
  8. print(file.." errored!")
  9. return true
  10. else
  11. return d
  12. end
  13. end
  14. do
  15. local function serializeImpl(t)
  16. local sType = type(t)
  17. if sType == "table" then
  18. local lstcnt=0
  19. for k,v in pairs(t) do
  20. lstcnt = lstcnt + 1
  21. end
  22. local result = "{"
  23. local aset=1
  24. for k,v in pairs(t) do
  25. if k==aset then
  26. result = result..serializeImpl(v)..","
  27. aset=aset+1
  28. else
  29. result = result..("["..serializeImpl(k).."]="..serializeImpl(v)..",")
  30. end
  31. end
  32. result = result.."}"
  33. return result
  34. elseif sType == "string" then
  35. return string.gsub(string.gsub(string.format("%q",t),"\\\n","\\n"),"\n","\\n")
  36. elseif sType == "number" or sType == "boolean" or sType == "nil" then
  37. return tostring(t)
  38. elseif sType == "function" then
  39. local status,data=pcall(string.dump,t)
  40. if status then
  41. return 'func('..string.format("%q",data)..')'
  42. else
  43. error()
  44. end
  45. else
  46. error()
  47. end
  48. end
  49.  
  50. function split(T,func)
  51. if func then
  52. T=func(T)
  53. end
  54. local Out={}
  55. if type(T)=="table" then
  56. for k,v in pairs(T) do
  57. Out[split(k)]=split(v)
  58. end
  59. else
  60. Out=T
  61. end
  62. return Out
  63. end
  64.  
  65. local function serialize( t )
  66. t=split(t)
  67. return serializeImpl( t, tTracking )
  68. end
  69.  
  70. local function unserialize( s )
  71. local func, e = loadstring( "return "..s, "serialize" )
  72. local funcs={}
  73. if not func then
  74. return e
  75. end
  76. setfenv( func, {
  77. func=function(S)
  78. local new={}
  79. funcs[new]=S
  80. return new
  81. end,
  82. })
  83. return split(func(),function(val)
  84. if funcs[val] then
  85. return loadstring(funcs[val])
  86. else
  87. return val
  88. end
  89. end)
  90. end
  91. local function clear()
  92. term.clear()
  93. term.setCursorPos(1,1)
  94. end
  95. clear()
  96. local data={}
  97. local pending={"disk"}
  98. clear()
  99. print("packing...")
  100. local folders={}
  101. while true do
  102. if not pending[1] then
  103. for k,v in pairs(folders) do
  104. sfs.delete(v)
  105. end
  106. break
  107. end
  108. local file=pending[1]
  109. print(file)
  110. table.remove(pending,1)
  111. if (sfs.isReadOnly(file) or file==encryptapi) and file~="/" then
  112. elseif sfs.isDir(file) then
  113. if file~="/" then
  114. data[file]=false
  115. end
  116. for k,v in pairs(sfs.list(file)) do
  117. table.insert(pending,sfs.combine(file,v))
  118. end
  119. if not sfs.isReadOnly(file) then
  120. table.insert(folders,file)
  121. end
  122. else
  123. local file2=sfs.open(file,"r")
  124. if file2 then
  125. data[file]=file2.readAll()
  126. file2.close()
  127. else
  128. print(file.." is unreadable!")
  129. end
  130. end
  131. sleep(0)
  132. end
  133. rednet.broadcast(serialize(data))
  134. end
Advertisement
Add Comment
Please, Sign In to add comment