| 1 |
#' Copy files, directories or links |
| 2 |
#' |
| 3 |
#' @description |
| 4 |
#' `file_copy()` copies files. |
| 5 |
#' |
| 6 |
#' `link_copy()` creates a new link pointing to the same location as the previous link. |
| 7 |
#' |
| 8 |
#' `dir_copy()` copies the directory recursively at the new location. |
| 9 |
#' @details |
| 10 |
#' The behavior of `dir_copy()` differs slightly than that of `file.copy()` when |
| 11 |
#' `overwrite = TRUE`. The directory will always be copied to `new_path`, even |
| 12 |
#' if the name differs from the basename of `path`. |
| 13 |
#' @param new_path A character vector of paths to the new locations. |
| 14 |
#' @param overwrite Overwrite files if they exist. If this is `FALSE` and the |
| 15 |
#' file exists an error will be thrown. |
| 16 |
#' @template fs |
| 17 |
#' @return The new path (invisibly). |
| 18 |
#' @name copy |
| 19 |
#' @export |
| 20 |
#' @examples |
| 21 |
#' \dontshow{.old_wd <- setwd(tempdir())} |
| 22 |
#' file_create("foo") |
| 23 |
#' file_copy("foo", "bar") |
| 24 |
#' try(file_copy("foo", "bar")) |
| 25 |
#' file_copy("foo", "bar", overwrite = TRUE) |
| 26 |
#' file_delete(c("foo", "bar")) |
| 27 |
#' |
| 28 |
#' dir_create("foo") |
| 29 |
#' # Create a directory and put a few files in it |
| 30 |
#' files <- file_create(c("foo/bar", "foo/baz")) |
| 31 |
#' file_exists(files) |
| 32 |
#' |
| 33 |
#' # Copy the directory |
| 34 |
#' dir_copy("foo", "foo2") |
| 35 |
#' file_exists(path("foo2", path_file(files))) |
| 36 |
#' |
| 37 |
#' # Create a link to the directory |
| 38 |
#' link_create(path_abs("foo"), "loo") |
| 39 |
#' link_path("loo") |
| 40 |
#' link_copy("loo", "loo2") |
| 41 |
#' link_path("loo2") |
| 42 |
#' |
| 43 |
#' # Cleanup |
| 44 |
#' dir_delete(c("foo", "foo2")) |
| 45 |
#' link_delete(c("loo", "loo2")) |
| 46 |
#' \dontshow{setwd(.old_wd)} |
| 47 |
file_copy <- function(path, new_path, overwrite = FALSE) { |
| 48 |
# TODO: copy attributes, e.g. cp -p? |
| 49 |
assert_no_missing(path) |
| 50 |
assert_no_missing(new_path) |
| 51 |
|
| 52 |
old <- path_expand(path) |
| 53 |
new <- path_expand(new_path) |
| 54 |
|
| 55 |
is_directory <- file_exists(new) & is_dir(new) |
| 56 |
|
| 57 |
if (length(new) == 1 && is_directory[[1]]) { |
| 58 |
new <- rep(new, length(path)) |
| 59 |
} |
| 60 |
assert( |
| 61 |
"Length of `path` must equal length of `new_path`", |
| 62 |
length(old) == length(new) |
| 63 |
) |
| 64 |
|
| 65 |
new[is_directory] <- path(new[is_directory], basename(old)) |
| 66 |
|
| 67 |
.Call(fs_copyfile_, old, new, isTRUE(overwrite)) |
| 68 |
|
| 69 |
invisible(path_tidy(new)) |
| 70 |
} |
| 71 |
|
| 72 |
#' @rdname copy |
| 73 |
#' @export |
| 74 |
dir_copy <- function(path, new_path, overwrite = FALSE) { |
| 75 |
assert_no_missing(path) |
| 76 |
assert_no_missing(new_path) |
| 77 |
assert("`path` must be a directory", all(is_dir(path))) |
| 78 |
assert( |
| 79 |
"Length of `path` must equal length of `new_path`", |
| 80 |
length(path) == length(new_path) |
| 81 |
) |
| 82 |
|
| 83 |
for (i in seq_along(path)) { |
| 84 |
if (!isTRUE(overwrite) && isTRUE(unname(is_dir(new_path[[i]])))) { |
| 85 |
new_path[[i]] <- path(new_path[[i]], path_file(path)) |
| 86 |
} |
| 87 |
dir_create(new_path[[i]]) |
| 88 |
|
| 89 |
dirs <- dir_ls(path[[i]], type = "directory", recurse = TRUE, all = TRUE) |
| 90 |
dir_create(path(new_path[[i]], path_rel(dirs, path[[i]]))) |
| 91 |
|
| 92 |
files <- dir_ls( |
| 93 |
path[[i]], |
| 94 |
recurse = TRUE, |
| 95 |
type = c( |
| 96 |
"unknown", |
| 97 |
"file", |
| 98 |
"FIFO", |
| 99 |
"socket", |
| 100 |
"character_device", |
| 101 |
"block_device" |
| 102 |
), |
| 103 |
all = TRUE |
| 104 |
) |
| 105 |
file_copy( |
| 106 |
files, |
| 107 |
path(new_path[[i]], path_rel(files, path[[i]])), |
| 108 |
overwrite = overwrite |
| 109 |
) |
| 110 |
|
| 111 |
links <- dir_ls(path[[i]], recurse = TRUE, type = "symlink", all = TRUE) |
| 112 |
link_copy( |
| 113 |
links, |
| 114 |
path(new_path[[i]], path_rel(links, path[[i]])), |
| 115 |
overwrite = overwrite |
| 116 |
) |
| 117 |
} |
| 118 |
|
| 119 |
invisible(path_tidy(new_path)) |
| 120 |
} |
| 121 |
|
| 122 |
#' @rdname copy |
| 123 |
#' @export |
| 124 |
link_copy <- function(path, new_path, overwrite = FALSE) { |
| 125 |
assert_no_missing(path) |
| 126 |
assert_no_missing(new_path) |
| 127 |
stopifnot(all(is_link(path))) |
| 128 |
|
| 129 |
old <- path_expand(path) |
| 130 |
new <- path_expand(new_path) |
| 131 |
|
| 132 |
to_delete <- isTRUE(overwrite) & link_exists(new) |
| 133 |
if (any(to_delete)) { |
| 134 |
link_delete(new[to_delete]) |
| 135 |
} |
| 136 |
|
| 137 |
invisible(link_create(link_path(old), new)) |
| 138 |
} |