Bladeren bron

Formatting, cleaning, comments

Wolfgang Ortmann 9 jaren geleden
bovenliggende
commit
ccbcef69f2
5 gewijzigde bestanden met toevoegingen van 447 en 523 verwijderingen
  1. 21 54
      src/expiretools.cpp
  2. 6 6
      src/expiretools.h
  3. 13 34
      src/kind.ag
  4. 379 427
      src/kind.cpp
  5. 28 2
      src/stringtools.h

+ 21 - 54
src/expiretools.cpp

@@ -12,22 +12,28 @@ using namespace std;
 
 void debugPrint(const std::string& s);
 
-DateTime stringToDate(const string& dateString)
+void readSetRules(const KindConfig &conf,
+		    map<string, pair<time_t, time_t> > &ruleSet,
+		    map<string, string> &backupSetRule)
 {
-  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);
+  Strings setRules = conf.getStrings("setRules");
+  if (!setRules.empty())
+    {
+      for (const string& rule : setRules)
+	{
+	  Strings splittedRule;
+	  split(rule, splittedRule, ':');
+	  if (splittedRule.size() != 3)
+	    throw Exception("config", "Error in setRule: " + rule);
+	  string name = splittedRule[0];
+	  if (name == "expire")
+	    throw Exception("config", "Use of reserved name expire in setRule is forbidden");
+	  backupSetRule[name] = rule;
+	  time_t distance = stot(splittedRule[1]);
+	  time_t keep = stot(splittedRule[2]);
+	  ruleSet[name] = pair<time_t, time_t>(distance, keep);
+	}
+    }
 }
 
 void stringToDate(const string& dateString, DateTime& t, string& label)
@@ -49,12 +55,6 @@ void stringToDate(const string& dateString, DateTime& t, string& label)
   t = 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)
@@ -136,36 +136,3 @@ DateTime getExpireDate(const DateTime& imageTime,
   return expireTime;
 }
 
-void createExpireFile(const string& image, const KindConfig& conf, string& rule)
-{
-  string expireFileName = image + "/expires";
-  // 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();
-}
-
-DateTime expireDate(const string& image, const KindConfig& conf, string& rule)
-{
-  string expireFileName = image + "/expires";
-  if (!fileExists(expireFileName)) // lost expire file ?
-    {
-      cout << "Recreate expire file " << expireFileName << endl;
-      createExpireFile(image, conf, rule);
-    }
-
-  debugPrint("reading " + 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]);
-}

+ 6 - 6
src/expiretools.h

@@ -1,19 +1,19 @@
 #ifndef KIND_EXPIRE_TOOLS
 #define KIND_EXPIRE_TOOLS
 
+#include <map>
 #include "DateTime.h"
 #include "KindConfig.h"
 
+void readSetRules(const KindConfig &conf,
+		  std::map<std::string, std::pair<time_t, time_t> > &ruleSet,
+		  std::map<std::string, std::string> &backupSetRule);
+
 DateTime imageDate(const std::string& image);
 
+// get expire time using expire rules (not backup sets)
 DateTime getExpireDate(const DateTime& imageTime,
                        const KindConfig& conf, std::string& rule);
 
-void createExpireFile(const std::string& image,
-                      const KindConfig& conf, std::string& rule);
-
-DateTime expireDate(const std::string& image,
-                    const KindConfig& conf, std::string& rule);
-
 void stringToDate(const std::string& dateString, DateTime& t, std::string& label);
 #endif

+ 13 - 34
src/kind.ag

@@ -164,16 +164,6 @@ string getImageName(const KindConfig& conf)
   return res;
 }
 
