MCPcopy Create free account
hub / github.com/cargo-lambda/cargo-lambda / use_zip_in_place

Function use_zip_in_place

crates/cargo-lambda-build/src/archive.rs:207–251  ·  view source on GitHub ↗
(
    zip_path: PathBuf,
    data: &BinaryData<'_>,
    include: Option<Vec<String>>,
)

Source from the content-addressed store, hash-verified

205}
206
207fn use_zip_in_place(
208 zip_path: PathBuf,
209 data: &BinaryData<'_>,
210 include: Option<Vec<String>>,
211) -> Result<BinaryArchive> {
212 let binary_path_in_zip = data.binary_path_in_zip()?;
213 let (arch, binary_modified_at) =
214 extract_data_from_zipped_binary(&zip_path, &binary_path_in_zip)?;
215
216 if let Some(files) = include {
217 let file = File::open(&zip_path)
218 .into_diagnostic()
219 .wrap_err_with(|| format!("failed to open zip file `{zip_path:?}`"))?;
220
221 // Zip2 doesn't support updating zip files in place, so we need to create a new zip file
222 // if we want to include files in the zip archive.
223 // Before we do that, we need to read the existing zip file and copy its contents to a new zip file.
224 let mut archive = ZipArchive::new(file).into_diagnostic()?;
225
226 let tmp_dir = tempfile::tempdir().into_diagnostic()?;
227 let tmp_path = tmp_dir.path().join(data.zip_name());
228 let tmp = File::create(&tmp_path).into_diagnostic()?;
229 let mut zip = ZipWriter::new(tmp);
230
231 for i in 0..archive.len() {
232 let file = archive.by_index_raw(i).into_diagnostic()?;
233 zip.raw_copy_file(file).into_diagnostic()?;
234 }
235
236 include_files_in_zip(&mut zip, &files)?;
237
238 zip.finish()
239 .into_diagnostic()
240 .wrap_err_with(|| format!("failed to finish zip file `{}`", tmp_path.display()))?;
241
242 drop(archive);
243 copy_and_replace(&tmp_path, &zip_path).into_diagnostic()?;
244 }
245
246 Ok(BinaryArchive::new(
247 zip_path.clone(),
248 arch.to_string(),
249 binary_modified_at,
250 ))
251}
252
253/// Create a zip file from a function binary.
254/// The binary inside the zip file is called `bootstrap` for function binaries.

Callers 1

create_binary_archiveFunction · 0.85

Calls 9

newFunction · 0.85
createFunction · 0.85
include_files_in_zipFunction · 0.85
copy_and_replaceFunction · 0.85
binary_path_in_zipMethod · 0.80
pathMethod · 0.80
finishMethod · 0.80
zip_nameMethod · 0.45

Tested by

no test coverage detected