Skip to content

Experimental: Require.cloneMethod = "hardlinkReadOnly" (opt-in read-only hard-link clones) - #239

Merged
eliotmcintire merged 6 commits into
developmentfrom
proto/readonly-hardlink-clone
Nov 30, 2026
Merged

eliotmcintire merged 6 commits into
developmentfrom
proto/readonly-hardlink-clone

Conversation

@eliotmcintire

@eliotmcintire eliotmcintire commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

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") (or R_REQUIRE_CLONE_METHOD), Require.cloneFrom links 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's inst/ folder are copied.

Technical details

Design. New option Require.cloneMethod = c("copy", "hardlinkReadOnly"), default "copy", also read from R_REQUIRE_CLONE_METHOD through optionsFromEnvVars(). It is a mode of the existing clone path: copyPackageFilesInner() keeps the same withCloneLock(), the same 00CLONE-* working folder and the same final replaceDirByRename(). Only the per-file step changes, in the new linkPackageReadOnly(). 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 from inst/ 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.link each 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 in pak.R stays a copy.

In-place writers checked.

  • file.copy(overwrite = TRUE): returns FALSE with a warning (not an error); the file is not touched. writeBin() and cat(file=) onto a shared file give an error.
  • R CMD INSTALL / 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 with pak::pkg_install("local::<dir>", lib = source) also worked.
  • remove.packages() and unlink(recursive = TRUE) of a cloned package in either library: work; the other library is intact.
  • Loading the package, requireNamespace(), the clone load check (cloneCheck.R): read only; fine.
  • Require itself: the file.copy(overwrite = TRUE) calls in pkgSnapshot.R copy source tarballs, not installed folders; the unzip in Require-helpers.R extracts into a fresh folder; copyPackages-style linkOrCopy() 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):

  • incident reproduction: in-place writes onto a shared file fail and both sizes are unchanged; a plain hard link made without chmod is emptied under both names;
  • the existing two-process race test now runs for both methods (copy: full before/after frame identical as before): no empty files, both libraries load;
  • new version into the source library, then into the clone's library;
  • remove.packages() / unlink() in either library;
  • fallbacks (each asserts the source modes are restored and the source is still writable): link fails, link fails half-way, chmod fails, chmod fails after the first file, a symlink in the package (the copy is writable like the source), a real cross-file-system clone from /dev/shm, Windows;
  • default is "copy", also from the environment variable.
  • New tests on base: fail (the option does not exist, so the clone copies). Full suite on branch: 0 failed, 0 skipped, 1211 passed. Base (origin/development, same machine): 0 failed, 0 skipped, 1128 passed.
  • Docs: 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):

method disk added by the clone clone time
copy 189,328 KB 0.49 s
hardlinkReadOnly 5,016 KB 0.27 s

(du given both libraries counts shared files once; the 5 MB is the new folders and inodes.)

Limitations. Against the default (copy):

  • The source library's R-installed files (R code, compiled code, help, metadata) become read-only, so a hand edit of one fails, there and in every clone. Install, update and remove still work, because R and pak replace a package by renaming its folder (tested above). Nothing writes into these files at run time: of 520 packages installed here, the only one that wrote into its own folder was climateData, into extdata/ (its WAL-mode SQLite index makes -shm/-wal files beside it). extdata/ comes from inst/, 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.
  • Root ignores the write bits, so an in-place write by root (e.g. R in a Docker container) would still empty a linked file in every library that shares it.

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 helpers tinyPkg(), 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) and linkPackageReadOnly() (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

…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
@eliotmcintire eliotmcintire added the enhancement New feature or request label Nov 27, 2026
eliotmcintire and others added 4 commits November 27, 2026 15:19
…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
eliotmcintire marked this pull request as ready for review November 30, 2026 23:17
@eliotmcintire
eliotmcintire merged commit 80e53f4 into development Nov 30, 2026
19 checks passed
@eliotmcintire
eliotmcintire deleted the proto/readonly-hardlink-clone branch November 30, 2026 23:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant