Debian Codegraph @

src/util.rs

raw symbols SCIP
1 use std::fs;
2
3 use crate::{murmur_hash2, Error, Result, Udev};
4
5 pub(crate) const LINE_SIZE: usize = 16384;
6
7 mod device_nodes;
8
9 pub use device_nodes::*;
10
11 impl Udev {
12 pub(crate) fn get_sys_core_link_value(slink: &str, syspath: &str) -> Result<String> {
13 let path = format!("{syspath}/{slink}");
14 let link = fs::read_link(path)?;
15
16 // get the basename of the symlinked target
17 Ok(link
18 .components()
19 .last()
20 .ok_or(Error::Io("empty sys core link value".into()))?
21 .as_os_str()
22 .to_str()
23 .ok_or(Error::Io(
24 "sys core link OS string contains non-Unicode bytes".into(),
25 ))?
26 .to_owned())
27 }
28 }
29
30 /// Compute a MurMurHash over the provided string.
31 pub fn string_hash32(s: &str) -> u32 {
32 murmur_hash2(s.as_bytes(), 0)
33 }
34
35 /// Gets a bunch of bit numbers out of the hash, and sets the bits into a bitfield.
36 pub fn string_bloom64(s: &str) -> u64 {
37 let hash = string_hash32(s);
38
39 (1u64 << (hash & 63))
40 | (1u64 << ((hash >> 6) & 63))
41 | (1u64 << ((hash >> 12) & 63))
42 | (1u64 << ((hash >> 18) & 63))
43 }
44
45 /// Gets the major part of the device number.
46 pub fn major(dev: libc::dev_t) -> u16 {
47 (((dev >> 31 >> 1) & 0xfffff000) | ((dev >> 8) & 0x00000fff)) as u16
48 }
49
50 /// Gets the minor part of the device number.
51 pub fn minor(dev: libc::dev_t) -> u16 {
52 (((dev >> 12) & 0xffffff00) | (dev & 0x000000ff)) as u16
53 }
54
55 /// Encodes provided string, removing potentially unsafe characters.
56 ///
57 /// From the `libudev` documentation:
58 ///
59 /// ```no_build,no_run
60 /// Encode all potentially unsafe characters of a string to the
61 /// corresponding 2 char hex value prefixed by '\x'.
62 /// ```
63 ///
64 /// Returns: `Ok(String)` on success, `Err(Error)` otherwise
65 pub fn encode_string(arg: &str) -> Result<String> {
66 encode_devnode_name(arg)
67 }