andykimpe / rpms / 389-ds-base

Forked from rpms/389-ds-base 5 months ago
Clone

Blame SOURCES/0022-Ticket-48224-redux-logconv.pl-should-handle-.tar.xz-.patch

a2f18f
From abfd367015c9d0dfa0b97b8473923c97eab0dab5 Mon Sep 17 00:00:00 2001
a2f18f
From: Rich Megginson <rmeggins@redhat.com>
a2f18f
Date: Wed, 15 Jul 2015 14:09:24 -0600
a2f18f
Subject: [PATCH 22/22] Ticket #48224 - redux - logconv.pl should handle
a2f18f
 *.tar.xz, *.txz, *.xz log files
a2f18f
a2f18f
https://fedorahosted.org/389/ticket/48224
a2f18f
Reviewed by: nhosoi, mreynolds (Thanks!)
a2f18f
Branch: 389-ds-base-1.3.4
a2f18f
Fix Description: Some platforms have no IO::Uncompress::UnXz, so have
a2f18f
to pipe out to the 'xz' command for uncompression.
a2f18f
Doing the 'xz' pipe will not work with compressed xz files in tar
a2f18f
archives, so issue an appropriate error.
a2f18f
The tar archive file handling was wrong - have to wrap the data in a
a2f18f
filehandle before passing to uncompress.
a2f18f
Added a lot of error checking - trying to uncompress plain text files,
a2f18f
trying to untar non-tar archives, trying to untar and uncompress a
a2f18f
tar file that is not compressed, other weird stuff like specifying a
a2f18f
.bz2 extension on a file compressed with .xz.
a2f18f
This will also need a spec file change:
a2f18f
Requires: perl-IO-Compress
a2f18f
Requires: perl-DB_File
a2f18f
Requires: perl-Archive-Tar
a2f18f
Requires: xz
a2f18f
Platforms tested: Fedora 21, RHEL 7.2 candidate
a2f18f
Flag Day: no
a2f18f
Doc impact: no
a2f18f
a2f18f
(cherry picked from commit ae5b62f53557c8ce2d174999c4b561ebc4ccde55)
a2f18f
(cherry picked from commit 8473ae0c49492dd7931dbdd3a8377119f53ce49b)
a2f18f
---
a2f18f
 ldap/admin/src/logconv.pl | 73 ++++++++++++++++++++++++++++++++++++-----------
a2f18f
 1 file changed, 57 insertions(+), 16 deletions(-)
a2f18f
a2f18f
diff --git a/ldap/admin/src/logconv.pl b/ldap/admin/src/logconv.pl
a2f18f
index a6bd6c2..d26e91e 100755
a2f18f
--- a/ldap/admin/src/logconv.pl
a2f18f
+++ b/ldap/admin/src/logconv.pl
a2f18f
@@ -398,17 +398,11 @@ my $totalLineCount = 0;
a2f18f
 
a2f18f
 sub isTarArchive {
a2f18f
 	local $_ = shift;
a2f18f
-	if (/\.txz$/ || /\.tar.xz$/) {
a2f18f
-		use IO::Uncompress::UnXz;
a2f18f
-	}
a2f18f
 	return /\.tar$/ || /\.tar\.bz2$/ || /\.tar.gz$/ || /\.tar.xz$/ || /\.tgz$/ || /\.tbz$/ || /\.txz$/;
a2f18f
 }
a2f18f
 
a2f18f
 sub isCompressed {
a2f18f
 	local $_ = shift;
a2f18f
-	if (/\.xz$/) {
a2f18f
-		use IO::Uncompress::UnXz;
a2f18f
-	}
a2f18f
 	return /\.gz$/ || /\.bz2$/ || /\.xz$/;
a2f18f
 }
a2f18f
 
a2f18f
@@ -418,6 +412,43 @@ sub tarNeedsUncompress {
a2f18f
 	return /\.tar.xz$/ || /\.txz$/;
a2f18f
 }
a2f18f
 
