Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#4930 closed Bugs (fixed)

filesystem::copy_file() has inconsistent behavior when copy_option::overwrite_if_exists is used

Reported by: postoronnimv77@… Owned by: Beman Dawes
Milestone: To Be Determined Component: filesystem
Version: Boost 1.44.0 Severity: Problem
Keywords: Cc:

Description

The behavior of filesystem::copy_file() function with copy_option::overwrite_if_exists option set is misleading and differs on Windows and on the rest (POSIX) systems.

On Windows CopyFile() API is used for this which correctly overwrites the target file if it exists.

On non-Windows systems open(2) is used with O_CREAT|O_WRONLY which causes only the first size(from_file) bytes of the destination file tp be overwritten with the source leaving the rest tail intact. So if the source file initially contains 'ABCD' and the destination contains 'abcde' the result of copy_file() with 'overwrite_if_exists option' set results the destination to contain 'ABCDe'.

The fix seems trivial: add O_TRUNC option when opening the destination file.

Change History (2)

comment:1 by Beman Dawes, 12 years ago

Resolution: fixed
Status: newclosed

Ouch! That was nasty!

Thanks for the bug report,

--Beman

comment:2 by Beman Dawes, 12 years ago

(In [67067]) Fix #4930, add V3 test case to detect any future regression

Note: See TracTickets for help on using tickets.