1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-14 10:13:05 +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>
<p>Represent <file>.gz</file> extension with a constant.</p>
</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-core-list>

View File

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