about summary refs log tree commit diff
path: root/users/zseri/store-ref-scanner/src/spec.rs
diff options
context:
space:
mode:
authorzseri <zseri.devel@ytrizja.de>2021-12-24T19·29+0100
committerzseri <zseri.devel@ytrizja.de>2021-12-25T01·22+0000
commit8cdda57580bb1b4fdb19091c45b437f6327d78c0 (patch)
tree388c06ee055193835fcab0a7791fbd6a40dc49d2 /users/zseri/store-ref-scanner/src/spec.rs
parent979502335b8130599a89e2b3f91aaa974aa6dfe5 (diff)
feat(zseri): Add store-ref-scanner crate r/3383
This crate implements the scanner for finding references to store
paths in uncompressed binary blobs and text files.
It is currently a minimally working prototype and
it is probably a good idea to polish the interface further.

Change-Id: I8406f9d52d254fc3d660ea2b9bc9b7841cc815ec
Reviewed-on: https://cl.tvl.fyi/c/depot/+/4596
Tested-by: BuildkiteCI
Reviewed-by: zseri <zseri.devel@ytrizja.de>
Diffstat (limited to '')
-rw-r--r--users/zseri/store-ref-scanner/src/spec.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/users/zseri/store-ref-scanner/src/spec.rs b/users/zseri/store-ref-scanner/src/spec.rs
new file mode 100644
index 0000000000..034779e8e8
--- /dev/null
+++ b/users/zseri/store-ref-scanner/src/spec.rs
@@ -0,0 +1,46 @@
+use crate::hbm::HalfBytesMask;
+use camino::Utf8PathBuf;
+use once_cell::sync::Lazy;
+
+pub struct StoreSpec {
+    /// path to store without trailing slash
+    pub path_to_store: Utf8PathBuf,
+
+    /// compressed map of allowed ASCII characters in hash part
+    pub valid_hashbytes: HalfBytesMask,
+
+    /// compressed map of allowed ASCII characters in part after hash
+    pub valid_restbytes: HalfBytesMask,
+
+    /// exact length of hash part of store paths
+    pub hashbytes_len: u8,
+}
+
+impl StoreSpec {
+    pub(crate) fn check_rest(&self, rest: &[u8]) -> bool {
+        let hbl = self.hashbytes_len.into();
+        rest.iter()
+            .take(hbl)
+            .take_while(|&&i| self.valid_hashbytes.contains(i))
+            .count()
+            == hbl
+    }
+}
+
+pub static SPEC_DFL_NIX2: Lazy<StoreSpec> = Lazy::new(|| StoreSpec {
+    path_to_store: "/nix/store".into(),
+    valid_hashbytes: HalfBytesMask::B32_REVSHA256,
+    valid_restbytes: HalfBytesMask::from_bytes(
+        b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-._?=",
+    ),
+    hashbytes_len: 32,
+});
+
+pub static SPEC_DFL_YZIX1: Lazy<StoreSpec> = Lazy::new(|| StoreSpec {
+    path_to_store: "/yzixs".into(),
+    valid_hashbytes: HalfBytesMask::B64_BLAKE2B256,
+    valid_restbytes: HalfBytesMask::from_bytes(
+        b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-._?=",
+    ),
+    hashbytes_len: 43,
+});