2020-08-24 17:57:46 +00:00
|
|
|
"""
|
|
|
|
URIs (kinda sorta, really they're capabilities?).
|
|
|
|
|
|
|
|
Ported to Python 3.
|
|
|
|
|
|
|
|
Methods ending in to_string() are actually to_bytes(), possibly should be fixed
|
|
|
|
in follow-up port.
|
|
|
|
"""
|
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from future.utils import PY2
|
|
|
|
if PY2:
|
2020-09-21 14:35:56 +00:00
|
|
|
# Don't import bytes or str, to prevent future's newbytes leaking and
|
|
|
|
# breaking code that only expects normal bytes.
|
2020-09-17 17:39:47 +00:00
|
|
|
from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, dict, list, object, range, max, min # noqa: F401
|
|
|
|
str = unicode
|
2020-08-24 17:57:46 +00:00
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
from past.builtins import unicode, long
|
2007-01-17 04:29:59 +00:00
|
|
|
|
2013-07-18 19:51:06 +00:00
|
|
|
import re
|
|
|
|
|
2017-02-27 17:56:49 +00:00
|
|
|
from zope.interface import implementer
|
2007-07-21 22:40:36 +00:00
|
|
|
from twisted.python.components import registerAdapter
|
2013-07-18 19:51:06 +00:00
|
|
|
|
2009-02-18 21:46:55 +00:00
|
|
|
from allmydata.storage.server import si_a2b, si_b2a
|
2008-02-15 02:45:12 +00:00
|
|
|
from allmydata.util import base32, hashutil
|
2013-04-15 18:30:49 +00:00
|
|
|
from allmydata.util.assertutil import _assert
|
2009-01-07 19:24:51 +00:00
|
|
|
from allmydata.interfaces import IURI, IDirnodeURI, IFileURI, IImmutableFileURI, \
|
2010-01-27 06:44:30 +00:00
|
|
|
IVerifierURI, IMutableFileURI, IDirectoryURI, IReadonlyDirectoryURI, \
|
|
|
|
MustBeDeepImmutableError, MustBeReadonlyError, CapConstraintError
|
2007-07-12 23:17:49 +00:00
|
|
|
|
2010-01-27 06:44:30 +00:00
|
|
|
class BadURIError(CapConstraintError):
|
2009-07-15 06:45:10 +00:00
|
|
|
pass
|
|
|
|
|
2010-01-27 07:03:09 +00:00
|
|
|
# The URI shall be an ASCII representation of a reference to the file/directory.
|
|
|
|
# It shall contain enough information to retrieve and validate the contents.
|
|
|
|
# It shall be expressed in a limited character set (currently base32 plus ':' and
|
|
|
|
# capital letters, but future URIs might use a larger charset).
|
|
|
|
|
|
|
|
# TODO:
|
|
|
|
# - rename all of the *URI classes/interfaces to *Cap
|
|
|
|
# - make variable and method names consistently use _uri for an URI string,
|
|
|
|
# and _cap for a Cap object (decoded URI)
|
2007-01-17 04:29:59 +00:00
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
BASE32STR_128bits = b'(%s{25}%s)' % (base32.BASE32CHAR, base32.BASE32CHAR_3bits)
|
|
|
|
BASE32STR_256bits = b'(%s{51}%s)' % (base32.BASE32CHAR, base32.BASE32CHAR_1bits)
|
2007-12-19 00:44:24 +00:00
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
NUMBER=b'([0-9]+)'
|
2007-12-19 00:44:24 +00:00
|
|
|
|
2007-06-02 01:48:01 +00:00
|
|
|
|
2017-02-27 17:56:49 +00:00
|
|
|
class _BaseURI(object):
|
2007-07-21 22:40:36 +00:00
|
|
|
def __hash__(self):
|
2008-09-23 18:52:49 +00:00
|
|
|
return self.to_string().__hash__()
|
2010-01-27 07:03:09 +00:00
|
|
|
|
2008-09-23 18:52:49 +00:00
|
|
|
def __eq__(self, them):
|
|
|
|
if isinstance(them, _BaseURI):
|
|
|
|
return self.to_string() == them.to_string()
|
|
|
|
else:
|
|
|
|
return False
|
2010-01-27 07:03:09 +00:00
|
|
|
|
2008-09-23 18:52:49 +00:00
|
|
|
def __ne__(self, them):
|
|
|
|
if isinstance(them, _BaseURI):
|
|
|
|
return self.to_string() != them.to_string()
|
|
|
|
else:
|
|
|
|
return True
|
2010-01-27 07:03:09 +00:00
|
|
|
|
2008-11-13 03:17:25 +00:00
|
|
|
def get_storage_index(self):
|
|
|
|
return self.storage_index
|
|
|
|
|
2010-01-27 07:03:09 +00:00
|
|
|
|
2017-02-27 17:56:49 +00:00
|
|
|
@implementer(IURI, IImmutableFileURI)
|
2007-07-21 22:40:36 +00:00
|
|
|
class CHKFileURI(_BaseURI):
|
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
BASE_STRING=b'URI:CHK:'
|
|
|
|
STRING_RE=re.compile(b'^URI:CHK:'+BASE32STR_128bits+b':'+
|
|
|
|
BASE32STR_256bits+b':'+NUMBER+b':'+NUMBER+b':'+NUMBER+
|
|
|
|
b'$')
|
2008-01-03 23:55:43 +00:00
|
|
|
|
|
|
|
def __init__(self, key, uri_extension_hash, needed_shares, total_shares,
|
|
|
|
size):
|
|
|
|
self.key = key
|
|
|
|
self.uri_extension_hash = uri_extension_hash
|
|
|
|
self.needed_shares = needed_shares
|
|
|
|
self.total_shares = total_shares
|
|
|
|
self.size = size
|
2008-02-01 19:27:37 +00:00
|
|
|
self.storage_index = hashutil.storage_index_hash(self.key)
|
2009-07-15 06:45:10 +00:00
|
|
|
if not len(self.storage_index) == 16: # sha256 hash truncated to 128
|
|
|
|
raise BadURIError("storage index must be 16 bytes long")
|
2008-01-03 23:55:43 +00:00
|
|
|
|
2008-01-03 05:12:15 +00:00
|
|
|
@classmethod
|
|
|
|
def init_from_string(cls, uri):
|
2008-01-03 23:55:43 +00:00
|
|
|
mo = cls.STRING_RE.search(uri)
|
2009-07-15 06:45:10 +00:00
|
|
|
if not mo:
|
2010-01-27 06:44:30 +00:00
|
|
|
raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
|
2008-02-15 02:27:47 +00:00
|
|
|
return cls(base32.a2b(mo.group(1)), base32.a2b(mo.group(2)),
|
2008-01-03 23:55:43 +00:00
|
|
|
int(mo.group(3)), int(mo.group(4)), int(mo.group(5)))
|
2007-07-22 01:23:15 +00:00
|
|
|
|
|
|
|
def to_string(self):
|
2007-07-21 22:40:36 +00:00
|
|
|
assert isinstance(self.needed_shares, int)
|
|
|
|
assert isinstance(self.total_shares, int)
|
|
|
|
assert isinstance(self.size, (int,long))
|
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
return (b'URI:CHK:%s:%s:%d:%d:%d' %
|
2008-02-15 02:27:47 +00:00
|
|
|
(base32.b2a(self.key),
|
|
|
|
base32.b2a(self.uri_extension_hash),
|
2007-07-21 22:40:36 +00:00
|
|
|
self.needed_shares,
|
|
|
|
self.total_shares,
|
|
|
|
self.size))
|
|
|
|
|
|
|
|
def is_readonly(self):
|
|
|
|
return True
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2007-07-21 22:40:36 +00:00
|
|
|
def is_mutable(self):
|
|
|
|
return False
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2007-07-21 22:40:36 +00:00
|
|
|
def get_readonly(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def get_size(self):
|
|
|
|
return self.size
|
|
|
|
|
2008-12-08 19:44:11 +00:00
|
|
|
def get_verify_cap(self):
|
2007-10-15 23:16:39 +00:00
|
|
|
return CHKFileVerifierURI(storage_index=self.storage_index,
|
|
|
|
uri_extension_hash=self.uri_extension_hash,
|
|
|
|
needed_shares=self.needed_shares,
|
|
|
|
total_shares=self.total_shares,
|
|
|
|
size=self.size)
|
|
|
|
|
2013-07-18 19:51:06 +00:00
|
|
|
|
2017-02-27 17:56:49 +00:00
|
|
|
@implementer(IVerifierURI)
|
2007-10-15 23:16:39 +00:00
|
|
|
class CHKFileVerifierURI(_BaseURI):
|
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
BASE_STRING=b'URI:CHK-Verifier:'
|
|
|
|
STRING_RE=re.compile(b'^URI:CHK-Verifier:'+BASE32STR_128bits+b':'+
|
|
|
|
BASE32STR_256bits+b':'+NUMBER+b':'+NUMBER+b':'+NUMBER)
|
2007-12-05 00:38:31 +00:00
|
|
|
|
2008-01-03 23:55:43 +00:00
|
|
|
def __init__(self, storage_index, uri_extension_hash,
|
2007-12-05 00:38:31 +00:00
|
|
|
needed_shares, total_shares, size):
|
2008-01-03 23:55:43 +00:00
|
|
|
assert len(storage_index) == 16
|
2007-12-05 00:38:31 +00:00
|
|
|
self.storage_index = storage_index
|
|
|
|
self.uri_extension_hash = uri_extension_hash
|
|
|
|
self.needed_shares = needed_shares
|
|
|
|
self.total_shares = total_shares
|
|
|
|
self.size = size
|
2007-10-15 23:16:39 +00:00
|
|
|
|
2008-01-03 05:12:15 +00:00
|
|
|
@classmethod
|
|
|
|
def init_from_string(cls, uri):
|
2008-01-03 23:55:43 +00:00
|
|
|
mo = cls.STRING_RE.search(uri)
|
2010-01-27 06:44:30 +00:00
|
|
|
if not mo:
|
|
|
|
raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
|
2009-02-18 21:46:55 +00:00
|
|
|
return cls(si_a2b(mo.group(1)), base32.a2b(mo.group(2)),
|
2008-01-03 23:55:43 +00:00
|
|
|
int(mo.group(3)), int(mo.group(4)), int(mo.group(5)))
|
2007-10-15 23:16:39 +00:00
|
|
|
|
|
|
|
def to_string(self):
|
|
|
|
assert isinstance(self.needed_shares, int)
|
|
|
|
assert isinstance(self.total_shares, int)
|
|
|
|
assert isinstance(self.size, (int,long))
|
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
return (b'URI:CHK-Verifier:%s:%s:%d:%d:%d' %
|
2009-02-18 21:46:55 +00:00
|
|
|
(si_b2a(self.storage_index),
|
2008-02-15 02:27:47 +00:00
|
|
|
base32.b2a(self.uri_extension_hash),
|
2007-10-15 23:16:39 +00:00
|
|
|
self.needed_shares,
|
|
|
|
self.total_shares,
|
|
|
|
self.size))
|
|
|
|
|
2010-01-27 06:44:30 +00:00
|
|
|
def is_readonly(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def is_mutable(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_readonly(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def get_verify_cap(self):
|
|
|
|
return self
|
|
|
|
|
2007-10-15 23:16:39 +00:00
|
|
|
|
2017-02-27 17:56:49 +00:00
|
|
|
@implementer(IURI, IImmutableFileURI)
|
2007-07-21 22:40:36 +00:00
|
|
|
class LiteralFileURI(_BaseURI):
|
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
BASE_STRING=b'URI:LIT:'
|
|
|
|
STRING_RE=re.compile(b'^URI:LIT:'+base32.BASE32STR_anybytes+b'$')
|
2008-01-03 23:55:43 +00:00
|
|
|
|
2007-07-21 22:40:36 +00:00
|
|
|
def __init__(self, data=None):
|
|
|
|
if data is not None:
|
2020-08-24 17:53:27 +00:00
|
|
|
assert isinstance(data, bytes)
|
2007-07-21 22:40:36 +00:00
|
|
|
self.data = data
|
|
|
|
|
2008-01-03 05:12:15 +00:00
|
|
|
@classmethod
|
|
|
|
def init_from_string(cls, uri):
|
2008-01-03 23:55:43 +00:00
|
|
|
mo = cls.STRING_RE.search(uri)
|
2010-01-27 06:44:30 +00:00
|
|
|
if not mo:
|
|
|
|
raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
|
2008-02-15 02:27:47 +00:00
|
|
|
return cls(base32.a2b(mo.group(1)))
|
2007-07-21 22:40:36 +00:00
|
|
|
|
|
|
|
def to_string(self):
|
2020-08-24 17:41:58 +00:00
|
|
|
return b'URI:LIT:%s' % base32.b2a(self.data)
|
2007-07-21 22:40:36 +00:00
|
|
|
|
|
|
|
def is_readonly(self):
|
|
|
|
return True
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2007-07-21 22:40:36 +00:00
|
|
|
def is_mutable(self):
|
|
|
|
return False
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2007-07-21 22:40:36 +00:00
|
|
|
def get_readonly(self):
|
|
|
|
return self
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2008-11-13 03:17:25 +00:00
|
|
|
def get_storage_index(self):
|
|
|
|
return None
|
2007-07-21 22:40:36 +00:00
|
|
|
|
2008-12-08 19:44:11 +00:00
|
|
|
def get_verify_cap(self):
|
2007-10-15 23:16:39 +00:00
|
|
|
# LIT files need no verification, all the data is present in the URI
|
|
|
|
return None
|
|
|
|
|
2007-07-21 22:40:36 +00:00
|
|
|
def get_size(self):
|
|
|
|
return len(self.data)
|
|
|
|
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2017-02-27 17:56:49 +00:00
|
|
|
@implementer(IURI, IMutableFileURI)
|
2007-11-01 22:15:29 +00:00
|
|
|
class WriteableSSKFileURI(_BaseURI):
|
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
BASE_STRING=b'URI:SSK:'
|
|
|
|
STRING_RE=re.compile(b'^'+BASE_STRING+BASE32STR_128bits+b':'+
|
|
|
|
BASE32STR_256bits+b'$')
|
2008-01-03 23:55:43 +00:00
|
|
|
|
2008-01-03 05:12:15 +00:00
|
|
|
def __init__(self, writekey, fingerprint):
|
2007-11-01 22:15:29 +00:00
|
|
|
self.writekey = writekey
|
|
|
|
self.readkey = hashutil.ssk_readkey_hash(writekey)
|
|
|
|
self.storage_index = hashutil.ssk_storage_index_hash(self.readkey)
|
2008-01-03 23:55:43 +00:00
|
|
|
assert len(self.storage_index) == 16
|
2007-11-01 22:15:29 +00:00
|
|
|
self.fingerprint = fingerprint
|
|
|
|
|
2008-01-03 05:12:15 +00:00
|
|
|
@classmethod
|
|
|
|
def init_from_string(cls, uri):
|
2008-01-03 23:55:43 +00:00
|
|
|
mo = cls.STRING_RE.search(uri)
|
2009-07-15 06:45:10 +00:00
|
|
|
if not mo:
|
|
|
|
raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
|
2008-02-15 02:27:47 +00:00
|
|
|
return cls(base32.a2b(mo.group(1)), base32.a2b(mo.group(2)))
|
2007-11-01 22:15:29 +00:00
|
|
|
|
|
|
|
def to_string(self):
|
2020-08-24 17:53:27 +00:00
|
|
|
assert isinstance(self.writekey, bytes)
|
|
|
|
assert isinstance(self.fingerprint, bytes)
|
2020-08-24 17:41:58 +00:00
|
|
|
return b'URI:SSK:%s:%s' % (base32.b2a(self.writekey),
|
|
|
|
base32.b2a(self.fingerprint))
|
2007-11-01 22:15:29 +00:00
|
|
|
|
2007-12-03 21:52:42 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return "<%s %s>" % (self.__class__.__name__, self.abbrev())
|
|
|
|
|
|
|
|
def abbrev(self):
|
2008-02-15 02:27:47 +00:00
|
|
|
return base32.b2a(self.writekey[:5])
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2009-01-31 02:31:10 +00:00
|
|
|
def abbrev_si(self):
|
|
|
|
return base32.b2a(self.storage_index)[:5]
|
2007-12-03 21:52:42 +00:00
|
|
|
|
2007-11-01 22:15:29 +00:00
|
|
|
def is_readonly(self):
|
|
|
|
return False
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2007-11-01 22:15:29 +00:00
|
|
|
def is_mutable(self):
|
|
|
|
return True
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2007-11-01 22:15:29 +00:00
|
|
|
def get_readonly(self):
|
|
|
|
return ReadonlySSKFileURI(self.readkey, self.fingerprint)
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2008-12-08 19:44:11 +00:00
|
|
|
def get_verify_cap(self):
|
2007-11-01 22:15:29 +00:00
|
|
|
return SSKVerifierURI(self.storage_index, self.fingerprint)
|
|
|
|
|
2013-07-18 19:51:06 +00:00
|
|
|
|
2017-02-27 17:56:49 +00:00
|
|
|
@implementer(IURI, IMutableFileURI)
|
2007-11-01 22:15:29 +00:00
|
|
|
class ReadonlySSKFileURI(_BaseURI):
|
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
BASE_STRING=b'URI:SSK-RO:'
|
|
|
|
STRING_RE=re.compile(b'^URI:SSK-RO:'+BASE32STR_128bits+b':'+BASE32STR_256bits+b'$')
|
2008-01-03 23:55:43 +00:00
|
|
|
|
2008-01-03 05:12:15 +00:00
|
|
|
def __init__(self, readkey, fingerprint):
|
2007-11-01 22:15:29 +00:00
|
|
|
self.readkey = readkey
|
|
|
|
self.storage_index = hashutil.ssk_storage_index_hash(self.readkey)
|
2008-01-03 23:55:43 +00:00
|
|
|
assert len(self.storage_index) == 16
|
2007-11-01 22:15:29 +00:00
|
|
|
self.fingerprint = fingerprint
|
|
|
|
|
2008-01-03 05:12:15 +00:00
|
|
|
@classmethod
|
|
|
|
def init_from_string(cls, uri):
|
2008-01-03 23:55:43 +00:00
|
|
|
mo = cls.STRING_RE.search(uri)
|
2009-07-15 06:45:10 +00:00
|
|
|
if not mo:
|
2010-01-27 06:44:30 +00:00
|
|
|
raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
|
2008-02-15 02:27:47 +00:00
|
|
|
return cls(base32.a2b(mo.group(1)), base32.a2b(mo.group(2)))
|
2007-11-01 22:15:29 +00:00
|
|
|
|
|
|
|
def to_string(self):
|
2020-08-24 17:53:27 +00:00
|
|
|
assert isinstance(self.readkey, bytes)
|
|
|
|
assert isinstance(self.fingerprint, bytes)
|
2020-08-24 17:41:58 +00:00
|
|
|
return b'URI:SSK-RO:%s:%s' % (base32.b2a(self.readkey),
|
|
|
|
base32.b2a(self.fingerprint))
|
2007-11-01 22:15:29 +00:00
|
|
|
|
2007-12-03 21:52:42 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return "<%s %s>" % (self.__class__.__name__, self.abbrev())
|
|
|
|
|
|
|
|
def abbrev(self):
|
2008-02-15 02:27:47 +00:00
|
|
|
return base32.b2a(self.readkey[:5])
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2009-01-31 02:31:10 +00:00
|
|
|
def abbrev_si(self):
|
|
|
|
return base32.b2a(self.storage_index)[:5]
|
2007-12-03 21:52:42 +00:00
|
|
|
|
2007-11-01 22:15:29 +00:00
|
|
|
def is_readonly(self):
|
|
|
|
return True
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2007-11-01 22:15:29 +00:00
|
|
|
def is_mutable(self):
|
|
|
|
return True
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2007-11-01 22:15:29 +00:00
|
|
|
def get_readonly(self):
|
|
|
|
return self
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2008-12-08 19:44:11 +00:00
|
|
|
def get_verify_cap(self):
|
2007-11-01 22:15:29 +00:00
|
|
|
return SSKVerifierURI(self.storage_index, self.fingerprint)
|
|
|
|
|
2013-07-18 19:51:06 +00:00
|
|
|
|
2017-02-27 17:56:49 +00:00
|
|
|
@implementer(IVerifierURI)
|
2007-11-01 22:15:29 +00:00
|
|
|
class SSKVerifierURI(_BaseURI):
|
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
BASE_STRING=b'URI:SSK-Verifier:'
|
|
|
|
STRING_RE=re.compile(b'^'+BASE_STRING+BASE32STR_128bits+b':'+BASE32STR_256bits+b'$')
|
2008-01-03 23:55:43 +00:00
|
|
|
|
2008-01-03 05:12:15 +00:00
|
|
|
def __init__(self, storage_index, fingerprint):
|
2008-01-03 23:55:43 +00:00
|
|
|
assert len(storage_index) == 16
|
2007-11-01 22:15:29 +00:00
|
|
|
self.storage_index = storage_index
|
|
|
|
self.fingerprint = fingerprint
|
|
|
|
|
2008-01-03 05:12:15 +00:00
|
|
|
@classmethod
|
|
|
|
def init_from_string(cls, uri):
|
2008-01-03 23:55:43 +00:00
|
|
|
mo = cls.STRING_RE.search(uri)
|
2010-01-27 06:44:30 +00:00
|
|
|
if not mo:
|
|
|
|
raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
|
2009-02-18 21:46:55 +00:00
|
|
|
return cls(si_a2b(mo.group(1)), base32.a2b(mo.group(2)))
|
2007-11-01 22:15:29 +00:00
|
|
|
|
|
|
|
def to_string(self):
|
2020-08-24 17:53:27 +00:00
|
|
|
assert isinstance(self.storage_index, bytes)
|
|
|
|
assert isinstance(self.fingerprint, bytes)
|
2020-08-24 17:41:58 +00:00
|
|
|
return b'URI:SSK-Verifier:%s:%s' % (si_b2a(self.storage_index),
|
|
|
|
base32.b2a(self.fingerprint))
|
2007-11-01 22:15:29 +00:00
|
|
|
|
2010-01-27 06:44:30 +00:00
|
|
|
def is_readonly(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def is_mutable(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_readonly(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def get_verify_cap(self):
|
|
|
|
return self
|
|
|
|
|
2013-07-18 19:51:06 +00:00
|
|
|
|
2017-02-27 17:56:49 +00:00
|
|
|
@implementer(IURI, IMutableFileURI)
|
2011-08-27 18:33:57 +00:00
|
|
|
class WriteableMDMFFileURI(_BaseURI):
|
2011-08-07 00:44:36 +00:00
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
BASE_STRING=b'URI:MDMF:'
|
|
|
|
STRING_RE=re.compile(b'^'+BASE_STRING+BASE32STR_128bits+b':'+BASE32STR_256bits+b'(:|$)')
|
2011-08-07 00:44:36 +00:00
|
|
|
|
2011-10-01 23:35:53 +00:00
|
|
|
def __init__(self, writekey, fingerprint):
|
2011-08-07 00:44:36 +00:00
|
|
|
self.writekey = writekey
|
|
|
|
self.readkey = hashutil.ssk_readkey_hash(writekey)
|
|
|
|
self.storage_index = hashutil.ssk_storage_index_hash(self.readkey)
|
|
|
|
assert len(self.storage_index) == 16
|
|
|
|
self.fingerprint = fingerprint
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def init_from_string(cls, uri):
|
|
|
|
mo = cls.STRING_RE.search(uri)
|
|
|
|
if not mo:
|
|
|
|
raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
|
2011-10-01 23:35:53 +00:00
|
|
|
return cls(base32.a2b(mo.group(1)), base32.a2b(mo.group(2)))
|
2011-08-07 00:44:36 +00:00
|
|
|
|
|
|
|
def to_string(self):
|
2020-08-24 17:53:27 +00:00
|
|
|
assert isinstance(self.writekey, bytes)
|
|
|
|
assert isinstance(self.fingerprint, bytes)
|
2020-08-24 17:41:58 +00:00
|
|
|
ret = b'URI:MDMF:%s:%s' % (base32.b2a(self.writekey),
|
|
|
|
base32.b2a(self.fingerprint))
|
2011-08-07 00:44:36 +00:00
|
|
|
return ret
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<%s %s>" % (self.__class__.__name__, self.abbrev())
|
|
|
|
|
|
|
|
def abbrev(self):
|
|
|
|
return base32.b2a(self.writekey[:5])
|
|
|
|
|
|
|
|
def abbrev_si(self):
|
|
|
|
return base32.b2a(self.storage_index)[:5]
|
|
|
|
|
|
|
|
def is_readonly(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def is_mutable(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_readonly(self):
|
2011-10-01 23:35:53 +00:00
|
|
|
return ReadonlyMDMFFileURI(self.readkey, self.fingerprint)
|
2011-08-07 00:44:36 +00:00
|
|
|
|
|
|
|
def get_verify_cap(self):
|
2011-10-01 23:35:53 +00:00
|
|
|
return MDMFVerifierURI(self.storage_index, self.fingerprint)
|
2011-08-07 00:44:36 +00:00
|
|
|
|
2013-07-18 19:51:06 +00:00
|
|
|
|
2017-02-27 17:56:49 +00:00
|
|
|
@implementer(IURI, IMutableFileURI)
|
2011-08-07 00:44:36 +00:00
|
|
|
class ReadonlyMDMFFileURI(_BaseURI):
|
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
BASE_STRING=b'URI:MDMF-RO:'
|
|
|
|
STRING_RE=re.compile(b'^' +BASE_STRING+BASE32STR_128bits+b':'+BASE32STR_256bits+b'(:|$)')
|
2011-08-07 00:44:36 +00:00
|
|
|
|
2011-10-01 23:35:53 +00:00
|
|
|
def __init__(self, readkey, fingerprint):
|
2011-08-07 00:44:36 +00:00
|
|
|
self.readkey = readkey
|
|
|
|
self.storage_index = hashutil.ssk_storage_index_hash(self.readkey)
|
|
|
|
assert len(self.storage_index) == 16
|
|
|
|
self.fingerprint = fingerprint
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def init_from_string(cls, uri):
|
|
|
|
mo = cls.STRING_RE.search(uri)
|
|
|
|
if not mo:
|
|
|
|
raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
|
|
|
|
|
2011-10-01 23:35:53 +00:00
|
|
|
return cls(base32.a2b(mo.group(1)), base32.a2b(mo.group(2)))
|
2011-08-07 00:44:36 +00:00
|
|
|
|
|
|
|
def to_string(self):
|
2020-08-24 17:53:27 +00:00
|
|
|
assert isinstance(self.readkey, bytes)
|
|
|
|
assert isinstance(self.fingerprint, bytes)
|
2020-08-24 17:41:58 +00:00
|
|
|
ret = b'URI:MDMF-RO:%s:%s' % (base32.b2a(self.readkey),
|
|
|
|
base32.b2a(self.fingerprint))
|
2011-08-07 00:44:36 +00:00
|
|
|
return ret
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<%s %s>" % (self.__class__.__name__, self.abbrev())
|
|
|
|
|
|
|
|
def abbrev(self):
|
|
|
|
return base32.b2a(self.readkey[:5])
|
|
|
|
|
|
|
|
def abbrev_si(self):
|
|
|
|
return base32.b2a(self.storage_index)[:5]
|
|
|
|
|
|
|
|
def is_readonly(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def is_mutable(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_readonly(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def get_verify_cap(self):
|
2011-10-01 23:35:53 +00:00
|
|
|
return MDMFVerifierURI(self.storage_index, self.fingerprint)
|
2011-08-07 00:44:36 +00:00
|
|
|
|
2013-07-18 19:51:06 +00:00
|
|
|
|
2017-02-27 17:56:49 +00:00
|
|
|
@implementer(IVerifierURI)
|
2011-08-07 00:44:36 +00:00
|
|
|
class MDMFVerifierURI(_BaseURI):
|
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
BASE_STRING=b'URI:MDMF-Verifier:'
|
|
|
|
STRING_RE=re.compile(b'^'+BASE_STRING+BASE32STR_128bits+b':'+BASE32STR_256bits+b'(:|$)')
|
2011-08-07 00:44:36 +00:00
|
|
|
|
2011-10-01 23:35:53 +00:00
|
|
|
def __init__(self, storage_index, fingerprint):
|
2011-08-07 00:44:36 +00:00
|
|
|
assert len(storage_index) == 16
|
|
|
|
self.storage_index = storage_index
|
|
|
|
self.fingerprint = fingerprint
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def init_from_string(cls, uri):
|
|
|
|
mo = cls.STRING_RE.search(uri)
|
|
|
|
if not mo:
|
|
|
|
raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
|
2011-10-01 23:35:53 +00:00
|
|
|
return cls(si_a2b(mo.group(1)), base32.a2b(mo.group(2)))
|
2011-08-07 00:44:36 +00:00
|
|
|
|
|
|
|
def to_string(self):
|
2020-08-24 17:53:27 +00:00
|
|
|
assert isinstance(self.storage_index, bytes)
|
|
|
|
assert isinstance(self.fingerprint, bytes)
|
2020-08-24 17:41:58 +00:00
|
|
|
ret = b'URI:MDMF-Verifier:%s:%s' % (si_b2a(self.storage_index),
|
|
|
|
base32.b2a(self.fingerprint))
|
2011-08-07 00:44:36 +00:00
|
|
|
return ret
|
|
|
|
|
|
|
|
def is_readonly(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def is_mutable(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_readonly(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def get_verify_cap(self):
|
|
|
|
return self
|
|
|
|
|
2013-07-18 19:51:06 +00:00
|
|
|
|
2017-02-27 17:56:49 +00:00
|
|
|
@implementer(IURI, IDirnodeURI)
|
2009-07-17 01:01:03 +00:00
|
|
|
class _DirectoryBaseURI(_BaseURI):
|
2007-12-03 21:52:42 +00:00
|
|
|
def __init__(self, filenode_uri=None):
|
|
|
|
self._filenode_uri = filenode_uri
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<%s %s>" % (self.__class__.__name__, self.abbrev())
|
|
|
|
|
2008-01-03 23:55:43 +00:00
|
|
|
@classmethod
|
|
|
|
def init_from_string(cls, uri):
|
|
|
|
mo = cls.BASE_STRING_RE.search(uri)
|
2009-07-15 06:45:10 +00:00
|
|
|
if not mo:
|
|
|
|
raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
|
2008-01-03 23:55:43 +00:00
|
|
|
bits = uri[mo.end():]
|
|
|
|
fn = cls.INNER_URI_CLASS.init_from_string(
|
|
|
|
cls.INNER_URI_CLASS.BASE_STRING+bits)
|
|
|
|
return cls(fn)
|
|
|
|
|
|
|
|
def to_string(self):
|
|
|
|
fnuri = self._filenode_uri.to_string()
|
|
|
|
mo = re.match(self.INNER_URI_CLASS.BASE_STRING, fnuri)
|
|
|
|
assert mo, fnuri
|
|
|
|
bits = fnuri[mo.end():]
|
|
|
|
return self.BASE_STRING+bits
|
|
|
|
|
2007-12-03 21:52:42 +00:00
|
|
|
def abbrev(self):
|
2020-08-24 17:41:58 +00:00
|
|
|
return self._filenode_uri.to_string().split(b':')[2][:5]
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2009-01-31 02:31:10 +00:00
|
|
|
def abbrev_si(self):
|
2010-02-22 03:36:52 +00:00
|
|
|
si = self._filenode_uri.get_storage_index()
|
|
|
|
if si is None:
|
2020-08-24 17:41:58 +00:00
|
|
|
return b"<LIT>"
|
2010-02-22 03:36:52 +00:00
|
|
|
return base32.b2a(si)[:5]
|
2007-11-01 22:15:29 +00:00
|
|
|
|
2007-12-03 21:52:42 +00:00
|
|
|
def is_mutable(self):
|
|
|
|
return True
|
|
|
|
|
2010-01-27 06:44:30 +00:00
|
|
|
def get_filenode_cap(self):
|
|
|
|
return self._filenode_uri
|
|
|
|
|
2008-12-08 19:44:11 +00:00
|
|
|
def get_verify_cap(self):
|
2009-07-17 01:01:03 +00:00
|
|
|
return DirectoryURIVerifier(self._filenode_uri.get_verify_cap())
|
2007-12-03 21:52:42 +00:00
|
|
|
|
2008-11-13 03:17:25 +00:00
|
|
|
def get_storage_index(self):
|
|
|
|
return self._filenode_uri.get_storage_index()
|
|
|
|
|
2013-07-18 19:51:06 +00:00
|
|
|
|
2017-02-27 17:56:49 +00:00
|
|
|
@implementer(IDirectoryURI)
|
2009-07-17 01:01:03 +00:00
|
|
|
class DirectoryURI(_DirectoryBaseURI):
|
2008-01-03 23:55:43 +00:00
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
BASE_STRING=b'URI:DIR2:'
|
|
|
|
BASE_STRING_RE=re.compile(b'^'+BASE_STRING)
|
2008-01-03 23:55:43 +00:00
|
|
|
INNER_URI_CLASS=WriteableSSKFileURI
|
|
|
|
|
2007-11-01 22:15:29 +00:00
|
|
|
def __init__(self, filenode_uri=None):
|
|
|
|
if filenode_uri:
|
|
|
|
assert not filenode_uri.is_readonly()
|
2009-07-17 01:01:03 +00:00
|
|
|
_DirectoryBaseURI.__init__(self, filenode_uri)
|
2007-11-01 22:15:29 +00:00
|
|
|
|
|
|
|
def is_readonly(self):
|
|
|
|
return False
|
2007-12-03 21:52:42 +00:00
|
|
|
|
2007-11-01 22:15:29 +00:00
|
|
|
def get_readonly(self):
|
2009-07-17 01:01:03 +00:00
|
|
|
return ReadonlyDirectoryURI(self._filenode_uri.get_readonly())
|
2007-11-01 22:15:29 +00:00
|
|
|
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2017-02-27 17:56:49 +00:00
|
|
|
@implementer(IReadonlyDirectoryURI)
|
2009-07-17 01:01:03 +00:00
|
|
|
class ReadonlyDirectoryURI(_DirectoryBaseURI):
|
2008-01-03 23:55:43 +00:00
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
BASE_STRING=b'URI:DIR2-RO:'
|
|
|
|
BASE_STRING_RE=re.compile(b'^'+BASE_STRING)
|
2008-01-03 23:55:43 +00:00
|
|
|
INNER_URI_CLASS=ReadonlySSKFileURI
|
|
|
|
|
2007-11-01 22:15:29 +00:00
|
|
|
def __init__(self, filenode_uri=None):
|
|
|
|
if filenode_uri:
|
|
|
|
assert filenode_uri.is_readonly()
|
2009-07-17 01:01:03 +00:00
|
|
|
_DirectoryBaseURI.__init__(self, filenode_uri)
|
2007-11-01 22:15:29 +00:00
|
|
|
|
|
|
|
def is_readonly(self):
|
|
|
|
return True
|
2007-12-03 21:52:42 +00:00
|
|
|
|
2007-11-01 22:15:29 +00:00
|
|
|
def get_readonly(self):
|
|
|
|
return self
|
|
|
|
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2009-11-04 17:24:53 +00:00
|
|
|
class _ImmutableDirectoryBaseURI(_DirectoryBaseURI):
|
|
|
|
def __init__(self, filenode_uri=None):
|
|
|
|
if filenode_uri:
|
|
|
|
assert isinstance(filenode_uri, self.INNER_URI_CLASS), filenode_uri
|
2010-01-27 06:44:30 +00:00
|
|
|
assert not filenode_uri.is_mutable()
|
2009-11-04 17:24:53 +00:00
|
|
|
_DirectoryBaseURI.__init__(self, filenode_uri)
|
|
|
|
|
|
|
|
def is_readonly(self):
|
|
|
|
return True
|
|
|
|
|
2010-01-27 06:44:30 +00:00
|
|
|
def is_mutable(self):
|
|
|
|
return False
|
|
|
|
|
2009-11-04 17:24:53 +00:00
|
|
|
def get_readonly(self):
|
|
|
|
return self
|
|
|
|
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2009-11-04 17:24:53 +00:00
|
|
|
class ImmutableDirectoryURI(_ImmutableDirectoryBaseURI):
|
2020-08-24 17:41:58 +00:00
|
|
|
BASE_STRING=b'URI:DIR2-CHK:'
|
|
|
|
BASE_STRING_RE=re.compile(b'^'+BASE_STRING)
|
2009-11-04 17:24:53 +00:00
|
|
|
INNER_URI_CLASS=CHKFileURI
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2009-11-04 17:24:53 +00:00
|
|
|
def get_verify_cap(self):
|
|
|
|
vcap = self._filenode_uri.get_verify_cap()
|
|
|
|
return ImmutableDirectoryURIVerifier(vcap)
|
|
|
|
|
|
|
|
|
|
|
|
class LiteralDirectoryURI(_ImmutableDirectoryBaseURI):
|
2020-08-24 17:41:58 +00:00
|
|
|
BASE_STRING=b'URI:DIR2-LIT:'
|
|
|
|
BASE_STRING_RE=re.compile(b'^'+BASE_STRING)
|
2009-11-04 17:24:53 +00:00
|
|
|
INNER_URI_CLASS=LiteralFileURI
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2009-11-04 17:24:53 +00:00
|
|
|
def get_verify_cap(self):
|
|
|
|
# LIT caps have no verifier, since they aren't distributed
|
|
|
|
return None
|
|
|
|
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2017-02-27 17:56:49 +00:00
|
|
|
@implementer(IDirectoryURI)
|
2011-08-07 00:44:36 +00:00
|
|
|
class MDMFDirectoryURI(_DirectoryBaseURI):
|
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
BASE_STRING=b'URI:DIR2-MDMF:'
|
|
|
|
BASE_STRING_RE=re.compile(b'^'+BASE_STRING)
|
2011-08-27 18:33:57 +00:00
|
|
|
INNER_URI_CLASS=WriteableMDMFFileURI
|
2011-08-07 00:44:36 +00:00
|
|
|
|
|
|
|
def __init__(self, filenode_uri=None):
|
|
|
|
if filenode_uri:
|
|
|
|
assert not filenode_uri.is_readonly()
|
|
|
|
_DirectoryBaseURI.__init__(self, filenode_uri)
|
|
|
|
|
|
|
|
def is_readonly(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_readonly(self):
|
|
|
|
return ReadonlyMDMFDirectoryURI(self._filenode_uri.get_readonly())
|
|
|
|
|
|
|
|
def get_verify_cap(self):
|
|
|
|
return MDMFDirectoryURIVerifier(self._filenode_uri.get_verify_cap())
|
|
|
|
|
|
|
|
|
2017-02-27 17:56:49 +00:00
|
|
|
@implementer(IReadonlyDirectoryURI)
|
2011-08-07 00:44:36 +00:00
|
|
|
class ReadonlyMDMFDirectoryURI(_DirectoryBaseURI):
|
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
BASE_STRING=b'URI:DIR2-MDMF-RO:'
|
|
|
|
BASE_STRING_RE=re.compile(b'^'+BASE_STRING)
|
2011-08-07 00:44:36 +00:00
|
|
|
INNER_URI_CLASS=ReadonlyMDMFFileURI
|
|
|
|
|
|
|
|
def __init__(self, filenode_uri=None):
|
|
|
|
if filenode_uri:
|
|
|
|
assert filenode_uri.is_readonly()
|
|
|
|
_DirectoryBaseURI.__init__(self, filenode_uri)
|
|
|
|
|
|
|
|
def is_readonly(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_readonly(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def get_verify_cap(self):
|
|
|
|
return MDMFDirectoryURIVerifier(self._filenode_uri.get_verify_cap())
|
|
|
|
|
2013-04-15 18:30:49 +00:00
|
|
|
|
2009-11-12 00:22:33 +00:00
|
|
|
def wrap_dirnode_cap(filecap):
|
|
|
|
if isinstance(filecap, WriteableSSKFileURI):
|
|
|
|
return DirectoryURI(filecap)
|
|
|
|
if isinstance(filecap, ReadonlySSKFileURI):
|
|
|
|
return ReadonlyDirectoryURI(filecap)
|
|
|
|
if isinstance(filecap, CHKFileURI):
|
|
|
|
return ImmutableDirectoryURI(filecap)
|
|
|
|
if isinstance(filecap, LiteralFileURI):
|
|
|
|
return LiteralDirectoryURI(filecap)
|
2011-08-27 18:33:57 +00:00
|
|
|
if isinstance(filecap, WriteableMDMFFileURI):
|
2011-08-07 00:44:36 +00:00
|
|
|
return MDMFDirectoryURI(filecap)
|
|
|
|
if isinstance(filecap, ReadonlyMDMFFileURI):
|
|
|
|
return ReadonlyMDMFDirectoryURI(filecap)
|
2013-04-15 18:30:49 +00:00
|
|
|
raise AssertionError("cannot interpret as a directory cap: %s" % filecap.__class__)
|
|
|
|
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2017-02-27 17:56:49 +00:00
|
|
|
@implementer(IVerifierURI)
|
2011-08-07 00:44:36 +00:00
|
|
|
class MDMFDirectoryURIVerifier(_DirectoryBaseURI):
|
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
BASE_STRING=b'URI:DIR2-MDMF-Verifier:'
|
|
|
|
BASE_STRING_RE=re.compile(b'^'+BASE_STRING)
|
2011-08-07 00:44:36 +00:00
|
|
|
INNER_URI_CLASS=MDMFVerifierURI
|
|
|
|
|
|
|
|
def __init__(self, filenode_uri=None):
|
|
|
|
if filenode_uri:
|
2013-04-15 18:30:49 +00:00
|
|
|
_assert(IVerifierURI.providedBy(filenode_uri))
|
2011-08-07 00:44:36 +00:00
|
|
|
self._filenode_uri = filenode_uri
|
|
|
|
|
|
|
|
def get_filenode_cap(self):
|
|
|
|
return self._filenode_uri
|
|
|
|
|
|
|
|
def is_mutable(self):
|
|
|
|
return False
|
2009-11-04 17:24:53 +00:00
|
|
|
|
2011-09-20 03:01:56 +00:00
|
|
|
def is_readonly(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_readonly(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
2017-02-27 17:56:49 +00:00
|
|
|
@implementer(IVerifierURI)
|
2009-07-17 01:01:03 +00:00
|
|
|
class DirectoryURIVerifier(_DirectoryBaseURI):
|
2007-11-01 22:15:29 +00:00
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
BASE_STRING=b'URI:DIR2-Verifier:'
|
|
|
|
BASE_STRING_RE=re.compile(b'^'+BASE_STRING)
|
2008-01-03 23:55:43 +00:00
|
|
|
INNER_URI_CLASS=SSKVerifierURI
|
|
|
|
|
2007-11-01 22:15:29 +00:00
|
|
|
def __init__(self, filenode_uri=None):
|
|
|
|
if filenode_uri:
|
2013-04-15 18:30:49 +00:00
|
|
|
_assert(IVerifierURI.providedBy(filenode_uri))
|
2007-11-01 22:15:29 +00:00
|
|
|
self._filenode_uri = filenode_uri
|
|
|
|
|
2009-11-11 22:25:42 +00:00
|
|
|
def get_filenode_cap(self):
|
2007-11-02 01:35:54 +00:00
|
|
|
return self._filenode_uri
|
|
|
|
|
2010-01-27 06:44:30 +00:00
|
|
|
def is_mutable(self):
|
|
|
|
return False
|
|
|
|
|
2011-09-20 03:01:56 +00:00
|
|
|
def is_readonly(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_readonly(self):
|
|
|
|
return self
|
|
|
|
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2017-02-27 17:56:49 +00:00
|
|
|
@implementer(IVerifierURI)
|
2009-11-04 17:24:53 +00:00
|
|
|
class ImmutableDirectoryURIVerifier(DirectoryURIVerifier):
|
2020-08-24 17:41:58 +00:00
|
|
|
BASE_STRING=b'URI:DIR2-CHK-Verifier:'
|
|
|
|
BASE_STRING_RE=re.compile(b'^'+BASE_STRING)
|
2009-11-04 17:24:53 +00:00
|
|
|
INNER_URI_CLASS=CHKFileVerifierURI
|
|
|
|
|
2010-01-27 06:44:30 +00:00
|
|
|
|
2019-05-15 06:17:44 +00:00
|
|
|
class UnknownURI(object):
|
2010-01-27 06:44:30 +00:00
|
|
|
def __init__(self, uri, error=None):
|
2009-07-03 01:07:49 +00:00
|
|
|
self._uri = uri
|
2010-01-27 06:44:30 +00:00
|
|
|
self._error = error
|
|
|
|
|
2009-07-03 01:07:49 +00:00
|
|
|
def to_string(self):
|
|
|
|
return self._uri
|
2007-11-01 22:15:29 +00:00
|
|
|
|
2010-01-27 06:44:30 +00:00
|
|
|
def get_readonly(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_error(self):
|
|
|
|
return self._error
|
|
|
|
|
|
|
|
def get_verify_cap(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
ALLEGED_READONLY_PREFIX = b'ro.'
|
|
|
|
ALLEGED_IMMUTABLE_PREFIX = b'imm.'
|
2010-01-27 06:44:30 +00:00
|
|
|
|
|
|
|
def from_string(u, deep_immutable=False, name=u"<unknown name>"):
|
2020-08-24 17:41:58 +00:00
|
|
|
"""Create URI from either unicode or byte string."""
|
|
|
|
if isinstance(u, unicode):
|
|
|
|
u = u.encode("utf-8")
|
|
|
|
if not isinstance(u, bytes):
|
|
|
|
raise TypeError("URI must be unicode string or bytes: %r" % (u,))
|
2010-01-27 06:44:30 +00:00
|
|
|
|
|
|
|
# We allow and check ALLEGED_READONLY_PREFIX or ALLEGED_IMMUTABLE_PREFIX
|
|
|
|
# on all URIs, even though we would only strictly need to do so for caps of
|
|
|
|
# new formats (post Tahoe-LAFS 1.6). URIs that are not consistent with their
|
|
|
|
# prefix are treated as unknown. This should be revisited when we add the
|
2012-03-08 23:17:19 +00:00
|
|
|
# new cap formats. See ticket #833 comment:31.
|
2010-01-27 06:44:30 +00:00
|
|
|
s = u
|
|
|
|
can_be_mutable = can_be_writeable = not deep_immutable
|
|
|
|
if s.startswith(ALLEGED_IMMUTABLE_PREFIX):
|
|
|
|
can_be_mutable = can_be_writeable = False
|
|
|
|
s = s[len(ALLEGED_IMMUTABLE_PREFIX):]
|
|
|
|
elif s.startswith(ALLEGED_READONLY_PREFIX):
|
|
|
|
can_be_writeable = False
|
|
|
|
s = s[len(ALLEGED_READONLY_PREFIX):]
|
|
|
|
|
|
|
|
error = None
|
|
|
|
try:
|
2020-08-24 17:41:58 +00:00
|
|
|
if s.startswith(b'URI:CHK:'):
|
2010-01-27 06:44:30 +00:00
|
|
|
return CHKFileURI.init_from_string(s)
|
2020-08-24 17:41:58 +00:00
|
|
|
elif s.startswith(b'URI:CHK-Verifier:'):
|
2010-01-27 06:44:30 +00:00
|
|
|
return CHKFileVerifierURI.init_from_string(s)
|
2020-08-24 17:41:58 +00:00
|
|
|
elif s.startswith(b'URI:LIT:'):
|
2010-01-27 06:44:30 +00:00
|
|
|
return LiteralFileURI.init_from_string(s)
|
2020-08-24 17:41:58 +00:00
|
|
|
elif s.startswith(b'URI:SSK:'):
|
2010-01-27 06:44:30 +00:00
|
|
|
if can_be_writeable:
|
|
|
|
return WriteableSSKFileURI.init_from_string(s)
|
|
|
|
kind = "URI:SSK file writecap"
|
2020-08-24 17:41:58 +00:00
|
|
|
elif s.startswith(b'URI:SSK-RO:'):
|
2010-01-27 06:44:30 +00:00
|
|
|
if can_be_mutable:
|
|
|
|
return ReadonlySSKFileURI.init_from_string(s)
|
|
|
|
kind = "URI:SSK-RO readcap to a mutable file"
|
2020-08-24 17:41:58 +00:00
|
|
|
elif s.startswith(b'URI:SSK-Verifier:'):
|
2010-01-27 06:44:30 +00:00
|
|
|
return SSKVerifierURI.init_from_string(s)
|
2020-08-24 17:41:58 +00:00
|
|
|
elif s.startswith(b'URI:MDMF:'):
|
2011-08-23 01:27:20 +00:00
|
|
|
if can_be_writeable:
|
|
|
|
return WriteableMDMFFileURI.init_from_string(s)
|
|
|
|
kind = "URI:MDMF file writecap"
|
2020-08-24 17:41:58 +00:00
|
|
|
elif s.startswith(b'URI:MDMF-RO:'):
|
2011-08-23 01:27:20 +00:00
|
|
|
if can_be_mutable:
|
|
|
|
return ReadonlyMDMFFileURI.init_from_string(s)
|
|
|
|
kind = "URI:MDMF-RO readcap to a mutable file"
|
2020-08-24 17:41:58 +00:00
|
|
|
elif s.startswith(b'URI:MDMF-Verifier:'):
|
2011-08-07 00:44:36 +00:00
|
|
|
return MDMFVerifierURI.init_from_string(s)
|
2020-08-24 17:41:58 +00:00
|
|
|
elif s.startswith(b'URI:DIR2:'):
|
2010-01-27 06:44:30 +00:00
|
|
|
if can_be_writeable:
|
|
|
|
return DirectoryURI.init_from_string(s)
|
|
|
|
kind = "URI:DIR2 directory writecap"
|
2020-08-24 17:41:58 +00:00
|
|
|
elif s.startswith(b'URI:DIR2-RO:'):
|
2010-01-27 06:44:30 +00:00
|
|
|
if can_be_mutable:
|
|
|
|
return ReadonlyDirectoryURI.init_from_string(s)
|
|
|
|
kind = "URI:DIR2-RO readcap to a mutable directory"
|
2020-08-24 17:41:58 +00:00
|
|
|
elif s.startswith(b'URI:DIR2-Verifier:'):
|
2010-01-27 06:44:30 +00:00
|
|
|
return DirectoryURIVerifier.init_from_string(s)
|
2020-08-24 17:41:58 +00:00
|
|
|
elif s.startswith(b'URI:DIR2-CHK:'):
|
2010-01-27 06:44:30 +00:00
|
|
|
return ImmutableDirectoryURI.init_from_string(s)
|
2020-08-24 17:41:58 +00:00
|
|
|
elif s.startswith(b'URI:DIR2-CHK-Verifier:'):
|
2011-08-27 19:50:48 +00:00
|
|
|
return ImmutableDirectoryURIVerifier.init_from_string(s)
|
2020-08-24 17:41:58 +00:00
|
|
|
elif s.startswith(b'URI:DIR2-LIT:'):
|
2010-01-27 06:44:30 +00:00
|
|
|
return LiteralDirectoryURI.init_from_string(s)
|
2020-08-24 17:41:58 +00:00
|
|
|
elif s.startswith(b'URI:DIR2-MDMF:'):
|
2011-08-07 00:44:36 +00:00
|
|
|
if can_be_writeable:
|
|
|
|
return MDMFDirectoryURI.init_from_string(s)
|
|
|
|
kind = "URI:DIR2-MDMF directory writecap"
|
2020-08-24 17:41:58 +00:00
|
|
|
elif s.startswith(b'URI:DIR2-MDMF-RO:'):
|
2011-08-07 00:44:36 +00:00
|
|
|
if can_be_mutable:
|
|
|
|
return ReadonlyMDMFDirectoryURI.init_from_string(s)
|
|
|
|
kind = "URI:DIR2-MDMF-RO readcap to a mutable directory"
|
2020-08-24 17:41:58 +00:00
|
|
|
elif s.startswith(b'URI:DIR2-MDMF-Verifier:'):
|
2011-08-27 19:50:48 +00:00
|
|
|
return MDMFDirectoryURIVerifier.init_from_string(s)
|
2020-08-24 17:41:58 +00:00
|
|
|
elif s.startswith(b'x-tahoe-future-test-writeable:') and not can_be_writeable:
|
2010-01-27 06:44:30 +00:00
|
|
|
# For testing how future writeable caps would behave in read-only contexts.
|
|
|
|
kind = "x-tahoe-future-test-writeable: testing cap"
|
2020-08-24 17:41:58 +00:00
|
|
|
elif s.startswith(b'x-tahoe-future-test-mutable:') and not can_be_mutable:
|
2010-01-27 06:44:30 +00:00
|
|
|
# For testing how future mutable readcaps would behave in immutable contexts.
|
|
|
|
kind = "x-tahoe-future-test-mutable: testing cap"
|
|
|
|
else:
|
|
|
|
return UnknownURI(u)
|
|
|
|
|
|
|
|
# We fell through because a constraint was not met.
|
|
|
|
# Prefer to report the most specific constraint.
|
|
|
|
if not can_be_mutable:
|
|
|
|
error = MustBeDeepImmutableError(kind + " used in an immutable context", name)
|
|
|
|
else:
|
|
|
|
error = MustBeReadonlyError(kind + " used in a read-only context", name)
|
2011-08-09 00:11:17 +00:00
|
|
|
|
2019-03-28 11:45:28 +00:00
|
|
|
except BadURIError as e:
|
2010-01-27 06:44:30 +00:00
|
|
|
error = e
|
|
|
|
|
|
|
|
return UnknownURI(u, error=error)
|
2007-06-02 01:48:01 +00:00
|
|
|
|
2008-01-11 03:43:42 +00:00
|
|
|
def is_uri(s):
|
|
|
|
try:
|
2010-01-27 06:44:30 +00:00
|
|
|
from_string(s, deep_immutable=False)
|
2008-01-11 03:43:42 +00:00
|
|
|
return True
|
|
|
|
except (TypeError, AssertionError):
|
|
|
|
return False
|
|
|
|
|
2010-01-27 06:44:30 +00:00
|
|
|
def is_literal_file_uri(s):
|
2020-08-24 17:41:58 +00:00
|
|
|
if isinstance(s, unicode):
|
|
|
|
s = s.encode("utf-8")
|
|
|
|
if not isinstance(s, bytes):
|
2010-01-27 06:44:30 +00:00
|
|
|
return False
|
2020-08-24 17:41:58 +00:00
|
|
|
return (s.startswith(b'URI:LIT:') or
|
|
|
|
s.startswith(ALLEGED_READONLY_PREFIX + b'URI:LIT:') or
|
|
|
|
s.startswith(ALLEGED_IMMUTABLE_PREFIX + b'URI:LIT:'))
|
2010-01-27 06:44:30 +00:00
|
|
|
|
|
|
|
def has_uri_prefix(s):
|
2020-08-24 17:41:58 +00:00
|
|
|
if isinstance(s, unicode):
|
|
|
|
s = s.encode("utf-8")
|
|
|
|
if not isinstance(s, bytes):
|
2010-01-27 06:44:30 +00:00
|
|
|
return False
|
2020-08-24 17:41:58 +00:00
|
|
|
return (s.startswith(b"URI:") or
|
|
|
|
s.startswith(ALLEGED_READONLY_PREFIX + b'URI:') or
|
|
|
|
s.startswith(ALLEGED_IMMUTABLE_PREFIX + b'URI:'))
|
2010-01-27 06:44:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
# These take the same keyword arguments as from_string above.
|
|
|
|
|
|
|
|
def from_string_dirnode(s, **kwargs):
|
|
|
|
u = from_string(s, **kwargs)
|
2013-04-15 18:30:49 +00:00
|
|
|
_assert(IDirnodeURI.providedBy(u))
|
2007-07-21 22:40:36 +00:00
|
|
|
return u
|
2007-01-17 04:29:59 +00:00
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
registerAdapter(from_string_dirnode, bytes, IDirnodeURI)
|
2007-01-17 04:29:59 +00:00
|
|
|
|
2010-01-27 06:44:30 +00:00
|
|
|
def from_string_filenode(s, **kwargs):
|
|
|
|
u = from_string(s, **kwargs)
|
2013-04-15 18:30:49 +00:00
|
|
|
_assert(IFileURI.providedBy(u))
|
2007-07-21 22:40:36 +00:00
|
|
|
return u
|
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
registerAdapter(from_string_filenode, bytes, IFileURI)
|
2007-01-17 04:29:59 +00:00
|
|
|
|
2010-01-27 06:44:30 +00:00
|
|
|
def from_string_mutable_filenode(s, **kwargs):
|
|
|
|
u = from_string(s, **kwargs)
|
2013-04-15 18:30:49 +00:00
|
|
|
_assert(IMutableFileURI.providedBy(u))
|
2007-11-01 22:15:29 +00:00
|
|
|
return u
|
2020-08-24 17:41:58 +00:00
|
|
|
registerAdapter(from_string_mutable_filenode, bytes, IMutableFileURI)
|
2007-11-01 22:15:29 +00:00
|
|
|
|
2010-01-27 06:44:30 +00:00
|
|
|
def from_string_verifier(s, **kwargs):
|
|
|
|
u = from_string(s, **kwargs)
|
2013-04-15 18:30:49 +00:00
|
|
|
_assert(IVerifierURI.providedBy(u))
|
2007-10-15 23:16:39 +00:00
|
|
|
return u
|
2020-08-24 17:41:58 +00:00
|
|
|
registerAdapter(from_string_verifier, bytes, IVerifierURI)
|
2007-10-15 23:16:39 +00:00
|
|
|
|
2007-01-17 04:29:59 +00:00
|
|
|
|
2007-06-12 01:25:18 +00:00
|
|
|
def pack_extension(data):
|
|
|
|
pieces = []
|
|
|
|
for k in sorted(data.keys()):
|
|
|
|
value = data[k]
|
|
|
|
if isinstance(value, (int, long)):
|
2020-08-24 17:41:58 +00:00
|
|
|
value = b"%d" % value
|
|
|
|
if isinstance(k, unicode):
|
|
|
|
k = k.encode("utf-8")
|
|
|
|
assert isinstance(value, bytes), k
|
|
|
|
assert re.match(br'^[a-zA-Z_\-]+$', k)
|
|
|
|
pieces.append(k + b':' + hashutil.netstring(value))
|
|
|
|
uri_extension = b''.join(pieces)
|
2007-06-12 01:25:18 +00:00
|
|
|
return uri_extension
|
|
|
|
|
|
|
|
def unpack_extension(data):
|
|
|
|
d = {}
|
|
|
|
while data:
|
2020-08-24 17:41:58 +00:00
|
|
|
colon = data.index(b':')
|
2007-06-12 01:25:18 +00:00
|
|
|
key = data[:colon]
|
|
|
|
data = data[colon+1:]
|
|
|
|
|
2020-08-24 17:41:58 +00:00
|
|
|
colon = data.index(b':')
|
2007-06-12 01:25:18 +00:00
|
|
|
number = data[:colon]
|
|
|
|
length = int(number)
|
|
|
|
data = data[colon+1:]
|
|
|
|
|
|
|
|
value = data[:length]
|
2020-08-24 17:53:27 +00:00
|
|
|
assert data[length:length+1] == b','
|
2007-06-12 01:25:18 +00:00
|
|
|
data = data[length+1:]
|
|
|
|
|
2020-09-16 15:20:08 +00:00
|
|
|
d[str(key, "utf-8")] = value
|
2007-06-12 01:25:18 +00:00
|
|
|
|
|
|
|
# convert certain things to numbers
|
2020-09-16 15:20:08 +00:00
|
|
|
for intkey in ('size', 'segment_size', 'num_segments',
|
|
|
|
'needed_shares', 'total_shares'):
|
2007-06-12 01:25:18 +00:00
|
|
|
if intkey in d:
|
|
|
|
d[intkey] = int(d[intkey])
|
|
|
|
return d
|
|
|
|
|
2007-06-12 01:38:21 +00:00
|
|
|
|
|
|
|
def unpack_extension_readable(data):
|
|
|
|
unpacked = unpack_extension(data)
|
2020-09-16 15:20:08 +00:00
|
|
|
unpacked["UEB_hash"] = hashutil.uri_extension_hash(data)
|
2007-06-12 01:38:21 +00:00
|
|
|
for k in sorted(unpacked.keys()):
|
2020-09-16 15:20:08 +00:00
|
|
|
if 'hash' in k:
|
2008-02-15 02:27:47 +00:00
|
|
|
unpacked[k] = base32.b2a(unpacked[k])
|
2007-06-12 01:38:21 +00:00
|
|
|
return unpacked
|
2007-06-25 20:23:51 +00:00
|
|
|
|