-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
130 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
mod binary_cache_store; | ||
mod path; | ||
mod path_info; | ||
mod store; | ||
|
||
pub use binary_cache_store::BinaryCacheStore; | ||
pub use path::{StorePath, StorePathHash, StorePathName}; | ||
pub use path_info::PathInfo; | ||
pub use store::{Store, StorePath}; | ||
pub use store::Store; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use crate::error::Error; | ||
use crate::util::base32; | ||
use std::fmt; | ||
use std::path::Path; | ||
|
||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)] | ||
pub struct StorePath { | ||
pub hash: StorePathHash, | ||
pub name: StorePathName, | ||
} | ||
|
||
pub const STORE_PATH_HASH_BYTES: usize = 20; | ||
pub const STORE_PATH_HASH_CHARS: usize = 32; | ||
|
||
impl StorePath { | ||
pub fn new(path: &Path, _store_dir: &str) -> Result<Self, Error> { | ||
// FIXME: check store_dir | ||
Self::new_from_base_name( | ||
path.file_name() | ||
.ok_or(Error::BadStorePath(path.into()))? | ||
.to_str() | ||
.ok_or(Error::BadStorePath(path.into()))?, | ||
) | ||
} | ||
|
||
pub fn new_from_base_name(base_name: &str) -> Result<Self, Error> { | ||
if base_name.len() < STORE_PATH_HASH_CHARS + 2 | ||
|| base_name.as_bytes()[STORE_PATH_HASH_CHARS] != '-' as u8 | ||
{ | ||
return Err(Error::BadStorePath(base_name.into())); | ||
} | ||
|
||
Ok(StorePath { | ||
hash: StorePathHash::new(&base_name[0..STORE_PATH_HASH_CHARS])?, | ||
name: StorePathName::new(&base_name[STORE_PATH_HASH_CHARS + 1..])?, | ||
}) | ||
} | ||
} | ||
|
||
impl fmt::Display for StorePath { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}-{}", self.hash, self.name) | ||
} | ||
} | ||
|
||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)] | ||
pub struct StorePathHash([u8; STORE_PATH_HASH_BYTES]); | ||
|
||
impl StorePathHash { | ||
pub fn new(s: &str) -> Result<Self, Error> { | ||
assert_eq!(s.len(), STORE_PATH_HASH_CHARS); | ||
let v = base32::decode(s)?; | ||
assert_eq!(v.len(), STORE_PATH_HASH_BYTES); | ||
let mut bytes: [u8; 20] = Default::default(); | ||
bytes.copy_from_slice(&v[0..STORE_PATH_HASH_BYTES]); | ||
Ok(Self(bytes)) | ||
} | ||
} | ||
|
||
impl fmt::Display for StorePathHash { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.write_str(&base32::encode(&self.0)) | ||
} | ||
} | ||
|
||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)] | ||
pub struct StorePathName(String); | ||
|
||
impl StorePathName { | ||
pub fn new(s: &str) -> Result<Self, Error> { | ||
if s.len() > 211 { | ||
return Err(Error::StorePathNameTooLong); | ||
} | ||
|
||
if s.starts_with('.') | ||
|| !s.chars().all(|c| { | ||
c.is_ascii_alphabetic() | ||
|| c.is_ascii_digit() | ||
|| c == '+' | ||
|| c == '-' | ||
|| c == '.' | ||
|| c == '_' | ||
|| c == '?' | ||
|| c == '=' | ||
}) | ||
{ | ||
return Err(Error::BadStorePathName); | ||
} | ||
|
||
Ok(Self(s.to_string())) | ||
} | ||
} | ||
|
||
impl fmt::Display for StorePathName { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.write_str(&self.0) | ||
} | ||
} | ||
|
||
// FIXME: add tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters