Make --replace-input work with / in path (fixes #365)

This commit is contained in:
Jay Berkenbilt 2019-10-12 19:25:09 -04:00
parent bcfb0c7d9a
commit e188d0fffa
5 changed files with 39 additions and 20 deletions

View File

@ -1,3 +1,9 @@
2019-10-12 Jay Berkenbilt <ejb@ql.org>
* 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 <ejb@ql.org>
* 9.0.1: release

View File

@ -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
<filename>.~qpdf-temp.<replaceable>infilename</replaceable>#</filename>
<filename><replaceable>infilename</replaceable>.~qpdf-temp#</filename>
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";
<filename>ChangeLog</filename> in the source distribution.
</para>
<variablelist>
<varlistentry>
<term>9.0.2: XXX</term>
<listitem>
<itemizedlist>
<listitem>
<para>
Bug Fix
</para>
<itemizedlist>
<listitem>
<para>
Fix the name of the temporary file used by
<option>--replace-input</option> so that it doesn't require
path splitting and works with paths include directories.
</para>
</listitem>
</itemizedlist>
</listitem>
</itemizedlist>
</listitem>
</varlistentry>
<varlistentry>
<term>9.0.1: September 20, 2019</term>
<listitem>

View File

@ -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 (! 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
{
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);

View File

@ -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);

View File

@ -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