From 2e36fd1128a5d096bf7ee8f70a72581be31fc93d Mon Sep 17 00:00:00 2001 From: "Andrey Jr. Melnikov" Date: Tue, 25 Feb 2020 01:42:22 +0300 Subject: [PATCH] Small cleanup - reuse existing code for copying file, count pdf pages. - remove temp file after processing. Fixes: 37b7d6467dcd ("foomatic-rip: correct handling of jobs") Signed-off-by: Andrey Jr. Melnikov --- filter/foomatic-rip/foomaticrip.c | 92 +++++++++++++------------------ 1 file changed, 37 insertions(+), 55 deletions(-) diff --git a/filter/foomatic-rip/foomaticrip.c b/filter/foomatic-rip/foomaticrip.c index ac248c026..70f5b7d25 100644 --- a/filter/foomatic-rip/foomaticrip.c +++ b/filter/foomatic-rip/foomaticrip.c @@ -560,8 +560,10 @@ int print_file(const char *filename, int convert) { FILE *file; char buf[8192]; + char tmpfilename[PATH_MAX]; int type; int startpos; + int pagecount; size_t n; int ret; @@ -603,7 +605,6 @@ int print_file(const char *filename, int convert) char pdf2ps_cmd[CMDLINE_MAX]; FILE *out, *in; int renderer_pid; - char tmpfilename[PATH_MAX] = ""; _log("Driver does not understand PDF input, " "converting to PostScript\n"); @@ -615,7 +616,7 @@ int print_file(const char *filename, int convert) { int fd; FILE *tmpfile; - + snprintf(tmpfilename, PATH_MAX, "%s/foomatic-XXXXXX", temp_dir()); fd = mkstemp(tmpfilename); if (fd < 0) { @@ -625,7 +626,7 @@ int print_file(const char *filename, int convert) tmpfile = fdopen(fd, "r+"); copy_file(tmpfile, stdin, buf, n); fclose(tmpfile); - + filename = tmpfilename; } @@ -668,7 +669,7 @@ int print_file(const char *filename, int convert) "Couldn't dup stdout of pdf-to-ps\n"); clearerr(stdin); - int pagecount = pdf_count_pages(filename); + pagecount = pdf_count_pages(filename); _log("File contains %d pages.\n", pagecount); if (pagecount < 0) { _log("Unexpected page_count\n"); @@ -700,61 +701,42 @@ int print_file(const char *filename, int convert) { if (convert) { - int fd; - FILE *tmpfile; - char tmpfilename[4096]; - - snprintf(tmpfilename, PATH_MAX, "%s/foomatic-XXXXXX", temp_dir()); - fd = mkstemp(tmpfilename); - if (fd < 0) { - _log("Could not create temporary file: %s\n", strerror(errno)); - return EXIT_PRNERR_NORETRY_BAD_SETTINGS; - } + int fd; + FILE *tmpfile; - if (write(fd,buf,n) != n) { - _log("ERROR: Can't copy stdin to temporary file\n"); - close(fd); - } - /* copy stdin to the tmp file */ - while ((n = read(0,buf,BUFSIZ)) > 0) { - if (write(fd,buf,n) != n) { - _log("ERROR: Can't copy stdin to temporary file\n"); - close(fd); - } - } - if (lseek(fd,0,SEEK_SET) < 0) { - _log("ERROR: Can't rewind temporary file\n"); - close(fd); - } - FILE *fp; + snprintf(tmpfilename, PATH_MAX, "%s/foomatic-XXXXXX", temp_dir()); + fd = mkstemp(tmpfilename); + if (fd < 0) { + _log("Could not create temporary file: %s\n", strerror(errno)); + return EXIT_PRNERR_NORETRY_BAD_SETTINGS; + } + tmpfile = fdopen(fd, "r+"); + copy_file(tmpfile, stdin, buf, n); + fclose(tmpfile); - if ((fp = fdopen(fd,"rb")) == 0) { - _log("ERROR: Can't fdopen temporary file\n"); - close(fd); - } - char gscommand[65536]; - char output[31] = ""; - int pagecount; - size_t bytes; - filename = strdup(tmpfilename); - snprintf(gscommand, 65536, "%s -q -dNOPAUSE -dBATCH -sDEVICE=bbox %s 2>&1 | grep -c HiResBoundingBox", - CUPS_GHOSTSCRIPT, filename); - FILE *pd = popen(gscommand, "r"); - bytes = fread(output, 1, 31, pd); - pclose(pd); - - if (bytes <= 0 || sscanf(output, "%d", &pagecount) < 1) - pagecount = -1; - _log("File contains %d pages.\n", pagecount); - if (pagecount < 0) { - _log("Unexpected page_count\n"); - return 0; - } - if (pagecount == 0) { - _log("No pages left, outputting empty file.\n"); + filename = tmpfilename; + + pagecount = pdf_count_pages(filename); + + if (pagecount < 0) { + _log("Unexpected page_count\n"); + unlink(tmpfilename); + return 1; + } + + if (pagecount == 0) { + _log("No pages left, outputting empty file.\n"); + unlink(tmpfilename); return 1; } - return print_ps(fp, NULL, 0, filename); + + _log("File contains %d pages.\n", pagecount); + + tmpfile = fopen(tmpfilename, "rb"); + ret = print_ps(tmpfile, NULL, 0, filename); + fclose(tmpfile); + unlink(tmpfilename); + return ret; } else return print_ps(stdin, buf, n, filename); -- 2.47.2