Debian Codegraph @

R/access.R

raw
1 access_types <- c("exists" = 0L, "read" = 4L, "write" = 2L, "execute" = 1L)
2
3 #' Query for existence and access permissions
4 #'
5 #' * `file_exists(path)` is a shortcut for `file_access(x, "exists")`
6 #' * `dir_exists(path)` and `link_exists(path)` are similar but also check that
7 #' the path is a directory or link, respectively. (`file_exists(path)` returns
8 #' `TRUE` if `path` exists and it is a directory. Use [is_file()] to check for
9 #' file (not directory) existence)
10 #'
11 #' @template fs
12 #' @param mode A character vector containing one or more of 'exists', 'read',
13 #' 'write', 'execute'.
14 #' @details **Cross-compatibility warning:** There is no executable bit on
15 #' Windows. Checking a file for mode 'execute' on Windows, e.g.
16 #' `file_access(x, "execute")` will always return `TRUE`.
17 #' @return A logical vector, with names corresponding to the input `path`.
18 #' @export
19 #' @examples
20 #' file_access("/")
21 #' file_access("/", "read")
22 #' file_access("/", "write")
23 #'
24 #' file_exists("WOMBATS")
25 file_access <- function(path, mode = "exists") {
26 assert_no_missing(path)
27 mode <- match.arg(mode, names(access_types), several.ok = TRUE)
28
29 old <- path_expand(path)
30 mode <- sum(access_types[mode])
31
32 .Call(fs_access_, unclass(old), as.integer(mode))
33 }
34
35 #' @rdname file_access
36 #' @export
37 file_exists <- function(path) {
38 old <- path_expand(path)
39 .Call(fs_exists_, old, path)
40 }
41
42 #' @rdname file_access
43 #' @export
44 dir_exists <- function(path) {
45 res <- is_dir(path)
46
47 links <- is_link(path)
48 res[links] <- is_dir(path_real(path[links]))
49
50 !is.na(res) & res
51 }
52
53 #' @rdname file_access
54 #' @export
55 link_exists <- function(path) {
56 res <- is_link(path)
57 !is.na(res) & res
58 }