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