Mercurial > hg > mercurial-crew
annotate hgext/largefiles/proto.py @ 16594:5516fdf3fe24 stable
largefiles: in putlfile, ensure tempfile's directory exists prior to creation
Let R be a repo served by an hg daemon on a machine with an empty largefiles
cache. Pushing a largefiles repo to R will result in a no-such-file-or-directory
OSError because putlfile will attempt to create a temporary file in
R/.hg/largefiles, which does not yet exist.
This patch also adds a regression test for this scenario.
author | hlian |
---|---|
date | Fri, 04 May 2012 14:36:40 -0400 |
parents | d87d9d8a8e03 |
children | 9e1616307c4c |
rev | line source |
---|---|
15168 | 1 # Copyright 2011 Fog Creek Software |
2 # | |
3 # This software may be used and distributed according to the terms of the | |
4 # GNU General Public License version 2 or any later version. | |
5 | |
6 import os | |
7 import urllib2 | |
8 | |
9 from mercurial import error, httprepo, util, wireproto | |
10 from mercurial.i18n import _ | |
11 | |
12 import lfutil | |
13 | |
15255
7ab05d752405
largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents:
15252
diff
changeset
|
14 LARGEFILES_REQUIRED_MSG = ('\nThis repository uses the largefiles extension.' |
7ab05d752405
largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents:
15252
diff
changeset
|
15 '\n\nPlease enable it in your Mercurial config ' |
7ab05d752405
largefiles: cosmetics, whitespace, code style
Greg Ward <greg@gerg.ca>
parents:
15252
diff
changeset
|
16 'file.\n') |
15168 | 17 |
18 def putlfile(repo, proto, sha): | |
15317
41f371150ccb
largefiles: make the store primary, and the user cache secondary
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15316
diff
changeset
|
19 '''Put a largefile into a repository's local store and into the |
41f371150ccb
largefiles: make the store primary, and the user cache secondary
Benjamin Pollack <benjamin@bitquabit.com>
parents:
15316
diff
changeset
|
20 user cache.''' |
15168 | 21 proto.redirect() |
15391
a5a6a9b7f3b9
largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents:
15317
diff
changeset
|
22 |
16594
5516fdf3fe24
largefiles: in putlfile, ensure tempfile's directory exists prior to creation
hlian
parents:
16247
diff
changeset
|
23 path = lfutil.storepath(repo, sha) |
5516fdf3fe24
largefiles: in putlfile, ensure tempfile's directory exists prior to creation
hlian
parents:
16247
diff
changeset
|
24 util.makedirs(os.path.dirname(path)) |
5516fdf3fe24
largefiles: in putlfile, ensure tempfile's directory exists prior to creation
hlian
parents:
16247
diff
changeset
|
25 tmpfp = util.atomictempfile(path, createmode=repo.store.createmode) |
5516fdf3fe24
largefiles: in putlfile, ensure tempfile's directory exists prior to creation
hlian
parents:
16247
diff
changeset
|
26 |
15168 | 27 try: |
28 try: | |
15391
a5a6a9b7f3b9
largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents:
15317
diff
changeset
|
29 proto.getfile(tmpfp) |
16155
1b2b42e866be
largefiles: respect store.createmode and avoid extra file copy
Martin Geisler <mg@aragost.com>
parents:
15778
diff
changeset
|
30 tmpfp._fp.seek(0) |
1b2b42e866be
largefiles: respect store.createmode and avoid extra file copy
Martin Geisler <mg@aragost.com>
parents:
15778
diff
changeset
|
31 if sha != lfutil.hexsha1(tmpfp._fp): |
15778
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
32 raise IOError(0, _('largefile contents do not match hash')) |
15391
a5a6a9b7f3b9
largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents:
15317
diff
changeset
|
33 tmpfp.close() |
16155
1b2b42e866be
largefiles: respect store.createmode and avoid extra file copy
Martin Geisler <mg@aragost.com>
parents:
15778
diff
changeset
|
34 lfutil.linktousercache(repo, sha) |
15391
a5a6a9b7f3b9
largefiles: replace tempfile.NamedTemporaryFile with tempfile.mkstemp
Hao Lian <hao@fogcreek.com>
parents:
15317
diff
changeset
|
35 except IOError, e: |
15778
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
36 repo.ui.warn(_('largefiles: failed to put %s into store: %s') % |
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
37 (sha, e.strerror)) |
15168 | 38 return wireproto.pushres(1) |
39 finally: | |
16155
1b2b42e866be
largefiles: respect store.createmode and avoid extra file copy
Martin Geisler <mg@aragost.com>
parents:
15778
diff
changeset
|
40 tmpfp.discard() |
15168 | 41 |
42 return wireproto.pushres(0) | |
43 | |
44 def getlfile(repo, proto, sha): | |
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
45 '''Retrieve a largefile from the repository-local cache or system |
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
46 cache.''' |
15168 | 47 filename = lfutil.findfile(repo, sha) |
48 if not filename: | |
49 raise util.Abort(_('requested largefile %s not present in cache') % sha) | |
50 f = open(filename, 'rb') | |
51 length = os.fstat(f.fileno())[6] | |
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
52 |
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
53 # Since we can't set an HTTP content-length header here, and |
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
54 # Mercurial core provides no way to give the length of a streamres |
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
55 # (and reading the entire file into RAM would be ill-advised), we |
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
56 # just send the length on the first line of the response, like the |
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
57 # ssh proto does for string responses. |
15168 | 58 def generator(): |
59 yield '%d\n' % length | |
60 for chunk in f: | |
61 yield chunk | |
62 return wireproto.streamres(generator()) | |
63 | |
64 def statlfile(repo, proto, sha): | |
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
65 '''Return '2\n' if the largefile is missing, '1\n' if it has a |
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
66 mismatched checksum, or '0\n' if it is in good condition''' |
15168 | 67 filename = lfutil.findfile(repo, sha) |
68 if not filename: | |
69 return '2\n' | |
70 fd = None | |
71 try: | |
72 fd = open(filename, 'rb') | |
73 return lfutil.hexsha1(fd) == sha and '0\n' or '1\n' | |
74 finally: | |
75 if fd: | |
76 fd.close() | |
77 | |
78 def wirereposetup(ui, repo): | |
79 class lfileswirerepository(repo.__class__): | |
80 def putlfile(self, sha, fd): | |
81 # unfortunately, httprepository._callpush tries to convert its | |
82 # input file-like into a bundle before sending it, so we can't use | |
83 # it ... | |
84 if issubclass(self.__class__, httprepo.httprepository): | |
15778
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
85 res = None |
15168 | 86 try: |
15778
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
87 res = self._call('putlfile', data=fd, sha=sha, |
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
88 headers={'content-type':'application/mercurial-0.1'}) |
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
89 d, output = res.split('\n', 1) |
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
90 for l in output.splitlines(True): |
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
91 self.ui.warn(_('remote: '), l, '\n') |
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
92 return int(d) |
15168 | 93 except (ValueError, urllib2.HTTPError): |
15778
f15c646bffc7
largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents:
15391
diff
changeset
|
94 self.ui.warn(_('unexpected putlfile response: %s') % res) |
15168 | 95 return 1 |
96 # ... but we can't use sshrepository._call because the data= | |
97 # argument won't get sent, and _callpush does exactly what we want | |
98 # in this case: send the data straight through | |
99 else: | |
100 try: | |
101 ret, output = self._callpush("putlfile", fd, sha=sha) | |
102 if ret == "": | |
103 raise error.ResponseError(_('putlfile failed:'), | |
104 output) | |
105 return int(ret) | |
106 except IOError: | |
107 return 1 | |
108 except ValueError: | |
109 raise error.ResponseError( | |
110 _('putlfile failed (unexpected response):'), ret) | |
111 | |
112 def getlfile(self, sha): | |
113 stream = self._callstream("getlfile", sha=sha) | |
114 length = stream.readline() | |
115 try: | |
116 length = int(length) | |
117 except ValueError: | |
15170
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
118 self._abort(error.ResponseError(_("unexpected response:"), |
c1a4a3220711
largefiles: fix over-long lines
Matt Mackall <mpm@selenic.com>
parents:
15168
diff
changeset
|
119 length)) |
15168 | 120 return (length, stream) |
121 | |
122 def statlfile(self, sha): | |
123 try: | |
124 return int(self._call("statlfile", sha=sha)) | |
125 except (ValueError, urllib2.HTTPError): | |
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
126 # If the server returns anything but an integer followed by a |
15168 | 127 # newline, newline, it's not speaking our language; if we get |
128 # an HTTP error, we can't be sure the largefile is present; | |
15252
6e809bb4f969
largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents:
15224
diff
changeset
|
129 # either way, consider it missing. |
15168 | 130 return 2 |
131 | |
132 repo.__class__ = lfileswirerepository | |
133 | |
134 # advertise the largefiles=serve capability | |
135 def capabilities(repo, proto): | |
16247
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16155
diff
changeset
|
136 return capabilitiesorig(repo, proto) + ' largefiles=serve' |
15168 | 137 |
138 # duplicate what Mercurial's new out-of-band errors mechanism does, because | |
139 # clients old and new alike both handle it well | |
16247
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16155
diff
changeset
|
140 def webprotorefuseclient(self, message): |
15168 | 141 self.req.header([('Content-Type', 'application/hg-error')]) |
142 return message | |
143 | |
16247
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16155
diff
changeset
|
144 def sshprotorefuseclient(self, message): |
15168 | 145 self.ui.write_err('%s\n-\n' % message) |
146 self.fout.write('\n') | |
147 self.fout.flush() | |
148 | |
149 return '' | |
150 | |
151 def heads(repo, proto): | |
152 if lfutil.islfilesrepo(repo): | |
15224
7c604d8c7e83
largefiles: remove pre-1.9 code from extension first bundled with 1.9
Na'Tosha Bard <natosha@unity3d.com>
parents:
15170
diff
changeset
|
153 return wireproto.ooberror(LARGEFILES_REQUIRED_MSG) |
15168 | 154 return wireproto.heads(repo, proto) |
155 | |
16247
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16155
diff
changeset
|
156 def sshrepocallstream(self, cmd, **args): |
15168 | 157 if cmd == 'heads' and self.capable('largefiles'): |
158 cmd = 'lheads' | |
159 if cmd == 'batch' and self.capable('largefiles'): | |
160 args['cmds'] = args['cmds'].replace('heads ', 'lheads ') | |
16247
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16155
diff
changeset
|
161 return ssholdcallstream(self, cmd, **args) |
15168 | 162 |
16247
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16155
diff
changeset
|
163 def httprepocallstream(self, cmd, **args): |
15168 | 164 if cmd == 'heads' and self.capable('largefiles'): |
165 cmd = 'lheads' | |
166 if cmd == 'batch' and self.capable('largefiles'): | |
167 args['cmds'] = args['cmds'].replace('heads ', 'lheads ') | |
16247
d87d9d8a8e03
largefiles: remove use of underscores that breaks coding convention
Na'Tosha Bard <natosha@unity3d.com>
parents:
16155
diff
changeset
|
168 return httpoldcallstream(self, cmd, **args) |