2013-07-21 22:41:06 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
import errno
|
2014-03-28 21:51:09 +00:00
|
|
|
import io
|
2013-07-21 22:41:06 +00:00
|
|
|
import llfuse
|
2014-03-26 21:42:20 +00:00
|
|
|
import msgpack
|
2013-07-21 22:41:06 +00:00
|
|
|
import os
|
2013-07-23 11:07:48 +00:00
|
|
|
import stat
|
2014-03-28 21:51:09 +00:00
|
|
|
import tempfile
|
2013-07-21 22:41:06 +00:00
|
|
|
import time
|
2014-03-26 20:47:01 +00:00
|
|
|
from attic.archive import Archive
|
2013-07-27 12:44:12 +00:00
|
|
|
from attic.helpers import daemonize
|
2014-03-26 21:42:20 +00:00
|
|
|
from attic.remote import cache_if_remote
|
|
|
|
|
2013-07-30 12:52:02 +00:00
|
|
|
# Does this version of llfuse support ns precision?
|
|
|
|
have_fuse_mtime_ns = hasattr(llfuse.EntryAttributes, 'st_mtime_ns')
|
2013-07-27 12:44:12 +00:00
|
|
|
|
2013-07-21 22:41:06 +00:00
|
|
|
|
2014-03-28 21:51:09 +00:00
|
|
|
class ItemCache:
|
|
|
|
def __init__(self):
|
|
|
|
self.fd = tempfile.TemporaryFile()
|
|
|
|
self.offset = 1000000
|
|
|
|
|
|
|
|
def add(self, item):
|
|
|
|
pos = self.fd.seek(0, io.SEEK_END)
|
|
|
|
self.fd.write(msgpack.packb(item))
|
|
|
|
return pos + self.offset
|
|
|
|
|
|
|
|
def get(self, inode):
|
|
|
|
self.fd.seek(inode - self.offset, io.SEEK_SET)
|
|
|
|
return next(msgpack.Unpacker(self.fd))
|
|
|
|
|
|
|
|
|
2013-07-21 22:41:06 +00:00
|
|
|
class AtticOperations(llfuse.Operations):
|
2013-07-24 11:05:47 +00:00
|
|
|
"""Export Attic archive as a fuse filesystem
|
2013-07-21 22:41:06 +00:00
|
|
|
"""
|
2014-03-26 20:47:01 +00:00
|
|
|
def __init__(self, key, repository, manifest, archive):
|
2013-07-21 22:41:06 +00:00
|
|
|
super(AtticOperations, self).__init__()
|
2013-07-23 11:40:54 +00:00
|
|
|
self._inode_count = 0
|
2013-07-21 22:41:06 +00:00
|
|
|
self.key = key
|
2014-03-26 21:42:20 +00:00
|
|
|
self.repository = cache_if_remote(repository)
|
2013-07-21 22:41:06 +00:00
|
|
|
self.items = {}
|
2013-07-23 11:40:54 +00:00
|
|
|
self.parent = {}
|
|
|
|
self.contents = defaultdict(dict)
|
2014-03-26 20:47:01 +00:00
|
|
|
self.default_dir = {b'mode': 0o40755, b'mtime': int(time.time() * 1e9), b'uid': os.getuid(), b'gid': os.getgid()}
|
2014-03-27 21:43:06 +00:00
|
|
|
self.pending_archives = {}
|
2014-03-28 21:51:09 +00:00
|
|
|
self.cache = ItemCache()
|
2014-03-26 20:47:01 +00:00
|
|
|
if archive:
|
|
|
|
self.process_archive(archive)
|
|
|
|
else:
|
2014-03-28 21:51:09 +00:00
|
|
|
# Create root inode
|
2014-03-27 21:43:06 +00:00
|
|
|
self.parent[1] = self.allocate_inode()
|
|
|
|
self.items[1] = self.default_dir
|
2014-03-26 20:47:01 +00:00
|
|
|
for archive_name in manifest.archives:
|
2014-03-28 21:51:09 +00:00
|
|
|
# Create archive placeholder inode
|
2014-03-27 21:43:06 +00:00
|
|
|
archive_inode = self.allocate_inode()
|
|
|
|
self.items[archive_inode] = self.default_dir
|
|
|
|
self.parent[archive_inode] = 1
|
|
|
|
self.contents[1][os.fsencode(archive_name)] = archive_inode
|
|
|
|
self.pending_archives[archive_inode] = Archive(repository, key, manifest, archive_name)
|
2014-03-26 20:47:01 +00:00
|
|
|
|
|
|
|
def process_archive(self, archive, prefix=[]):
|
2014-09-07 12:54:18 +00:00
|
|
|
"""Build fuse inode hierarchy from archive metadata
|
2014-03-26 20:47:01 +00:00
|
|
|
"""
|
2014-03-26 21:42:20 +00:00
|
|
|
unpacker = msgpack.Unpacker()
|
|
|
|
for key, chunk in zip(archive.metadata[b'items'], self.repository.get_many(archive.metadata[b'items'])):
|
|
|
|
data = self.key.decrypt(key, chunk)
|
|
|
|
unpacker.feed(data)
|
|
|
|
for item in unpacker:
|
|
|
|
segments = prefix + os.fsencode(os.path.normpath(item[b'path'])).split(b'/')
|
|
|
|
del item[b'path']
|
|
|
|
num_segments = len(segments)
|
|
|
|
parent = 1
|
|
|
|
for i, segment in enumerate(segments, 1):
|
|
|
|
# Insert a default root inode if needed
|
|
|
|
if self._inode_count == 0 and segment:
|
|
|
|
archive_inode = self.allocate_inode()
|
|
|
|
self.items[archive_inode] = self.default_dir
|
|
|
|
self.parent[archive_inode] = parent
|
|
|
|
# Leaf segment?
|
|
|
|
if i == num_segments:
|
|
|
|
if b'source' in item and stat.S_ISREG(item[b'mode']):
|
|
|
|
inode = self._find_inode(item[b'source'], prefix)
|
2014-03-28 21:51:09 +00:00
|
|
|
item = self.cache.get(inode)
|
|
|
|
item[b'nlink'] = item.get(b'nlink', 1) + 1
|
2014-03-26 21:42:20 +00:00
|
|
|
self.items[inode] = item
|
2014-03-28 21:51:09 +00:00
|
|
|
else:
|
|
|
|
inode = self.cache.add(item)
|
2014-03-26 21:42:20 +00:00
|
|
|
self.parent[inode] = parent
|
|
|
|
if segment:
|
|
|
|
self.contents[parent][segment] = inode
|
|
|
|
elif segment in self.contents[parent]:
|
|
|
|
parent = self.contents[parent][segment]
|
2013-07-24 11:05:47 +00:00
|
|
|
else:
|
|
|
|
inode = self.allocate_inode()
|
2014-03-26 21:42:20 +00:00
|
|
|
self.items[inode] = self.default_dir
|
|
|
|
self.parent[inode] = parent
|
|
|
|
if segment:
|
|
|
|
self.contents[parent][segment] = inode
|
|
|
|
parent = inode
|
2013-07-23 11:40:54 +00:00
|
|
|
|
|
|
|
def allocate_inode(self):
|
|
|
|
self._inode_count += 1
|
|
|
|
return self._inode_count
|
2013-07-21 22:41:06 +00:00
|
|
|
|
2013-07-27 12:31:28 +00:00
|
|
|
def statfs(self):
|
|
|
|
stat_ = llfuse.StatvfsData()
|
|
|
|
stat_.f_bsize = 512
|
|
|
|
stat_.f_frsize = 512
|
|
|
|
stat_.f_blocks = 0
|
|
|
|
stat_.f_bfree = 0
|
|
|
|
stat_.f_bavail = 0
|
|
|
|
stat_.f_files = 0
|
|
|
|
stat_.f_ffree = 0
|
|
|
|
stat_.f_favail = 0
|
|
|
|
return stat_
|
|
|
|
|
2014-03-28 21:51:09 +00:00
|
|
|
def get_item(self, inode):
|
|
|
|
try:
|
|
|
|
return self.items[inode]
|
|
|
|
except KeyError:
|
|
|
|
return self.cache.get(inode)
|
|
|
|
|
2014-03-26 20:47:01 +00:00
|
|
|
def _find_inode(self, path, prefix=[]):
|
|
|
|
segments = prefix + os.fsencode(os.path.normpath(path)).split(b'/')
|
2013-07-23 11:07:48 +00:00
|
|
|
inode = 1
|
|
|
|
for segment in segments:
|
2013-07-23 11:40:54 +00:00
|
|
|
inode = self.contents[inode][segment]
|
|
|
|
return inode
|
2013-07-21 22:41:06 +00:00
|
|
|
|
2013-07-23 11:40:54 +00:00
|
|
|
def getattr(self, inode):
|
2014-03-28 21:51:09 +00:00
|
|
|
item = self.get_item(inode)
|
2013-07-21 22:41:06 +00:00
|
|
|
size = 0
|
|
|
|
try:
|
|
|
|
size = sum(size for _, size, _ in item[b'chunks'])
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
entry = llfuse.EntryAttributes()
|
2013-07-23 11:40:54 +00:00
|
|
|
entry.st_ino = inode
|
2013-07-21 22:41:06 +00:00
|
|
|
entry.generation = 0
|
|
|
|
entry.entry_timeout = 300
|
|
|
|
entry.attr_timeout = 300
|
|
|
|
entry.st_mode = item[b'mode']
|
2013-07-23 11:40:54 +00:00
|
|
|
entry.st_nlink = item.get(b'nlink', 1)
|
2013-07-21 22:41:06 +00:00
|
|
|
entry.st_uid = item[b'uid']
|
2013-07-25 20:54:19 +00:00
|
|
|
entry.st_gid = item[b'gid']
|
2013-07-21 22:41:06 +00:00
|
|
|
entry.st_rdev = item.get(b'rdev', 0)
|
|
|
|
entry.st_size = size
|
|
|
|
entry.st_blksize = 512
|
|
|
|
entry.st_blocks = 1
|
2013-07-30 12:52:02 +00:00
|
|
|
if have_fuse_mtime_ns:
|
|
|
|
entry.st_atime_ns = item[b'mtime']
|
|
|
|
entry.st_mtime_ns = item[b'mtime']
|
|
|
|
entry.st_ctime_ns = item[b'mtime']
|
|
|
|
else:
|
|
|
|
entry.st_atime = item[b'mtime'] / 1e9
|
|
|
|
entry.st_mtime = item[b'mtime'] / 1e9
|
|
|
|
entry.st_ctime = item[b'mtime'] / 1e9
|
2013-07-21 22:41:06 +00:00
|
|
|
return entry
|
|
|
|
|
2013-07-23 08:44:29 +00:00
|
|
|
def listxattr(self, inode):
|
2014-03-28 21:51:09 +00:00
|
|
|
item = self.get_item(inode)
|
2013-07-27 20:33:43 +00:00
|
|
|
return item.get(b'xattrs', {}).keys()
|
2013-07-23 08:44:29 +00:00
|
|
|
|
|
|
|
def getxattr(self, inode, name):
|
2014-03-28 21:51:09 +00:00
|
|
|
item = self.get_item(inode)
|
2013-07-23 08:44:29 +00:00
|
|
|
try:
|
|
|
|
return item.get(b'xattrs', {})[name]
|
|
|
|
except KeyError:
|
|
|
|
raise llfuse.FUSEError(errno.ENODATA)
|
|
|
|
|
2014-03-27 21:43:06 +00:00
|
|
|
def _load_pending_archive(self, inode):
|
|
|
|
# Check if this is an archive we need to load
|
|
|
|
archive = self.pending_archives.pop(inode, None)
|
|
|
|
if archive:
|
|
|
|
self.process_archive(archive, [os.fsencode(archive.name)])
|
|
|
|
|
2013-07-21 22:41:06 +00:00
|
|
|
def lookup(self, parent_inode, name):
|
2014-03-27 21:43:06 +00:00
|
|
|
self._load_pending_archive(parent_inode)
|
2013-07-21 22:41:06 +00:00
|
|
|
if name == b'.':
|
|
|
|
inode = parent_inode
|
|
|
|
elif name == b'..':
|
2013-07-23 11:40:54 +00:00
|
|
|
inode = self.parent[parent_inode]
|
2013-07-21 22:41:06 +00:00
|
|
|
else:
|
2013-07-23 11:40:54 +00:00
|
|
|
inode = self.contents[parent_inode].get(name)
|
2013-07-21 22:41:06 +00:00
|
|
|
if not inode:
|
|
|
|
raise llfuse.FUSEError(errno.ENOENT)
|
|
|
|
return self.getattr(inode)
|
|
|
|
|
|
|
|
def open(self, inode, flags):
|
|
|
|
return inode
|
|
|
|
|
|
|
|
def opendir(self, inode):
|
2014-03-27 21:43:06 +00:00
|
|
|
self._load_pending_archive(inode)
|
2013-07-21 22:41:06 +00:00
|
|
|
return inode
|
|
|
|
|
|
|
|
def read(self, fh, offset, size):
|
|
|
|
parts = []
|
2014-03-28 21:51:09 +00:00
|
|
|
item = self.get_item(fh)
|
2013-07-21 22:41:06 +00:00
|
|
|
for id, s, csize in item[b'chunks']:
|
|
|
|
if s < offset:
|
|
|
|
offset -= s
|
|
|
|
continue
|
|
|
|
n = min(size, s - offset)
|
|
|
|
chunk = self.key.decrypt(id, self.repository.get(id))
|
|
|
|
parts.append(chunk[offset:offset+n])
|
|
|
|
offset = 0
|
|
|
|
size -= n
|
|
|
|
if not size:
|
|
|
|
break
|
|
|
|
return b''.join(parts)
|
|
|
|
|
|
|
|
def readdir(self, fh, off):
|
2013-07-23 11:40:54 +00:00
|
|
|
entries = [(b'.', fh), (b'..', self.parent[fh])]
|
|
|
|
entries.extend(self.contents[fh].items())
|
2013-07-21 22:41:06 +00:00
|
|
|
for i, (name, inode) in enumerate(entries[off:], off):
|
|
|
|
yield name, self.getattr(inode), i + 1
|
|
|
|
|
|
|
|
def readlink(self, inode):
|
2014-03-28 21:51:09 +00:00
|
|
|
item = self.get_item(inode)
|
|
|
|
return os.fsencode(item[b'source'])
|
2013-07-23 11:40:54 +00:00
|
|
|
|
2013-07-27 12:44:12 +00:00
|
|
|
def mount(self, mountpoint, extra_options, foreground=False):
|
|
|
|
options = ['fsname=atticfs', 'ro']
|
|
|
|
if extra_options:
|
|
|
|
options.extend(extra_options.split(','))
|
|
|
|
llfuse.init(self, mountpoint, options)
|
|
|
|
if not foreground:
|
|
|
|
daemonize()
|
2013-07-23 11:40:54 +00:00
|
|
|
try:
|
|
|
|
llfuse.main(single=True)
|
|
|
|
except:
|
|
|
|
llfuse.close()
|
|
|
|
raise
|
|
|
|
llfuse.close()
|