Subversion Repositories public

Rev

Rev 47 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
45 luk 1
 
2
/// inotify cron table manipulator classes implementation
3
/**
4
 * \file incrontab.cpp
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
#include <sstream>
18
#include <stdio.h>
19
#include <errno.h>
20
 
21
#include "inotify-cxx.h"
22
 
23
#include "incrontab.h"
24
 
25
 
26
 
27
#define INCRON_ALLOW_PATH "/etc/incron.allow"
28
#define INCRON_DENY_PATH "/etc/incron.deny"
29
 
30
 
31
 
32
 
33
InCronTabEntry::InCronTabEntry()
34
: m_uMask(0)
35
{
36
 
37
}
38
 
39
InCronTabEntry::InCronTabEntry(const std::string& rPath, uint32_t uMask, const std::string& rCmd)
40
: m_path(rPath),
41
  m_uMask(uMask),
42
  m_cmd(rCmd)
43
{
44
 
45
}
46
 
47
std::string InCronTabEntry::ToString() const
48
{
49
  std::ostringstream ss;
50
 
51
  std::string m;
52
 
53
  InotifyEvent::DumpTypes(m_uMask, m);
54
  if (m.empty())
55
    m = "0";
56
 
57
  ss << m_path << " " << m << " " << m_cmd;
58
  return ss.str();
59
}
60
 
61
bool InCronTabEntry::Parse(const std::string& rStr, InCronTabEntry& rEntry)
62
{
63
  char s1[1000], s2[1000], s3[1000];
64
  unsigned long u;
65
 
66
  if (sscanf(rStr.c_str(), "%s %s %[^\n]", s1, s2, s3) != 3)
67
    return false;
68
 
69
  rEntry.m_path = s1;
70
  rEntry.m_cmd = s3;
71
  rEntry.m_uMask = 0;  
72
 
73
  if (sscanf(s2, "%lu", &u) == 1) {
74
    rEntry.m_uMask = (uint32_t) u;
75
  }
76
  else {
77
    StringTokenizer tok(s2);
78
    while (tok.HasMoreTokens()) {
79
      rEntry.m_uMask |= InotifyEvent::GetMaskByName(tok.GetNextToken());
80
    }
81
  }
82
 
83
  return true;
84
}
85
 
86
bool InCronTab::Load(const std::string& rPath)
87
{
88
  m_tab.clear();
89
 
90
  FILE* f = fopen(rPath.c_str(), "r");
91
  if (f == NULL)
92
    return false;
93
 
94
  char s[1000];
95
  InCronTabEntry e;
96
  while (fgets(s, 1000, f) != NULL) {
97
    if (InCronTabEntry::Parse(s, e)) {
98
      m_tab.push_back(e);
99
    }
100
  }
101
 
102
  fclose(f);
103
 
104
  return true;
105
}
106
 
107
bool InCronTab::Save(const std::string& rPath)
108
{
109
  FILE* f = fopen(rPath.c_str(), "w");
110
  if (f == NULL)
111
    return false;
112
 
113
  std::deque<InCronTabEntry>::iterator it = m_tab.begin();
114
  while (it != m_tab.end()) {
115
    fputs((*it).ToString().c_str(), f);
116
    fputs("\n", f);
117
    it++;
118
  }
119
 
120
  fclose(f);
121
 
122
  return true;
123
}
124
 
125
bool InCronTab::CheckUser(const std::string& rUser)
126
{
127
  char s[100], u[100];
128
 
129
  FILE* f = fopen(INCRON_ALLOW_PATH, "r");
130
  if (f == NULL) {
131
    if (errno == ENOENT) {
132
      f = fopen(INCRON_DENY_PATH, "r");
133
      if (f == NULL) {
134
        return errno == ENOENT;
135
      }
136
      while (fgets(s, 100, f) != NULL) {
137
        if (sscanf(s, "%s", u) == 1) {
138
          if (rUser == u) {
139
            fclose(f);
140
            return false;
141
          }
142
        }
143
      }
144
      fclose(f);
145
      return true;
146
    }
147
 
148
    return false;
149
  }
150
 
151
  while (fgets(s, 100, f) != NULL) {
152
    if (sscanf(s, "%s", u) == 1) {
153
      if (rUser == u) {
154
        fclose(f);
155
        return true;
156
      }
157
    }
158
  }
159
 
160
  fclose(f);
161
  return false;
162
}
163
 
164
std::string InCronTab::GetUserTablePath(const std::string& rUser)
165
{
166
  std::string s(INCRON_TABLE_BASE);
167
  s.append(rUser);
168
  return s;
169
}
170