From 8cd637e198bb5cc604087fb80c1abd00ebd1a5c9 Mon Sep 17 00:00:00 2001 From: Bastian Kleineidam Date: Wed, 3 Mar 2010 21:08:57 +0100 Subject: [PATCH] Prevent overwriting the archive itself when extracting single-file archives. --- patoolib/programs/bzip2.py | 2 +- patoolib/programs/gzip.py | 2 +- patoolib/programs/lzma.py | 2 +- patoolib/programs/lzop.py | 6 ++---- patoolib/programs/uncompress.py | 2 +- patoolib/programs/xz.py | 2 +- patoolib/util.py | 9 +++++++++ 7 files changed, 16 insertions(+), 9 deletions(-) diff --git a/patoolib/programs/bzip2.py b/patoolib/programs/bzip2.py index 0bbf31c..6b10a14 100644 --- a/patoolib/programs/bzip2.py +++ b/patoolib/programs/bzip2.py @@ -24,7 +24,7 @@ def extract_bzip2 (archive, encoding, cmd, **kwargs): if kwargs['verbose']: cmdlist.append('-v') cmdlist.extend(['-c', '-d']) - outfile = os.path.join(kwargs['outdir'], util.stripext(archive)) + outfile = util.get_single_outfile(kwargs['outdir'], archive) cmdlist.append('--') cmdlist.extend([archive, '>', outfile]) # note that for shell calls the command must be a string diff --git a/patoolib/programs/gzip.py b/patoolib/programs/gzip.py index ff8fffa..129da2b 100644 --- a/patoolib/programs/gzip.py +++ b/patoolib/programs/gzip.py @@ -25,7 +25,7 @@ def extract_gzip (archive, encoding, cmd, **kwargs): cmdlist.append('-v') cmdlist.extend(['-c', '-d']) cmdlist.append('--') - outfile = os.path.join(kwargs['outdir'], util.stripext(archive)) + outfile = util.get_single_outfile(kwargs['outdir'], archive) cmdlist.extend([archive, '>', outfile]) # note that for shell calls the command must be a string cmd = " ".join([util.shell_quote(x) for x in cmdlist]) diff --git a/patoolib/programs/lzma.py b/patoolib/programs/lzma.py index c348704..bf82e03 100644 --- a/patoolib/programs/lzma.py +++ b/patoolib/programs/lzma.py @@ -25,7 +25,7 @@ def extract_lzma (archive, encoding, cmd, **kwargs): cmdlist.append('-v') cmdlist.extend(['-c', '-d']) cmdlist.append('--') - outfile = os.path.join(kwargs['outdir'], util.stripext(archive)) + outfile = util.get_single_outfile(kwargs['outdir'], archive) cmdlist.extend([archive, '>', outfile]) # note that for shell calls the command must be a string cmd = " ".join([util.shell_quote(x) for x in cmdlist]) diff --git a/patoolib/programs/lzop.py b/patoolib/programs/lzop.py index c0c96d3..62cb645 100644 --- a/patoolib/programs/lzop.py +++ b/patoolib/programs/lzop.py @@ -15,7 +15,7 @@ # along with this program. If not, see . """Archive commands for the lzop program.""" import os -from patoolib import util +from .. import util def extract_lzop (archive, encoding, cmd, **kwargs): @@ -25,9 +25,7 @@ def extract_lzop (archive, encoding, cmd, **kwargs): if kwargs['verbose']: cmdlist.append('--verbose') cmdlist.append('--') - outfile = os.path.join(kwargs['outdir'], util.stripext(archive)) - if archive == outfile: - outfile = archive + ".raw" + outfile = util.get_single_outfile(kwargs['outdir'], archive) cmdlist.extend([archive, '>', outfile]) # note that for shell calls the command must be a string cmd = " ".join([util.shell_quote(x) for x in cmdlist]) diff --git a/patoolib/programs/uncompress.py b/patoolib/programs/uncompress.py index 2e4c9a3..7832a93 100644 --- a/patoolib/programs/uncompress.py +++ b/patoolib/programs/uncompress.py @@ -24,7 +24,7 @@ def extract_compress (archive, encoding, cmd, **kwargs): if kwargs['verbose']: cmdlist.append('-v') cmdlist.append('-c') - outfile = os.path.join(kwargs['outdir'], util.stripext(archive)) + outfile = util.get_single_outfile(kwargs['outdir'], archive) cmdlist.extend([archive, '>', outfile]) # note that for shell calls the command must be a string cmd = " ".join([util.shell_quote(x) for x in cmdlist]) diff --git a/patoolib/programs/xz.py b/patoolib/programs/xz.py index ce8d36a..3706ca3 100644 --- a/patoolib/programs/xz.py +++ b/patoolib/programs/xz.py @@ -25,7 +25,7 @@ def extract_xz (archive, encoding, cmd, **kwargs): cmdlist.append('-v') cmdlist.extend(['-c', '-d']) cmdlist.append('--') - outfile = os.path.join(kwargs['outdir'], util.stripext(archive)) + outfile = util.get_single_outfile(kwargs['outdir'], archive) cmdlist.extend([archive, '>', outfile]) # note that for shell calls the command must be a string cmd = " ".join([util.shell_quote(x) for x in cmdlist]) diff --git a/patoolib/util.py b/patoolib/util.py index 06c4634..a9d5500 100644 --- a/patoolib/util.py +++ b/patoolib/util.py @@ -224,6 +224,15 @@ def stripext (filename): return os.path.splitext(os.path.basename(filename))[0] +def get_single_outfile (directory, archive): + """Get output filename if archive is in a single file format like gzip.""" + outfile = os.path.join(directory, stripext(archive)) + if archive == outfile: + # prevent overwriting the archive itself + outfile += ".raw" + return outfile + + def log_error (msg, out=sys.stderr): """Print error message to stderr (or any other given output).""" print >> out, "patool error:", msg