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