streamclone.py
93 lines
| 3.2 KiB
| text/x-python
|
PythonLexer
/ mercurial / streamclone.py
|
r2612 | # streamclone.py - streaming clone server support for mercurial | ||
# | ||||
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> | ||||
# | ||||
# This software may be used and distributed according to the terms | ||||
# of the GNU General Public License, incorporated herein by reference. | ||||
|
r5396 | import os, osutil, stat, util, lock | ||
|
r2612 | |||
# if server supports streaming clone, it advertises "stream" | ||||
# capability with value that is version+flags of repo it is serving. | ||||
# client only streams if it can read that repo format. | ||||
def walkrepo(root): | ||||
'''iterate over metadata files in repository. | ||||
walk in natural (sorted) order. | ||||
yields 2-tuples: name of .d or .i file, size of file.''' | ||||
strip_count = len(root) + len(os.sep) | ||||
def walk(path, recurse): | ||||
|
r5396 | for e, kind, st in osutil.listdir(path, stat=True): | ||
|
r2612 | pe = os.path.join(path, e) | ||
|
r5396 | if kind == stat.S_IFDIR: | ||
|
r2612 | if recurse: | ||
for x in walk(pe, True): | ||||
yield x | ||||
else: | ||||
|
r5396 | if kind != stat.S_IFREG or len(e) < 2: | ||
|
r2612 | continue | ||
sfx = e[-2:] | ||||
if sfx in ('.d', '.i'): | ||||
yield pe[strip_count:], st.st_size | ||||
# write file data first | ||||
for x in walk(os.path.join(root, 'data'), True): | ||||
yield x | ||||
# write manifest before changelog | ||||
|
r6762 | meta = util.sort(walk(root, False)) | ||
|
r2623 | meta.reverse() | ||
|
r2612 | for x in meta: | ||
yield x | ||||
# stream file format is simple. | ||||
# | ||||
# server writes out line that says how many files, how many total | ||||
# bytes. separator is ascii space, byte counts are strings. | ||||
# | ||||
# then for each file: | ||||
# | ||||
# server writes out line that says file name, how many bytes in | ||||
# file. separator is ascii nul, byte count is string. | ||||
# | ||||
# server writes out raw file data. | ||||
|
r6794 | def stream_out(repo, fileobj, untrusted=False): | ||
|
r2612 | '''stream out all metadata files in repository. | ||
writes to file-like object, must support write() and optional flush().''' | ||||
|
r2621 | |||
|
r4834 | if not repo.ui.configbool('server', 'uncompressed', untrusted=untrusted): | ||
|
r6794 | fileobj.write('1\n') | ||
|
r2621 | return | ||
|
r2612 | # get consistent snapshot of repo. lock during scan so lock not | ||
# needed while we stream, and commits can happen. | ||||
|
r5456 | repolock = None | ||
|
r3687 | try: | ||
|
r4915 | try: | ||
repolock = repo.lock() | ||||
except (lock.LockHeld, lock.LockUnavailable), inst: | ||||
repo.ui.warn('locking the repository failed: %s\n' % (inst,)) | ||||
|
r6794 | fileobj.write('2\n') | ||
|
r4915 | return | ||
|
r3687 | |||
|
r6794 | fileobj.write('0\n') | ||
|
r4915 | repo.ui.debug('scanning\n') | ||
entries = [] | ||||
total_bytes = 0 | ||||
for name, size in walkrepo(repo.spath): | ||||
name = repo.decodefn(util.pconvert(name)) | ||||
entries.append((name, size)) | ||||
total_bytes += size | ||||
finally: | ||||
del repolock | ||||
|
r2612 | |||
repo.ui.debug('%d files, %d bytes to transfer\n' % | ||||
(len(entries), total_bytes)) | ||||
|
r6794 | fileobj.write('%d %d\n' % (len(entries), total_bytes)) | ||
|
r2612 | for name, size in entries: | ||
repo.ui.debug('sending %s (%d bytes)\n' % (name, size)) | ||||
|
r6794 | fileobj.write('%s\0%d\n' % (name, size)) | ||
|
r3791 | for chunk in util.filechunkiter(repo.sopener(name), limit=size): | ||
|
r6794 | fileobj.write(chunk) | ||
flush = getattr(fileobj, 'flush', None) | ||||
if flush: flush() | ||||