Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

## New features

* Experimental: `options(Require.cloneMethod = "hardlinkReadOnly")` (or `R_REQUIRE_CLONE_METHOD`) makes `Require.cloneFrom` hard-link the files R itself installed (R code, compiled code, help) instead of copying them, after taking the write permission off the source files. Files a package ships in `inst/` (data files, databases) are still copied, so a package that writes into them is unaffected. The files are then read-only under every name, so an in-place write to one (`file.copy(overwrite = TRUE)`, `writeBin()`) fails instead of emptying it in every library that shares it; installs, `remove.packages()` and clones still work, as they replace files and folders by rename. The default stays `"copy"`. The mode makes the source library's files read-only too, is never used on Windows or across file systems, and copies any package it cannot chmod or link. See `?RequireOptions`.

* `install = "HEAD"`, or `options(Require.install = "HEAD")`, installs every requested package, GitHub or CRAN, if missing and updates it if not current: to the latest commit of its branch (GitHub) or the newest release (CRAN), as if each were written `"pkg (HEAD)"`. This replaces a minimum version (`>=`), as modules usually write, and other requests for the same package; a package with an exact or maximum version is kept as requested. Dependencies are updated only when they fail a version floor, like `pak::pak(upgrade = FALSE)`. Packages already loaded are installed too, with the usual "Please restart R" warning. New `UpdateOrInstall()` is `Install(packages, install = "HEAD", ...)`. Set the option before `setupProject()` to update all of a project's packages. `Install()` now also takes `"force"` and `"HEAD"` from `Require.install`.

* `updatePackages()` has a new `packages` argument. `NULL`, the default, updates every installed package as before. Otherwise it installs or updates the named packages and all of their dependencies (recursively) to the latest GitHub commit or CRAN release, like `pak::pak(packages, upgrade = TRUE)`; `UpdateOrInstall()` is the `upgrade = FALSE` counterpart. `updatePackages()` now also passes `libPaths` on to `Install()`.
Expand Down
79 changes: 70 additions & 9 deletions R/Require2.R
Original file line number Diff line number Diff line change
Expand Up @@ -2128,13 +2128,14 @@ cloneCopies <- function(cp, toLib) {
cloneCheckRecord(cp, toLib) # loaded after the install; see cloneCheckStart()
}

