expiretools.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include <iostream>
  2. #include <algorithm>
  3. #include "Exception.h"
  4. #include "Lexer.h"
  5. #include "FileName.h"
  6. #include "filetools.h"
  7. #include "rulecomp.h"
  8. #include "expiretools.h"
  9. using namespace std;
  10. void debugPrint(const std::string& s);
  11. void readSetRules(const KindConfig& conf,
  12. std::map<std::string, int >& setIdx,
  13. std::vector<SetRule>& backupSetRule)
  14. {
  15. Strings setRules = conf.getStrings("setRule");
  16. setIdx.clear();
  17. backupSetRule.clear();
  18. if (!setRules.empty())
  19. {
  20. for (const string& rule : setRules)
  21. {
  22. // read rule and store to vector
  23. Strings splittedRule;
  24. split(rule, splittedRule, ':');
  25. if (splittedRule.size() != 3)
  26. throw Exception("config", "Error in setRule: " + rule);
  27. string name = splittedRule[0];
  28. if (name == "expire")
  29. throw Exception("config", "Use of reserved name >>expire<< in setRule is forbidden");
  30. SetRule s;
  31. s.rule = rule;
  32. s.name = name;
  33. s.distance = stot(splittedRule[1]);
  34. s.keep = stot(splittedRule[2]);
  35. if (s.distance < 0 || s.keep < 0)
  36. throw Exception("SetRules", "Times must be positive");
  37. backupSetRule.push_back(s);
  38. }
  39. // sort backupSets by time to keep
  40. sort(backupSetRule.begin(), backupSetRule.end());
  41. // create map for index
  42. for (unsigned int i = 0; i < backupSetRule.size(); ++i)
  43. setIdx[backupSetRule[i].name] = i;
  44. }
  45. }
  46. void stringToDate(const string& dateString, DateTime& t, string& label)
  47. {
  48. Strings ss;
  49. split(dateString, ss, '-');
  50. if (ss.size() < 5)
  51. throw Exception("stringToDate", "date format invalid");
  52. label = ss[0];
  53. int Y = stoi(ss[1]);
  54. int M = stoi(ss[2]);
  55. int D = stoi(ss[3]);
  56. int h = stoi(ss[4]);
  57. int m = 0, s = 0;
  58. if (ss.size() > 5) // longImageName
  59. m = stoi(ss[5]);
  60. if (ss.size() > 6)
  61. s = stoi(ss[6]);
  62. t = DateTime(Y, M, D, h, m, s);
  63. }
  64. void parseRule(string rule,
  65. set<int>& M, set<int>& D, set<int>& W, set<int>& h,
  66. time_t& exptime)
  67. {
  68. for (unsigned int i = 0; i < rule.size(); ++i)
  69. rule[i] = tolower(rule[i]);
  70. substitute(rule, ' ', ',');
  71. reduceToOne(rule, ',');
  72. // rule = hour wday mday month <exptime>
  73. Lexer p(rule);
  74. h = getValues(p, 0, 23); // hour
  75. p.expect(',');
  76. W = getValues(p, 0, 7, 1); // wday
  77. p.expect(',');
  78. D = getValues(p, 1, 31); // day of month
  79. p.expect(',');
  80. M = getValues(p, 1, 12, 2); // month
  81. #if 0
  82. // debug-output
  83. cout << "hour: ";
  84. for (int i : h)
  85. cout << i << " ";
  86. cout << endl;
  87. cout << "wday: ";
  88. for (int i : W)
  89. cout << i << " ";
  90. cout << endl;
  91. cout << "mday: ";
  92. for (int i : D)
  93. cout << i << " ";
  94. cout << endl;
  95. cout << "month: ";
  96. for (int i : M)
  97. cout << i << " ";
  98. cout << endl;
  99. #endif
  100. string ts = p.getAll();
  101. substitute(ts, ',', ' ');
  102. exptime = stot(ts);
  103. if (exptime < 0)
  104. throw Exception("ExpireRule " + rule, "Time period must be positive");
  105. }
  106. DateTime getExpireDate(const DateTime& imageTime,
  107. const KindConfig& conf, string& rule)
  108. {
  109. DateTime expireTime;
  110. rule.clear();
  111. Strings expireRules = conf.getStrings("expireRule");
  112. for (unsigned int k = 0; k < expireRules.size(); ++k)
  113. {
  114. debugPrint("Checking rule " + expireRules[k]);
  115. set<int> M, D, W, h;
  116. set<int> Y, m, s;
  117. time_t expirePeriod;
  118. parseRule(expireRules[k], M, D, W, h, expirePeriod);
  119. if (imageTime.match(Y, M, D, W, h, m, s))
  120. {
  121. debugPrint("match");
  122. expireTime = imageTime + expirePeriod;
  123. rule = expireRules[k];
  124. // continue search: last rule matches
  125. }
  126. }
  127. if (rule.empty())
  128. throw Exception("expire", "no rule found");
  129. return expireTime;
  130. }