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-16 20:14:39 +00:00
|
|
|
from allmydata.immutable.checker import Results, \
|
|
|
|
SimpleCHKFileChecker, SimpleCHKFileVerifier
|
2007-12-03 21:52:42 +00:00
|
|
|
|
|
|
|
class FileNode:
|
2008-07-16 00:23:25 +00:00
|
|
|
implements(IFileNode, ICheckable)
|
2007-12-03 21:52:42 +00:00
|
|
|
|
|
|
|
def __init__(self, uri, client):
|
|
|
|
u = IFileURI(uri)
|
|
|
|
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):
|
|
|
|
return IFileURI(self.uri).get_size()
|
|
|
|
|
|
|
|
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 IFileURI(self.uri).get_verifier()
|
|
|
|
|
2008-07-16 00:23:25 +00:00
|
|
|
def check(self, verify=False, repair=False):
|
|
|
|
assert repair is False # not implemented yet
|
|
|
|
vcap = self.get_verifier()
|
|
|
|
if verify:
|
|
|
|
v = SimpleCHKFileVerifier(self._client, vcap)
|
|
|
|
return v.start()
|
|
|
|
else:
|
|
|
|
peer_getter = self._client.get_permuted_peers
|
|
|
|
v = SimpleCHKFileChecker(peer_getter, vcap)
|
|
|
|
return v.check()
|
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
|
|
|
|
|
|
|
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)
|