Debian Codegraph @

R/path.R

raw
1
2 the$alpha <- c(letters, LETTERS)
3
4 renv_path_absolute <- function(path) {
5
6 substr(path, 1L, 1L) %in% c("~", "/", "\\") || (
7 substr(path, 1L, 1L) %in% the$alpha &&
8 substr(path, 2L, 3L) %in% c(":/", ":\\")
9 )
10
11 }
12
13 renv_path_aliased <- function(path) {
14
15 home <- Sys.getenv("HOME", unset = Sys.getenv("R_USER"))
16 if (!nzchar(home))
17 return(path)
18
19 home <- gsub("\\", "/", home, fixed = TRUE)
20 path <- gsub("\\", "/", path, fixed = TRUE)
21
22 match <- regexpr(home, path, fixed = TRUE, useBytes = TRUE)
23 path[match == 1L] <- file.path("~", substring(path[match == 1L], nchar(home) + 2L))
24
25 path
26
27 }
28
29 renv_path_within <- function(path, parent) {
30 path <- renv_path_canonicalize(path)
31 prefix <- paste(renv_path_canonicalize(parent), "/", sep = "")
32 path == parent | substring(path, 1L, nchar(prefix)) == prefix
33 }
34
35 renv_path_normalize <- function(path, winslash = "/", mustWork = FALSE) {
36 if (renv_platform_unix())
37 renv_path_normalize_unix(path, winslash, mustWork)
38 else
39 renv_path_normalize_win32(path, winslash, mustWork)
40 }
41
42 renv_path_normalize_unix <- function(path,
43 winslash = "/",
44 mustWork = FALSE)
45 {
46 # force paths to be absolute -- this ensures that path prefixes
47 # are prepended even if the path does not exist
48 bad <- !map_lgl(path, renv_path_absolute)
49 if (any(bad)) {
50 prefix <- normalizePath(".", winslash = winslash)
51 path[bad] <- paste(prefix, path[bad], sep = winslash)
52 }
53
54 # normalize the expanded paths
55 normalizePath(path, winslash, mustWork)
56 }
57
58 # NOTE: in versions of R < 4.0.0, normalizePath() does not normalize path
59 # casing; e.g. normalizePath("~/MyPaTh") will not normalize to "~/MyPath"
60 # (assuming that is the "true" underlying casing on the filesystem)
61 #
62 # we work around this by round-tripping between the short name and
63 # the long name, as Windows then has no choice but to figure out
64 # the correct casing for us
65 #
66 # this isn't 100% reliable (not all paths have a short-path equivalent)
67 # but seems to be good enough in practice ...
68 #
69 # except that, if the path contains characters that cannot be represented in the
70 # current encoding, then attempting to normalize the short version of that path
71 # will fail -- so if the path is already UTF-8, then we need to avoid
72 # round-tripping through the short path.
73 #
74 # furthermore, it appears that shortPathName() can mis-encode its result for
75 # strings marked with latin1 encoding?
76 #
77 # https://github.com/rstudio/renv/issues/629
78 renv_path_normalize_win32 <- function(path,
79 winslash = "/",
80 mustWork = FALSE)
81 {
82
83 # see the NOTE above, this workaround is only necessary for R < 4.0.0,
84 # and it complicates things unnecessarily
85 if (getRversion() >= "4.0.0")
86 return(renv_path_normalize_unix(path, winslash, mustWork))
87
88 # get encoding for this set of paths
89 enc <- Encoding(path)
90
91 # perform separate operations for each
92 utf8 <- enc == "UTF-8"
93 latin1 <- enc == "latin1"
94 unknown <- enc == "unknown"
95
96 # normalize based on their encoding
97 path[utf8] <- normalizePath(path[utf8], winslash, mustWork)
98 path[latin1] <- normalizePath(path[latin1], winslash, mustWork)
99 path[unknown] <- renv_path_normalize_win32_impl(path[unknown], winslash, mustWork)
100
101 # return resulting path
102 path
103 }
104
105 renv_path_normalize_win32_impl <- function(path,
106 winslash = "/",
107 mustWork = FALSE)
108 {
109 # get short path
110 expanded <- path.expand(path)
111 short <- utils::shortPathName(expanded)
112
113 # if a UTF-8 string is passed to utils::shortPathName(), it seems that
114 # the string might be latin1-encoded, even though it's marked as UTF-8?
115 if (!identical(R.version$crt, "ucrt")) {
116 utf8 <- Encoding(short) == "UTF-8"
117 Encoding(short[utf8]) <- "latin1"
118 }
119
120 # normalize
121 normalizePath(short, winslash, mustWork)
122 }
123
124 # TODO: this is a lie; for existing paths symlinks will be resolved.
125 # don't use this for paths that need to be uniquely resolved!
126 renv_path_canonicalize <- function(path) {
127 parent <- dirname(path)
128 root <- renv_path_normalize(parent)
129 trimmed <- sub("/+$", "", root)
130 file.path(trimmed, basename(path))
131 }
132
133 renv_path_same <- function(lhs, rhs) {
134 renv_path_canonicalize(lhs) == renv_path_canonicalize(rhs)
135 }
136
137 # get the nth path component from the end of the path
138 renv_path_component <- function(path, index = 1) {
139 splat <- strsplit(path, "[/\\]+")
140 map_chr(splat, function(parts) parts[length(parts) - index + 1])
141 }
142
143 renv_path_pretty <- function(path) {
144 renv_json_quote(renv_path_aliased(path))
145 }
146
147 renv_path_relative <- function(path, root) {
148 within <- startsWith(path, root)
149 path[within] <- substring(path[within], nchar(root) + 2L)
150 path
151 }
152