]> arthur.barton.de Git - bup.git/commitdiff
check_repo_or_die: don't test via stat "pack/."
authorRob Browning <rlb@defaultvalue.org>
Fri, 16 Dec 2016 17:48:03 +0000 (11:48 -0600)
committerRob Browning <rlb@defaultvalue.org>
Sat, 17 Dec 2016 16:49:14 +0000 (10:49 -0600)
Apparently this actually returns valid stat info on OS X now:

  python -c 'import os; print os.stat('plain-file/.')

No idea why, but since check_repo_or_die() currently depends on that
throwing an exception, rewrite it to avoid the problem.

Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
lib/bup/git.py
lib/bup/helpers.py

index e0f285df00051fba4376d37884ad3101d9e00d44..0f701a5d623cb3090d36631595dbda33feb1bc4c 100644 (file)
@@ -12,7 +12,8 @@ from bup.helpers import (Sha1, add_error, chunkyreader, debug1, debug2,
                          fdatasync,
                          hostname, localtime, log, merge_iter,
                          mmap_read, mmap_readwrite,
-                         progress, qprogress, unlink, username, userfullname,
+                         progress, qprogress, stat_if_exists,
+                         unlink, username, userfullname,
                          utc_offset_str)
 
 
@@ -1014,21 +1015,20 @@ def init_repo(path=None):
 
 
 def check_repo_or_die(path=None):
-    """Make sure a bup repository exists, and abort if not.
-    If the path to a particular repository was not specified, this function
-    initializes the default repository automatically.
-    """
+    """Check to see if a bup repository probably exists, and abort if not."""
     guess_repo(path)
-    try:
-        os.stat(repo('objects/pack/.'))
-    except OSError as e:
-        if e.errno == errno.ENOENT:
-            log('error: %r is not a bup repository; run "bup init"\n'
-                % repo())
+    top = repo()
+    pst = stat_if_exists(top + '/objects/pack')
+    if pst and stat.S_ISDIR(pst.st_mode):
+        return
+    if not pst:
+        top_st = stat_if_exists(top)
+        if not top_st:
+            log('error: repository %r does not exist (see "bup help init")\n'
+                % top)
             sys.exit(15)
-        else:
-            log('error: %s\n' % e)
-            sys.exit(14)
+    log('error: %r is not a repository\n' % top)
+    sys.exit(14)
 
 
 _ver = None
index f7cd26883ccfede83243719c02cb8a36fc37b7a4..86ac90f5793d2d38ba1a5d84c1460376e022781c 100644 (file)
@@ -91,6 +91,15 @@ def partition(predicate, stream):
     return (leading_matches(), rest())
 
 
+def stat_if_exists(path):
+    try:
+        return os.stat(path)
+    except OSError as e:
+        if e.errno != errno.ENOENT:
+            raise
+    return None
+
+
 # Write (blockingly) to sockets that may or may not be in blocking mode.
 # We need this because our stderr is sometimes eaten by subprocesses
 # (probably ssh) that sometimes make it nonblocking, if only temporarily,