You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
videl/src/resolve.rs

60 lines
1.4 KiB

//! Resolution
//!
//! Pathname and filename encodings and such
use super::*;
use std::{
path::{
Path,
},
io::{
self,
},
os::unix::ffi::OsStrExt as _, //idc about windows
marker::Unpin,
};
use tokio::{
prelude::*,
io::AsyncWrite,
};
#[cfg(not(feature="fast_pathnames"))]
use sha2::{
Sha256, Digest,
};
/// Encode a pathname to an output buffer
///
/// # Notes
/// This only gets the final part of the encoded pathname. You should prepend the parent afterwards.
pub async fn encode_pathname_to<P: AsRef<Path>, W: AsyncWrite+Unpin>(from: P, mut to: W) -> io::Result<usize>
{
let string = encode_pathname(from);
to.write_all(string.as_bytes()).await?;
Ok(string.len())
}
/// Encode a pathname to a string
///
/// # Notes
/// This only gets the final part of the encoded pathname. You should prepend the parent afterwards.
pub fn encode_pathname<P: AsRef<Path>>(from: P) -> String
{
cfg_if! {
if #[cfg(feature="fast_pathnames")] {
base64::encode(from.as_ref().as_os_str().as_bytes())
} else {
let mut hasher = Sha256::new();
hasher.update(from.as_ref().as_os_str().as_bytes());
hasher.finalize().as_hex().to_hex_string()
}
}
}
//TODO: `deocode_pathname`. Takes input pathname (the full encoded one), and either:
// - fast_pathnames: Decodes the final part into the pathname
// - not(fast_pathnames): Looks in `metadata` to find the correct pathname, and validates the hash