summaryrefslogtreecommitdiffstats
path: root/plugins/plugin_inotify/src/config.rs
blob: 6859b4e9d20a7a51da67b1211fc00244bb0151f2 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use std::collections::HashMap;
use std::path::PathBuf;

#[derive(serde::Deserialize, Debug, tedge_api::Config)]
pub struct InotifyConfig {
    /// Target to send notifications to
    pub(crate) target: String,

    /// Whether to error in the plugin, when inotify returns an error
    ///
    /// If the plugin itself errors, these will always be returned
    /// Defaults to true
    #[serde(default = "fail_on_err_default")]
    pub(crate) fail_on_err: bool,

    /// Pathes to watch
    pub(crate) pathes: HashMap<PathBuf, Vec<Watchmode>>,
}

fn fail_on_err_default() -> bool {
    true
}

#[derive(serde::Deserialize, Copy, Clone, Debug, tedge_api::Config)]
#[config(untagged)]
#[allow(non_camel_case_types)]
pub enum Watchmode {
    /// File was accessed
    /// When watching a directory, this event is only triggered for objects inside the directory,
    /// not the directory itself.
    ACCESS,

    // Metadata (permissions, timestamps, …) changed
    //
    // When watching a directory, this event can be triggered for the directory itself, as well as objects inside the directory.
    ATTRIB,

    // File opened for writing was closed
    //
    // When watching a directory, this event is only triggered for objects inside the directory, not the directory itself.
    CLOSE_WRITE,

    // File or directory not opened for writing was closed
    //
    // When watching a directory, this event can be triggered for the directory itself, as well as objects inside the directory.
    CLOSE_NOWRITE,

    // File/directory created in watched directory
    //
    // When watching a directory, this event is only triggered for objects inside the directory, not the directory itself.
    CREATE,

    // File/directory deleted from watched directory
    //
    // When watching a directory, this event is only triggered for objects inside the directory, not the directory itself.
    DELETE,

    // Watched file/directory was deleted
    DELETE_SELF,

    // File was modified
    //
    // When watching a directory, this event is only triggered for objects inside the directory, not the directory itself.
    MODIFY,

    // Watched file/directory was moved
    MOVE_SELF,

    // File was renamed/moved; watched directory contained old name
    //
    // When watching a directory, this event is only triggered for objects inside the directory, not the directory itself.
    MOVED_FROM,

    // File was renamed/moved; watched directory contains new name
    //
    // When watching a directory, this event is only triggered for objects inside the directory, not the directory itself.
    MOVED_TO,

    // File or directory was opened
    //
    // When watching a directory, this event can be triggered for the directory itself, as well as objects inside the directory.
    OPEN,

    // Watch for all events
    //
    // This constant is simply a convenient combination of the following other constants:
    //
    //     ACCESS
    //     ATTRIB
    //     CLOSE_WRITE
    //     CLOSE_NOWRITE
    //     CREATE
    //     DELETE
    //     DELETE_SELF
    //     MODIFY
    //     MOVE_SELF
    //     MOVED_FROM
    //     MOVED_TO
    //     OPEN
    ALL_EVENTS,

    // Watch for all move events
    //
    // This constant is simply a convenient combination of the following other constants:
    //
    //     MOVED_FROM
    //     MOVED_TO
    MOVE,

    // Watch for all close events
    //
    // This constant is simply a convenient combination of the following other constants:
    //
    //     CLOSE_WRITE
    //     CLOSE_NOWRITE
    CLOSE,

    // Don’t dereference the path if it is a symbolic link
    DONT_FOLLOW,

    // Filter events for directory entries that have been unlinked
    EXCL_UNLINK,

    // If a watch for the inode exists, amend it instead of replacing it
    MASK_ADD,

    // Only receive one event, then remove the watch
    ONESHOT,

    // Only watch path, if it is a directory
    ONLYDIR,
}

impl From<Watchmode> for inotify::WatchMask {
    fn from(wm: Watchmode) -> Self {
        match wm {
            Watchmode::ACCESS => inotify::WatchMask::ACCESS,
            Watchmode::ATTRIB => inotify::WatchMask::ATTRIB,
            Watchmode::CLOSE_WRITE => inotify::WatchMask::CLOSE_WRITE,
            Watchmode::CLOSE_NOWRITE => inotify::WatchMask::CLOSE_NOWRITE,
            Watchmode::CREATE => inotify::WatchMask::CREATE,
            Watchmode::DELETE => inotify::WatchMask::DELETE,
            Watchmode::DELETE_SELF => inotify::WatchMask::DELETE_SELF,
            Watchmode::MODIFY => inotify::WatchMask::MODIFY,
            Watchmode::MOVE_SELF => inotify::WatchMask::MOVE_SELF,
            Watchmode::MOVED_FROM => inotify::WatchMask::MOVED_FROM,
            Watchmode::MOVED_TO => inotify::WatchMask::MOVED_TO,
            Watchmode::OPEN => inotify::WatchMask::OPEN,
            Watchmode::ALL_EVENTS => inotify::WatchMask::ALL_EVENTS,
            Watchmode::MOVE => inotify::WatchMask::MOVE,
            Watchmode::CLOSE => inotify::WatchMask::CLOSE,
            Watchmode::DONT_FOLLOW => inotify::WatchMask::DONT_FOLLOW,
            Watchmode::EXCL_UNLINK => inotify::WatchMask::EXCL_UNLINK,
            Watchmode::MASK_ADD => inotify::WatchMask::MASK_ADD,
            Watchmode::ONESHOT => inotify::WatchMask::ONESHOT,
            Watchmode::ONLYDIR => inotify::WatchMask::ONLYDIR,
        }
    }
}