std::experimental::filesystem::copy
Von cppreference.com
< cpp | experimental | fs
| Definiert im Header <experimental/filesystem> |
||
| void copy( const path& from, const path& to ); void copy( const path& from, const path& to, error_code& ec ); |
(1) | (Dateisystem-TS) |
| void copy( const path& from, const path& to, copy_options options ); void copy( const path& from, const path& to, copy_options options, error_code& ec ); |
(2) | (Dateisystem-TS) |
Kopiert Dateien und Verzeichnisse mit einer Vielzahl von Optionen
1) Die Standardvariante, äquivalent zu (2) mit copy_options::none als options.
2) Kopiert die Datei oder das Verzeichnis from in die Datei oder das Verzeichnis to, unter Verwendung der durch options angegebenen Kopieroptionen. Das Verhalten ist undefiniert, wenn mehr als eine Option in einer der copy_options-Optionsgruppen in options vorhanden ist (auch in der Gruppe
copy_file, die für copy nicht relevant ist).Das Verhalten ist wie folgt:
- Zuerst, bevor irgendetwas anderes geschieht, wird der Typ und die Berechtigungen von from durch nicht mehr als einen Aufruf von status ermittelt (oder, wenn
copy_options::skip_symlinksodercopy_options::create_symlinksin options vorhanden sind, durch einen Aufruf vonsymlink_status). - Falls notwendig, wird der Status von to auf die gleiche Weise ermittelt, durch nicht mehr als einen Status- oder Symlink_Status-Aufruf.
- Wenn from nicht existiert, wird ein Fehler gemeldet.
- Wenn from und to dieselbe Datei sind, wie durch equivalent() bestimmt, wird ein Fehler gemeldet.
- Wenn entweder from oder to keine reguläre Datei, kein Verzeichnis und kein Symlink ist, wie durch is_other bestimmt, wird ein Fehler gemeldet.
- Wenn from ein Verzeichnis ist, to aber eine reguläre Datei ist, wird ein Fehler gemeldet.
- Wenn from ein symbolischer Link ist, dann:
- Wenn
copy_options::skip_symlinkin options vorhanden ist, geschieht nichts. - Andernfalls, wenn to nicht existiert und
copy_options::copy_symlinksin options vorhanden ist, verhält es sich so, als ob copy_symlink(from, to) aufgerufen würde. - Andernfalls wird ein Fehler gemeldet.
- Wenn
- Andernfalls, wenn from eine reguläre Datei ist, dann:
- Wenn
copy_options::directories_onlyin options vorhanden ist, geschieht nichts. - Andernfalls, wenn
copy_options::create_symlinksin options vorhanden ist, wird ein Symlink zu to erstellt. Hinweis: from muss ein absoluter Pfad sein, es sei denn, to befindet sich im aktuellen Verzeichnis. - Andernfalls, wenn
copy_options::create_hard_linksin options vorhanden ist, wird ein Hardlink zu to erstellt. - Andernfalls, wenn to ein Verzeichnis ist, verhält es sich so, als ob copy_file(from, to/from.filename(), options) aufgerufen würde (erstellt eine Kopie von from als Datei im Verzeichnis to).
- Andernfalls verhält es sich so, als ob copy_file(from, to, options) aufgerufen würde (kopiert die Datei).
- Wenn
- Andernfalls, wenn from ein Verzeichnis ist und entweder options
copy_options::recursiveenthält oder gleichcopy_options::noneist.
- Wenn to nicht existiert, wird zuerst create_directory(to, from) ausgeführt (erstellt das neue Verzeichnis mit einer Kopie der Attribute des alten Verzeichnisses).
- Dann, unabhängig davon, ob to bereits existierte oder gerade erstellt wurde, werden die Dateien in from iteriert, als ob durch for (const directory_entry& x : directory_iterator(from)) und für jeden Verzeichniseintrag wird rekursiv copy(x.path(), to/x.path().filename(), options | unspecified) aufgerufen, wobei unspecified ein spezielles Bit ist, das keine andere Auswirkung hat, wenn es in options gesetzt ist (der einzige Zweck dieses Bits ist es, die rekursive Kopie von Unterverzeichnissen zu verhindern, wenn options
copy_options::noneist).
- Andernfalls geschieht nichts.
Inhalt |
[edit] Parameter
| from | - | Pfad zur Quellendatei, zum Quellverzeichnis oder zum Quell-Symlink |
| to | - | Pfad zur Zieldatei, zum Zielverzeichnis oder zum Ziel-Symlink |
| ec | - | Ausgabeparameter für die Fehlerberichterstattung in der nicht auslösenden Überladung |
[edit] Rückgabewert
(keine)
[edit] Ausnahmen
Die Überladung, die keinen error_code&-Parameter hat, wirft eine filesystem_error bei zugrundeliegenden Betriebssystem-API-Fehlern, konstruiert mit from als erstem Argument, to als zweitem Argument und dem Betriebssystem-Fehlercode als Fehlercode-Argument. std::bad_alloc kann geworfen werden, wenn die Speicherzuweisung fehlschlägt. Die Überladung, die einen error_code&-Parameter annimmt, setzt diesen auf den Betriebssystem-API-Fehlercode, wenn ein Betriebssystem-API-Aufruf fehlschlägt, und führt ec.clear() aus, wenn keine Fehler auftreten. Diese Überladung hatnoexcept-Spezifikation:
noexcept
[edit] Hinweise
Das Standardverhalten beim Kopieren von Verzeichnissen ist das nicht-rekursive Kopieren: Die Dateien werden kopiert, aber nicht die Unterverzeichnisse.
// Given // /dir1 contains /dir1/file1, /dir1/file2, /dir1/dir2 // and /dir1/dir2 contains /dir1/dir2/file3 // After std::experimental::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
Während mit copy_options::recursive auch die Unterverzeichnisse mit ihrem Inhalt rekursiv kopiert werden.
// ...but after std::experimental::filesystem::copy("/dir1", "/dir3", 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
[edit] Beispiel
Führen Sie diesen Code aus
#include <experimental/filesystem> #include <fstream> #include <iostream> namespace fs = std::experimental::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) // sandbox holds 2 files and 2 directories, one of which has a subdirectory // sandbox/file1.txt // sandbox/file2.txt // sandbox/dir2 // sandbox/dir // sandbox/dir/subdir fs::copy("sandbox", "sandbox/copy", fs::copy_options::recursive); // sandbox/copy holds copies of the above files and subdirectories fs::remove_all("sandbox"); }
[edit] Siehe auch
| spezifiziert die Semantik von Kopiervorgängen (Aufzählung) | |
| kopiert einen symbolischen Link (Funktion) | |
| kopiert Dateiinhalte (Funktion) |