Show More
@@ -1,152 +1,125 b'' | |||
|
1 | 1 | # sshserver.py - ssh protocol server support for mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> |
|
5 | 5 | # |
|
6 | 6 | # This software may be used and distributed according to the terms of the |
|
7 | 7 | # GNU General Public License version 2 or any later version. |
|
8 | 8 | |
|
9 |
import util, hook, wireproto |
|
|
9 | import util, hook, wireproto | |
|
10 | 10 | import os, sys |
|
11 | 11 | |
|
12 | 12 | class sshserver(wireproto.abstractserverproto): |
|
13 | 13 | def __init__(self, ui, repo): |
|
14 | 14 | self.ui = ui |
|
15 | 15 | self.repo = repo |
|
16 | 16 | self.lock = None |
|
17 | 17 | self.fin = ui.fin |
|
18 | 18 | self.fout = ui.fout |
|
19 | 19 | |
|
20 | 20 | hook.redirect(True) |
|
21 | 21 | ui.fout = repo.ui.fout = ui.ferr |
|
22 | 22 | |
|
23 | 23 | # Prevent insertion/deletion of CRs |
|
24 | 24 | util.setbinary(self.fin) |
|
25 | 25 | util.setbinary(self.fout) |
|
26 | 26 | |
|
27 | 27 | def getargs(self, args): |
|
28 | 28 | data = {} |
|
29 | 29 | keys = args.split() |
|
30 | 30 | for n in xrange(len(keys)): |
|
31 | 31 | argline = self.fin.readline()[:-1] |
|
32 | 32 | arg, l = argline.split() |
|
33 | 33 | if arg not in keys: |
|
34 | 34 | raise util.Abort("unexpected parameter %r" % arg) |
|
35 | 35 | if arg == '*': |
|
36 | 36 | star = {} |
|
37 | 37 | for k in xrange(int(l)): |
|
38 | 38 | argline = self.fin.readline()[:-1] |
|
39 | 39 | arg, l = argline.split() |
|
40 | 40 | val = self.fin.read(int(l)) |
|
41 | 41 | star[arg] = val |
|
42 | 42 | data['*'] = star |
|
43 | 43 | else: |
|
44 | 44 | val = self.fin.read(int(l)) |
|
45 | 45 | data[arg] = val |
|
46 | 46 | return [data[k] for k in keys] |
|
47 | 47 | |
|
48 | 48 | def getarg(self, name): |
|
49 | 49 | return self.getargs(name)[0] |
|
50 | 50 | |
|
51 | 51 | def getfile(self, fpout): |
|
52 | 52 | self.sendresponse('') |
|
53 | 53 | count = int(self.fin.readline()) |
|
54 | 54 | while count: |
|
55 | 55 | fpout.write(self.fin.read(count)) |
|
56 | 56 | count = int(self.fin.readline()) |
|
57 | 57 | |
|
58 | 58 | def redirect(self): |
|
59 | 59 | pass |
|
60 | 60 | |
|
61 | 61 | def groupchunks(self, changegroup): |
|
62 | 62 | while True: |
|
63 | 63 | d = changegroup.read(4096) |
|
64 | 64 | if not d: |
|
65 | 65 | break |
|
66 | 66 | yield d |
|
67 | 67 | |
|
68 | 68 | def sendresponse(self, v): |
|
69 | 69 | self.fout.write("%d\n" % len(v)) |
|
70 | 70 | self.fout.write(v) |
|
71 | 71 | self.fout.flush() |
|
72 | 72 | |
|
73 | 73 | def sendstream(self, source): |
|
74 | 74 | write = self.fout.write |
|
75 | 75 | for chunk in source.gen: |
|
76 | 76 | write(chunk) |
|
77 | 77 | self.fout.flush() |
|
78 | 78 | |
|
79 | 79 | def sendpushresponse(self, rsp): |
|
80 | 80 | self.sendresponse('') |
|
81 | 81 | self.sendresponse(str(rsp.res)) |
|
82 | 82 | |
|
83 | 83 | def sendpusherror(self, rsp): |
|
84 | 84 | self.sendresponse(rsp.res) |
|
85 | 85 | |
|
86 | 86 | def sendooberror(self, rsp): |
|
87 | 87 | self.ui.ferr.write('%s\n-\n' % rsp.message) |
|
88 | 88 | self.ui.ferr.flush() |
|
89 | 89 | self.fout.write('\n') |
|
90 | 90 | self.fout.flush() |
|
91 | 91 | |
|
92 | 92 | def serve_forever(self): |
|
93 | 93 | try: |
|
94 | 94 | while self.serve_one(): |
|
95 | 95 | pass |
|
96 | 96 | finally: |
|
97 | 97 | if self.lock is not None: |
|
98 | 98 | self.lock.release() |
|
99 | 99 | sys.exit(0) |
|
100 | 100 | |
|
101 | 101 | handlers = { |
|
102 | 102 | str: sendresponse, |
|
103 | 103 | wireproto.streamres: sendstream, |
|
104 | 104 | wireproto.pushres: sendpushresponse, |
|
105 | 105 | wireproto.pusherr: sendpusherror, |
|
106 | 106 | wireproto.ooberror: sendooberror, |
|
107 | 107 | } |
|
108 | 108 | |
|
109 | 109 | def serve_one(self): |
|
110 | 110 | cmd = self.fin.readline()[:-1] |
|
111 | 111 | if cmd and cmd in wireproto.commands: |
|
112 | 112 | rsp = wireproto.dispatch(self.repo, self, cmd) |
|
113 | 113 | self.handlers[rsp.__class__](self, rsp) |
|
114 | 114 | elif cmd: |
|
115 | 115 | impl = getattr(self, 'do_' + cmd, None) |
|
116 | 116 | if impl: |
|
117 | 117 | r = impl() |
|
118 | 118 | if r is not None: |
|
119 | 119 | self.sendresponse(r) |
|
120 | 120 | else: self.sendresponse("") |
|
121 | 121 | return cmd != '' |
|
122 | 122 | |
|
123 | def do_lock(self): | |
|
124 | '''DEPRECATED - allowing remote client to lock repo is not safe''' | |
|
125 | ||
|
126 | self.lock = self.repo.lock() | |
|
127 | return "" | |
|
128 | ||
|
129 | def do_unlock(self): | |
|
130 | '''DEPRECATED''' | |
|
131 | ||
|
132 | if self.lock: | |
|
133 | self.lock.release() | |
|
134 | self.lock = None | |
|
135 | return "" | |
|
136 | ||
|
137 | def do_addchangegroup(self): | |
|
138 | '''DEPRECATED''' | |
|
139 | ||
|
140 | if not self.lock: | |
|
141 | self.sendresponse("not locked") | |
|
142 | return | |
|
143 | ||
|
144 | self.sendresponse("") | |
|
145 | cg = changegroup.cg1unpacker(self.fin, "UN") | |
|
146 | r = changegroup.addchangegroup(self.repo, cg, 'serve', self._client()) | |
|
147 | self.lock.release() | |
|
148 | return str(r) | |
|
149 | ||
|
150 | 123 | def _client(self): |
|
151 | 124 | client = os.environ.get('SSH_CLIENT', '').split(' ', 1)[0] |
|
152 | 125 | return 'remote:ssh:' + client |
|
1 | NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now