std::filesystem::copy(3)
π NAME π
std::filesystem::copy β std::filesystem::copy π
okay so this is the std::filesystem::copy(3) page from the C++ Standard Libary (yes, the header spells it like that, not me π), and it is literally the manual for copying files and directories with a whole variety of options.
β¨ Synopsis β¨
Defined in header <filesystem>:
void copy( const std::filesystem::path& from,
const std::filesystem::path& to ); (1) (since C++17)
void copy( const std::filesystem::path& from,
const std::filesystem::path& to,
std::error_code& ec ); (2) (since C++17)
void copy( const std::filesystem::path& from,
const std::filesystem::path& to,
std::filesystem::copy_options options ); (3) (since C++17)
void copy( const std::filesystem::path& from,
const std::filesystem::path& to,
std::filesystem::copy_options options,
std::error_code& ec ); (4) (since C++17)
Copies files and directories, with a variety of options. Iconic.
1,2) The default, equivalent to (3,4) with copy_options::none used as options.
3,4) Copies the file or directory from to file or directory to, using the copy options indicated by options.
β οΈ BESTIE, HEAR ME: the behavior is undefined if there is more than one option in any of thecopy_optionsoption group present inoptions(even in thecopy_filegroup). Do not do that. For real. π
The behavior is as follows:
- First, before doing anything else, obtains type and permissions of
fromby no more than a single call to:std::filesystem::symlink_status, ifcopy_options::skip_symlinks,copy_options::copy_symlinks, orcopy_options::create_symlinksis present inoptions;std::filesystem::statusotherwise.
- If necessary, obtains the status of
to, by no more than a single call to:std::filesystem::symlink_status, ifcopy_options::skip_symlinksorcopy_options::create_symlinksis present inoptions;std::filesystem::statusotherwise (including the case wherecopy_options::copy_symlinksis present inoptions).
- If either
fromortohas an implementation-defined file type, the effects of this function are implementation-defined. - If
fromdoes not exist, reports an error. - If
fromandtoare the same file as determined bystd::filesystem::equivalent, reports an error. - If either
fromortois not a regular file, a directory, or a symlink, as determined bystd::filesystem::is_other, reports an error. - If
fromis a directory, buttois a regular file, reports an error. - If
fromis a symbolic link, then:- If
copy_options::skip_symlinkis present inoptions, does nothing. - Otherwise, if
todoes not exist andcopy_options::copy_symlinksis present inoptions, then behaves as ifcopy_symlink(from, to). - Otherwise, reports an error.
- If
- Otherwise, if
fromis a regular file, then:- If
copy_options::directories_onlyis present inoptions, does nothing. - Otherwise, if
copy_options::create_symlinksis present inoptions, creates a symlink toto. Note:frommust be an absolute path unlesstois in the current directory. - Otherwise, if
copy_options::create_hard_linksis present inoptions, creates a hard link toto. - Otherwise, if
tois a directory, then behaves as ifcopy_file(from, to/from.filename(), options)(creates a copy offromas a file in the directoryto). - Otherwise, behaves as if
copy_file(from, to, options)(copies the file).
- If
- Otherwise, if
fromis a directory andcopy_options::create_symlinksis set inoptions, reports an error with an error code equal tostd::make_error_code(std::errc::is_a_directory). - Otherwise, if
fromis a directory and eitheroptionshascopy_options::recursiveor iscopy_options::none:- If
todoes not exist, first executescreate_directory(to, from)(creates the new directory with a copy of the old directory's attributes). - Then, whether
toalready existed or was just created, iterates over the files contained infromas if byfor (const std::filesystem::directory_entry& x : std::filesystem::directory_iterator(from))and for each directory entry, recursively callscopy(x.path(), to/x.path().filename(), options | in-recursive-copy), wherein-recursive-copyis a special bit that has no other effect when set inoptions. (The sole purpose of setting this bit is to prevent recursive copying subdirectories ifoptionsiscopy_options::none.)
- If
- 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) β she returns nothing, bestie. Void vibes only. β¨
π Exceptions π
- Any overload not marked
noexceptmay throwstd::bad_allocif memory allocation fails. 1,3)Throwsstd::filesystem::filesystem_erroron underlying OS API errors, constructed withfromas the first path argument,toas the second path argument, and the OS error code as the error code argument.2,4)Sets astd::error_code¶meter to the OS API error code if an OS API call fails, and executesec.clear()if no errors occur.
πΈ Notes πΈ
The default behavior when copying directories is the non-recursive copy: the files are copied, but not the subdirectories:
// Given
// /dir1 contains /dir1/file1, /dir1/file2, /dir1/dir2
// and /dir1/dir2 contains /dir1/dir2/file3
// After
std::filesystem::copy("/dir1", "/dir3");
// /dir3 is created (with the attributes of /dir1)
// /dir1/file1 is copied to /dir3/file1
// /dir1/file2 is copied to /dir3/file2
While with copy_options::recursive, the subdirectories are also copied, with their content, recursively.
// ...but after
std::filesystem::copy("/dir1", "/dir3",
std::filesystem::copy_options::recursive);
// /dir3 is created (with the attributes of /dir1)
// /dir1/file1 is copied to /dir3/file1
// /dir1/file2 is copied to /dir3/file2
// /dir3/dir2 is created (with the attributes of /dir1/dir2)
// /dir1/dir2/file3 is copied to /dir3/dir2/file3
π¦ Example π¦
// Run this code
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::filesystem;
int main()
{
fs::create_directories("sandbox/dir/subdir");
std::ofstream("sandbox/file1.txt").put('a');
fs::copy("sandbox/file1.txt", "sandbox/file2.txt"); // copy file
fs::copy("sandbox/dir", "sandbox/dir2"); // copy directory (non-recursive)
const auto copyOptions = fs::copy_options::update_existing
| fs::copy_options::recursive
| fs::copy_options::directories_only
;
fs::copy("sandbox", "sandbox_copy", copyOptions);
static_cast<void>(std::system("tree"));
fs::remove_all("sandbox");
fs::remove_all("sandbox_copy");
}
π Possible output: π
.
βββ sandbox
β βββ dir
β β βββ subdir
β βββ dir2
β βββ file1.txt
β βββ file2.txt
βββ sandbox_copy
βββ dir
β βββ subdir
βββ dir2
8 directories, 2 files
π§ Defect reports π§
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 3013, applied toC++17:error_codeoverload markednoexceptbut can allocate memory β correct behavior:noexceptremoved.LWG 2682, applied toC++17: attempting to create a symlink for a directory succeeds but does nothing β correct behavior: reports an error.
π See also π
copy_optionsβ specifies semantics of copy operations(C++17)(enum)copy_symlinkβ copies a symbolic link(C++17)(function)copy_fileβ copies file contents(C++17)(function)
Thatβs the whole page, bestie π«Ά source/version: http://cppreference.com; date: 2024.06.10. Mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms