Fix archive truncating archived file names based on prefix length (#3124)

When archiving multiple files, the full length of the calculated
`arcroot` would be removed from the beginning of all file names. If
there was no arcroot, the first two characters of all files would be
removed, so `file.txt` would become `le.txt`.

This patch uses the regular expressions substitution anchored to the
start of the string, so the arcroot is only removed if it is actually
present.
This commit is contained in:
Ryan Brown 2016-10-14 12:19:36 -04:00 committed by Matt Clay
parent 25da992785
commit bbe8e1f53b

View file

@ -245,6 +245,7 @@ def main():
elif format == 'tar': elif format == 'tar':
arcfile = tarfile.open(dest, 'w') arcfile = tarfile.open(dest, 'w')
match_root = re.compile('^%s' % re.escape(arcroot))
for path in archive_paths: for path in archive_paths:
if os.path.isdir(path): if os.path.isdir(path):
# Recurse into directories # Recurse into directories
@ -254,7 +255,7 @@ def main():
for dirname in dirnames: for dirname in dirnames:
fullpath = dirpath + dirname fullpath = dirpath + dirname
arcname = fullpath[len(arcroot):] arcname = match_root.sub('', fullpath)
try: try:
if format == 'zip': if format == 'zip':
@ -268,7 +269,7 @@ def main():
for filename in filenames: for filename in filenames:
fullpath = dirpath + filename fullpath = dirpath + filename
arcname = fullpath[len(arcroot):] arcname = match_root.sub('', fullpath)
if not filecmp.cmp(fullpath, dest): if not filecmp.cmp(fullpath, dest):
try: try:
@ -283,9 +284,9 @@ def main():
errors.append('Adding %s: %s' % (path, str(e))) errors.append('Adding %s: %s' % (path, str(e)))
else: else:
if format == 'zip': if format == 'zip':
arcfile.write(path, path[len(arcroot):]) arcfile.write(path, match_root.sub('', path))
else: else:
arcfile.add(path, path[len(arcroot):], recursive=False) arcfile.add(path, match_root.sub('', path), recursive=False)
successes.append(path) successes.append(path)