/
start
1277 lines (1083 loc) · 43.6 KB
/
start
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
---Config [temporary, for the beta]
local rootLocations = { -- Put any additional locations for blocks (e.x. "disk","disk1") here. Upon full release, adding disks will be through an FS call.
"rootFS", -- do remove this one
"disk",
}
local dev = false
--if dev then
local log = {} -- dev feature, makes my life easier if not local.
--end
--enable once done making startup fileV
--multishell = nil -- your stupid multishell(okay, it's fine and all, but it uses the window API...)
--term.redirect(term.native()) -- your slow window API
local cfs = {}--_G.fs -- create a reference to the current filesystem
for i,o in pairs(fs) do
cfs[i] = o
end
--TODO: Symlinks, users & groups, permissions, and some error logs and ext2al error logs, AND STANDARDIZE THE INDENTATION
--Implementation of ext2CC. Note that we dont
--want to just go passing this arround. Pass
--arround a wrapper to this so that this itself
--cannot ever see unwanted modification.
--[[
The allmighty inode
Some random notes:
*2 is always the directory for /
The stuff in there and what it means
fileSize - The size of the file we're talking about here. Total size.
blocks - The blocks this file is on
blocksSize - A ext2CC exclusive, real-world filesystems will have a standard size for every block. The reason this exists in ext2CC is because of the lack of a standardize block size(if I could've used a single file and jumped arround to byte numbers, I would've had a standard block size. But, with the way it is currently, it's pointless.)
]]
local function subOutFirstDir(_path)
----Input: String- the path that the first directory shall be extracted from
----Output: String- the first directory with no / after it, String- the rest of the path with no / prefix
--if there is a / in the front of this directory then we can just return /. CC's default filesystem does not work like this, however, we aren't CC's default filesystem.
if string.sub(_path,1,1) == "/" then -- fs.combine removes a / at the first part of the directory. easiest to just handle / on its own.
return "/",string.sub(_path,2,#_path)
end
_path=cfs.combine(_path,"")--get rid of any BS
local spot = string.find(_path,"/")
if not spot then
return _path
end
return string.sub(_path,1,spot-1),string.sub(_path,spot+1,#_path)
end
local function charTableToString(_chartable)
local str = ""
for i=1,table.maxn(_chartable) do
str = str..string.char(_chartable[i])
end
return str
end
local function stringToCharTable(_string)
local char = {}
for i=1,#_string do
char[i] = string.byte(string.sub(_string,i,i))
end
return char
end
local function getRealFileSize(_file)
local file = cfs.open(_file,"r")
if not file then
return 0
end
local len = #file.readAll()
file.close()
return len
end
local ext2CC = { --Takes inspiration from EXT2, but is different in many ways. Its most prominent ext2 feature would be the inode table, present on all unix-style filesytems.
new = function(s,_locations)
----Input: String location, Number block size
----Output: Me
assert(tostring(_location),"ExCC.new - #1 must be String")
--assert(cfs.exists(iLocation),"ExCC.new - #1 must exist on filesystem")
local self = {}
setmetatable(self,{__index=s})
self.locations = _locations
--open inode
--print(self.location)
local file = cfs.open(cfs.combine(self.locations[1],"inode"),"r") -- first location should always have the inode
--print(file)
local fInode = file.readAll()
file.close()
fInode = textutils.unserialize(fInode)
if not fInode then
printError("Filesytem is corrupt (missing or invalid inode table)")
end
self.inode = fInode[1] --whoever made lua's tables start at 1, I sware to
self.blockInfo = fInode[2]
return self
end,
writeToBlock = function(self,_blocknum,_ammount,_what,__startAt) --maybe move this and its following function to be outside and local?
----Input: Number- The block number
--Number ammount - Ammount to write
--Table of Numbers - Binary of what to write
--to the block,
----Output: Number - 0 for success, 1 for
--out of space 2 for insufficient
--permissions 3 for file not found,
--Table of Numbers - what is
--left that couldnt fit
--~Notes:
--Does NOT allocate a block for you.
--Doesn't check anything about the block. It just writes to it, no questions asked.
local realpath = self.blockInfo.blockLocations[_blocknum]
if not realpath then
return 3
end
local file = cfs.open(realpath,"wb")
for i=1,_ammount do
file.write(_what[i+(_startAt or 0)])
if i % 100000 == 0 then -- if dividing 10000 by i will have no remainder then yield
os.queueEvent"oneYield"
os.pullEvent"oneYield"
end
end
--[[for o=1,_ammount do -- remove chars weve gone over
table.remove(_what,1)
end]]
file.close()
return 0
end,
readFromBlock = function(self,_blocknum,_ammount)
----Input: Number- The block number
--Number - Ammount of bytes to read
----Output: Number - 0 for success,
--2 for insufficient
--permissions, 3 for file not found
--Table of Numbers - what is
local file = cfs.open(self.blockInfo.blockLocations[_blocknum] or "","rb")
if not file then --file not found
return 3
end
local contents = {}
for i=1,_ammount do
contents[i] = file.read()
if contents[i] == nil then
break
end
end
file.close()
--print(tostring(_ammount)..tostring(contents))sleep(2)
return 0,contents
end,
allocateBlock = function(self)
----Input: Nothing
----Output: A free block, if there is one.
for i=1,#self.blockInfo.freeBlocks do
if cfs.getFreeSpace(self.blockInfo.blockLocations[self.blockInfo.freeBlocks[i]]) > 3000 then
return table.remove(self.blockInfo.freeBlocks,i) --note that table.remove returns what was removed.
end
end
return false
end,
getDirectory = function(self,_fakePath)
_fakePath = cfs.combine(_fakePath,"")
if _fakePath == "" then
return 0,2
end
local err,folderInode = self:locateInInode(_fakePath)
if err ~= 0 then
-- print"ExtErr"
return err --pass on the error
elseif (self.inode[(folderInode or -1)] or {}).type ~= 1 then
-- print"intErr"
return 4
end
return 0,folderInode
end,
listInDirectory_wInode = function(self,_folderInode)
----Input: String- the "fake" path of the directory
----Output: Table-The directory table, which is formatted: filename:inode#
log[#log+1] = "folderInode: "..tostring(_folderInode)
local err,directory = self:read_wInode(_folderInode)
log[#log+1]="alive"
if err ~= 0 then
return er
end
directory = charTableToString(directory)
log[#log+1] = directory
directory = textutils.unserialize(directory)
return 0,directory
end,
locateInInode = function(self,_fakePath)
----Input: String- the "fake" path of the file
----Output: Number - You know the drill here see readFromBlock if you don't--but this adds 4, which is using file as directory, Table - The inode for this file
_fakePath = cfs.combine(_fakePath,"")
local _,iLimiter = string.gsub(_fakePath,"/","")
local currentFile,currentInode,currentDirTable,isUsingFileAsDir
currentInode = 2 --Root is always 2. Otherwise, we would never be able to find anything
for i=1,iLimiter+2 do
local dircontents = ""
if not self.inode[currentInode] then
return 3
end
for i=1,#self.inode[currentInode].blocks do -- almost always runs just once unless a rarer case where a folders's split across drives
local ok,charTable = self:readFromBlock(self.inode[currentInode].blocks[i],self.inode[currentInode].blocksSize[i]) -- read the folder's file
dircontents = dircontents..charTableToString(charTable)
end
currentDirTable = textutils.unserialize(dircontents)
if not currentDirTable then --if this directory is empty
currentDirTable = {}
end
if (not currentDirTable[currentFile]) and i ~= 1 then
return 3
end
currentInode = currentDirTable[currentFile] or currentInode
if _fakePath then --We check this because, upon reaching a final, non-directory path, there will be nothing left of _fakePath
currentFile, _fakePath = subOutFirstDir(_fakePath)
if self.inode[currentInode].type ~= 1 then --Since there is stuff left in this directory, this needs to come up as a file.
--print("fa "..self.inode[currentInode].type)
return 4
end
end
--print("f".._fakePath)
end
--print("returning "..textutils.serialize(currentInodeTable))
return 0,currentInode
end,
write_wInode = function(self,_inodeID,_what)
----Input: Number - Inode ID of what to write to.
----Ouput: Number - Error code
local myInode = self.inode[_inodeID]
myInode.fileSize = #_what
myInode.blocks = myInode.blocks or {} -- Make a list of blocks potentially availiable for writing to.
local blocks,hasUsedBlock = {}
if myInode.blocks[1] then--if there's anything in here
table.insert(blocks,myInode.blocks[#myInode.blocks]) -- Add the last block of this current file
hasUsedBlock = true -- Make sure they know that this block doesn't need to be allocated | Maybe redo this to delete everything this file has, add its blocks to freeBlocks, then probably reuse most of them
end
for i=1,#(self.blockInfo.freeBlocks or {}) do
table.insert(blocks,self.blockInfo.freeBlocks[i]) -- Add all the free blocks
end
local left = 0 -- funny story about accidently having this set to 1 here...
for i=1,#blocks do
local freeSpace = cfs.getFreeSpace(self.blockInfo.blockLocations[blocks[i]]) + getRealFileSize(self.blockInfo.blockLocations[blocks[i]]) -- for blocks that where previously occupied but then the file was deleted, and that block is on an almost-full drive.
if (freeSpace > 3000) then
local ammountWrite = math.min(#_what-left,freeSpace-3000)
local err
--print("1 write: "..tostring(ammountWrite))
err = self:writeToBlock(blocks[i],ammountWrite,_what,left)
if err ~= 0 then --If there's an error, pass it on
--print("errCode:"..tostring(err))
return err
end
left = left+ammountWrite
if hasUsedBlock then
myInode.blocksSize[#myInode.blocksSize] = ammountWrite
else
myInode.blocks[#myInode.blocks+1] = blocks[i]
myInode.blocksSize = myInode.blocksSize or {}
myInode.blocksSize[#myInode.blocks] = ammountWrite
end
for f=1,#self.blockInfo.freeBlocks do
if self.blockInfo.freeBlocks[f] == blocks[i] then
table.remove(self.blockInfo.freeBlocks,f)
end
end
if left >= #_what then -- if the _what table has been all written
--print"allDone"
self:writeInode() -- save the inode to disk
return 0
end
hasUsedBlock = false --This will set this to false after the first for loop, because the only block we'll use is the last one.
end
end
for i=1,#self.locations do
if cfs.getFreeSpace(self.locations[i]) > 3000 then
--print"NEWBLOCKS"
for i=1,10 do --make 10 new blocks because this saves time
local newBlockNum = #self.blockInfo.blockLocations+1
self.blockInfo.blockLocations[newBlockNum] = cfs.combine(self.locations[i],"blocks/"..tostring(newBlockNum))
self.blockInfo.freeBlocks[#self.blockInfo.freeBlocks+1] = newBlockNum
end
self:writeInode()
if left > 0 then
if not self:write_wInode(_inodeID,_what) == 1 then
return 0 -- If this worked then we're good
end--write the rest of it
end
end
end
return 1
end,
putInDir = function(self,_fakePath)
local name = cfs.getName(_fakePath)
local fakeDirPath = string.sub(_fakePath,1,#_fakePath-(#name) ) --TODO : switch to fs.getDir ?
log[#log+1] = "dirPath "..tostring(fakeDirPath)
log[#log+1] = "nam "..tostring(name)
local err,dirInode = self:getDirectory(fakeDirPath)
if err ~= 0 then
return err
end
local err,dirList = self:listInDirectory_wInode(dirInode)
log[#log+1]="m"
if err ~= 0 then
return err
end
local inodeID = dirList[cfs.getName(_fakePath)]
--print(name.." : "..tostring(inodeID))
if not inodeID then --need to make one!
inodeID = #self.inode+1
self.inode[inodeID] = {}
dirList[name] = inodeID
--print(textutils.serialize(dirList))
self:write_wInode(dirInode,stringToCharTable(textutils.serialize(dirList)))
--print"d"
end
return inodeID,dirInode
end,
write = function(self,_fakePath,_what,_type) ---Maybe make this return the inode ID?
----Input: String - The "fake" path to the file
--String - The text to write.
----Output: Number - 0 for success,
--1 for out of space
--2 for insufficient perms.
--
local inodeID,dirInode = self:putInDir(_fakePath)
--print(name.." : "..tostring(inodeID))
self.inode[inodeID] = self.inode[inodeID] or {}
self.inode[inodeID].type = _type or 0
--self.inode[inodeID].temp = true --debug to see what inodes this was writing
self.inode[inodeID].hardLinks = self.inode[inodeID].hardLinks or {}
self.inode[inodeID].hardLinks[#self.inode[inodeID].hardLinks+1] = dirInode
--print"B4"
return self:write_wInode(inodeID,_what)
end,
read = function(self,_fakePath,_ammount)
local err,inodeID = self:locateInInode(_fakePath)
if err ~= 0 then --we've come across an error. pass it on down.
return err
end
--print("in "..tostring(inodeID))
return self:read_wInode(inodeID,_ammount)
end,
read_wInode = function(self, _inodeID,_ammount)
----Input: String - the "fake" path to the file
---Number - Ammount of bytes to read
----Output: Table of Numbers - The resulting character codes
local chars,ammountRead = {},0
for i=1,#self.inode[_inodeID].blocks do
local file = cfs.open(self.blockInfo.blockLocations[ self.inode[_inodeID].blocks[i] ],"rb")
if not file then
return 3
end
for i=1,self.inode[_inodeID].blocksSize[i] do
chars[#chars+1] = file.read()
ammountRead=ammountRead+1
if ammountRead >= (_ammount or ammountRead+1) then
file.close()
return 0,chars
end
end
file.close()
end
--sleep(3)
--print(textutils.serialize(chars).." + "..textutils.serialize(self.inode[_inodeID].blocksSize))
return 0,chars
end,
delete_wInode = function(self,inodeID) -- rip --TODO make this only delete it from inode if all hard links are gone.
if inodeID == 2 then--um... no.
return 6
end
--print"del"
if not self.inode[inodeID] then --well
return
end
local hardLinks = self.inode[inodeID].hardLinks
for i,link in pairs(hardLinks) do
--print("h"..tostring(link))
local err,dirTable = self:listInDirectory_wInode(link)
for y,u in pairs(dirTable) do
--print("y: "..tostring(y))
--print("u"..tostring(u))
if u == inodeID then
dirTable[y] = nil
break
end
end
dirTable = stringToCharTable(textutils.serialize(dirTable))
self:write_wInode(link,dirTable)
end
local blocks = self.inode[inodeID].blocks
for i=1,#blocks do
self.blockInfo.freeBlocks[#self.blockInfo.freeBlocks+1] = blocks[i]
end
self.inode[inodeID] = nil
self:writeInode()
return 0
end,
writeInode = function(self) -- TODO : Open in append mode for effeciency
----Input:
----Output:
----Notes:
---Writes the current inode to the inode table file.
local file = cfs.open(cfs.combine(self.locations[1],"inode"),"w")
if not file then
return 3
end
local inodeSerialized =textutils.serialize({self.inode,self.blockInfo})
if not inodeSerialized then
return 5
end
--print("WROTEINODE "..textutils.serialize(inodeSerialized))
file.write(inodeSerialized)
file.close()
return 0
end
}
local rootFS = ext2CC:new(rootLocations)
local function fsAssert(beTrue,errIfIsnt) -- a CC FS-style assert, errors at the program calling the function rather than in this one
----Input: Boolean- the variable to be checked, String- the error if it's false
----Output: An error message if it's false
if not beTrue then
error(errIfIsnt,4)
end
end
local tEmpty = {} -- A variable for fs.complete
local filesys = rootFS --implement other fses later
local fs = {
open = function(_fakePath,_mode)
----Input: String The "fake" path, String the mode to use
----Output: Your typical CC-style open table, if no errors occured.
----Notes: The file systems only support writing and reading in char tables, but conversion is easy and functions are provided to do it.
----Close() doesn't really do anything for read modes except throw an error if you try to read more. I'd still recommend calling it in case a different filesystem works differently
---TODO: Add support for different file systems, don't allow them to open a directory, check for permissions, check for symlinks. An implied requirement of this is to use the inode # rather than string when read/writing to the filesys
local wrongArgs = "Expected string, string"
local notOpen = "Stream closed"
fsAssert(tostring(_fakePath),wrongArgs) -- These error messages must be 100% identical to the normal ones.
fsAssert(tostring(_mode),wrongArgs)
--Local variables for use inside the returned tables
--local remainingText,remainingChar,currentWriteText,currentWriteChar
if checkIfNotExists then ---implement this
return
end
if _mode == "r" then
local err,char = filesys:read(_fakePath)
--print(err)
--print(textutils.serialize(char))
if not char then
return
end
local remainingText,isOpen,bytesRead = charTableToString(char),true,0
local bytesToRead = #remainingText
return {
readLine = function()
fsAssert(isOpen,notOpen)
local area = (string.find(remainingText,"\n") or #remainingText)
local returnThis = string.sub(remainingText,1,area)
remainingText = string.sub(remainingText,area,#remainingText)
bytesRead = bytesRead+#returnThis
if bytesRead > bytesToRead then
return false
end
return returnThis
end,
readAll = function()
fsAssert(isOpen,notOpen)
local returnThis = remainingText
remainingText = ""
return returnThis
end,
close = function()
isOpen = false
return
end
}
elseif _mode == "w" then
local currentWriteText,isOpen = "",true
return {
write = function(_what)
fsAssert(isOpen,notOpen)
currentWriteText = currentWriteText.._what
end,
writeLine = function(_what)
fsAssert(isOpen,notOpen)
currentWriteText = currentWriteText.._what.."\n"
end,
close = function()
isOpen = false
--print(_fakePath,stringToCharTable(currentWriteText))
local a=filesys:write(_fakePath,stringToCharTable(currentWriteText))
--print("er"..tostring(a))
end,
}
elseif _mode == "a" then
local currentWriteText,isOpen = "",true
return {
write = function(_what)
fsAssert(isOpen,notOpen)
currentWriteText = currentWriteText.._what
end,
writeLine = function(_what)
fsAssert(isOpen,notOpen)
currentWriteText = currentWriteText.._what.."\n"
end,
close = function()
isOpen = false
filesys:write(_fakePath,stringToCharTable(currentWriteText))
end,
flush = function()
filesys:write(_fakePath,stringToCharTable(currentWriteText))
end
}
elseif _mode == "wb" then
local currentWriteChar,isOpen = {},true
return {
write = function(_char)
fsAssert(isOpen,notOpen)
if _char then
currentWriteChar[#currentWriteChar+1] = _char
end
end,
flush = function()
filesys:write(_fakePath,currentWriteChar)
end,
close = function()
isOpen = false
filesys:write(_fakePath,currentWriteChar)
end
}
elseif _mode == "rb" then --easiest one.
local err,remainingChar = filesys:read(_fakePath)
local isOpen = true
return {
read = function()
fsAssert(isOpen,notOpen)
return table.remove(remainingChar,1)
end,
close = function()
isOpen = false
end
}
end
fsAssert(false,"Unsupported mode") -- if we haven't returned anything yet, they're not giving us the right mode.
end,
list = function(_dir)
----Input: String- directory
----Output: Linear table-files in directory
local err,folderInode = rootFS:getDirectory(_dir)
--print(folderInode)
fsAssert(err == 0,"Not a directory")
local err,dirList = filesys:listInDirectory_wInode(folderInode)
fsAssert(err == 0,"Not a directory")
fsAssert(type(dirList)=="table","Not a directory")
local linearDirList = {}
for i,o in pairs(dirList) do --List directory gives us name:inode#. This converts to a linear table.
linearDirList[#linearDirList+1]=i
end
return linearDirList
end,
isDir = function(_dir)
_dir = _dir or ""
--print("isDir ".._dir)
if filesys:getDirectory(_dir) == 0 then
return true
else
return false
end
end,
isReadOnly = function(_file)--TODO
_file = _file or ""
return false
end,
exists = function(_file)
_file = cfs.combine(_file,"")
if _file == "" then --Hey man, does the ground I'm standing on exist?
return true --...uh, yeah?
end
local err,inodeID = filesys:locateInInode(_file)
if inodeID then
return true
else
return false
end
end,
delete = function(_file)
local err,inodeID = rootFS:locateInInode(_file)
if not inodeID then
return
end
if rootFS.inode[inodeID].type == 1 then --TODO
local err,dirList = rootFS:listInDirectory_wInode(inodeID)
if not err then
end
end
local err = rootFS:delete_wInode(inodeID)
if err == 6 then
fsAssert(false,"Access denied")
end
end,
makeDir = function(_file) -- directories are just files
rootFS:write(_file,{123,125},1) --123 = "{", 125 = "}". 1 is type for directory.
end,
getSize = function(_file)
local err,inodeID = rootFS:locateInInode(_file)
if not inodeID then
return
end
return rootFS.inode[inodeID].fileSize
end,
--move = function(_file,_file2) --TODO
}
local romFS = {}
local romFS_meta = {}
romFS_meta.__index = function(tab,key)
--[[
Metatables are called when an operation is being done on the table. This specific operation, __index (so things like myTable[myKey] would result in this function being called with the inputs myTable and myKey as table and key), is called when the table in indexed and no variables are found.
]]
return function(...) ---Note that there is no reason to validate the path because realFS does that for us
local args= {...}
--print("r"..textutils.serialize(args))
--local pos = string.find(args[1],"/")
--args[1] = string.sub(args[1],1,(pos or 4)-1)
return cfs[key](table.unpack(args)) --funny story about the original variable nam for tab and this line...
end
end
setmetatable(romFS,romFS_meta)
local ccFS = {}
local ccFS_meta = {}
ccFS_meta.__index = function(tab,key)
return function(...)
local args= {...}
local pos = string.find(args[1],"/")
args[1] = string.sub(args[1],(pos or 5)+1,#args[1])
return cfs[key](table.unpack(args)) --funny story about the original variable nam for tab and this line...
end
end
setmetatable(ccFS,ccFS_meta)
local fileSystems = { --Mount dir : filesytem
[""] = fs,
["ccFS"] = ccFS,
["rom"] = romFS, -- for compatibility reasons we'll go ahead and mount it at /rom
}
local function getFileSystemAndRealPath(_fakePath) -- figure out which fs to return them. This means that each filesystem needs to re-implement its own "fs" API.
--print"a"
_fakePath = cfs.combine((_fakePath or ""),"")
local _fakeDir = _fakePath
while true do
--[[if _fakeDir == "" then
return rootFS,_fakePath
elseif _fakeDir == "rom" then --redo this later, rom will be mounted to something like /mnt/ccrom
return cfs,_fakePath
elseif _fakeDir == "realworldfs"
end]]
for i,o in pairs(fileSystems) do
if _fakeDir == i then --if this full path is something we have in our filesystem database
if i=="rom" then
--print"rom"
--sleep(0.2)
end
return _fakePath,o --return the fake fs library
end
end
_fakeDir = cfs.getDir(_fakeDir)
if _fakeDir == ".." then -- this shouldn't ever be true
printError"We got problems"
sleep(100)
end
end
end
local realFS = {} --the fs we pass to programs
--------------------------------------------
--THE FOLLOWING ARE SNIPPETS FROM BIOS.LUA--
--------------------------------------------
local function complete( _thefs, sPath, sLocation, bIncludeFiles, bIncludeDirs)
bIncludeFiles = (bIncludeFiles ~= false)
bIncludeDirs = (bIncludeDirs ~= false)
local sDir = sLocation
local nStart = 1
local nSlash = string.find( sPath, "[/\\]", nStart )
if nSlash == 1 then
sDir = ""
nStart = 2
end
local sName
while not sName do
local nSlash = string.find( sPath, "[/\\]", nStart )
if nSlash then
local sPart = string.sub( sPath, nStart, nSlash - 1 )
sDir = cfs.combine( sDir, sPart )
nStart = nSlash + 1
else
sName = string.sub( sPath, nStart )
end
end
if _thefs.isDir( sDir ) then
local tResults = {}
if bIncludeDirs and sPath == "" then
table.insert( tResults, "." )
end
if sDir ~= "" then
if sPath == "" then
table.insert( tResults, (bIncludeDirs and "..") or "../" )
elseif sPath == "." then
table.insert( tResults, (bIncludeDirs and ".") or "./" )
end
end
local tFiles = _thefs.list( sDir )
for n=1,#tFiles do
local sFile = tFiles[n]
if #sFile >= #sName and string.sub( sFile, 1, #sName ) == sName then
local bIsDir = _thefs.isDir( cfs.combine( sDir, sFile ) )
local sResult = string.sub( sFile, #sName + 1 )
if bIsDir then
table.insert( tResults, sResult .. "/" )
if bIncludeDirs and #sResult > 0 then
table.insert( tResults, sResult )
end
else
if bIncludeFiles and #sResult > 0 then
table.insert( tResults, sResult )
end
end
end
end
return tResults
end
return tEmpty
end
---------------------------------
--END OF SNIPPETS FROM BIOS.LUA--
---------------------------------
local function find(_path,wildcard,_mustBePath)
local wildcard,hasOne = string.gsub(wildcard,"*","")
if (not hasOne) or (not fs.isDir(_path)) then
return {cfs.combine(_path,wildcard)}
end
local finds = fs.list(_path)
--print("wildcard is "..wildcard)
local result = {}
for i=1,#finds do --if (not requring to be dir, or is dir) and it meets the wildcard, then
--print(tostring(finds[i]))
if ( (not _mustBePath) or realFS.isDir(finds[i]) ) and string.find(finds[i],wildcard) then
result[#result+1] = cfs.combine(_path,finds[i])
else
--table.remove(finds,i)
end
end
return result
end
local realFS_meta = {}
realFS_meta.__index = function(tab,key)
if true then -- We have functions other than what is in type(cfs[key]) == "function" then -- Everything in fs is a function...
--print(key)
--sleep(0)
return function(...)
local args,filesys= {...}
args[1],filesys = getFileSystemAndRealPath(args[1])
if key == "getDir" or key == "getName" then
return cfs[key](table.unpack(args))
elseif key == "complete" then
args[2],filesys = getFileSystemAndRealPath(args[2])
return complete(filesys,table.unpack(args))
elseif key == "find" then
local totalTable = {}
local curTable = {args[1]}
while true do
local wildcardWPath = string.sub(curTable[1],1,(string.find(curTable[1],"*") or #curTable[1]))
--print"1"
if wildcardWPath == "" then
wildcard = ""
else
wildcard = cfs.getName(wildcardWPath)
end
local path = cfs.getDir(wildcardWPath)--print(curTable[1],1,(string.find(wildcard,"/") or #wildcard)+(#wildcardWPath-#wildcard))
--[[print("Wildcard w path" .. tostring(wildcardWPath))
print("Wildcard " .. tostring(wildcard))
print("path " .. tostring(path))]]
local smallTable = find(path,wildcard,string.find(wildcard,"/"))
for i=1,#smallTable do
local pos = 1
local further = false
--while true do -- validate if this should be taken further
if string.find(smallTable[i],"*") then
--print"aded"
curTable[#curTable+1] = smallTable[i] -- add results to the next input
end
totalTable[#totalTable+1] = smallTable[i] -- add to total output
end
table.remove(curTable,1) -- remove what we've already done
--local spot = string.find(args[1],"/",spot+11)
if not curTable[1] then --spot then
break
end
end
return totalTable
elseif key == "list" then
local list = filesys[key](args[1])
--[[for i,_ in pairs(fileSystems) do
if (cfs.getDir(i) == args[1]) and i ~= "" then
list[#list+1] = i
end
end]]
return list
elseif (not args[2]) or key == "open" then
--print("returning")
return filesys[key](args[1],args[2])
elseif key == "move" then --non-fs.open and has 2 args(move,copy,combine,complete)
elseif key == "copy" then
args[2],filesys2 = getFileSystemAndRealPath(args[2])
local file = filesys.open(args[1],"rb")
local file2 = filesys2.open(args[2],"wb")
local res = true
while res do
res = file.read()
if res then
file2.write(res)
end
end
file.close()
file2.close()
else
--print("strOp")
--sleep(0)
return cfs[key](table.unpack(args))
end
end
else
--print"NAF"
return nil --Everything in fs is a function
end
end
setmetatable(realFS,realFS_meta)
--
local function vt(is,shouldBe) -- verifyTable
if type(is) ~= "table" then --...
return true
end
for i=1,#shouldBe do
if is[i] ~= shouldBe[i] then
return i
end
end
return false
end
----[[
local function bigolunittest() -- shoddy unit tests
--ininiation