Skip to content

Commit

Permalink
Close #11: Add cmd to verify file existence
Browse files Browse the repository at this point in the history
Signed-off-by: Lee Smet <[email protected]>
  • Loading branch information
LeeSmet committed Dec 22, 2020
1 parent a1a7f68 commit 893b784
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
17 changes: 9 additions & 8 deletions src/etcd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,16 @@ impl Etcd {
}

/// loads the metadata for a given path and prefix
pub async fn load_meta(&self, path: &PathBuf) -> Result<MetaData, String> {
pub async fn load_meta(&self, path: &PathBuf) -> Result<Option<MetaData>, String> {
let key = self.build_key(path)?;
Ok(toml::from_slice(
&self
.read_value(&key)
.await?
.ok_or("no meta found for path".to_string())?,
)
.map_err(|e| e.to_string())?)
Ok(if let Some(value) = self.read_value(&key).await? {
Some(
toml::from_slice(&value)
.map_err(|e| format!("could not decode metadata: {}", e))?,
)
} else {
None
})
}

// helper functions to read and write a value
Expand Down
43 changes: 41 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ enum Cmd {
#[structopt(name = "file", long, short, parse(from_os_str))]
file: std::path::PathBuf,
},
/// Check if a file exists in the backend
///
/// Checks if a valid metadata entry is available for the path. If it is, the hex of the file
/// checksum is printed, as well as the resolved path
Check {
/// Path of the file to check.
///
/// The original path which was used to store the file.
#[structopt(name = "file", long, short, parse(from_os_str))]
file: std::path::PathBuf,
},
}

fn main() -> Result<(), String> {
Expand Down Expand Up @@ -148,7 +159,11 @@ fn main() -> Result<(), String> {
cluster.save_meta(&file, &metadata).compat().await?;
}
Cmd::Retrieve { ref file } => {
let metadata = cluster.load_meta(file).compat().await?;
let metadata = cluster
.load_meta(file)
.compat()
.await?
.ok_or("no metadata found for file")?;
let decoded = recover_data(&metadata).await?;

let encryptor = AESGCM::new(metadata.encryption().key().clone());
Expand All @@ -169,12 +184,36 @@ fn main() -> Result<(), String> {
out.write_all(&original).map_err(|e| e.to_string())?;
}
Cmd::Rebuild { ref file } => {
let metadata = cluster.load_meta(file).compat().await?;
let metadata = cluster
.load_meta(file)
.compat()
.await?
.ok_or("no metadata found for file")?;
let decoded = recover_data(&metadata).await?;

let metadata = store_data(decoded, metadata.checksum().clone(), &cfg).await?;
cluster.save_meta(&file, &metadata).compat().await?;
}
Cmd::Check { ref file } => {
match cluster.load_meta(file).compat().await? {
Some(metadata) => {
let file = canonicalize_path(&file)?;
// strip the virtual_root, if one is set
let actual_path = if let Some(ref virtual_root) = cfg.virtual_root() {
file.strip_prefix(virtual_root)
.map_err(|e| format!("could not strip path prefix: {}", e))?
} else {
file.as_path()
};
println!(
"{}\t{}",
hex::encode(metadata.checksum()),
actual_path.to_string_lossy()
);
}
None => std::process::exit(1),
};
}
};

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/test_etcd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn main() {
let rec = cluster.load_meta(&path).compat().await.unwrap();

log::info!("comparing data");
assert_eq!(&rec, &data);
assert_eq!(&rec.unwrap(), &data);
log::info!("compared data");
});
}

0 comments on commit 893b784

Please sign in to comment.