-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNGAS_fs.py
1100 lines (980 loc) · 39.7 KB
/
NGAS_fs.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import bz2
import os
import stat
import errno
import sys
import hashlib
from sqlobject import *
from sqlobject.inheritance import InheritableSQLObject
import sqlite3
import threading
import time
import urllib2
import atpy
from fuse import Fuse
import fuse
if not hasattr(fuse, '__version__'):
raise RuntimeError, \
"your fuse-py doesn't know of fuse.__version__, probably it's too old."
fuse.fuse_python_api = (0, 2)
SERVER_LOCATION = 'http://pleiades.icrar.org:7777/'
class PseudoStat(fuse.Stat):
"""
専用ã®statæ§‹é€ ä½“
- st_mode ä¿è·ãƒ¢ãƒ¼ãƒ‰
- st_ino inode番å·
- st_dev device番å·
- st_nlink ãƒãƒ¼ãƒ‰ãƒªãƒ³ã‚¯ã®æ•°
- st_uid オーナーUID
- st_gid オーナーGID
- st_size ファイルサイズ(Byte)
- st_atime 最終アクセス時間
- st_mtime 最終更新時間
- st_ctime プラットフォームä¾å˜ã€Unixã§ã¯æœ€çµ‚メタデータ変更時間
     Windowsã§ã¯ä½œæˆæ™‚é–“
"""
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
class Inode(SQLObject):
"""
Creates an object to interact with the inode table
"""
inode_num = IntCol(notNone=True) #Uniquely identifies each item
rev_id = IntCol(notNone=True) #States the item 'version'
uid = IntCol(notNone=True) #User id
gid = IntCol(notNone=True) #Group id
atime = FloatCol(notNone=True) #Access time
mtime = FloatCol(notNone=True) #Modify time
ctime = FloatCol(notNone=True) #Creation time
size = IntCol(notNone=True) #Size of file
mode = IntCol(notNone=True) #Access permissions
dev = IntCol(default=0) #Not sure, has always been 0
inum_r_index = DatabaseIndex("inode_num","rev_id")
class Dentry(SQLObject):
"""
Creates an object to interact with the dentry table
"""
parent = ForeignKey("Inode", notNone=True) #primary key of root from inode table
filename = UnicodeCol(notNone=True) #Name of file
inode_num = IntCol(notNone=True) #reference to inode_num from inode table
p_index = DatabaseIndex("parent")
p_name_index = DatabaseIndex("parent", "filename")
class RawData(SQLObject):
"""
Interects with a table that holds the raw data, not used in this implementation
"""
hash_sha256 = StringCol(notNone=True, length=64)
data = StringCol(notNone=True)
hash_index = DatabaseIndex("hash_sha256")
def _set_data(self, value):
self._SO_set_data(bz2.compress(value).encode("base64"))
def _get_data(self):
return bz2.decompress(self._SO_get_data().decode("base64"))
class DataList(SQLObject):
"""
Interacts with the data_list table
"""
parent = ForeignKey("Inode", notNone=True) #Reference to inode primary key
series = IntCol(notNone=True, default=0) #A type of versioning, 0 for new files
data = ForeignKey("RawData", notNone=True) #Reference to raw_data, not used
ps_index = DatabaseIndex("parent", "series")
def split_path(path):
"""
Returns everything after the final '/'
Removes '/' if it's the last character.
"""
if path[-1] == "/":
path = path[:-1]
return path.split("/")[1:]
class FileSystemError(BaseException):
"""ファイルシステムãŒè¿”ã™ã™ã¹ã¦ã®ä¾‹å¤–ã®ç¶™æ‰¿å…ƒ"""
pass
class IllegalInode(FileSystemError):
"""ä¸æ£ãªi-nodeを検知"""
pass
class VFile(object):
"""
Honestly not sure what this is for, have left it unedited and haven't had it interfering.
"""
BLOCK_SIZE = 32*1024
def __init__(self, inode_e):
"""Inodeã‚’å—ã‘å–ã£ã¦åˆæœŸåŒ–"""
self.__inode = inode_e
if not isinstance(inode_e, Inode):
raise IllegalInode()
self.__count = 0
self.__dirty = False
self.__lock = threading.Lock()
self.__list = []
def open(self):
self.__count += 1
def close(self):
"""å‚照カウンタをデクリメントã—ã¦å¿…è¦ãªã‚‰DBã«æ›¸ã込む"""
self.__count -= 1
if self.__count < 1 & self.__dirty:
self.fsync()
def is_close(self):
if self.__count > 0:
return False
return True
def __read_list(self):
if not self.__list:
tmp = DataList.selectBy(parent=self.__inode).orderBy("series")
cnt = tmp.count()
if cnt == 0:
inodes = Inode.selectBy(inode_num=self.__inode.inode_num)\
.orderBy("-rev_id")
for i_n in inodes:
tmp = DataList.selectBy(parent=i_n).orderBy("series")
cnt = tmp.count()
if cnt == 0:
break
if cnt == 0 and self.__inode.size > 0:
raise FileSystemError(self.__inode,list(inodes))
for d in tmp:
self.__list.append(d.data)
def read(self, length, offset):
if offset > self.__inode.size:
raise FileSystemError("Offset is greater than size.")
if (offset + length) > self.__inode.size:
length = self.__inode.size - offset
self.__read_list()
i_start = int(offset / self.BLOCK_SIZE)
start = i_start * self.BLOCK_SIZE
if len(self.__list) == 0:
return ""
if len(self.__list) < i_start:
return ""
buf = self.__list[i_start].data[offset - start:]
self.__lock.acquire()
for d in self.__list[i_start+1:]:
buf += d.data
if len(buf) > length:
break
self.__lock.release()
return buf[:length]
def __write_block(self, blk):
"""ブãƒãƒƒã‚¯ã‚’書ã込む
é‡è¤‡ã®ç¢ºèªã‚’è¡Œã†
書ãè¾¼ã¿ã«æˆåŠŸã—ãŸå ´åˆã€RawDataオブジェクトを返ã™
"""
hash_sha256 = hashlib.sha256(blk).hexdigest()
if RawData.selectBy(hash_sha256=hash_sha256).count() > 0:
same_hashs = RawData.selectBy(hash_sha256=hash_sha256)
hash_md5 = hashlib.md5(blk).digest()
for same_hash in same_hashs:
hash = hashlib.md5(same_hash.data).digest()
if hash_md5 == hash:
return same_hash
return RawData(hash_sha256=hash_sha256, data=blk)
def write_wo_t(self, buf, offset):
"""一時ファイルを用ã„ãšã«æ›¸ãè¾¼ã¿"""
self.__read_list()
now = time.time()
size = offset + len(buf)
if size < self.__inode.size:
size = self.__inode.size
self.__read_list()
self.__lock.acquire()
if not self.__dirty:
max_rev = Inode.selectBy(inode_num=self.__inode.inode_num).\
max("rev_id")
if not max_rev == self.__inode.rev_id:
raise FileSystemError("You use old file.")
new_i = Inode(inode_num=self.__inode.inode_num,
rev_id=(max_rev+1), uid=self.__inode.uid,
gid=self.__inode.gid, atime=now,
mtime=now, ctime=self.__inode.ctime, size=size,
mode=33060)
self.__inode = new_i
self.__dirty = True
else:
new_i = self.__inode
new_i.atime = now
new_i.mtime = now
new_i.size = size
tmp_list = []
pos = 0
i_start = int(offset / self.BLOCK_SIZE)
b_offset = offset - (i_start * self.BLOCK_SIZE)
i_end = int((offset + len(buf)) / self.BLOCK_SIZE)
b_len = offset + len(buf) - (i_end * self.BLOCK_SIZE)
for d in self.__list[:i_start-1]:
tmp_list.append(d)
tmp_buf = self.__list[i_start].data
if i_start == i_end:
tmp_buf[b_offset:b_len] = buf[:]
else:
tmp_buf[b_offset:] = buf[:b_offset]
tmp_list.append(self.__write_block(tmp_buf))
tmp_buf = buf[b_offset:]
for d in self.__list[i_start+1:i_end]:
tb = tmp_buf[:self.BLOCK_SIZE]
tmp_list.append(self.__write_block(tb))
tmp_buf = tmp_buf[self.BLOCK_SIZE:]
if not i_start == i_end:
tb = self.__list[i_end].data
tb[len(tmp_buf):] = tmp_buf
tmp_list.append(self.__write_block(tb))
for d in self.__list[i_end+1:]:
tmp_list.append(d)
new_i.syncUpdate()
self.__list = tmp_list
self.__lock.release()
def write_w_t(self, buf, offset):
"""一時ファイルを経由ã—ã¦æ›¸ãè¾¼ã¿"""
self.__read_list()
import tempfile
tf = tempfile.TemporaryFile()
now = time.time()
size = offset + len(buf)
if size < self.__inode.size:
size = self.__inode.size
self.__lock.acquire()
if not self.__dirty:
max_rev = Inode.selectBy(inode_num=self.__inode.inode_num).\
max("rev_id")
if not max_rev == self.__inode.rev_id:
raise FileSystemError("You use old file.")
new_i = Inode(inode_num=self.__inode.inode_num,
rev_id=(max_rev+1), uid=self.__inode.uid,
gid=self.__inode.gid, atime=now,
mtime=now, ctime=self.__inode.ctime, size=size,
mode=33060)
self.__inode = new_i
self.__dirty = True
else:
new_i = self.__inode
new_i.atime = now
new_i.mtime = now
new_i.size = size
tmp_list = []
if len(self.__list) < 10:
for d in self.__list:
tf.write(d.data)
tf.seek(offset)
tf.write(buf)
tf.seek(0)
b_count = int(size / self.BLOCK_SIZE)
if size > b_count * self.BLOCK_SIZE:
b_count += 1
for i in range(b_count):
tb = tf.read(self.BLOCK_SIZE)
if len(tb) > self.BLOCK_SIZE:
tb = tb + "\0" * (self.BLOCK_SIZE-len(tb))
sd = RawData.selectBy(hash_sha256=\
hashlib.sha256(tb).hexdigest())
if sd.count() == 0:
d = RawData(hash_sha256=hashlib.sha256(tb).hexdigest(),
data=tb)
tmp_list.append(d)
else:
tmp_list.append(sd[0])
else:
i_start = int(offset / self.BLOCK_SIZE)
b_offset = offset - (i_start * self.BLOCK_SIZE)
i_end = int((offset + len(buf)) / self.BLOCK_SIZE)
b_len = offset + len(buf) - (i_end * self.BLOCK_SIZE)
if blen > 0:
i_end += 1
for d in self.__list[i_start:i_end]:
tf.write(d.data)
tf.seek(b_offset)
tf.write(buf)
tf.seek(0)
tmp_list = self.__list[:]
if i_end < len(self.__list):
for i in range(i_start, i_end+1):
tb = tf.read(self.BLOCK_SIZE)
tmp_list[i] = self.__write_block(tb)
else:
for i in range(i_start, len(self.__list)):
tb = tf.read(self.BLOCK_SIZE)
tmp_list[i] = self.__write_block(tb)
for i in range(len(self.__list), i_end+1):
tb = tf.read(self.BLOCK_SIZE)
tmp_list.append(self.__write_block(tb))
new_i.syncUpdate()
self.__list = tmp_list
self.__lock.release()
def write(self, buf, offset):
return self.write_w_t(buf, offset)
def get_entry(self):
return self.__inode
def truncate(self, size):
self.__read_list()
now = time.time()
self.__read_list()
self.__lock.acquire()
if not self.__dirty:
max_rev = Inode.selectBy(inode_num=self.__inode.inode_num).\
max("rev_id")
if not max_rev == self.__inode.rev_id:
raise FileSystemError("You use old file.", \
max_rev, self.__inode.rev_id)
new_i = Inode(inode_num=self.__inode.inode_num,
rev_id=(max_rev+1), uid=self.__inode.uid,
gid=self.__inode.gid, atime=now,
mtime=now, ctime=self.__inode.ctime, size=size,
mode=self.__inode.mode)
self.__inode = new_i
self.__dirty = True
else:
new_i = self.__inode
new_i.atime = now
new_i.mtime = now
new_i.size = size
new_i.syncUpdate()
self.__lock.release()
def chmod(self, mode):
self.__read_list()
now = time.time()
self.__read_list()
self.__lock.acquire()
if not self.__dirty:
max_rev = Inode.selectBy(inode_num=self.__inode.inode_num).\
max("rev_id")
if not max_rev == self.__inode.rev_id:
raise FileSystemError("You use old file.")
new_i = Inode(inode_num=self.__inode.inode_num,
rev_id=(max_rev+1), uid=self.__inode.uid,
gid=self.__inode.gid, atime=now,
mtime=now, ctime=self.__inode.ctime, size=self.__inode.size,
mode=mode)
self.__inode = new_i
self.__dirty = True
else:
new_i = self.__inode
new_i.atime = now
new_i.mtime = now
new_i.mode = mode
new_i.syncUpdate()
self.__lock.release()
def chown(self, uid, gid):
self.__read_list()
now = time.time()
self.__read_list()
self.__lock.acquire()
if not self.__dirty:
max_rev = Inode.selectBy(inode_num=self.__inode.inode_num).\
max("rev_id")
if not max_rev == self.__inode.rev_id:
raise FileSystemError("You use old file.")
new_i = Inode(inode_num=self.__inode.inode_num,
rev_id=(max_rev+1), uid=uid,
gid=gid, atime=now,
mtime=now, ctime=self.__inode.ctime, size=self.__inode.size,
mode=self.__inode.mode)
self.__inode = new_i
self.__dirty = True
else:
new_i = self.__inode
new_i.atime = now
new_i.mtime = now
new_i.uid = uid
new_i.gid = gid
new_i.syncUpdate()
self.__lock.release()
def fsync(self):
self.__lock.acquire()
self.__inode.syncUpdate()
for i, v in enumerate(self.__list):
DataList(parent=self.__inode, series=i, data=v)
self.__dirty = False
self.__lock.release()
class DBDumpFS:
"""
This is the class for interactions with the dumpfs.sqlite DB.
Holds all of the overwritten functions used to modify the filesystem.
"""
BLOCK_SIZE = 32*1024
def __init__(self, db_scheme=""):
"""
Connects to and opens DB if exists, creates it if it doesn't
"""
if not db_scheme:
import tempfile
self.__tf = tempfile.NamedTemporaryFile()
db_scheme = "sqlite:" + self.__tf.name
conn = connectionForURI(db_scheme)
sqlhub.processConnection = conn
Inode.createTable(ifNotExists=True)
Dentry.createTable(ifNotExists=True)
RawData.createTable(ifNotExists=True)
DataList.createTable(ifNotExists=True)
self.__init_root()
self.__openfiles = dict()
def __init_root(self):
"""
Creates the root node within the database
"""
if not list(Inode.selectBy(inode_num=0)):
now = time.time()
root_node = Inode(inode_num=0, rev_id=0,
uid=os.getuid(), gid=os.getgid(),
atime=now, ctime=now, mtime=now,
size=0, mode=stat.S_IFDIR|0o755)
def __get_parent_inode(self, path):
"""
Returns the inode of parent directory.
Raises error if on root.
self: object, object(file/directory) that the function was called on.
path: string, current working directory path.
"""
if path == "/":
raise FileSystemError("No parent directory.")
parent_dir = Inode.selectBy(inode_num=0).orderBy("-rev_id")[0]
for fn in split_path(path)[:-1]:
tmp = Dentry.selectBy(parent=parent_dir, filename=fn)
if tmp.count() == 0:
raise FileSystemError("file not found.")
parent_dir = Inode.selectBy(inode_num=tmp[0].inode_num).\
orderBy("-rev_id")[0]
return parent_dir.inode_num
def __get_inode(self, path):
"""
Returns the inode of the object (file/directory).
Raises an error if the object doesn't exist.
self: object, object(file/directory) that the function was called on.
path: string, current working directory path.
"""
if path == "/":
return 0
parent_dir = Inode.selectBy(inode_num=self.__get_parent_inode(path)).\
orderBy("-rev_id")[0]
tmp = Dentry.selectBy(parent=parent_dir,
filename=split_path(path)[-1])
if tmp.count() == 0:
raise FileSystemError("file not found.:",path)
ret = Inode.selectBy(inode_num=tmp[0].inode_num).orderBy("-rev_id")[0]
return ret.inode_num
def fsync(self):
"""
Ensures all files in VFile are up-to-date.
Not sure on actual useage.
"""
for vfile in self.__openfiles.values():
vfile.fsync()
def stat(self, path):
"""
Returns the inode table entry of the object.
self: object, object(file/directory) that the function was called on.
path: string, current working directory path.
"""
if path in self.__openfiles:
inode_ent = self.__openfiles[path].get_entry()
else:
inode = self.__get_inode(path)
inode_ent = Inode.selectBy(inode_num=inode).orderBy("-rev_id")[0]
return inode_ent
def readdir(self, path):
"""
Reads the directory and returns a list of the items within it.
self: object, object(file/directory) that the function was called on.
path: string, current working directory path.
"""
inode = self.__get_inode(path)
inode_ent = Inode.selectBy(inode_num=inode).orderBy("-rev_id")[0]
if not stat.S_ISDIR(inode_ent.mode):
raise FileSystemError("Not a directory.")
return list(Dentry.selectBy(parent=inode_ent))
def mknod(self, path, mode, dev):
"""
Creates a new file/directory on path
self: object, object(file/directory) that the function was called on.
path: string, current working directory path.
mode: interger, file permissions of object
dev: interger, no idea as I just leave it 0
"""
try:
self.__get_inode(path)
except FileSystemError:
parent_i_num = self.__get_parent_inode(path)
parent_i = Inode.selectBy(inode_num=parent_i_num).\
orderBy("-rev_id")[0]
now = time.time()
inode_num = Inode.select().max("inode_num") + 1
conn = sqlhub.getConnection()
trans = conn.transaction()
ret = Inode(inode_num=inode_num, rev_id=0, uid=os.getuid(),
gid=os.getgid(), atime=now, ctime=now,
mtime=now, size=0, mode=mode, dev=dev, connection=trans)
Dentry(parent=parent_i, inode_num=inode_num,
filename=split_path(path)[-1], connection=trans)
trans.commit()
return ret.inode_num
def create(self, path, mode):
"""
Calls mknod to create the file/directory on path.
self: object, object(file/directory) that the function was called on.
path: string, current working directory path.
mode: interger, file permissions.
"""
return self.mknod(path, mode, 0)
def mkdir(self, path, mode):
"""
Call create to make a directory.
"""
return self.create(path, mode|stat.S_IFDIR)
def rmdir(self, path):
"""
Call remove to remove directory.
"""
return self.remove(path)
def open(self, path, flags, mode=0):
"""
Opens a file for reading and writing depending on permissions.
self: object, object(file/directory) that the function was called on.
path: string, current working directory path.
flags: binary?, standard flags for file creation.
mode: octal, used to store file permissions.
"""
if mode == 0:
mode = 0o644|stat.S_IFREG
if flags&os.O_CREAT == os.O_CREAT:
self.create(path, mode)
T = atpy.Table(SERVER_LOCATION + 'QUERY?query=files_list&format=list',type='ascii')
if not (path[1:] in T['col3']):
if path in self.__openfiles:
self.__openfiles[path].open()
else:
inode = self.__get_inode(path)
inode_ent = Inode.selectBy(inode_num=inode).orderBy("-rev_id")[0]
inode_ent.mode = 33060 #Sets the file to read-only mode before opening it.
self.__openfiles[path]=VFile(inode_ent)
self.__openfiles[path].open()
def close(self, path):
"""
Closes the file/directory
self: object, object(file/directory) that the function was called on.
path: string, current working directory path.
"""
if path in self.__openfiles:
self.__openfiles[path].close()
if self.__openfiles[path].is_close():
del self.__openfiles[path]
def read(self, path, length, offset):
"""
Reads the selected file.
If the file is from the server it is downloaded and read without being saved locally.
If it is a local file, it is read locally.
self: object, object(file/directory) that the function was called on.
path: string, current working directory path.
length: interger?, amount of the file to read.
offset: interger?, starting position in file (invalid for reading from server.
"""
#Create a list of available files
T = atpy.Table(SERVER_LOCATION + 'QUERY?query=files_list&format=list',type='ascii')
#Check is file is local
if not (path[1:] in T['col3']):
if not path in self.__openfiles:
self.open(path, 0)
return self.__openfiles[path].read(length, offset)
#File is not local so open and read from server
else:
urlString = SERVER_LOCATION + 'RETRIEVE?file_id=' + path[1:]
ht = None
ht = urllib2.urlopen(urlString)
return ht.read(length)
def write(self, path, buf, offset):
"""
Writes a specified file.
NOTE: At current implementation, all files are READ-ONLY
self: object, object(file/directory) that the function was called on.
path: string, current working directory path.
buf: string?, contains data to be written to file.
offset: interger?, position in file to start writing.
"""
if not path in self.__openfiles:
self.open(path, 0)
return sqlhub.doInTransaction(self.__openfiles[path].write,
buf, offset)
def remove(self, path):
"""
Removes the file from the directory and database.
NOTE: This funtion should not be used and therefore has been removed.
self: object, object(file/directory) that the function was called on.
path: string, current working directory path.
"""
#Create a list of available files
T = atpy.Table(SERVER_LOCATION + 'QUERY?query=files_list&format=list',type='ascii')
#Check is file is local
if not (path[1:] in T['col3']):
now = time.time()
i_num = self.__get_inode(path)
parent_i_num = self.__get_parent_inode(path)
parent_i = Inode.selectBy(inode_num=parent_i_num).orderBy("-rev_id")[0]
dl = Dentry.selectBy(parent=parent_i)
conn = sqlhub.getConnection()
trans = conn.transaction()
new_i = Inode(inode_num=parent_i.inode_num,
rev_id=parent_i.rev_id+1,
uid=parent_i.uid, gid=parent_i.gid,
atime=now, mtime=parent_i.mtime,
ctime=parent_i.ctime, size=parent_i.size,
mode=parent_i.mode, connection=trans)
for de in dl:
if de.inode_num != i_num:
Dentry(parent=new_i, filename=de.filename,
inode_num=de.inode_num, connection=trans)
trans.commit()
if path in self.__openfiles:
while not self.__openfiles[path].is_close():
self.__openfiles[path].close()
del self.__openfiles[path]
#If not local then file is from server and removal is denied.
else:
pass
def rename(self, oldPath, newPath):
"""
Removes an object from oldPath and places it on newPath
NOTE: This function should not be used and has therefore been removed.
self: object, object(file/directory) that the function was called on.
oldPath: string, current path of object.
newPath: string, new path of object.
"""
conn = sqlhub.getConnection()
trans = conn.transaction()
now = time.time()
i_num = self.__get_inode(oldPath)
parent_i_num = self.__get_parent_inode(oldPath)
parent_i = Inode.selectBy(inode_num=parent_i_num).orderBy("-rev_id")[0]
dl = Dentry.selectBy(parent=parent_i)
new_i = Inode(inode_num=parent_i.inode_num,
rev_id=parent_i.rev_id+1,
uid=parent_i.uid, gid=parent_i.gid,
atime=now, mtime=parent_i.mtime,
ctime=parent_i.ctime, size=parent_i.size,
mode=parent_i.mode, connection=trans)
for de in dl:
if de.inode_num != i_num:
Dentry(parent=new_i, filename=de.filename,
inode_num=de.inode_num, connection=trans)
parent_i_num = self.__get_parent_inode(newPath)
parent_i = Inode.selectBy(inode_num=parent_i_num).orderBy("-rev_id")[0]
Dentry(parent=new_i, filename=split_path(newPath)[-1],
inode_num=i_num, connection=trans)
old_i = Inode.selectBy(inode_num=i_num).orderBy("-rev_id")[0]
Inode(inode_num=old_i.inode_num,
rev_id=old_i.rev_id+1,
uid=old_i.uid, gid=old_i.gid,
atime=now, mtime=old_i.mtime,
ctime=old_i.ctime, size=old_i.size,
mode=old_i.mode, connection=trans)
trans.commit()
if oldPath in self.__openfiles:
while not self.__openfiles[oldPath].is_close():
self.__openfiles[oldPath].close()
del self.__openfiles[oldPath]
def chmod(self, path, mode):
"""
Changes access permissions
NOTE: Doesn't work.
"""
if path in self.__openfiles:
self.__openfiles[path].chmod(mode)
else:
inode_num = self.__get_inode(path)
old_i = Inode.selectBy(inode_num=inode_num).orderBy("-rev_id")[0]
vfile = VFile(old_i)
vfile.chmod(mode)
vfile.close()
def chown(self, path, uid, gid):
"""
Changes ownership of object.
NOTE: Doesn't work, states invalid permissions even if run via root.
"""
if path in self.__openfiles:
self.__openfiles[path].chown(uid,gid)
else:
inode_num = self.__get_inode(path)
old_i = Inode.selectBy(inode_num=inode_num).orderBy("-rev_id")[0]
vfile = VFile(old_i)
vfile.chown(uid, gid)
vfile.close()
def truncate (self, path, size):
"""
Changes the size of the object.
NOTE: Doesn't seem to work.
"""
if path in self.__openfiles:
self.__openfiles[path].truncate(size)
else:
inode_num = self.__get_inode(path)
old_i = Inode.selectBy(inode_num=inode_num).orderBy("-rev_id")[0]
vfile = VFile(old_i)
vfile.truncate(size)
vfile.close()
def flush(self, path):
"""
Syncs the object if it has been opened.
self: object, object(file/directory) that the function was called on.
path: string, current working directory path.
"""
if path in self.__openfiles:
self.__openfiles[path].fsync()
def link(self, oldPath, newPath):
"""
Creates a link from the object at oldPath to an object at newPath.
NOTE: This does not work on items from the server, the link is unreadable.
self: object, object(file/directory) that the function was called on.
oldPath: string, path of original file
newPath, string, path of link file
"""
conn = sqlhub.getConnection()
trans = conn.transaction()
now = time.time()
i_num = self.__get_inode(oldPath)
parent_i_num = self.__get_parent_inode(newPath)
parent_i = Inode.selectBy(inode_num=parent_i_num).orderBy("-rev_id")[0]
dl = Dentry.selectBy(parent=parent_i)
new_i = Inode(inode_num=parent_i.inode_num,
rev_id=parent_i.rev_id+1,
uid=parent_i.uid, gid=parent_i.gid,
atime=now, mtime=parent_i.mtime,
ctime=parent_i.ctime, size=parent_i.size,
mode=parent_i.mode, connection=trans)
for de in dl:
Dentry(parent=new_i, filename=de.filename,
inode_num=de.inode_num, connection=trans)
Dentry(parent=new_i, filename=split_path(newPath)[-1],
inode_num=i_num, connection=trans)
trans.commit()
def symlink(self, oldPath, newPath):
"""
Creates a symbolic link from oldPath to newPath.
NOTE: Raises I/O error.
"""
mode = 0o644|stat.S_IFLNK
i_num = self.mknod(newPath, mode, 0)
self.write(newPath, oldPath, 0)
def readlink(self, path):
"""
Reads a symlink.
NOTE: Can't create symlinks.
"""
self.open(path, 0)
a = self.stat(path)
if (a.mode&stat.S_IFLNK) == stat.S_IFLNK:
return self.read(path, a.size, 0)
raise FileSystemError("Not symlink")
class SqliteDumpFS(Fuse):
"""
Honestly not sure what this is for, have left it unedited and haven't had it interfering.
"""
block_size = 32*1024
def __init__(self, *args, **kw):
Fuse.__init__(self, *args, **kw)
self.db_path = "./dumpfs.sqlite"
self.__backend = DBDumpFS()
def main(self, *args, **kw):
import os
fullpath = os.path.abspath(os.path.expanduser(os.path.expandvars(
self.db_path)))
self.__backend = DBDumpFS("sqlite:"+fullpath)
Fuse.main(self, *args, **kw)
def getattr(self, path):
print "*** getattr :", path
try:
inode = self.__backend.stat(path)
except FileSystemError:
return -errno.ENOENT
st = PseudoStat()
st.st_atime = inode.atime
st.st_ctime = inode.ctime
st.st_dev = inode.dev
st.st_gid = inode.gid
st.st_ino = inode.inode_num
st.st_mode = inode.mode
st.st_mtime = inode.mtime
if inode.mode&stat.S_IFDIR:
st.st_nlink = 2
else:
st.st_nlink = 1
st.st_size = inode.size
st.st_uid = inode.uid
return st
def readdir(self, path, offset):
print "*** readdir", path, offset
rets = [fuse.Direntry("."), fuse.Direntry("..")]
dlist = self.__backend.readdir(path)
for de in dlist:
ret = fuse.Direntry(str(de.filename))
ret.ino = de.inode_num
rets.append(ret)
return rets[offset:]
def mythread(self):
"""
何ã®ãŸã‚ã®ãƒ¡ã‚½ãƒƒãƒ‰ã‹ä¸æ˜Ž
"""
print "*** mythread"
return -errno.ENOSYS
def chmod(self, path, mode):
"""
åŒåã®ã‚³ãƒžãƒ³ãƒ‰ã¨ã»ã¼åŒã˜
"""
print "*** chmod :", path, oct(mode)
self.__backend.chmod(path, mode)
return 0
def chown (self, path, uid, gid):
print '*** chown', path, uid, gid
self.__backend.chown(path, uid, gid)
return 0
def fsync (self, path, isFsyncFile):
print '*** fsync', path, isFsyncFile
self.__backend.fsync()
return 0
def link (self, targetPath, linkPath):
"""
ãƒãƒ¼ãƒ‰ãƒªãƒ³ã‚¯ã®ç”Ÿæˆ
"""
print '*** link', targetPath, linkPath
self.__backend.link(targetPath, linkPath)
return 0
def mkdir (self, path, mode):
"""
ディレクトリã®ç”Ÿæˆ
"""
print '*** mkdir', path, oct(mode)
self.__backend.mkdir(path, mode)
return 0
def mknod (self, path, mode, dev):
"""
pathã§æŒ‡å®šã•ã‚ŒãŸãƒ‘スをæŒã¤ãƒ•ã‚¡ã‚¤ãƒ«ã‚’生æˆã™ã‚‹
modeãŒS_IFCHRã¨S_IFBLK以外ãªã‚‰devã¯ç„¡è¦–ã™ã‚‹
S_IFCHRã¨S_IFBLKãªã‚‰devã¯ä½œæˆã™ã‚‹ãƒ‡ãƒã‚¤ã‚¹ãƒ•ã‚¡ã‚¤ãƒ«ã®
メジャー番å·ã¨ãƒžã‚¤ãƒŠãƒ¼ç•ªå·
"""
print '*** mknod', path, oct(mode), dev
self.__backend.mknod(path, mode, dev)
return 0
def create(self, path, flags, mode):
print '*** create', path, flags, oct(mode)
self.__backend.create(path, mode)
return 0
def open (self, path, flags):
self.__backend.open(path, flags)
return 0
def read (self, path, length, offset):
"""
DBã‹ã‚‰è©²å½“部分をå–り出ã™
"""
print '*** read', path, length, offset
return self.__backend.read(path, length, offset)
def flush(self, path):
print '*** flush', path
self.__backend.flush(path)
return 0
def readlink (self, path):
"""
symlinkã®ãƒªãƒ³ã‚¯å…ˆã®æŽ¢ç´¢
"""
print '*** readlink', path
return self.__backend.readlink(path)
def release (self, path, flags):
"""
openfilesã‹ã‚‰è©²å½“エントリã®å‰Šé™¤
dirtyフラグãŒç«‹ã£ã¦ã„ã‚‹ãªã‚‰DBã¸ã®æ›¸ãè¾¼ã¿ã‚‚è¡Œã†
"""
print "*** release :", path, flags
self.__backend.close(path)
return 0
def rename (self, oldPath, newPath):
"""