]> arthur.barton.de Git - bup.git/blob - cmd/server-cmd.py
server: only suggest a max of one pack per receive-objects cycle.
[bup.git] / cmd / server-cmd.py
1 #!/usr/bin/env python
2 import sys, struct, mmap
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     log('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     log('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.PackIdx(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         #log('expecting %d bytes\n' % n)
55         if not n:
56             log('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             log('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         #log('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 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             w.objcache.refresh(skip_midx = True)
92             oldpack = w.objcache.exists(sha)
93             log('new suggestion: %r\n' % oldpack)
94             assert(oldpack)
95             assert(oldpack != True)
96             assert(not oldpack.endswith('.midx'))
97             w.objcache.refresh(skip_midx = False)
98         if not suggested and oldpack:
99             assert(oldpack.endswith('.idx'))
100             (dir,name) = os.path.split(oldpack)
101             if not (name in suggested):
102                 log("bup server: suggesting index %s\n" % name)
103                 conn.write('index %s\n' % name)
104                 suggested[name] = 1
105         else:
106             w._raw_write([buf])
107     # NOTREACHED
108
109
110 def read_ref(conn, refname):
111     git.check_repo_or_die()
112     r = git.read_ref(refname)
113     conn.write('%s\n' % (r or '').encode('hex'))
114     conn.ok()
115
116
117 def update_ref(conn, refname):
118     git.check_repo_or_die()
119     newval = conn.readline().strip()
120     oldval = conn.readline().strip()
121     git.update_ref(refname, newval.decode('hex'), oldval.decode('hex'))
122     conn.ok()
123
124
125 def cat(conn, id):
126     git.check_repo_or_die()
127     try:
128         for blob in git.cat(id):
129             conn.write(struct.pack('!I', len(blob)))
130             conn.write(blob)
131     except KeyError, e:
132         log('server: error: %s\n' % e)
133         conn.write('\0\0\0\0')
134         conn.error(e)
135     else:
136         conn.write('\0\0\0\0')
137         conn.ok()
138
139
140 optspec = """
141 bup server
142 """
143 o = options.Options('bup server', optspec)
144 (opt, flags, extra) = o.parse(sys.argv[1:])
145
146 if extra:
147     o.fatal('no arguments expected')
148
149 log('bup server: reading from stdin.\n')
150
151 commands = {
152     'init-dir': init_dir,
153     'set-dir': set_dir,
154     'list-indexes': list_indexes,
155     'send-index': send_index,
156     'receive-objects': receive_objects,
157     'read-ref': read_ref,
158     'update-ref': update_ref,
159     'cat': cat,
160 }
161
162 # FIXME: this protocol is totally lame and not at all future-proof.
163 # (Especially since we abort completely as soon as *anything* bad happens)
164 conn = Conn(sys.stdin, sys.stdout)
165 lr = linereader(conn)
166 for _line in lr:
167     line = _line.strip()
168     if not line:
169         continue
170     log('bup server: command: %r\n' % line)
171     words = line.split(' ', 1)
172     cmd = words[0]
173     rest = len(words)>1 and words[1] or ''
174     if cmd == 'quit':
175         break
176     else:
177         cmd = commands.get(cmd)
178         if cmd:
179             cmd(conn, rest)
180         else:
181             raise Exception('unknown server command: %r\n' % line)
182
183 log('bup server: done\n')