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 if self.parent_inode is None:
85 self.arvfile.fuse_entry = None
89 class StringFile(File):
90 """Wrap a simple string as a file"""
92 __slots__ = ("contents",)
94 def __init__(self, parent_inode, contents, _mtime):
95 super(StringFile, self).__init__(parent_inode, _mtime)
96 self.contents = contents
99 return len(self.contents)
101 def readfrom(self, off, size, num_retries=0):
102 return bytes(self.contents[off:(off+size)], encoding='utf-8')
105 class ObjectFile(StringFile):
106 """Wrap a dict as a serialized json object."""
108 __slots__ = ("object_uuid",)
110 def __init__(self, parent_inode, obj):
111 super(ObjectFile, self).__init__(parent_inode, "", 0)
112 self.object_uuid = obj['uuid']
116 return self.object_uuid
118 def update(self, obj=None):
120 # TODO: retrieve the current record for self.object_uuid
121 # from the server. For now, at least don't crash when
122 # someone tells us it's a good time to update but doesn't
123 # pass us a fresh obj. See #8345
125 self._mtime = convertTime(obj['modified_at']) if 'modified_at' in obj else 0
126 self.contents = json.dumps(obj, indent=4, sort_keys=True) + "\n"
132 class FuncToJSONFile(StringFile):
133 """File content is the return value of a given function, encoded as JSON.
135 The function is called at the time the file is read. The result is
136 cached until invalidate() is called.
139 __slots__ = ("func",)
141 def __init__(self, parent_inode, func):
142 super(FuncToJSONFile, self).__init__(parent_inode, "", 0)
145 # invalidate_inode() is asynchronous with no callback to wait for. In
146 # order to guarantee userspace programs don't get stale data that was
147 # generated before the last invalidate(), we must disallow inode
149 self.allow_attr_cache = False
153 return super(FuncToJSONFile, self).size()
155 def readfrom(self, *args, **kwargs):
157 return super(FuncToJSONFile, self).readfrom(*args, **kwargs)
162 self._mtime = time.time()
164 self.contents = json.dumps(obj, indent=4, sort_keys=True) + "\n"