MCPcopy Index your code
hub / github.com/cargo-lambda/cargo-lambda / zip_binary

Function zip_binary

crates/cargo-lambda-build/src/archive.rs:257–322  ·  view source on GitHub ↗

Create a zip file from a function binary. The binary inside the zip file is called `bootstrap` for function binaries. The binary inside the zip file is called by its name, and put inside the `extensions` directory, for extension binaries.

(
    binary_path: BP,
    destination_directory: DD,
    data: &BinaryData,
    include: Option<Vec<String>>,
)

Source from the content-addressed store, hash-verified

255/// The binary inside the zip file is called by its name, and put inside the `extensions`
256/// directory, for extension binaries.
257pub fn zip_binary<BP: AsRef<Path>, DD: AsRef<Path>>(
258 binary_path: BP,
259 destination_directory: DD,
260 data: &BinaryData,
261 include: Option<Vec<String>>,
262) -> Result<BinaryArchive> {
263 let path = binary_path.as_ref();
264 let dir = destination_directory.as_ref();
265
266 let zipped = dir.join(data.zip_name());
267 debug!(?data, ?path, ?dir, ?zipped, "zipping binary");
268
269 let zipped_binary = File::create(&zipped)
270 .into_diagnostic()
271 .wrap_err_with(|| format!("failed to create zip file `{zipped:?}`"))?;
272
273 let mut file = File::open(path)
274 .into_diagnostic()
275 .wrap_err_with(|| format!("failed to open binary file `{path:?}`"))?;
276
277 let file_metadata = file
278 .metadata()
279 .into_diagnostic()
280 .wrap_err_with(|| format!("failed to get metadata from file `{path:?}`"))?;
281
282 let binary_modified_at = file_metadata
283 .modified()
284 .ok()
285 .or_else(|| file_metadata.created().ok());
286
287 let (binary_data, arch) = read_binary(&mut file, path)?;
288
289 let mut zip = ZipWriter::new(zipped_binary);
290 if let Some(files) = include {
291 include_files_in_zip(&mut zip, &files)?;
292 }
293
294 if let Some(parent) = data.parent_dir() {
295 let options = SimpleFileOptions::default();
296 zip.add_directory(parent, options)
297 .into_diagnostic()
298 .wrap_err_with(|| {
299 format!("failed to add directory `{parent}` to zip file `{zipped:?}`")
300 })?;
301 }
302
303 let binary_path_in_zip = data.binary_path_in_zip()?;
304
305 let options = zip_file_options(&file, path)?;
306
307 zip.start_file(binary_path_in_zip.to_string(), options)
308 .into_diagnostic()
309 .wrap_err_with(|| format!("failed to start zip file `{binary_path_in_zip:?}`"))?;
310 zip.write_all(&binary_data)
311 .into_diagnostic()
312 .wrap_err_with(|| format!("failed to write data into zip file `{binary_path_in_zip:?}`"))?;
313 zip.finish()
314 .into_diagnostic()

Callers 9

create_binary_archiveFunction · 0.85
test_zip_funcionFunction · 0.85
test_zip_extensionFunction · 0.85
test_consistent_hashFunction · 0.85
runFunction · 0.85
load_archiveFunction · 0.85

Calls 10

createFunction · 0.85
read_binaryFunction · 0.85
newFunction · 0.85
include_files_in_zipFunction · 0.85
zip_file_optionsFunction · 0.85
BinaryModifiedAtClass · 0.85
parent_dirMethod · 0.80
binary_path_in_zipMethod · 0.80
finishMethod · 0.80
zip_nameMethod · 0.45

Tested by 6

test_zip_funcionFunction · 0.68
test_zip_extensionFunction · 0.68
test_consistent_hashFunction · 0.68