2007-12-03 21:52:42 +00:00
|
|
|
|
|
|
|
from zope.interface import implements
|
2007-12-04 04:37:54 +00:00
|
|
|
from twisted.internet import defer
|
2008-07-16 00:23:25 +00:00
|
|
|
from allmydata.interfaces import IFileNode, IFileURI, IURI, ICheckable
|
2007-12-04 04:37:54 +00:00
|
|
|
from allmydata import uri
|
2008-07-17 01:20:57 +00:00
|
|
|
from allmydata.immutable.checker import Results, DeepCheckResults, \
|
2008-07-16 20:14:39 +00:00
|
|
|
SimpleCHKFileChecker, SimpleCHKFileVerifier
|
2007-12-03 21:52:42 +00:00
|
|
|
|
|
|
|
class FileNode:
|
2008-07-16 00:23:25 +00:00
|
|
|
implements(IFileNode, ICheckable)
|
2008-07-17 23:47:09 +00:00
|
|
|
checker_class = SimpleCHKFileChecker
|
|
|
|
verifier_class = SimpleCHKFileVerifier
|
2007-12-03 21:52:42 +00:00
|
|
|
|
|
|
|
def __init__(self, uri, client):
|
|
|
|
u = IFileURI(uri)
|
2008-07-17 23:47:09 +00:00
|
|
|
self.u = u
|
2007-12-03 21:52:42 +00:00
|
|
|
self.uri = u.to_string()
|
|
|
|
self._client = client
|
|
|
|
|
|
|
|
def get_uri(self):
|
|
|
|
return self.uri
|
|
|
|
|
2008-05-19 20:03:00 +00:00
|
|
|
def is_mutable(self):
|
|
|
|
return False
|
|
|
|
|
2007-12-03 21:52:42 +00:00
|
|
|
def is_readonly(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_readonly_uri(self):
|
|
|
|
return self.uri
|
|
|
|
|
|
|
|
def get_size(self):
|
2008-07-17 23:47:09 +00:00
|
|
|
return self.u.get_size()
|
2007-12-03 21:52:42 +00:00
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash((self.__class__, self.uri))
|
|
|
|
def __cmp__(self, them):
|
|
|
|
if cmp(type(self), type(them)):
|
|
|
|
return cmp(type(self), type(them))
|
|
|
|
if cmp(self.__class__, them.__class__):
|
|
|
|
return cmp(self.__class__, them.__class__)
|
|
|
|
return cmp(self.uri, them.uri)
|
|
|
|
|
|
|
|
def get_verifier(self):
|
2008-07-17 23:47:09 +00:00
|
|
|
return self.u.get_verifier()
|
2007-12-03 21:52:42 +00:00
|
|
|
|
2008-07-16 00:23:25 +00:00
|
|
|
def check(self, verify=False, repair=False):
|
|
|
|
assert repair is False # not implemented yet
|
2008-07-17 23:47:09 +00:00
|
|
|
storage_index = self.u.storage_index
|
|
|
|
k = self.u.needed_shares
|
|
|
|
N = self.u.total_shares
|
|
|
|
size = self.u.size
|
|
|
|
ueb_hash = self.u.uri_extension_hash
|
2008-07-16 00:23:25 +00:00
|
|
|
if verify:
|
2008-07-17 23:47:09 +00:00
|
|
|
v = self.verifier_class(self._client,
|
|
|
|
storage_index, k, N, size, ueb_hash)
|
2008-07-16 00:23:25 +00:00
|
|
|
else:
|
2008-07-17 23:47:09 +00:00
|
|
|
v = self.checker_class(self._client, storage_index, k, N)
|
|
|
|
return v.start()
|
2007-12-03 21:52:42 +00:00
|
|
|
|
2008-07-17 01:20:57 +00:00
|
|
|
def deep_check(self, verify=False, repair=False):
|
|
|
|
d = self.check(verify, repair)
|
|
|
|
def _done(r):
|
2008-07-17 23:47:09 +00:00
|
|
|
dr = DeepCheckResults(self.get_verifier().storage_index)
|
2008-07-17 01:20:57 +00:00
|
|
|
dr.add_check(r)
|
|
|
|
return dr
|
|
|
|
d.addCallback(_done)
|
|
|
|
return d
|
|
|
|
|
2007-12-03 21:52:42 +00:00
|
|
|
def download(self, target):
|
|
|
|
downloader = self._client.getServiceNamed("downloader")
|
|
|
|
return downloader.download(self.uri, target)
|
|
|
|
|
|
|
|
def download_to_data(self):
|
|
|
|
downloader = self._client.getServiceNamed("downloader")
|
|
|
|
return downloader.download_to_data(self.uri)
|
|
|
|
|
2007-12-04 04:37:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LiteralFileNode:
|
2008-07-16 00:23:25 +00:00
|
|
|
implements(IFileNode, ICheckable)
|
2007-12-04 04:37:54 +00:00
|
|
|
|
|
|
|
def __init__(self, my_uri, client):
|
|
|
|
u = IFileURI(my_uri)
|
|
|
|
assert isinstance(u, uri.LiteralFileURI)
|
|
|
|
self.uri = u.to_string()
|
|
|
|
self._client = client
|
|
|
|
|
|
|
|
def get_uri(self):
|
|
|
|
return self.uri
|
|
|
|
|
2008-05-19 20:03:00 +00:00
|
|
|
def is_mutable(self):
|
|
|
|
return False
|
|
|
|
|
2007-12-04 04:37:54 +00:00
|
|
|
def is_readonly(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_readonly_uri(self):
|
|
|
|
return self.uri
|
|
|
|
|
|
|
|
def get_size(self):
|
|
|
|
return len(IURI(self.uri).data)
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash((self.__class__, self.uri))
|
|
|
|
def __cmp__(self, them):
|
|
|
|
if cmp(type(self), type(them)):
|
|
|
|
return cmp(type(self), type(them))
|
|
|
|
if cmp(self.__class__, them.__class__):
|
|
|
|
return cmp(self.__class__, them.__class__)
|
|
|
|
return cmp(self.uri, them.uri)
|
|
|
|
|
|
|
|
def get_verifier(self):
|
|
|
|
return None
|
|
|
|
|
2008-07-16 00:23:25 +00:00
|
|
|
def check(self, verify=False, repair=False):
|
|
|
|
# neither verify= nor repair= affect LIT files
|
|
|
|
r = Results(None)
|
|
|
|
r.healthy = True
|
|
|
|
r.problems = []
|
|
|
|
return defer.succeed(r)
|
2007-12-04 04:37:54 +00:00
|
|
|
|
2008-07-17 23:47:09 +00:00
|
|
|
def deep_check(self, verify=False, repair=False):
|
|
|
|
d = self.check(verify, repair)
|
|
|
|
def _done(r):
|
|
|
|
dr = DeepCheckResults(None)
|
|
|
|
dr.add_check(r)
|
|
|
|
return dr
|
|
|
|
d.addCallback(_done)
|
|
|
|
return d
|
|
|
|
|
2007-12-04 04:37:54 +00:00
|
|
|
def download(self, target):
|
2008-07-16 00:23:25 +00:00
|
|
|
# note that this does not update the stats_provider
|
2007-12-04 04:37:54 +00:00
|
|
|
data = IURI(self.uri).data
|
|
|
|
target.open(len(data))
|
|
|
|
target.write(data)
|
|
|
|
target.close()
|
|
|
|
return defer.maybeDeferred(target.finish)
|
|
|
|
|
|
|
|
def download_to_data(self):
|
|
|
|
data = IURI(self.uri).data
|
|
|
|
return defer.succeed(data)
|