3 from bup import options, git
4 from bup.helpers import *
9 def init_dir(conn, arg):
11 debug1('bup server: bupdir initialized: %r\n' % git.repodir)
15 def set_dir(conn, arg):
16 git.check_repo_or_die(arg)
17 debug1('bup server: bupdir is %r\n' % git.repodir)
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)
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)))
39 def receive_objects(conn, junk):
41 git.check_repo_or_die()
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)
56 debug1('bup server: received %d object%s.\n'
57 % (w.count, w.count!=1 and "s" or ''))
60 (dir, name) = os.path.split(fullpath)
61 conn.write('%s.idx\n' % name)
65 debug2('bup server: receive-objects suspended.\n')
70 buf = conn.read(n) # object sizes in bup are reasonably small
71 #debug2('read %d bytes\n' % n)
74 raise Exception('object read: expected %d bytes, got %d\n'
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
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
91 w.objcache.refresh(skip_midx = True)
92 oldpack = w.objcache.exists(sha)
93 debug2('new suggestion: %r\n' % 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 debug1("bup server: suggesting index %s\n" % name)
103 conn.write('index %s\n' % name)
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'))
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'))
128 git.check_repo_or_die()
130 cat_pipe = git.CatPipe()
132 for blob in cat_pipe.join(id):
133 conn.write(struct.pack('!I', len(blob)))
136 log('server: error: %s\n' % e)
137 conn.write('\0\0\0\0')
140 conn.write('\0\0\0\0')
147 o = options.Options('bup server', optspec)
148 (opt, flags, extra) = o.parse(sys.argv[1:])
151 o.fatal('no arguments expected')
153 debug2('bup server: reading from stdin.\n')
156 'init-dir': init_dir,
158 'list-indexes': list_indexes,
159 'send-index': send_index,
160 'receive-objects': receive_objects,
161 'read-ref': read_ref,
162 'update-ref': update_ref,
166 # FIXME: this protocol is totally lame and not at all future-proof.
167 # (Especially since we abort completely as soon as *anything* bad happens)
168 conn = Conn(sys.stdin, sys.stdout)
169 lr = linereader(conn)
174 debug1('bup server: command: %r\n' % line)
175 words = line.split(' ', 1)
177 rest = len(words)>1 and words[1] or ''
181 cmd = commands.get(cmd)
185 raise Exception('unknown server command: %r\n' % line)
187 debug1('bup server: done\n')