# Copy each of `Packages` from `fromLib` into `toLib`, never hard-linking: a link makes two
# libraries share one inode, so a later in-place write to either (a second clone, a reinstall)
# empties the file in both (26,525 files in every library sharing them, 2026-11-25). Each package
# is copied into a working folder inside `toLib` (same file system) and renamed into place, so a
# clone replaces the whole package folder, and `toLib` is locked meanwhile so two processes cannot
# clone into it at once.
copyPackageFilesInner <- function(Packages, fromLib, toLib) {
# Copy each of `Packages` from `fromLib` into `toLib`. By default (`Require.cloneMethod = "copy"`) every
# file is copied, never hard-linked: a link makes two libraries share one inode, so a later in-place
# write to either (a second clone, a reinstall) empties the file in both (26,525 files in every
# library sharing them, 2026-11-25). Each package is copied into a working folder inside `toLib`
# (same file system) and renamed into place, so a clone replaces the whole package folder, and `toLib`
# is locked meanwhile so two processes cannot clone into it at once. With `"hardlinkReadOnly"`
# (experimental) only the per-file step changes; see linkPackageReadOnly().
copyPackageFilesInner <- function(Packages, fromLib, toLib, method = cloneMethod()) {
checkPath(toLib, create = TRUE)
withCloneLock(toLib, function(refreshLock) {
lapply(Packages, function(packToClone) {
Expand All @@ -2144,14 +2145,74 @@ copyPackageFilesInner <- function(Packages, fromLib, toLib) {
work <- tempfile("00CLONE-", tmpdir = toLib)
on.exit(unlink(work, recursive = TRUE), add = TRUE)
dir.create(work)
ok <- all(file.copy(from, work, recursive = TRUE, copy.date = TRUE)) &&
replaceDirByRename(file.path(work, packToClone), file.path(toLib, packToClone))
ok <- (identical(method, "hardlinkReadOnly") && linkPackageReadOnly(from, work)) ||
all(file.copy(from, work, recursive = TRUE, copy.date = TRUE))
ok <- ok && replaceDirByRename(file.path(work, packToClone), file.path(toLib, packToClone))
refreshLock() # a long clone must not look abandoned
invisible(ok)
})
})
}

# `Require.cloneMethod`: "copy" (the default) or "hardlinkReadOnly". Never a link on Windows, and
# an unknown value is a warning and "copy". (A link that fails, e.g. across file systems, is
# handled per package by linkPackageReadOnly().)
cloneMethod <- function(method = getOption("Require.cloneMethod", "copy")) {
method <- as.character(method)[1]
if (is.na(method) || !method %in% c("copy", "hardlinkReadOnly")) {
warning("Require.cloneMethod must be 'copy' or 'hardlinkReadOnly', not '", method, "'; using 'copy'",
call. = FALSE)
return("copy")
}
if (isWindows()) "copy" else method
}

# Top-level entries of an installed package that R CMD INSTALL makes itself (code, help, metadata).
# Everything else in an installed package came from its inst/ folder (data files, databases,
# templates), which a package may write into at run time, so linkPackageReadOnly() copies it.
.cloneLinkableTop <- c("DESCRIPTION", "NAMESPACE", "INDEX", "Meta", "R", "data", "demo", "doc",
"exec", "help", "html", "libs", "po")

# Experimental: make the folder `from` appear as `to`/<basename(from)> by hard-linking each file
# that R itself installed (see .cloneLinkableTop; files from inst/ are copied, keeping their mode),
# after taking the write bits off the source file. Permissions belong to the inode, so the file is
# read-only under every name and a later write in place (`file.copy(overwrite = TRUE)`, `writeBin()`)
# fails with EACCES instead of truncating the file in every library that shares it. Replacing a
# file or folder by rename needs only a writable directory, and directories are made new, not linked,
# so installs, `remove.packages()` and `unlink()` still work. Returns FALSE, leaving nothing behind
# in `to`, if anything fails (a symlink in the package, chmod not permitted, a link across file
# systems); the caller then copies. The cheap checks (symlinks, new folders) come first, then every
# file is linked, and only then are the sources chmodded (a link shares the inode, so the mode applies
# under both names). A failed link therefore changes no mode; a failed chmod puts back the modes of
# exactly the files it changed. Either way the fallback copy has the original modes. The shared files
# are writable for the moments between the links and the chmod, while the destination library is locked.
# (`link` and `chmod` are arguments so that tests can make them fail.)
linkPackageReadOnly <- function(from, to, link = file.link, chmod = Sys.chmod) {
top <- file.path(to, basename(from))
changed <- character()
orig <- integer()
ok <- tryCatch({
files <- list.files(from, recursive = TRUE, all.files = TRUE, no.. = TRUE, include.dirs = FALSE)
dirs <- setdiff(list.dirs(from, full.names = FALSE, recursive = TRUE), "")
src <- file.path(from, files)
mode <- as.integer(file.mode(src))
orig <- setNames(mode, src)
ro <- bitwAnd(mode, bitwNot(strtoi("222", 8L))) # no write bit for anyone
lnk <- sub("/.*", "", files) %in% .cloneLinkableTop # the rest came from inst/: copied
todo <- lnk & mode != ro
all(!is.na(mode)) && !any(nzchar(Sys.readlink(src))) && dir.create(top) &&
all(vapply(file.path(top, dirs), dir.create, logical(1))) &&
all(suppressWarnings(link(src[lnk], file.path(top, files[lnk])))) &&
all(file.copy(src[!lnk], file.path(top, files[!lnk]), copy.mode = TRUE, copy.date = TRUE)) &&
{ changed <- src[todo]; all(chmod(src[todo], mode = as.octmode(ro[todo]))) }
}, error = function(e) FALSE)
if (!isTRUE(ok)) {
unlink(top, recursive = TRUE)
if (length(changed)) Sys.chmod(changed, mode = as.octmode(orig[changed]))
}
isTRUE(ok)
}

# Put the folder `new` at `to` with file.rename(), never writing into `to`. An existing `to` is
# renamed aside first and removed only once `new` is in place; if that fails it is put back.
replaceDirByRename <- function(new, to) {
Expand Down
23 changes: 23 additions & 0 deletions R/RequireOptions.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,28 @@
#' copy is used when its version meets the user's requirement and those of
#' every package pak plans to install; of several copies, the version pak
#' resolved is preferred, then the highest.}
#' \item{`cloneMethod`}{ Default: `"copy"`. How `cloneFrom` puts a package into the
#' destination library: `"copy"` copies every file. `"hardlinkReadOnly"` is
#' experimental: each file that R itself installed (R code, compiled code,
#' help, metadata) is hard-linked, not copied, after the write
#' permission is taken off the *source* file. Files the package ships in its
#' `inst/` folder (data files, databases) are copied as usual, because a
#' package may write into those. Permissions belong to the file,
#' not to a name for it, so the file is read-only in every library that shares it,
#' and writing into it in place (`file.copy(overwrite = TRUE)`, `writeBin()`,
#' `cat >`) is an error instead of silently emptying the file in all of them.
#' Replacing a file or folder by renaming one over it, as R's installer and
#' `Require` do, needs only a writable folder and still works, as do
#' `remove.packages()` and `unlink()`. **This mode changes the source library:
#' its package files become read-only, so any tool that edits an installed
#' package's files in place (a package that writes into its own folder at run
#' time, a manual edit) now fails there and in every clone.** Installing a
#' newer version into either library gives that library its own new files; the
#' other keeps the old ones. Space is saved and cloning is faster, but nothing
#' is shared across file systems or on Windows, where a package is copied; a
#' package that cannot be chmodded or linked (a symlink in it, a source you do
#' not own) is copied too, never left half-linked. Also set by the environment
#' variable `R_REQUIRE_CLONE_METHOD`.}
#' \item{`downloadTimeout`}{Default: `300L` (seconds). Used as the floor for
#' `options("timeout")` while `Require` itself downloads a file from GitHub
#' ([.downloadFileMasterMainAuth()]). R's stock 60-second timeout is too
Expand Down Expand Up @@ -134,6 +156,7 @@ RequireOptions <- function() {
Require.cachePkgDir = "default",
Require.checkInternet = TRUE,
Require.cloneFrom = TRUE,
Require.cloneMethod = "copy",
Require.downloadTimeout = 300L,
Require.install = TRUE,
Require.installPackagesSys = 2L, # if (isMacOS()) 2L else 2L,
Expand Down
2 changes: 1 addition & 1 deletion R/pak.R
Original file line number Diff line number Diff line change
Expand Up @@ -3307,7 +3307,7 @@ ensurePakInProjectLib <- function(projLib, repos = getOption("repos"),
projLibNorm <- tryCatch(normalizePath(projLib, mustWork = FALSE),
error = function(e) projLib)
if (!is.null(src) && !identical(dirname(src), projLibNorm)) {
linked <- all(unlist(copyPackageFilesInner("pak", dirname(src), projLib)))
linked <- all(unlist(copyPackageFilesInner("pak", dirname(src), projLib, method = "copy")))
if (linked)
messageVerbose(" Copied pak from ", src, " (no reinstall needed)",
verbose = verbose, verboseLevel = 1)
Expand Down
22 changes: 22 additions & 0 deletions man/RequireOptions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading