-
Notifications
You must be signed in to change notification settings - Fork 80
/
create.R
67 lines (60 loc) · 1.84 KB
/
create.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#' Create a file, directory, or link.
#'
#' These functions ensure that `path` exists; if it already exists it will
#' be left unchanged. That means that ompared to [file.create()],
#' `file_create()` will not truncate an existing, and compared to
#' [dir.create()], `dir_create()` will silently ignore
#' existing directories.
#'
#' @template fs
#' @param mode If file/directory is created, what mode should it have?
#'
#' Links do not have mode; they inherit the mode of the file they link to.
#' @param recursive should intermediate directories be created if they do not
#' exist?
#' @examples
#' x <- file_create(tempfile())
#' is_file(x)
#' # dir_create applied to the same path will fail
#' try(dir_create(x))
#'
#' x <- dir_create(tempfile())
#' is_dir(x)
#' # file_create applied to the same path will fail
#' try(file_create(x))
#' @export
file_create <- function(path, mode = "u+rw,go+r") {
stopifnot(length(mode) == 1)
create_(path_expand(path), mode)
invisible(path_tidy(path))
}
#' @export
#' @rdname file_create
dir_create <- function(path, mode = "u+rwx,go+rx", recursive = TRUE) {
paths <- path_split(path)
for (p in paths) {
if (length(p) == 1 || !isTRUE(recursive)) {
mkdir_(p, mode)
}
else {
mkdir_(Reduce(file.path, p, accumulate = TRUE), mode)
}
}
invisible(path_tidy(path))
}
#' @export
#' @rdname file_create
#' @param new_path The path where the link should be created.
#' @param symbolic Boolean value determining if the link should be a symbolic
#' (the default) or hard link.
link_create <- function(path, new_path, symbolic = TRUE) {
path <- path_expand(path)
new_path <- path_expand(new_path)
stopifnot(length(path) == length(new_path))
if (isTRUE(symbolic)) {
link_create_symbolic_(path, new_path)
} else {
link_create_hard_(path, new_path)
}
invisible(path_tidy(new_path))
}