]> arthur.barton.de Git - bup.git/blob - cmd/server-cmd.py
PackIdxList.refresh(): remember to exclude old midx files.
[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         if oldpack and (oldpack == True or oldpack.endswith('.midx')):
80             # FIXME: we shouldn't really have to know about midx files
81             # at this layer.  But exists() on a midx doesn't return the
82             # packname (since it doesn't know)... probably we should just
83             # fix that deficiency of midx files eventually, although it'll
84             # make the files bigger.  This method is certainly not very
85             # efficient.
86             w.objcache.refresh(skip_midx = True)
87             oldpack = w.objcache.exists(sha)
88             log('new suggestion: %r\n' % oldpack)
89             assert(oldpack)
90             assert(oldpack != True)
91             assert(not oldpack.endswith('.midx'))
92             w.objcache.refresh(skip_midx = False)
93         if oldpack:
94             assert(oldpack.endswith('.idx'))
95             (dir,name) = os.path.split(oldpack)
96             if not (name in suggested):
97                 log("bup server: suggesting index %s\n" % name)
98                 conn.write('index %s\n' % name)
99                 suggested[name] = 1
100         else:
101             w._raw_write([buf])
102     # NOTREACHED
103
104
105 def read_ref(conn, refname):
106     git.check_repo_or_die()
107     r = git.read_ref(refname)
108     conn.write('%s\n' % (r or '').encode('hex'))
109     conn.ok()
110
111
112 def update_ref(conn, refname):
113     git.check_repo_or_die()
114     newval = conn.readline().strip()
115     oldval = conn.readline().strip()
116     git.update_ref(refname, newval.decode('hex'), oldval.decode('hex'))
117     conn.ok()
118
119
120 def cat(conn, id):
121     git.check_repo_or_die()
122     try:
123         for blob in git.cat(id):
124             conn.write(struct.pack('!I', len(blob)))
125             conn.write(blob)
126     except KeyError, e:
127         log('server: error: %s\n' % e)
128         conn.write('\0\0\0\0')
129         conn.error(e)
130     else:
131         conn.write('\0\0\0\0')
132         conn.ok()
133
134
135 optspec = """
136 bup server
137 """
138 o = options.Options('bup server', optspec)
139 (opt, flags, extra) = o.parse(sys.argv[1:])
140
141 if extra:
142     o.fatal('no arguments expected')
143
144 log('bup server: reading from stdin.\n')
145
146 commands = {
147     'init-dir': init_dir,
148     'set-dir': set_dir,
149     'list-indexes': list_indexes,
150     'send-index': send_index,
151     'receive-objects': receive_objects,
152     'read-ref': read_ref,
153     'update-ref': update_ref,
154     'cat': cat,
155 }
156
157 # FIXME: this protocol is totally lame and not at all future-proof.
158 # (Especially since we abort completely as soon as *anything* bad happens)
159 conn = Conn(sys.stdin, sys.stdout)
160 lr = linereader(conn)
161 for _line in lr:
162     line = _line.strip()
163     if not line:
164         continue
165     log('bup server: command: %r\n' % line)
166     words = line.split(' ', 1)
167     cmd = words[0]
168     rest = len(words)>1 and words[1] or ''
169     if cmd == 'quit':
170         break
171     else:
172         cmd = commands.get(cmd)
173         if cmd:
174             cmd(conn, rest)
175         else:
176             raise Exception('unknown server command: %r\n' % line)
177
178 log('bup server: done\n')