2 from zope.interface import implements
3 from twisted.internet import defer
4 from allmydata.interfaces import IFileNode, IFileURI, IURI, ICheckable
5 from allmydata import uri
6 from allmydata.immutable.checker import SimpleCHKFileChecker, \
9 class ImmutableFileNode(object):
10 implements(IFileNode, ICheckable)
11 checker_class = SimpleCHKFileChecker
12 verifier_class = SimpleCHKFileVerifier
14 def __init__(self, uri, client):
15 self.u = IFileURI(uri)
18 def get_readonly_uri(self):
24 def is_readonly(self):
28 return self.u.__hash__()
29 def __eq__(self, other):
30 if IFileNode.providedBy(other):
31 return self.u.__eq__(other.u)
34 def __ne__(self, other):
35 if IFileNode.providedBy(other):
36 return self.u.__eq__(other.u)
40 class FileNode(ImmutableFileNode):
41 checker_class = SimpleCHKFileChecker
43 def __init__(self, uri, client):
44 ImmutableFileNode.__init__(self, uri, client)
47 return self.u.to_string()
50 return self.u.get_size()
52 def get_verifier(self):
53 return self.u.get_verifier()
55 def get_storage_index(self):
56 return self.u.storage_index
58 def check(self, verify=False):
59 storage_index = self.u.storage_index
60 k = self.u.needed_shares
61 N = self.u.total_shares
63 ueb_hash = self.u.uri_extension_hash
65 v = self.verifier_class(self._client,
66 storage_index, k, N, size, ueb_hash)
68 v = self.checker_class(self._client, storage_index, k, N)
71 def check_and_repair(self, verify=False):
72 # this is a stub, to allow the deep-check tests to pass.
73 #raise NotImplementedError("not implemented yet")
74 from allmydata.checker_results import CheckAndRepairResults
75 cr = CheckAndRepairResults(self.u.storage_index)
76 d = self.check(verify)
78 cr.pre_repair_results = cr.post_repair_results = r
79 cr.repair_attempted = False
84 def download(self, target):
85 downloader = self._client.getServiceNamed("downloader")
86 return downloader.download(self.get_uri(), target)
88 def download_to_data(self):
89 downloader = self._client.getServiceNamed("downloader")
90 return downloader.download_to_data(self.get_uri())
94 class LiteralFileNode(ImmutableFileNode):
96 def __init__(self, uri, client):
97 ImmutableFileNode.__init__(self, uri, client)
100 return self.u.to_string()
103 return len(self.u.data)
105 def get_verifier(self):
108 def get_storage_index(self):
111 def check(self, verify=False):
112 # neither verify= nor repair= affect LIT files, and the check returns
114 return defer.succeed(None)
116 def check_and_repair(self, verify=False):
117 return defer.succeed(None)
119 def download(self, target):
120 # note that this does not update the stats_provider
122 target.open(len(data))
125 return defer.maybeDeferred(target.finish)
127 def download_to_data(self):
129 return defer.succeed(data)