| 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 |
from __future__ import annotations |
| 7 |
|
| 8 |
import os |
| 9 |
|
| 10 |
from mercurial.i18n import _ |
| 11 |
|
| 12 |
from mercurial import ( |
| 13 |
error, |
| 14 |
exthelper, |
| 15 |
httppeer, |
| 16 |
util, |
| 17 |
wireprototypes, |
| 18 |
wireprotov1peer, |
| 19 |
wireprotov1server, |
| 20 |
) |
| 21 |
|
| 22 |
from . import lfutil |
| 23 |
|
| 24 |
urlerr = util.urlerr |
| 25 |
urlreq = util.urlreq |
| 26 |
|
| 27 |
LARGEFILES_REQUIRED_MSG = ( |
| 28 |
b'\nThis repository uses the largefiles extension.' |
| 29 |
b'\n\nPlease enable it in your Mercurial config ' |
| 30 |
b'file.\n' |
| 31 |
) |
| 32 |
|
| 33 |
eh = exthelper.exthelper() |
| 34 |
|
| 35 |
|
| 36 |
def putlfile(repo, proto, sha): |
| 37 |
"""Server command for putting a largefile into a repository's local store |
| 38 |
and into the user cache.""" |
| 39 |
with proto.mayberedirectstdio() as output: |
| 40 |
path = lfutil.storepath(repo, sha) |
| 41 |
util.makedirs(os.path.dirname(path)) |
| 42 |
tmpfp = util.atomictempfile(path, createmode=repo.store.createmode) |
| 43 |
|
| 44 |
try: |
| 45 |
for p in proto.getpayload(): |
| 46 |
tmpfp.write(p) |
| 47 |
tmpfp._fp.seek(0) |
| 48 |
if sha != lfutil.hexsha1(tmpfp._fp): |
| 49 |
raise OSError(0, _(b'largefile contents do not match hash')) |
| 50 |
tmpfp.close() |
| 51 |
lfutil.linktousercache(repo, sha) |
| 52 |
except OSError as e: |
| 53 |
repo.ui.warn( |
| 54 |
_(b'largefiles: failed to put %s into store: %s\n') |
| 55 |
% (sha, e.strerror) |
| 56 |
) |
| 57 |
return wireprototypes.pushres( |
| 58 |
1, output.getvalue() if output else b'' |
| 59 |
) |
| 60 |
finally: |
| 61 |
tmpfp.discard() |
| 62 |
|
| 63 |
return wireprototypes.pushres(0, output.getvalue() if output else b'') |
| 64 |
|
| 65 |
|
| 66 |
def getlfile(repo, proto, sha): |
| 67 |
"""Server command for retrieving a largefile from the repository-local |
| 68 |
cache or user cache.""" |
| 69 |
filename = lfutil.findfile(repo, sha) |
| 70 |
if not filename: |
| 71 |
raise error.Abort( |
| 72 |
_(b'requested largefile %s not present in cache') % sha |
| 73 |
) |
| 74 |
|
| 75 |
# TODO: fix the fd leak here |
| 76 |
f = open(filename, 'rb') |
| 77 |
length = os.fstat(f.fileno()).st_size |
| 78 |
|
| 79 |
# Since we can't set an HTTP content-length header here, and |
| 80 |
# Mercurial core provides no way to give the length of a streamres |
| 81 |
# (and reading the entire file into RAM would be ill-advised), we |
| 82 |
# just send the length on the first line of the response, like the |
| 83 |
# ssh proto does for string responses. |
| 84 |
def generator(): |
| 85 |
yield b'%d\n' % length |
| 86 |
yield from util.filechunkiter(f) |
| 87 |
|
| 88 |
return wireprototypes.streamreslegacy(gen=generator()) |
| 89 |
|
| 90 |
|
| 91 |
def statlfile(repo, proto, sha): |
| 92 |
"""Server command for checking if a largefile is present - returns '2\n' if |
| 93 |
the largefile is missing, '0\n' if it seems to be in good condition. |
| 94 |
|
| 95 |
The value 1 is reserved for mismatched checksum, but that is too expensive |
| 96 |
to be verified on every stat and must be caught be running 'hg verify' |
| 97 |
server side.""" |
| 98 |
filename = lfutil.findfile(repo, sha) |
| 99 |
if not filename: |
| 100 |
return wireprototypes.bytesresponse(b'2\n') |
| 101 |
return wireprototypes.bytesresponse(b'0\n') |
| 102 |
|
| 103 |
|
| 104 |
def wirereposetup(ui, repo): |
| 105 |
orig_commandexecutor = repo.commandexecutor |
| 106 |
|
| 107 |
class lfileswirerepository(repo.__class__): |
| 108 |
def commandexecutor(self): |
| 109 |
executor = orig_commandexecutor() |
| 110 |
if self.capable(b'largefiles'): |
| 111 |
orig_callcommand = executor.callcommand |
| 112 |
|
| 113 |
class lfscommandexecutor(executor.__class__): |
| 114 |
def callcommand(self, command, args): |
| 115 |
if command == b'heads': |
| 116 |
command = b'lheads' |
| 117 |
return orig_callcommand(command, args) |
| 118 |
|
| 119 |
executor.__class__ = lfscommandexecutor |
| 120 |
return executor |
| 121 |
|
| 122 |
@wireprotov1peer.batchable |
| 123 |
def lheads(self): |
| 124 |
return self.heads.batchable(self) |
| 125 |
|
| 126 |
def putlfile(self, sha, fd): |
| 127 |
# unfortunately, httprepository._callpush tries to convert its |
| 128 |
# input file-like into a bundle before sending it, so we can't use |
| 129 |
# it ... |
| 130 |
if issubclass(self.__class__, httppeer.httppeer): |
| 131 |
res = self._call( |
| 132 |
b'putlfile', |
| 133 |
data=fd, |
| 134 |
sha=sha, |
| 135 |
headers={'content-type': 'application/mercurial-0.1'}, |
| 136 |
) |
| 137 |
try: |
| 138 |
d, output = res.split(b'\n', 1) |
| 139 |
for l in output.splitlines(True): |
| 140 |
self.ui.warn(_(b'remote: '), l) # assume l ends with \n |
| 141 |
return int(d) |
| 142 |
except ValueError: |
| 143 |
self.ui.warn(_(b'unexpected putlfile response: %r\n') % res) |
| 144 |
return 1 |
| 145 |
# ... but we can't use sshrepository._call because the data= |
| 146 |
# argument won't get sent, and _callpush does exactly what we want |
| 147 |
# in this case: send the data straight through |
| 148 |
else: |
| 149 |
try: |
| 150 |
ret, output = self._callpush(b"putlfile", fd, sha=sha) |
| 151 |
if ret == b"": |
| 152 |
raise error.ResponseError( |
| 153 |
_(b'putlfile failed:'), output |
| 154 |
) |
| 155 |
return int(ret) |
| 156 |
except OSError: |
| 157 |
return 1 |
| 158 |
except ValueError: |
| 159 |
raise error.ResponseError( |
| 160 |
_(b'putlfile failed (unexpected response):'), ret |
| 161 |
) |
| 162 |
|
| 163 |
def getlfile(self, sha): |
| 164 |
"""returns an iterable with the chunks of the file with sha sha""" |
| 165 |
stream = self._callstream(b"getlfile", sha=sha) |
| 166 |
length = stream.readline() |
| 167 |
try: |
| 168 |
length = int(length) |
| 169 |
except ValueError: |
| 170 |
self._abort( |
| 171 |
error.ResponseError(_(b"unexpected response:"), length) |
| 172 |
) |
| 173 |
|
| 174 |
# SSH streams will block if reading more than length |
| 175 |
for chunk in util.filechunkiter(stream, limit=length): |
| 176 |
yield chunk |
| 177 |
# HTTP streams must hit the end to process the last empty |
| 178 |
# chunk of Chunked-Encoding so the connection can be reused. |
| 179 |
if issubclass(self.__class__, httppeer.httppeer): |
| 180 |
chunk = stream.read(1) |
| 181 |
if chunk: |
| 182 |
self._abort( |
| 183 |
error.ResponseError(_(b"unexpected response:"), chunk) |
| 184 |
) |
| 185 |
|
| 186 |
@wireprotov1peer.batchable |
| 187 |
def statlfile(self, sha): |
| 188 |
def decode(d): |
| 189 |
try: |
| 190 |
return int(d) |
| 191 |
except (ValueError, urlerr.httperror): |
| 192 |
# If the server returns anything but an integer followed by a |
| 193 |
# newline, newline, it's not speaking our language; if we get |
| 194 |
# an HTTP error, we can't be sure the largefile is present; |
| 195 |
# either way, consider it missing. |
| 196 |
return 2 |
| 197 |
|
| 198 |
result = {b'sha': sha} |
| 199 |
return result, decode |
| 200 |
|
| 201 |
repo.__class__ = lfileswirerepository |
| 202 |
|
| 203 |
|
| 204 |
# advertise the largefiles=serve capability |
| 205 |
@eh.wrapfunction(wireprotov1server, '_capabilities') |
| 206 |
def _capabilities(orig, repo, proto): |
| 207 |
'''announce largefile server capability''' |
| 208 |
caps = orig(repo, proto) |
| 209 |
caps.append(b'largefiles=serve') |
| 210 |
return caps |
| 211 |
|
| 212 |
|
| 213 |
def heads(orig, repo, proto): |
| 214 |
"""Wrap server command - largefile capable clients will know to call |
| 215 |
lheads instead""" |
| 216 |
if lfutil.islfilesrepo(repo): |
| 217 |
return wireprototypes.ooberror(LARGEFILES_REQUIRED_MSG) |
| 218 |
|
| 219 |
return orig(repo, proto) |