std::filesystem::copy (3) - Linux Manuals
std::filesystem::copy: std::filesystem::copy
NAME
std::filesystem::copy - std::filesystem::copy
Synopsis
Defined in header <filesystem>
void copy( const std::filesystem::path& from,
const std::filesystem::path& to );
void copy( const std::filesystem::path& from, (1) (since C++17)
const std::filesystem::path& to,
std::error_code& ec );
void copy( const std::filesystem::path& from,
const std::filesystem::path& to,
std::filesystem::copy_options options );
void copy( const std::filesystem::path& from, (2) (since C++17)
const std::filesystem::path& to,
std::filesystem::copy_options options,
std::error_code& ec );
Copies files and directories, with a variety of options
1) The default, equivalent to (2) with copy_options::none used as options
2) Copies the file or directory from to file or directory to, using the copy options indicated by options. The behavior is undefined if there is more than one option in any of the copy_options option group present in options (even in the copy_file group).
The behavior is as follows:
* First, before doing anything else, obtains type and permissions of from by no more than a single call to
* If necessary, obtains the status of to, by no more than a single call to
* If either from or to has an implementation-defined file_type, the effects of this function are implementation-defined.
* If from does not exist, reports an error.
* If from and to are the same file as determined by std::filesystem::equivalent, reports an error
* If either from or to is not a regular file, a directory, or a symlink, as determined by std::filesystem::is_other, reports an error
* If from is a directory, but to is a regular file, reports an error
* If from is a symbolic link, then
* Otherwise, if from is a regular file, then
* Otherwise, if from is a directory and copy_options::create_symlinks is set in options, reports an error with an error code equal to std::make_error_code(std::errc::is_a_directory).
* Otherwise, if from is a directory and either options has copy_options::recursive or is copy_options::none,
* Otherwise does nothing
Parameters
from - path to the source file, directory, or symlink
to - path to the target file, directory, or symlink
ec - out-parameter for error reporting in the non-throwing overload
Return value
(none)
Exceptions
The overload that does not take a std::error_code& parameter throws filesystem_error on underlying OS API errors, constructed with from as the first path argument, to as the second path argument, and the OS error code as the error code argument. The overload taking a std::error_code& parameter sets it to the OS API error code if an OS API call fails, and executes ec.clear() if no errors occur. Any overload not marked noexcept may throw std::bad_alloc if memory allocation fails.
Notes
The default behavior when copying directories is the non-recursive copy: the files are copied, but not the subdirectories:
While with copy_options::recursive, the subdirectories are also copied, with their content, recursively.
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
LWG_3013 C++17 error_code overload marked noexcept but can allocate memory noexcept removed
LWG_2682 C++17 attempting to create a symlink for a directory succeeds but does nothing reports an error
Example
// Run this code
See also
copy_options specifies semantics of copy operations
(C++17)
copy_symlink copies a symbolic link
(C++17)
copy_file copies file contents
(C++17)