1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 from __future__ import absolute_import
6 from builtins import bytes
13 from .fresh import FreshBase, convertTime
15 _logger = logging.getLogger('arvados.arvados_fuse')
17 class File(FreshBase):
18 """Base for file objects."""
20 __slots__ = ("inode", "parent_inode", "_mtime")
22 def __init__(self, parent_inode, _mtime=0):
23 super(File, self).__init__()
25 self.parent_inode = parent_inode
31 def readfrom(self, off, size, num_retries=0):
34 def writeto(self, off, size, num_retries=0):
35 raise Exception("Not writable")
50 class FuseArvadosFile(File):
51 """Wraps a ArvadosFile."""
53 __slots__ = ('arvfile', '_enable_write')
55 def __init__(self, parent_inode, arvfile, _mtime, enable_write):
56 super(FuseArvadosFile, self).__init__(parent_inode, _mtime)
57 self.arvfile = arvfile
58 self._enable_write = enable_write
61 with llfuse.lock_released:
62 return self.arvfile.size()
64 def readfrom(self, off, size, num_retries=0):
65 with llfuse.lock_released:
66 return self.arvfile.readfrom(off, size, num_retries, exact=True)
68 def writeto(self, off, buf, num_retries=0):
69 with llfuse.lock_released:
70 return self.arvfile.writeto(off, buf, num_retries)
76 return self._enable_write and self.arvfile.writable()
79 with llfuse.lock_released:
81 self.arvfile.parent.root_collection().save()
84 class StringFile(File):
85 """Wrap a simple string as a file"""
86 def __init__(self, parent_inode, contents, _mtime):
87 super(StringFile, self).__init__(parent_inode, _mtime)
88 self.contents = contents
91 return len(self.contents)
93 def readfrom(self, off, size, num_retries=0):
94 return bytes(self.contents[off:(off+size)], encoding='utf-8')
97 class ObjectFile(StringFile):
98 """Wrap a dict as a serialized json object."""
100 def __init__(self, parent_inode, obj):
101 super(ObjectFile, self).__init__(parent_inode, "", 0)
102 self.object_uuid = obj['uuid']
106 return self.object_uuid
108 def update(self, obj=None):
110 # TODO: retrieve the current record for self.object_uuid
111 # from the server. For now, at least don't crash when
112 # someone tells us it's a good time to update but doesn't
113 # pass us a fresh obj. See #8345
115 self._mtime = convertTime(obj['modified_at']) if 'modified_at' in obj else 0
116 self.contents = json.dumps(obj, indent=4, sort_keys=True) + "\n"
122 class FuncToJSONFile(StringFile):
123 """File content is the return value of a given function, encoded as JSON.
125 The function is called at the time the file is read. The result is
126 cached until invalidate() is called.
128 def __init__(self, parent_inode, func):
129 super(FuncToJSONFile, self).__init__(parent_inode, "", 0)
132 # invalidate_inode() is asynchronous with no callback to wait for. In
133 # order to guarantee userspace programs don't get stale data that was
134 # generated before the last invalidate(), we must disallow inode
136 self.allow_attr_cache = False
140 return super(FuncToJSONFile, self).size()
142 def readfrom(self, *args, **kwargs):
144 return super(FuncToJSONFile, self).readfrom(*args, **kwargs)
149 self._mtime = time.time()
151 self.contents = json.dumps(obj, indent=4, sort_keys=True) + "\n"