-#if 0
-bool isValidImage(const string& imageName)
-{
-  return dirExists(imageName) &&
-         !fileExists(imageName + "/error") &&
-         fileExists(imageName + "/expires") &&
-         dirExists(imageName + "/tree");
-}
-#endif
-
 Images findImages(const string& vaultpath, const KindConfig& conf, bool all)
 {
   Strings dirs;
@@ -392,43 +382,29 @@ void backupVault(const string& vault,
 
       // existing images
       Images validImageList = findImages(vaultpath, conf, false);
-      string currentSet = "expire";
+      string currentSet = "expire"; // we are not using backupSets
 
-      // check if we are using setRules
+      // check if we are using backup sets
 
       map<string, pair<time_t, time_t> > ruleSet;
       map<string, string> backupSetRule;
 
       if (conf.hasKey("setRules"))
         {
-          Strings setRules = conf.getStrings("setRules");
-          if (!setRules.empty())
+	  readSetRules(conf,ruleSet,backupSetRule);
+          if (!ruleSet.empty())
             {
               backupNow = false;
-              for (const string& rule : setRules)
-                {
-                  Strings splittedRule;
-                  split(rule, splittedRule, ':');
-                  if (splittedRule.size() != 3)
-                    throw Exception("config", "Error in setRule: " + rule);
-                  string name = splittedRule[0];
-                  if (name == "expire")
-                    throw Exception("config", "Can't use reserved name expire in setRule");
-                  backupSetRule[name] = rule;
-                  time_t distance = stot(splittedRule[1]);
-                  time_t keep = stot(splittedRule[2]);
-                  ruleSet[name] = pair<time_t, time_t>(distance, keep);
-                }
 
               // find time for nextBackup for every backupSet
               map<string, DateTime> nextBackup;
 
-              // set default time for next Backup to now
+              // find time for next backup
+              // set default time for next Backup to now 
+	      //    (if there is no image yet)
               for (auto rule : ruleSet)
                 nextBackup[rule.first] = imageTime;
 
-              // find time for next backup
-              //
               for (const Image& image : validImageList)
                 {
                   if (image.series != "expire")
@@ -449,7 +425,7 @@ void backupVault(const string& vault,
               for (auto rule : ruleSet)
                 {
                   string name = rule.first;
-                  if (nextBackup[name] <= imageTime)
+                  if (nextBackup[name] <= imageTime + 5) // small offset of 5s for "jitter"
                     {
                       backupNow = true;
                       if (currentSet.empty())
@@ -462,7 +438,8 @@ void backupVault(const string& vault,
         }
 
       verbosePrint("backup to \"" + imageFullName + "\"");
-      verbosePrint("backup set is \"" + currentSet + "\"");
+      if (!currentSet.empty())
+	verbosePrint("backup set is \"" + currentSet + "\"");
 
       if (backupNow)
         {
@@ -480,10 +457,12 @@ void backupVault(const string& vault,
 
           if (!dryRun)
             {
+	      // set symlink to last image
               string lastLink = vaultpath + "/last";
               unlink(lastLink.c_str());
               symlink(imageFullName.c_str(), lastLink.c_str());
 
+	      // write expire date to file
               DateTime expireTime;
               string rule;
               if (currentSet == "expire")
@@ -530,7 +509,7 @@ void expireVault(const string& vault, KindConfig conf, DateTime now)
     {
       debugPrint(image.name);
 
-      DateTime imageTime = imageDate(image.name);
+      DateTime imageTime = image.time;
 
       if (imageTime != now &&          // ignore just created image
           image.name != lastValidImage // ignore last valid image

File diff suppressed because it is too large
+ 379 - 427
src/kind.cpp


+ 28 - 2
src/stringtools.h

@@ -11,27 +11,53 @@
 std::string delSpaces(const std::string& s);
 std::string trim(const std::string& s);
 
+// reduce each occurence of char to one char
+//   if a char is used as delimiter and may be doubled
+//   this function 
 void reduceToOne(std::string& s, char c);
+
+// replace each occurence of c1 with character c2
 void substitute(std::string& s, char c1, char c2);
 
 // split in parts
+// uses del as delimiter to split a string in parts
+// if expectedParts is given and not zero a wrong 
+// number of parts causes an exception
 int split(const std::string& s, Strings& parts,
           char del, int expectedParts = 0);
 
-// string parsing
+// functions for parsing of string
+// parsing starts at position i and sets i to the 
+// position after end of read part
+// white space after read part is skipped
+
+// skip white spaces 
 void skipWS(const std::string& s, unsigned  int& i);
+
+// read int value
 int getInt(const std::string& s, unsigned int& i);
+
+// read long int value
 long int getLongInt(const std::string& s, unsigned int& i);
+
+// read word (series of letters)
 std::string getWord(const std::string& s, unsigned int& i);
 
+// pick all digits from a string and convert to long int
+// quick and dirty solution to read formatted ints like "1,000,000"
 long int getNumber(const std::string& l);
 
+// compares first characters of s with the string start
 bool startsWith(const std::string& s, const std::string& start);
 
 // string to time
+// converts a given string to a time period (in seconds)
 time_t stot(const std::string& s);
 
-void replacePlaceHolder(std::string& format,
+// replaces in string template the placeholder string with the 
+// string given as content
+// "my name is %name","%name","Wolfgang" -> "my name is Wolfgang"
+void replacePlaceHolder(std::string& templ,
                         const std::string& placeholder,
                         const std::string& content);
 

Some files were not shown because too many files changed in this diff