lux_lib/luarocks/
install_binary_rock.rs

1use std::{
2    collections::HashMap,
3    io::{self, Cursor},
4    path::{Path, PathBuf},
5};
6
7use bytes::Bytes;
8use tempdir::TempDir;
9use thiserror::Error;
10
11use crate::{
12    build::{
13        external_dependency::{ExternalDependencyError, ExternalDependencyInfo},
14        utils::recursive_copy_dir,
15        BuildBehaviour,
16    },
17    config::Config,
18    hash::HasIntegrity,
19    lockfile::{
20        LocalPackage, LocalPackageHashes, LockConstraint, LockfileError, OptState, PinnedState,
21    },
22    lua_rockspec::{LuaVersionError, RemoteLuaRockspec},
23    luarocks::rock_manifest::RockManifest,
24    package::PackageSpec,
25    progress::{Progress, ProgressBar},
26    remote_package_source::RemotePackageSource,
27    rockspec::Rockspec,
28    tree::{self, Tree, TreeError},
29};
30use crate::{lockfile::RemotePackageSourceUrl, rockspec::LuaVersionCompatibility};
31
32use super::rock_manifest::RockManifestError;
33
34#[derive(Error, Debug)]
35pub enum InstallBinaryRockError {
36    #[error("IO operation failed: {0}")]
37    Io(#[from] io::Error),
38    #[error(transparent)]
39    Lockfile(#[from] LockfileError),
40    #[error(transparent)]
41    Tree(#[from] TreeError),
42    #[error(transparent)]
43    ExternalDependencyError(#[from] ExternalDependencyError),
44    #[error(transparent)]
45    LuaVersionError(#[from] LuaVersionError),
46    #[error("failed to unpack packed rock: {0}")]
47    Zip(#[from] zip::result::ZipError),
48    #[error("rock_manifest not found. Cannot install rock files that were packed using LuaRocks version 1")]
49    RockManifestNotFound,
50    #[error(transparent)]
51    RockManifestError(#[from] RockManifestError),
52    #[error(
53        "the entry {0} listed in the `rock_manifest` is neither a file nor a directory: {1:?}"
54    )]
55    NotAFileOrDirectory(String, std::fs::Metadata),
56}
57
58pub(crate) struct BinaryRockInstall<'a> {
59    rockspec: &'a RemoteLuaRockspec,
60    rock_bytes: Bytes,
61    source: RemotePackageSource,
62    pin: PinnedState,
63    opt: OptState,
64    entry_type: tree::EntryType,
65    constraint: LockConstraint,
66    behaviour: BuildBehaviour,
67    config: &'a Config,
68    tree: &'a Tree,
69    progress: &'a Progress<ProgressBar>,
70}
71
72impl<'a> BinaryRockInstall<'a> {
73    pub(crate) fn new(
74        rockspec: &'a RemoteLuaRockspec,
75        source: RemotePackageSource,
76        rock_bytes: Bytes,
77        entry_type: tree::EntryType,
78        config: &'a Config,
79        tree: &'a Tree,
80        progress: &'a Progress<ProgressBar>,
81    ) -> Self {
82        Self {
83            rockspec,
84            rock_bytes,
85            source,
86            config,
87            tree,
88            progress,
89            constraint: LockConstraint::default(),
90            behaviour: BuildBehaviour::default(),
91            pin: PinnedState::default(),
92            opt: OptState::default(),
93            entry_type,
94        }
95    }
96
97    pub(crate) fn pin(self, pin: PinnedState) -> Self {
98        Self { pin, ..self }
99    }
100
101    pub(crate) fn opt(self, opt: OptState) -> Self {
102        Self { opt, ..self }
103    }
104
105    pub(crate) fn constraint(self, constraint: LockConstraint) -> Self {
106        Self { constraint, ..self }
107    }
108
109    pub(crate) fn behaviour(self, behaviour: BuildBehaviour) -> Self {
110        Self { behaviour, ..self }
111    }
112
113    pub(crate) async fn install(self) -> Result<LocalPackage, InstallBinaryRockError> {
114        let rockspec = self.rockspec;
115        self.progress.map(|p| {
116            p.set_message(format!(
117                "Unpacking and installing {}@{}...",
118                rockspec.package(),
119                rockspec.version()
120            ))
121        });
122        for (name, dep) in rockspec.external_dependencies().current_platform() {
123            let _ = ExternalDependencyInfo::probe(name, dep, self.config.external_deps())?;
124        }
125
126        rockspec.lua_version_matches(self.config)?;
127
128        let hashes = LocalPackageHashes {
129            rockspec: rockspec.hash()?,
130            source: self.rock_bytes.hash()?,
131        };
132        let source_url = match &self.source {
133            RemotePackageSource::LuarocksBinaryRock(url) => {
134                Some(RemotePackageSourceUrl::Url { url: url.clone() })
135            }
136            _ => None,
137        };
138        let mut package = LocalPackage::from(
139            &PackageSpec::new(rockspec.package().clone(), rockspec.version().clone()),
140            self.constraint,
141            rockspec.binaries(),
142            self.source,
143            source_url,
144            hashes,
145        );
146        package.spec.pinned = self.pin;
147        package.spec.opt = self.opt;
148        match self.tree.lockfile()?.get(&package.id()) {
149            Some(package) if self.behaviour == BuildBehaviour::NoForce => Ok(package.clone()),
150            _ => {
151                let unpack_dir = TempDir::new("lux-cli-rock").unwrap().into_path();
152                let cursor = Cursor::new(self.rock_bytes);
153                let mut zip = zip::ZipArchive::new(cursor)?;
154                zip.extract(&unpack_dir)?;
155                let rock_manifest_file = unpack_dir.join("rock_manifest");
156                if !rock_manifest_file.is_file() {
157                    return Err(InstallBinaryRockError::RockManifestNotFound);
158                }
159                let rock_manifest_content = std::fs::read_to_string(rock_manifest_file)?;
160                let output_paths = match self.entry_type {
161                    tree::EntryType::Entrypoint => self.tree.entrypoint(&package)?,
162                    tree::EntryType::DependencyOnly => self.tree.dependency(&package)?,
163                };
164                let rock_manifest = RockManifest::new(&rock_manifest_content)?;
165                install_manifest_entries(
166                    &rock_manifest.lib.entries,
167                    &unpack_dir.join("lib"),
168                    &output_paths.lib,
169                )
170                .await?;
171                install_manifest_entries(
172                    &rock_manifest.lua.entries,
173                    &unpack_dir.join("lua"),
174                    &output_paths.src,
175                )
176                .await?;
177                install_manifest_entries(
178                    &rock_manifest.bin.entries,
179                    &unpack_dir.join("bin"),
180                    &output_paths.bin,
181                )
182                .await?;
183                install_manifest_entries(
184                    &rock_manifest.doc.entries,
185                    &unpack_dir.join("doc"),
186                    &output_paths.doc,
187                )
188                .await?;
189                install_manifest_entries(
190                    &rock_manifest.root.entries,
191                    &unpack_dir,
192                    &output_paths.etc,
193                )
194                .await?;
195                // rename <name>-<version>.rockspec
196                let rockspec_path = output_paths.etc.join(format!(
197                    "{}-{}.rockspec",
198                    package.name(),
199                    package.version()
200                ));
201                if rockspec_path.is_file() {
202                    tokio::fs::copy(&rockspec_path, output_paths.rockspec_path()).await?;
203                    tokio::fs::remove_file(&rockspec_path).await?;
204                }
205                Ok(package)
206            }
207        }
208    }
209}
210
211async fn install_manifest_entries<T>(
212    entry: &HashMap<PathBuf, T>,
213    src: &Path,
214    dest: &Path,
215) -> Result<(), InstallBinaryRockError> {
216    for relative_src_path in entry.keys() {
217        let target = dest.join(relative_src_path);
218        let src_path = src.join(relative_src_path);
219        if src_path.is_dir() {
220            recursive_copy_dir(&src.to_path_buf(), &target).await?;
221        } else if src_path.is_file() {
222            tokio::fs::create_dir_all(target.parent().unwrap()).await?;
223            tokio::fs::copy(src.join(relative_src_path), target).await?;
224        } else {
225            let metadata = std::fs::metadata(&src_path)?;
226            return Err(InstallBinaryRockError::NotAFileOrDirectory(
227                src_path.to_string_lossy().to_string(),
228                metadata,
229            ));
230        }
231    }
232    Ok(())
233}
234
235#[cfg(test)]
236mod test {
237
238    use io::Read;
239
240    use crate::{
241        config::ConfigBuilder,
242        operations::{unpack_rockspec, DownloadedPackedRockBytes, Pack, Remove},
243        progress::MultiProgress,
244    };
245
246    use super::*;
247
248    /// This relatively large integration test case tests the following:
249    ///
250    /// - Install a packed rock that was packed using luarocks 3.11 from the test resources.
251    /// - Pack the rock using our own `Pack` implementation.
252    /// - Verify that the `rock_manifest` entry of the original packed rock and our own packed rock
253    ///   are equal (this means luarocks should be able to install our packed rock).
254    /// - Uninstall the local package.
255    /// - Install the package from our packed rock.
256    /// - Verify that the contents of the install directories when installing from both packed rocks
257    ///   are the same.
258    #[tokio::test]
259    async fn install_binary_rock_roundtrip() {
260        if std::env::var("LUX_SKIP_IMPURE_TESTS").unwrap_or("0".into()) == "1" {
261            println!("Skipping impure test");
262            return;
263        }
264        let content = std::fs::read("resources/test/toml-edit-0.6.0-1.linux-x86_64.rock").unwrap();
265        let rock_bytes = Bytes::copy_from_slice(&content);
266        let packed_rock_file_name = "toml-edit-0.6.0-1.linux-x86_64.rock".to_string();
267        let cursor = Cursor::new(rock_bytes.clone());
268        let mut zip = zip::ZipArchive::new(cursor).unwrap();
269        let manifest_index = zip.index_for_path("rock_manifest").unwrap();
270        let mut manifest_file = zip.by_index(manifest_index).unwrap();
271        let mut content = String::new();
272        manifest_file.read_to_string(&mut content).unwrap();
273        let orig_manifest = RockManifest::new(&content).unwrap();
274        let rock = DownloadedPackedRockBytes {
275            name: "toml-edit".into(),
276            version: "0.6.0-1".parse().unwrap(),
277            bytes: rock_bytes,
278            file_name: packed_rock_file_name.clone(),
279            url: "https://test.org".parse().unwrap(),
280        };
281        let rockspec = unpack_rockspec(&rock).await.unwrap();
282        let install_root = assert_fs::TempDir::new().unwrap();
283        let config = ConfigBuilder::new()
284            .unwrap()
285            .user_tree(Some(install_root.to_path_buf()))
286            .build()
287            .unwrap();
288        let progress = MultiProgress::new();
289        let bar = progress.new_bar();
290        let tree = config
291            .user_tree(config.lua_version().unwrap().clone())
292            .unwrap();
293        let local_package = BinaryRockInstall::new(
294            &rockspec,
295            RemotePackageSource::Test,
296            rock.bytes,
297            tree::EntryType::Entrypoint,
298            &config,
299            &tree,
300            &Progress::Progress(bar),
301        )
302        .install()
303        .await
304        .unwrap();
305        let rock_layout = tree.entrypoint_layout(&local_package);
306        let orig_install_tree_integrity = rock_layout.rock_path.hash().unwrap();
307
308        let pack_dest_dir = assert_fs::TempDir::new().unwrap();
309        let packed_rock = Pack::new(
310            pack_dest_dir.to_path_buf(),
311            tree.clone(),
312            local_package.clone(),
313        )
314        .pack()
315        .await
316        .unwrap();
317        assert_eq!(
318            packed_rock
319                .file_name()
320                .unwrap()
321                .to_string_lossy()
322                .to_string(),
323            packed_rock_file_name.clone()
324        );
325
326        // let's make sure our own pack/unpack implementation roundtrips correctly
327        Remove::new(&config)
328            .package(local_package.id())
329            .remove()
330            .await
331            .unwrap();
332        let content = std::fs::read(&packed_rock).unwrap();
333        let rock_bytes = Bytes::copy_from_slice(&content);
334        let cursor = Cursor::new(rock_bytes.clone());
335        let mut zip = zip::ZipArchive::new(cursor).unwrap();
336        let manifest_index = zip.index_for_path("rock_manifest").unwrap();
337        let mut manifest_file = zip.by_index(manifest_index).unwrap();
338        let mut content = String::new();
339        manifest_file.read_to_string(&mut content).unwrap();
340        let packed_manifest = RockManifest::new(&content).unwrap();
341        assert_eq!(packed_manifest, orig_manifest);
342        let rock = DownloadedPackedRockBytes {
343            name: "toml-edit".into(),
344            version: "0.6.0-1".parse().unwrap(),
345            bytes: rock_bytes,
346            file_name: packed_rock_file_name.clone(),
347            url: "https://test.org".parse().unwrap(),
348        };
349        let rockspec = unpack_rockspec(&rock).await.unwrap();
350        let bar = progress.new_bar();
351        let local_package = BinaryRockInstall::new(
352            &rockspec,
353            RemotePackageSource::Test,
354            rock.bytes,
355            tree::EntryType::Entrypoint,
356            &config,
357            &tree,
358            &Progress::Progress(bar),
359        )
360        .install()
361        .await
362        .unwrap();
363        let rock_layout = tree.entrypoint_layout(&local_package);
364        assert!(rock_layout.rockspec_path().is_file());
365        let new_install_tree_integrity = rock_layout.rock_path.hash().unwrap();
366        assert_eq!(orig_install_tree_integrity, new_install_tree_integrity);
367    }
368}