a2f18f
+# rhel7 can't grok xz
a2f18f
+sub doUncompress {
a2f18f
+	local $_ = shift;
a2f18f
+	my $data = shift;
a2f18f
+	my $TARFH;
a2f18f
+	# some platforms don't have xz support in IO::Uncompress::AnyUncompress
a2f18f
+	if (/\.tar.xz$/ || /\.txz$/ || /\.xz$/) {
a2f18f
+		if ($data) {
a2f18f
+			openFailed("Cannot read from compressed xz file in tar archive.\nPlease un-tar the tar file first, then pass individual .xz files to this program.\n", $_);
a2f18f
+		}
a2f18f
+		# so use the xz command directly
a2f18f
+		# NOTE: This doesn't work if the argument is a file handle e.g. from
a2f18f
+		# Archive::Tar
a2f18f
+		$! = 0; # clear
a2f18f
+		if (!open($TARFH, "xz -dc $_ |") or $!) {
a2f18f
+			openFailed($!, $_);
a2f18f
+			return;
a2f18f
+		}
a2f18f
+	} else {
a2f18f
+		my $uncompressthing;
a2f18f
+		if ($data) {
a2f18f
+			# make a filehandle object from data
a2f18f
+			open($uncompressthing, "<", \$data) or openFailed($!, $_);
a2f18f
+		} else {
a2f18f
+			# just read from the file
a2f18f
+			$uncompressthing = $_;
a2f18f
+		}
a2f18f
+		$TARFH = new IO::Uncompress::AnyUncompress $uncompressthing or
a2f18f
+			do { openFailed($AnyUncompressError, $_); return; };
a2f18f
+		if (*$TARFH->{Plain}) {
a2f18f
+			openFailed("Unknown compression", $_);
a2f18f
+			return;
a2f18f
+		}
a2f18f
+	}
a2f18f
+	return $TARFH;
a2f18f
+}
a2f18f
+
a2f18f
 sub convertTimeToSeconds {
a2f18f
 	my $log_line = shift;
a2f18f
 
a2f18f
@@ -497,6 +528,10 @@ for (my $count=0; $count < $file_count; $count++){
a2f18f
 	if($logCount > 1 && $count == 0 && $skipFirstFile == 1){
a2f18f
 		next;
a2f18f
 	}
a2f18f
+	if (! -r $logname) {
a2f18f
+		print "File not found: $logname\n";
a2f18f
+		next;
a2f18f
+	}
a2f18f
 	$linesProcessed = 0; $lineBlockCount = 0;
a2f18f
 	my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$atime,$mtime,$ctime,$blksize,$blocks);
a2f18f
 	($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$cursize,
a2f18f
@@ -513,11 +548,12 @@ for (my $count=0; $count < $file_count; $count++){
a2f18f
 	my $tariter = 0;
a2f18f
 	my $tarfile = 0;
a2f18f
 	my $comp = 0;
a2f18f
+	$LOGFH = undef;
a2f18f
 	if (isTarArchive($logname)) {
a2f18f
 		$tar = Archive::Tar->new();
a2f18f
 		if (tarNeedsUncompress($logname)) {
a2f18f
-			my $TARFH = new IO::Uncompress::AnyUncompress $logname or
a2f18f
-				do { openFailed($AnyUncompressError, $logname); next };
a2f18f
+			my $TARFH = doUncompress($logname);
a2f18f
+			next if (!$TARFH);
a2f18f
 			$tariter = Archive::Tar->iter($TARFH);
a2f18f
 		} else {
a2f18f
 			$tariter = Archive::Tar->iter($logname);
a2f18f
@@ -540,24 +576,21 @@ for (my $count=0; $count < $file_count; $count++){
a2f18f
 				next;
a2f18f
 			}
a2f18f
 			if (isCompressed($tarfile->name)) {
a2f18f
-				$LOGFH = new IO::Uncompress::AnyUncompress \$tarfile->name or
a2f18f
-					do { openFailed($AnyUncompressError, $logname); next };
a2f18f
+				$LOGFH = doUncompress($tarfile->name, $tarfile->get_content);
a2f18f
+				next if (!$LOGFH);
a2f18f
 				# no way in general to know how big the uncompressed file is - so
a2f18f
 				# assume a factor of 10 inflation - only used for progress reporting
a2f18f
 				$cursize *= 10;
a2f18f
 			} else {
a2f18f
-				open(LOG,"<",\$tarfile->data) or do { openFailed($!, $tarfile->name) ; next };
a2f18f
-				$LOGFH = \*LOG;
a2f18f
+				open($LOGFH,"<",\$tarfile->data) or do { openFailed($!, $tarfile->name) ; next };
a2f18f
 			}
a2f18f
 		} elsif ($comp) {
a2f18f
-			$LOGFH = new IO::Uncompress::AnyUncompress $logname or
a2f18f
-				do { openFailed($AnyUncompressError, $logname); next };
a2f18f
+			$LOGFH = doUncompress($logname);
a2f18f
 			# no way in general to know how big the uncompressed file is - so
a2f18f
 			# assume a factor of 10 inflation - only used for progress reporting
a2f18f
 			$cursize *= 10;
a2f18f
 		} else {
a2f18f
-			open(LOG,$logname) or do { openFailed($!, $logname); next };
a2f18f
-			$LOGFH = \*LOG;
a2f18f
+			open($LOGFH,$logname) or do { openFailed($!, $logname); next };
a2f18f
 		}
a2f18f
 		my $firstline = "yes";
a2f18f
 		while(<$LOGFH>){
a2f18f
@@ -588,6 +621,14 @@ for (my $count=0; $count < $file_count; $count++){
a2f18f
 		}
a2f18f
 		last if (!$tariter);
a2f18f
 	}
a2f18f
+	if ($tar) {
a2f18f
+		if ($tar->error()) {
a2f18f
+			openFailed($tar->error(), $logname);
a2f18f
+		}
a2f18f
+		if ($Archive::Tar::error) {
a2f18f
+			openFailed($Archive::Tar::error, $logname);
a2f18f
+		}
a2f18f
+	}
a2f18f
 }
a2f18f
 
a2f18f
 if ($totalLineCount eq "0"){
a2f18f
-- 
a2f18f
1.9.3
a2f18f