Subversion Repositories public

Rev

Rev 69 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 69 Rev 84
1
1
2
/// inotify cron configuration implementation
2
/// inotify cron configuration implementation
3
/**
3
/**
4
 * \file incroncfg.cpp
4
 * \file incroncfg.cpp
5
 *
5
 *
6
 * incron configuration
6
 * incron configuration
7
 *
7
 *
8
 * Copyright (C) 2007 Lukas Jelinek, <lukas@aiken.cz>
8
 * Copyright (C) 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
#include <fstream>
17
#include <fstream>
18
#include <sstream>
18
#include <sstream>
19
19
20
#include "incroncfg.h"
20
#include "incroncfg.h"
21
21
22
22
23
#define INCRON_CFG_DEFAULT "/etc/incron.conf"
23
#define INCRON_CFG_DEFAULT "/etc/incron.conf"
24
24
25
25
26
typedef std::map<std::string, std::string> CFG_MAP;
26
typedef std::map<std::string, std::string> CFG_MAP;
27
typedef CFG_MAP::iterator CFG_ITER;
27
typedef CFG_MAP::iterator CFG_ITER;
28
28
29
29
30
CFG_MAP IncronCfg::m_values;
30
CFG_MAP IncronCfg::m_values;
31
CFG_MAP IncronCfg::m_defaults;
31
CFG_MAP IncronCfg::m_defaults;
32
32
33
33
34
void IncronCfg::Init()
34
void IncronCfg::Init()
35
{
35
{
36
  m_defaults.insert(CFG_MAP::value_type("system_table_dir", "/etc/incron.d"));
36
  m_defaults.insert(CFG_MAP::value_type("system_table_dir", "/etc/incron.d"));
37
  m_defaults.insert(CFG_MAP::value_type("user_table_dir", "/var/spool/incron"));
37
  m_defaults.insert(CFG_MAP::value_type("user_table_dir", "/var/spool/incron"));
38
  m_defaults.insert(CFG_MAP::value_type("allowed_users", "/etc/incron.allow"));
38
  m_defaults.insert(CFG_MAP::value_type("allowed_users", "/etc/incron.allow"));
39
  m_defaults.insert(CFG_MAP::value_type("denied_users", "/etc/incron.deny"));
39
  m_defaults.insert(CFG_MAP::value_type("denied_users", "/etc/incron.deny"));
40
  m_defaults.insert(CFG_MAP::value_type("lockfile_dir", "/var/run"));
40
  m_defaults.insert(CFG_MAP::value_type("lockfile_dir", "/var/run"));
41
  m_defaults.insert(CFG_MAP::value_type("lockfile_name", "incrond"));
41
  m_defaults.insert(CFG_MAP::value_type("lockfile_name", "incrond"));
42
  m_defaults.insert(CFG_MAP::value_type("editor", ""));
42
  m_defaults.insert(CFG_MAP::value_type("editor", ""));
43
}
43
}
44
44
45
void IncronCfg::Load(const std::string& rPath)
45
void IncronCfg::Load(const std::string& rPath)
46
{
46
{
47
  char s[1024];
47
  char s[1024];
48
 
48
 
49
  std::ifstream is(rPath.c_str());
49
  std::ifstream is(rPath.c_str());
50
  if (is.is_open()) {
50
  if (is.is_open()) {
51
    while (!is.eof() && !is.fail()) {
51
    while (!is.eof() && !is.fail()) {
52
      is.getline(s, 1023);
52
      is.getline(s, 1023);
53
      std::string key, val;
53
      std::string key, val;
54
      if (ParseLine(s, key, val)) {
54
      if (ParseLine(s, key, val)) {
55
        m_values.insert(CFG_MAP::value_type(key, val));
55
        m_values.insert(CFG_MAP::value_type(key, val));
56
      }
56
      }
57
    }
57
    }
58
    is.close();
58
    is.close();
59
    return;
59
    return;
60
  }
60
  }
61
 
61
 
62
  if (rPath == INCRON_CFG_DEFAULT)
62
  if (rPath == INCRON_CFG_DEFAULT)
63
    return;
63
    return;
64
 
64
 
65
  is.open(INCRON_CFG_DEFAULT);
65
  is.open(INCRON_CFG_DEFAULT);
66
  if (is.is_open()) {
66
  if (is.is_open()) {
67
    while (!is.eof() && !is.fail()) {
67
    while (!is.eof() && !is.fail()) {
68
      is.getline(s, 1023);
68
      is.getline(s, 1023);
69
      std::string key, val;
69
      std::string key, val;
70
      if (ParseLine(s, key, val)) {
70
      if (ParseLine(s, key, val)) {
71
        m_values.insert(CFG_MAP::value_type(key, val));
71
        m_values.insert(CFG_MAP::value_type(key, val));
72
      }
72
      }
73
    }
73
    }
74
    is.close();
74
    is.close();
75
  }
75
  }
76
}
76
}
77
77
78
bool IncronCfg::GetValue(const std::string& rKey, std::string& rVal)
78
bool IncronCfg::GetValue(const std::string& rKey, std::string& rVal)
79
{
79
{
80
  CFG_ITER it = m_values.find(rKey);
80
  CFG_ITER it = m_values.find(rKey);
81
  if (it != m_values.end()) {
81
  if (it != m_values.end()) {
82
    rVal = (*it).second;
82
    rVal = (*it).second;
83
    return true;  
83
    return true;  
84
  }
84
  }
85
 
85
 
86
  it = m_defaults.find(rKey);
86
  it = m_defaults.find(rKey);
87
  if (it != m_defaults.end()) {
87
  if (it != m_defaults.end()) {
88
    rVal = (*it).second;
88
    rVal = (*it).second;
89
    return true;  
89
    return true;  
90
  }
90
  }
91
 
91
 
92
  return false;
92
  return false;
93
}
93
}
94
94
95
bool IncronCfg::GetValue(const std::string& rKey, int& rVal)
95
bool IncronCfg::GetValue(const std::string& rKey, int& rVal)
96
{
96
{
97
  std::string s;
97
  std::string s;
98
  if (GetValue(rKey, s)) {
98
  if (GetValue(rKey, s)) {
99
    if (sscanf(s.c_str(), "%i", &rVal) == 1)
99
    if (sscanf(s.c_str(), "%i", &rVal) == 1)
100
      return true;
100
      return true;
101
  }
101
  }
102
 
102
 
103
  return false;
103
  return false;
104
}
104
}
105
105
106
bool IncronCfg::GetValue(const std::string& rKey, unsigned& rVal)
106
bool IncronCfg::GetValue(const std::string& rKey, unsigned& rVal)
107
{
107
{
108
  std::string s;
108
  std::string s;
109
  if (GetValue(rKey, s)) {
109
  if (GetValue(rKey, s)) {
110
    if (sscanf(s.c_str(), "%u", &rVal) == 1)
110
    if (sscanf(s.c_str(), "%u", &rVal) == 1)
111
      return true;
111
      return true;
112
  }
112
  }
113
 
113
 
114
  return false;
114
  return false;
115
}
115
}
116
116
117
bool IncronCfg::GetValue(const std::string& rKey, bool& rVal)
117
bool IncronCfg::GetValue(const std::string& rKey, bool& rVal)
118
{
118
{
119
  std::string s;
119
  std::string s;
120
  if (GetValue(rKey, s)) {
120
  if (GetValue(rKey, s)) {
121
    size_t len = (size_t) s.length();
121
    size_t len = (size_t) s.length();
122
    for (size_t i = 0; i < len; i++) {
122
    for (size_t i = 0; i < len; i++) {
123
      s[i] = (char) tolower(s[i]);
123
      s[i] = (char) tolower(s[i]);
124
    }
124
    }
125
   
125
   
126
    rVal = (s == "1" || s == "true" || s == "yes" || s == "on" || s == "enable" || s == "enabled");
126
    rVal = (s == "1" || s == "true" || s == "yes" || s == "on" || s == "enable" || s == "enabled");
127
    return true;
127
    return true;
128
  }
128
  }
129
 
129
 
130
  return false;
130
  return false;
131
}
131
}
132
132
133
std::string IncronCfg::BuildPath(const std::string& rPath, const std::string& rName)
133
std::string IncronCfg::BuildPath(const std::string& rPath, const std::string& rName)
134
{
134
{
135
  if (rPath.rfind('/') == rPath.length() - 1)
135
  if (rPath.rfind('/') == rPath.length() - 1)
136
    return rPath + rName;
136
    return rPath + rName;
137
   
137
   
138
  return rPath + "/" + rName;
138
  return rPath + "/" + rName;
139
}
139
}
140
140
141
bool IncronCfg::ParseLine(const char* s, std::string& rKey, std::string& rVal)
141
bool IncronCfg::ParseLine(const char* s, std::string& rKey, std::string& rVal)
142
{
142
{
143
  // CAUTION: This code hasn't been optimized. It may be slow.
143
  // CAUTION: This code hasn't been optimized. It may be slow.
144
 
144
 
145
  char key[1024], val[1024];
145
  char key[1024], val[1024];
146
 
146
 
147
  if (IsComment(s))
147
  if (IsComment(s))
148
    return false;
148
    return false;
149
 
149
 
150
  std::istringstream ss(s);
150
  std::istringstream ss(s);
151
  ss.get(key, 1023, '=');
151
  ss.get(key, 1023, '=');
152
  if (ss.fail())
152
  if (ss.fail())
153
    return false;
153
    return false;
154
   
154
   
155
  ss.get(val, 1023);
155
  ss.get(val, 1023);
156
  if (ss.fail())
156
  if (ss.fail())
157
    return false;
157
    return false;
158
   
158
   
159
  rKey = key;
159
  rKey = key;
160
  rVal = val;
160
  rVal = val;
161
   
161
   
162
  std::string::size_type a = rKey.find_first_not_of(" \t");
162
  std::string::size_type a = rKey.find_first_not_of(" \t");
163
  std::string::size_type b = rKey.find_last_not_of(" \t");
163
  std::string::size_type b = rKey.find_last_not_of(" \t");
164
  if (a == std::string::npos || b == std::string::npos)
164
  if (a == std::string::npos || b == std::string::npos)
165
    return false;
165
    return false;
166
   
166
   
167
  rKey = rKey.substr(a, b-a+1);
167
  rKey = rKey.substr(a, b-a+1);
168
 
168
 
169
  a = rVal.find_first_not_of(" \t=");
169
  a = rVal.find_first_not_of(" \t=");
170
  b = rVal.find_last_not_of(" \t");
170
  b = rVal.find_last_not_of(" \t");
171
  if (a == std::string::npos || b == std::string::npos) {
171
  if (a == std::string::npos || b == std::string::npos) {
172
    rVal = "";
172
    rVal = "";
173
  }
173
  }
174
  else {
174
  else {
175
    rVal = rVal.substr(a, b-a+1);
175
    rVal = rVal.substr(a, b-a+1);
176
  }
176
  }
177
   
177
   
178
  return true;
178
  return true;
179
}
179
}
180
180
181
bool IncronCfg::IsComment(const char* s)
181
bool IncronCfg::IsComment(const char* s)
182
{
182
{
183
  char* sx = strchr(s, '#');
183
  char* sx = strchr(s, '#');
184
  if (sx == NULL)
184
  if (sx == NULL)
185
    return false;
185
    return false;
186
   
186
   
187
  size_t len = sx - s;
187
  size_t len = sx - s;
188
  for (size_t i = 0; i < len; i++) {
188
  for (size_t i = 0; i < len; i++) {
189
    if (!(s[i] == ' ' || s[i] == '\t'))
189
    if (!(s[i] == ' ' || s[i] == '\t'))
190
      return false;
190
      return false;
191
  }
191
  }
192
 
192
 
193
  return true;
193
  return true;
194
}
194
}
195
195
196
 
196