]> arthur.barton.de Git - bup.git/blob - cmd/server-cmd.py
cmd/server: find .idx filenames more efficiently when needed.
[bup.git] / cmd / server-cmd.py
1 #!/usr/bin/env python
2 import sys, struct
3 from bup import options, git
4 from bup.helpers import *
5
6 suspended_w = None
7
8
9 def init_dir(conn, arg):
10     git.init_repo(arg)
11     debug1('bup server: bupdir initialized: %r\n' % git.repodir)
12     conn.ok()
13
14
15 def set_dir(conn, arg):
16     git.check_repo_or_die(arg)
17     debug1('bup server: bupdir is %r\n' % git.repodir)
18     conn.ok()
19
20     
21 def list_indexes(conn, junk):
22     git.check_repo_or_die()
23     for f in os.listdir(git.repo('objects/pack')):
24         if f.endswith('.idx'):
25             conn.write('%s\n' % f)
26     conn.ok()
27
28
29 def send_index(conn, name):
30     git.check_repo_or_die()
31     assert(name.find('/') < 0)
32     assert(name.endswith('.idx'))
33     idx = git.open_idx(git.repo('objects/pack/%s' % name))
34     conn.write(struct.pack('!I', len(idx.map)))
35     conn.write(idx.map)
36     conn.ok()
37
38
39 def receive_objects(conn, junk):
40     global suspended_w
41     git.check_repo_or_die()
42     suggested = {}
43     if suspended_w:
44         w = suspended_w
45         suspended_w = None
46     else:
47         w = git.PackWriter()
48     while 1:
49         ns = conn.read(4)
50         if not ns:
51             w.abort()
52             raise Exception('object read: expected length header, got EOF\n')
53         n = struct.unpack('!I', ns)[0]
54         #debug2('expecting %d bytes\n' % n)
55         if not n:
56             debug1('bup server: received %d object%s.\n' 
57                 % (w.count, w.count!=1 and "s" or ''))
58             fullpath = w.close()
59             if fullpath:
60                 (dir, name) = os.path.split(fullpath)
61                 conn.write('%s.idx\n' % name)
62             conn.ok()
63             return
64         elif n == 0xffffffff:
65             debug2('bup server: receive-objects suspended.\n')
66             suspended_w = w
67             conn.ok()
68             return
69             
70         buf = conn.read(n)  # object sizes in bup are reasonably small
71         #debug2('read %d bytes\n' % n)
72         if len(buf) < n:
73             w.abort()
74             raise Exception('object read: expected %d bytes, got %d\n'
75                             % (n, len(buf)))
76         (type, content) = git._decode_packobj(buf)
77         sha = git.calc_hash(type, content)
78         oldpack = w.exists(sha)
79         # FIXME: we only suggest a single index per cycle, because the client
80         # is currently too dumb to download more than one per cycle anyway.
81         # Actually we should fix the client, but this is a minor optimization
82         # on the server side.
83         if not suggested and \
84           oldpack and (oldpack == True or oldpack.endswith('.midx')):
85             # FIXME: we shouldn't really have to know about midx files
86             # at this layer.  But exists() on a midx doesn't return the
87             # packname (since it doesn't know)... probably we should just
88             # fix that deficiency of midx files eventually, although it'll
89             # make the files bigger.  This method is certainly not very
90             # efficient.
91             oldpack = w.objcache.packname_containing(sha)
92             debug2('new suggestion: %r\n' % oldpack)
93             assert(oldpack)
94             assert(oldpack != True)
95             assert(not oldpack.endswith('.midx'))
96             w.objcache.refresh()
97         if not suggested and oldpack:
98             assert(oldpack.endswith('.idx'))
99             (dir,name) = os.path.split(oldpack)
100             if not (name in suggested):
101                 debug1("bup server: suggesting index %s\n" % name)
102                 conn.write('index %s\n' % name)
103                 suggested[name] = 1
104         else:
105             w._raw_write([buf])
106     # NOTREACHED
107
108
109 def read_ref(conn, refname):
110     git.check_repo_or_die()
111     r = git.read_ref(refname)
112     conn.write('%s\n' % (r or '').encode('hex'))
113     conn.ok()
114
115
116 def update_ref(conn, refname):
117     git.check_repo_or_die()
118     newval = conn.readline().strip()
119     oldval = conn.readline().strip()
120     git.update_ref(refname, newval.decode('hex'), oldval.decode('hex'))
121     conn.ok()
122
123
124 cat_pipe = None
125 def cat(conn, id):
126     global cat_pipe
127     git.check_repo_or_die()
128     if not cat_pipe:
129         cat_pipe = git.CatPipe()
130     try:
131         for blob in cat_pipe.join(id):
132             conn.write(struct.pack('!I', len(blob)))
133             conn.write(blob)
134     except KeyError, e:
135         log('server: error: %s\n' % e)
136         conn.write('\0\0\0\0')
137         conn.error(e)
138     else:
139         conn.write('\0\0\0\0')
140         conn.ok()
141
142
143 optspec = """
144 bup server
145 """
146 o = options.Options('bup server', optspec)
147 (opt, flags, extra) = o.parse(sys.argv[1:])
148
149 if extra:
150     o.fatal('no arguments expected')
151
152 debug2('bup server: reading from stdin.\n')
153
154 commands = {
155     'init-dir': init_dir,
156     'set-dir': set_dir,
157     'list-indexes': list_indexes,
158     'send-index': send_index,
159     'receive-objects': receive_objects,
160     'read-ref': read_ref,
161     'update-ref': update_ref,
162     'cat': cat,
163 }
164
165 # FIXME: this protocol is totally lame and not at all future-proof.
166 # (Especially since we abort completely as soon as *anything* bad happens)
167 conn = Conn(sys.stdin, sys.stdout)
168 lr = linereader(conn)
169 for _line in lr:
170     line = _line.strip()
171     if not line:
172         continue
173     debug1('bup server: command: %r\n' % line)
174     words = line.split(' ', 1)
175     cmd = words[0]
176     rest = len(words)>1 and words[1] or ''
177     if cmd == 'quit':
178         break
179     else:
180         cmd = commands.get(cmd)
181         if cmd:
182             cmd(conn, rest)
183         else:
184             raise Exception('unknown server command: %r\n' % line)
185
186 debug1('bup server: done\n')