Fix Filesystem #2925, copy_file atomiticity

[SVN r53073]
This commit is contained in:
Beman Dawes
2009-05-17 15:55:46 +00:00
parent 7bc9e5db73
commit 0aaa65e768
4 changed files with 61 additions and 22 deletions

View File

@@ -706,9 +706,9 @@ namespace boost
}
BOOST_FILESYSTEM_DECL error_code
copy_file_api( const std::wstring & from, const std::wstring & to )
copy_file_api( const std::wstring & from, const std::wstring & to, bool fail_if_exists )
{
return error_code( ::CopyFileW( from.c_str(), to.c_str(), /*fail_if_exists=*/true )
return error_code( ::CopyFileW( from.c_str(), to.c_str(), fail_if_exists )
? 0 : ::GetLastError(), system_category );
}
@@ -886,9 +886,9 @@ namespace boost
}
BOOST_FILESYSTEM_DECL error_code
copy_file_api( const std::string & from, const std::string & to )
copy_file_api( const std::string & from, const std::string & to, bool fail_if_exists )
{
return error_code( ::CopyFileA( from.c_str(), to.c_str(), /*fail_if_exists=*/true )
return error_code( ::CopyFileA( from.c_str(), to.c_str(), fail_if_exists )
? 0 : ::GetLastError(), system_category );
}
@@ -1203,22 +1203,30 @@ namespace boost
BOOST_FILESYSTEM_DECL error_code
copy_file_api( const std::string & from_file_ph,
const std::string & to_file_ph )
const std::string & to_file_ph, bool fail_if_exists )
{
const std::size_t buf_sz = 32768;
boost::scoped_array<char> buf( new char [buf_sz] );
int infile=-1, outfile=-1; // -1 means not open
struct stat from_stat;
if ( ::stat( from_file_ph.c_str(), &from_stat ) != 0
|| (infile = ::open( from_file_ph.c_str(),
O_RDONLY )) < 0
|| (outfile = ::open( to_file_ph.c_str(),
O_WRONLY | O_CREAT | O_EXCL,
from_stat.st_mode )) < 0 )
// bug fixed: code previously did a stat() on the from_file first, but that
// introduced a gratuitous race condition; the stat() is now done after the open()
if ( (infile = ::open( from_file_ph.c_str(), O_RDONLY )) < 0 )
{ return error_code( errno, system_category ); }
struct stat from_stat;
if ( ::stat( from_file_ph.c_str(), &from_stat ) != 0 )
{ return error_code( errno, system_category ); }
int oflag = O_CREAT | O_WRONLY;
if ( fail_if_exists ) oflag |= O_EXCL;
if ( (outfile = ::open( to_file_ph.c_str(), oflag, from_stat.st_mode )) < 0 )
{
if ( infile >= 0 ) ::close( infile );
return error_code( errno, system_category );
int open_errno = errno;
BOOST_ASSERT( infile >= 0 );
::close( infile );
return error_code( open_errno, system_category );
}
ssize_t sz, sz_read=1, sz_write;