Просмотр исходного кода

add check when sources.list changed

Michael Vogt лет назад: 12
Родитель
Сommit
e6645b9fb9
3 измененных файлов с 43 добавлено и 11 удалено
  1. 21 4
      apt-pkg/contrib/hashes.cc
  2. 8 1
      apt-pkg/contrib/hashes.h
  3. 14 6
      cmdline/apt.cc

+ 21 - 4
apt-pkg/contrib/hashes.cc

@@ -54,6 +54,26 @@ HashString::HashString(std::string StringedHash)			/*{{{*/
 }
 									/*}}}*/
 bool HashString::VerifyFile(std::string filename) const			/*{{{*/
+{
+   std::string fileHash = GetHashForFile(filename);
+
+   if(_config->FindB("Debug::Hashes",false) == true)
+      std::clog << "HashString::VerifyFile: got: " << fileHash << " expected: " << toStr() << std::endl;
+
+   return (fileHash == Hash);
+}
+									/*}}}*/
+bool HashString::FromFile(std::string filename)          		/*{{{*/
+{
+   // pick the strongest hash
+   if (Type == "")
+      Type = _SupportedHashes[0];
+
+   Hash = GetHashForFile(filename);
+   return true;
+}
+									/*}}}*/
+std::string HashString::GetHashForFile(std::string filename) const      /*{{{*/
 {
    std::string fileHash;
 
@@ -84,10 +104,7 @@ bool HashString::VerifyFile(std::string filename) const			/*{{{*/
    }
    Fd.Close();
 
-   if(_config->FindB("Debug::Hashes",false) == true)
-      std::clog << "HashString::VerifyFile: got: " << fileHash << " expected: " << toStr() << std::endl;
-
-   return (fileHash == Hash);
+   return fileHash;
 }
 									/*}}}*/
 const char** HashString::SupportedHashes()

+ 8 - 1
apt-pkg/contrib/hashes.h

@@ -36,7 +36,10 @@ class HashString
  protected:
    std::string Type;
    std::string Hash;
-   static const char * _SupportedHashes[10];
+   static const char* _SupportedHashes[10];
+
+   // internal helper
+   std::string GetHashForFile(std::string filename) const;
 
  public:
    HashString(std::string Type, std::string Hash);
@@ -49,6 +52,10 @@ class HashString
    // verify the given filename against the currently loaded hash
    bool VerifyFile(std::string filename) const;
 
+   // generate a hash string from the given filename
+   bool FromFile(std::string filename);
+
+
    // helper
    std::string toStr() const;                    // convert to str as "type:hash"
    bool empty() const;

+ 14 - 6
cmdline/apt.cc

@@ -41,6 +41,7 @@
 #include <apt-pkg/pkgsystem.h>
 #include <apt-pkg/indexfile.h>
 #include <apt-pkg/metaindex.h>
+#include <apt-pkg/hashes.h>
 
 #include <apti18n.h>
 
@@ -61,21 +62,21 @@
 // ---------------------------------------------------------------------
 bool EditSources(CommandLine &CmdL)
 {
-   // FIXME: suport CmdL.FileList to specify sources.list.d files
+   bool res;
+   pkgSourceList sl;
+   std::string outs;
 
+   // FIXME: suport CmdL.FileList to specify sources.list.d files
    std::string sourceslist = _config->FindFile("Dir::Etc::sourcelist");
 
-   // FIXME: take hash before, 
-   //        when changed display message to apt update
-   bool res;
-   pkgSourceList sl;
+   HashString before;
+   before.FromFile(sourceslist);
 
    do {
       EditFileInSensibleEditor(sourceslist);
       _error->PushToStack();
       res = sl.Read(sourceslist);
       if (!res) {
-         std::string outs;
          strprintf(outs, _("Failed to parse %s. Edit again? "),
                    sourceslist.c_str());
          std::cout << outs;
@@ -84,6 +85,13 @@ bool EditSources(CommandLine &CmdL)
       _error->RevertToStack();
    } while (res == false);
 
+   if (!before.VerifyFile(sourceslist)) {
+      strprintf(
+         outs, _("Your '%s' file changed, please run 'apt-get update'."),
+         sourceslist.c_str());
+      std::cout << outs << std::endl;
+   }
+
    return true;
 }
 									/*}}}*/