Subversion Repositories public

Rev

Rev 47 | Rev 63 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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