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