Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bad lzma tar ball detection #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/cygapt/cygapt.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ def _doInstallExternal(self, ball):

def _doInstall(self):
ball = self.getBall();
if cautils.is_tarfile(ball):
if cautils.is_tarfile(ball, self.__dosXz):
if not self.__cygwinPlatform:
self._doInstallExternal(ball);
tf = cautils.open_tarfile(ball, self.__dosXz);
Expand Down Expand Up @@ -1199,7 +1199,7 @@ def _doUnpack(self):
));
return 1;
os.mkdir(self.__pkgName);
if cautils.is_tarfile(ball):
if cautils.is_tarfile(ball, self.__dosXz):
tf = cautils.open_tarfile(ball, self.__dosXz);
tf.extractall(self.__pkgName);
tf.close();
Expand Down
6 changes: 6 additions & 0 deletions src/cygapt/test/fixtures/utils/README
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ cyglsa-bad1.dll : MZ header changed to ZM
cyglsa-bad2.dll : PE\0\0 header changed to EP\0\0
cyglsa-bad3.dll : machine value changed from 0x014C to 0xDEAD
cyglsa-bad4.dll : truncated before PE header

empty.tar.xz : empty file
emptytar.tar.xz : empty file with lzma compression
valid.tar.xz : empty file in a tarball with lzma compression
badtar.tar.xz : corrupted tarball with lzma compression
badlzma.tar.xz : corrupted lzma compression
Binary file added src/cygapt/test/fixtures/utils/badlzma.tar.xz
Binary file not shown.
Binary file added src/cygapt/test/fixtures/utils/badtar.tar.xz
Binary file not shown.
Empty file.
Binary file added src/cygapt/test/fixtures/utils/emptytar.tar.xz
Binary file not shown.
Binary file added src/cygapt/test/fixtures/utils/valid.tar.xz
Binary file not shown.
29 changes: 29 additions & 0 deletions src/cygapt/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,35 @@ def _successOpenTarfile(self, pkgname, xzPath="xz"):

self.assertEqual(sorted(filelist), sorted(self._var_setupIni.__dict__[pkgname].filelist));

@dataProvider('getIsTarfileData')
def testIsTarfile(self, filename, expected):
fn = os.path.join(__DIR__, 'fixtures', 'utils', filename);
self.assertTrue(utils.is_tarfile(fn) is expected);
self.assertFalse(os.path.exists(os.path.splitext(fn)[0]));

def getIsTarfileData(self):
return [
['badtar.tar.xz', False],
['empty.tar.xz', False],
['valid.tar.xz', True],
['emptytar.tar.xz', True],
];

@dataProvider('getIsTarfileData')
def testIsTarfileWithoutPATH(self, filename, expected):
if not sys.platform.startswith("cygwin") and not sys.platform.startswith("linux") :
self.skipTest("requires cygwin or linux");

fn = os.path.join(__DIR__, 'fixtures', 'utils', filename);
old_path = os.environ['PATH'];

try:
os.environ['PATH'] = "";
self.assertTrue(utils.is_tarfile(fn, '/usr/bin/xz') is expected);
self.assertFalse(os.path.exists(os.path.splitext(fn)[0]));
finally:
os.environ['PATH'] = old_path;

@dataProvider('getPEArchitectureData')
def testPEArchitecture(self, filename, expected):
fn = os.path.join(__DIR__, 'fixtures', 'utils', filename);
Expand Down
24 changes: 21 additions & 3 deletions src/cygapt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from cygapt.url_opener import CygAptURLopener;
from cygapt.structure import ConfigStructure;
from cygapt.process import Process;
from cygapt.process.exception import ProcessFailedException;

def cygpath(path):
p = Process(["cygpath", path]);
Expand Down Expand Up @@ -97,7 +98,18 @@ def open_tarfile(ball, xzPath='xz'):
ball = ball[:-3]; # remove .xz extension
remove_if_exists(ball);
Process([xzPath, '-k', '-d', ball_orig]).mustRun();
tf = tarfile.open(ball);
# an empty tarball is valid
if 0 == os.path.getsize(ball) :
with tarfile.open(ball, 'w'):
pass;
try:
tf = tarfile.open(ball);
except tarfile.TarError:
if ball_orig != ball:
remove_if_exists(ball);

raise;

if ball_orig != ball:
tf_close_orig = tf.close;
def tf_close():
Expand All @@ -107,8 +119,14 @@ def tf_close():
tf.close = tf_close;
return tf;

def is_tarfile(ball):
return ball.lower().endswith('.tar.xz') or tarfile.is_tarfile(ball);
def is_tarfile(ball, xzPath='xz'):
try:
open_tarfile(ball, xzPath).close();
return True;
except tarfile.TarError:
return False;
except ProcessFailedException:
return False;

def prsort(lst):
lst.sort();
Expand Down