]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/helpers.py
Honor git config pack.packSizeLimit when set
[bup.git] / lib / bup / helpers.py
index 3c4cdb20b16099b602f09e7a4272fd7940aadcd9..86ac90f5793d2d38ba1a5d84c1460376e022781c 100644 (file)
@@ -9,6 +9,12 @@ import hashlib, heapq, math, operator, time, grp, tempfile
 
 from bup import _helpers
 
+
+class Nonlocal:
+    """Helper to deal with Python scoping issues"""
+    pass
+
+
 sc_page_size = os.sysconf('SC_PAGE_SIZE')
 assert(sc_page_size > 0)
 
@@ -41,11 +47,57 @@ def atof(s):
 buglvl = atoi(os.environ.get('BUP_DEBUG', 0))
 
 
-# If the platform doesn't have fdatasync (OS X), fall back to fsync.
 try:
-    fdatasync = os.fdatasync
+    _fdatasync = os.fdatasync
 except AttributeError:
-    fdatasync = os.fsync
+    _fdatasync = os.fsync
+
+if sys.platform.startswith('darwin'):
+    # Apparently os.fsync on OS X doesn't guarantee to sync all the way down
+    import fcntl
+    def fdatasync(fd):
+        try:
+            return fcntl.fcntl(fd, fcntl.F_FULLFSYNC)
+        except IOError as e:
+            # Fallback for file systems (SMB) that do not support F_FULLFSYNC
+            if e.errno == errno.ENOTSUP:
+                return _fdatasync(fd)
+            else:
+                raise
+else:
+    fdatasync = _fdatasync
+
+
+def partition(predicate, stream):
+    """Returns (leading_matches_it, rest_it), where leading_matches_it
+    must be completely exhausted before traversing rest_it.
+
+    """
+    stream = iter(stream)
+    ns = Nonlocal()
+    ns.first_nonmatch = None
+    def leading_matches():
+        for x in stream:
+            if predicate(x):
+                yield x
+            else:
+                ns.first_nonmatch = (x,)
+                break
+    def rest():
+        if ns.first_nonmatch:
+            yield ns.first_nonmatch[0]
+            for x in stream:
+                yield x
+    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.
@@ -59,7 +111,7 @@ def _hard_write(fd, buf):
             raise IOError('select(fd) returned without being writable')
         try:
             sz = os.write(fd, buf)
-        except OSError, e:
+        except OSError as e:
             if e.errno != errno.EAGAIN:
                 raise
         assert(sz >= 0)
@@ -129,7 +181,7 @@ def mkdirp(d, mode=None):
             os.makedirs(d, mode)
         else:
             os.makedirs(d)
-    except OSError, e:
+    except OSError as e:
         if e.errno == errno.EEXIST:
             pass
         else:
@@ -193,14 +245,15 @@ def unlink(f):
     """
     try:
         os.unlink(f)
-    except OSError, e:
+    except OSError as e:
         if e.errno != errno.ENOENT:
             raise
 
 
-def readpipe(argv, preexec_fn=None):
+def readpipe(argv, preexec_fn=None, shell=False):
     """Run a subprocess and return its output."""
-    p = subprocess.Popen(argv, stdout=subprocess.PIPE, preexec_fn=preexec_fn)
+    p = subprocess.Popen(argv, stdout=subprocess.PIPE, preexec_fn=preexec_fn,
+                         shell=shell)
     out, err = p.communicate()
     if p.returncode != 0:
         raise Exception('subprocess %r failed with status %d'
@@ -243,8 +296,8 @@ to return multiple strings in order to respect ARG_MAX)."""
         yield readpipe(command + sub_args, preexec_fn=preexec_fn)
 
 
