Show More
@@ -1,200 +1,200 | |||||
1 | # Copyright 2009-2010 Gregory P. Ward |
|
1 | # Copyright 2009-2010 Gregory P. Ward | |
2 | # Copyright 2009-2010 Intelerad Medical Systems Incorporated |
|
2 | # Copyright 2009-2010 Intelerad Medical Systems Incorporated | |
3 | # Copyright 2010-2011 Fog Creek Software |
|
3 | # Copyright 2010-2011 Fog Creek Software | |
4 | # Copyright 2010-2011 Unity Technologies |
|
4 | # Copyright 2010-2011 Unity Technologies | |
5 | # |
|
5 | # | |
6 | # This software may be used and distributed according to the terms of the |
|
6 | # This software may be used and distributed according to the terms of the | |
7 | # GNU General Public License version 2 or any later version. |
|
7 | # GNU General Public License version 2 or any later version. | |
8 |
|
8 | |||
9 | '''base class for store implementations and store-related utility code''' |
|
9 | '''base class for store implementations and store-related utility code''' | |
10 |
|
10 | |||
11 | import re |
|
11 | import re | |
12 |
|
12 | |||
13 | from mercurial import util, node, hg |
|
13 | from mercurial import util, node, hg | |
14 | from mercurial.i18n import _ |
|
14 | from mercurial.i18n import _ | |
15 |
|
15 | |||
16 | import lfutil |
|
16 | import lfutil | |
17 |
|
17 | |||
18 | class StoreError(Exception): |
|
18 | class StoreError(Exception): | |
19 | '''Raised when there is a problem getting files from or putting |
|
19 | '''Raised when there is a problem getting files from or putting | |
20 | files to a central store.''' |
|
20 | files to a central store.''' | |
21 | def __init__(self, filename, hash, url, detail): |
|
21 | def __init__(self, filename, hash, url, detail): | |
22 | self.filename = filename |
|
22 | self.filename = filename | |
23 | self.hash = hash |
|
23 | self.hash = hash | |
24 | self.url = url |
|
24 | self.url = url | |
25 | self.detail = detail |
|
25 | self.detail = detail | |
26 |
|
26 | |||
27 | def longmessage(self): |
|
27 | def longmessage(self): | |
28 | return (_("error getting id %s from url %s for file %s: %s\n") % |
|
28 | return (_("error getting id %s from url %s for file %s: %s\n") % | |
29 | (self.hash, self.url, self.filename, self.detail)) |
|
29 | (self.hash, self.url, self.filename, self.detail)) | |
30 |
|
30 | |||
31 | def __str__(self): |
|
31 | def __str__(self): | |
32 | return "%s: %s" % (self.url, self.detail) |
|
32 | return "%s: %s" % (self.url, self.detail) | |
33 |
|
33 | |||
34 | class basestore(object): |
|
34 | class basestore(object): | |
35 | def __init__(self, ui, repo, url): |
|
35 | def __init__(self, ui, repo, url): | |
36 | self.ui = ui |
|
36 | self.ui = ui | |
37 | self.repo = repo |
|
37 | self.repo = repo | |
38 | self.url = url |
|
38 | self.url = url | |
39 |
|
39 | |||
40 | def put(self, source, hash): |
|
40 | def put(self, source, hash): | |
41 |
'''Put source file into the store |
|
41 | '''Put source file into the store so it can be retrieved by hash.''' | |
42 | raise NotImplementedError('abstract method') |
|
42 | raise NotImplementedError('abstract method') | |
43 |
|
43 | |||
44 | def exists(self, hashes): |
|
44 | def exists(self, hashes): | |
45 | '''Check to see if the store contains the given hashes. Given an |
|
45 | '''Check to see if the store contains the given hashes. Given an | |
46 | iterable of hashes it returns a mapping from hash to bool.''' |
|
46 | iterable of hashes it returns a mapping from hash to bool.''' | |
47 | raise NotImplementedError('abstract method') |
|
47 | raise NotImplementedError('abstract method') | |
48 |
|
48 | |||
49 | def get(self, files): |
|
49 | def get(self, files): | |
50 | '''Get the specified largefiles from the store and write to local |
|
50 | '''Get the specified largefiles from the store and write to local | |
51 | files under repo.root. files is a list of (filename, hash) |
|
51 | files under repo.root. files is a list of (filename, hash) | |
52 | tuples. Return (success, missing), lists of files successfully |
|
52 | tuples. Return (success, missing), lists of files successfully | |
53 | downloaded and those not found in the store. success is a list |
|
53 | downloaded and those not found in the store. success is a list | |
54 | of (filename, hash) tuples; missing is a list of filenames that |
|
54 | of (filename, hash) tuples; missing is a list of filenames that | |
55 | we could not get. (The detailed error message will already have |
|
55 | we could not get. (The detailed error message will already have | |
56 | been presented to the user, so missing is just supplied as a |
|
56 | been presented to the user, so missing is just supplied as a | |
57 | summary.)''' |
|
57 | summary.)''' | |
58 | success = [] |
|
58 | success = [] | |
59 | missing = [] |
|
59 | missing = [] | |
60 | ui = self.ui |
|
60 | ui = self.ui | |
61 |
|
61 | |||
62 | util.makedirs(lfutil.storepath(self.repo, '')) |
|
62 | util.makedirs(lfutil.storepath(self.repo, '')) | |
63 |
|
63 | |||
64 | at = 0 |
|
64 | at = 0 | |
65 | for filename, hash in files: |
|
65 | for filename, hash in files: | |
66 | ui.progress(_('getting largefiles'), at, unit='lfile', |
|
66 | ui.progress(_('getting largefiles'), at, unit='lfile', | |
67 | total=len(files)) |
|
67 | total=len(files)) | |
68 | at += 1 |
|
68 | at += 1 | |
69 | ui.note(_('getting %s:%s\n') % (filename, hash)) |
|
69 | ui.note(_('getting %s:%s\n') % (filename, hash)) | |
70 |
|
70 | |||
71 | storefilename = lfutil.storepath(self.repo, hash) |
|
71 | storefilename = lfutil.storepath(self.repo, hash) | |
72 | tmpfile = util.atomictempfile(storefilename + '.tmp', |
|
72 | tmpfile = util.atomictempfile(storefilename + '.tmp', | |
73 | createmode=self.repo.store.createmode) |
|
73 | createmode=self.repo.store.createmode) | |
74 |
|
74 | |||
75 | try: |
|
75 | try: | |
76 | hhash = self._getfile(tmpfile, filename, hash) |
|
76 | hhash = self._getfile(tmpfile, filename, hash) | |
77 | except StoreError, err: |
|
77 | except StoreError, err: | |
78 | ui.warn(err.longmessage()) |
|
78 | ui.warn(err.longmessage()) | |
79 | hhash = "" |
|
79 | hhash = "" | |
80 | tmpfile.close() |
|
80 | tmpfile.close() | |
81 |
|
81 | |||
82 | if hhash != hash: |
|
82 | if hhash != hash: | |
83 | if hhash != "": |
|
83 | if hhash != "": | |
84 | ui.warn(_('%s: data corruption (expected %s, got %s)\n') |
|
84 | ui.warn(_('%s: data corruption (expected %s, got %s)\n') | |
85 | % (filename, hash, hhash)) |
|
85 | % (filename, hash, hhash)) | |
86 | util.unlink(storefilename + '.tmp') |
|
86 | util.unlink(storefilename + '.tmp') | |
87 | missing.append(filename) |
|
87 | missing.append(filename) | |
88 | continue |
|
88 | continue | |
89 |
|
89 | |||
90 | util.rename(storefilename + '.tmp', storefilename) |
|
90 | util.rename(storefilename + '.tmp', storefilename) | |
91 | lfutil.linktousercache(self.repo, hash) |
|
91 | lfutil.linktousercache(self.repo, hash) | |
92 | success.append((filename, hhash)) |
|
92 | success.append((filename, hhash)) | |
93 |
|
93 | |||
94 | ui.progress(_('getting largefiles'), None) |
|
94 | ui.progress(_('getting largefiles'), None) | |
95 | return (success, missing) |
|
95 | return (success, missing) | |
96 |
|
96 | |||
97 | def verify(self, revs, contents=False): |
|
97 | def verify(self, revs, contents=False): | |
98 | '''Verify the existence (and, optionally, contents) of every big |
|
98 | '''Verify the existence (and, optionally, contents) of every big | |
99 | file revision referenced by every changeset in revs. |
|
99 | file revision referenced by every changeset in revs. | |
100 | Return 0 if all is well, non-zero on any errors.''' |
|
100 | Return 0 if all is well, non-zero on any errors.''' | |
101 | failed = False |
|
101 | failed = False | |
102 |
|
102 | |||
103 | self.ui.status(_('searching %d changesets for largefiles\n') % |
|
103 | self.ui.status(_('searching %d changesets for largefiles\n') % | |
104 | len(revs)) |
|
104 | len(revs)) | |
105 | verified = set() # set of (filename, filenode) tuples |
|
105 | verified = set() # set of (filename, filenode) tuples | |
106 |
|
106 | |||
107 | for rev in revs: |
|
107 | for rev in revs: | |
108 | cctx = self.repo[rev] |
|
108 | cctx = self.repo[rev] | |
109 | cset = "%d:%s" % (cctx.rev(), node.short(cctx.node())) |
|
109 | cset = "%d:%s" % (cctx.rev(), node.short(cctx.node())) | |
110 |
|
110 | |||
111 | for standin in cctx: |
|
111 | for standin in cctx: | |
112 | if self._verifyfile(cctx, cset, contents, standin, verified): |
|
112 | if self._verifyfile(cctx, cset, contents, standin, verified): | |
113 | failed = True |
|
113 | failed = True | |
114 |
|
114 | |||
115 | numrevs = len(verified) |
|
115 | numrevs = len(verified) | |
116 | numlfiles = len(set([fname for (fname, fnode) in verified])) |
|
116 | numlfiles = len(set([fname for (fname, fnode) in verified])) | |
117 | if contents: |
|
117 | if contents: | |
118 | self.ui.status( |
|
118 | self.ui.status( | |
119 | _('verified contents of %d revisions of %d largefiles\n') |
|
119 | _('verified contents of %d revisions of %d largefiles\n') | |
120 | % (numrevs, numlfiles)) |
|
120 | % (numrevs, numlfiles)) | |
121 | else: |
|
121 | else: | |
122 | self.ui.status( |
|
122 | self.ui.status( | |
123 | _('verified existence of %d revisions of %d largefiles\n') |
|
123 | _('verified existence of %d revisions of %d largefiles\n') | |
124 | % (numrevs, numlfiles)) |
|
124 | % (numrevs, numlfiles)) | |
125 | return int(failed) |
|
125 | return int(failed) | |
126 |
|
126 | |||
127 | def _getfile(self, tmpfile, filename, hash): |
|
127 | def _getfile(self, tmpfile, filename, hash): | |
128 | '''Fetch one revision of one file from the store and write it |
|
128 | '''Fetch one revision of one file from the store and write it | |
129 | to tmpfile. Compute the hash of the file on-the-fly as it |
|
129 | to tmpfile. Compute the hash of the file on-the-fly as it | |
130 | downloads and return the hash. Close tmpfile. Raise |
|
130 | downloads and return the hash. Close tmpfile. Raise | |
131 | StoreError if unable to download the file (e.g. it does not |
|
131 | StoreError if unable to download the file (e.g. it does not | |
132 | exist in the store).''' |
|
132 | exist in the store).''' | |
133 | raise NotImplementedError('abstract method') |
|
133 | raise NotImplementedError('abstract method') | |
134 |
|
134 | |||
135 | def _verifyfile(self, cctx, cset, contents, standin, verified): |
|
135 | def _verifyfile(self, cctx, cset, contents, standin, verified): | |
136 | '''Perform the actual verification of a file in the store. |
|
136 | '''Perform the actual verification of a file in the store. | |
137 | 'cset' is only used in warnings. |
|
137 | 'cset' is only used in warnings. | |
138 | 'contents' controls verification of content hash. |
|
138 | 'contents' controls verification of content hash. | |
139 | 'standin' is the standin path of the largefile to verify. |
|
139 | 'standin' is the standin path of the largefile to verify. | |
140 | 'verified' is maintained as a set of already verified files. |
|
140 | 'verified' is maintained as a set of already verified files. | |
141 | Returns _true_ if it is a standin and any problems are found! |
|
141 | Returns _true_ if it is a standin and any problems are found! | |
142 | ''' |
|
142 | ''' | |
143 | raise NotImplementedError('abstract method') |
|
143 | raise NotImplementedError('abstract method') | |
144 |
|
144 | |||
145 | import localstore, wirestore |
|
145 | import localstore, wirestore | |
146 |
|
146 | |||
147 | _storeprovider = { |
|
147 | _storeprovider = { | |
148 | 'file': [localstore.localstore], |
|
148 | 'file': [localstore.localstore], | |
149 | 'http': [wirestore.wirestore], |
|
149 | 'http': [wirestore.wirestore], | |
150 | 'https': [wirestore.wirestore], |
|
150 | 'https': [wirestore.wirestore], | |
151 | 'ssh': [wirestore.wirestore], |
|
151 | 'ssh': [wirestore.wirestore], | |
152 | } |
|
152 | } | |
153 |
|
153 | |||
154 | _scheme_re = re.compile(r'^([a-zA-Z0-9+-.]+)://') |
|
154 | _scheme_re = re.compile(r'^([a-zA-Z0-9+-.]+)://') | |
155 |
|
155 | |||
156 | # During clone this function is passed the src's ui object |
|
156 | # During clone this function is passed the src's ui object | |
157 | # but it needs the dest's ui object so it can read out of |
|
157 | # but it needs the dest's ui object so it can read out of | |
158 | # the config file. Use repo.ui instead. |
|
158 | # the config file. Use repo.ui instead. | |
159 | def _openstore(repo, remote=None, put=False): |
|
159 | def _openstore(repo, remote=None, put=False): | |
160 | ui = repo.ui |
|
160 | ui = repo.ui | |
161 |
|
161 | |||
162 | if not remote: |
|
162 | if not remote: | |
163 | lfpullsource = getattr(repo, 'lfpullsource', None) |
|
163 | lfpullsource = getattr(repo, 'lfpullsource', None) | |
164 | if lfpullsource: |
|
164 | if lfpullsource: | |
165 | path = ui.expandpath(lfpullsource) |
|
165 | path = ui.expandpath(lfpullsource) | |
166 | else: |
|
166 | else: | |
167 | path = ui.expandpath('default-push', 'default') |
|
167 | path = ui.expandpath('default-push', 'default') | |
168 |
|
168 | |||
169 | # ui.expandpath() leaves 'default-push' and 'default' alone if |
|
169 | # ui.expandpath() leaves 'default-push' and 'default' alone if | |
170 | # they cannot be expanded: fallback to the empty string, |
|
170 | # they cannot be expanded: fallback to the empty string, | |
171 | # meaning the current directory. |
|
171 | # meaning the current directory. | |
172 | if path == 'default-push' or path == 'default': |
|
172 | if path == 'default-push' or path == 'default': | |
173 | path = '' |
|
173 | path = '' | |
174 | remote = repo |
|
174 | remote = repo | |
175 | else: |
|
175 | else: | |
176 | path, _branches = hg.parseurl(path) |
|
176 | path, _branches = hg.parseurl(path) | |
177 | remote = hg.peer(repo, {}, path) |
|
177 | remote = hg.peer(repo, {}, path) | |
178 |
|
178 | |||
179 | # The path could be a scheme so use Mercurial's normal functionality |
|
179 | # The path could be a scheme so use Mercurial's normal functionality | |
180 | # to resolve the scheme to a repository and use its path |
|
180 | # to resolve the scheme to a repository and use its path | |
181 | path = util.safehasattr(remote, 'url') and remote.url() or remote.path |
|
181 | path = util.safehasattr(remote, 'url') and remote.url() or remote.path | |
182 |
|
182 | |||
183 | match = _scheme_re.match(path) |
|
183 | match = _scheme_re.match(path) | |
184 | if not match: # regular filesystem path |
|
184 | if not match: # regular filesystem path | |
185 | scheme = 'file' |
|
185 | scheme = 'file' | |
186 | else: |
|
186 | else: | |
187 | scheme = match.group(1) |
|
187 | scheme = match.group(1) | |
188 |
|
188 | |||
189 | try: |
|
189 | try: | |
190 | storeproviders = _storeprovider[scheme] |
|
190 | storeproviders = _storeprovider[scheme] | |
191 | except KeyError: |
|
191 | except KeyError: | |
192 | raise util.Abort(_('unsupported URL scheme %r') % scheme) |
|
192 | raise util.Abort(_('unsupported URL scheme %r') % scheme) | |
193 |
|
193 | |||
194 | for classobj in storeproviders: |
|
194 | for classobj in storeproviders: | |
195 | try: |
|
195 | try: | |
196 | return classobj(ui, repo, remote) |
|
196 | return classobj(ui, repo, remote) | |
197 | except lfutil.storeprotonotcapable: |
|
197 | except lfutil.storeprotonotcapable: | |
198 | pass |
|
198 | pass | |
199 |
|
199 | |||
200 | raise util.Abort(_('%s does not appear to be a largefile store') % path) |
|
200 | raise util.Abort(_('%s does not appear to be a largefile store') % path) |
@@ -1,74 +1,73 | |||||
1 | # Copyright 2009-2010 Gregory P. Ward |
|
1 | # Copyright 2009-2010 Gregory P. Ward | |
2 | # Copyright 2009-2010 Intelerad Medical Systems Incorporated |
|
2 | # Copyright 2009-2010 Intelerad Medical Systems Incorporated | |
3 | # Copyright 2010-2011 Fog Creek Software |
|
3 | # Copyright 2010-2011 Fog Creek Software | |
4 | # Copyright 2010-2011 Unity Technologies |
|
4 | # Copyright 2010-2011 Unity Technologies | |
5 | # |
|
5 | # | |
6 | # This software may be used and distributed according to the terms of the |
|
6 | # This software may be used and distributed according to the terms of the | |
7 | # GNU General Public License version 2 or any later version. |
|
7 | # GNU General Public License version 2 or any later version. | |
8 |
|
8 | |||
9 | '''store class for local filesystem''' |
|
9 | '''store class for local filesystem''' | |
10 |
|
10 | |||
11 | from mercurial.i18n import _ |
|
11 | from mercurial.i18n import _ | |
12 |
|
12 | |||
13 | import lfutil |
|
13 | import lfutil | |
14 | import basestore |
|
14 | import basestore | |
15 |
|
15 | |||
16 | class localstore(basestore.basestore): |
|
16 | class localstore(basestore.basestore): | |
17 | '''localstore first attempts to grab files out of the store in the remote |
|
17 | '''localstore first attempts to grab files out of the store in the remote | |
18 | Mercurial repository. Failing that, it attempts to grab the files from |
|
18 | Mercurial repository. Failing that, it attempts to grab the files from | |
19 | the user cache.''' |
|
19 | the user cache.''' | |
20 |
|
20 | |||
21 | def __init__(self, ui, repo, remote): |
|
21 | def __init__(self, ui, repo, remote): | |
22 | self.remote = remote.local() |
|
22 | self.remote = remote.local() | |
23 | super(localstore, self).__init__(ui, repo, self.remote.url()) |
|
23 | super(localstore, self).__init__(ui, repo, self.remote.url()) | |
24 |
|
24 | |||
25 | def put(self, source, hash): |
|
25 | def put(self, source, hash): | |
26 | if lfutil.instore(self.remote, hash): |
|
26 | if lfutil.instore(self.remote, hash): | |
27 | return |
|
27 | return | |
28 |
lfutil.link(lfutil.storepath(self.re |
|
28 | lfutil.link(source, lfutil.storepath(self.remote, hash)) | |
29 | lfutil.storepath(self.remote, hash)) |
|
|||
30 |
|
29 | |||
31 | def exists(self, hashes): |
|
30 | def exists(self, hashes): | |
32 | retval = {} |
|
31 | retval = {} | |
33 | for hash in hashes: |
|
32 | for hash in hashes: | |
34 | retval[hash] = lfutil.instore(self.remote, hash) |
|
33 | retval[hash] = lfutil.instore(self.remote, hash) | |
35 | return retval |
|
34 | return retval | |
36 |
|
35 | |||
37 |
|
36 | |||
38 | def _getfile(self, tmpfile, filename, hash): |
|
37 | def _getfile(self, tmpfile, filename, hash): | |
39 | path = lfutil.findfile(self.remote, hash) |
|
38 | path = lfutil.findfile(self.remote, hash) | |
40 | if not path: |
|
39 | if not path: | |
41 | raise basestore.StoreError(filename, hash, self.url, |
|
40 | raise basestore.StoreError(filename, hash, self.url, | |
42 | _("can't get file locally")) |
|
41 | _("can't get file locally")) | |
43 | fd = open(path, 'rb') |
|
42 | fd = open(path, 'rb') | |
44 | try: |
|
43 | try: | |
45 | return lfutil.copyandhash(fd, tmpfile) |
|
44 | return lfutil.copyandhash(fd, tmpfile) | |
46 | finally: |
|
45 | finally: | |
47 | fd.close() |
|
46 | fd.close() | |
48 |
|
47 | |||
49 | def _verifyfile(self, cctx, cset, contents, standin, verified): |
|
48 | def _verifyfile(self, cctx, cset, contents, standin, verified): | |
50 | filename = lfutil.splitstandin(standin) |
|
49 | filename = lfutil.splitstandin(standin) | |
51 | if not filename: |
|
50 | if not filename: | |
52 | return False |
|
51 | return False | |
53 | fctx = cctx[standin] |
|
52 | fctx = cctx[standin] | |
54 | key = (filename, fctx.filenode()) |
|
53 | key = (filename, fctx.filenode()) | |
55 | if key in verified: |
|
54 | if key in verified: | |
56 | return False |
|
55 | return False | |
57 |
|
56 | |||
58 | expecthash = fctx.data()[0:40] |
|
57 | expecthash = fctx.data()[0:40] | |
59 | storepath = lfutil.storepath(self.remote, expecthash) |
|
58 | storepath = lfutil.storepath(self.remote, expecthash) | |
60 | verified.add(key) |
|
59 | verified.add(key) | |
61 | if not lfutil.instore(self.remote, expecthash): |
|
60 | if not lfutil.instore(self.remote, expecthash): | |
62 | self.ui.warn( |
|
61 | self.ui.warn( | |
63 | _('changeset %s: %s references missing %s\n') |
|
62 | _('changeset %s: %s references missing %s\n') | |
64 | % (cset, filename, storepath)) |
|
63 | % (cset, filename, storepath)) | |
65 | return True # failed |
|
64 | return True # failed | |
66 |
|
65 | |||
67 | if contents: |
|
66 | if contents: | |
68 | actualhash = lfutil.hashfile(storepath) |
|
67 | actualhash = lfutil.hashfile(storepath) | |
69 | if actualhash != expecthash: |
|
68 | if actualhash != expecthash: | |
70 | self.ui.warn( |
|
69 | self.ui.warn( | |
71 | _('changeset %s: %s references corrupted %s\n') |
|
70 | _('changeset %s: %s references corrupted %s\n') | |
72 | % (cset, filename, storepath)) |
|
71 | % (cset, filename, storepath)) | |
73 | return True # failed |
|
72 | return True # failed | |
74 | return False |
|
73 | return False |
General Comments 0
You need to be logged in to leave comments.
Login now