浏览代码

forbid overriding config values in same file

Wolfgang Ortmann 9 年之前
父节点
当前提交
5e33c0349d
共有 6 个文件被更改,包括 107 次插入108 次删除
  1. 2 2
      src/Image.cpp
  2. 15 15
      src/KindConfig.cpp
  3. 3 3
      src/KindConfig.h
  4. 20 21
      src/kind.ag
  5. 65 65
      src/kind.cpp
  6. 2 2
      src/stringtools.cpp

+ 2 - 2
src/Image.cpp

@@ -38,11 +38,11 @@ void Image::printInfo() const
   if (valid)
   if (valid)
     {
     {
       if (series != "expire")
       if (series != "expire")
-	cout << "backup set: " << series << endl;
+        cout << "backup set: " << series << endl;
       cout << "created: " << time.getString('h') << endl;
       cout << "created: " << time.getString('h') << endl;
       cout << "expires: " << expire.getString('h') << " -  " << timeString(expire - DateTime::now()) << endl;
       cout << "expires: " << expire.getString('h') << " -  " << timeString(expire - DateTime::now()) << endl;
     }
     }
-  
+
   else
   else
     cout << "invalid" << endl;
     cout << "invalid" << endl;
 }
 }

+ 15 - 15
src/KindConfig.cpp

@@ -1,4 +1,6 @@
 #include <iostream>
 #include <iostream>
+#include <fstream>
+#include <set>
 
 
 #include "Exception.h"
 #include "Exception.h"
 #include "KindConfig.h"
 #include "KindConfig.h"
@@ -11,16 +13,16 @@ KindConfig::KindConfig(const std::string& fn)
   addFile(fn);
   addFile(fn);
 }
 }
 
 
-KindConfig::KindConfig(std::istream& is)
+void KindConfig::addFile(const std::string& fn)
 {
 {
-  // settings.clear();
-  addFile(is);
-}
+  ifstream is(fn);
+  if (!is.good())
+    throw Exception("read config", string("Cannot open ") + fn + " for reading");
 
 
-void KindConfig::addFile(std::istream& is)
-{
   string line;
   string line;
   string lastkey;
   string lastkey;
+  set<string> usedKeys;
+
   while (getline(is, line))
   while (getline(is, line))
     {
     {
       line = trim(line);
       line = trim(line);
@@ -35,22 +37,20 @@ void KindConfig::addFile(std::istream& is)
             throw Exception("read config", "empty key");
             throw Exception("read config", "empty key");
 
 
           if (del == "=")
           if (del == "=")
-            settings[key].clear();
+            {
+              if (usedKeys.count(key) != 0)
+                throw Exception("Read config file " + fn, "entry overrides existing key \"" + key + "\"");
+              // clear previous entries
+              settings[key].clear();
+            }
 
 
           settings[key].push_back(value);
           settings[key].push_back(value);
+          usedKeys.insert(key);
           lastkey = key;
           lastkey = key;
         }
         }
     }
     }
 }
 }
 
 
-void KindConfig::addFile(const std::string& fn)
-{
-  ifstream is(fn);
-  if (!is.good())
-    throw Exception("read config", string("Cannot open ") + fn + " for reading");
-  addFile(is);
-}
-
 bool KindConfig::hasKey(std::string key) const
 bool KindConfig::hasKey(std::string key) const
 {
 {
   key = trim(key);
   key = trim(key);

+ 3 - 3
src/KindConfig.h

@@ -1,7 +1,7 @@
 #ifndef Kind_CONFIG_H
 #ifndef Kind_CONFIG_H
 #define Kind_CONFIG_H
 #define Kind_CONFIG_H
 
 
-#include <fstream>
+//#include <fstream>
 #include <map>
 #include <map>
 #include <string>
 #include <string>
 #include "Strings.h"
 #include "Strings.h"
@@ -12,10 +12,10 @@ class KindConfig
 public:
 public:
   KindConfig() {}
   KindConfig() {}
   KindConfig(const std::string& fn);
   KindConfig(const std::string& fn);
-  KindConfig(std::istream& is);
+  //  KindConfig(std::istream& is);
 
 
   void addFile(const std::string& fn);
   void addFile(const std::string& fn);
-  void addFile(std::istream& is);
+  //  void addFile(std::istream& is);
   void add(const std::string& key, const std::string& value)
   void add(const std::string& key, const std::string& value)
   {
   {
     settings[key].push_back(value);
     settings[key].push_back(value);

+ 20 - 21
src/kind.ag

@@ -78,14 +78,15 @@ void readSizes(const string& logSizeFile)
           string vault = getWord(s, i);
           string vault = getWord(s, i);
           long int s1 = getLongInt(s, i);
           long int s1 = getLongInt(s, i);
           long int s2 = getLongInt(s, i);
           long int s2 = getLongInt(s, i);
-	  try {
-	    findVault(vault);
-	    sizes[vault] = Sizes(s1, s2);
-	  }
-	  catch(...)
-	    {
-	      // ignore missing vaults
-	    }
+          try
+            {
+              findVault(vault);
+              sizes[vault] = Sizes(s1, s2);
+            }
+          catch (...)
+            {
+              // ignore missing vaults
+            }
         }
         }
     }
     }
 }
 }
