refactor(nix-compat/store_path): make digest and name private
Change-Id: I62cbe883afcf3dd0c8d4de0e3b845069eb750c97 Reviewed-on: https://cl.tvl.fyi/c/depot/+/9855 Reviewed-by: flokli <flokli@flokli.de> Tested-by: BuildkiteCI
This commit is contained in:
parent
36f2b69de5
commit
7f7c1ae7be
6 changed files with 27 additions and 21 deletions
|
|
@ -61,10 +61,10 @@ impl TvixStoreIO {
|
||||||
sub_path: &Path,
|
sub_path: &Path,
|
||||||
) -> Result<Option<Node>, io::Error> {
|
) -> Result<Option<Node>, io::Error> {
|
||||||
let path_info_service = self.path_info_service.clone();
|
let path_info_service = self.path_info_service.clone();
|
||||||
let digest = store_path.digest;
|
let task = self.tokio_handle.spawn({
|
||||||
let task = self
|
let digest = *store_path.digest();
|
||||||
.tokio_handle
|
async move { path_info_service.get(digest).await }
|
||||||
.spawn(async move { path_info_service.get(digest).await });
|
});
|
||||||
let path_info = match self.tokio_handle.block_on(task).unwrap()? {
|
let path_info = match self.tokio_handle.block_on(task).unwrap()? {
|
||||||
// If there's no PathInfo found, early exit
|
// If there's no PathInfo found, early exit
|
||||||
None => return Ok(None),
|
None => return Ok(None),
|
||||||
|
|
|
||||||
|
|
@ -52,8 +52,18 @@ pub enum Error {
|
||||||
/// path.
|
/// path.
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||||
pub struct StorePath {
|
pub struct StorePath {
|
||||||
pub digest: [u8; DIGEST_SIZE],
|
digest: [u8; DIGEST_SIZE],
|
||||||
pub name: String,
|
name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StorePath {
|
||||||
|
pub fn digest(&self) -> &[u8; DIGEST_SIZE] {
|
||||||
|
&self.digest
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn name(&self) -> &str {
|
||||||
|
self.name.as_ref()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialOrd for StorePath {
|
impl PartialOrd for StorePath {
|
||||||
|
|
@ -187,14 +197,6 @@ pub(crate) fn validate_name(s: &[u8]) -> Result<String, Error> {
|
||||||
Ok(String::from_utf8(s.to_vec()).unwrap())
|
Ok(String::from_utf8(s.to_vec()).unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ensures the StorePath fulfils the requirements for store paths.
|
|
||||||
/// Useful when populating the struct manually instead of parsing.
|
|
||||||
pub fn validate(s: &StorePath) -> Result<(), Error> {
|
|
||||||
validate_name(s.name.as_bytes())?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for StorePath {
|
impl fmt::Display for StorePath {
|
||||||
/// The string representation of a store path starts with a digest (20
|
/// The string representation of a store path starts with a digest (20
|
||||||
/// bytes), [crate::nixbase32]-encoded, followed by a `-`,
|
/// bytes), [crate::nixbase32]-encoded, followed by a `-`,
|
||||||
|
|
|
||||||
|
|
@ -169,9 +169,10 @@ impl TvixStoreFs {
|
||||||
} else {
|
} else {
|
||||||
// If we don't have it, look it up in PathInfoService.
|
// If we don't have it, look it up in PathInfoService.
|
||||||
let path_info_service = self.path_info_service.clone();
|
let path_info_service = self.path_info_service.clone();
|
||||||
let task = self
|
let task = self.tokio_handle.spawn({
|
||||||
.tokio_handle
|
let digest = *store_path.digest();
|
||||||
.spawn(async move { path_info_service.get(store_path.digest).await });
|
async move { path_info_service.get(digest).await }
|
||||||
|
});
|
||||||
match self.tokio_handle.block_on(task).unwrap()? {
|
match self.tokio_handle.block_on(task).unwrap()? {
|
||||||
// the pathinfo doesn't exist, so the file doesn't exist.
|
// the pathinfo doesn't exist, so the file doesn't exist.
|
||||||
None => Ok(None),
|
None => Ok(None),
|
||||||
|
|
|
||||||
|
|
@ -74,7 +74,7 @@ impl PathInfoService for MemoryPathInfoService {
|
||||||
// This overwrites existing PathInfo objects.
|
// This overwrites existing PathInfo objects.
|
||||||
Ok(nix_path) => {
|
Ok(nix_path) => {
|
||||||
let mut db = self.db.write().unwrap();
|
let mut db = self.db.write().unwrap();
|
||||||
db.insert(nix_path.digest, path_info.clone());
|
db.insert(*nix_path.digest(), path_info.clone());
|
||||||
|
|
||||||
Ok(path_info)
|
Ok(path_info)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -119,7 +119,10 @@ impl PathInfoService for SledPathInfoService {
|
||||||
))),
|
))),
|
||||||
// In case the PathInfo is valid, and we were able to extract a NixPath, store it in the database.
|
// In case the PathInfo is valid, and we were able to extract a NixPath, store it in the database.
|
||||||
// This overwrites existing PathInfo objects.
|
// This overwrites existing PathInfo objects.
|
||||||
Ok(nix_path) => match self.db.insert(nix_path.digest, path_info.encode_to_vec()) {
|
Ok(nix_path) => match self
|
||||||
|
.db
|
||||||
|
.insert(*nix_path.digest(), path_info.encode_to_vec())
|
||||||
|
{
|
||||||
Ok(_) => Ok(path_info),
|
Ok(_) => Ok(path_info),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!("failed to insert PathInfo: {}", e);
|
warn!("failed to insert PathInfo: {}", e);
|
||||||
|
|
|
||||||
|
|
@ -130,12 +130,12 @@ impl PathInfo {
|
||||||
// This is safe, because we ensured the proper length earlier already.
|
// This is safe, because we ensured the proper length earlier already.
|
||||||
let reference_digest = self.references[i].to_vec().try_into().unwrap();
|
let reference_digest = self.references[i].to_vec().try_into().unwrap();
|
||||||
|
|
||||||
if reference_names_store_path.digest != reference_digest {
|
if reference_names_store_path.digest() != &reference_digest {
|
||||||
return Err(
|
return Err(
|
||||||
ValidatePathInfoError::InconsistentNarinfoReferenceNameDigest(
|
ValidatePathInfoError::InconsistentNarinfoReferenceNameDigest(
|
||||||
i,
|
i,
|
||||||
reference_digest,
|
reference_digest,
|
||||||
reference_names_store_path.digest,
|
*reference_names_store_path.digest(),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue