| 1 |
# Copyright 2009-2010 Gregory P. Ward |
| 2 |
# Copyright 2009-2010 Intelerad Medical Systems Incorporated |
| 3 |
# Copyright 2010-2011 Fog Creek Software |
| 4 |
# Copyright 2010-2011 Unity Technologies |
| 5 |
# |
| 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. |
| 8 |
|
| 9 |
'''base class for store implementations and store-related utility code''' |
| 10 |
|
| 11 |
from __future__ import annotations |
| 12 |
|
| 13 |
from mercurial.i18n import _ |
| 14 |
|
| 15 |
from mercurial.node import short |
| 16 |
from mercurial import util |
| 17 |
from mercurial.utils import ( |
| 18 |
urlutil, |
| 19 |
) |
| 20 |
|
| 21 |
from . import lfutil |
| 22 |
|
| 23 |
|
| 24 |
class StoreError(Exception): |
| 25 |
"""Raised when there is a problem getting files from or putting |
| 26 |
files to a central store.""" |
| 27 |
|
| 28 |
def __init__(self, filename, hash, url, detail): |
| 29 |
self.filename = filename |
| 30 |
self.hash = hash |
| 31 |
self.url = url |
| 32 |
self.detail = detail |
| 33 |
|
| 34 |
def longmessage(self): |
| 35 |
return _(b"error getting id %s from url %s for file %s: %s\n") % ( |
| 36 |
self.hash, |
| 37 |
urlutil.hidepassword(self.url), |
| 38 |
self.filename, |
| 39 |
self.detail, |
| 40 |
) |
| 41 |
|
| 42 |
def __str__(self): |
| 43 |
return b"%s: %s" % (urlutil.hidepassword(self.url), self.detail) |
| 44 |
|
| 45 |
|
| 46 |
class basestore: |
| 47 |
def __init__(self, ui, repo, url): |
| 48 |
self.ui = ui |
| 49 |
self.repo = repo |
| 50 |
self.url = url |
| 51 |
|
| 52 |
def put(self, source, hash): |
| 53 |
'''Put source file into the store so it can be retrieved by hash.''' |
| 54 |
raise NotImplementedError(b'abstract method') |
| 55 |
|
| 56 |
def exists(self, hashes): |
| 57 |
"""Check to see if the store contains the given hashes. Given an |
| 58 |
iterable of hashes it returns a mapping from hash to bool.""" |
| 59 |
raise NotImplementedError(b'abstract method') |
| 60 |
|
| 61 |
def get(self, files): |
| 62 |
"""Get the specified largefiles from the store and write to local |
| 63 |
files under repo.root. files is a list of (filename, hash) |
| 64 |
tuples. Return (success, missing), lists of files successfully |
| 65 |
downloaded and those not found in the store. success is a list |
| 66 |
of (filename, hash) tuples; missing is a list of filenames that |
| 67 |
we could not get. (The detailed error message will already have |
| 68 |
been presented to the user, so missing is just supplied as a |
| 69 |
summary.)""" |
| 70 |
success = [] |
| 71 |
missing = [] |
| 72 |
ui = self.ui |
| 73 |
|
| 74 |
at = 0 |
| 75 |
available = self.exists({hash for (_filename, hash) in files}) |
| 76 |
with ui.makeprogress( |
| 77 |
_(b'getting largefiles'), unit=_(b'files'), total=len(files) |
| 78 |
) as progress: |
| 79 |
for filename, hash in files: |
| 80 |
progress.update(at) |
| 81 |
at += 1 |
| 82 |
ui.note(_(b'getting %s:%s\n') % (filename, hash)) |
| 83 |
|
| 84 |
if not available.get(hash): |
| 85 |
ui.warn( |
| 86 |
_(b'%s: largefile %s not available from %s\n') |
| 87 |
% (filename, hash, urlutil.hidepassword(self.url)) |
| 88 |
) |
| 89 |
missing.append(filename) |
| 90 |
continue |
| 91 |
|
| 92 |
if self._gethash(filename, hash): |
| 93 |
success.append((filename, hash)) |
| 94 |
else: |
| 95 |
missing.append(filename) |
| 96 |
|
| 97 |
return (success, missing) |
| 98 |
|
| 99 |
def _gethash(self, filename, hash): |
| 100 |
"""Get file with the provided hash and store it in the local repo's |
| 101 |
store and in the usercache. |
| 102 |
filename is for informational messages only. |
| 103 |
""" |
| 104 |
util.makedirs(lfutil.storepath(self.repo, b'')) |
| 105 |
storefilename = lfutil.storepath(self.repo, hash) |
| 106 |
|
| 107 |
tmpname = storefilename + b'.tmp' |
| 108 |
with util.atomictempfile( |
| 109 |
tmpname, createmode=self.repo.store.createmode |
| 110 |
) as tmpfile: |
| 111 |
try: |
| 112 |
gothash = self._getfile(tmpfile, filename, hash) |
| 113 |
except StoreError as err: |
| 114 |
self.ui.warn(err.longmessage()) |
| 115 |
gothash = b"" |
| 116 |
|
| 117 |
if gothash != hash: |
| 118 |
if gothash != b"": |
| 119 |
self.ui.warn( |
| 120 |
_(b'%s: data corruption (expected %s, got %s)\n') |
| 121 |
% (filename, hash, gothash) |
| 122 |
) |
| 123 |
util.unlink(tmpname) |
| 124 |
return False |
| 125 |
|
| 126 |
util.rename(tmpname, storefilename) |
| 127 |
lfutil.linktousercache(self.repo, hash) |
| 128 |
return True |
| 129 |
|
| 130 |
def verify(self, revs, contents=False): |
| 131 |
"""Verify the existence (and, optionally, contents) of every big |
| 132 |
file revision referenced by every changeset in revs. |
| 133 |
Return 0 if all is well, non-zero on any errors.""" |
| 134 |
|
| 135 |
self.ui.status( |
| 136 |
_(b'searching %d changesets for largefiles\n') % len(revs) |
| 137 |
) |
| 138 |
verified = set() # set of (filename, filenode) tuples |
| 139 |
filestocheck = [] # list of (cset, filename, expectedhash) |
| 140 |
for rev in revs: |
| 141 |
cctx = self.repo[rev] |
| 142 |
cset = b"%d:%s" % (cctx.rev(), short(cctx.node())) |
| 143 |
|
| 144 |
for standin in cctx: |
| 145 |
filename = lfutil.splitstandin(standin) |
| 146 |
if filename: |
| 147 |
fctx = cctx[standin] |
| 148 |
key = (filename, fctx.filenode()) |
| 149 |
if key not in verified: |
| 150 |
verified.add(key) |
| 151 |
expectedhash = lfutil.readasstandin(fctx) |
| 152 |
filestocheck.append((cset, filename, expectedhash)) |
| 153 |
|
| 154 |
failed = self._verifyfiles(contents, filestocheck) |
| 155 |
|
| 156 |
numrevs = len(verified) |
| 157 |
numlfiles = len({fname for (fname, fnode) in verified}) |
| 158 |
if contents: |
| 159 |
self.ui.status( |
| 160 |
_(b'verified contents of %d revisions of %d largefiles\n') |
| 161 |
% (numrevs, numlfiles) |
| 162 |
) |
| 163 |
else: |
| 164 |
self.ui.status( |
| 165 |
_(b'verified existence of %d revisions of %d largefiles\n') |
| 166 |
% (numrevs, numlfiles) |
| 167 |
) |
| 168 |
return int(failed) |
| 169 |
|
| 170 |
def _getfile(self, tmpfile, filename, hash): |
| 171 |
"""Fetch one revision of one file from the store and write it |
| 172 |
to tmpfile. Compute the hash of the file on-the-fly as it |
| 173 |
downloads and return the hash. Close tmpfile. Raise |
| 174 |
StoreError if unable to download the file (e.g. it does not |
| 175 |
exist in the store).""" |
| 176 |
raise NotImplementedError(b'abstract method') |
| 177 |
|
| 178 |
def _verifyfiles(self, contents, filestocheck): |
| 179 |
"""Perform the actual verification of files in the store. |
| 180 |
'contents' controls verification of content hash. |
| 181 |
'filestocheck' is list of files to check. |
| 182 |
Returns _true_ if any problems are found! |
| 183 |
""" |
| 184 |
raise NotImplementedError(b'abstract method') |