]> arthur.barton.de Git - bup.git/commitdiff
Adds --graft option to bup save.
authorZoran Zaric <zz@zoranzaric.de>
Tue, 4 Jan 2011 02:22:27 +0000 (03:22 +0100)
committerAvery Pennarun <apenwarr@gmail.com>
Tue, 4 Jan 2011 21:09:23 +0000 (13:09 -0800)
This adds the option to rename paths when saving them.

A directory /root/chroot/a/etc saved with "bup save -n chroots
--graft /root/chroot/a/etc=/chroots/a" would be saved as
/chroots/a/etc.

Signed-off-by: Zoran Zaric <zz@zoranzaric.de>
Documentation/bup-save.md
cmd/save-cmd.py
lib/bup/helpers.py
lib/bup/t/thelpers.py
t/test.sh

index 638e5019d2710204e1ffb6f8e9b3d9830dea27b2..caadd4e99ed9d308b20c7b86dcb42967c002df4a 100644 (file)
@@ -89,6 +89,13 @@ for `bup-index`(1).
     "bup save -n webserver --strip-path=/root/chroots" would
     be saved as */webserver/etc*
     
+--graft=*old_path*=*new_path*
+:   a graft point *old_path*=*new_path* (can be used more than
+    once).
+
+    A directory */root/chroot/a/etc* saved with
+    "bup save -n chroots --graft /root/chroot/a/etc=/chroots/a"
+    would be saved as */chroots/a/etc*
 
 # EXAMPLE
     
index fe1519e2e5cb08df5b4e5191e03d988ecb80c284..4720af714ee52f1a095b918ff31c2e4bd97616bd 100755 (executable)
@@ -19,6 +19,7 @@ bwlimit=   maximum bytes/sec to transmit to server
 f,indexfile=  the name of the index file (normally BUP_DIR/bupindex)
 strip      strips the path to every filename given
 strip-path= path-prefix to be stripped when saving
+graft=     a graft point *old_path*=*new_path* (can be used morethan once)
 """
 o = options.Options('bup save', optspec)
 (opt, flags, extra) = o.parse(sys.argv[1:])
@@ -42,6 +43,22 @@ else:
 if opt.strip and opt.strip_path:
     o.fatal("--strip is incompatible with --strip-path")
 
+graft_points = []
+if opt.graft:
+    if opt.strip:
+        o.fatal("--strip is incompatible with --graft")
+
+    if opt.strip_path:
+        o.fatal("--strip-path is incompatible with --graft")
+
+    for (option, parameter) in flags:
+        if option == "--graft":
+            splitted_parameter = parameter.split('=')
+            if len(splitted_parameter) != 2:
+                o.fatal("a graft point must be of the form old_path=new_path")
+            graft_points.append((realpath(splitted_parameter[0]),
+                                 realpath(splitted_parameter[1])))
+
 is_reverse = os.environ.get('BUP_SERVER_REVERSE')
 if is_reverse and opt.remote:
     o.fatal("don't use -r in reverse mode; it's automatic")
@@ -209,6 +226,9 @@ for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_during):
         dirp = stripped_base_path.split('/')
     elif opt.strip_path:
         dirp = strip_path(opt.strip_path, dir).split('/')
+    elif graft_points:
+        grafted = graft_path(graft_points, dir)
+        dirp = grafted.split('/')
     else:
         dirp = dir.split('/')
     while parts > dirp:
index cb378a97421cd53363b853e214eca51959b4314d..ae3b1cbd8064e9c927201e961f23c181397e3bad 100644 (file)
@@ -1,4 +1,5 @@
 """Helper functions and classes for bup."""
+
 import sys, os, pwd, subprocess, errno, socket, select, mmap, stat, re
 from bup import _version
 
@@ -433,6 +434,7 @@ def strip_path(prefix, path):
 def strip_base_path(path, base_paths):
     """Strips the base path from a given path.
 
+
     Determines the base path for the given string and the strips it
     using strip_path().
     Iterates over all base_paths from long to short, to prevent that
@@ -444,6 +446,14 @@ def strip_base_path(path, base_paths):
             return strip_path(bp, path)
     return path
 
+def graft_path(graft_points, path):
+    normalized_path = realpath(path)
+    for graft_point in graft_points:
+        old_prefix, new_prefix = graft_point
+        if normalized_path.startswith(old_prefix):
+            return re.sub(r'^' + old_prefix, new_prefix, normalized_path)
+    return normalized_path
+
 
 # hashlib is only available in python 2.5 or higher, but the 'sha' module
 # produces a DeprecationWarning in python 2.6 or higher.  We want to support
index 306d39b943118b3dc0698354fd00e518ac3b2a9b..17f6bcdbab2bb5e8b88ea7ee4a08203db3390d8b 100644 (file)
@@ -28,3 +28,26 @@ def test_strip_base_path():
     path = "/var/backup/daily.0/localhost/etc/"
     base_paths = ["/var", "/var/backup", "/var/backup/daily.0/localhost"]
     WVPASSEQ(strip_base_path(path, base_paths), '/etc')
+
+@wvtest
+def test_graft_path():
+    middle_matching_old_path = "/user"
+    non_matching_old_path = "/usr"
+    matching_old_path = "/home"
+    matching_full_path = "/home/user"
+    new_path = "/opt"
+
+    all_graft_points = [(middle_matching_old_path, new_path),
+                        (non_matching_old_path, new_path),
+                        (matching_old_path, new_path)]
+
+    path = "/home/user/"
+
+    WVPASSEQ(graft_path([(middle_matching_old_path, new_path)], path),
+                        "/home/user")
+    WVPASSEQ(graft_path([(non_matching_old_path, new_path)], path),
+                        "/home/user")
+    WVPASSEQ(graft_path([(matching_old_path, new_path)], path), "/opt/user")
+    WVPASSEQ(graft_path(all_graft_points, path), "/opt/user")
+    WVPASSEQ(graft_path([(matching_full_path, new_path)], path),
+                        "/opt")
index f9a633a3500582849ce6736a404bdd9105fe6fcc..4920308c805287e1288dafeb3a83888c4ef75443 100755 (executable)
--- a/t/test.sh
+++ b/t/test.sh
@@ -338,6 +338,28 @@ b
 d/
 f"
 
+WVSTART "graft_points"
+D=graft-points.tmp
+rm -rf $D
+mkdir $D
+export BUP_DIR="$D/.bup"
+WVPASS bup init
+touch $D/a
+WVPASS bup random 128k >$D/b
+mkdir $D/d $D/d/e
+WVPASS bup random 512 >$D/f
+WVPASS bup index -ux $D
+bup save --graft $TOP/$D=/grafted -n graft-point-absolute $D
+WVPASSEQ "$(bup ls graft-point-absolute/latest/grafted/)" "a
+b
+d/
+f"
+bup save --graft $D=grafted -n graft-point-relative $D
+WVPASSEQ "$(bup ls graft-point-relative/latest/$TOP/grafted/)" "a
+b
+d/
+f"
+
 WVSTART "indexfile"
 D=indexfile.tmp
 INDEXFILE=tmpindexfile.tmp