|
@@ -16,12 +16,15 @@
|
|
|
#include "Exception.h"
|
|
#include "Exception.h"
|
|
|
|
|
|
|
|
#include "DateTime.h"
|
|
#include "DateTime.h"
|
|
|
|
|
+#include "Strings.h"
|
|
|
#include "FileName.h"
|
|
#include "FileName.h"
|
|
|
|
|
+
|
|
|
#include "KindConfig.h"
|
|
#include "KindConfig.h"
|
|
|
#include "filetools.h"
|
|
#include "filetools.h"
|
|
|
#include "Lexer.h"
|
|
#include "Lexer.h"
|
|
|
#include "rulecomp.h"
|
|
#include "rulecomp.h"
|
|
|
-#include "Strings.h"
|
|
|
|
|
|
|
+
|
|
|
|
|
+#include "expiretools.h"
|
|
|
|
|
|
|
|
/*AppGen
|
|
/*AppGen
|
|
|
%% Beschreibung des Programmes:
|
|
%% Beschreibung des Programmes:
|
|
@@ -51,6 +54,8 @@ using namespace std;
|
|
|
|
|
|
|
|
/*AppGen:Global*/
|
|
/*AppGen:Global*/
|
|
|
|
|
|
|
|
|
|
+void createExpireFile(const string& image, const KindConfig& conf, string& rule);
|
|
|
|
|
+
|
|
|
Strings banks;
|
|
Strings banks;
|
|
|
|
|
|
|
|
typedef pair<long int, long int> Sizes;
|
|
typedef pair<long int, long int> Sizes;
|
|
@@ -359,6 +364,9 @@ void backupVault(const string& vault,
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
cout << "Not executing " << rsyncCmd << endl;
|
|
cout << "Not executing " << rsyncCmd << endl;
|
|
|
|
|
+
|
|
|
|
|
+ string rule;
|
|
|
|
|
+ createExpireFile(imageFullName, conf, rule);
|
|
|
}
|
|
}
|
|
|
catch (Exception ex)
|
|
catch (Exception ex)
|
|
|
{
|
|
{
|
|
@@ -366,142 +374,9 @@ void backupVault(const string& vault,
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-DateTime stringToDate(const string& dateString)
|
|
|
|
|
-{
|
|
|
|
|
- Strings ss;
|
|
|
|
|
- split(dateString, ss, '-');
|
|
|
|
|
- if (ss.size() < 5)
|
|
|
|
|
- throw Exception("stringToDate", "date format invalid");
|
|
|
|
|
- int Y = stoi(ss[1]);
|
|
|
|
|
- int M = stoi(ss[2]);
|
|
|
|
|
- int D = stoi(ss[3]);
|
|
|
|
|
- int h = stoi(ss[4]);
|
|
|
|
|
- int m = 0, s = 0;
|
|
|
|
|
- if (ss.size() > 5) // longImageName
|
|
|
|
|
- m = stoi(ss[5]);
|
|
|
|
|
- if (ss.size() > 6)
|
|
|
|
|
- s = stoi(ss[6]);
|
|
|
|
|
- return DateTime(Y, M, D, h, m, s);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-DateTime imageDate(const string& image)
|
|
|
|
|
-{
|
|
|
|
|
- FileName fn(image);
|
|
|
|
|
- return stringToDate(fn.getName());
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-void parseRule(string rule,
|
|
|
|
|
- set<int>& M, set<int>& D, set<int>& W, set<int>& h,
|
|
|
|
|
- time_t& exptime)
|
|
|
|
|
-{
|
|
|
|
|
- for (unsigned int i = 0; i < rule.size(); ++i)
|
|
|
|
|
- rule[i] = tolower(rule[i]);
|
|
|
|
|
-
|
|
|
|
|
- substitute(rule, ' ', ',');
|
|
|
|
|
- reduceToOne(rule, ',');
|
|
|
|
|
-
|
|
|
|
|
- // rule = hour wday mday month <exptime>
|
|
|
|
|
- Lexer p(rule);
|
|
|
|
|
-
|
|
|
|
|
- h = getValues(p, 0, 23); // hour
|
|
|
|
|
-
|
|
|
|
|
- p.expect(',');
|
|
|
|
|
-
|
|
|
|
|
- W = getValues(p, 0, 7, 1); // wday
|
|
|
|
|
-
|
|
|
|
|
- p.expect(',');
|
|
|
|
|
-
|
|
|
|
|
- D = getValues(p, 1, 31); // day of month
|
|
|
|
|
-
|
|
|
|
|
- p.expect(',');
|
|
|
|
|
-
|
|
|
|
|
- M = getValues(p, 1, 12, 2); // month
|
|
|
|
|
-
|
|
|
|
|
-#if 0
|
|
|
|
|
- // debug-output
|
|
|
|
|
- cout << "hour: ";
|
|
|
|
|
- for (int i : h)
|
|
|
|
|
- cout << i << " ";
|
|
|
|
|
- cout << endl;
|
|
|
|
|
- cout << "wday: ";
|
|
|
|
|
- for (int i : W)
|
|
|
|
|
- cout << i << " ";
|
|
|
|
|
- cout << endl;
|
|
|
|
|
- cout << "mday: ";
|
|
|
|
|
- for (int i : D)
|
|
|
|
|
- cout << i << " ";
|
|
|
|
|
- cout << endl;
|
|
|
|
|
- cout << "month: ";
|
|
|
|
|
- for (int i : M)
|
|
|
|
|
- cout << i << " ";
|
|
|
|
|
- cout << endl;
|
|
|
|
|
-#endif
|
|
|
|
|
-
|
|
|
|
|
- string ts = p.getAll();
|
|
|
|
|
- substitute(ts, ',', ' ');
|
|
|
|
|
- exptime = stot(ts);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-DateTime getExpireDate(const DateTime& imageTime, const KindConfig& conf, string& rule)
|
|
|
|
|
-{
|
|
|
|
|
- DateTime expireTime;
|
|
|
|
|
- rule.clear();
|
|
|
|
|
- Strings expireRules = conf.getStrings("expireRule");
|
|
|
|
|
- for (unsigned int k = 0; k < expireRules.size(); ++k)
|
|
|
|
|
- {
|
|
|
|
|
- debugPrint("Checking rule " + expireRules[k]);
|
|
|
|
|
-
|
|
|
|
|
- set<int> M, D, W, h;
|
|
|
|
|
- set<int> Y, m, s;
|
|
|
|
|
- time_t expirePeriod;
|
|
|
|
|
- parseRule(expireRules[k], M, D, W, h, expirePeriod);
|
|
|
|
|
-
|
|
|
|
|
- if (imageTime.match(Y, M, D, W, h, m, s))
|
|
|
|
|
- {
|
|
|
|
|
- debugPrint("match");
|
|
|
|
|
- expireTime = imageTime + expirePeriod;
|
|
|
|
|
- rule = expireRules[k];
|
|
|
|
|
- // continue search: last rule matches
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- if (rule.empty())
|
|
|
|
|
- throw Exception("expire", "no rule found");
|
|
|
|
|
- return expireTime;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-DateTime expireDate(const string& image, const KindConfig& conf, string& rule)
|
|
|
|
|
-{
|
|
|
|
|
- string expireFileName = image + "/expires";
|
|
|
|
|
- debugPrint("reading " + expireFileName);
|
|
|
|
|
-#if 1
|
|
|
|
|
- if (fileExists(expireFileName))
|
|
|
|
|
- {
|
|
|
|
|
- Strings s;
|
|
|
|
|
- file2Strings(expireFileName, s);
|
|
|
|
|
- if (s.empty())
|
|
|
|
|
- throw Exception("expireDate", "expire empty");
|
|
|
|
|
- if (s.size() > 1)
|
|
|
|
|
- rule = s[1];
|
|
|
|
|
- return stringToDate(s[0]);
|
|
|
|
|
- }
|
|
|
|
|
-#endif
|
|
|
|
|
- // epire date from image date + rules
|
|
|
|
|
- DateTime imageTime = imageDate(image);
|
|
|
|
|
- DateTime expireTime = getExpireDate(imageTime, conf, rule);
|
|
|
|
|
-
|
|
|
|
|
- // create file
|
|
|
|
|
- ofstream xfile(expireFileName);
|
|
|
|
|
- // we use prefix "expire-" to allow same parsing as for imagenames
|
|
|
|
|
- xfile << "expire-" << expireTime.getString('m') << endl;
|
|
|
|
|
- xfile << rule << endl;
|
|
|
|
|
- xfile.close();
|
|
|
|
|
- return expireTime;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
int removeDir(const string& path)
|
|
int removeDir(const string& path)
|
|
|
{
|
|
{
|
|
|
debugPrint("removeDir " + path);
|
|
debugPrint("removeDir " + path);
|
|
|
-
|
|
|
|
|
DIR* d = opendir(path.c_str());
|
|
DIR* d = opendir(path.c_str());
|
|
|
|
|
|
|
|
int r = -1;
|
|
int r = -1;
|
|
@@ -510,19 +385,14 @@ int removeDir(const string& path)
|
|
|
struct dirent* p;
|
|
struct dirent* p;
|
|
|
|
|
|
|
|
r = 0;
|
|
r = 0;
|
|
|
-
|
|
|
|
|
while (!r && (p = readdir(d)))
|
|
while (!r && (p = readdir(d)))
|
|
|
{
|
|
{
|
|
|
int r2 = 0;
|
|
int r2 = 0;
|
|
|
-
|
|
|
|
|
string fn = p->d_name;
|
|
string fn = p->d_name;
|
|
|
-
|
|
|
|
|
if (fn != "." && fn != "..")
|
|
if (fn != "." && fn != "..")
|
|
|
{
|
|
{
|
|
|
fn = path + "/" + fn;
|
|
fn = path + "/" + fn;
|
|
|
-
|
|
|
|
|
debugPrint("-- " + fn);
|
|
debugPrint("-- " + fn);
|
|
|
-
|
|
|
|
|
struct stat statbuf;
|
|
struct stat statbuf;
|
|
|
if (lstat(fn.c_str(), &statbuf) == 0)
|
|
if (lstat(fn.c_str(), &statbuf) == 0)
|
|
|
{
|
|
{
|
|
@@ -551,14 +421,12 @@ int removeDir(const string& path)
|
|
|
}
|
|
}
|
|
|
r = r2;
|
|
r = r2;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
closedir(d);
|
|
closedir(d);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (r == 0)
|
|
if (r == 0)
|
|
|
{
|
|
{
|
|
|
debugPrint("Remove Dir itself " + path);
|
|
debugPrint("Remove Dir itself " + path);
|
|
|
-
|
|
|
|
|
r = rmdir(path.c_str());
|
|
r = rmdir(path.c_str());
|
|
|
}
|
|
}
|
|
|
|
|
|