Rev 69 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
45 | luk | 1 | |
2 | /// inotify cron table manipulator classes header |
||
3 | /** |
||
4 | * \file incrontab.h |
||
5 | * |
||
6 | * inotify cron system |
||
7 | * |
||
63 | luk | 8 | * Copyright (C) 2006, 2007 Lukas Jelinek, <lukas@aiken.cz> |
45 | luk | 9 | * |
10 | * This program is free software; you can use it, redistribute |
||
11 | * it and/or modify it under the terms of the GNU General Public |
||
12 | * License, version 2 (see LICENSE-GPL). |
||
13 | * |
||
14 | */ |
||
15 | |||
16 | |||
17 | #ifndef _INCRONTAB_H_ |
||
18 | #define _INCRONTAB_H_ |
||
19 | |||
20 | #include <string> |
||
21 | #include <deque> |
||
22 | |||
23 | #include "strtok.h" |
||
24 | |||
69 | luk | 25 | /* |
67 | luk | 26 | /// Incron user table base directory |
27 | #define INCRON_USER_TABLE_BASE "/var/spool/incron/" |
||
45 | luk | 28 | |
67 | luk | 29 | /// Incron system table base directory |
30 | #define INCRON_SYS_TABLE_BASE "/etc/incron.d/" |
||
69 | luk | 31 | */ |
45 | luk | 32 | |
47 | luk | 33 | /// Incron table entry class. |
69 | luk | 34 | class IncronTabEntry |
45 | luk | 35 | { |
36 | public: |
||
47 | luk | 37 | /// Constructor. |
38 | /** |
||
39 | * Creates an empty entry for later use with Parse(). |
||
40 | * |
||
41 | * \sa Parse() |
||
42 | */ |
||
69 | luk | 43 | IncronTabEntry(); |
45 | luk | 44 | |
47 | luk | 45 | /// Constructor. |
46 | /** |
||
47 | * Creates an entry based on defined parameters. |
||
48 | * |
||
49 | * \param[in] rPath watched filesystem path |
||
50 | * \param[in] uMask event mask |
||
51 | * \param[in] rCmd command string |
||
52 | */ |
||
69 | luk | 53 | IncronTabEntry(const std::string& rPath, uint32_t uMask, const std::string& rCmd); |
45 | luk | 54 | |
47 | luk | 55 | /// Destructor. |
69 | luk | 56 | ~IncronTabEntry() {} |
45 | luk | 57 | |
47 | luk | 58 | /// Converts the entry to string representation. |
59 | /** |
||
60 | * This method creates a string for use in a table file. |
||
61 | * |
||
62 | * \return string representation |
||
63 | */ |
||
45 | luk | 64 | std::string ToString() const; |
65 | |||
47 | luk | 66 | /// Parses a string and attempts to extract entry parameters. |
67 | /** |
||
68 | * \param[in] rStr parsed string |
||
69 | * \param[out] rEntry parametrized entry |
||
70 | * \return true = success, false = failure |
||
71 | */ |
||
69 | luk | 72 | static bool Parse(const std::string& rStr, IncronTabEntry& rEntry); |
45 | luk | 73 | |
47 | luk | 74 | /// Returns the watch filesystem path. |
75 | /** |
||
76 | * \return watch path |
||
77 | */ |
||
45 | luk | 78 | inline const std::string& GetPath() const |
79 | { |
||
80 | return m_path; |
||
81 | } |
||
82 | |||
47 | luk | 83 | /// Returns the event mask. |
84 | /** |
||
85 | * \return event mask |
||
86 | */ |
||
45 | luk | 87 | inline int32_t GetMask() const |
88 | { |
||
89 | return m_uMask; |
||
90 | } |
||
91 | |||
47 | luk | 92 | /// Returns the command string. |
93 | /** |
||
94 | * \return command string |
||
95 | */ |
||
45 | luk | 96 | inline const std::string& GetCmd() const |
97 | { |
||
98 | return m_cmd; |
||
99 | } |
||
100 | |||
47 | luk | 101 | /// Checks whether this entry has set loop-avoidance. |
102 | /** |
||
103 | * \return true = no loop, false = loop allowed |
||
104 | */ |
||
105 | inline bool IsNoLoop() const |
||
106 | { |
||
107 | return m_fNoLoop; |
||
108 | } |
||
109 | |||
55 | luk | 110 | /// Add backslashes before spaces in the source path. |
111 | /** |
||
112 | * It also adds backslashes before all original backslashes |
||
113 | * of course. |
||
114 | * |
||
115 | * The source string is not modified and a copy is returned |
||
116 | * instead. |
||
117 | * |
||
118 | * This method is intended to be used for paths in user tables. |
||
119 | * |
||
120 | * \param[in] rPath path to be modified |
||
121 | * \return modified path |
||
122 | */ |
||
123 | static std::string GetSafePath(const std::string& rPath); |
||
124 | |||
45 | luk | 125 | protected: |
47 | luk | 126 | std::string m_path; ///< watch path |
127 | uint32_t m_uMask; ///< event mask |
||
128 | std::string m_cmd; ///< command string |
||
129 | bool m_fNoLoop; ///< no loop yes/no |
||
45 | luk | 130 | }; |
131 | |||
132 | |||
47 | luk | 133 | /// Incron table class. |
69 | luk | 134 | class IncronTab |
45 | luk | 135 | { |
136 | public: |
||
47 | luk | 137 | /// Constructor. |
69 | luk | 138 | IncronTab() {} |
45 | luk | 139 | |
47 | luk | 140 | /// Destructor. |
69 | luk | 141 | ~IncronTab() {} |
45 | luk | 142 | |
47 | luk | 143 | /// Add an entry to the table. |
144 | /** |
||
145 | * \param[in] rEntry table entry |
||
146 | */ |
||
69 | luk | 147 | inline void Add(const IncronTabEntry& rEntry) |
45 | luk | 148 | { |
149 | m_tab.push_back(rEntry); |
||
150 | } |
||
151 | |||
47 | luk | 152 | /// Removes all entries. |
45 | luk | 153 | inline void Clear() |
154 | { |
||
155 | m_tab.clear(); |
||
156 | } |
||
157 | |||
47 | luk | 158 | /// Checks whether the table is empty. |
159 | /** |
||
160 | * \return true = empty, false = otherwise |
||
161 | */ |
||
45 | luk | 162 | inline bool IsEmpty() const |
163 | { |
||
164 | return m_tab.empty(); |
||
165 | } |
||
166 | |||
47 | luk | 167 | /// Returns the count of entries. |
168 | /** |
||
169 | * \return count of entries |
||
170 | */ |
||
45 | luk | 171 | inline int GetCount() const |
172 | { |
||
173 | return (int) m_tab.size(); |
||
174 | } |
||
175 | |||
47 | luk | 176 | /// Returns an entry. |
177 | /** |
||
178 | * \return reference to the entry for the given index |
||
179 | * |
||
180 | * \attention This method doesn't test index bounds. If you |
||
181 | * pass an invalid value the program may crash |
||
182 | * and/or behave unpredictible way! |
||
183 | */ |
||
69 | luk | 184 | inline IncronTabEntry& GetEntry(int index) |
45 | luk | 185 | { |
186 | return m_tab[index]; |
||
187 | } |
||
188 | |||
47 | luk | 189 | /// Loads the table. |
190 | /** |
||
191 | * \param[in] rPath path to a source table file |
||
192 | * \return true = success, false = failure |
||
193 | */ |
||
45 | luk | 194 | bool Load(const std::string& rPath); |
195 | |||
47 | luk | 196 | /// Saves the table. |
197 | /** |
||
198 | * \param[in] rPath path to a destination table file |
||
199 | * \return true = success, false = failure |
||
200 | */ |
||
45 | luk | 201 | bool Save(const std::string& rPath); |
202 | |||
47 | luk | 203 | /// Checks whether an user has permission to use incron. |
204 | /** |
||
205 | * \param[in] rUser user name |
||
206 | * \return true = permission OK, false = otherwise |
||
207 | */ |
||
45 | luk | 208 | static bool CheckUser(const std::string& rUser); |
209 | |||
47 | luk | 210 | /// Composes a path to an user incron table file. |
211 | /** |
||
212 | * \param[in] rUser user name |
||
213 | * \return path to the table file |
||
214 | * |
||
215 | * \attention No tests (existence, permission etc.) are done. |
||
216 | */ |
||
45 | luk | 217 | static std::string GetUserTablePath(const std::string& rUser); |
67 | luk | 218 | |
219 | /// Composes a path to a system incron table file. |
||
220 | /** |
||
221 | * \param[in] rName table name (pseudouser) |
||
222 | * \return path to the table file |
||
223 | * |
||
224 | * \attention No tests (existence, permission etc.) are done. |
||
225 | */ |
||
226 | static std::string GetSystemTablePath(const std::string& rName); |
||
45 | luk | 227 | |
228 | protected: |
||
69 | luk | 229 | std::deque<IncronTabEntry> m_tab; ///< incron table |
45 | luk | 230 | }; |
231 | |||
232 | |||
233 | #endif //_INCRONTAB_H_ |