浏览代码

extract: use ar crate instead of command

Alexandre Janniaux 5 年之前
父节点
当前提交
a73697ad7c
共有 1 个文件被更改,包括 25 次插入7 次删除
  1. 25 7
      src/main.rs

+ 25 - 7
src/main.rs

@@ -3,6 +3,9 @@ use std::path::PathBuf;
 use std::process::Command;
 use std::result::Result;
 use structopt::StructOpt;
+use ar::Archive;
+use std::fs::File;
+use std::io;
 
 #[derive(Debug, StructOpt)]
 #[structopt(name = "Localize", about = "Localize a static library into a single object archive")]
@@ -18,18 +21,33 @@ struct LocalizeArgs
 }
 
 fn extract(input: PathBuf, output_directory: PathBuf)
-    -> Result<(), ()>
+    -> Result<Vec<PathBuf>, ()>
 {
     println!("Extracting {:?} into {:?}", input, output_directory);
 
-    let output = Command::new("ar")
-        .arg("x")
-        .arg(input)
-        .current_dir(output_directory)
-        .spawn()
+    let input_file = File::open(input)
         .map_err(|_| ())?;
 
-    Ok(())
+    let mut archive = Archive::new(input_file);
+    while let Some(entry_result) = archive.next_entry()
+    {
+        let mut entry = entry_result.unwrap();
+
+        let identifier = std::str::from_utf8(
+            entry.header().identifier()).unwrap();
+
+        let mut output_filename = output_directory.clone();
+        output_filename.push(identifier);
+
+        println!(" - {:?}", output_filename);
+
+        let mut file = File::create(output_filename)
+            .unwrap();
+
+        io::copy(&mut entry, &mut file);
+    }
+
+    Ok(vec![])
 }
 
 fn link_partial(inputs: Vec<PathBuf>, output: PathBuf)