-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patharchive.lua
219 lines (193 loc) · 8.53 KB
/
archive.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
-- Archive library for CC
local LibDeflate = require "LibDeflate"
local compression_level = nil -- compression level (nil for default)
local function split(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
local function getComponent(tab, keys)
if #keys == 0 then return tab end
local k = table.remove(keys, 1)
if tab[k] == nil then return nil
elseif type(tab[k]) ~= "table" then return tab[k]
elseif k == "." then return getComponent(tab, keys)
else return getComponent(tab[k], keys) end
end
local function extract(data, path)
if string.sub(path, 1, 1) == "/" then path = string.sub(path, 2) end
fs.makeDir(path)
for k,v in pairs(data) do
if type(v) == "table" then extract(v, path .. "/" .. k) else
local file = fs.open(path .. "/" .. k, "wb")
for s in string.gmatch(v, ".") do file.write(string.byte(s)) end
file.close()
end
end
end
local function import(path)
local retval = {}
if path == nil then error("Path is nil", 2) end
if not fs.isDir(path) then error(path .. ": Not a directory", 2) end
for k,v in pairs(fs.list(path)) do
if fs.isDir(path .. "/" .. v) then retval[v] = import(path .. "/" .. v) else
local file = fs.open(path .. "/" .. v, "rb")
local r = ""
local b = file.read()
while b ~= nil do
r = r .. string.char(b)
b = file.read()
end
file.close()
retval[v] = r
end
end
return retval
end
local function create(data)
local retval = {}
retval.data = data
function retval.write(path)
local str = LibDeflate:CompressGzip(textutils.serialize(retval.data), compression_level and {level=compression_level})
local file = fs.open(path, "wb")
for s in string.gmatch(str, ".") do file.write(string.byte(s)) end
file.close()
end
function retval.extract(path) extract(retval.data, path) end
function retval.list(path)
if string.sub(path, 1, 1) == "/" then path = string.sub(path, 2) end
local dir = getComponent(retval.data, split(path, "/"))
if type(dir) ~= "table" then error(path .. ": Directory not found", 2) end
local retval = {}
for k,v in pairs(dir) do table.insert(retval, k) end
return retval
end
function retval.exists(path)
if string.sub(path, 1, 1) == "/" then path = string.sub(path, 2) end
return getComponent(retval.data, split(path, "/")) ~= nil
end
function retval.isDir(path)
if string.sub(path, 1, 1) == "/" then path = string.sub(path, 2) end
return type(getComponent(retval.data, split(path, "/"))) == "table"
end
function retval.isReadOnly() return false end
function retval.getSize(path)
if string.sub(path, 1, 1) == "/" then path = string.sub(path, 2) end
local file = getComponent(retval.data, split(path, "/"))
if type(file) ~= "string" then error(path .. ": File not found", 2) end
return string.len(file)
end
function retval.getFreeSpace() return math.huge end
function retval.makeDir(path)
if string.sub(path, 1, 1) == "/" then path = string.sub(path, 2) end
local dir = getComponent(retval.data, split(fs.getDir(path), "/"))
if type(dir) ~= "table" then error(fs.getDir(path) .. ": Directory not found", 2) end
dir[fs.getName(path)] = {}
end
function retval.move(path, toPath)
retval.copy(path, toPath, 1)
retval.delete(path, 1)
end
function retval.copy(path, toPath, offset)
offset = offset or 0
if string.sub(path, 1, 1) == "/" then path = string.sub(path, 2 + offset) end
local file = getComponent(retval.data, split(path, "/"))
if type(file) ~= "string" then error(path .. ": File not found", 2 + offset) end
local toDir = getComponent(retval.data, split(fs.getDir(toPath), "/"))
if type(toDir) ~= "table" then error(fs.getDir(toPath) .. ": Directory not found", 2 + offset) end
toDir[fs.getName(toPath)] = file
end
function retval.delete(path, offset)
offset = offset or 0
if string.sub(path, 1, 1) == "/" then path = string.sub(path, 2) end
local fromDir = getComponent(retval.data, split(fs.getDir(path), "/"))
if type(fromDir) ~= "table" then error(fs.getDir(path) .. ": Directory not found", 2 + offset) end
fromDir[fs.getName(path)] = nil
end
function retval.open(path, mode)
if string.sub(path, 1, 1) == "/" then path = string.sub(path, 2) end
local file = getComponent(retval.data, split(path, "/"))
if type(file) ~= "string" and not string.find(mode, "a") then error(path .. ": File not found", 2) end
if string.find(mode, "a") then file = "" end
local retval = {close = function() if retval.flush ~= nil then retval.flush() end end}
local pos = string.find(mode, "a") and string.len(file) or 1
if string.find(mode, "b") then
if string.find(mode, "r") then
retval.read = function()
pos = pos + 1
return string.byte(string.sub(file, pos - 1, pos - 1))
end
elseif string.find(mode, "w") or string.find(mode, "a") then
retval.write = function(c)
if pos > string.len(file) then file = file .. string.char(c)
else file = string.sub(file, 1, pos - 1) .. string.char(c) .. string.sub(file, pos + 1) end
pos = pos + 1
end
retval.flush = function()
local dir = getComponent(retval.data, split(fs.getDir(path), "/"))
dir[fs.getName(path)] = file
end
end
else
if string.find(mode, "r") then
retval.readLine = function()
if pos > string.len(file) then return nil end
local retval = ""
local c = string.sub(file, pos, pos)
pos = pos + 1
while c ~= "\n" and pos <= string.len(file) do
retval = retval .. c
c = string.sub(file, pos, pos)
pos = pos + 1
end
return retval
end
retval.readAll = function()
if pos > string.len(file) then return nil end
pos = string.len(file) + 1
return file
end
elseif string.find(mode, "w") or string.find(mode, "a") then
retval.write = function(text)
if pos > string.len(file) then file = file .. text
else file = string.sub(file, 1, pos - 1) .. text .. string.sub(file, pos + string.len(text)) end
pos = pos + string.len(text)
end
retval.writeLine = function(text) retval.write(text .. "\n") end
retval.flush = function()
local dir = getComponent(retval.data, split(fs.getDir(path), "/"))
dir[fs.getName(path)] = file
end
end
end
return retval
end
-- for CCKernel2
function retval.getPermissions() return 15 end
function retval.setPermissions() end
function retval.getOwner() return 0 end
function retval.setOwner() end
return retval
end
local function new() return create({}) end
local function load(path) return create(import(path)) end
local function read(path)
local file = fs.open(path, "rb")
local retval = ""
local b = file.read()
while b ~= nil do
retval = retval .. string.char(b)
b = file.read()
end
file.close()
return create(textutils.unserialize(LibDeflate:DecompressGzip(retval)))
end
local function setCompressionLevel(level) compression_level = level end
local archive = {new = new, load = load, read = read, setCompressionLevel = setCompressionLevel}
setmetatable(archive, {__call = function(self, path) if path ~= nil and fs.exists(path) then if fs.isDir(path) then return load(path) else return read(path) end else return new() end end})
return archive