forked from IgorTimofeev/GUI
-
Notifications
You must be signed in to change notification settings - Fork 3
/
filesystem_adv.lua
498 lines (407 loc) · 13.4 KB
/
filesystem_adv.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
local fs = require("filesystem")
--------------------------------------------------------------------------------
local filesystem = {
SORTING_NAME = 1,
SORTING_TYPE = 2,
SORTING_DATE = 3,
}
local BUFFER_SIZE = 1024
local BOOT_PROXY
local mountedProxies = {}
--------------------------------------- String-related path processing -----------------------------------------
function filesystem.path(path)
return path:match("^(.+%/).") or ""
end
function filesystem.name(path)
return path:match("%/?([^%/]+%/?)$")
end
function filesystem.extension(path, lower)
return path:match("[^%/]+(%.[^%/]+)%/?$")
end
function filesystem.hideExtension(path)
return path:match("(.+)%..+") or path
end
function filesystem.isHidden(path)
if path:sub(1, 1) == "." then
return true
end
return false
end
function filesystem.removeSlashes(path)
return path:gsub("/+", "/")
end
--------------------------------------- Mounted filesystem support -----------------------------------------
filesystem.get = fs.get
--------------------------------------- I/O methods -----------------------------------------
local function readString(self, count)
-- If current buffer content is a "part" of "count of data" we need to read
if count > #self.buffer then
local data, chunk = self.buffer
while #data < count do
chunk = self.proxy.read(self.stream, BUFFER_SIZE)
if chunk then
data = data .. chunk
else
self.position = self:seek("end", 0)
-- EOF at start
if data == "" then
return nil
-- EOF after read
else
return data
end
end
end
self.buffer = data:sub(count + 1, -1)
chunk = data:sub(1, count)
self.position = self.position + #chunk
return chunk
else
local data = self.buffer:sub(1, count)
self.buffer = self.buffer:sub(count + 1, -1)
self.position = self.position + count
return data
end
end
local function readLine(self)
local data = ""
while true do
if #self.buffer > 0 then
local starting, ending = self.buffer:find("\n")
if starting then
local chunk = self.buffer:sub(1, starting - 1)
self.buffer = self.buffer:sub(ending + 1, -1)
self.position = self.position + #chunk
return data .. chunk
else
data = data .. self.buffer
end
end
local chunk = self.proxy.read(self.stream, BUFFER_SIZE)
if chunk then
self.buffer = chunk
self.position = self.position + #chunk
-- EOF
else
local data = self.buffer
self.position = self:seek("end", 0)
return #data > 0 and data or nil
end
end
end
local function lines(self)
return function()
local line = readLine(self)
if line then
return line
else
self:close()
end
end
end
local function readAll(self)
local data, chunk = ""
while true do
chunk = self.proxy.read(self.stream, 4096)
if chunk then
data = data .. chunk
-- EOF
else
self.position = self:seek("end", 0)
return data
end
end
end
local function readBytes(self, count, littleEndian)
if count == 1 then
local data = readString(self, 1)
if data then
return string.byte(data)
end
return nil
else
local bytes, result = { string.byte(readString(self, count) or "\x00", 1, 8) }, 0
if littleEndian then
for i = #bytes, 1, -1 do
result = bit32.bor(bit32.lshift(result, 8), bytes[i])
end
else
for i = 1, #bytes do
result = bit32.bor(bit32.lshift(result, 8), bytes[i])
end
end
return result
end
end
local function readUnicodeChar(self)
local byteArray = { string.byte(readString(self, 1)) }
local nullBitPosition = 0
for i = 1, 7 do
if bit32.band(bit32.rshift(byteArray[1], 8 - i), 0x1) == 0x0 then
nullBitPosition = i
break
end
end
for i = 1, nullBitPosition - 2 do
table.insert(byteArray, string.byte(readString(self, 1)))
end
return string.char(table.unpack(byteArray))
end
local function read(self, format, ...)
local formatType = type(format)
if formatType == "number" then
return readString(self, format)
elseif formatType == "string" then
format = format:gsub("^%*", "")
if format == "a" then
return readAll(self)
elseif format == "l" then
return readLine(self)
elseif format == "b" then
return readBytes(self, 1)
elseif format == "bs" then
return readBytes(self, ...)
elseif format == "u" then
return readUnicodeChar(self)
else
error("bad argument #2 ('a' (whole file), 'l' (line), 'u' (unicode char), 'b' (byte as number) or 'bs' (sequence of n bytes as number) expected, got " .. format .. ")")
end
else
error("bad argument #1 (number or string expected, got " .. formatType .. ")")
end
end
local function seek(self, pizda, cyka)
if pizda == "set" then
local result, reason = self.proxy.seek(self.stream, "set", cyka)
if result then
self.position = result
self.buffer = ""
end
return result, reason
elseif pizda == "cur" then
local result, reason = self.proxy.seek(self.stream, "set", self.position + cyka)
if result then
self.position = result
self.buffer = ""
end
return result, reason
elseif pizda == "end" then
local result, reason = self.proxy.seek(self.stream, "end", cyka)
if result then
self.position = result
self.buffer = ""
end
return result, reason
else
error("bad argument #2 ('set', 'cur' or 'end' expected, got " .. tostring(whence) .. ")")
end
end
local function write(self, ...)
local data = { ... }
for i = 1, #data do
data[i] = tostring(data[i])
end
data = table.concat(data)
-- Data is small enough to fit buffer
if #data < (BUFFER_SIZE - #self.buffer) then
self.buffer = self.buffer .. data
return true
else
-- Write current buffer content
local success, reason = self.proxy.write(self.stream, self.buffer)
if success then
-- If data will not fit buffer, use iterative writing with data partitioning
if #data > BUFFER_SIZE then
for i = 1, #data, BUFFER_SIZE do
success, reason = self.proxy.write(self.stream, data:sub(i, i + BUFFER_SIZE - 1))
if not success then
break
end
end
self.buffer = ""
return success, reason
-- Data will perfectly fit in empty buffer
else
self.buffer = data
return true
end
else
return false, reason
end
end
end
local function writeBytes(self, ...)
return write(self, string.char(...))
end
local function close(self)
if self.write and #self.buffer > 0 then
self.proxy.write(self.stream, self.buffer)
end
return self.proxy.close(self.stream)
end
function filesystem.open(path, mode)
local proxy, proxyPath = filesystem.get(path)
local result, reason = proxy.open(path, mode)
if result then
local handle = {
proxy = proxy,
stream = result,
position = 0,
buffer = "",
close = close,
seek = seek,
}
if mode == "r" or mode == "rb" then
handle.readString = readString
handle.readUnicodeChar = readUnicodeChar
handle.readBytes = readBytes
handle.readLine = readLine
handle.lines = lines
handle.readAll = readAll
handle.read = read
return handle
elseif mode == "w" or mode == "wb" or mode == "a" or mode == "ab" then
handle.write = write
handle.writeBytes = writeBytes
return handle
else
error("bad argument #2 ('r', 'rb', 'w', 'wb' or 'a' expected, got )" .. tostring(mode) .. ")")
end
else
return nil, reason
end
end
--------------------------------------- Rest proxy methods -----------------------------------------
function filesystem.exists(path)
local proxy, proxyPath = filesystem.get(path)
return proxy.exists(proxyPath)
end
function filesystem.size(path)
local proxy, proxyPath = filesystem.get(path)
return proxy.size(proxyPath)
end
function filesystem.isDirectory(path)
local proxy, proxyPath = filesystem.get(path)
return proxy.isDirectory(proxyPath)
end
function filesystem.makeDirectory(path)
local proxy, proxyPath = filesystem.get(path)
return proxy.makeDirectory(proxyPath)
end
function filesystem.lastModified(path)
local proxy, proxyPath = filesystem.get(path)
return proxy.lastModified(proxyPath)
end
function filesystem.remove(path)
local proxy, proxyPath = filesystem.get(path)
return proxy.remove(proxyPath)
end
function filesystem.rename(fromPath, toPath)
local fromProxy, fromProxyPath = filesystem.get(fromPath)
local toProxy, toProxyPath = filesystem.get(toPath)
-- If it's the same filesystem component
if fromProxy.address == toProxy.address then
return fromProxy.rename(fromProxyPath, toProxyPath)
else
-- Copy files to destination
filesystem.copy(fromPath, toPath)
-- Remove original files
filesystem.remove(fromPath)
end
end
--------------------------------------- Advanced methods -----------------------------------------
function filesystem.copy(fromPath, toPath)
local function copyRecursively(fromPath, toPath)
if filesystem.isDirectory(fromPath) then
filesystem.makeDirectory(toPath)
local list = filesystem.list(fromPath)
for i = 1, #list do
copyRecursively(fromPath .. "/" .. list[i], toPath .. "/" .. list[i])
end
else
local fromHandle = filesystem.open(fromPath, "rb")
if fromHandle then
local toHandle = filesystem.open(toPath, "wb")
if toHandle then
while true do
local chunk = readString(fromHandle, BUFFER_SIZE)
if chunk then
if not write(toHandle, chunk) then
break
end
else
toHandle:close()
fromHandle:close()
break
end
end
end
end
end
end
copyRecursively(fromPath, toPath)
end
function filesystem.read(path)
local handle, reason = filesystem.open(path, "rb")
if handle then
local data = readAll(handle)
handle:close()
return data
end
return false, reason
end
function filesystem.lines(path)
local handle, reason = filesystem.open(path, "rb")
if handle then
return handle:lines()
else
error(reason)
end
end
function filesystem.readLines(path)
local handle, reason = filesystem.open(path, "rb")
if handle then
local lines, index, line = {}, 1
repeat
line = readLine(handle)
lines[index] = line
index = index + 1
until not line
handle:close()
return lines
end
return false, reason
end
local function writeOrAppend(append, path, ...)
filesystem.makeDirectory(filesystem.path(path))
local handle, reason = filesystem.open(path, append and "ab" or "wb")
if handle then
local result, reason = write(handle, ...)
handle:close()
return result, reason
end
return false, reason
end
function filesystem.write(path, ...)
return writeOrAppend(false, path, ...)
end
function filesystem.append(path, ...)
return writeOrAppend(true, path, ...)
end
function filesystem.writeTable(path, ...)
return filesystem.write(path, require("Text").serialize(...))
end
function filesystem.readTable(path)
local result, reason = filesystem.read(path)
if result then
return require("Text").deserialize(result)
end
return result, reason
end
function filesystem.setProxy(proxy)
BOOT_PROXY = proxy
end
function filesystem.getProxy()
return BOOT_PROXY
end
return filesystem