Debian Codegraph @

R/ensure.R

raw
1
2 ensure_existing_path <- function(path) {
3 if (!file.exists(path))
4 stopf("no file at path '%s'", path)
5 invisible(path)
6 }
7
8 ensure_existing_file <- function(path) {
9 info <- renv_file_info(path)
10 if (is.na(info$isdir))
11 stopf("no file at path '%s'", path)
12 else if (identical(info$isdir, TRUE))
13 stopf("file '%s' exists but is a directory")
14 invisible(path)
15 }
16
17 ensure_directory <- function(paths, umask = NULL) {
18
19 # handle zero-path case
20 if (empty(paths))
21 return(invisible(paths))
22
23 # set umask if necessary
24 if (!is.null(umask))
25 renv_scope_umask("0")
26
27 # for each path, try to either create the directory, or assert that
28 # the directory already exists. this should also help handle cases
29 # where 'dir.create()' fails because another process created the
30 # directory at the same time we attempted to do so
31 for (path in paths) {
32
33 ok <-
34 dir.create(path, recursive = TRUE, showWarnings = FALSE) ||
35 dir.exists(path)
36
37 if (!ok)
38 stopf("failed to create directory at path '%s'", path)
39
40 }
41
42 # return the paths
43 invisible(paths)
44
45 }
46
47 ensure_parent_directory <- function(path) {
48 ensure_directory(unique(dirname(path)))
49 }
50