-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05-filesystem.lua
More file actions
1841 lines (1753 loc) · 77.6 KB
/
Copy path05-filesystem.lua
File metadata and controls
1841 lines (1753 loc) · 77.6 KB
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--- filesystem
-- @section filesystem
--[[
CraftOS mount metadata is stored in the following format: {
meta = {
type = "directory",
owner = "root",
permissions = {
root = {read = true, write = true, execute = true}
},
worldPermissions = {read = true, write = false, execute = true}
},
contents = {
home = {
meta = {
type = "directory",
owner = "user",
permissions = {
user = {read = true, write = true, execute = true},
admins = {read = true, write = false, execute = true}
},
worldPermissions = {read = false, write = false, execute = false}
},
contents = {
["info.txt"] = {
meta = {
type = "link",
owner = "user",
permissions = {
user = {read = true, write = true, execute = false}
},
worldPermissions = {read = false, write = false, execute = false}
},
link = "../info.txt"
}
}
},
["info.txt"] = {
meta = {
type = "file",
owner = "root",
permissions = {
root = {read = true, write = true, execute = false}
},
worldPermissions = {read = true, write = false, execute = false}
}
}
}
}
]]
--- Stores the current mounts as a key-value table of paths to filesystem objects.
mounts = {}
--- Stores all FIFOs in a table-indexed table.
fifos = {}
--- Stores paths that have fsevents registered, and the process(es) that are listening.
fsevents = {}
--- This table contains all filesystem types. Use this to insert more filesystem
-- types into the system.
--
-- A filesystem type has to implement one method for each function in the
-- filesystem API, with the exception of mounting-related functions and `combine`,
-- as well as a `new` method that is called with the process, the source device,
-- and the options table (if present). Paths passed to these methods (outside
-- `new`) take a relative path to the mountpoint, NOT the absolute path.
filesystems = {
craftos = {
meta = {
meta = {
type = "directory",
owner = "root",
permissions = {
root = {read = true, write = true, execute = true}
},
worldPermissions = {read = true, write = false, execute = true},
setuser = false
},
contents = {}
},
metapath = "/meta.ltn",
lastDispatch = 0
},
tmpfs = {},
drivefs = {},
tablefs = {},
bind = {},
}
local function getRealPath(process, path)
local p = fs.combine(process.root, path:sub(1, 1) == "/" and "" or process.dir, path)
if "/" .. p .. "/" ~= process.root and p:find(process.root:sub(2), 1, true) ~= 1 then error(path .. ": No such file or directory", 4) end
return p
end
local function getMount(process, path, list)
local fullPath = split(getRealPath(process, path), "/\\")
if #fullPath == 0 then
if list then return mounts[""], path, "" end
return mounts[""][1], path, ""
end
local maxPath
for k in pairs(mounts) do
local ok = true
for i,c in ipairs(split(k, "/\\")) do if fullPath[i] ~= c then ok = false break end end
if ok and (not maxPath or #k > #maxPath) then maxPath = k end
end
if not maxPath then panic("Could not find mount for path " .. path .. ". Where is root?") end
local parts = split(maxPath, "/\\")
local p = #fullPath >= #parts + 1 and fs.combine(table.unpack(fullPath, #parts + 1, #fullPath)) or ""
--syslog.debug(path, #parts, #fullPath, p)
local mounts = mounts[maxPath]
if list then return mounts, p, maxPath end
local mount = mounts[1]
if #mounts > 1 then
for _, v in ipairs(mounts) do
local ok, res = pcall(v.stat, v, process, p, true)
if ok and res then
mount = v
break
end
end
end
return mount, p, maxPath
end
--- Creates a new read file handle over string data.
-- @tparam Process process The process to open as
-- @tparam string data The contents of the file to read
-- @tparam boolean binary Whether to open in binary mode
-- @treturn[1] Handle The new file handle
-- @treturn[2] nil If an error occurred
-- @treturn[2] string An error message describing why the file couldn't be opened
function filesystem.readhandle(process, data, binary)
if data == "" then
local didRead = false
local function read()
if didRead then return nil end
didRead = true
return ""
end
local handle = {
readLine = read,
readAll = read,
read = read,
close = function() end
}
if binary then
function handle.seek() didRead = false end
function handle.read(n) if not n then return nil end return read() end
end
return handle
end
local pos = 1
local closed = false
local t = {
readLine = function(newline)
if closed then error("attempt to use a closed file", 2) end
if pos > #data then return nil end
local d
d, pos = data:match("([^\n]*" .. (newline and "\n?)" or ")\n?") .. "()", pos)
return d
end,
readAll = function()
if closed then error("attempt to use a closed file", 2) end
if pos > #data then return nil end
local d = data:sub(pos)
pos = #d + 1
return d
end,
read = function(n)
if closed then error("attempt to use a closed file", 2) end
if n ~= nil and type(n) ~= "number" then error("bad argument #1 (expected number, got " .. type(n) .. ")", 2) end
n = n or 1
if pos > #data then return nil end
local d = data:sub(pos, pos + n - 1)
pos = pos + n
return d
end,
close = function()
if closed then error("attempt to use a closed file", 2) end
closed = true
end
}
if binary then
t.read = function(n)
if closed then error("attempt to use a closed file", 2) end
if n ~= nil and type(n) ~= "number" then error("bad argument #1 (expected number, got " .. type(n) .. ")", 2) end
if pos > #data then return nil end
if n then
local d = data:sub(pos, pos + n - 1)
pos = pos + n
return d
else
local d = data:byte(pos)
pos = pos + 1
return d
end
end
t.seek = function(whence, offset)
if whence ~= nil and type(whence) ~= "string" then error("bad argument #1 (expected string, got " .. type(whence) .. ")", 2) end
if offset ~= nil and type(offset) ~= "number" then error("bad argument #2 (expected number, got " .. type(offset) .. ")", 2) end
whence = whence or "cur"
offset = offset or 0
if closed then error("attempt to use closed file", 2) end
if whence == "set" then pos = offset + 1
elseif whence == "cur" then pos = pos + offset
elseif whence == "end" then pos = math.max(#data - offset, 1)
else error("Invalid whence", 2) end
return pos - 1
end
else
data = data:gsub("[\x80-\xFF]+", function(s)
local r = ""
if not pcall(function() for _, code in utf8.codes(s) do r = r .. (code < 256 and string.char(code) or "?") end end) then return s end
return r
end)
end
for _, v in pairs(t) do setfenv(v, process.env) debug.protect(v) end
return setmetatable(t, {__name = "file"})
end
--- Creates a new write file handle with a generic writer.
-- @tparam Process process The process to open as
-- @tparam function(data:string,reset:boolean) writer A function to call to write pending data to the file - user mode!
-- If reset is true, data contains the entire file and old contents should be replaced; otherwise, it contains just the new data to append
-- @tparam boolean binary Whether to open in binary mode
-- @treturn[1] Handle The new file handle
-- @treturn[2] nil If an error occurred
-- @treturn[2] string An error message describing why the file couldn't be opened
function filesystem.writehandle(process, writer, binary)
setfenv(writer, process.env)
local function setenv(t)
for _, v in pairs(t) do setfenv(v, process.env) debug.protect(v) end
return setmetatable(t, {__name = "file"})
end
local closed = false
if binary then
local pos = 1
local buf = ""
local partial = ""
return setenv {
write = function(d)
if closed then error("attempt to use a closed file", 2) end
if type(d) == "number" then
buf, pos = buf:sub(1, pos - 1) .. string.char(d) .. buf:sub(pos + 1), pos + 1
if partial then partial = partial .. string.char(d) end
elseif type(d) == "string" then
buf, pos = buf:sub(1, pos - 1) .. d .. buf:sub(pos + #d), pos + #d
if partial then partial = partial .. d end
else error("bad argument #1 (expected string or number, got " .. type(d) .. ")", 2) end
end,
writeLine = function(d)
if closed then error("attempt to use a closed file", 2) end
if type(d) == "number" then
buf, pos = buf:sub(1, pos - 1) .. string.char(d) .. "\n" .. buf:sub(pos + 2), pos + 2
if partial then partial = partial .. string.char(d) .. "\n" end
elseif type(d) == "string" then
buf, pos = buf:sub(1, pos - 1) .. d .. "\n" .. buf:sub(pos + #d + 1), pos + #d + 1
if partial then partial = partial .. d .. "\n" end
else error("bad argument #1 (expected string or number, got " .. type(d) .. ")", 2) end
end,
seek = function(whence, offset)
if whence ~= nil and type(whence) ~= "string" then error("bad argument #1 (expected string, got " .. type(whence) .. ")", 2) end
if offset ~= nil and type(offset) ~= "number" then error("bad argument #2 (expected number, got " .. type(offset) .. ")", 2) end
whence = whence or "cur"
offset = offset or 0
if closed then error("attempt to use closed file", 2) end
local oldp = pos
if whence == "set" then pos = offset + 1
elseif whence == "cur" then pos = pos + offset
elseif whence == "end" then pos = math.max(#buf - offset, 1)
else error("Invalid whence", 2) end
if oldp ~= pos then partial = nil end
return pos - 1
end,
flush = function()
if closed then error("attempt to use a closed file", 2) end
if partial then writer(partial, false)
else writer(buf, true) end
partial = ""
end,
close = function()
if closed then error("attempt to use a closed file", 2) end
closed = true
if partial then writer(partial, false)
else writer(buf, true) end
partial = ""
end
}
else
local buf = ""
return setenv {
write = function(d)
if closed then error("attempt to use a closed file", 2) end
buf = buf .. tostring(d)
end,
writeLine = function(d)
if closed then error("attempt to use a closed file", 2) end
buf = buf .. tostring(d) .. "\n"
end,
flush = function()
if closed then error("attempt to use a closed file", 2) end
writer(buf, false)
buf = ""
end,
close = function()
if closed then error("attempt to use a closed file", 2) end
writer(buf, false)
buf = ""
closed = true
end
}
end
end
--- Creates a new file handle for a generic FIFO object.
-- @tparam Process process The process to open as
-- @tparam {data=string} obj A handle for the shared FIFO data
-- @tparam string mode The mode to open in
-- @treturn[1] Handle The new file handle
-- @treturn[2] nil If an error occurred
-- @treturn[2] string An error message describing why the file couldn't be opened
function filesystem.fifohandle(process, obj, mode)
local closed = false
local function setenv(t)
for _, v in pairs(t) do setfenv(v, process.env) debug.protect(v) end
return setmetatable(t, {__name = "file"})
end
if mode == "r" then
return setenv {
readLine = function(newline)
if closed then error("attempt to use a closed file", 2) end
if #obj.data == 0 then return nil end
local d
d, obj.data = obj.data:match("([^\n]*" .. (newline and "\n?)" or ")\n?") .. "(.*)")
return d
end,
readAll = function()
if closed then error("attempt to use a closed file", 2) end
if #obj.data == 0 then return nil end
local d = obj.data
obj.data = ""
return d
end,
read = function(n)
if closed then error("attempt to use a closed file", 2) end
if n ~= nil and type(n) ~= "number" then error("bad argument #1 (expected number, got " .. type(n) .. ")", 2) end
n = n or 1
if #obj.data == 0 then return nil end
local d = obj.data:sub(1, n)
obj.data = obj.data:sub(n + 1)
return d
end,
close = function()
if closed then error("attempt to use a closed file", 2) end
closed = true
end
}
elseif mode == "w" or mode == "a" then
local buf = obj.data
return setenv {
write = function(d)
if closed then error("attempt to use a closed file", 2) end
buf = buf .. tostring(d)
end,
writeLine = function(d)
if closed then error("attempt to use a closed file", 2) end
buf = buf .. tostring(d) .. "\n"
end,
flush = function()
if closed then error("attempt to use a closed file", 2) end
obj.data = buf
end,
close = function()
if closed then error("attempt to use a closed file", 2) end
obj.data = buf
closed = true
end
}
elseif mode == "rb" then
return setenv {
readLine = function(newline)
if closed then error("attempt to use a closed file", 2) end
if #obj.data == 0 then return nil end
local d
d, obj.data = obj.data:match("([^\n]*" .. (newline and "\n?)" or ")\n?") .. "(.*)")
return d
end,
readAll = function()
if closed then error("attempt to use a closed file", 2) end
if #obj.data == 0 then return nil end
local d = obj.data
obj.data = ""
return d
end,
read = function(n)
if closed then error("attempt to use a closed file", 2) end
if n ~= nil and type(n) ~= "number" then error("bad argument #1 (expected number, got " .. type(n) .. ")", 2) end
if #obj.data == 0 then return nil end
if n then
local d = obj.data:sub(1, n)
obj.data = obj.data:sub(n + 1)
return d
else
local d = obj.data:byte()
obj.data = obj.data:sub(2)
return d
end
end,
seek = function(whence, offset)
if whence ~= nil and type(whence) ~= "string" then error("bad argument #1 (expected string, got " .. type(whence) .. ")", 2) end
if offset ~= nil and type(offset) ~= "number" then error("bad argument #2 (expected number, got " .. type(offset) .. ")", 2) end
if closed then error("attempt to use closed file", 2) end
return 0
end,
close = function()
if closed then error("attempt to use a closed file", 2) end
closed = true
end
}
elseif mode == "wb" or mode == "ab" then
local buf = obj.data
return setenv {
write = function(d)
if closed then error("attempt to use a closed file", 2) end
if type(d) == "number" then buf = buf .. string.char(d)
elseif type(d) == "string" then buf = buf .. d
else error("bad argument #1 (expected string or number, got " .. type(d) .. ")", 2) end
end,
writeLine = function(d)
if closed then error("attempt to use a closed file", 2) end
if type(d) == "number" then buf = buf .. string.char(d) .. "\n"
elseif type(d) == "string" then buf = buf .. d .. "\n"
else error("bad argument #1 (expected string or number, got " .. type(d) .. ")", 2) end
end,
seek = function(whence, offset)
if whence ~= nil and type(whence) ~= "string" then error("bad argument #1 (expected string, got " .. type(whence) .. ")", 2) end
if offset ~= nil and type(offset) ~= "number" then error("bad argument #2 (expected number, got " .. type(offset) .. ")", 2) end
if closed then error("attempt to use closed file", 2) end
return #obj.data + #buf
end,
flush = function()
if closed then error("attempt to use a closed file", 2) end
obj.data = buf
end,
close = function()
if closed then error("attempt to use a closed file", 2) end
obj.data = buf
closed = true
end
}
else return nil, "Invalid mode" end
end
filesystem.openfifo = filesystem.fifohandle -- compatibility
-- craftos fs implementation
do
local file = fs.open("/meta.ltn", "r")
if file then
filesystems.craftos.meta = unserialize(file.readAll()) or filesystems.craftos.meta
filesystems.craftos.lastDispatch = os.epoch "utc"
file.close()
end
end
shutdownHooks[#shutdownHooks+1] = function()
syslog.log("Syncing filesystem")
local file = fs.open(filesystems.craftos.metapath, "w")
if file then
file.write(serialize(filesystems.craftos.meta, {compact = true}))
file.close()
end
end
if args.fsmeta then
local file = fs.open(args.fsmeta, "r")
if file then
local meta = unserialize(file.readAll())
file.close()
if meta then
local function merge(src, dest)
for k, v in pairs(src) do
if dest[k] and type(dest[k]) == "table" and type(v) == "table" then merge(v, dest[k])
else dest[k] = v end
end
end
merge(meta, filesystems.craftos.meta)
end
end
end
function filesystems.craftos:getmeta(user, path, nolink)
local stack = {}
local t = self.meta
local parts = split(path, "/\\")
for i, p in ipairs(parts) do
if p == ".." then
t = table.remove(stack)
if not t then return nil end
elseif not p:match "^%.*$" then
if not t then return nil
elseif t.meta.type ~= "directory" then error("Not a directory", 2)
elseif t.meta.permissions[user] then if not t.meta.permissions[user].execute then error("Permission denied", 2) end
elseif not t.meta.worldPermissions.execute then error("Permission denied", 2) end
stack[#stack+1] = t
t = t.contents[p]
if t and t.meta.type == "link" and not nolink then
local link = filesystem.combine(t.meta.link, table.unpack(parts, i + 1))
if fs.combine(link) == fs.combine(path) then error("Loop in link", 2) end
error {link = true, path = link, orig = path}
end
end
end
return t and t.meta
end
function filesystems.craftos:setmeta(user, path, meta, nolink)
local stack = {}
local t = self.meta
local name
local parts = split(path, "/\\")
for i, p in ipairs(parts) do
if p == ".." then
t = table.remove(stack)
if not t then error("Not a directory", 2) end
elseif not p:match "^%.*$" then
if t.meta.type ~= "directory" then error("Not a directory", 2)
elseif t.meta.permissions[user] then if not t.meta.permissions[user].execute then error("Permission denied", 2) end
elseif not t.meta.worldPermissions.execute then error("Permission denied", 2) end
if not t.contents[p] then t.contents[p] = { -- Initialize with default directory meta if not present
meta = { -- This means the directory was created on-disk but the metadata was never set
type = "directory", -- We'll set it properly at the end (or something)
owner = t.meta.owner or "root",
permissions = {
root = {read = true, write = true, execute = true}
},
worldPermissions = {read = true, write = false, execute = true},
setuser = false
},
contents = {}
} end
stack[#stack+1] = t
t = t.contents[p]
name = p
if t and t.meta.type == "link" and not nolink then
local link = filesystem.combine(t.meta.link, table.unpack(parts, i + 1))
if fs.combine(link) == fs.combine(path) then error("Loop in link", 2) end
error {link = true, path = link, orig = path}
end
end
end
if meta ~= nil then
t.meta = {
type = meta.type,
owner = meta.owner,
permissions = deepcopy(meta.permissions),
worldPermissions = deepcopy(meta.worldPermissions),
setuser = meta.setuser,
link = meta.link
}
if meta.type ~= "directory" then t.contents = nil end
else stack[#stack].contents[name] = nil end
if os.epoch "utc" - self.lastDispatch > 1000 then
local file = assert(fs.open(self.metapath, "w"))
file.write(serialize(self.meta, {compact = true}))
file.close()
self.lastDispatch = os.epoch "utc"
end
end
function filesystems.craftos:new(process, path, options)
expect.field(options, "ro", "boolean", "nil")
-- CraftOS mounts will always require root
if process.user ~= "root" then error("Could not mount " .. path .. ": Permission denied", 3)
elseif not fs.isDir(path) then error("Could not mount " .. path .. ": No such directory", 3) end
return setmetatable({
path = path,
readOnly = options.ro
}, {__index = self})
end
function filesystems.craftos:open(process, path, mode)
local ok, stat = pcall(self.stat, self, process, path)
if not ok then
if type(stat) == "table" then error(stat) end
return nil, stat
elseif not stat then
if mode:sub(1, 1) == "w" or mode:sub(1, 1) == "a" then
if self.readOnly then return nil, "Read-only filesystem" end
local pok, pstat = pcall(self.stat, self, process, fs.getDir(path))
if not pok or not pstat then
if type(pstat) == "table" then error(pstat) end
local mok, err = pcall(self.mkdir, self, process, fs.getDir(path))
if not mok then
if type(err) == "table" then error(err) end
return nil, err:gsub("kernel:%d: ", "")
end
pstat = self:stat(process, fs.getDir(path))
if not pstat then return nil, "Could not stat " .. fs.getDir(path) end
end
if process.user ~= "root" then
local perms = pstat.permissions[process.user] or pstat.worldPermissions
if not perms.write then return nil, "Permission denied" end
end
local meta = {
type = "file",
owner = process.user,
permissions = deepcopy(pstat.permissions),
worldPermissions = deepcopy(pstat.worldPermissions),
setuser = false
}
-- We do a swap here so it doesn't break if pstat.owner == process.user
if pstat.owner then
local t = meta.permissions[pstat.owner]
meta.permissions[pstat.owner] = nil
meta.permissions[process.user] = t
end
self:setmeta(process.user, fs.combine(self.path, path), meta)
local file, err = fs.open(fs.combine(self.path, path), mode)
if not file then return file, err end
return setmetatable(file, {__name = "file"})
else return nil, "File not found" end
elseif stat.type == "directory" then return nil, "Is a directory" end
local perms = stat.permissions[process.user] or stat.worldPermissions
--syslog.debug(path, mode, perms.read, perms.write, perms.execute)
if process.user ~= "root" and ((mode:sub(1, 1) == "r" and not perms.read) or ((mode:sub(1, 1) == "w" or mode:sub(1, 1) == "a") and not perms.write)) then return nil, "Permission denied" end
if stat.type == "fifo" then
local meta = self:getmeta(process.user, fs.combine(self.path, path))
local fifo = fifos[meta]
if not fifo then
fifo = {data = ""}
fifos[meta] = fifo
end
return filesystem.fifohandle(process, fifo, mode)
end
local file, err = fs.open(fs.combine(self.path, path), mode)
if not file then return nil, err end
return setmetatable(file, {__name = "file"})
end
function filesystems.craftos:list(process, path)
local stat = self:stat(process, path)
if not stat or stat.type ~= "directory" then error(path .. ": Not a directory", 2) end
if process.user ~= "root" then
local perms = stat.permissions[process.user] or stat.worldPermissions
if not perms.read then error(path .. ": Permission denied", 2) end
end
return fs.list(fs.combine(self.path, path))
end
-- TODO: Block access when a parent directory isn't readable
function filesystems.craftos:stat(process, path, nolink)
local p = fs.combine(self.path, path)
if p:find(self.path:gsub("^/", ""):gsub("/$", ""), 1, false) ~= 1 then return nil end
local ok, attr = pcall(fs.attributes, p)
if not ok or not attr then return nil end
attr.type = attr.isDir and "directory" or "file"
attr.special = {}
attr.isDir = nil
if not attr.modified then attr.modified = attr.modification end
attr.modification = nil
attr.capacity = fs.getCapacity(p) or 0
attr.freeSpace = fs.getFreeSpace(p)
local ro = attr.isReadOnly
attr.isReadOnly = nil
local meta = self:getmeta(process.user, fs.combine(self.path, path), nolink)
-- The path may exist on the filesystem but have no metadata.
if meta then
attr.owner = meta.owner
attr.permissions = deepcopy(meta.permissions)
attr.worldPermissions = deepcopy(meta.worldPermissions)
attr.type = meta.type or attr.type
attr.setuser = meta.setuser
attr.link = meta.link
else
attr.owner = "root" -- all files are root-owned by default
attr.permissions = {
root = {read = true, write = true, execute = true}
}
attr.worldPermissions = {read = true, write = false, execute = true}
attr.setuser = false
end
if ro then
attr.worldPermissions.write = false
for _, v in pairs(attr.permissions) do v.write = false end
end
return attr
end
function filesystems.craftos:remove(process, path)
if self.readOnly then error(path .. ": Read-only filesystem", 2) end
local stat = self:stat(process, path, true)
if not stat then return end
local function checkWriteRecursive(p)
local s = self:stat(process, p, true)
local perms = s.permissions[process.user] or s.worldPermissions
if process.user ~= "root" and not perms.write then error(p .. ": Permission denied", 3) end
if s.type == "directory" then
if process.user ~= "root" and not perms.read then error(p .. ": Permission denied", 3) end
for _, v in ipairs(fs.list(fs.combine(self.path, p))) do checkWriteRecursive(fs.combine(p, v)) end
end
end
checkWriteRecursive(path)
fs.delete(fs.combine(self.path, path))
self:setmeta(process.user, fs.combine(self.path, path), nil, true)
end
function filesystems.craftos:rename(process, from, to)
if self.readOnly then error("Read-only filesystem", 2) end
local fromstat = self:stat(process, from, true)
local tostat = self:stat(process, to, true)
if not fromstat then error(from .. ": No such file or directory", 2)
elseif tostat then error(to .. ": " .. tostat.type:gsub("%w", string.upper, 1) .. " already exists", 2) end
tostat = self:stat(process, fs.getDir(to))
if not tostat then
self:mkdir(process, fs.getDir(to))
tostat = self:stat(process, fs.getDir(to))
end
if process.user ~= "root" then
local perms = tostat.permissions[process.user] or tostat.worldPermissions
if not perms.write then error(to .. ": Permission denied", 2) end
end
fs.move(fs.combine(self.path, from), fs.combine(self.path, to))
self:setmeta(process.user, fs.combine(self.path, to), self:getmeta(process.user, fs.combine(self.path, from), true), true)
self:setmeta(process.user, fs.combine(self.path, from), nil, true)
end
function filesystems.craftos:mkdir(process, path)
if self.readOnly then error(path .. ": Read-only filesystem", 2) end
local stat = self:stat(process, path)
if stat then
if stat.type == "directory" then return
else error(path .. ": File already exists", 2) end
end
local parts = split(path, "/\\")
local i = #parts
repeat
i=i-1
stat = self:stat(process, table.concat(parts, "/", 1, i))
if stat then
if stat.type == "directory" then break
else error(path .. ": File already exists", 2) end
end
until stat or i <= 0
if not stat then
if path:match "^/" then stat = assert(self:stat(process, "/"))
else stat = assert(filesystem.stat(process, process.dir)) end
end
if process.user ~= "root" then
local perms = stat.permissions[process.user] or stat.worldPermissions
if not perms.write then error(path .. ": Permission denied", 2) end
end
local meta = {
type = "directory",
owner = process.user,
permissions = deepcopy(stat.permissions),
worldPermissions = deepcopy(stat.worldPermissions)
}
if stat.owner then
local t = meta.permissions[stat.owner]
meta.permissions[stat.owner] = nil
meta.permissions[process.user] = t
end
i=i+1
while i <= #parts do
self:setmeta(process.user, fs.combine(self.path, table.concat(parts, "/", 1, i)), deepcopy(meta))
i=i+1
end
fs.makeDir(fs.combine(self.path, path))
end
function filesystems.craftos:link(process, path, location)
local stat = self:stat(process, path, true)
if stat then error(path .. ": File exists", 2) end
self:setmeta(process.user, fs.combine(self.path, path), nil, true)
assert(self:open(process, path, "w")).close()
local meta = self:getmeta(process.user, fs.combine(self.path, path), true)
meta.type, meta.link = "link", location
self:setmeta(process.user, fs.combine(self.path, path), meta, true)
end
function filesystems.craftos:mkfifo(process, path)
local stat = self:stat(process, path)
if stat then error(path .. ": File exists", 2) end
assert(self:open(process, path, "w")).close()
local meta = self:getmeta(process.user, fs.combine(self.path, path), true)
meta.type = "fifo"
self:setmeta(process.user, fs.combine(self.path, path), meta, true)
end
function filesystems.craftos:chmod(process, path, user, mode)
if self.readOnly then error(path .. ": Read-only filesystem", 2) end
local stat = self:stat(process, path, true)
if not stat then error(path .. ": No such file or directory", 2) end
if not stat.owner or (process.user ~= "root" and process.user ~= stat.owner) then error(path .. ": Permission denied", 2) end
local perms
if user == nil then perms = stat.worldPermissions
else
perms = stat.permissions[user]
if not perms then
perms = deepcopy(stat.worldPermissions)
stat.permissions[user] = perms
end
end
if type(mode) == "string" then
if mode:match "^[%+%-=][rwxs]+$" then
local m = mode:sub(1, 1)
local t = {}
for c in mode:gmatch("[rwxs]") do
if c == "r" then t.read = true
elseif c == "w" then t.write = true
elseif c == "s" then t.setuser = true
else t.execute = true end
end
if m == "+" then
if t.read then perms.read = true end
if t.write then perms.write = true end
if t.execute then perms.execute = true end
if t.setuser then stat.setuser = true end
elseif m == "-" then
if t.read then perms.read = false end
if t.write then perms.write = false end
if t.execute then perms.execute = false end
if t.setuser then stat.setuser = false end
else
perms.read = t.read or false
perms.write = t.write or false
perms.execute = t.execute or false
stat.setuser = t.setuser or false
end
else
perms.read = mode:sub(1, 1) ~= "-"
perms.write = mode:sub(2, 2) ~= "-"
perms.execute = mode:sub(3, 3) ~= "-"
stat.setuser = mode:sub(3, 3) == "s"
end
elseif type(mode) == "number" then
stat.setuser = bit32.btest(mode, 8)
perms.read = bit32.btest(mode, 4)
perms.write = bit32.btest(mode, 2)
perms.execute = bit32.btest(mode, 1)
else
if mode.read ~= nil then perms.read = mode.read end
if mode.write ~= nil then perms.write = mode.write end
if mode.execute ~= nil then perms.execute = mode.execute end
if mode.setuser ~= nil then stat.setuser = mode.setuser end
end
self:setmeta(process.user, fs.combine(self.path, path), deepcopy(stat), true)
end
function filesystems.craftos:chown(process, path, owner)
if self.readOnly then error(path .. ": Read-only filesystem", 2) end
local stat = self:stat(process, path, true)
if not stat then error(path .. ": No such file or directory", 2) end
if not stat.owner or (process.user ~= "root" and process.user ~= stat.owner) then error(path .. ": Permission denied", 2) end
stat.owner = owner
stat.setuser = false
self:setmeta(process.user, fs.combine(self.path, path), deepcopy(stat), true)
end
function filesystems.craftos:info()
return "craftos", self.path, {ro = self.readOnly}
end
-- tmpfs implementation
-- tmpfs stores data in the same format as craftos meta, but with the addition of storing file data in .data
function filesystems.tmpfs:getpath(user, path, nolink)
local t = self
local parts = split(path, "/\\")
for i, p in ipairs(parts) do
if not t then return nil
elseif t.type ~= "directory" then error("Not a directory", 2)
elseif t.permissions[user] then if not t.permissions[user].execute then error("Permission denied", 2) end
elseif not t.worldPermissions.execute then error("Permission denied", 2) end
t = t.contents[p]
if t and t.type == "link" and not (nolink and i == #parts) then error {link = true, path = filesystem.combine(t.link, table.unpack(parts, i + 1)), orig = path} end
end
return t
end
function filesystems.tmpfs:setpath(user, path, data, nolink)
local t = self
local e = split(path, "/\\")
local last = e[#e]
e[#e] = nil
for i, p in ipairs(e) do
if t.type ~= "directory" then error("Not a directory", 2)
elseif t.permissions[user] then if not t.permissions[user].execute then error("Permission denied", 2) end
elseif not t.worldPermissions.execute then error("Permission denied", 2) end
if not t.contents[p] then t.contents[p] = { -- Initialize with default directory meta if not present
type = "directory",
owner = t.owner,
permissions = deepcopy(t.permissions),
worldPermissions = deepcopy(t.worldPermissions),
setuser = false,
created = os.epoch "utc",
modified = os.epoch "utc",
contents = {}
} end
t = t.contents[p]
if t and t.type == "link" then error {link = true, path = filesystem.combine(t.link, table.unpack(e, i + 1)), orig = path} end
end
if t.type ~= "directory" then error("Not a directory", 2)
elseif user ~= "root" then
if t.permissions[user] then if not t.permissions[user].execute then error("Permission denied", 2) end
elseif not t.worldPermissions.execute then error("Permission denied", 2) end
end
if not nolink and t.contents[last] and t.contents[last].type == "link" then error {link = true, path = t.contents[last].link, orig = path} end
t.contents[last] = data
end
function filesystems.tmpfs:new(process, src, options)
return setmetatable({
type = "directory",
owner = process.user,
permissions = {
[process.user] = {read = true, write = true, execute = true}
},
worldPermissions = {read = true, write = false, execute = true},
setuser = false,
created = os.epoch "utc",
modified = os.epoch "utc",
contents = {}
}, {__index = self})
end
function filesystems.tmpfs:_open_internal(process, path, mode)
local epoch = os.epoch
local data = self:getpath(process.user, path)
if not data then return nil, "No such file" end
if mode == "r" or mode == "rb" then return filesystem.readhandle(process, data.data, mode == "rb")
elseif mode == "w" or mode == "wb" then
data.data = ""
data.modified = epoch "utc"
return filesystem.writehandle(process, function(buf, full)
if full then data.data = buf else data.data = data.data .. buf end
data.modified = epoch "utc"
if self.__flush then self:__flush() end
end, mode == "wb")
elseif mode == "a" or mode == "ab" then
local orig = data.data
return filesystem.writehandle(process, function(buf, full)
if full then data.data = orig .. buf else data.data = data.data .. buf end
data.modified = epoch "utc"
if self.__flush then self:__flush() end
end, mode == "ab")
else return nil, "Invalid mode" end
end
function filesystems.tmpfs:open(process, path, mode)
if self.readOnly and (mode:sub(1, 1) == "w" or mode:sub(1, 1) == "a") then return nil, "Read-only filesystem" end
local ok, stat = pcall(self.stat, self, process, path)
if not ok then
if type(stat) == "table" then error(stat) end
return nil, stat
elseif not stat then
if mode:sub(1, 1) == "w" or mode:sub(1, 1) == "a" then
local pok, pstat = pcall(self.stat, self, process, fs.getDir(path))
if not pok or not pstat then
if type(pstat) == "table" then error(pstat) end
local mok, err = pcall(self.mkdir, self, process, fs.getDir(path))
if not mok then
if type(err) == "table" then error(err) end
return nil, err:gsub("kernel:%d: ", "")
end
pstat = self:stat(process, fs.getDir(path))
end
if process.user ~= "root" then
local perms = pstat.permissions[process.user] or pstat.worldPermissions
if not perms.write then return nil, "Permission denied" end
end
local meta = {
type = "file",
owner = process.user,
permissions = deepcopy(pstat.permissions),
worldPermissions = deepcopy(pstat.worldPermissions),
setuser = false,
created = os.epoch "utc",
modified = os.epoch "utc",
data = ""
}
-- We do a swap here so it doesn't break if pstat.owner == process.user
local t = meta.permissions[pstat.owner]
meta.permissions[pstat.owner] = nil
meta.permissions[process.user] = t
self:setpath(process.user, path, meta)
return self:_open_internal(process, path, mode)
else return nil, "File not found" end
elseif stat.type == "directory" then return nil, "Is a directory" end
if process.user ~= "root" then
local perms = stat.permissions[process.user] or stat.worldPermissions
--syslog.debug(path, mode, perms.read, perms.write, perms.execute)
if (mode:sub(1, 1) == "r" and not perms.read) or ((mode:sub(1, 1) == "w" or mode:sub(1, 1) == "a") and not perms.write) then return nil, "Permission denied" end
end