Copies the content of the file source to dest, which is created or, if it exists, replaced. Returns whether it all worked.
copy_file :: (source: string, dest: string, silent := true) -> bool
The parent folder of dest must exist. Fails when source is missing or is a directory, when dest is the same file as
source (it would be emptied first), or when either path has a NUL byte. If the copy fails half-way, dest keeps what was
written. Permissions and times are not copied. silent has no effect here; on
Windows a failure is logged only when it is false, and passing it keeps code the same on every target.
Parameters
- source: string
- dest: string
- silent := true
Returns
- bool
#import "Extensions/Environment";
#import "File";
base := environment_variable("TMPDIR");
if base == "" base = environment_variable("TEMP");
if base == "" base = "/tmp";
source := tprint("%/jai-doc-copy-source.txt", base);
dest := tprint("%/jai-doc-copy-dest.txt", base);
defer {
file_delete(source);
file_delete(dest);
}
assert(write_entire_file(source, "copper"));
assert(write_entire_file(dest, "a longer file that gets replaced"));
assert(copy_file(source, dest));
text, ok := read_entire_file(dest);
assert(ok && text == "copper");
free(text);
assert(!copy_file(source, source, silent = true)); // onto itself
assert(!copy_file(tprint("%-missing", source), dest, silent = true));