##// END OF EJS Templates
streamclone: stream_out tried to decodefilename non-normalized paths
Patrick Mezard -
r4127:a3a6f71e default
parent child Browse files
Show More
@@ -1,96 +1,96
1 1 # streamclone.py - streaming clone server support for mercurial
2 2 #
3 3 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
4 4 #
5 5 # This software may be used and distributed according to the terms
6 6 # of the GNU General Public License, incorporated herein by reference.
7 7
8 8 from demandload import demandload
9 9 from i18n import gettext as _
10 10 demandload(globals(), "os stat util lock")
11 11
12 12 # if server supports streaming clone, it advertises "stream"
13 13 # capability with value that is version+flags of repo it is serving.
14 14 # client only streams if it can read that repo format.
15 15
16 16 def walkrepo(root):
17 17 '''iterate over metadata files in repository.
18 18 walk in natural (sorted) order.
19 19 yields 2-tuples: name of .d or .i file, size of file.'''
20 20
21 21 strip_count = len(root) + len(os.sep)
22 22 def walk(path, recurse):
23 23 ents = os.listdir(path)
24 24 ents.sort()
25 25 for e in ents:
26 26 pe = os.path.join(path, e)
27 27 st = os.lstat(pe)
28 28 if stat.S_ISDIR(st.st_mode):
29 29 if recurse:
30 30 for x in walk(pe, True):
31 31 yield x
32 32 else:
33 33 if not stat.S_ISREG(st.st_mode) or len(e) < 2:
34 34 continue
35 35 sfx = e[-2:]
36 36 if sfx in ('.d', '.i'):
37 37 yield pe[strip_count:], st.st_size
38 38 # write file data first
39 39 for x in walk(os.path.join(root, 'data'), True):
40 40 yield x
41 41 # write manifest before changelog
42 42 meta = list(walk(root, False))
43 43 meta.sort()
44 44 meta.reverse()
45 45 for x in meta:
46 46 yield x
47 47
48 48 # stream file format is simple.
49 49 #
50 50 # server writes out line that says how many files, how many total
51 51 # bytes. separator is ascii space, byte counts are strings.
52 52 #
53 53 # then for each file:
54 54 #
55 55 # server writes out line that says file name, how many bytes in
56 56 # file. separator is ascii nul, byte count is string.
57 57 #
58 58 # server writes out raw file data.
59 59
60 60 def stream_out(repo, fileobj):
61 61 '''stream out all metadata files in repository.
62 62 writes to file-like object, must support write() and optional flush().'''
63 63
64 64 if not repo.ui.configbool('server', 'uncompressed'):
65 65 fileobj.write('1\n')
66 66 return
67 67
68 68 # get consistent snapshot of repo. lock during scan so lock not
69 69 # needed while we stream, and commits can happen.
70 70 try:
71 71 repolock = repo.lock()
72 72 except (lock.LockHeld, lock.LockUnavailable), inst:
73 73 repo.ui.warn('locking the repository failed: %s\n' % (inst,))
74 74 fileobj.write('2\n')
75 75 return
76 76
77 77 fileobj.write('0\n')
78 78 repo.ui.debug('scanning\n')
79 79 entries = []
80 80 total_bytes = 0
81 81 for name, size in walkrepo(repo.spath):
82 name = util.pconvert(repo.decodefn(name))
82 name = repo.decodefn(util.pconvert(name))
83 83 entries.append((name, size))
84 84 total_bytes += size
85 85 repolock.release()
86 86
87 87 repo.ui.debug('%d files, %d bytes to transfer\n' %
88 88 (len(entries), total_bytes))
89 89 fileobj.write('%d %d\n' % (len(entries), total_bytes))
90 90 for name, size in entries:
91 91 repo.ui.debug('sending %s (%d bytes)\n' % (name, size))
92 92 fileobj.write('%s\0%d\n' % (name, size))
93 93 for chunk in util.filechunkiter(repo.sopener(name), limit=size):
94 94 fileobj.write(chunk)
95 95 flush = getattr(fileobj, 'flush', None)
96 96 if flush: flush()
General Comments 0
You need to be logged in to leave comments. Login now