summaryrefslogtreecommitdiffstats
path: root/crates/core/tedge_mapper/src/c8y/dynamic_discovery.rs
blob: 535f396b4bd9bc669ca591172dbc4cf719856ec9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use std::{ffi::OsString, path::PathBuf};

use inotify::{Event, EventMask, Inotify, WatchMask};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
pub enum EventType {
    ADD,
    REMOVE,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct DiscoverOp {
    pub ops_dir: PathBuf,
    pub event_type: EventType,
    pub operation_name: String,
}

#[derive(thiserror::Error, Debug)]
#[allow(clippy::enum_variant_names)]
pub enum DynamicDiscoverOpsError {
    #[error("Failed to add watch to directory: {0}")]
    FailedtoAddWatch(String),

    #[error("A non-UTF8 name cannot be used as an operation name: {0:?}")]
    NotAnOperationName(OsString),

    #[error(transparent)]
    EventError(#[from] std::io::Error),
}

pub fn create_inotify_watch(ops_dir: PathBuf) -> Result<Inotify, DynamicDiscoverOpsError> {
    let mut inotify = Inotify::init()?;
    inotify
        .add_watch(ops_dir.clone(), WatchMask::CLOSE_WRITE | WatchMask::DELETE)
        .map_err(|_| {
            DynamicDiscoverOpsError::FailedtoAddWatch(ops_dir.to_string_lossy().to_string())
        })?;
    Ok(inotify)
}

pub fn create_inofity_event_stream(
    ops_dir: PathBuf,
) -> Result<inotify::EventStream<[u8; 1024]>, DynamicDiscoverOpsError> {
    let buffer = [0; 1024];
    let mut ino = create_inotify_watch(ops_dir)?;
    Ok(ino.event_stream(buffer)?)
}

pub fn process_inotify_events(
    ops_dir: PathBuf,
    event: Event<OsString>,
) -> Result<Option<DiscoverOp>, DynamicDiscoverOpsError> {
    if let Some(ops_name) = event.clone().name {
        let operation_name = ops_name
            .to_str()
            .ok_or(DynamicDiscoverOpsError::NotAnOperationName(
                ops_name.clone(),
            ));

        match operation_name {
            Ok(ops_name) => match event.mask {
                EventMask::DELETE => {
                    return Ok(Some(DiscoverOp {
                        ops_dir,
                        event_type: EventType::REMOVE,
                        operation_name: ops_name.to_string(),
                    }))
                }
                EventMask::CLOSE_WRITE => {
                    return Ok(Some(DiscoverOp {
                        ops_dir,
                        event_type: EventType::ADD,
                        operation_name: ops_name.to_string(),
                    }))
                }
                _ => return Ok(None),
            },
            Err(e) => return Err(e),
        }
    }
    Ok(None)
}

#[cfg(test)]
#[test]
fn create_inotify_with_non_existing_dir() {
    let err = create_inotify_watch("/tmp/discover_ops".into()).unwrap_err();
    assert_eq!(
        err.to_string(),
        "Failed to add watch to directory: /tmp/discover_ops"
    );
}

#[test]
fn create_inotify_with_right_directory() {
    use tempfile::TempDir;
    let dir = TempDir::new().unwrap().into_path();
    let res = create_inotify_watch(dir);
    assert!(res.is_ok());
}