subcomponent 0.1.0

A components orchestrator
/*
 * Copyright (c) 2017 Jean Guyomarc'h
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

extern crate std;
use subprocess;
use unpacker;

struct Bzip2 {
}

impl unpacker::Unpacker for Bzip2 {
   fn unpack(&self, in_file: &std::path::Path, out_dir: &std::path::Path) -> Result<std::path::PathBuf, unpacker::Error> {
      info!("Unpacking (bzip2) {:?} in {:?}", in_file, out_dir);
      unpacker::check_unpack_args(in_file, out_dir)?;

      let in_path_str = match in_file.to_str() {
         Some(path) => path,
         None => { return Err(unpacker::PathError); }
      };

      /* Create the output directory if it does not exist */
      if ! out_dir.exists() {
         std::fs::create_dir_all(out_dir)?;
      }

      /* We will write a single file in out_dir with the basename (without
       * extension) of in_file */
      let mut output = out_dir.to_path_buf();
      match in_file.file_name() {
         Some(os_str) => {
            output.push(os_str);
         },
         None => { return Err(unpacker::PathError); }
      }
      output.set_extension("");

      let mut cmd = subprocess::new("bzip2");
      cmd.stdout(std::process::Stdio::null());
      cmd.arg("-d");
      cmd.arg(in_path_str);

      subprocess::run(&mut cmd)?;
      Ok(output)
   }
}

pub fn new() -> Box<unpacker::Unpacker> {
   Box::new(Bzip2 {
   })
}

pub fn name_get() -> &'static str {
   "bz2"
}