1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
use std::str::FromStr;
use std::iter::FromIterator;
use std::error::Error;
use std::convert::From;
use std::num::ParseIntError;
use std::fmt::{self, Display, Formatter};
use schedule::{Schedule, Period, Calendar, ScheduleParseError, PeriodParseError};
#[derive(Debug, PartialEq)]
pub enum CrontabEntry {
EnvVar(EnvVarEntry),
User(UserCrontabEntry),
System(SystemCrontabEntry),
Anacron(AnacrontabEntry)
}
#[derive(Debug, PartialEq)]
pub struct EnvVarEntry(pub String, pub String);
#[derive(Debug, PartialEq)]
pub struct UserCrontabEntry {
pub sched: Schedule,
pub cmd: String
}
#[derive(Debug, PartialEq)]
pub struct SystemCrontabEntry {
pub sched: Schedule,
pub user: UserInfo,
pub cmd: String
}
#[derive(Debug, PartialEq)]
pub struct AnacrontabEntry {
pub period: Period,
pub delay: u32,
pub jobid: String,
pub cmd: String
}
impl From<UserCrontabEntry> for CrontabEntry {
fn from(entry: UserCrontabEntry) -> CrontabEntry {
CrontabEntry::User(entry)
}
}
impl From<SystemCrontabEntry> for CrontabEntry {
fn from(entry: SystemCrontabEntry) -> CrontabEntry {
CrontabEntry::System(entry)
}
}
impl From<AnacrontabEntry> for CrontabEntry {
fn from(entry: AnacrontabEntry) -> CrontabEntry {
CrontabEntry::Anacron(entry)
}
}
impl From<EnvVarEntry> for CrontabEntry {
fn from(entry: EnvVarEntry) -> CrontabEntry {
CrontabEntry::EnvVar(entry)
}
}
impl Display for CrontabEntry {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match *self {
CrontabEntry::EnvVar(ref entry) => entry.fmt(f),
CrontabEntry::Anacron(ref entry) => entry.fmt(f),
CrontabEntry::User(ref entry) => entry.fmt(f),
CrontabEntry::System(ref entry) => entry.fmt(f),
}
}
}
impl CrontabEntry {
pub fn period<'a>(&'a self) -> Option<&'a Period> {
match *self {
CrontabEntry::Anacron(AnacrontabEntry { ref period, .. }) => Some(period),
CrontabEntry::User(UserCrontabEntry { sched: Schedule::Period(ref period), .. }) => Some(period),
CrontabEntry::System(SystemCrontabEntry { sched: Schedule::Period(ref period), .. }) => Some(period),
_ => None
}
}
pub fn calendar<'a>(&'a self) -> Option<&'a Calendar> {
match *self {
CrontabEntry::User(UserCrontabEntry { sched: Schedule::Calendar(ref cal), .. }) => Some(cal),
CrontabEntry::System(SystemCrontabEntry { sched: Schedule::Calendar(ref cal), .. }) => Some(cal),
_ => None
}
}
pub fn command<'a>(&'a self) -> Option<&'a str> {
match *self {
CrontabEntry::User(UserCrontabEntry { ref cmd, .. }) => Some(&**cmd),
CrontabEntry::System(SystemCrontabEntry { ref cmd, .. }) => Some(&**cmd),
CrontabEntry::Anacron(AnacrontabEntry { ref cmd, .. }) => Some(&**cmd),
_ => None
}
}
pub fn user<'a>(&'a self) -> Option<&'a str> {
match *self {
CrontabEntry::System(SystemCrontabEntry { user: UserInfo(ref user, _, _), .. }) => Some(&**user),
_ => None
}
}
pub fn group<'a>(&'a self) -> Option<&'a str> {
match *self {
CrontabEntry::System(SystemCrontabEntry { user: UserInfo(_, Some(ref group), _), .. }) => Some(&**group),
_ => None
}
}
}
impl Display for EnvVarEntry {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}={}", self.0, self.1)
}
}
impl FromStr for EnvVarEntry {
type Err = CrontabEntryParseError;
fn from_str(s: &str) -> Result<EnvVarEntry, CrontabEntryParseError> {
let spaces = [' ', '\t'];
let mut splits = s.splitn(2, '=');
let name = match splits.next() {
Some(n) => n.trim_right_matches(&spaces[..]),
None => return Err(CrontabEntryParseError::MissingEnvVarName)
};
if name.len() == 0 {
return Err(CrontabEntryParseError::MissingEnvVarName)
}
if name.chars().any(|v| v == ' ' || v == '\t') {
return Err(CrontabEntryParseError::InvalidEnvVarName)
}
let mut value = match splits.next() {
Some(v) => v.trim_left_matches(&spaces[..]),
None => return Err(CrontabEntryParseError::MissingEnvVarValue)
};
if value.len() > 1 {
if &value[..1] == "'" || &value[..1] == "\"" && &value[..1] == &value[value.len()-1..] {
value = &value[1..value.len()-1];
}
}
Ok(EnvVarEntry(name.to_owned(), value.to_owned()))
}
}
#[derive(Debug, PartialEq)]
pub struct UserInfo(pub String, pub Option<String>, pub Option<String>);
#[derive(Debug, PartialEq)]
pub struct UserInfoParseError;
impl Display for UserInfo {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
try!(self.0.fmt(f));
if let Some(ref group) = self.1 {
try!(f.write_str(":"));
try!(group.fmt(f));
}
if let Some(ref class) = self.2 {
try!(f.write_str(":"));
try!(class.fmt(f));
}
Ok(())
}
}
impl FromStr for UserInfo {
type Err = UserInfoParseError;
fn from_str(s: &str) -> Result<UserInfo, UserInfoParseError> {
let mut splits = s.split(':');
Ok(UserInfo(
try!(splits.next().ok_or(UserInfoParseError).map(ToOwned::to_owned)),
splits.next().map(ToOwned::to_owned),
splits.next().map(ToOwned::to_owned)
))
}
}
impl Error for UserInfoParseError {
fn description(&self) -> &str {
"invalid user name"
}
fn cause(&self) -> Option<&Error> {
None
}
}
impl Display for UserInfoParseError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_str("invalid user name")
}
}
#[derive(Debug, PartialEq)]
pub enum CrontabEntryParseError {
InvalidSchedule(ScheduleParseError),
InvalidPeriod(PeriodParseError),
InvalidUser(UserInfoParseError),
InvalidDelay(ParseIntError),
InvalidEnvVarName,
MissingPeriod,
MissingDelay,
MissingJobId,
MissingEnvVarName,
MissingEnvVarValue,
}
impl Display for CrontabEntryParseError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
use self::CrontabEntryParseError::*;
match *self {
InvalidSchedule(ref e) => write!(f, "invalid schedule: {}", e),
InvalidPeriod(ref e) => write!(f, "invalid period: {}", e),
InvalidUser(ref e) => write!(f, "invalid user: {}", e),
InvalidDelay(ref e) => write!(f, "invalid delay: {}", e),
_ => f.write_str(self.description()),
}
}
}
impl Error for CrontabEntryParseError {
fn description(&self) -> &str {
use self::CrontabEntryParseError::*;
match *self {
InvalidSchedule(_) => "invalid schedule",
InvalidPeriod(_) => "invalid period",
InvalidUser(_) => "invalid user",
InvalidDelay(_) => "invalid delay",
InvalidEnvVarName => "invalid environment variable name",
MissingPeriod => "missing period",
MissingDelay => "missing delay",
MissingJobId => "missing jobid",
MissingEnvVarName => "missing environment variable name",
MissingEnvVarValue => "missing environment variable value",
}
}
fn cause(&self) -> Option<&Error> {
use self::CrontabEntryParseError::*;
match *self {
InvalidSchedule(ref e) => Some(e),
InvalidPeriod(ref e) => Some(e),
InvalidUser(ref e) => Some(e),
InvalidDelay(ref e) => Some(e),
_ => None,
}
}
}
impl Display for UserCrontabEntry {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{} {}", self.sched, self.cmd)
}
}
impl FromStr for UserCrontabEntry {
type Err = CrontabEntryParseError;
fn from_str(s: &str) -> Result<UserCrontabEntry, CrontabEntryParseError> {
let seps = [' ', '\t'];
let mut splits = s.split(&seps[..]).filter(|v| *v != "");
Ok(UserCrontabEntry {
sched: try!(Schedule::from_iter(&mut splits)),
cmd: splits.collect::<Vec<&str>>().join(" ")
})
}
}
impl Display for SystemCrontabEntry {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{} {} {}", self.sched, self.user, self.cmd)
}
}
impl FromStr for SystemCrontabEntry {
type Err = CrontabEntryParseError;
fn from_str(s: &str) -> Result<SystemCrontabEntry, CrontabEntryParseError> {
let seps = [' ', '\t'];
let mut splits = s.split(&seps[..]).filter(|v| *v != "");
Ok(SystemCrontabEntry {
sched: try!(Schedule::from_iter(&mut splits)),
user: try!(splits.next().ok_or(UserInfoParseError).and_then(FromStr::from_str)),
cmd: splits.collect::<Vec<&str>>().join(" ")
})
}
}
impl Display for AnacrontabEntry {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "@{} {} {} {}", self.period, self.delay, self.jobid, self.cmd)
}
}
impl FromStr for AnacrontabEntry {
type Err = CrontabEntryParseError;
fn from_str(s: &str) -> Result<AnacrontabEntry, CrontabEntryParseError> {
let seps = [' ', '\t'];
let mut splits = s.split(&seps[..]).filter(|v| *v != "");
Ok(AnacrontabEntry {
period: try!(splits.next().map(|v| v.parse().map_err(CrontabEntryParseError::InvalidPeriod)).unwrap_or(Err(CrontabEntryParseError::MissingPeriod))),
delay: try!(splits.next().map(|v| v.parse().map_err(CrontabEntryParseError::InvalidDelay)).unwrap_or(Err(CrontabEntryParseError::MissingDelay))),
jobid: try!(splits.next().map(ToOwned::to_owned).ok_or(CrontabEntryParseError::MissingJobId)),
cmd: splits.collect::<Vec<&str>>().join(" ")
})
}
}
impl From<ScheduleParseError> for CrontabEntryParseError {
fn from(e: ScheduleParseError) -> CrontabEntryParseError {
CrontabEntryParseError::InvalidSchedule(e)
}
}
impl From<UserInfoParseError> for CrontabEntryParseError {
fn from(e: UserInfoParseError) -> CrontabEntryParseError {
CrontabEntryParseError::InvalidUser(e)
}
}
impl From<ParseIntError> for CrontabEntryParseError {
fn from(e: ParseIntError) -> CrontabEntryParseError {
CrontabEntryParseError::InvalidDelay(e)
}
}