|
| 1 | +use std::{error::Error, fmt::Display, path::PathBuf}; |
| 2 | + |
1 | 3 | use fact_ebpf::event_t; |
2 | 4 |
|
3 | 5 | use crate::{host_info, mount_info::MountInfo}; |
4 | 6 |
|
5 | 7 | use super::{process::Process, Event, FileData}; |
6 | 8 |
|
| 9 | +#[derive(Debug)] |
| 10 | +pub(crate) enum EventParserError { |
| 11 | + NotFound, |
| 12 | + ProcessParse(String), |
| 13 | + FileParse(String), |
| 14 | +} |
| 15 | + |
| 16 | +impl Error for EventParserError {} |
| 17 | +impl Display for EventParserError { |
| 18 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 19 | + match self { |
| 20 | + EventParserError::NotFound => write!(f, "mountpoint not found"), |
| 21 | + EventParserError::ProcessParse(e) => write!(f, "Failed to parse process: {e}"), |
| 22 | + EventParserError::FileParse(e) => write!(f, "Failed to parse file: {e}"), |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
7 | 27 | pub(crate) struct EventParser { |
8 | | - mountinfo: MountInfo, |
| 28 | + pub mountinfo: MountInfo, |
9 | 29 | } |
10 | 30 |
|
11 | 31 | impl EventParser { |
12 | | - pub(crate) fn new() -> anyhow::Result<Self> { |
13 | | - let mountinfo = MountInfo::new()?; |
| 32 | + pub(crate) fn new(paths: &[PathBuf]) -> anyhow::Result<Self> { |
| 33 | + let mountinfo = MountInfo::new(paths)?; |
14 | 34 |
|
15 | 35 | Ok(EventParser { mountinfo }) |
16 | 36 | } |
17 | 37 |
|
18 | | - pub(crate) fn parse(&mut self, event: &event_t) -> anyhow::Result<Event> { |
19 | | - let process = Process::try_from(event.process)?; |
| 38 | + pub(crate) fn refresh(&mut self, paths: &[PathBuf]) -> anyhow::Result<()> { |
| 39 | + self.mountinfo.refresh(paths) |
| 40 | + } |
| 41 | + |
| 42 | + pub(crate) fn parse(&mut self, event: &event_t) -> Result<Event, EventParserError> { |
| 43 | + let process = match Process::try_from(event.process) { |
| 44 | + Ok(p) => p, |
| 45 | + Err(e) => return Err(EventParserError::ProcessParse(e.to_string())), |
| 46 | + }; |
20 | 47 | let timestamp = host_info::get_boot_time() + event.timestamp; |
21 | 48 |
|
22 | 49 | let mounts = match self.mountinfo.get(&event.dev) { |
23 | 50 | Some(mounts) => mounts, |
24 | | - None => { |
25 | | - self.mountinfo.refresh()?; |
26 | | - match self.mountinfo.get(&event.dev) { |
27 | | - Some(mounts) => mounts, |
28 | | - None => self.mountinfo.insert_empty(event.dev), |
29 | | - } |
30 | | - } |
| 51 | + None => return Err(EventParserError::NotFound), |
31 | 52 | }; |
32 | 53 |
|
33 | | - let file = FileData::new(event.type_, event.filename, event.host_file, mounts)?; |
| 54 | + let file = match FileData::new(event.type_, event.filename, event.host_file, mounts) { |
| 55 | + Ok(f) => f, |
| 56 | + Err(e) => return Err(EventParserError::FileParse(e.to_string())), |
| 57 | + }; |
34 | 58 |
|
35 | 59 | Ok(Event { |
36 | 60 | timestamp, |
|
0 commit comments