std::filesy...copy_options(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

✨ NAME ✨

okay bestie, we're in the C++ Standard Libary manual (yes, that's literally the spelling on the page, not me 😭), and this is the page for std::filesystem::copy_options β€” aka std::filesystem::copy_options. iconic name, honestly πŸ’…

πŸ¦‹ Synopsis πŸ¦‹

Defined in header <filesystem>:

Defined in header <filesystem>
enum class copy_options {

    none = /* unspecified */,
    skip_existing = /* unspecified */,
    overwrite_existing = /* unspecified */,
    update_existing = /* unspecified */,
    recursive = /* unspecified */,           (since C++17)
    copy_symlinks = /* unspecified */,
    skip_symlinks = /* unspecified */,
    directories_only = /* unspecified */,
    create_symlinks = /* unspecified */,
    create_hard_links = /* unspecified */

};

So this type is literally the options menu for controlling the behavior of the copy() and copy_file() function. very main character energy for an enum, ngl πŸ’–

copy_options satisfies the requirements of BitmaskType β€” which means the bitwise operators operator&, operator|, operator^, operator~, operator&=, operator|=, and operator^= are defined for this type. none represents the empty bitmask; every other enumerator represents a distinct bitmask element. slay, bitwise bestie ✨

πŸ’… Member constants πŸ’…

okay so listen: at most one copy option in each of the following options groups may be present, otherwise the behavior of the copy functions is undefined. no cap, do not mix and match inside a group 😭

πŸŽ€ options controlling copy_file() when the file already exists πŸŽ€

  • none β€” Report an error (default behavior).
  • skip_existing β€” Keep the existing file, without reporting an error.
  • overwrite_existing β€” Replace the existing file.
  • update_existing β€” Replace the existing file only if it is older than the file being copied.

🌸 options controlling the effects of copy() on subdirectories 🌸

  • none β€” Skip subdirectories (default behavior).
  • recursive β€” Recursively copy subdirectories and their content.
  • none β€” Follow symlinks (default behavior).
  • copy_symlinks β€” Copy symlinks as symlinks, not as the files they point to.
  • skip_symlinks β€” Ignore symlinks.

πŸ‘‘ options controlling the kind of copying copy() does πŸ‘‘

  • none β€” Copy file content (default behavior).
  • directories_only β€” Copy the directory structure, but do not copy any non-directory files.
  • create_symlinks β€” Instead of creating copies of files, create symlinks pointing to the originals. Note: the source path must be an absolute path unless the destination path is in the current directory.
  • create_hard_links β€” Instead of creating copies of files, create hardlinks that resolve to the same files as the originals.

🌈 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

πŸ”₯ See also πŸ”₯

  • copy β€” (C++17) copies files or directories (function)
  • copy_file β€” (C++17) copies file contents (function)

πŸ’– source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::filesy...copy_options(3) β€” and that's the whole manual page, bestie. periodt. πŸ’–

πŸ“¦ where this page comes from