-def realpath(p):
-    """Get the absolute path of a file.
+def resolve_parent(p):
+    """Return the absolute path of a file without following any final symlink.
 
     Behaves like os.path.realpath, but doesn't follow a symlink for the last
     element. (ie. if 'p' itself is a symlink, this one won't follow it, but it
@@ -269,8 +322,16 @@ def detect_fakeroot():
     return os.getenv("FAKEROOTKEY") != None
 
 
+_warned_about_superuser_detection = None
 def is_superuser():
     if sys.platform.startswith('cygwin'):
+        if sys.getwindowsversion()[0] > 5:
+            # Sounds like situation is much more complicated here
+            global _warned_about_superuser_detection
+            if not _warned_about_superuser_detection:
+                log("can't detect root status for OS version > 5; assuming not root")
+                _warned_about_superuser_detection = True
+            return False
         import ctypes
         return ctypes.cdll.shell32.IsUserAnAdmin()
     else:
@@ -753,7 +814,7 @@ if _mincore:
             msize = min(_fmincore_chunk_size, st.st_size - pos)
             try:
                 m = mmap.mmap(fd, msize, mmap.MAP_PRIVATE, 0, 0, pos)
-            except mmap.error, ex:
+            except mmap.error as ex:
                 if ex.errno == errno.EINVAL or ex.errno == errno.ENODEV:
                     # Perhaps the file was a pipe, i.e. "... | bup split ..."
                     return None
@@ -832,6 +893,15 @@ def clear_errors():
     saved_errors = []
 
 
+def die_if_errors(msg=None, status=1):
+    global saved_errors
+    if saved_errors:
+        if not msg:
+            msg = 'warning: %d errors encountered\n' % len(saved_errors)
+        log(msg)
+        sys.exit(status)
+
+
 def handle_ctrl_c():
     """Replace the default exception handler for KeyboardInterrupt (Ctrl-C).
 
@@ -878,7 +948,7 @@ def parse_date_or_fatal(str, fatal):
     For now we expect a string that contains a float."""
     try:
         date = float(str)
-    except ValueError, e:
+    except ValueError as e:
         raise fatal('invalid date format (should be a float): %r' % e)
     else:
         return date
@@ -891,15 +961,15 @@ def parse_excludes(options, fatal):
     for flag in options:
         (option, parameter) = flag
         if option == '--exclude':
-            excluded_paths.append(realpath(parameter))
+            excluded_paths.append(resolve_parent(parameter))
         elif option == '--exclude-from':
             try:
-                f = open(realpath(parameter))
-            except IOError, e:
+                f = open(resolve_parent(parameter))
+            except IOError as e:
                 raise fatal("couldn't read %s" % parameter)
             for exclude_path in f.readlines():
                 # FIXME: perhaps this should be rstrip('\n')
-                exclude_path = realpath(exclude_path.strip())
+                exclude_path = resolve_parent(exclude_path.strip())
                 if exclude_path:
                     excluded_paths.append(exclude_path)
     return sorted(frozenset(excluded_paths))
@@ -915,12 +985,12 @@ def parse_rx_excludes(options, fatal):
         if option == '--exclude-rx':
             try:
                 excluded_patterns.append(re.compile(parameter))
-            except re.error, ex:
+            except re.error as ex:
                 fatal('invalid --exclude-rx pattern (%s): %s' % (parameter, ex))
         elif option == '--exclude-rx-from':
             try:
-                f = open(realpath(parameter))
-            except IOError, e:
+                f = open(resolve_parent(parameter))
+            except IOError as e:
                 raise fatal("couldn't read %s" % parameter)
             for pattern in f.readlines():
                 spattern = pattern.rstrip('\n')
@@ -928,7 +998,7 @@ def parse_rx_excludes(options, fatal):
                     continue
                 try:
                     excluded_patterns.append(re.compile(spattern))
-                except re.error, ex:
+                except re.error as ex:
                     fatal('invalid --exclude-rx pattern (%s): %s' % (spattern, ex))
     return excluded_patterns
 
@@ -1075,3 +1145,41 @@ else:
         return time.strftime('%z', localtime(t))
     def to_py_time(x):
         return x
+
+
+_some_invalid_save_parts_rx = re.compile(r'[[ ~^:?*\\]|\.\.|//|@{')
+
+def valid_save_name(name):
+    # Enforce a superset of the restrictions in git-check-ref-format(1)
+    if name == '@' \
+       or name.startswith('/') or name.endswith('/') \
+       or name.endswith('.'):
+        return False
+    if _some_invalid_save_parts_rx.search(name):
+        return False
+    for c in name:
+        if ord(c) < 0x20 or ord(c) == 0x7f:
+            return False
+    for part in name.split('/'):
+        if part.startswith('.') or part.endswith('.lock'):
+            return False
+    return True
+
+
+_period_rx = re.compile(r'^([0-9]+)(s|min|h|d|w|m|y)$')
+
+def period_as_secs(s):
+    if s == 'forever':
+        return float('inf')
+    match = _period_rx.match(s)
+    if not match:
+        return None
+    mag = int(match.group(1))
+    scale = match.group(2)
+    return mag * {'s': 1,
+                  'min': 60,
+                  'h': 60 * 60,
+                  'd': 60 * 60 * 24,
+                  'w': 60 * 60 * 24 * 7,
+                  'm': 60 * 60 * 24 * 31,
+                  'y': 60 * 60 * 24 * 366}[scale]