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 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 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
257 #[tokio::test]
258 async fn install_binary_rock() {
259 let rock = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
260 .join("resources/test/sample-project-0.1.0-1.all.rock");
261 let content = std::fs::read(rock).unwrap();
262 let rock_bytes = Bytes::copy_from_slice(&content);
263 let packed_rock_file_name = "sample-project-0.1.0-1.all.rock".to_string();
264 let cursor = Cursor::new(rock_bytes.clone());
265 let mut zip = zip::ZipArchive::new(cursor).unwrap();
266 let manifest_index = zip.index_for_path("rock_manifest").unwrap();
267 let mut manifest_file = zip.by_index(manifest_index).unwrap();
268 let mut content = String::new();
269 manifest_file.read_to_string(&mut content).unwrap();
270 let rock = DownloadedPackedRockBytes {
271 name: "sample-project".into(),
272 version: "0.1.0-1".parse().unwrap(),
273 bytes: rock_bytes,
274 file_name: packed_rock_file_name.clone(),
275 url: "https://test.org".parse().unwrap(),
276 };
277 let rockspec = unpack_rockspec(&rock).await.unwrap();
278 let install_root = assert_fs::TempDir::new().unwrap();
279 let config = ConfigBuilder::new()
280 .unwrap()
281 .user_tree(Some(install_root.to_path_buf()))
282 .build()
283 .unwrap();
284 let tree = config
285 .user_tree(config.lua_version().unwrap().clone())
286 .unwrap();
287 let local_package = BinaryRockInstall::new(
288 &rockspec,
289 RemotePackageSource::Test,
290 rock.bytes,
291 tree::EntryType::Entrypoint,
292 &config,
293 &tree,
294 )
295 .install()
296 .await
297 .unwrap();
298 let foo_bar_module = tree
299 .layout_for(&local_package)
300 .src
301 .join("foo")
302 .join("bar.lua");
303 assert!(foo_bar_module.is_file());
304 }
305
306 #[cfg(all(target_os = "linux", target_arch = "x86_64"))]
317 #[tokio::test]
318 async fn install_binary_rock_roundtrip() {
319 use crate::operations::{Pack, Uninstall};
320
321 if std::env::var("LUX_SKIP_IMPURE_TESTS").unwrap_or("0".into()) == "1" {
322 println!("Skipping impure test");
323 return;
324 }
325 let rock = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
326 .join("resources/test/toml-edit-0.6.0-1.linux-x86_64.rock");
327 let content = std::fs::read(rock).unwrap();
328 let rock_bytes = Bytes::copy_from_slice(&content);
329 let packed_rock_file_name = "toml-edit-0.6.0-1.linux-x86_64.rock".to_string();
330 let cursor = Cursor::new(rock_bytes.clone());
331 let mut zip = zip::ZipArchive::new(cursor).unwrap();
332 let manifest_index = zip.index_for_path("rock_manifest").unwrap();
333 let mut manifest_file = zip.by_index(manifest_index).unwrap();
334 let mut content = String::new();
335 manifest_file.read_to_string(&mut content).unwrap();
336 let orig_manifest = RockManifest::new(&content).unwrap();
337 let rock = DownloadedPackedRockBytes {
338 name: "toml-edit".into(),
339 version: "0.6.0-1".parse().unwrap(),
340 bytes: rock_bytes,
341 file_name: packed_rock_file_name.clone(),
342 url: "https://test.org".parse().unwrap(),
343 };
344 let rockspec = unpack_rockspec(&rock).await.unwrap();
345 let install_root = assert_fs::TempDir::new().unwrap();
346 let config = ConfigBuilder::new()
347 .unwrap()
348 .user_tree(Some(install_root.to_path_buf()))
349 .build()
350 .unwrap();
351 let tree = config
352 .user_tree(config.lua_version().unwrap().clone())
353 .unwrap();
354 let local_package = BinaryRockInstall::new(
355 &rockspec,
356 RemotePackageSource::Test,
357 rock.bytes,
358 tree::EntryType::Entrypoint,
359 &config,
360 &tree,
361 )
362 .install()
363 .await
364 .unwrap();
365 let layout = tree.layout_for(&local_package);
366 assert!(layout.lib.join("toml_edit.so").is_file());
367
368 let orig_install_tree_integrity = layout.root.hash().await.unwrap();
369
370 let pack_dest_dir = assert_fs::TempDir::new().unwrap();
371 let packed_rock = Pack::new(
372 pack_dest_dir.to_path_buf(),
373 tree.clone(),
374 local_package.clone(),
375 )
376 .pack()
377 .await
378 .unwrap();
379 assert_eq!(
380 packed_rock
381 .file_name()
382 .unwrap()
383 .to_string_lossy()
384 .to_string(),
385 packed_rock_file_name.clone()
386 );
387
388 Uninstall::new()
390 .config(&config)
391 .package(local_package.id())
392 .remove()
393 .await
394 .unwrap();
395 let content = std::fs::read(&packed_rock).unwrap();
396 let rock_bytes = Bytes::copy_from_slice(&content);
397 let cursor = Cursor::new(rock_bytes.clone());
398 let mut zip = zip::ZipArchive::new(cursor).unwrap();
399 let manifest_index = zip.index_for_path("rock_manifest").unwrap();
400 let mut manifest_file = zip.by_index(manifest_index).unwrap();
401 let mut content = String::new();
402 manifest_file.read_to_string(&mut content).unwrap();
403 let packed_manifest = RockManifest::new(&content).unwrap();
404 assert_eq!(packed_manifest, orig_manifest);
405 let rock = DownloadedPackedRockBytes {
406 name: "toml-edit".into(),
407 version: "0.6.0-1".parse().unwrap(),
408 bytes: rock_bytes,
409 file_name: packed_rock_file_name.clone(),
410 url: "https://test.org".parse().unwrap(),
411 };
412 let rockspec = unpack_rockspec(&rock).await.unwrap();
413 let local_package = BinaryRockInstall::new(
414 &rockspec,
415 RemotePackageSource::Test,
416 rock.bytes,
417 tree::EntryType::Entrypoint,
418 &config,
419 &tree,
420 )
421 .install()
422 .await
423 .unwrap();
424 let layout = tree.layout_for(&local_package);
425 assert!(layout.rockspec_path().is_file());
426 let new_install_tree_integrity = layout.root.hash().await.unwrap();
427 assert_eq!(orig_install_tree_integrity, new_install_tree_integrity);
428 }
429}