From 155c7ba5de0390b6eedca50c70e0498f09d9c218 Mon Sep 17 00:00:00 2001
From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Date: Fri, 20 Mar 2015 16:13:51 +0100
Subject: [PATCH] Bug 13876: [QA Follow-up] Resolve updatedatabase logfile
 issues
Content-Type: text/plain; charset=utf-8

This patch removes the test on write permissions of the logdir.
It uses File::Temp to generate a file in the logdir or otherwise in
a system tmp dir.
This resolves issues with both write permissions as well as SELinux
denials.
Note that after generating an empty file, we are appending to it in
the system statement.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Amended: only use File::Temp in the fallback case. Do not add the four
character suffix in the first attempt.

Signed-off-by: Chris Nighswonger <cnighswonger@foundations.edu>
---
 installer/install.pl |   20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/installer/install.pl b/installer/install.pl
index 63094ab..e43caa0 100755
--- a/installer/install.pl
+++ b/installer/install.pl
@@ -7,7 +7,6 @@ use diagnostics;
 use C4::InstallAuth;
 use CGI qw ( -utf8 );
 use POSIX qw(strftime);
-use File::Temp qw( tempdir );
 
 use C4::Context;
 use C4::Output;
@@ -323,12 +322,9 @@ elsif ( $step && $step == 3 ) {
 
         my $now = POSIX::strftime( "%Y-%m-%dT%H:%M:%S", localtime() );
         my $logdir = C4::Context->config('logdir');
-        unless ( -w $logdir ) {
-            $logdir = tempdir;
-        }
-        my ( $logfilepath, $logfilepath_errors ) = ( $logdir . "/updatedatabase_$now.log", $logdir . "/updatedatabase-error_$now.log" );
+        my ( $logfilepath, $logfilepath_errors ) = ( chk_log($logdir, "updatedatabase_$now"), chk_log($logdir, "updatedatabase-error_$now") );
 
-        my $cmd = C4::Context->config("intranetdir") . "/installer/data/$info{dbms}/updatedatabase.pl > $logfilepath 2> $logfilepath_errors";
+        my $cmd = C4::Context->config("intranetdir") . "/installer/data/$info{dbms}/updatedatabase.pl >> $logfilepath 2>> $logfilepath_errors";
 
         system($cmd );
 
@@ -423,3 +419,15 @@ else {
     }
 }
 output_html_with_http_headers $query, $cookie, $template->output;
+
+sub chk_log { #returns a logfile in $dir or - if that failed - in temp dir
+    my ($dir, $name) = @_;
+    my $fn=$dir.'/'.$name.'.log';
+    if( ! open my $fh, '>', $fn ) {
+        $name.= '_XXXX';
+        require File::Temp;
+        ($fh, $fn)= File::Temp::tempfile( $name, TMPDIR => 1, SUFFIX => '.log');
+        #if this should not work, let croak take over
+    }
+    return $fn;
+}
-- 
1.7.10.4