1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
5    Copyright (C) 2011-2012 Planets Communications B.V.
6    Copyright (C) 2013-2019 Bareos GmbH & Co. KG
7 
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12 
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    Affero General Public License for more details.
17 
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22 */
23 
24 #include "include/bareos.h"
25 #include "dird/date_time_bitfield.h"
26 #include "dird/run_hour_validator.h"
27 
28 #include <array>
29 #include <iostream>
30 
31 namespace directordaemon {
32 
33 static constexpr int kMonthsPerYear{12};
34 static constexpr int kDaysPerWeek{7};
35 
test(const std::array<int,kMonthsPerYear> a,int day_of_year)36 static bool test(const std::array<int, kMonthsPerYear> a, int day_of_year)
37 {
38   for (auto d : a) {
39     if (day_of_year > ((d - 1) - kDaysPerWeek) && day_of_year <= (d - 1)) {
40       return true;
41     }
42   }
43   return false;
44 }
45 
IsDayOfYearInLastWeek(int year,int day_of_year)46 static bool IsDayOfYearInLastWeek(int year, int day_of_year)
47 {
48   if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
49     return test({31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366},
50                 day_of_year);  // leap year
51   }
52   return test({31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
53               day_of_year);
54 }
55 
56 // calculate the current hour of the year
RunHourValidator(time_t time)57 RunHourValidator::RunHourValidator(time_t time) : time_(time)
58 {
59   struct tm tm = {0};
60   localtime_r(&time_, &tm);
61   hour_ = tm.tm_hour;
62   mday_ = tm.tm_mday - 1;
63   wday_ = tm.tm_wday;
64   month_ = tm.tm_mon;
65   wom_ = mday_ / kDaysPerWeek;
66   woy_ = TmWoy(time_); /* get week of year */
67   yday_ = tm.tm_yday;  /* get day of year */
68   is_last_week_ = IsDayOfYearInLastWeek(tm.tm_year + 1900, yday_);
69 }
70 
71 // check if the calculated hour matches the runtime bitfiled
TriggersOn(const DateTimeBitfield & date_time_bitfield)72 bool RunHourValidator::TriggersOn(const DateTimeBitfield& date_time_bitfield)
73 {
74   return BitIsSet(hour_, date_time_bitfield.hour) &&
75          BitIsSet(mday_, date_time_bitfield.mday) &&
76          BitIsSet(wday_, date_time_bitfield.wday) &&
77          BitIsSet(month_, date_time_bitfield.month) &&
78          (BitIsSet(wom_, date_time_bitfield.wom) ||
79           (is_last_week_ && date_time_bitfield.last_week_of_month)) &&
80          BitIsSet(woy_, date_time_bitfield.woy);
81 }
82 
PrintDebugMessage(int debuglevel) const83 void RunHourValidator::PrintDebugMessage(int debuglevel) const
84 {
85   Dmsg8(debuglevel, "now = %x: h=%d m=%d md=%d wd=%d wom=%d woy=%d yday=%d\n",
86         time_, hour_, month_, mday_, wday_, wom_, woy_, yday_);
87 }
88 
89 }  // namespace directordaemon
90