@@ -221,13 +222,13 @@ void listImageInfo(const string& vault,
   readVaultConfig(vault, conf);
   readVaultConfig(vault, conf);
   string vaultPath = findVault(vault);
   string vaultPath = findVault(vault);
   Images imageList = findImages(vaultPath, conf, true);
   Images imageList = findImages(vaultPath, conf, true);
-  cout << "---"<<endl;
+  cout << "---" << endl;
   for (auto img : imageList)
   for (auto img : imageList)
     {
     {
       if (img.series == backupSet || backupSet.empty())
       if (img.series == backupSet || backupSet.empty())
         {
         {
-	  img.printInfo();
-	  cout << "---"<<endl;
+          img.printInfo();
+          cout << "---" << endl;
         }
         }
     }
     }
 }
 }
@@ -613,9 +614,7 @@ void expireVault(const string& vault, KindConfig conf, DateTime now)
   for (Image image : imagelist)
   for (Image image : imagelist)
     {
     {
       if (debug)
       if (debug)
-	{
-	  image.printInfo();
-	}
+        image.printInfo();
 
 
       DateTime imageTime = image.time;
       DateTime imageTime = image.time;
 
 
@@ -742,13 +741,13 @@ int main(int argc, char* argv[])
 
 
 
 
       for (string vault : vaults)
       for (string vault : vaults)
-	{
-	  if (doBackup)
-	    if (backupVault(vault, conf, imageTime, fullImage, forcedBackupSet))
-	      writeSizes(logSizeFile);
-	  if (doExpire)
-	    expireVault(vault, conf, imageTime);
-	}
+        {
+          if (doBackup)
+            if (backupVault(vault, conf, imageTime, fullImage, forcedBackupSet))
+              writeSizes(logSizeFile);
+          if (doExpire)
+            expireVault(vault, conf, imageTime);
+        }
 
 
       if (!quiet)
       if (!quiet)
         cout << DateTime::now().getString('h') << ": finished" << endl;
         cout << DateTime::now().getString('h') << ": finished" << endl;

+ 65 - 65
src/kind.cpp

@@ -168,21 +168,21 @@ void readSizes(const string& logSizeFile)
 # 80 "kind.ag"
 # 80 "kind.ag"
           long int s2 = getLongInt(s, i);
           long int s2 = getLongInt(s, i);
 # 81 "kind.ag"
 # 81 "kind.ag"
-	  try {
+          try {
 # 82 "kind.ag"
 # 82 "kind.ag"
-	    findVault(vault);
+              findVault(vault);
 # 83 "kind.ag"
 # 83 "kind.ag"
-	    sizes[vault] = Sizes(s1, s2);
+              sizes[vault] = Sizes(s1, s2);
 # 84 "kind.ag"
 # 84 "kind.ag"
-	  }
+            }
 # 85 "kind.ag"
 # 85 "kind.ag"
-	  catch(...)
+          catch (...)
 # 86 "kind.ag"
 # 86 "kind.ag"
-	    {
+            {
 # 87 "kind.ag"
 # 87 "kind.ag"
-	      // ignore missing vaults
+              // ignore missing vaults
 # 88 "kind.ag"
 # 88 "kind.ag"
-	    }
+            }
 # 89 "kind.ag"
 # 89 "kind.ag"
         }
         }
 # 90 "kind.ag"
 # 90 "kind.ag"
