[X2Go-Commits] [cups-x2go] 01/01: cups-x2go{, .conf}: port to File::Temp. Use Text::ParseWords to split up the ps2pdf command line correctly. Don't use system() but IPC::Open2::open2(). Capture the ps2pdf program's stdout and write it to the temporary file handle "manually".

git-admin at x2go.org git-admin at x2go.org
Fri Mar 20 02:45:36 CET 2015


This is an automated email from the git hooks/post-receive script.

x2go pushed a commit to branch master
in repository cups-x2go.

commit 00eb58a247aee90a6f3105dded0d448850205fe6
Author: Mihai Moldovan <ionic at ionic.de>
Date:   Fri Mar 20 02:44:33 2015 +0100

    cups-x2go{,.conf}: port to File::Temp. Use Text::ParseWords to split up the ps2pdf command line correctly. Don't use system() but IPC::Open2::open2(). Capture the ps2pdf program's stdout and write it to the temporary file handle "manually".
    
    Should fix file problems reported by Jan Bi on IRC.
---
 cups-x2go        |   78 ++++++++++++++++++++++++++++++++++++++++++------------
 cups-x2go.conf   |    3 ++-
 debian/changelog |    6 +++++
 3 files changed, 69 insertions(+), 18 deletions(-)

diff --git a/cups-x2go b/cups-x2go
index 473d179..e6a0c32 100755
--- a/cups-x2go
+++ b/cups-x2go
@@ -22,6 +22,9 @@ use Sys::Syslog qw( :standard :macros );
 use Sys::Hostname;
 use File::Basename;
 use File::Copy;
+use File::Temp;
+use IPC::Open2;
+use Text::ParseWords;
 use strict;
 
 openlog($0,'cons,pid','user');
@@ -38,7 +41,7 @@ my $x2goserver = "local";
 my $printdsa = "/root/.ssh/id_dsa-x2goprint";
 
 # PS2PDF command
-my $ps2pdf = "/usr/bin/gs -q -dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -sOutputFile=\"%s.pdf\" -dAutoRotatePages=/PageByPage -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode -dPDFSETTINGS=/prepress -dDoNumCopies -c .setpdfwrite -f \"%s\"";
+my $ps2pdf = "/usr/bin/gs -q -dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -sOutputFile=- -dAutoRotatePages=/PageByPage -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode -dPDFSETTINGS=/prepress -dDoNumCopies -c .setpdfwrite -f \"%s\"";
 #my $ps2pdf = "/usr/bin/gs -q -dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -sOutputFile=\"%s.pdf\" -dAutoRotatePages=/PageByPage -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode -dPDFSETTINGS=/prepress -dDoNumCopies -c .setpdfwrite -f /usr/bin/margin-offset.ps \"%s\"";
 
 ## loglevel for cups-x2go, possible values: emerg, alert, crit, err, warning, notice, info, debug
@@ -189,41 +192,82 @@ my $psFile;
 ($jobID, $userName, $jobTitle, $copies, $printOptions, $psFile) =  @ARGV;
 syslog('notice', "Print job received from cups -> $jobID $userName $jobTitle $copies $printOptions $psFile");
 
-my $tempFile;
+my $jid = $jobID;
+my $uid = $userName;
+$jid =~ s/\W//g; #sanity check
+$uid =~ s/\W//g; #sanity check
+
+my $template = "$jid-$uid-cupsjob$$";
+my $ps_template = $template . "X" x 16;
+my $pdf_template = $template . ".pdfX" x 16;
+my $title_template = $template . ".pdf.titleX" x 16;
 if (!$psFile)
 {
-	my $jid = $jobID;
-	my $uid = $userName;
-	$jid =~ s/\W//g; #sanity check
-	$uid =~ s/\W//g; #sanity check
-	$tempFile = "/tmp/$jid-$uid-cupsjob$$";
-	open (OUT, ">$tempFile") or die "ERROR: Cannot write $tempFile: $!\n";
+	my ($tfh, $tempFile) = tempfile ($ps_template, TMPDIR => 1);
 	syslog('info', "Print job comes from STDIN, writing incoming job to temp file $tempFile\n");
 	while(<STDIN>)
 	{
-		print OUT "$_";
+		print $tfh $_;
 	}
-	close OUT;
+	close $tfh;
 
 	$psFile = $tempFile;
 }
 
 # converting PS file that we retrieved from CUPS into PDF format
