-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSVFS.py
More file actions
2316 lines (2152 loc) · 77.9 KB
/
SVFS.py
File metadata and controls
2316 lines (2152 loc) · 77.9 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
# SVFS 2.0 - Simple Virtual File System
# Both simple and powerful virtual file system, written in pure python.
#
# Copyright (C) 2012 Andrew Stolberg
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
# Updated and ported to Python 3 by Van Lindberg, 2025
import os, sys, struct, datetime, time, io, weakref
class SVFS(object):
class SVFSError(Exception):
def __init__(self, code, msg):
self.code = code
self.msg = msg
def __str__(self):
return "[Errno " + str(self.code) + "] " + self.msg
class SVFSIOError(Exception):
def __init__(self, code, msg):
self.code = code
self.msg = msg
def __str__(self):
return "[Errno " + str(self.code) + "] " + self.msg
class Identifier(object):
def __init__(self, idb="VFS2"):
self.idb = idb
def tofile(self, f):
if isinstance(self.idb, str):
self.idb = self.idb.encode("ascii", errors="replace")
if len(self.idb) > 4:
self.idb = self.idb[:4]
if len(self.idb) < 4:
self.idb = self.idb.ljust(4, b"\0")
f.write(self.idb)
return 0
def fromfile(self, f):
self.idb = f.read(4)
return 0
class Metadata(object):
def __init__(self, vnm="", ftl=0, cml=0, csz=1, rev=0):
self.vnm = vnm
self.ftl = ftl
self.cml = cml
self.csz = csz
self.rev = rev
if self.csz < 1:
self.csz = 1
def tofile(self, f):
if isinstance(self.vnm, str):
self.vnm = self.vnm.encode("utf-8")
f.write(struct.pack("<256p", self.vnm))
f.write(struct.pack("<L", self.ftl))
f.write(struct.pack("<L", self.cml))
f.write(struct.pack("<H", self.csz - 1))
f.write(struct.pack("<B", self.rev))
return 0
def fromfile(self, f):
self.vnm = struct.unpack("<256p", f.read(256))[0]
if isinstance(self.vnm, bytes):
self.vnm = self.vnm.decode("utf-8", "replace")
self.ftl = struct.unpack("<L", f.read(4))[0]
self.cml = struct.unpack("<L", f.read(4))[0]
self.csz = struct.unpack("<H", f.read(2))[0] + 1
self.rev = struct.unpack("<B", f.read(1))[0]
return 0
class FTEntry(object):
def __init__(
self, fnm="", typ=0, pnt=0, fcl=0, cts=0.0, ats=0.0, wts=0.0, fsz=0
):
self.fnm = fnm
self.typ = typ
self.pnt = pnt
self.fcl = fcl
self.cts = cts
self.ats = ats
self.wts = wts
self.fsz = fsz
def tofile(self, f):
if isinstance(self.fnm, str):
self.fnm = self.fnm.replace("/", "")
self.fnm = self.fnm.encode("utf-8")
else:
self.fnm = self.fnm.replace(b"/", b"")
f.write(struct.pack("<256p", self.fnm))
f.write(struct.pack("<B", self.typ))
f.write(struct.pack("<L", self.pnt))
f.write(struct.pack("<L", self.fcl))
f.write(struct.pack("<d", self.cts))
f.write(struct.pack("<d", self.ats))
f.write(struct.pack("<d", self.wts))
f.write(struct.pack("<Q", self.fsz))
return 0
def fromfile(self, f):
self.fnm = struct.unpack("<256p", f.read(256))[0]
if isinstance(self.fnm, bytes):
self.fnm = self.fnm.decode("utf-8", "replace")
self.typ = struct.unpack("<B", f.read(1))[0]
self.pnt = struct.unpack("<L", f.read(4))[0]
self.fcl = struct.unpack("<L", f.read(4))[0]
self.cts = struct.unpack("<d", f.read(8))[0]
self.ats = struct.unpack("<d", f.read(8))[0]
self.wts = struct.unpack("<d", f.read(8))[0]
self.fsz = struct.unpack("<Q", f.read(8))[0]
return 0
class CMEntry(object):
def __init__(self, val=0):
self.val = val
def tofile(self, f):
f.write(struct.pack("<L", self.val))
return 0
def fromfile(self, f):
self.val = struct.unpack("<L", f.read(4))[0]
return 0
class Cluster(object):
def __init__(self, size=1):
if size < 1:
size = 1
if size > 65536:
size = 65536
self.size = size
self.data = bytearray(size)
def tofile(self, f):
f.write(self.data)
return 0
def fromfile(self, f):
self.data = bytearray(f.read(self.size))
return 0
class SVFSstats(object):
def __init__(self):
self.clustersize = 0
self.clustercount = 0
self.clusterfree = 0
self.filescount = 0
self.filesfree = 0
self.maxnamelength = 0
class SVFSaltstats(object):
def __init__(self):
self.f_bsize = 0
self.f_frsize = 0
self.f_blocks = 0
self.f_bfree = 0
self.f_bavail = 0
self.f_files = 0
self.f_ffree = 0
self.f_favail = 0
self.f_flag = 0
self.f_namemax = 0
def __getitem__(self, i):
if i == 0:
return self.f_bsize
elif i == 1:
return self.f_frsize
elif i == 2:
return self.f_blocks
elif i == 3:
return self.f_bfree
elif i == 4:
return self.f_bavail
elif i == 5:
return self.f_files
elif i == 6:
return self.f_ffree
elif i == 7:
return self.f_favail
elif i == 8:
return self.f_flag
elif i == 9:
return self.f_namemax
else:
raise IndexError("Tuple index out of range")
class SVFSpathstats(object):
def __init__(self):
self.st_mode = 0
self.st_ino = 0
self.st_dev = 0
self.st_nlink = 0
self.st_uid = 0
self.st_gid = 0
self.st_size = 0
self.st_atime = 0
self.st_mtime = 0
self.st_ctime = 0
self.st_obtype = 0
def __getitem__(self, i):
if i == 0:
return self.st_mode
elif i == 1:
return self.st_ino
elif i == 2:
return self.st_dev
elif i == 3:
return self.st_nlink
elif i == 4:
return self.st_uid
elif i == 5:
return self.st_gid
elif i == 6:
return self.st_size
elif i == 7:
return self.st_atime
elif i == 8:
return self.st_mtime
elif i == 9:
return self.st_ctime
else:
raise IndexError("Tuple index out of range")
class SVFSfile(object):
def __init__(self):
self._path = ""
self._mode = 0
self._pos = 0
self._info = None
self._cchain = None
self._encoding = None
self._errors = None
self._newlines = None
self._softspace = 0
self._parent = None
self._closed = None
self._fentry = None
def close(self):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
self._closed = True
return 0
@property
def closed(self):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
return self._closed
@property
def encoding(self):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
return self._encoding
@property
def errors(self):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
return self._errors
@property
def name(self):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
return self._path
@property
def newlines(self):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
return self._newlines
@property
def softspace(self):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
return self._softspace
@softspace.setter
def softspace(self, value):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
if not isinstance(value, int):
raise ValueError("Not an integer")
self._softspace = value
@property
def mode(self):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
if self._mode == 1:
return "rb"
if self._mode == 2:
return "rb+"
if self._mode == 3:
return "wb"
if self._mode == 4:
return "wb+"
if self._mode == 5:
return "ab"
if self._mode == 6:
return "ab+"
self._mode = 1
return "rb"
def truncate(self, size=-1):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
if self._closed:
raise ValueError("I/O operation on closed file")
if self._mode not in (2, 3, 4, 5, 6):
raise self._parent.SVFSIOError(1, "File not open for writing")
if size < 0:
size = self._pos
try:
chg = []
if self._info.fsz <= size:
return 0
count = size // self._parent.meta.csz
if size % self._parent.meta.csz > 0:
count += 1
cchg = False
while len(self._cchain) > count:
cchg = True
if len(self._cchain) > 1:
self._parent.cmap[self._cchain[len(self._cchain) - 1]].val = 0
self._parent.cmap[self._cchain[len(self._cchain) - 2]].val = 1
chg.append(self._cchain[len(self._cchain) - 1])
chg.append(self._cchain[len(self._cchain) - 2])
del self._cchain[len(self._cchain) - 1]
else:
self._parent.cmap[self._cchain[len(self._cchain) - 1]].val = 0
chg.append(self._cchain[len(self._cchain) - 1])
del self._cchain[len(self._cchain) - 1]
self._info.fcl = 0
self._info.fsz = size
index = self._fentry
self._parent.seekftbln(self._parent.svfs, index)
self._parent.ftbl[index].tofile(self._parent.svfs)
if cchg:
chg = set(chg)
self._parent.updateclustermap(
self._parent.svfs, self._parent.meta, self._parent.cmap, chg
)
return 0
except:
raise RuntimeError("Unknown error")
def flush(self):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
if self._closed:
raise ValueError("I/O operation on closed file")
return 0
def fileno(self):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
if self._closed:
raise ValueError("I/O operation on closed file")
return self._fentry
def isatty(self):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
if self._closed:
raise ValueError("I/O operation on closed file")
return False
def tell(self):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
if self._closed:
raise ValueError("I/O operation on closed file")
return self._pos
def seek(self, offset, whence=0):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
if self._closed:
raise ValueError("I/O operation on closed file")
if whence == 0:
if offset >= 0:
self._pos = offset
return 0
else:
raise self._parent.SVFSIOError(0, "Negative seek offset")
elif whence == 1:
if self._pos + offset >= 0:
self._pos += offset
return 0
else:
raise self._parent.SVFSIOError(0, "Negative seek offset")
elif whence == 2:
if self._info.fsz + offset >= 0:
self._pos += self._info.fsz + offset
return 0
else:
raise self._parent.SVFSIOError(0, "Negative seek offset")
raise ValueError("Bad argument")
def write(self, str_):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
if self._closed:
raise ValueError("I/O operation on closed file")
if self._mode not in (2, 3, 4, 5, 6):
raise self._parent.SVFSIOError(1, "File not open for writing")
if self._mode in (5, 6):
self._pos = self._info.fsz
if isinstance(str_, str):
enc = self._encoding if self._encoding is not None else "utf-8"
str_ = str_.encode(enc)
if not isinstance(str_, (bytes, bytearray)):
raise ValueError("Buffer error")
chg = []
wbuf = len(str_)
clus = 0
count = wbuf
wbufd = count
fsz = self._info.fsz
nfsz = self._pos + count
if nfsz > fsz:
fsz = nfsz
clusi = self._pos // self._parent.meta.csz
cluso = self._pos % self._parent.meta.csz
script = []
if fsz % self._parent.meta.csz > 0:
tmp2 = True
else:
tmp2 = False
if len(self._cchain) < ((fsz // self._parent.meta.csz) + tmp2):
stats = self._parent.getsvfsstats(
self._parent.meta, self._parent.ftbl, self._parent.cmap
)
if stats.clusterfree < (
((fsz // self._parent.meta.csz) + tmp2) - len(self._cchain)
):
raise self._parent.SVFSError(
9, "Not enough free disk space to perform operation"
)
else:
if (len(self._cchain)) > 0:
tmp = ((fsz // self._parent.meta.csz) + tmp2) - (
len(self._cchain)
)
else:
tmp = (fsz // self._parent.meta.csz) + tmp2
while tmp > 0:
tmp -= 1
script.append((2,))
script.append((1, clusi, cluso))
recl = self._parent.meta.csz - cluso
if recl > count:
recl = count
count -= recl
pbuf = 0
script.append((0, pbuf, recl))
pbuf += recl
while count > 0:
clus += 1
script.append((1, clusi + clus, 0))
if count >= self._parent.meta.csz:
recl = self._parent.meta.csz
elif count < self._parent.meta.csz:
recl = fsz % self._parent.meta.csz
elif count > self._parent.meta.csz:
count = self._parent.meta.csz
count -= recl
script.append((0, pbuf, recl))
pbuf += recl
offset = 0
clstrbuf = self._parent.Cluster(self._parent.meta.csz)
cchg = False
for i in script:
if i[0] == 0:
clstrbuf.data[offset : offset + i[2]] = str_[i[1] : i[1] + i[2] + 1]
clstrbuf.tofile(self._parent.svfs)
if i[0] == 1:
self._parent.seekclstnmd(
self._parent.svfs, self._parent.meta, self._cchain[i[1]]
)
offset = i[2]
clstrbuf.fromfile(self._parent.svfs)
self._parent.seekclstnmd(
self._parent.svfs, self._parent.meta, self._cchain[i[1]]
)
if i[0] == 2:
cchg = True
tmp = self._parent.getfreeclstr(
self._parent.meta, self._parent.cmap
)
if len(self._cchain) != 0:
if (
self._parent.cmap[self._cchain[len(self._cchain) - 1]].val
== 1
):
self._parent.cmap[
self._cchain[len(self._cchain) - 1]
].val = (tmp + 2)
self._parent.cmap[tmp].val = 1
chg.append(self._cchain[len(self._cchain) - 1])
chg.append(tmp)
self._cchain.append(tmp)
else:
self._parent.cmap[tmp].val = 1
self._cchain.append(tmp)
self._info.fcl = tmp
chg.append(tmp)
self._info.ats = time.mktime(datetime.datetime.now().timetuple())
self._info.wts = time.mktime(datetime.datetime.now().timetuple())
self._info.fsz = fsz
index = self._fentry
self._parent.seekftbln(self._parent.svfs, index)
self._parent.ftbl[index].tofile(self._parent.svfs)
if cchg:
chg = set(chg)
self._parent.updateclustermap(
self._parent.svfs, self._parent.meta, self._parent.cmap, chg
)
self._pos = self._pos + wbufd
return 0
def writelines(self, sequence):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
if self._closed:
raise ValueError("I/O operation on closed file")
for i in sequence:
if not isinstance(i, (str, bytes, bytearray)):
raise ValueError("Sequence of strings or bytes required")
for i in sequence:
try:
self.write(i)
except:
raise self._parent.SVFSIOError(3, "Write failed")
return 0
def read(self, size=-1):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
if self._closed:
raise ValueError("I/O operation on closed file")
if self._mode not in (1, 2, 4, 6):
raise self._parent.SVFSIOError(2, "File not open for reading")
if size == 0:
return b""
clus = 0
fsz = self._info.fsz
if size > 0:
nfsz = self._pos + size
if nfsz > fsz:
nfsz = fsz
size = nfsz - self._pos
if size < 0:
size = fsz - self._pos
if size <= 0:
return b""
wbuf = size
bfr = bytearray(wbuf)
clusi = self._pos // self._parent.meta.csz
cluso = self._pos % self._parent.meta.csz
script = []
script.append((1, clusi, cluso))
recl = self._parent.meta.csz - cluso
if recl > size:
recl = size
size -= recl
pbuf = 0
script.append((0, pbuf, recl))
pbuf += recl
while size > 0:
clus += 1
script.append((1, clusi + clus, 0))
if size >= self._parent.meta.csz:
recl = self._parent.meta.csz
elif size < self._parent.meta.csz:
recl = fsz % self._parent.meta.csz
size -= recl
script.append((0, pbuf, recl))
pbuf += recl
offset = 0
clstrbuf = self._parent.Cluster(self._parent.meta.csz)
for i in script:
if i[0] == 0:
clstrbuf.fromfile(self._parent.svfs)
bfr[i[1] : i[1] + i[2]] = clstrbuf.data[offset : offset + i[2]]
if i[0] == 1:
self._parent.seekclstnmd(
self._parent.svfs, self._parent.meta, self._cchain[i[1]]
)
offset = i[2]
clstrbuf.fromfile(self._parent.svfs)
self._parent.seekclstnmd(
self._parent.svfs, self._parent.meta, self._cchain[i[1]]
)
self._info.ats = time.mktime(datetime.datetime.now().timetuple())
index = self._fentry
self._parent.seekftbln(self._parent.svfs, index)
self._parent.ftbl[index].tofile(self._parent.svfs)
self._pos = self._pos + wbuf
result_bytes = bytes(bfr)
if self._encoding is not None:
return result_bytes.decode(self._encoding)
return result_bytes
def __iter__(self):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
if self._closed:
raise ValueError("I/O operation on closed file")
return self
def __enter__(self):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
if self._closed:
raise ValueError("I/O operation on closed file")
return self
def __exit__(self, exc_type, exc_val, exc_tb):
try:
return self.close()
except:
raise self._parent.SVFSError(2, "SVFS is not opened")
def __del__(self):
try:
self.close()
except:
pass
def __next__(self):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
if self._closed:
raise ValueError("I/O operation on closed file")
if self._mode not in (1, 2, 4, 6):
raise self._parent.SVFSIOError(2, "File not open for reading")
while 1:
ret = self.readline()
if not ret:
raise StopIteration
else:
return ret
def readline(self, size=-1):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
if self._closed:
raise ValueError("I/O operation on closed file")
if self._mode not in (1, 2, 4, 6):
raise self._parent.SVFSIOError(2, "File not open for reading")
if size == 0:
return b"" if self._encoding is None else ""
# Determine mode: text if encoding is set, else binary.
is_text = self._encoding is not None
nl = "\n" if is_text else b"\n"
cr = "\r" if is_text else b"\r"
bfr = bytearray() if not is_text else ""
tmp = b"" if not is_text else ""
chunksize = self._parent.meta.csz
if size < 0:
size = self._info.fsz - self._pos
if size <= 0:
return b"" if not is_text else ""
wpos = self._pos
wcount = size
length = 0
newl = None
end = False
while not end:
if wcount == 0:
break
# read in same mode; self.read returns text if encoding set.
tmp = self.read(chunksize)
if not tmp:
break
if is_text:
pos = tmp.find(nl)
else:
pos = tmp.find(nl)
if pos != -1:
if is_text:
bfr += tmp[: pos + 1]
else:
bfr += tmp[: pos + 1]
length += pos + 1
newl = nl
end = True
break
else:
if is_text:
bfr += tmp
else:
bfr += tmp
length += len(tmp)
wcount -= len(tmp)
self.seek(wpos + length)
if self._newlines is None:
self._newlines = newl
else:
if self._newlines is None:
self._newlines = newl
else:
if self._newlines != newl:
if not isinstance(self._newlines, tuple):
self._newlines = (self._newlines,)
if newl not in self._newlines:
self._newlines = self._newlines + (newl,)
# Ensure returning a string in text mode
if self._encoding is not None and isinstance(bfr, bytes):
return bfr.decode(self._encoding)
return bfr
def readlines(self, sizehint=-1):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
if self._closed:
raise ValueError("I/O operation on closed file")
if self._mode not in (1, 2, 4, 6):
raise self._parent.SVFSIOError(2, "File not open for reading")
if sizehint == 0:
return []
if sizehint < 0:
sizehint = self._info.fsz
if sizehint == 0:
return []
length = 0
ret = []
for i in self:
length += len(i)
ret.append(i)
if length >= sizehint:
break
# If in text mode, ensure all lines are strings.
if self._encoding is not None:
ret = [
line if isinstance(line, str) else line.decode(self._encoding)
for line in ret
]
return ret
def xreadlines(self):
if not self._parent.opened:
raise self._parent.SVFSError(2, "SVFS is not opened")
if self._closed:
raise ValueError("I/O operation on closed file")
return iter(self)
def __init__(self):
self.opened = False
self.svfs = None
self.path = None
self.ident = None
self.meta = None
self.ftbl = None
self.cmap = None
self.files = []
self.currev = 1
self.type = 0
def convert_bytes(self, bytes):
suffixes = (" bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB")
bytes = float(bytes)
sval = 0
while bytes > 1024:
sval += 1
bytes = bytes / 1024
if sval > 8:
raise ValueError("Value is too high")
return ("%.2f" % bytes) + suffixes[sval]
def seekident(self, f):
f.seek(0)
return 0
def seekmetad(self, f):
f.seek(4)
return 0
def seekftble(self, f):
f.seek(4 + 267)
return 0
def seekftbln(self, f, n):
f.seek(4 + 267 + (297 * n))
return 0
def seekclmap(self, f, ftl):
f.seek(4 + 267 + (297 * ftl))
return 0
def seekcmapn(self, f, ftl, n):
f.seek(4 + 267 + (297 * ftl) + (4 * n))
return 0
def seekclust(self, f, ftl, cml):
f.seek(4 + 267 + (297 * ftl) + (4 * cml))
return 0
def seekclstn(self, f, ftl, cml, csz, n):
f.seek(4 + 267 + (297 * ftl) + (4 * cml) + (csz * n))
return 0
def seekfseof(self, f, ftl, cml, csz):
f.seek(4 + 267 + (297 * ftl) + (4 * cml) + (csz * cml))
return 0
def seekclmapmd(self, f, md):
f.seek(4 + 267 + (297 * md.ftl))
return 0
def seekcmapnmd(self, f, md, n):
f.seek(4 + 267 + (297 * md.ftl) + (4 * n))
return 0
def seekclustmd(self, f, md):
f.seek(4 + 267 + (297 * md.ftl) + (4 * md.cml))
return 0
def seekclstnmd(self, f, md, n):
f.seek(4 + 267 + (297 * md.ftl) + (4 * md.cml) + (md.csz * n))
return 0
def seekfseofmd(self, f, md):
f.seek(4 + 267 + (297 * md.ftl) + (4 * md.cml) + (md.csz * md.cml))
return 0
def ftblcreate(self, length):
ftbl = []
while length > 0:
length -= 1
ftbl.append(self.FTEntry())
return ftbl
def ftbltofile(self, f, ftbl):
for i in ftbl:
i.tofile(f)
return 0
def ftblfromfile(self, f, ftbl):
for i in ftbl:
i.fromfile(f)
return 0
def cmapcreate(self, length):
cmap = []
while length > 0:
length -= 1
cmap.append(self.CMEntry())
return cmap
def cmaptofile(self, f, cmap):
for i in cmap:
i.tofile(f)
return 0
def cmapfromfile(self, f, cmap):
for i in cmap:
i.fromfile(f)
return 0
def getfreefile(self, md, ftbl):
for i in range(md.ftl):
if ftbl[i].typ == 0:
return i
raise self.SVFSError(0, "File table is full")
def countfreefiles(self, md, ftbl):
cnt = 0
for i in ftbl:
if i.typ == 0:
cnt += 1
return cnt
def getfreeclstr(self, md, cmap):
for i in range(md.cml):
if cmap[i].val == 0:
return i
raise self.SVFSError(1, "Disk is full")
def countfreeclstr(self, md, cmap):
cnt = 0
for i in cmap:
if i.val == 0:
cnt += 1
return cnt
def getsvfsstats(self, md, ftbl, cmap):
if not self.opened:
raise self.SVFSError(2, "SVFS is not opened")
stats = self.SVFSstats()
stats.clustersize = md.csz
stats.clustercount = md.cml
stats.clusterfree = self.countfreeclstr(md, cmap)
stats.filescount = md.ftl
stats.filesfree = self.countfreefiles(md, ftbl)
stats.maxnamelength = 255
return stats
def statvfs(self, path):
if not self.opened:
raise self.SVFSError(2, "SVFS is not opened")
if path in ["", "/"]:
path = self.ftbl[0].fnm
if self.followpath(self.ftbl, path) == -1:
raise self.SVFSError(3, "No such file or directory")
stats = self.SVFSaltstats()
stats.f_bsize = self.meta.csz
stats.f_frsize = self.meta.csz
stats.f_blocks = self.meta.cml
stats.f_bfree = self.countfreeclstr(self.meta, self.cmap)
stats.f_bavail = stats.f_bfree
stats.f_files = self.meta.ftl
stats.f_ffree = self.countfreefiles(self.meta, self.ftbl)
stats.f_favail = stats.f_ffree
stats.f_flag = 0
stats.f_namemax = 255
return stats
def stat(self, path):
if not self.opened:
raise self.SVFSError(2, "SVFS is not opened")
if path in ["", "/"]:
path = self.ftbl[0].fnm
frec = self.followpath(self.ftbl, path)
if frec == -1:
raise self.SVFSError(3, "No such file or directory")
stats = self.SVFSpathstats()
stats.st_mode = 0
if self.ftbl[frec].typ in (2, 4):
stats.st_mode = stats.st_mode | 0o040000 # Changed from 0040000
if self.ftbl[frec].typ == 3:
stats.st_mode = stats.st_mode | 0o060000 # Changed from 0060000
if self.ftbl[frec].typ == 1:
stats.st_mode = stats.st_mode | 0o100000 # Changed from 0100000
stats.st_ino = frec
if self.ftbl[frec].typ == 3:
stats.st_dev = 1
else:
stats.st_dev = 0
stats.st_nlink = 1
stats.st_uid = 0
stats.st_gid = 0
stats.st_size = self.ftbl[frec].fsz
stats.st_atime = self.ftbl[frec].ats
stats.st_mtime = self.ftbl[frec].wts
stats.st_ctime = self.ftbl[frec].cts
stats.st_obtype = self.ftbl[frec].typ
return stats
def fstatvfs(self, fd):
if not self.opened:
raise self.SVFSError(2, "SVFS is not opened")
if fd >= 0 and fd < self.meta.ftl:
if self.meta.fself.ftbl[fd].typ == 0:
raise self.SVFSError(3, "No such file or directory")
else:
raise self.SVFSError(3, "No such file or directory")
stats = self.SVFSaltstats()
stats.f_bsize = self.meta.csz
stats.f_frsize = self.meta.csz
stats.f_blocks = self.meta.cml
stats.f_bfree = self.countfreeclstr(self.meta, self.cmap)