@@ -454,7 +454,7 @@ void listImageInfo(const string& vault,
 # 223 "kind.ag"
 # 223 "kind.ag"
   Images imageList = findImages(vaultPath, conf, true);
   Images imageList = findImages(vaultPath, conf, true);
 # 224 "kind.ag"
 # 224 "kind.ag"
-  cout << "---"<<endl;
+  cout << "---" << endl;
 # 225 "kind.ag"
 # 225 "kind.ag"
   for (auto img : imageList)
   for (auto img : imageList)
 # 226 "kind.ag"
 # 226 "kind.ag"
@@ -464,9 +464,9 @@ void listImageInfo(const string& vault,
 # 228 "kind.ag"
 # 228 "kind.ag"
         {
         {
 # 229 "kind.ag"
 # 229 "kind.ag"
-	  img.printInfo();
+          img.printInfo();
 # 230 "kind.ag"
 # 230 "kind.ag"
-	  cout << "---"<<endl;
+          cout << "---" << endl;
 # 231 "kind.ag"
 # 231 "kind.ag"
         }
         }
 # 232 "kind.ag"
 # 232 "kind.ag"
@@ -634,7 +634,7 @@ void doBackup(const string& vault,
 # 313 "kind.ag"
 # 313 "kind.ag"
       string userAtHost = conf.getString("user") + "@" +
       string userAtHost = conf.getString("user") + "@" +
 # 314 "kind.ag"
 # 314 "kind.ag"
-                          conf.getString("host");
+      conf.getString("host");
 # 315 "kind.ag"
 # 315 "kind.ag"
       string rshCommand = remoteShell;
       string rshCommand = remoteShell;
 # 316 "kind.ag"
 # 316 "kind.ag"
@@ -806,9 +806,9 @@ void doBackup(const string& vault,
 # 399 "kind.ag"
 # 399 "kind.ag"
       if (rc == 0 ||
       if (rc == 0 ||
 # 400 "kind.ag"
 # 400 "kind.ag"
-          rc == 24 || // "no error" or "vanished source files" (ignored)
+      rc == 24 || // "no error" or "vanished source files" (ignored)
 # 401 "kind.ag"
 # 401 "kind.ag"
-          rc == 6144) // workaround for wrong exit code ??!!
+      rc == 6144) // workaround for wrong exit code ??!!
 # 402 "kind.ag"
 # 402 "kind.ag"
         {
         {
 # 403 "kind.ag"
 # 403 "kind.ag"
@@ -1238,11 +1238,11 @@ void expireVault(const string& vault, KindConfig conf, DateTime now)
 # 615 "kind.ag"
 # 615 "kind.ag"
       if (debug)
       if (debug)
 # 616 "kind.ag"
 # 616 "kind.ag"
-	{
+        {
 # 617 "kind.ag"
 # 617 "kind.ag"
-	  image.printInfo();
+          image.printInfo();
 # 618 "kind.ag"
 # 618 "kind.ag"
-	}
+        }
 # 619 "kind.ag"
 # 619 "kind.ag"
 
 
 # 620 "kind.ag"
 # 620 "kind.ag"
@@ -1252,7 +1252,7 @@ void expireVault(const string& vault, KindConfig conf, DateTime now)
 # 622 "kind.ag"
 # 622 "kind.ag"
       if (imageTime != now &&          // ignore just created image
       if (imageTime != now &&          // ignore just created image
 # 623 "kind.ag"
 # 623 "kind.ag"
-          image.name != lastValidImage // ignore last valid image
+      image.name != lastValidImage // ignore last valid image
 # 624 "kind.ag"
 # 624 "kind.ag"
          )
          )
 # 625 "kind.ag"
 # 625 "kind.ag"
@@ -1389,15 +1389,15 @@ void usage()
   exit(1);
   exit(1);
 }
 }
 
 
-void error(const string &msg)
+void error(const string& msg)
 {
 {
   cout << endl << ag_programName << " - error: " << msg << endl << endl;
   cout << endl << ag_programName << " - error: " << msg << endl << endl;
   usage();
   usage();
 }
 }
 
 
-int ptoi(const char *para)
+int ptoi(const char* para)
 {
 {
-  char *end;
+  char* end;
   int res = strtol(para, &end, 10);
   int res = strtol(para, &end, 10);
   if (end == para)
   if (end == para)
     error(string("no int: ") + para);
     error(string("no int: ") + para);
@@ -1406,9 +1406,9 @@ int ptoi(const char *para)
   return res;
   return res;
 }
 }
 
 
-double ptod(const char *para)
+double ptod(const char* para)
 {
 {
-  char *end;
+  char* end;
   double res = strtod(para, &end);
   double res = strtod(para, &end);
   if (end == para)
   if (end == para)
     error(string("no double: ") + para);
     error(string("no double: ") + para);
@@ -1417,17 +1417,17 @@ double ptod(const char *para)
   return res;
   return res;
 }
 }
 
 
-int main(int argc, char **argv)
+int main(int argc, char** argv)
 {
 {
-string masterConfig = "";
-bool fullImage = false;
-bool doBackup = false;
-bool doExpire = false;
-bool listConfig = false;
-bool listImages = false;
-string forcedBackupSet = "";
-
-string vault = "";
+  string masterConfig = "";
+  bool fullImage = false;
+  bool doBackup = false;
+  bool doExpire = false;
+  bool listConfig = false;
+  bool listImages = false;
+  string forcedBackupSet = "";
+
+  string vault = "";
   static struct option ag_long_options[] =
   static struct option ag_long_options[] =
   {
   {
     {"masterconfig", required_argument, 0, 'c' },
     {"masterconfig", required_argument, 0, 'c' },
@@ -1458,52 +1458,52 @@ string vault = "";
           error("Expecting option parameter");
           error("Expecting option parameter");
           break;
           break;
         case 'c':
         case 'c':
-              masterConfig = optarg;
-              break;
+          masterConfig = optarg;
+          break;
 
 
         case 'f':
         case 'f':
-              fullImage = true;
-              break;
+          fullImage = true;
+          break;
 
 
         case 'B':
         case 'B':
-              doBackup = true;
-              break;
+          doBackup = true;
+          break;
 
 
         case 'E':
         case 'E':
-              doExpire = true;
-              break;
+          doExpire = true;
+          break;
 
 
         case 'C':
         case 'C':
-              listConfig = true;
-              break;
+          listConfig = true;
+          break;
 
 
         case 'I':
         case 'I':
-              listImages = true;
-              break;
+          listImages = true;
+          break;
 
 
         case 'D':
         case 'D':
-              dryRun = true;
-              break;
+          dryRun = true;
+          break;
 
 
         case 'F':
         case 'F':
-              forcedBackupSet = optarg;
-              break;
+          forcedBackupSet = optarg;
+          break;
 
 
         case 'v':
         case 'v':
-              verbose = true;
-              break;
+          verbose = true;
+          break;
 
 
         case 'd':
         case 'd':
-              debug = true;
-              break;
+          debug = true;
+          break;
 
 
         case 'q':
         case 'q':
-              quiet = true;
-              break;
+          quiet = true;
+          break;
 
 
         case 'h':
         case 'h':
-              usage();
-              break;
+          usage();
+          break;
 
 
         default:
         default:
           error("error in options");
           error("error in options");
@@ -1513,7 +1513,7 @@ string vault = "";
     vault = argv[optind++];
     vault = argv[optind++];
   else error("Parameter vault_or_group needed");
   else error("Parameter vault_or_group needed");
 
 
-/*AppGen:MainEnd*/
+  /*AppGen:MainEnd*/
 # 668 "kind.ag"
 # 668 "kind.ag"
 
 
 # 669 "kind.ag"
 # 669 "kind.ag"
@@ -1669,19 +1669,19 @@ string vault = "";
 # 744 "kind.ag"
 # 744 "kind.ag"
       for (string vault : vaults)
       for (string vault : vaults)
 # 745 "kind.ag"
 # 745 "kind.ag"
-	{
+        {
 # 746 "kind.ag"
 # 746 "kind.ag"
-	  if (doBackup)
+          if (doBackup)
 # 747 "kind.ag"
 # 747 "kind.ag"
-	    if (backupVault(vault, conf, imageTime, fullImage, forcedBackupSet))
+            if (backupVault(vault, conf, imageTime, fullImage, forcedBackupSet))
 # 748 "kind.ag"
 # 748 "kind.ag"
-	      writeSizes(logSizeFile);
+              writeSizes(logSizeFile);
 # 749 "kind.ag"
 # 749 "kind.ag"
-	  if (doExpire)
+          if (doExpire)
 # 750 "kind.ag"
 # 750 "kind.ag"
-	    expireVault(vault, conf, imageTime);
+            expireVault(vault, conf, imageTime);
 # 751 "kind.ag"
 # 751 "kind.ag"
-	}
+        }
 # 752 "kind.ag"
 # 752 "kind.ag"
 
 
 # 753 "kind.ag"
 # 753 "kind.ag"

+ 2 - 2
src/stringtools.cpp

@@ -198,9 +198,9 @@ time_t stot(const string& str)
 string timeString(time_t t)
 string timeString(time_t t)
 {
 {
   string res;
   string res;
-  if (t<0)
+  if (t < 0)
     {
     {
-      res="- ";
+      res = "- ";
       t = -t;
       t = -t;
     }
     }
   int sec = t % 60;
   int sec = t % 60;