store_ref_scanner/
lib.rs

1#![no_std]
2#![forbid(clippy::cast_ptr_alignment, trivial_casts, unconditional_recursion)]
3#![deny(clippy::as_conversions)]
4
5mod hbm;
6pub use hbm::HalfBytesMask;
7
8mod spec;
9pub use spec::*;
10
11/// limit maximal length of store basename
12const BASENAME_MAXLEN: usize = 255;
13
14/// this is a trait which implements the interface of possible inputs
15/// (usually byte slices)
16pub trait ScannerInput: AsRef<[u8]> + Sized {
17    /// Splits the input into two at the given index.
18    /// Afterwards self contains elements [at, len), and the returned input part contains elements [0, at).
19    fn split_to(&mut self, at: usize) -> Self;
20    fn finish(&mut self);
21}
22
23impl ScannerInput for &[u8] {
24    fn split_to(&mut self, at: usize) -> Self {
25        let (a, b) = self.split_at(at);
26        *self = b;
27        a
28    }
29
30    fn finish(&mut self) {
31        *self = &[];
32    }
33}
34
35impl ScannerInput for &mut [u8] {
36    fn split_to(&mut self, at: usize) -> Self {
37        // Lifetime dance taken from `impl Write for &mut [u8]`.
38        // Taken from crate `std`.
39        let (a, b) = core::mem::take(self).split_at_mut(at);
40        *self = b;
41        a
42    }
43
44    fn finish(&mut self) {
45        *self = &mut [];
46    }
47}
48
49/// this is the primary structure of this crate
50///
51/// it represents a scanner which scans binary slices for store references,
52/// and implements an iterator interfaces which returns these as byte slices.
53pub struct StoreRefScanner<'x, Input: 'x> {
54    input: Input,
55    spec: &'x StoreSpec<'x>,
56}
57
58impl<'x, Input> StoreRefScanner<'x, Input>
59where
60    Input: ScannerInput + 'x,
61{
62    pub fn new(input: Input, spec: &'x StoreSpec<'x>) -> Self {
63        for i in [&spec.valid_hashbytes, &spec.valid_restbytes] {
64            for j in [b'\0', b' ', b'\t', b'\n', b'/', b'\\'] {
65                assert!(!i.contains(j));
66            }
67        }
68        Self { input, spec }
69    }
70}
71
72impl<'x, Input: 'x> Iterator for StoreRefScanner<'x, Input>
73where
74    Input: ScannerInput + 'x,
75{
76    type Item = Input;
77
78    fn next(&mut self) -> Option<Input> {
79        let hbl: usize = self.spec.hashbytes_len.into();
80        'outer: while !self.input.as_ref().is_empty() {
81            if !self.spec.path_to_store.is_empty() {
82                let p2sas = self.spec.path_to_store;
83                while !self.input.as_ref().starts_with(p2sas.as_bytes()) {
84                    if self.input.as_ref().is_empty() {
85                        break 'outer;
86                    }
87                    self.input.split_to(1);
88                }
89                self.input.split_to(p2sas.len());
90                if self.input.as_ref().is_empty() {
91                    break 'outer;
92                }
93            }
94            let hsep = matches!(self.input.as_ref().iter().next(), Some(b'/') | Some(b'\\'));
95            self.input.split_to(1);
96            if hsep && self.spec.check_rest(self.input.as_ref()) {
97                // we have found a valid hash
98                // rest contains the store basename and all following components
99                // now let's search for the end
100                // and then cut off possible following components after the basename
101                let rlen = self
102                    .input
103                    .as_ref()
104                    .iter()
105                    .enumerate()
106                    .take(BASENAME_MAXLEN)
107                    .skip(hbl)
108                    .find(|&(_, &i)| !self.spec.valid_restbytes.contains(i))
109                    .map(|(eosp, _)| eosp)
110                    .unwrap_or_else(|| core::cmp::min(BASENAME_MAXLEN, self.input.as_ref().len()));
111                return Some(self.input.split_to(rlen));
112            }
113        }
114        self.input.finish();
115        None
116    }
117}
118
119#[cfg(test)]
120mod tests {
121    use super::*;
122    extern crate alloc;
123    use alloc::{vec, vec::Vec};
124
125    #[test]
126    fn simple_nix2() {
127        let drv: &[u8] = br#"
128            Derive([("out","","r:sha256","")],[("/nix/store/2ax7bvjdfkzim69q957i0jlg0nvmapg0-util-linux-2.37.2.drv",["dev"]),("/nix/store/6b55ssmh8pzqsc4q4kw1yl3kqvr4fvqj-bash-5.1-p12.drv",["out"]),("/nix/store/fp2vx24kczlzv84avds28wyzsmrn8kyv-source.drv",["out"]),("/nix/store/s6c2lm5hpsvdwnxq9y1g3ngncghjzc3k-stdenv-linux.drv",["out"]),("/nix/store/xlnzpf4mzghi8vl0krabrgcbnqk5qjf3-pkg-config-wrapper-0.29.2.drv",["out"])],["/nix/store/03sl46khd8gmjpsad7223m32ma965vy9-fix-static.patch","/nix/store/2q3z7587yhlz0i2xvfvvap42zk5carlv-bcache-udev-modern.patch","/nix/store/9krlzvny65gdc8s7kpb6lkx8cd02c25b-default-builder.sh"],"x86_64-linux","/0g15yibzzi3rmw29gqlbms05x9dbghbvh61v1qggydvmzh3bginw/bin/bash",["-e","/nix/store/9krlzvny65gdc8s7kpb6lkx8cd02c25b-default-builder.sh"],[("buildInputs","/0sdk1r4l43yw4g6lmqdhd92vhdfhlwz3m76jxzvzsqsv63czw2km"),("builder","/0g15yibzzi3rmw29gqlbms05x9dbghbvh61v1qggydvmzh3bginw/bin/bash"),("configureFlags",""),("depsBuildBuild",""),("depsBuildBuildPropagated",""),("depsBuildTarget",""),("depsBuildTargetPropagated",""),("depsHostHost",""),("depsHostHostPropagated",""),("depsTargetTarget",""),("depsTargetTargetPropagated",""),("doCheck",""),("doInstallCheck",""),("makeFlags","PREFIX=/1rz4g4znpzjwh1xymhjpm42vipw92pr73vdgl6xs1hycac8kf2n9 UDEVLIBDIR=/1rz4g4znpzjwh1xymhjpm42vipw92pr73vdgl6xs1hycac8kf2n9/lib/udev/"),("name","bcache-tools-1.0.7"),("nativeBuildInputs","/1kw0rwgdyq9q69wmmsa5d2kap6p52b0yldbzi4w17bhcq5g5cp2f"),("out","/1rz4g4znpzjwh1xymhjpm42vipw92pr73vdgl6xs1hycac8kf2n9"),("outputHashAlgo","sha256"),("outputHashMode","recursive"),("outputs","out"),("patches","/nix/store/2q3z7587yhlz0i2xvfvvap42zk5carlv-bcache-udev-modern.patch /nix/store/03sl46khd8gmjpsad7223m32ma965vy9-fix-static.patch"),("pname","bcache-tools"),("preBuild","sed -e \"s|/bin/sh|/0g15yibzzi3rmw29gqlbms05x9dbghbvh61v1qggydvmzh3bginw/bin/sh|\" -i *.rules\n"),("preInstall","mkdir -p \"$out/sbin\" \"$out/lib/udev/rules.d\" \"$out/share/man/man8\"\n"),("prePatch","sed -e \"/INSTALL.*initramfs\\/hook/d\" \\\n    -e \"/INSTALL.*initcpio\\/install/d\" \\\n    -e \"/INSTALL.*dracut\\/module-setup.sh/d\" \\\n    -e \"s/pkg-config/$PKG_CONFIG/\" \\\n    -i Makefile\n"),("propagatedBuildInputs",""),("propagatedNativeBuildInputs",""),("src","/nix/store/6izcafvfcbz19chi7hl20834g0fa043n-source"),("stdenv","/01ncyv8bxibj0imgfvmxgqy648n697bachil6aw6i46g1jk0bbds"),("strictDeps",""),("system","x86_64-linux"),("version","1.0.7")])
129        "#;
130        // we convert everything into strings because it is way easier to compare elements in error messages
131        let refs: Vec<&str> = StoreRefScanner::new(drv, &StoreSpec::DFL_NIX2)
132            .map(|i| core::str::from_utf8(i).unwrap())
133            .collect();
134        let refs_expect: Vec<&[u8]> = vec![
135            b"2ax7bvjdfkzim69q957i0jlg0nvmapg0-util-linux-2.37.2.drv",
136            b"6b55ssmh8pzqsc4q4kw1yl3kqvr4fvqj-bash-5.1-p12.drv",
137            b"fp2vx24kczlzv84avds28wyzsmrn8kyv-source.drv",
138            b"s6c2lm5hpsvdwnxq9y1g3ngncghjzc3k-stdenv-linux.drv",
139            b"xlnzpf4mzghi8vl0krabrgcbnqk5qjf3-pkg-config-wrapper-0.29.2.drv",
140            b"03sl46khd8gmjpsad7223m32ma965vy9-fix-static.patch",
141            b"2q3z7587yhlz0i2xvfvvap42zk5carlv-bcache-udev-modern.patch",
142            b"9krlzvny65gdc8s7kpb6lkx8cd02c25b-default-builder.sh",
143            b"9krlzvny65gdc8s7kpb6lkx8cd02c25b-default-builder.sh",
144            b"2q3z7587yhlz0i2xvfvvap42zk5carlv-bcache-udev-modern.patch",
145            b"03sl46khd8gmjpsad7223m32ma965vy9-fix-static.patch",
146            b"6izcafvfcbz19chi7hl20834g0fa043n-source",
147        ];
148        let refs_expect: Vec<&str> = refs_expect
149            .into_iter()
150            .map(|i| core::str::from_utf8(i).unwrap())
151            .collect();
152        assert_eq!(refs, refs_expect);
153    }
154
155    #[test]
156    fn simple_yzix1() {
157        // I haven't yet produced any yzix derivation which included /yzixs absolute paths...
158        let fake: &[u8] = br#"
159            /yzixs/4Zx1PBoft1YyAuKdhjAY1seZFHloxQ+8voHQRkRMuys:         ASCII text
160            /yzixs/dNE3yogD4JHKHzNa2t3jQMZddT8wjqlMDB0naDIFo0A:         ASCII text
161            /yzixs/FMluSVOHLc4bxX7F4lBCXafNljBnDn+rAM5HzG7k8LI:         unified diff output, ASCII text
162            /yzixs/g2G3GRL87hGEdw9cq2BZWqDQP_HeHSPRLbJ9P9KH+HI:         unified diff output, ASCII text
163            /yzixs/H08Av1ZAONwFdzVLpFQm0Sc0dvyk0sbnk82waoBig7I:         ASCII text
164            /yzixs/IndARQp+gaGDLS3K+PeyXdaRqAcCyS3EIbRXkkYjC94:         unified diff output, ASCII text
165            /yzixs/IrLPnbkEolTAuWRxkXpuvVs6Imb1iB6wUJcI+fxWwkU:         POSIX shell script, ASCII text executable
166            /yzixs/JsS_H3n3TSh2R6fiIzgOPZdjSmRkV71vGxstJJKPmr4:         unified diff output, ASCII text
167            /yzixs/LZ6pQh1x8DRxZ2IYzetBRS4LuE__IXFjpOfQPxHVwpw:         unified diff output, ASCII text
168            /yzixs/mEi2RPep9daRs0JUvwt1JsDfgYSph5sH_+_ihwn8IGQ:         ASCII text
169            /yzixs/nd4DyljinP3auDMHL_LrpsRJkWQpSHQK2jqtyyzWcBA:         POSIX shell script, ASCII text executable
170            /yzixs/nzpaknF0_ONSHtd0i_e1E3pkLF1QPeJQhAB7x9Ogo_M:         unified diff output, ASCII text
171            /yzixs/UZ3uzVUUMC1gKGLw6tg_aLFwoFrJedXB3xbhEgQOaiY:         unified diff output, ASCII text
172            /yzixs/VKyXxKTXsDGxYJ24YgbvCc1bZkA5twp3TC+Gbi4Kwd8:         unified diff output, ASCII text
173            /yzixs/VPJMl8O1xkc1LsJznpoQrCrQO0Iy+ODCPsgoUBLiRZc:         unified diff output, ASCII text
174            /yzixs/W6r1ow001ASHRj+gtRfyj9Fb_gCO_pBztX8WhYXVdIc:         unified diff output, ASCII text
175            /yzixs/xvwEcXIob_rQynUEtQiQbwaDXEobTVKEGaBMir9oH9k:         unified diff output, ASCII text
176            /yzixs/ZPvQbRJrtyeSITvW3FUZvw99hhNOO3CFqGgmWgScxcg:         ASCII text
177        "#;
178        let refs: Vec<&str> = StoreRefScanner::new(fake, &StoreSpec::DFL_YZIX1)
179            .map(|i| core::str::from_utf8(i).unwrap())
180            .collect();
181        let refs_expect: Vec<&[u8]> = vec![
182            b"4Zx1PBoft1YyAuKdhjAY1seZFHloxQ+8voHQRkRMuys",
183            b"dNE3yogD4JHKHzNa2t3jQMZddT8wjqlMDB0naDIFo0A",
184            b"FMluSVOHLc4bxX7F4lBCXafNljBnDn+rAM5HzG7k8LI",
185            b"g2G3GRL87hGEdw9cq2BZWqDQP_HeHSPRLbJ9P9KH+HI",
186            b"H08Av1ZAONwFdzVLpFQm0Sc0dvyk0sbnk82waoBig7I",
187            b"IndARQp+gaGDLS3K+PeyXdaRqAcCyS3EIbRXkkYjC94",
188            b"IrLPnbkEolTAuWRxkXpuvVs6Imb1iB6wUJcI+fxWwkU",
189            b"JsS_H3n3TSh2R6fiIzgOPZdjSmRkV71vGxstJJKPmr4",
190            b"LZ6pQh1x8DRxZ2IYzetBRS4LuE__IXFjpOfQPxHVwpw",
191            b"mEi2RPep9daRs0JUvwt1JsDfgYSph5sH_+_ihwn8IGQ",
192            b"nd4DyljinP3auDMHL_LrpsRJkWQpSHQK2jqtyyzWcBA",
193            b"nzpaknF0_ONSHtd0i_e1E3pkLF1QPeJQhAB7x9Ogo_M",
194            b"UZ3uzVUUMC1gKGLw6tg_aLFwoFrJedXB3xbhEgQOaiY",
195            b"VKyXxKTXsDGxYJ24YgbvCc1bZkA5twp3TC+Gbi4Kwd8",
196            b"VPJMl8O1xkc1LsJznpoQrCrQO0Iy+ODCPsgoUBLiRZc",
197            b"W6r1ow001ASHRj+gtRfyj9Fb_gCO_pBztX8WhYXVdIc",
198            b"xvwEcXIob_rQynUEtQiQbwaDXEobTVKEGaBMir9oH9k",
199            b"ZPvQbRJrtyeSITvW3FUZvw99hhNOO3CFqGgmWgScxcg",
200        ];
201        let refs_expect: Vec<&str> = refs_expect
202            .into_iter()
203            .map(|i| core::str::from_utf8(i).unwrap())
204            .collect();
205        assert_eq!(refs, refs_expect);
206    }
207
208    #[test]
209    fn just_store() {
210        for i in [&StoreSpec::DFL_NIX2, &StoreSpec::DFL_YZIX1] {
211            let refs: Vec<&[u8]> = StoreRefScanner::new(i.path_to_store.as_bytes(), i).collect();
212            assert!(refs.is_empty());
213        }
214    }
215}