| 1 |
#' Create files, directories, or links |
| 2 |
#' |
| 3 |
#' The functions `file_create()` and `dir_create()` ensure that `path` exists; |
| 4 |
#' if it already exists it will be left unchanged. That means that compared to |
| 5 |
#' [file.create()], `file_create()` will not truncate an existing file, and |
| 6 |
#' compared to [dir.create()], `dir_create()` will silently ignore existing |
| 7 |
#' directories. |
| 8 |
#' |
| 9 |
#' @param path A character vector of one or more paths. For `link_create()`, |
| 10 |
#' this is the target. |
| 11 |
#' @param mode If file/directory is created, what mode should it have? |
| 12 |
#' |
| 13 |
#' Links do not have mode; they inherit the mode of the file they link to. |
| 14 |
#' @param recurse should intermediate directories be created if they do not |
| 15 |
#' exist? |
| 16 |
#' @param recursive (Deprecated) If `TRUE` recurse fully. |
| 17 |
#' @param ... Additional arguments passed to [path()] |
| 18 |
#' @return The path to the created object (invisibly). |
| 19 |
#' @name create |
| 20 |
#' @examples |
| 21 |
#' \dontshow{.old_wd <- setwd(tempdir())} |
| 22 |
#' file_create("foo") |
| 23 |
#' is_file("foo") |
| 24 |
#' # dir_create applied to the same path will fail |
| 25 |
#' try(dir_create("foo")) |
| 26 |
#' |
| 27 |
#' dir_create("bar") |
| 28 |
#' is_dir("bar") |
| 29 |
#' # file_create applied to the same path will fail |
| 30 |
#' try(file_create("bar")) |
| 31 |
#' |
| 32 |
#' # Cleanup |
| 33 |
#' file_delete("foo") |
| 34 |
#' dir_delete("bar") |
| 35 |
#' \dontshow{setwd(.old_wd)} |
| 36 |
#' @export |
| 37 |
file_create <- function(path, ..., mode = "u=rw,go=r") { |
| 38 |
assert_no_missing(path) |
| 39 |
assert("`mode` must be of length 1", length(mode) == 1) |
| 40 |
|
| 41 |
mode <- as_fs_perms(mode) |
| 42 |
new <- path_expand(path(path, ...)) |
| 43 |
|
| 44 |
.Call(fs_create_, new, as.integer(mode)) |
| 45 |
invisible(path_tidy(new)) |
| 46 |
} |
| 47 |
|
| 48 |
#' @export |
| 49 |
#' @rdname create |
| 50 |
dir_create <- function( |
| 51 |
path, |
| 52 |
..., |
| 53 |
mode = "u=rwx,go=rx", |
| 54 |
recurse = TRUE, |
| 55 |
recursive |
| 56 |
) { |
| 57 |
assert_no_missing(path) |
| 58 |
assert("`mode` must be of length 1", length(mode) == 1) |
| 59 |
|
| 60 |
if (!missing(recursive)) { |
| 61 |
recurse <- recursive |
| 62 |
warning( |
| 63 |
"`recursive` is deprecated, please use `recurse` instead", |
| 64 |
immediate. = TRUE, |
| 65 |
call. = FALSE |
| 66 |
) |
| 67 |
} |
| 68 |
|
| 69 |
mode <- as_fs_perms(mode) |
| 70 |
new <- path_expand(path(path, ...)) |
| 71 |
|
| 72 |
if (!isTRUE(recurse)) { |
| 73 |
.Call(fs_mkdir_, new, as.integer(mode)) |
| 74 |
return(invisible(path_tidy(new))) |
| 75 |
} |
| 76 |
|
| 77 |
paths <- path_split(new) |
| 78 |
for (p in paths) { |
| 79 |
if (length(p) == 1) { |
| 80 |
.Call(fs_mkdir_, p, as.integer(mode)) |
| 81 |
} else { |
| 82 |
p_paths <- Reduce(get("path", mode = "function"), p, accumulate = TRUE) |
| 83 |
if (is_absolute_path(p[[1]])) { |
| 84 |
p_paths <- p_paths[-1] |
| 85 |
} |
| 86 |
.Call(fs_mkdir_, p_paths, as.integer(mode)) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
invisible(path_tidy(new)) |
| 91 |
} |
| 92 |
|
| 93 |
#' @export |
| 94 |
#' @rdname create |
| 95 |
#' @param new_path The path where the link should be created. |
| 96 |
#' @param symbolic Boolean value determining if the link should be a symbolic |
| 97 |
#' (the default) or hard link. |
| 98 |
link_create <- function(path, new_path, symbolic = TRUE) { |
| 99 |
assert_no_missing(path) |
| 100 |
assert_no_missing(new_path) |
| 101 |
assert( |
| 102 |
"Length of `path` must equal length of `new_path`", |
| 103 |
length(path) == length(new_path) |
| 104 |
) |
| 105 |
|
| 106 |
old <- path_expand(path) |
| 107 |
new <- path_expand(new_path) |
| 108 |
|
| 109 |
if (isTRUE(symbolic)) { |
| 110 |
.Call(fs_link_create_symbolic_, old, new) |
| 111 |
} else { |
| 112 |
.Call(fs_link_create_hard_, old, new) |
| 113 |
} |
| 114 |
|
| 115 |
invisible(path_tidy(new_path)) |
| 116 |
} |