|
| 1 | +use std::io::Cursor; |
| 2 | +use std::io::Read; |
| 3 | + |
| 4 | +use flate2::read::GzDecoder; |
| 5 | +use oak_fs::file_lock::FileLock; |
| 6 | + |
| 7 | +use crate::download::Outcome; |
| 8 | + |
| 9 | +/// Names of the R base packages, i.e. everything that ships with R and carries |
| 10 | +/// `Priority: base` in its DESCRIPTION. |
| 11 | +pub(crate) const BASE_PACKAGES: &[&str] = &[ |
| 12 | + "base", |
| 13 | + "compiler", |
| 14 | + "datasets", |
| 15 | + "graphics", |
| 16 | + "grDevices", |
| 17 | + "grid", |
| 18 | + "methods", |
| 19 | + "parallel", |
| 20 | + "splines", |
| 21 | + "stats", |
| 22 | + "stats4", |
| 23 | + "tcltk", |
| 24 | + "tools", |
| 25 | + "utils", |
| 26 | +]; |
| 27 | + |
| 28 | +/// Download the R source tarball for R {version} from CRAN's archive. |
| 29 | +/// |
| 30 | +/// Base R packages (e.g. `base`, `utils`, `stats`) are not distributed at the standard |
| 31 | +/// `src/contrib/` location on CRAN. Instead, we must retrieve them from the base R |
| 32 | +/// sources themselves, which lives at `src/base/R-{major}/R-{version}.tar.gz`. Each |
| 33 | +/// package is located inside that tarball at `src/library/{package}/`. |
| 34 | +/// |
| 35 | +/// Returns `Ok(None)` if the tarball is not on CRAN (e.g. a development R version), which |
| 36 | +/// we treat as "source unavailable" rather than an error. |
| 37 | +pub(crate) fn download(version: &str) -> anyhow::Result<Option<Vec<u8>>> { |
| 38 | + let major = version |
| 39 | + .split('.') |
| 40 | + .next() |
| 41 | + .ok_or_else(|| anyhow::anyhow!("Invalid R version for base source download: {version}"))?; |
| 42 | + |
| 43 | + let mirrors = ["https://cran.r-project.org", "https://cran.rstudio.com"]; |
| 44 | + let suffix = format!("src/base/R-{major}/R-{version}.tar.gz"); |
| 45 | + |
| 46 | + match crate::download::download_with_mirrors(&suffix, &mirrors)? { |
| 47 | + Outcome::Success(response) => { |
| 48 | + let mut bytes = Vec::new(); |
| 49 | + response.into_body().into_reader().read_to_end(&mut bytes)?; |
| 50 | + Ok(Some(bytes)) |
| 51 | + }, |
| 52 | + Outcome::NotFound => Ok(None), |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/// Extract a single base package's R files from the R source tarball bytes. |
| 57 | +/// |
| 58 | +/// Writes `R-{version}/src/library/{package}/R/*.R` entries into an `R/` folder inside |
| 59 | +/// the directory `destination_lock` lives in. Files are marked read only to match the |
| 60 | +/// rest of the cache. |
| 61 | +pub(crate) fn extract( |
| 62 | + package: &str, |
| 63 | + version: &str, |
| 64 | + bytes: &[u8], |
| 65 | + destination_lock: &FileLock, |
| 66 | +) -> anyhow::Result<()> { |
| 67 | + let destination = destination_lock.parent().join("R"); |
| 68 | + std::fs::create_dir(&destination)?; |
| 69 | + |
| 70 | + let cursor = Cursor::new(bytes); |
| 71 | + let gz = GzDecoder::new(cursor); |
| 72 | + let mut archive = tar::Archive::new(gz); |
| 73 | + |
| 74 | + let prefix = format!("R-{version}/src/library/{package}/R/"); |
| 75 | + |
| 76 | + for entry in archive.entries()? { |
| 77 | + let mut entry = entry?; |
| 78 | + let path = entry.path()?; |
| 79 | + |
| 80 | + let Some(relative) = path.strip_prefix(&prefix).ok() else { |
| 81 | + continue; |
| 82 | + }; |
| 83 | + |
| 84 | + if relative |
| 85 | + .extension() |
| 86 | + .is_none_or(|ext| ext != "R" && ext != "r") |
| 87 | + { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + let absolute = destination.join(relative); |
| 92 | + |
| 93 | + // Some base packages (e.g. `utils`) have platform-specific subdirs under `R/` |
| 94 | + // like `R/windows/` and `R/unix/` (their `Makefile` handles them at install |
| 95 | + // time). Create parents if one is required so `unpack()` can write nested files. |
| 96 | + if let Some(parent) = relative.parent().filter(|p| !p.as_os_str().is_empty()) { |
| 97 | + std::fs::create_dir_all(destination.join(parent))?; |
| 98 | + } |
| 99 | + |
| 100 | + entry.unpack(&absolute)?; |
| 101 | + crate::fs::set_readonly(&absolute)?; |
| 102 | + } |
| 103 | + |
| 104 | + Ok(()) |
| 105 | +} |
| 106 | + |
| 107 | +#[cfg(test)] |
| 108 | +mod tests { |
| 109 | + use oak_fs::file_lock::Filesystem; |
| 110 | + use tempfile::TempDir; |
| 111 | + |
| 112 | + use crate::base::download; |
| 113 | + use crate::base::extract; |
| 114 | + |
| 115 | + /// Requires internet access and downloads a large tarball of the R sources |
| 116 | + #[ignore = "Downloads a 40mb tarball"] |
| 117 | + #[test] |
| 118 | + fn test_base_download_and_extract() { |
| 119 | + let bytes = download("4.5.0").unwrap().expect("R 4.5.0 source to exist"); |
| 120 | + |
| 121 | + let destination_tempdir = TempDir::new().unwrap(); |
| 122 | + let destination = Filesystem::new(destination_tempdir.path().to_path_buf()); |
| 123 | + let destination_lock = destination.open_rw_exclusive_create(".lock").unwrap(); |
| 124 | + |
| 125 | + extract("utils", "4.5.0", &bytes, &destination_lock).unwrap(); |
| 126 | + |
| 127 | + // Spot check: `utils` has a well-known `help.R` file |
| 128 | + let help = destination_lock.parent().join("R").join("help.R"); |
| 129 | + assert!(help.exists()); |
| 130 | + assert!(help.metadata().unwrap().permissions().readonly()); |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn test_base_download_unknown_version_returns_none() { |
| 135 | + let bytes = download("0.0.0").unwrap(); |
| 136 | + assert!(bytes.is_none()); |
| 137 | + } |
| 138 | +} |
0 commit comments