-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathlibpaths.R
186 lines (136 loc) · 4.46 KB
/
libpaths.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
`_renv_libpaths` <- new.env(parent = emptyenv())
# NOTE: if sandboxing is used then these symbols will be clobbered;
# save them so we can properly restore them later if so required
renv_libpaths_init <- function() {
assign(".libPaths()", .libPaths(), envir = `_renv_libpaths`)
assign(".Library", .Library, envir = `_renv_libpaths`)
assign(".Library.site", .Library.site, envir = `_renv_libpaths`)
}
renv_libpaths_default <- function() {
.libPaths()[1]
}
renv_libpaths_all <- function() {
.libPaths()
}
renv_libpaths_system <- function() {
get(".Library", envir = `_renv_libpaths`)
}
renv_libpaths_site <- function() {
get(".Library.site", envir = `_renv_libpaths`)
}
renv_libpaths_external <- function(project) {
projlib <- settings$external.libraries(project = project)
conflib <- config$external.libraries(project)
.expand_R_libs_env_var(c(projlib, conflib))
}
# on Windows, attempting to use a library path containing
# characters considered special by cmd.exe will fail.
# to guard against this, we try to create a junction point
# from the temporary directory to the target library path
#
# https://github.com/rstudio/renv/issues/334
renv_libpaths_safe <- function(libpaths) {
if (renv_libpaths_safe_check(libpaths))
return(libpaths)
map_chr(libpaths, renv_libpaths_safe_impl)
}
renv_libpaths_safe_check <- function(libpaths) {
# if any of the paths have single quotes, then
# we need to use a safe path
# https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17973
if (any(grepl("'", libpaths, fixed = TRUE)))
return(FALSE)
# on Windows, we need to use safe library paths for R < 4.0.0
# https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17709
if (renv_platform_windows() && getRversion() < "4.0.0")
return(FALSE)
# otherwise, we're okay
return(TRUE)
}
renv_libpaths_safe_impl <- function(libpath) {
# check for an unsafe library path
unsafe <-
Encoding(libpath) == "UTF-8" ||
grepl("[&<>^|'\"]", libpath)
# if the path appears safe, use it as-is
if (!unsafe)
return(libpath)
# try to form a safe library path
methods <- c(
renv_libpaths_safe_tempdir,
renv_libpaths_safe_userlib
)
for (method in methods) {
safelib <- catchall(method(libpath))
if (is.character(safelib))
return(safelib)
}
# could not form a safe library path;
# just use the existing library path as-is
libpath
}
renv_libpaths_safe_tempdir <- function(libpath) {
safelib <- tempfile("renv-library-")
if (renv_platform_windows())
renv_file_junction(libpath, safelib)
else
file.symlink(libpath, safelib)
safelib
}
renv_libpaths_safe_userlib <- function(libpath) {
# form path into user library
userlib <- renv_libpaths_user()[[1]]
base <- file.path(userlib, ".renv-links")
ensure_directory(base)
# create name for actual junction point
name <- renv_hash_text(libpath)
safelib <- file.path(base, name)
# if the junction already exists, use it
if (renv_file_same(libpath, safelib))
return(safelib)
# otherwise, try to create it. note that junction
# points can be removed with a non-recursive unlink
unlink(safelib)
if (renv_platform_windows())
renv_file_junction(libpath, safelib)
else
file.symlink(libpath, safelib)
safelib
}
renv_libpaths_set <- function(libpaths) {
oldlibpaths <- .libPaths()
safepaths <- renv_libpaths_safe(libpaths)
.libPaths(safepaths)
oldlibpaths
}
# NOTE: may return more than one library path!
renv_libpaths_user <- function() {
# if renv is active, the user library will be saved
envvars <- c("RENV_DEFAULT_R_LIBS_USER", "R_LIBS_USER")
for (envvar in envvars) {
value <- Sys.getenv(envvar, unset = NA)
if (is.na(value) || value == "<NA>" || !nzchar(value))
next
parts <- strsplit(value, .Platform$path.sep, fixed = TRUE)[[1]]
return(parts)
}
# otherwise, default to active library (shouldn't happen but best be safe)
renv_libpaths_default()
}
renv_libpaths_activate <- function(project) {
projlib <- renv_paths_library(project = project)
extlib <- renv_libpaths_external(project = project)
userlib <- if (config$user.library())
renv_libpaths_user()
libpaths <- c(projlib, extlib, userlib)
lapply(libpaths, ensure_directory)
renv_libpaths_set(libpaths)
libpaths
}
renv_libpaths_restore <- function() {
libpaths <- get(".libPaths()", envir = `_renv_libpaths`)
renv_libpaths_set(libpaths)
}
renv_libpaths_resolve <- function(library) {
library %||% renv_libpaths_all()
}