]> arthur.barton.de Git - bup.git/blobdiff - cmd/server-cmd.py
Change server_mode=='dumb' to just a dumb_server_mode bool.
[bup.git] / cmd / server-cmd.py
index c74ffbffb43339f6b6bd9f0345dfc4f939a5375a..42ec1d4ef4a59242928ce9a5d55f5b249b288369 100755 (executable)
@@ -1,28 +1,40 @@
 #!/usr/bin/env python
-import sys, struct, mmap
+import os, sys, struct
 from bup import options, git
 from bup.helpers import *
 
 suspended_w = None
+dumb_server_mode = False
+
+def _set_mode():
+    global dumb_server_mode
+    dumb_server_mode = os.path.exists(git.repo('bup-dumb-server'))
+    debug1('bup server: serving in %s mode\n' 
+           % (dumb_server_mode and 'dumb' or 'smart'))
 
 
 def init_dir(conn, arg):
     git.init_repo(arg)
-    log('bup server: bupdir initialized: %r\n' % git.repodir)
+    debug1('bup server: bupdir initialized: %r\n' % git.repodir)
+    _set_mode()
     conn.ok()
 
 
 def set_dir(conn, arg):
     git.check_repo_or_die(arg)
-    log('bup server: bupdir is %r\n' % git.repodir)
+    debug1('bup server: bupdir is %r\n' % git.repodir)
+    _set_mode()
     conn.ok()
 
     
 def list_indexes(conn, junk):
     git.check_repo_or_die()
+    suffix = ''
+    if dumb_server_mode:
+        suffix = ' load'
     for f in os.listdir(git.repo('objects/pack')):
         if f.endswith('.idx'):
-            conn.write('%s\n' % f)
+            conn.write('%s%s\n' % (f, suffix))
     conn.ok()
 
 
@@ -30,13 +42,13 @@ def send_index(conn, name):
     git.check_repo_or_die()
     assert(name.find('/') < 0)
     assert(name.endswith('.idx'))
-    idx = git.PackIdx(git.repo('objects/pack/%s' % name))
+    idx = git.open_idx(git.repo('objects/pack/%s' % name))
     conn.write(struct.pack('!I', len(idx.map)))
     conn.write(idx.map)
     conn.ok()
 
 
-def receive_objects(conn, junk):
+def receive_objects_v2(conn, junk):
     global suspended_w
     git.check_repo_or_die()
     suggested = {}
@@ -51,33 +63,37 @@ def receive_objects(conn, junk):
             w.abort()
             raise Exception('object read: expected length header, got EOF\n')
         n = struct.unpack('!I', ns)[0]
-        #log('expecting %d bytes\n' % n)
+        #debug2('expecting %d bytes\n' % n)
         if not n:
-            log('bup server: received %d object%s.\n' 
+            debug1('bup server: received %d object%s.\n' 
                 % (w.count, w.count!=1 and "s" or ''))
-            fullpath = w.close()
+            fullpath = w.close(run_midx=not dumb_server_mode)
             if fullpath:
                 (dir, name) = os.path.split(fullpath)
                 conn.write('%s.idx\n' % name)
             conn.ok()
             return
         elif n == 0xffffffff:
-            log('bup server: receive-objects suspended.\n')
+            debug2('bup server: receive-objects suspended.\n')
             suspended_w = w
             conn.ok()
             return
             
+        shar = conn.read(20)
+        crcr = struct.unpack('!I', conn.read(4))[0]
+        n -= 20 + 4
         buf = conn.read(n)  # object sizes in bup are reasonably small
-        #log('read %d bytes\n' % n)
+        #debug2('read %d bytes\n' % n)
         if len(buf) < n:
             w.abort()
             raise Exception('object read: expected %d bytes, got %d\n'
                             % (n, len(buf)))
-        (type, content) = git._decode_packobj(buf)
-        sha = git.calc_hash(type, content)
-        oldpack = w.exists(sha)
+        if dumb_server_mode:
+            oldpack = None
+        else:
+            oldpack = w.exists(shar)
         # FIXME: we only suggest a single index per cycle, because the client
-        # is currently dumb to download more than one per cycle anyway.
+        # is currently too dumb to download more than one per cycle anyway.
         # Actually we should fix the client, but this is a minor optimization
         # on the server side.
         if not suggested and \
@@ -88,23 +104,30 @@ def receive_objects(conn, junk):
             # fix that deficiency of midx files eventually, although it'll
             # make the files bigger.  This method is certainly not very
             # efficient.
-            w.objcache.refresh(skip_midx = True)
-            oldpack = w.objcache.exists(sha)
-            log('new suggestion: %r\n' % oldpack)
+            oldpack = w.objcache.packname_containing(shar)
+            debug2('new suggestion: %r\n' % oldpack)
             assert(oldpack)
             assert(oldpack != True)
             assert(not oldpack.endswith('.midx'))
-            w.objcache.refresh(skip_midx = False)
+            w.objcache.refresh()
         if not suggested and oldpack:
             assert(oldpack.endswith('.idx'))
             (dir,name) = os.path.split(oldpack)
             if not (name in suggested):
-                log("bup server: suggesting index %s\n" % name)
+                debug1("bup server: suggesting index %s\n" % name)
                 conn.write('index %s\n' % name)
                 suggested[name] = 1
         else:
-            w._raw_write([buf])
+            nw, crc = w._raw_write([buf], sha=shar)
+            _check(w, crcr, crc, 'object read: expected crc %d, got %d\n')
+            _check(w, n, nw, 'object read: expected %d bytes, got %d\n')
     # NOTREACHED
+    
+
+def _check(w, expected, actual, msg):
+    if expected != actual:
+        w.abort()
+        raise Exception(msg % (expected, actual))
 
 
 def read_ref(conn, refname):
@@ -122,10 +145,14 @@ def update_ref(conn, refname):
     conn.ok()
 
 
+cat_pipe = None
 def cat(conn, id):
+    global cat_pipe
     git.check_repo_or_die()
+    if not cat_pipe:
+        cat_pipe = git.CatPipe()
     try:
-        for blob in git.cat(id):
+        for blob in cat_pipe.join(id):
             conn.write(struct.pack('!I', len(blob)))
             conn.write(blob)
     except KeyError, e:
@@ -146,14 +173,14 @@ o = options.Options('bup server', optspec)
 if extra:
     o.fatal('no arguments expected')
 
-log('bup server: reading from stdin.\n')
+debug2('bup server: reading from stdin.\n')
 
 commands = {
     'init-dir': init_dir,
     'set-dir': set_dir,
     'list-indexes': list_indexes,
     'send-index': send_index,
-    'receive-objects': receive_objects,
+    'receive-objects-v2': receive_objects_v2,
     'read-ref': read_ref,
     'update-ref': update_ref,
     'cat': cat,
@@ -167,7 +194,7 @@ for _line in lr:
     line = _line.strip()
     if not line:
         continue
-    log('bup server: command: %r\n' % line)
+    debug1('bup server: command: %r\n' % line)
     words = line.split(' ', 1)
     cmd = words[0]
     rest = len(words)>1 and words[1] or ''
@@ -180,4 +207,4 @@ for _line in lr:
         else:
             raise Exception('unknown server command: %r\n' % line)
 
-log('bup server: done\n')
+debug1('bup server: done\n')