1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-07-17 01:12:23 +02:00

Allow empty files to be created with FileCommon::fileStringWrite() and use temp files to avoid partial reads.

This commit is contained in:
David Steele
2017-01-27 10:04:41 -05:00
parent 2553c7a998
commit 0163d3b3e0
2 changed files with 22 additions and 12 deletions

View File

@ -252,6 +252,10 @@
<release-item> <release-item>
<p>Represent <file>.gz</file> extension with a constant.</p> <p>Represent <file>.gz</file> extension with a constant.</p>
</release-item> </release-item>
<release-item>
<p>Allow empty files to be created with <code>FileCommon::fileStringWrite()</code> and use temp files to avoid partial reads.</p>
</release-item>
</release-refactor-list> </release-refactor-list>
</release-core-list> </release-core-list>

View File

@ -19,6 +19,7 @@ use IO::Handle;
use pgBackRest::Common::Exception; use pgBackRest::Common::Exception;
use pgBackRest::Common::Log; use pgBackRest::Common::Log;
use pgBackRest::Version;
#################################################################################################################################### ####################################################################################################################################
# Default modes # Default modes
@ -874,7 +875,7 @@ push @EXPORT, qw(fileStringRead);
#################################################################################################################################### ####################################################################################################################################
# fileStringWrite # fileStringWrite
# #
# Write a string to the specified file. # Create/overwrite a file with a string.
#################################################################################################################################### ####################################################################################################################################
sub fileStringWrite sub fileStringWrite
{ {
@ -884,23 +885,28 @@ sub fileStringWrite
$strOperation, $strOperation,
$strFileName, $strFileName,
$strContent, $strContent,
$bSync $bSync,
) = ) =
logDebugParam logDebugParam
( (
__PACKAGE__ . '::fileStringWrite', \@_, __PACKAGE__ . '::fileStringWrite', \@_,
{name => 'strFileName', trace => true}, {name => 'strFileName', trace => true},
{name => 'strContent', trace => true}, {name => 'strContent', trace => true, required => false},
{name => 'bSync', default => true, trace => true}, {name => 'bSync', default => true, trace => true},
); );
# Open the file for writing # Generate temp filename
sysopen(my $hFile, $strFileName, O_WRONLY | O_CREAT | O_TRUNC, oct(640)) my $strFileNameTemp = $strFileName . '.' . BACKREST_EXE . '.tmp';
or confess &log(ERROR, "unable to open ${strFileName}");
# Write the string # Open file for writing
my $hFile = fileOpen($strFileNameTemp, O_WRONLY | O_CREAT | O_TRUNC, $strFileModeDefault);
# Write content
if (defined($strContent) && length($strContent) > 0)
{
syswrite($hFile, $strContent) syswrite($hFile, $strContent)
or confess &log(ERROR, "unable to write string to ${strFileName}: $!", ERROR_FILE_WRITE); or confess &log(ERROR, "unable to write string to ${strFileName}: $!", ERROR_FILE_WRITE);
}
# Sync file # Sync file
$hFile->sync() if $bSync; $hFile->sync() if $bSync;
@ -908,8 +914,8 @@ sub fileStringWrite
# Close file # Close file
close($hFile); close($hFile);
# Sync directory # Move file to final location
filePathSync(dirname($strFileName)) if $bSync; fileMove($strFileNameTemp, $strFileName, undef, $bSync);
# Return from function and log return values if any # Return from function and log return values if any
return logDebugReturn($strOperation); return logDebugReturn($strOperation);