Skip to content
Browse files

Merge branch 'zz/strip_path_fix'

* zz/strip_path_fix:
  Fix a bug in strip_path when prefix is a symlink
  Add a testcase for strip_path
  • Loading branch information...
2 parents 0ee1a48 + c1db01d commit 6b4311b19b40beb7b760ae0176c085797f92ce35 @apenwarr apenwarr committed Jan 8, 2011
Showing with 28 additions and 4 deletions.
  1. +5 −4 lib/bup/helpers.py
  2. +23 −0 lib/bup/t/thelpers.py
View
9 lib/bup/helpers.py
@@ -421,9 +421,9 @@ def strip_path(prefix, path):
if prefix == None:
raise Exception('no path given')
- normalized_prefix = realpath(prefix)
+ normalized_prefix = os.path.realpath(prefix)
debug2("normalized_prefix: %s\n" % normalized_prefix)
- normalized_path = realpath(path)
+ normalized_path = os.path.realpath(path)
debug2("normalized_path: %s\n" % normalized_path)
if normalized_path.startswith(normalized_prefix):
return normalized_path[len(normalized_prefix):]
@@ -438,10 +438,11 @@ def strip_base_path(path, base_paths):
Iterates over all base_paths from long to short, to prevent that
a too short base_path is removed.
"""
+ normalized_path = os.path.realpath(path)
sorted_base_paths = sorted(base_paths, key=len, reverse=True)
for bp in sorted_base_paths:
- if path.startswith(realpath(bp)):
- return strip_path(bp, path)
+ if normalized_path.startswith(os.path.realpath(bp)):
+ return strip_path(bp, normalized_path)
return path
View
23 lib/bup/t/thelpers.py
@@ -1,3 +1,4 @@
+import os
from bup.helpers import *
from wvtest import *
@@ -28,3 +29,25 @@ 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_strip_symlinked_base_path():
+ tmpdir = os.path.join(os.getcwd(),"test_strip_symlinked_base_path.tmp")
+ symlink_src = os.path.join(tmpdir, "private", "var")
+ symlink_dst = os.path.join(tmpdir, "var")
+ path = os.path.join(symlink_dst, "a")
+
+ os.mkdir(tmpdir)
+ os.mkdir(os.path.join(tmpdir, "private"))
+ os.mkdir(symlink_src)
+ os.symlink(symlink_src, symlink_dst)
+
+ result = strip_base_path(path, [symlink_dst])
+
+ os.remove(symlink_dst)
+ os.rmdir(symlink_src)
+ os.rmdir(os.path.join(tmpdir, "private"))
+ os.rmdir(tmpdir)
+
+ WVPASSEQ(result, "/a")
+

0 comments on commit 6b4311b

Please sign in to comment.
Something went wrong with that request. Please try again.