Subversion Repositories public

Rev

Rev 108 | Details | Compare with Previous | Last modification | View Log | RSS feed

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