MudkipTheEpic

FileSystem 2.0 [WIP]

Feb 3rd, 2013
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.66 KB | None | 0 0
  1. if fs.setReadOnly ~= nil then error("Already loaded!") end
  2. local shellProgram = shell.getRunningProgram
  3. local oldOpen = fs.open
  4. local oldCopy = fs.copy
  5. local oldDelete = fs.delete
  6. local oldMove = fs.move
  7. local oldReadOnly = fs.isReadOnly
  8. local oldList = fs.list
  9. local rT = {}
  10. --local iR = {}
  11. local hidden = {}
  12. local allowedPrograms={}
  13. local function isAllowed()
  14. if #allowedPrograms==0 then return true end
  15. for k,v in pairs(allowedPrograms) do
  16. if v==shellProgram() then return true end
  17. end
  18. return false
  19. end
  20. local function hidecheck(filename)
  21. for k,v in pairs(hidden) do
  22. if v==filename then return true end
  23. end
  24. return false
  25. end
  26. --[
  27. local function readcheck(filename)
  28. for k,v in pairs(iR) do
  29. if v==filename then return true end
  30. end
  31. return false
  32. end
  33. --]
  34. local function check(filename)
  35. for k,v in pairs(rT) do
  36. if v==filename then return true end
  37. end
  38. --for k,v in pairs(iR) do
  39. --if v==filename then return true end
  40. --end
  41. return false
  42. end
  43. local function errorout()
  44. error("File is readonly", 0)
  45. end
  46.  
  47. function fs.open(f,v)
  48. --if readcheck(v) then errorout() end
  49. if isAllowed() then return oldOpen(f,v) end
  50. if v ~= ("r" or "rb" or nil) then
  51. if check(f) then
  52. errorout()
  53. end
  54. end
  55. return oldOpen(f,v)
  56. end
  57.  
  58. function fs.delete(f)
  59. if isAllowed() then return oldDelete(f) end
  60. if check(f) then
  61. errorout()
  62. end
  63. return oldDelete(f)
  64. end
  65.  
  66. function fs.copy(f,v)
  67. if isAllowed() then return oldCopy(f,v) end
  68. if check(f) then
  69. errorout()
  70. end
  71. return oldCopy(f,v)
  72. end
  73.  
  74. function fs.move(f,v)
  75. if isAllowed() then return oldMove(f,v) end
  76. if check(f) then
  77. errorout()
  78. end
  79. return oldMove(f,v)
  80. end
  81.  
  82. function fs.isReadOnly(f)
  83. if check(f) then
  84. return true
  85. end
  86. return oldReadOnly(f)
  87. end
  88.  
  89. function fs.setReadOnly(file, bool)
  90. if not isAllowed() then error("Not permitted.",2) return nil end
  91. if not type(bool) == "boolean" then error("Boolean expected, got "..type(bool)..".") end
  92. if bool then
  93. if not check(file) then
  94. table.insert(rT, file)
  95. end
  96. return true
  97. else
  98. for k,v in pairs(rT) do
  99. if v == file then
  100. table.remove(rT, k)
  101. derp = true
  102. end
  103. end
  104. if derp then return true end
  105. return false
  106. end
  107. end
  108.  
  109. --[
  110. function fs.setReadable(file, bool)
  111. if not type(bool) == "boolean" then error("Boolean expected, got "..type(bool)..".") end
  112. if not bool then
  113. table.insert(iR, file)
  114. return true
  115. else
  116. for k,v in pairs(iR) do
  117. if v == file then
  118. table.remove(iR, k)
  119. return true
  120. end
  121. end
  122. return false
  123. end
  124. end
  125.  
  126. --]
  127. function fs.hide(file)
  128. if not isAllowed() then error("Not permitted.",2) return nil end
  129. if not hidecheck(file) then
  130. table.insert(hidden,file)
  131. end
  132. return true
  133. end
  134.  
  135. function fs.show(file)
  136. if not isAllowed() then error("Not permitted.",2) return nil end
  137. for k,v in pairs(hidden) do
  138. if v==file then
  139. table.remove(hidden,k)
  140. derp = true
  141. end
  142. end
  143. derp = derp or false
  144. return derp
  145. end
  146.  
  147. function fs.list(dir)
  148. local dir=dir or ""
  149. if isAllowed() then return oldList(dir) end
  150. if hidecheck(dir) then return {} end
  151. local d = oldList(dir)
  152. for k,v in pairs(d) do
  153. if hidecheck(v) then
  154. table.remove(d,k)
  155. end
  156. end
  157. return d
  158. end
  159.  
  160. --Make all programs in config.permissions allowed
  161. while true do
  162. if not fs.exists("config.permissions") then break end
  163. local fh=fs.open("config.permissions", "r")
  164. while true do
  165. local line=fh.readLine()
  166. if line==nil then break end
  167. table.insert(allowedPrograms, line)
  168. end
  169. fh.close()
  170. break
  171. end
  172. --Hide and make read-only allowed programs
  173. for k,v in pairs(allowedPrograms) do
  174. if not check(v) then
  175. table.insert(rT,v)
  176. end
  177. if not hidecheck(v) then
  178. table.insert(hidden,v)
  179. end
  180. end
  181. table.insert(rT,"config.permissions")
  182. table.insert(hidden,"config.permissions")
  183. table.insert(rT,shellProgram())
  184. table.insert(hidden,shellProgram())
Advertisement
Add Comment
Please, Sign In to add comment