expiretools.cpp 3.1 KB

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