1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
11 from fresh import FreshBase, convertTime
13 _logger = logging.getLogger('arvados.arvados_fuse')
15 class File(FreshBase):
16 """Base for file objects."""
18 __slots__ = ("inode", "parent_inode", "_mtime")
20 def __init__(self, parent_inode, _mtime=0):
21 super(File, self).__init__()
23 self.parent_inode = parent_inode
29 def readfrom(self, off, size, num_retries=0):
32 def writeto(self, off, size, num_retries=0):
33 raise Exception("Not writable")
48 class FuseArvadosFile(File):
49 """Wraps a ArvadosFile."""
51 __slots__ = ('arvfile',)
53 def __init__(self, parent_inode, arvfile, _mtime):
54 super(FuseArvadosFile, self).__init__(parent_inode, _mtime)
55 self.arvfile = arvfile
58 with llfuse.lock_released:
59 return self.arvfile.size()
61 def readfrom(self, off, size, num_retries=0):
62 with llfuse.lock_released:
63 return self.arvfile.readfrom(off, size, num_retries, exact=True)
65 def writeto(self, off, buf, num_retries=0):
66 with llfuse.lock_released:
67 return self.arvfile.writeto(off, buf, num_retries)
73 return self.arvfile.writable()
76 with llfuse.lock_released:
78 self.arvfile.parent.root_collection().save()
81 class StringFile(File):
82 """Wrap a simple string as a file"""
83 def __init__(self, parent_inode, contents, _mtime):
84 super(StringFile, self).__init__(parent_inode, _mtime)
85 self.contents = contents
88 return len(self.contents)
90 def readfrom(self, off, size, num_retries=0):
91 return self.contents[off:(off+size)]
94 class ObjectFile(StringFile):
95 """Wrap a dict as a serialized json object."""
97 def __init__(self, parent_inode, obj):
98 super(ObjectFile, self).__init__(parent_inode, "", 0)
99 self.object_uuid = obj['uuid']
103 return self.object_uuid
105 def update(self, obj=None):
107 # TODO: retrieve the current record for self.object_uuid
108 # from the server. For now, at least don't crash when
109 # someone tells us it's a good time to update but doesn't
110 # pass us a fresh obj. See #8345
112 self._mtime = convertTime(obj['modified_at']) if 'modified_at' in obj else 0
113 self.contents = json.dumps(obj, indent=4, sort_keys=True) + "\n"
119 class FuncToJSONFile(StringFile):
120 """File content is the return value of a given function, encoded as JSON.
122 The function is called at the time the file is read. The result is
123 cached until invalidate() is called.
125 def __init__(self, parent_inode, func):
126 super(FuncToJSONFile, self).__init__(parent_inode, "", 0)
129 # invalidate_inode() is asynchronous with no callback to wait for. In
130 # order to guarantee userspace programs don't get stale data that was
131 # generated before the last invalidate(), we must disallow inode
133 self.allow_attr_cache = False
137 return super(FuncToJSONFile, self).size()
139 def readfrom(self, *args, **kwargs):
141 return super(FuncToJSONFile, self).readfrom(*args, **kwargs)
146 self._mtime = time.time()
148 self.contents = json.dumps(obj, indent=4, sort_keys=True) + "\n"