Repository navigation
Experimental: Require.cloneMethod = "hardlinkReadOnly" (opt-in read-only hard-link clones) - #239
Merged
Merged
Conversation
…linking read-only package files (default stays copy) A mode of copyPackageFilesInner(): the same lock, 00CLONE working folder and final rename; only the per-file step becomes chmod-source-read-only + file.link. Falls back to copy per package if chmod or link fails, and always on Windows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Lx3DXeS4gUhjnvsZYVXumT
…d on failure, so the fallback copy keeps the original modes; compare full frame in the copy race test Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Lx3DXeS4gUhjnvsZYVXumT
Conflicts: clonePackages() (legacy path) was deleted in development; kept this branch's copyPackageFilesInner(method =) and the cloneMethod option doc, minus the usePak = FALSE sentence. man/ regenerated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Lx3DXgG6hSA1CRAMhvKajR
…what R installed Only R-made top-level entries (R, libs, help, html, Meta, data, doc, demo, po, exec, DESCRIPTION, NAMESPACE, INDEX) are linked and made read-only. Everything else came from inst/ (data files, databases), which a package may write into at run time -- climateData's WAL-mode SQLite index did -- so it is copied with its own mode and the source copy is never made read-only. About 16% of a typical library (0.18 of 1.13 GB here). New test; bump to 2.1.1.9042. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Lx3DXgG6hSA1CRAMhvKajR
eliotmcintire
marked this pull request as ready for review
November 30, 2026 23:17
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is an experimental, opt-in way to share package files between libraries without copying them. Only the code and help files R itself installs are shared; data files a package ships are still copied. The shared files are made read-only, so a program that tries to write into one fails with an error instead of silently emptying it in every library that shares it. The default is unchanged: packages are copied.
Affects: nothing by default. With
options(Require.cloneMethod = "hardlinkReadOnly")(orR_REQUIRE_CLONE_METHOD),Require.cloneFromlinks the R-installed files of each package (R code, compiled code, help), and those files become read-only in the source library too. Files from a package'sinst/folder are copied.Technical details
Design. New option
Require.cloneMethod = c("copy", "hardlinkReadOnly"), default"copy", also read fromR_REQUIRE_CLONE_METHODthroughoptionsFromEnvVars(). It is a mode of the existing clone path:copyPackageFilesInner()keeps the samewithCloneLock(), the same00CLONE-*working folder and the same finalreplaceDirByRename(). Only the per-file step changes, in the newlinkPackageReadOnly(). It links only the top-level entries R CMD INSTALL makes itself (.cloneLinkableTop: R, libs, help, html, Meta, data, doc, demo, po, exec, DESCRIPTION, NAMESPACE, INDEX); everything else came frominst/and is copied with its own mode, its source never chmodded (16% of a 1.13 GB library here, 49 MB of it climateData's tile index). For the linked files:file.linkeach source file into the working folder, then take the write bits off the source files (Sys.chmod). Link comes first because a link shares the inode (the mode applies under both names) and a failed link then changes no mode; if chmod fails, the modes of exactly the files it changed are put back, so the fallback copy has the original modes. Directories are made new, not linked. Permissions belong to the inode, so the file is read-only under every name. Falls back to copying that package (nothing half-linked is left) if the package holds a symlink, chmod is refused, or a link fails (other file system). Never used on Windows. The pak bootstrap copy inpak.Rstays a copy.In-place writers checked.
file.copy(overwrite = TRUE): returns FALSE with a warning (not an error); the file is not touched.writeBin()andcat(file=)onto a shared file give an error.install.packages()of a newer version into the SOURCE library, while its files are read-only and linked: works. R renames the old folder aside and removes it, which needs only a writable folder. The clone keeps the old version, intact and loadable; its files' link count drops to 1. The same ad hoc check withpak::pkg_install("local::<dir>", lib = source)also worked.remove.packages()andunlink(recursive = TRUE)of a cloned package in either library: work; the other library is intact.requireNamespace(), the clone load check (cloneCheck.R): read only; fine.file.copy(overwrite = TRUE)calls inpkgSnapshot.Rcopy source tarballs, not installed folders; theunzipinRequire-helpers.Rextracts into a fresh folder;copyPackages-stylelinkOrCopy()is not called by Require. Nothing in Require writes into an installed package folder in place.Tests (
test-25cloneFrom_testthat.R;NOT_CRAN=true; scratch library, temp libraries only):remove.packages()/unlink()in either library;/dev/shm, Windows;"copy", also from the environment variable.devtools::document()run;R CMD check --no-manual: 0 errors, 0 warnings, 0 notes (scratch library; the check also ran the tests).Space and time (20 packages, 13,280 files, 189 MB source, temporary source library, one run each, Linux):
(
dugiven both libraries counts shared files once; the 5 MB is the new folders and inodes.)Limitations. Against the default (copy):
extdata/(its WAL-mode SQLite index makes-shm/-walfiles beside it).extdata/comes frominst/, so it is now copied, not linked, and climateData works unchanged; Read the bundled tile index without writing beside it climateData#43 stops those writes anyway.The rest is the same as copying: a package that cannot be linked or chmodded (other file system, Windows, a symlink, a file we don't own) is copied and the source modes are restored; once either library reinstalls a package, it has its own files again. Copying 20 packages already takes only about 0.5 s, so the saving is mostly disk.
Reuses:
copyPackageFilesInner(),withCloneLock(),replaceDirByRename(),isWindows(),optionsFromEnvVars()/RequireOptions(), and the test helperstinyPkg(),cloneFromLib(),statFiles(),pkgFiles(),tinyRepo()and the existing race test (now run per method); new:cloneMethod()(validates the option and returns copy on Windows; nothing existing does this) andlinkPackageReadOnly()(the chmod-and-link step; the only existing linker,linkOrCopy(), links without chmod, is for SpaDES.project and falls back to symlink).🤖 Generated with Claude Code
https://claude.ai/code/session_01Lx3DXeS4gUhjnvsZYVXumT