4 changed files with 122 additions and 9 deletions
			
			
		- 
					1src/storage.rs
 - 
					1src/symbols/mod.rs
 - 
					94src/symbols/stored_directory.rs
 - 
					35src/symbols/systemd/node_js_user_service.rs
 
@ -0,0 +1,94 @@ | 
				
			|||
use std::borrow::{Borrow, Cow};
 | 
				
			|||
use std::error::Error;
 | 
				
			|||
use std::fmt;
 | 
				
			|||
use std::fs;
 | 
				
			|||
use std::io;
 | 
				
			|||
use std::path::Path;
 | 
				
			|||
use std::str::FromStr;
 | 
				
			|||
 | 
				
			|||
use command_runner::CommandRunner;
 | 
				
			|||
use resources::Resource;
 | 
				
			|||
use symbols::Symbol;
 | 
				
			|||
use storage::Storage;
 | 
				
			|||
 | 
				
			|||
#[derive(Debug, PartialEq)]
 | 
				
			|||
pub enum StorageDirection { load, save }
 | 
				
			|||
 | 
				
			|||
pub struct StoredDirectory<'a, S> where S: Storage {
 | 
				
			|||
  path: Cow<'a, str>,
 | 
				
			|||
  storage: S,
 | 
				
			|||
  dir: StorageDirection,
 | 
				
			|||
  command_runner: &'a CommandRunner
 | 
				
			|||
}
 | 
				
			|||
 | 
				
			|||
impl<'a, S> StoredDirectory<'a, S> where S: Storage {
 | 
				
			|||
  pub fn new(path: Cow<'a, str>, storage: S, dir: StorageDirection, command_runner: &'a CommandRunner) -> Self {
 | 
				
			|||
    StoredDirectory {
 | 
				
			|||
      path: path,
 | 
				
			|||
      storage: storage,
 | 
				
			|||
      dir: dir,
 | 
				
			|||
      command_runner: command_runner
 | 
				
			|||
    }
 | 
				
			|||
  }
 | 
				
			|||
}
 | 
				
			|||
 | 
				
			|||
impl<'a, S> fmt::Display for StoredDirectory<'a, S> where S: Storage {
 | 
				
			|||
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 | 
				
			|||
    write!(f, "Stored directory {} ({:?})", self.path, self.dir)
 | 
				
			|||
  }
 | 
				
			|||
}
 | 
				
			|||
 | 
				
			|||
impl<'a, S> Symbol for StoredDirectory<'a, S> where S: Storage {
 | 
				
			|||
  fn target_reached(&self) -> Result<bool, Box<Error>> {
 | 
				
			|||
    let metadata = fs::metadata(self.path.as_ref());
 | 
				
			|||
    // Check if dir exists
 | 
				
			|||
    if let Err(e) = metadata {
 | 
				
			|||
      return if e.kind() == io::ErrorKind::NotFound {
 | 
				
			|||
        Ok(self.dir == StorageDirection::save)
 | 
				
			|||
      } else {
 | 
				
			|||
        Err(Box::new(e))
 | 
				
			|||
      };
 | 
				
			|||
    }
 | 
				
			|||
    if !metadata.unwrap().is_dir() {
 | 
				
			|||
      return Err(Box::new(io::Error::new(io::ErrorKind::AlreadyExists, "Could not create a directory, non-directory file exists")));
 | 
				
			|||
    }
 | 
				
			|||
 | 
				
			|||
    let dump_date = try!(self.storage.recent_date());
 | 
				
			|||
    let output = try!(self.command_runner.run_with_args("sh", &["-c", &format!("find {} -printf '%T@\\n' | sort -r | head -n1 | grep '^[0-9]\\+' -o", self.path)]));
 | 
				
			|||
    if output.status.code() != Some(0) {
 | 
				
			|||
      return Err(try!(String::from_utf8(output.stderr)).into());
 | 
				
			|||
    }
 | 
				
			|||
    let modified_date = try!(u64::from_str(try!(String::from_utf8(output.stdout)).trim_right()));
 | 
				
			|||
    Ok(if self.dir == StorageDirection::save { modified_date <= dump_date } else { dump_date <= modified_date })
 | 
				
			|||
  }
 | 
				
			|||
 | 
				
			|||
  fn execute(&self) -> Result<(), Box<Error>> {
 | 
				
			|||
    if self.dir == StorageDirection::load {
 | 
				
			|||
      try!(self.command_runner.run_with_args("cp", &["-a", &try!(self.storage.read_filename()), self.path.borrow()]));
 | 
				
			|||
    } else {
 | 
				
			|||
      try!(self.command_runner.run_with_args("cp", &["-a", self.path.borrow(), &self.storage.write_filename()]));
 | 
				
			|||
    }
 | 
				
			|||
    Ok(())
 | 
				
			|||
  }
 | 
				
			|||
 | 
				
			|||
  fn get_prerequisites(&self) -> Vec<Resource> {
 | 
				
			|||
    if self.dir == StorageDirection::save { return vec![]; }
 | 
				
			|||
    if let Some(parent) = Path::new(self.path.as_ref()).parent() {
 | 
				
			|||
      vec![ Resource::new("dir", parent.to_string_lossy()) ]
 | 
				
			|||
    } else {
 | 
				
			|||
      vec![]
 | 
				
			|||
    }
 | 
				
			|||
  }
 | 
				
			|||
 | 
				
			|||
  fn provides(&self) -> Option<Vec<Resource>> {
 | 
				
			|||
    if self.dir == StorageDirection::load {
 | 
				
			|||
      Some(vec![ Resource::new("dir", self.path.to_string()) ])
 | 
				
			|||
    } else {
 | 
				
			|||
      None
 | 
				
			|||
    }
 | 
				
			|||
  }
 | 
				
			|||
}
 | 
				
			|||
 | 
				
			|||
#[cfg(test)]
 | 
				
			|||
mod test {
 | 
				
			|||
}
 | 
				
			|||
						Write
						Preview
					
					
					Loading…
					
					Cancel
						Save
					
		Reference in new issue