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