Opened 14 years ago

Closed 12 years ago

Last modified 4 years ago

#2925 closed Bugs (fixed)

boost::filesystem cannot atomically copy file if target exists

Reported by: andre-boost@… Owned by: Beman Dawes
Milestone: Boost 1.39.0 Component: filesystem
Version: Boost 1.38.0 Severity: Problem
Keywords: Cc:

Description

The following code leads to race conditions, i.e. can fail with 'file exists' if another process interferes (pseudo code)

void copy (std::string src, 
           std::string target, 
           int flags)
{
  if ( (flags & Overwrite)     &&
       boost::fs::exists (tgt) ) 
  {
    boost::fs::remove (tgt);
  }

  // second process can create file again here

  boost::fs::copy_file (src, tgt);
}

I think similar problems are obvious for other boost::fs operations.

A solution would be to allow flags for boost's operations, and forward them to open (that is where the current code bails out I think). Other solutions are possible, too, like adding another call which allows overwrite.

Thanks, Andre.

Change History (2)

comment:1 by Beman Dawes, 12 years ago

Resolution: fixed
Status: newclosed

The overwrite_if_exists option takes care of this. It has been available for several releases - I should have closed the ticket at the time it was added.

Thanks,

--Beman

comment:2 by anonymous, 4 years ago

Summary: boost::filesystem cannot atomically copy file if tarteg existsboost::filesystem cannot atomically copy file if target exists
Note: See TracTickets for help on using tickets.