2 from zope.interface import implements
3 from twisted.internet import defer
4 from twisted.internet.interfaces import IPushProducer, IConsumer
5 from allmydata.interfaces import IFileNode, IFileURI, ICheckable
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, monitor, verify=False):
59 # TODO: pass the Monitor to SimpleCHKFileChecker or
60 # SimpleCHKFileVerifier, have it call monitor.raise_if_cancelled()
61 # before sending each request.
62 storage_index = self.u.storage_index
63 k = self.u.needed_shares
64 N = self.u.total_shares
66 ueb_hash = self.u.uri_extension_hash
68 v = self.verifier_class(self._client,
69 storage_index, k, N, size, ueb_hash)
71 v = self.checker_class(self._client, storage_index, k, N)
74 def check_and_repair(self, monitor, verify=False):
75 # this is a stub, to allow the deep-check tests to pass.
76 #raise NotImplementedError("not implemented yet")
77 from allmydata.checker_results import CheckAndRepairResults
78 cr = CheckAndRepairResults(self.u.storage_index)
79 d = self.check(verify)
81 cr.pre_repair_results = cr.post_repair_results = r
82 cr.repair_attempted = False
87 def download(self, target):
88 downloader = self._client.getServiceNamed("downloader")
89 return downloader.download(self.get_uri(), target)
91 def download_to_data(self):
92 downloader = self._client.getServiceNamed("downloader")
93 return downloader.download_to_data(self.get_uri())
95 class LiteralProducer:
96 implements(IPushProducer)
97 def resumeProducing(self):
99 def stopProducing(self):
102 class LiteralFileNode(ImmutableFileNode):
104 def __init__(self, uri, client):
105 ImmutableFileNode.__init__(self, uri, client)
108 return self.u.to_string()
111 return len(self.u.data)
113 def get_verifier(self):
116 def get_storage_index(self):
119 def check(self, monitor, verify=False):
120 return defer.succeed(None)
122 def check_and_repair(self, monitor, verify=False):
123 return defer.succeed(None)
125 def download(self, target):
126 # note that this does not update the stats_provider
128 if IConsumer.providedBy(target):
129 target.registerProducer(LiteralProducer(), True)
130 target.open(len(data))
132 if IConsumer.providedBy(target):
133 target.unregisterProducer()
135 return defer.maybeDeferred(target.finish)
137 def download_to_data(self):
139 return defer.succeed(data)