File: create.rs

package info (click to toggle)
rust-ar 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 192 kB
  • sloc: makefile: 4
file content (42 lines) | stat: -rw-r--r-- 1,136 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//! Creates an archive from one or more input files.
//!
//! To create a new archive, run:
//!
//! ```shell
//! cargo run --example create <path/to/output.a> <path/to/input1> <input2..>
//! ```
//!
//! Assuming the output file doesn't already exist, this is roughly equivalent
//! to running:
//!
//! ```shell
//! ar -cr <path/to/output.a> <path/to/input1> <input2..>
//! ```

extern crate ar;

use std::env;
use std::fs::File;
use std::path::Path;

fn main() {
    let num_args = env::args().count();
    if num_args < 3 {
        println!("Usage: create <outpath> <inpath> [<inpath>...]");
        return;
    }

    let output_path = env::args().nth(1).unwrap();
    let output_path = Path::new(&output_path);
    let output_file =
        File::create(output_path).expect("failed to open output file");
    let mut builder = ar::Builder::new(output_file);

    for index in 2..num_args {
        let input_path = env::args().nth(index).unwrap();
        let input_path = Path::new(&input_path);
        builder
            .append_path(input_path)
            .expect(&format!("failed to add {:?} to archive", input_path));
    }
}