]> arthur.barton.de Git - bup.git/blobdiff - cmd/save-cmd.py
save: catch and log SSH errors
[bup.git] / cmd / save-cmd.py
index 3f1649a5e74c87f875f96c91d4bbff76439554f2..b8e4f7aa597d999d877a31a40b36f4c40e205a71 100755 (executable)
@@ -1,5 +1,8 @@
 #!/usr/bin/env python
 import sys, stat, time, math
+from cStringIO import StringIO
+from errno import EACCES
+
 from bup import hashsplit, git, options, index, client, metadata, hlinkdb
 from bup.helpers import *
 from bup.hashsplit import GIT_MODE_TREE, GIT_MODE_FILE, GIT_MODE_SYMLINK
@@ -71,9 +74,13 @@ if opt.name and opt.name.startswith('.'):
     o.fatal("'%s' is not a valid branch name" % opt.name)
 refname = opt.name and 'refs/heads/%s' % opt.name or None
 if opt.remote or is_reverse:
-    cli = client.Client(opt.remote)
+    try:
+        cli = client.Client(opt.remote)
+    except client.ClientError, e:
+        log('error: %s' % e)
+        sys.exit(1)
     oldref = refname and cli.read_ref(refname) or None
-    w = cli.new_packwriter()
+    w = cli.new_packwriter(compression_level=opt.compress)
 else:
     cli = None
     oldref = refname and git.read_ref(refname) or None
@@ -124,12 +131,16 @@ def _pop(force_tree, dir_metadata=None):
     part = parts.pop()
     shalist = shalists.pop()
     metalist = metalists.pop()
-    if metalist:
+    if metalist and not force_tree:
         if dir_metadata: # Override the original metadata pushed for this dir.
             metalist = [('', dir_metadata)] + metalist[1:]
         sorted_metalist = sorted(metalist, key = lambda x : x[0])
         metadata = ''.join([m[1].encode() for m in sorted_metalist])
-        shalist.append((0100644, '.bupm', w.new_blob(metadata)))
+        metadata_f = StringIO(metadata)
+        mode, id = hashsplit.split_to_blob_or_tree(w.new_blob, w.new_tree,
+                                                   [metadata_f],
+                                                   keep_boundaries=False)
+        shalist.append((mode, '.bupm', id))
     tree = force_tree or w.new_tree(shalist)
     if shalists:
         shalists[-1].append((GIT_MODE_TREE,
@@ -180,10 +191,13 @@ def progress_report(n):
 
 indexfile = opt.indexfile or git.repo('bupindex')
 r = index.Reader(indexfile)
-if not os.access(indexfile + '.meta', os.W_OK|os.R_OK):
-    log('error: cannot access "%s"; have you run bup index?' % indexfile)
+try:
+    msr = index.MetaStoreReader(indexfile + '.meta')
+except IOError, ex:
+    if ex.errno != EACCES:
+        raise
+    log('error: cannot access %r; have you run bup index?' % indexfile)
     sys.exit(1)
-msr = index.MetaStoreReader(indexfile + '.meta')
 hlink_db = hlinkdb.HLinkDB(indexfile + '.hlink')
 
 def already_saved(ent):
@@ -264,7 +278,8 @@ for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_during):
         continue
     if opt.smaller and ent.size >= opt.smaller:
         if exists and not hashvalid:
-            add_error('skipping large file "%s"' % ent.name)
+            if opt.verbose:
+                log('skipping large file "%s"\n' % ent.name)
             lastskip_name = ent.name
         continue
 
@@ -296,8 +311,13 @@ for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_during):
         dir_name, fs_path = dirp[0]
         first_root = dirp[0]
         # Not indexed, so just grab the FS metadata or use empty metadata.
-        meta = metadata.from_path(fs_path) if fs_path else metadata.Metadata()
-        _push(dir_name, meta)
+        try:
+           meta = metadata.from_path(fs_path) if fs_path else metadata.Metadata()
+        except (OSError, IOError), e:
+            add_error(e)
+            lastskip_name = dir_name
+        else:
+           _push(dir_name, meta)
     elif first_root != dirp[0]:
         root_collision = True
 
@@ -309,8 +329,13 @@ for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_during):
     for path_component in dirp[len(parts):]:
         dir_name, fs_path = path_component
         # Not indexed, so just grab the FS metadata or use empty metadata.
-        meta = metadata.from_path(fs_path) if fs_path else metadata.Metadata()
-        _push(dir_name, meta)
+        try:
+           meta = metadata.from_path(fs_path) if fs_path else metadata.Metadata()
+        except (OSError, IOError), e:
+            add_error(e)
+            lastskip_name = dir_name
+        else:
+           _push(dir_name, meta)
 
     if not file:
         if len(parts) == 1:
@@ -381,9 +406,14 @@ for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_during):
             shalists[-1].append(git_info)
             sort_key = git.shalist_item_sort_key((ent.mode, file, id))
             hlink = find_hardlink_target(hlink_db, ent)
-            metalists[-1].append((sort_key,
-                                  metadata.from_path(ent.name,
-                                                     hardlink_target=hlink)))
+            try:
+                meta = metadata.from_path(ent.name, hardlink_target=hlink)
+            except (OSError, IOError), e:
+                add_error(e)
+                lastskip_name = ent.name
+            else:
+                metalists[-1].append((sort_key, meta))
+
     if exists and wasmissing:
         count += oldsize
         subcount = 0
@@ -407,7 +437,7 @@ tree = _pop(force_tree = None,
 if opt.tree:
     print tree.encode('hex')
 if opt.commit or opt.name:
-    msg = 'bup save\n\nGenerated by command:\n%r' % sys.argv
+    msg = 'bup save\n\nGenerated by command:\n%r\n' % sys.argv
     commit = w.new_commit(oldref, tree, date, msg)
     if opt.commit:
         print commit.encode('hex')