Merge branch 'master' into 6602-job-runtime-display
[arvados.git] / services / fuse / arvados_fuse / fusefile.py
1 import logging
2 import re
3 import json
4 import llfuse
5
6 from fresh import FreshBase, convertTime
7
8 _logger = logging.getLogger('arvados.arvados_fuse')
9
10 class File(FreshBase):
11     """Base for file objects."""
12
13     def __init__(self, parent_inode, _mtime=0):
14         super(File, self).__init__()
15         self.inode = None
16         self.parent_inode = parent_inode
17         self._mtime = _mtime
18
19     def size(self):
20         return 0
21
22     def readfrom(self, off, size, num_retries=0):
23         return ''
24
25     def writeto(self, off, size, num_retries=0):
26         raise Exception("Not writable")
27
28     def mtime(self):
29         return self._mtime
30
31     def clear(self, force=False):
32         return True
33
34     def writable(self):
35         return False
36
37     def flush(self):
38         pass
39
40
41 class FuseArvadosFile(File):
42     """Wraps a ArvadosFile."""
43
44     def __init__(self, parent_inode, arvfile, _mtime):
45         super(FuseArvadosFile, self).__init__(parent_inode, _mtime)
46         self.arvfile = arvfile
47
48     def size(self):
49         with llfuse.lock_released:
50             return self.arvfile.size()
51
52     def readfrom(self, off, size, num_retries=0):
53         with llfuse.lock_released:
54             return self.arvfile.readfrom(off, size, num_retries, exact=True)
55
56     def writeto(self, off, buf, num_retries=0):
57         with llfuse.lock_released:
58             return self.arvfile.writeto(off, buf, num_retries)
59
60     def stale(self):
61         return False
62
63     def writable(self):
64         return self.arvfile.writable()
65
66     def flush(self):
67         with llfuse.lock_released:
68             if self.writable():
69                 self.arvfile.parent.root_collection().save()
70
71
72 class StringFile(File):
73     """Wrap a simple string as a file"""
74     def __init__(self, parent_inode, contents, _mtime):
75         super(StringFile, self).__init__(parent_inode, _mtime)
76         self.contents = contents
77
78     def size(self):
79         return len(self.contents)
80
81     def readfrom(self, off, size, num_retries=0):
82         return self.contents[off:(off+size)]
83
84
85 class ObjectFile(StringFile):
86     """Wrap a dict as a serialized json object."""
87
88     def __init__(self, parent_inode, obj):
89         super(ObjectFile, self).__init__(parent_inode, "", 0)
90         self.object_uuid = obj['uuid']
91         self.update(obj)
92
93     def uuid(self):
94         return self.object_uuid
95
96     def update(self, obj=None):
97         self._mtime = convertTime(obj['modified_at']) if 'modified_at' in obj else 0
98         self.contents = json.dumps(obj, indent=4, sort_keys=True) + "\n"
99
100     def persisted(self):
101         return True