From e188d0fffa60e20114fbbedb58f9e9a5e65778fd Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sat, 12 Oct 2019 19:25:09 -0400 Subject: Make --replace-input work with / in path (fixes #365) --- ChangeLog | 6 ++++++ manual/qpdf-manual.xml | 23 ++++++++++++++++++++++- qpdf/qpdf.cc | 22 +++++++--------------- qpdf/qtest/qpdf.test | 4 ++-- qpdf/qtest/qpdf/replace-warn.out | 4 ++-- 5 files changed, 39 insertions(+), 20 deletions(-) diff --git a/ChangeLog b/ChangeLog index c16052b9..d10a8997 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2019-10-12 Jay Berkenbilt + + * Change the name of the temporary file used by --replace-input to + work with arbitrary absolute or relative paths without requiring + path splitting logic. Fixes #365. + 2019-09-20 Jay Berkenbilt * 9.0.1: release diff --git a/manual/qpdf-manual.xml b/manual/qpdf-manual.xml index edaa15d1..a71f79ca 100644 --- a/manual/qpdf-manual.xml +++ b/manual/qpdf-manual.xml @@ -458,7 +458,7 @@ make If specified, the output file name should be omitted. This option tells qpdf to replace the input file with the output. It does this by writing to - .~qpdf-temp.infilename# + infilename.~qpdf-temp# and, when done, overwriting the input file with the temporary file. If there were any warnings, the original input is saved as @@ -4364,6 +4364,27 @@ print "\n"; ChangeLog in the source distribution. + + 9.0.2: XXX + + + + + Bug Fix + + + + + Fix the name of the temporary file used by + so that it doesn't require + path splitting and works with paths include directories. + + + + + + + 9.0.1: September 20, 2019 diff --git a/qpdf/qpdf.cc b/qpdf/qpdf.cc index dd1cd0d4..b7ef8cc7 100644 --- a/qpdf/qpdf.cc +++ b/qpdf/qpdf.cc @@ -5148,11 +5148,10 @@ static void write_outfile(QPDF& pdf, Options& o) std::string temp_out; if (o.replace_input) { - // Use a file name that is hidden by default in the OS to - // avoid having it become momentarily visible in a - // graphical file manager or in case it gets left behind - // because of some kind of error. - temp_out = ".~qpdf-temp." + std::string(o.infilename) + "#"; + // Append but don't prepend to the path to generate a + // temporary name. This saves us from having to split the path + // by directory and non-directory. + temp_out = std::string(o.infilename) + ".~qpdf-temp#"; // o.outfilename will be restored to 0 before temp_out // goes out of scope. o.outfilename = temp_out.c_str(); @@ -5180,18 +5179,11 @@ static void write_outfile(QPDF& pdf, Options& o) { // We must close the input before we can rename files pdf.closeInputSource(); - std::string backup; + std::string backup = std::string(o.infilename) + ".~qpdf-orig"; bool warnings = pdf.anyWarnings(); - if (warnings) - { - // If there are warnings, the user may care about this - // file, so give it a non-hidden name that will be - // lexically grouped with the original file. - backup = std::string(o.infilename) + ".~qpdf-orig"; - } - else + if (! warnings) { - backup = ".~qpdf-orig." + std::string(o.infilename) + "#"; + backup.append(1, '#'); } QUtil::rename_file(o.infilename, backup.c_str()); QUtil::rename_file(temp_out.c_str(), o.infilename); diff --git a/qpdf/qtest/qpdf.test b/qpdf/qtest/qpdf.test index 683700a0..6c3ea8ef 100644 --- a/qpdf/qtest/qpdf.test +++ b/qpdf/qtest/qpdf.test @@ -207,7 +207,7 @@ foreach my $d (['auto-ü', 1], ['auto-öπ', 2]) my ($u, $n) = @$d; $td->runtest("replace input $u", {$td->COMMAND => "qpdf --deterministic-id" . - " --object-streams=generate --replace-input $u.pdf"}, + " --object-streams=generate --replace-input ./$u.pdf"}, {$td->STRING => "", $td->EXIT_STATUS => 0}, $td->NORMALIZE_NEWLINES); $td->runtest("check output ($u)", @@ -219,7 +219,7 @@ foreach my $d (['auto-ü', 1], ['auto-öπ', 2]) system("cp xref-with-short-size.pdf auto-warn.pdf") == 0 or die; $td->runtest("replace input with warnings", {$td->COMMAND => - "qpdf --deterministic-id --replace-input auto-warn.pdf"}, + "qpdf --deterministic-id --replace-input ./auto-warn.pdf"}, {$td->FILE => "replace-warn.out", $td->EXIT_STATUS => 3}, $td->NORMALIZE_NEWLINES); diff --git a/qpdf/qtest/qpdf/replace-warn.out b/qpdf/qtest/qpdf/replace-warn.out index 09a9261a..320ad4df 100644 --- a/qpdf/qtest/qpdf/replace-warn.out +++ b/qpdf/qtest/qpdf/replace-warn.out @@ -1,3 +1,3 @@ -WARNING: auto-warn.pdf (xref stream, offset 16227): Cross-reference stream data has the wrong size; expected = 52; actual = 56 -qpdf: there are warnings; original file kept in auto-warn.pdf.~qpdf-orig +WARNING: ./auto-warn.pdf (xref stream, offset 16227): Cross-reference stream data has the wrong size; expected = 52; actual = 56 +qpdf: there are warnings; original file kept in ./auto-warn.pdf.~qpdf-orig qpdf: operation succeeded with warnings; resulting file may have some problems -- cgit v1.2.3-70-g09d2