Opened 14 years ago

Closed 13 years ago

#2866 closed Bugs (fixed)

boost::filesystem::rename throw exception when target file exists, but documentation tell us that it must be replaced

Reported by: t.kozlov@… Owned by: Beman Dawes
Milestone: Boost 1.41.0 Component: filesystem
Version: Boost 1.40.0 Severity: Problem
Keywords: Cc: sean@…

Description

[Note: If from_p and to_p resolve to the same file, no action is taken. '''Otherwise, if to_p resolves to an existing file, it is removed'''. A symbolic link is itself renamed, rather than the file it resolves to being renamed. -- end note]

A part from libs/filesystem/operations.cpp

BOOST_FILESYSTEM_DECL error_code
rename_api( const std::string & from, const std::string & to )
{
    return error_code( ::MoveFileA( from.c_str(), to.c_str() )
      ? 0 : ::GetLastError(), system_category );
}
BOOST_FILESYSTEM_DECL error_code
rename_api( const std::string & from, const std::string & to )
{
   // POSIX is too permissive so must check
   error_code dummy;
   if ( fs::exists( status_api( to, dummy ) ) ) 
     return error_code( EEXIST, system_category );
   return error_code( std::rename( from.c_str(), to.c_str() ) != 0 
     ? errno : 0, system_category );
}

And boost/filesystem/operation.hpp

BOOST_FS_FUNC(void) rename( const Path & from_path, const Path & to_path )
{
  system::error_code ec( detail::rename_api(
    from_path.external_directory_string(),
    to_path.external_directory_string() ) );
  if ( ec )
    boost::throw_exception( basic_filesystem_error<Path>(
      "boost::filesystem::rename",
      from_path, to_path, ec ) );
}

Change History (3)

comment:1 by csapuntz@…, 13 years ago

Severity: CosmeticProblem

MoveFileA/W error out if destination exists. The posix rename_api is written to error out if destination exists (though there is a race of course).

Recommend using MoveFileEx(MOVEFILE_REPLACE_EXISTING) on Windows

comment:2 by sean@…, 13 years ago

Cc: sean@… added
Milestone: Boost 1.39.0Boost 1.41.0
Version: Boost 1.38.0Boost 1.40.0

Passing the call back to POSIX's rename(2) call should result in the desired outcome that matches the semantics of both POSIX's rename(2) call and boost::filesystem's documentation. The attached patch fixes this problem and changes the behavior of boost::filesystem::rename() to match that of rename(2). I believe that the Win32 API is doing the correct thing. The extra exists() checks should be removed from the POSIX versions according to the posix specification. POSIX rename(2) guarantees rename(2) to be atomic and the extra check creates a race condition that prevents boost's rename() from being used.

Thanks in advance.

http://www.opengroup.org/onlinepubs/000095399/functions/rename.html

--- /tmp/operations.cpp 2009-11-17 14:40:39.000000000 -0800 +++ contrib-Darwin-i386/boost-1.41.0.beta1.cmake1/libs/filesystem/src/operations.cpp 2009-11-17 14:41:00.000000000 -0800 @@ -1186,10 +1186,6 @@

BOOST_FILESYSTEM_DECL error_code rename_api( const std::string & from, const std::string & to ) {

  • POSIX is too permissive so must check
  • error_code dummy;
  • if ( fs::exists( status_api( to, dummy ) ) )
  • return error_code( EEXIST, system_category );

return error_code( std::rename( from.c_str(), to.c_str() ) != 0

? errno : 0, system_category );

}

comment:3 by Beman Dawes, 13 years ago

Resolution: fixed
Status: newclosed

(In [58226]) rename() specs now same as POSIX; fix #2866, but for V3 only

Note: See TracTickets for help on using tickets.