-$ps2pdf=~s/%s/$psFile/g;
+my @ps2pdf_args = parse_line (" ", 0, $ps2pdf);
+my $ps2pdf_cmd = shift (@ps2pdf_args);
+
+for (my $i = 0; $i < @ps2pdf_args; ++$i) {
+	$ps2pdf_args[$i] =~ s/%s/$psFile/g;
+}
+
 syslog('info', "Converting printjob with command: $ps2pdf\n");
 
+my ($pdf_fh, $pdfFile) = tempfile ($pdf_template, TMPDIR => 1);
+
 # the TMPDIR env var is needed for ghostscript...
 $ENV{TMPDIR}="/tmp";
-system("$ps2pdf");
+my ($gs_out, $gs_in, $wait_ret, $real_ret);
+my $gs_pid = open2 ($gs_out, $gs_in, $ps2pdf_cmd, @ps2pdf_args);
+waitpid ($gs_pid, 0);
+
+$wait_ret = $?;
+$real_ret = $wait_ret >> 8;
+
+my $conv_fail = 0;
+if (-1 == $wait_ret) {
+	syslog('err', "ERROR: cups-x2go failed to spawn ps2pdf conversion process: $!");
+	$conv_fail = 1;
+}
+elsif ($wait_ret & 127) {
+	my $signal = $wait_ret & 127;
+	syslog('err', "ERROR: cups-x2go failed to execute ps2pdf conversion program: interrupted by signal $signal");
+	$conv_fail = 1;
+}
+else {
+	if (0 != $real_ret) {
+		syslog('err', "ERROR: ps2pdf conversion program failed to convert $psFile: failed with exit code$real_ret");
+		$conv_fail = 1;
+	}
+}
+
+if ($conv_fail) {
+	syslog('err', "ERROR: cups-x2go failed to process postscript file $psFile");
+	die "$0: Failed to convert postscript file $psFile";
+}
+
+syslog('debug', "cups-x2go processed postscript file $psFile to $pdfFile");
+
+print $pdf_fh $gs_out;
+close ($pdf_fh);
 
 # after we have created the PDF from CUPS's PS file, we can drop the PS file
 unlink ($psFile);
 
-my $pdfFile="$psFile.pdf";
-my $titleFile="$pdfFile.title";
-open (TITLE,">$titleFile");
-print TITLE $jobTitle;
-close (TITLE);
+my ($title_fh, $titleFile) = tempfile ($title_template, TMPDIR => 1);
+print $title_fh $jobTitle;
+close ($title_fh);
 
 getsessions();
 syslog('debug', "Retrieved session list: @sessions\n");
diff --git a/cups-x2go.conf b/cups-x2go.conf
index 89ed3ce..7a06843 100644
--- a/cups-x2go.conf
+++ b/cups-x2go.conf
@@ -14,7 +14,8 @@
 #printdsa = /root/.ssh/id_dsa-x2goprint
 
 ## command to generate PDF file
-#ps2pdf = /usr/bin/gs -q -dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -sOutputFile=\"%s.pdf\" -dAutoRotatePages=/PageByPage -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode -dPDFSETTINGS=/prepress -dDoNumCopies -c .setpdfwrite -f \"%s\"
+## CAUTION: this command MUST output the resulting PDF File to standard output.
+#ps2pdf = /usr/bin/gs -q -dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -sOutputFile=- -dAutoRotatePages=/PageByPage -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode -dPDFSETTINGS=/prepress -dDoNumCopies -c .setpdfwrite -f \"%s\"
 
 ## loglevel for cups-x2go, possible values: emerg, alert, crit, err, warning, notice, info, debug
 #loglevel = notice
diff --git a/debian/changelog b/debian/changelog
index bdfbe40..4da7667 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -22,6 +22,12 @@ cups-x2go (3.0.1.2-0x2go1) UNRELEASED; urgency=low
     + Also own %{_prefix}/lib for SLES < 12, as previous versions ship the
       arch-dependent %{_libdir}/cups only.
     + Make package noarch again.
+  * New upstream version (3.0.1.2):
+    - cups-x2go{,.conf}: port to File::Temp. Use Text::ParseWords to split up the
+      ps2pdf command line correctly. Don't use system() but
+      IPC::Open2::open2(). Capture the ps2pdf program's stdout and write it to
+      the temporary file handle "manually".
+      Should fix problems reported by Jan Bi on IRC.
 
  -- X2Go Release Manager <git-admin at x2go.org>  Tue, 10 Feb 2015 21:08:34 +0100
 

--
Alioth's /srv/git/code.x2go.org/cups-x2go.git//..//_hooks_/post-receive-email on /srv/git/code.x2go.org/cups-x2go.git


More information about the x2go-commits mailing list