diff options
author | Jon Moroney <darakian@gmail.com> | 2020-08-05 19:46:51 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-05 19:46:51 -0700 |
commit | be232991e81e28ddcca0d951bedee99575cc5a00 (patch) | |
tree | 46ac3ecb414bf003bdf687d6864534bc3c2dbf5a | |
parent | 513de13295099a9ad0cdff98e35c3130a73daa24 (diff) | |
parent | 46c6e72abfb21bb2204e20de927e83de9ed44b26 (diff) |
Merge pull request #38 from darakian/store-fs-metadata
Add metadata as new entry
-rw-r--r-- | src/fileinfo.rs | 68 | ||||
-rw-r--r-- | src/lib.rs | 4 |
2 files changed, 48 insertions, 24 deletions
diff --git a/src/fileinfo.rs b/src/fileinfo.rs index a0088f4..3b24823 100644 --- a/src/fileinfo.rs +++ b/src/fileinfo.rs @@ -4,7 +4,7 @@ use std::hash::Hasher; use std::path::PathBuf; use std::cmp::Ordering; use std::io::Read; -use std::fs; +use std::fs::{self, Metadata}; const BLOCK_SIZE: usize = 4096; @@ -19,7 +19,7 @@ pub enum HashMode{ pub struct Fileinfo{ full_hash: Option<u128>, partial_hash: Option<u128>, - file_length: u64, + metadata: Metadata, pub(crate) file_paths: Vec<PathBuf>, } @@ -27,44 +27,56 @@ impl Fileinfo{ /// Creates a new Fileinfo collection struct. /// /// # Examples - /// ``` + /// ```no_run /// use std::path::Path; /// use ddh::fileinfo::Fileinfo; + /// use std::fs; /// + /// fn main() -> std::io::Result<()> { /// Fileinfo::new( /// None, /// None, - /// 3, + /// fs::metadata("./foo/bar.txt")?, /// Path::new("./foo/bar.txt").to_path_buf() /// ); + /// Ok(()) + /// } /// ``` - pub fn new(full_hash: Option<u128>, partial_hash: Option<u128>, length: u64, path: PathBuf) -> Self{ - Fileinfo{full_hash: full_hash, partial_hash: partial_hash, file_length: length, file_paths: vec![path]} + pub fn new(full_hash: Option<u128>, partial_hash: Option<u128>, meta: Metadata, path: PathBuf) -> Self{ + Fileinfo{full_hash: full_hash, partial_hash: partial_hash, metadata: meta, file_paths: vec![path]} } /// Gets the length of the files in the current collection. /// /// # Examples - /// ``` + /// ```no_run /// use std::path::Path; /// use ddh::fileinfo::Fileinfo; + /// use std::fs; /// - /// let fi = Fileinfo::new(None, None, 3, Path::new("./foo/bar.txt").to_path_buf()); + /// fn main() -> std::io::Result<()> { + /// let fi = Fileinfo::new(None, None, fs::metadata("./foo/bar.txt")?, Path::new("./foo/bar.txt").to_path_buf()); /// let len = fi.get_length(); /// assert_eq!(3, len); + /// Ok(()) + /// } /// ``` pub fn get_length(&self) -> u64{ - self.file_length + self.metadata.len() } /// Gets the hash of the full file if available. /// /// # Examples - /// ``` + /// ```no_run /// use std::path::Path; /// use ddh::fileinfo::Fileinfo; + /// use std::fs; /// - /// let fi = Fileinfo::new(Some(123), None, 3, Path::new("./foo/bar.txt").to_path_buf()); + /// fn main() -> std::io::Result<()> { + /// let fi = Fileinfo::new(Some(123), None, fs::metadata("./foo/bar.txt")?, Path::new("./foo/bar.txt").to_path_buf()); /// let f_hash = fi.get_full_hash(); /// assert_eq!(Some(123), f_hash); + /// Ok(()) + /// } /// ``` pub fn get_full_hash(&self) -> Option<u128>{ self.full_hash @@ -75,13 +87,17 @@ impl Fileinfo{ /// Gets the hash of the partially read file if available. /// /// # Examples - /// ``` + /// ```no_run /// use std::path::Path; /// use ddh::fileinfo::Fileinfo; + /// use std::fs; /// - /// let fi = Fileinfo::new(None, Some(123), 3, Path::new("./foo/bar.txt").to_path_buf()); + /// fn main() -> std::io::Result<()> { + /// let fi = Fileinfo::new(None, Some(123), fs::metadata("./foo/bar.txt")?, Path::new("./foo/bar.txt").to_path_buf()); /// let p_hash = fi.get_partial_hash(); /// assert_eq!(Some(123), p_hash); + /// Ok(()) + /// } /// ``` pub fn get_partial_hash(&self) -> Option<u128>{ self.partial_hash @@ -92,13 +108,17 @@ impl Fileinfo{ /// Gets a candidate name. This will be the name of the first file inserted into the collection and so can vary. /// /// # Examples - /// ``` + /// ```no_run /// use std::path::Path; /// use ddh::fileinfo::Fileinfo; + /// use std::fs; /// - /// let fi = Fileinfo::new(None, None, 3, Path::new("./foo/bar.txt").to_path_buf()); + /// fn main() -> std::io::Result<()> { + /// let fi = Fileinfo::new(None, None, fs::metadata("./foo/bar.txt")?, Path::new("./foo/bar.txt").to_path_buf()); /// let some_name = fi.get_candidate_name(); - /// assert_eq!("bar.txt", some_name) + /// assert_eq!("bar.txt", some_name); + /// Ok(()) + /// } /// ``` pub fn get_candidate_name(&self) -> &str{ self.file_paths @@ -114,14 +134,18 @@ impl Fileinfo{ /// Gets all paths in the current collection. This can be used to get the names of each file with the string `rsplit("/")` method. /// /// # Examples - /// ``` + /// ```no_run /// use std::path::Path; /// use ddh::fileinfo::Fileinfo; + /// use std::fs; /// - /// let fi = Fileinfo::new(None, None, 3, Path::new("./foo/bar.txt").to_path_buf()); + /// fn main() -> std::io::Result<()> { + /// let fi = Fileinfo::new(None, None, fs::metadata("./foo/bar.txt")?, Path::new("./foo/bar.txt").to_path_buf()); /// let all_files = fi.get_paths(); /// assert_eq!(&vec![Path::new("./foo/bar.txt").to_path_buf()], /// all_files); + /// Ok(()) + /// } /// ``` pub fn get_paths(&self) -> &Vec<PathBuf>{ return &self.file_paths @@ -171,7 +195,7 @@ impl Serialize for Fileinfo{ let mut state = serializer.serialize_struct("Fileinfo", 4)?; state.serialize_field("partial_hash", &self.partial_hash)?; state.serialize_field("full_hash", &self.full_hash)?; - state.serialize_field("file_length", &self.file_length)?; + state.serialize_field("file_length", &self.get_length())?; state.serialize_field("file_paths", &self.file_paths)?; state.end() } @@ -179,7 +203,7 @@ impl Serialize for Fileinfo{ impl PartialEq for Fileinfo{ fn eq(&self, other: &Fileinfo) -> bool { - (self.file_length==other.file_length)&& + (self.get_length()==other.get_length())&& (self.partial_hash==other.partial_hash)&& (self.full_hash==other.full_hash) } @@ -193,7 +217,7 @@ impl PartialOrd for Fileinfo{ } else if self.partial_hash.is_some() && other.partial_hash.is_some(){ Some(self.partial_hash.cmp(&other.partial_hash)) } else { - Some(self.file_length.cmp(&other.file_length)) + Some(self.get_length().cmp(&other.get_length())) } } } @@ -205,7 +229,7 @@ impl Ord for Fileinfo{ } else if self.partial_hash.is_some() && other.partial_hash.is_some(){ self.partial_hash.cmp(&other.partial_hash) } else { - self.file_length.cmp(&other.file_length) + self.get_length().cmp(&other.get_length()) } } } @@ -76,7 +76,7 @@ fn traverse_and_spawn(current_path: &Path, sender: Sender<ChannelPackage>) -> () Fileinfo::new( None, None, - current_path.metadata().expect("Error reading path length").len(), + current_path.metadata().expect("Error reading file metadata"), current_path.to_path_buf() )) ).expect("Error sending new ChannelPackage::Success"); @@ -100,7 +100,7 @@ fn traverse_and_spawn(current_path: &Path, sender: Sender<ChannelPackage>) -> () Fileinfo::new( None, None, - x.metadata().expect("Error reading path length").len(), + current_path.metadata().expect("Error reading file metadata"), x.path())) ).expect("Error sending new ChannelPackage::Success") ); |