Browse Source

Merge pull request #4713 from saurvs/forward-port-4698

Forward port #4698
TW 5 years ago
parent
commit
97eca3d287
1 changed files with 14 additions and 3 deletions
  1. 14 3
      src/borg/repository.py

+ 14 - 3
src/borg/repository.py

@@ -2,6 +2,7 @@ import errno
 import mmap
 import mmap
 import os
 import os
 import shutil
 import shutil
+import stat
 import struct
 import struct
 import time
 import time
 from binascii import hexlify, unhexlify
 from binascii import hexlify, unhexlify
@@ -233,11 +234,17 @@ class Repository:
             repository, user's can only use the quota'd repository, when their --restrict-to-path points
             repository, user's can only use the quota'd repository, when their --restrict-to-path points
             at the user's repository.
             at the user's repository.
         """
         """
-        if os.path.exists(path):
+        try:
+            st = os.stat(path)
+        except FileNotFoundError:
+            pass  # nothing there!
+        else:
+            # there is something already there!
             if self.is_repository(path):
             if self.is_repository(path):
                 raise self.AlreadyExists(path)
                 raise self.AlreadyExists(path)
-            if not os.path.isdir(path) or os.listdir(path):
+            if not stat.S_ISDIR(st.st_mode) or os.listdir(path):
                 raise self.PathAlreadyExists(path)
                 raise self.PathAlreadyExists(path)
+            # an empty directory is acceptable for us.
 
 
         while True:
         while True:
             # Check all parent directories for Borg's repository README
             # Check all parent directories for Borg's repository README
@@ -386,8 +393,12 @@ class Repository:
 
 
     def open(self, path, exclusive, lock_wait=None, lock=True):
     def open(self, path, exclusive, lock_wait=None, lock=True):
         self.path = path
         self.path = path
-        if not os.path.isdir(path):
+        try:
+            st = os.stat(path)
+        except FileNotFoundError:
             raise self.DoesNotExist(path)
             raise self.DoesNotExist(path)
+        if not stat.S_ISDIR(st.st_mode):
+            raise self.InvalidRepository(path)
         if lock:
         if lock:
             self.lock = Lock(os.path.join(path, 'lock'), exclusive, timeout=lock_wait).acquire()
             self.lock = Lock(os.path.join(path, 'lock'), exclusive, timeout=lock_wait).acquire()
         else:
         else: