Skip to main content

lux_lib/luarocks/
install_binary_rock.rs

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