Prechádzať zdrojové kódy

formatting and small changes
handling of overflow in stol
handling of options
more exact time periods

Wolfgang Ortmann 9 rokov pred
rodič
commit
000567a2ef
5 zmenil súbory, kde vykonal 579 pridanie a 577 odobranie
  1. 3 13
      src/expiretools.cpp
  2. 6 3
      src/expiretools.h
  3. 26 21
      src/kind.ag
  4. 524 526
      src/kind.cpp
  5. 20 14
      src/stringtools.cpp

+ 3 - 13
src/expiretools.cpp

@@ -89,7 +89,8 @@ void parseRule(string rule,
 }
 
 
-DateTime getExpireDate(const DateTime& imageTime, const KindConfig& conf, string& rule)
+DateTime getExpireDate(const DateTime& imageTime,
+                       const KindConfig& conf, string& rule)
 {
   DateTime expireTime;
   rule.clear();
@@ -102,18 +103,7 @@ DateTime getExpireDate(const DateTime& imageTime, const KindConfig& conf, string
       set<int> Y, m, s;
       time_t expirePeriod;
       parseRule(expireRules[k], M, D, W, h, expirePeriod);
-#if 0
-      cout << expireRules[k] << endl;
-      for (auto v : M) cout << v << " ";
-      cout << endl;
-      for (auto v : D) cout << v << " ";
-      cout << endl;
-      for (auto v : W) cout << v << " ";
-      cout << endl;
-      for (auto v : h) cout << v << " ";
-      cout << endl;
-      cout << imageTime.getString('m') << endl;
-#endif
+
       if (imageTime.match(Y, M, D, W, h, m, s))
         {
           debugPrint("match");

+ 6 - 3
src/expiretools.h

@@ -6,10 +6,13 @@
 
 DateTime imageDate(const std::string& image);
 
-DateTime getExpireDate(const DateTime& imageTime, const KindConfig& conf, std::string& rule);
+DateTime getExpireDate(const DateTime& imageTime,
+                       const KindConfig& conf, std::string& rule);
 
-void createExpireFile(const std::string& image, 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);
+DateTime expireDate(const std::string& image,
+                    const KindConfig& conf, std::string& rule);
 
 #endif

+ 26 - 21
src/kind.ag

@@ -40,10 +40,12 @@
   opt2: if not given or empty kind looks for
   opt2:   /etc/kind/master.conf
   opt2:   /ffp/etc/kind/master.conf
-  opt: B, backuponly, void, backupOnly, Only backup/no expire, false
-  opt: E, expireonly, void, expireOnly, Only expire/no backup, false
-  opt: D, dryrun, Void, dryRun, Dry run (no real backup), false
+  opt: B, backup, void, doBackup, Backup, false
+  opt: E, expire, void, doExpire, Expire, false
   opt: C, listconfig, void, listConfig, Show configuration, false
+  opt2:   if none of backup, expire or listconfig is specified,
+  opt2:   backup and expire is assumed.
+  opt: D, dryrun, Void, dryRun, Dry run (no real backup), false
   opt: v, verbose, Void, verbose,  Verbose,  false
   opt: d, debug, Void, debug, Debug output of many data, false
   opt: q, quiet, Void, quiet, Be quiet - no messages, false
@@ -177,8 +179,10 @@ void backupVault(const string& vault,
 
       readVaultConfig(vault, conf);
 
+      // where to store
       string vaultpath = findVault(vault);
 
+      // image path
       string imageName = getImageName(conf);
       if (!imageName.empty())
         imageName += '-';
@@ -216,6 +220,7 @@ void backupVault(const string& vault,
           error.close();
         }
 
+      // create source descriptor
       string host;
       if (conf.hasKey("host"))
         host = conf.getString("host");
@@ -237,9 +242,15 @@ void backupVault(const string& vault,
       if (!conf.getBool("ignorePermission"))
         rsyncCmd += "-pgo";
       vector<string> rso = conf.getStrings("rsyncOption");
-      for (string opt : rso)
+      for (const string& opt : rso)
         rsyncCmd += opt + " ";
 
+      // excludes
+      Strings excluded;
+
+      if (conf.hasKey("exclude"))
+        excluded += conf.getStrings("exclude");
+
       if (!host.empty())  // shell mode
         {
           // cout << "USING SHELLMODE '" << host << "'" << endl;
@@ -252,9 +263,6 @@ void backupVault(const string& vault,
 
           rshCommand += " " + userAtHost;
 
-          // excludes
-          Strings excluded;
-
           string userExcludeCommand = conf.getString("userExcludeCommand");
 
           if (!userExcludeCommand.empty())
@@ -291,9 +299,6 @@ void backupVault(const string& vault,
                 excluded += excludes2;
             }
 
-          if (conf.hasKey("exclude"))
-            excluded += conf.getStrings("exclude");
-
           if (!dryRun)
             strings2File(excluded, imageFullName + "/exclude");
 
@@ -311,13 +316,6 @@ void backupVault(const string& vault,
       else
         {
           // cout << "USING SERVERMODE" << endl;
-          vector<string> excluded;
-          if (conf.hasKey("exclude"))
-            {
-              Strings excludes = conf.getStrings("exclude");
-              for (string s : excludes)
-                excluded.push_back(s);
-            }
 
           if (!dryRun)
             strings2File(excluded, imageFullName + "/exclude");
@@ -340,7 +338,7 @@ void backupVault(const string& vault,
           // strings2File(backupResult, imageFullName + "/rsync-log");
           if (rc == 0 ||
               rc == 24 || // "no error" or "vanished source files" (ignored)
-              rc == 6114) // workaround for wrong exit code ??!!
+              rc == 6144) // workaround for wrong exit code ??!!
             {
               unlink(errorfile.c_str());
               string lastLink = vaultpath + "/last";
@@ -580,9 +578,16 @@ int main(int argc, char* argv[])
   string lockFile;
   try
     {
-      if (debug)
+      // handling of parameters and switches
+      if (debug)        // debug implies verbose
         verbose = true;
 
+      if (!doBackup && !doExpire && !listConfig)
+        {
+          doBackup = true;
+          doExpire = true;
+        }
+
       KindConfig conf;
 
       // default-values
@@ -646,7 +651,7 @@ int main(int argc, char* argv[])
       else
         vaults.push_back(vault);
 
-      if (!expireOnly)
+      if (doBackup)
         for (unsigned int i = 0; i < vaults.size(); ++i)
           {
             backupVault(vaults[i], conf, imageTime, fullImage);
@@ -662,7 +667,7 @@ int main(int argc, char* argv[])
               }
           }
 
-      if (!backupOnly)
+      if (doExpire)
         for (unsigned int i = 0; i < vaults.size(); ++i)
           expireVault(vaults[i], conf, imageTime);
 

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 524 - 526
src/kind.cpp


+ 20 - 14
src/stringtools.cpp

@@ -149,35 +149,41 @@ time_t stot(const string& s)
     unit += 's';
   if (unit == "secs")
     return val;
-  val *= 60;
   if (unit == "mins")
-    return val;
-  val *= 60;
+    return val * 60;
   if (unit == "hours")
-    return val;
-  val *= 24;
+    return val * 60 * 60;
   if (unit == "days")
-    return val;
+    return val * 60 * 60 * 24;
   if (unit == "weeks")
-    return val * 7;
-  val *= 30;
+    return val * 60 * 60 * 24 * 7;
   if (unit == "months")
-    return val;
-  val *= 12;
-  if (unit != "years")
-    throw Exception("Parse time", "unknown time unit " + unit);
-  return val;
+    return val * 60 * 60 * 24 * 30;
+  if (unit == "years")
+    return val * 60 * 60 * 24 * 365;
+  throw Exception("Parse time", "unknown time unit " + unit);
 }
 
 long int getNumber(const string& l)
 {
   // read *all* digits from string l ignoring all other characters
+  // "read 3,000,421 Bytes" => 3000421
   string d;
   for (unsigned int i = 0; i < l.size(); ++i)
     if (isdigit(l[i]))
       d += l[i];
   // return long int value of digits
-  return stol(d);
+  long int res = 0;
+  try
+    {
+      res = stol(d);
+    }
+  catch (...)
+    {
+      // we ignore overflow here because value is only informative
+      cout << "stol failed on " << l << endl;
+    }
+  return res;
 }
 
 void replacePlaceHolder(string& s,

Niektoré súbory nie sú zobrazené, pretože je v týchto rozdielových dátach zmenené mnoho súborov