Subversion Repositories public

Rev

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