Skip to content

Commit b9384cf

Browse files
committed
Make some global variables constant
1 parent ed87f07 commit b9384cf

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

src/Sandbox.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ include("Docker.jl")
5151
# Load the UserNamespace executor
5252
include("UserNamespaces.jl")
5353

54-
all_executors = Type{<:SandboxExecutor}[
54+
const all_executors = Type{<:SandboxExecutor}[
5555
# We always prefer the UserNamespaces executor, if we can use it,
5656
# and the unprivileged one most of all. Only after that do we try `docker`.
5757
UnprivilegedUserNamespacesExecutor,
@@ -89,16 +89,15 @@ function select_executor(verbose::Bool)
8989
error("Could not find any available executors for $(triplet(HostPlatform()))!")
9090
end
9191

92-
_preferred_executor = nothing
92+
const _preferred_executor = Ref{Union{Nothing, (x->Type{x}).(all_executors)...}}(nothing)
9393
const _preferred_executor_lock = ReentrantLock()
9494
function preferred_executor(;verbose::Bool = false)
9595
lock(_preferred_executor_lock) do
9696
# If we've already asked this question, return the old answer
97-
global _preferred_executor
98-
if _preferred_executor === nothing
99-
_preferred_executor = select_executor(verbose)
97+
if _preferred_executor[] === nothing
98+
_preferred_executor[] = select_executor(verbose)
10099
end
101-
return _preferred_executor
100+
return _preferred_executor[]
102101
end
103102
end
104103

src/utils.jl

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,27 +198,26 @@ Wrapper around libc's `getuid()` function
198198
"""
199199
getgid() = ccall(:getgid, Cint, ())
200200

201-
_sudo_cmd = nothing
201+
const _sudo_cmd = Ref{Union{Vector{String}, Nothing}}(nothing)
202202
function sudo_cmd()
203-
global _sudo_cmd
204203

205204
# Use cached value if we've already run this
206-
if _sudo_cmd !== nothing
207-
return _sudo_cmd
205+
if _sudo_cmd[] !== nothing
206+
return _sudo_cmd[]
208207
end
209208

210-
if getuid() == 0
209+
_sudo_cmd[] = if getuid() == 0
211210
# If we're already root, don't use any kind of sudo program
212-
_sudo_cmd = String[]
211+
String[]
213212
elseif Sys.which("sudo") !== nothing success(`sudo -V`)
214213
# If `sudo` is available, use that
215-
_sudo_cmd = ["sudo"]
214+
["sudo"]
216215
elseif Sys.which("su") !== nothing
217216
# Fall back to `su` if all else fails
218-
_sudo_cmd = ["su", "root", "-c"]
217+
["su", "root", "-c"]
219218
else
220219
@warn("No known sudo-like wrappers!")
221-
_sudo_cmd = String[]
220+
String[]
222221
end
223-
return _sudo_cmd
222+
return _sudo_cmd[]
224223
end

0 commit comments

Comments
 (0)