Modify

Ticket #2866 (closed Bugs: fixed)

Opened 5 years ago

Last modified 4 years ago

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

Reported by: t.kozlov@… Owned by: bemandawes
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 ) );
}

Attachments

Change History

comment:1 Changed 5 years ago by csapuntz@…

  • Severity changed from Cosmetic to Problem

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 Changed 4 years ago by sean@…

  • Cc sean@… added
  • Version changed from Boost 1.38.0 to Boost 1.40.0
  • Milestone changed from Boost 1.39.0 to Boost 1.41.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 Changed 4 years ago by bemandawes

  • Status changed from new to closed
  • Resolution set to fixed

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

View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
The resolution will be deleted. Next status will be 'reopened'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.