merge stuff from donkult
[ntk/apt.git] / ftparchive / cachedb.cc
index 8de3a0b..c2318bf 100644 (file)
    ##################################################################### */
                                                                        /*}}}*/
 // Include Files                                                       /*{{{*/
-#include "cachedb.h"
+#include <config.h>
 
-#include <apti18n.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/md5.h>
 #include <apt-pkg/sha1.h>
-#include <apt-pkg/sha256.h>
+#include <apt-pkg/sha2.h>
 #include <apt-pkg/strutl.h>
 #include <apt-pkg/configuration.h>
-    
+#include <apt-pkg/fileutl.h>
+
 #include <netinet/in.h>       // htonl, etc
+
+#include "cachedb.h"
+
+#include <apti18n.h>
                                                                        /*}}}*/
 
 // CacheDB::ReadyDB - Ready the DB2                                    /*{{{*/
 // ---------------------------------------------------------------------
 /* This opens the DB2 file for caching package information */
-bool CacheDB::ReadyDB(string DB)
+bool CacheDB::ReadyDB(std::string const &DB)
 {
    int err;
 
@@ -46,7 +50,7 @@ bool CacheDB::ReadyDB(string DB)
    
    DBLoaded = false;
    Dbp = 0;
-   DBFile = string();
+   DBFile = std::string();
    
    if (DB.empty())
       return true;
@@ -69,7 +73,7 @@ bool CacheDB::ReadyDB(string DB)
       // apt 0.6.44
       if (err == EINVAL)
       {
-        _error->Error(_("DB format is invalid. If you upgraded from a older version of apt, please remove and re-create the database."));
+        _error->Error(_("DB format is invalid. If you upgraded from an older version of apt, please remove and re-create the database."));
       }
       if (err)
       {
@@ -83,7 +87,7 @@ bool CacheDB::ReadyDB(string DB)
    return true;
 }
                                                                        /*}}}*/
-// CacheDB::OpenFile - Open the filei                                  /*{{{*/
+// CacheDB::OpenFile - Open the file                                   /*{{{*/
 // ---------------------------------------------------------------------
 /* */
 bool CacheDB::OpenFile()
@@ -102,9 +106,9 @@ bool CacheDB::OpenFile()
 // ---------------------------------------------------------------------
 /* This gets the size from the database if it's there.  If we need
  * to look at the file, also get the mtime from the file. */
-bool CacheDB::GetFileStat()
+bool CacheDB::GetFileStat(bool const &doStat)
 {
-       if ((CurStat.Flags & FlSize) == FlSize)
+       if ((CurStat.Flags & FlSize) == FlSize && doStat == false)
        {
                /* Already worked out the file size */
        }
@@ -139,7 +143,7 @@ bool CacheDB::GetCurStat()
    
        if (DBLoaded)
        {
-               /* First see if thre is anything about it
+               /* First see if there is anything about it
                   in the database */
 
                /* Get the flags (and mtime) */
@@ -160,9 +164,10 @@ bool CacheDB::GetCurStat()
                                                                        /*}}}*/
 // CacheDB::GetFileInfo - Get all the info about the file              /*{{{*/
 // ---------------------------------------------------------------------
-bool CacheDB::GetFileInfo(string FileName, bool DoControl, bool DoContents,
-                               bool GenContentsOnly, 
-                               bool DoMD5, bool DoSHA1, bool DoSHA256)
+bool CacheDB::GetFileInfo(std::string const &FileName, bool const &DoControl, bool const &DoContents,
+                               bool const &GenContentsOnly, bool const &DoMD5, bool const &DoSHA1,
+                               bool const &DoSHA256,   bool const &DoSHA512, 
+                          bool const &checkMtime)
 {
        this->FileName = FileName;
 
@@ -171,22 +176,28 @@ bool CacheDB::GetFileInfo(string FileName, bool DoControl, bool DoContents,
                return false;
    }   
    OldStat = CurStat;
-       
-       if (GetFileStat() == false)
+
+       if (GetFileStat(checkMtime) == false)
        {
                delete Fd;
                Fd = NULL;
                return false;   
        }
 
+    /* if mtime changed, update CurStat from disk */
+    if (checkMtime == true && OldStat.mtime != CurStat.mtime)
+        CurStat.Flags = FlSize;
+
        Stats.Bytes += CurStat.FileSize;
        Stats.Packages++;
 
-       if (DoControl && LoadControl() == false
-               || DoContents && LoadContents(GenContentsOnly) == false
-               || DoMD5 && GetMD5(false) == false
-               || DoSHA1 && GetSHA1(false) == false
-               || DoSHA256 && GetSHA256(false) == false)
+       if ((DoControl && LoadControl() == false)
+               || (DoContents && LoadContents(GenContentsOnly) == false)
+               || (DoMD5 && GetMD5(false) == false)
+               || (DoSHA1 && GetSHA1(false) == false)
+               || (DoSHA256 && GetSHA256(false) == false)
+               || (DoSHA512 && GetSHA512(false) == false)
+           )
        {
                delete Fd;
                Fd = NULL;
@@ -247,7 +258,7 @@ bool CacheDB::LoadControl()
 // CacheDB::LoadContents - Load the File Listing                       /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-bool CacheDB::LoadContents(bool GenOnly)
+bool CacheDB::LoadContents(bool const &GenOnly)
 {
    // Try to read the control information out of the DB.
    if ((CurStat.Flags & FlContents) == FlContents)
@@ -289,15 +300,19 @@ bool CacheDB::LoadContents(bool GenOnly)
 }
                                                                        /*}}}*/
 
-static string bytes2hex(uint8_t *bytes, size_t length) {
-   char space[65];
-   if (length * 2 > sizeof(space) - 1) length = (sizeof(space) - 1) / 2;
-   for (size_t i = 0; i < length; i++)
-      snprintf(&space[i*2], 3, "%02x", bytes[i]);
-   return string(space);
+static std::string bytes2hex(uint8_t *bytes, size_t length) {
+   char buf[3];
+   std::string space;
+
+   space.reserve(length*2 + 1);
+   for (size_t i = 0; i < length; i++) {
+      snprintf(buf, sizeof(buf), "%02x", bytes[i]);
+      space.append(buf);
+   }
+   return space;
 }
 
-static inline unsigned char xdig2num(char dig) {
+static inline unsigned char xdig2num(char const &dig) {
    if (isdigit(dig)) return dig - '0';
    if ('a' <= dig && dig <= 'f') return dig - 'a' + 10;
    if ('A' <= dig && dig <= 'F') return dig - 'A' + 10;
@@ -318,7 +333,7 @@ static void hex2bytes(uint8_t *bytes, const char *hex, int length) {
 // CacheDB::GetMD5 - Get the MD5 hash                                  /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-bool CacheDB::GetMD5(bool GenOnly)
+bool CacheDB::GetMD5(bool const &GenOnly)
 {
    // Try to read the control information out of the DB.
    if ((CurStat.Flags & FlMD5) == FlMD5)
@@ -337,7 +352,7 @@ bool CacheDB::GetMD5(bool GenOnly)
       return false;
    }
    MD5Summation MD5;
-   if (Fd->Seek(0) == false || MD5.AddFD(Fd->Fd(),CurStat.FileSize) == false)
+   if (Fd->Seek(0) == false || MD5.AddFD(*Fd, CurStat.FileSize) == false)
       return false;
    
    MD5Res = MD5.Result();
@@ -349,7 +364,7 @@ bool CacheDB::GetMD5(bool GenOnly)
 // CacheDB::GetSHA1 - Get the SHA1 hash                                        /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-bool CacheDB::GetSHA1(bool GenOnly)
+bool CacheDB::GetSHA1(bool const &GenOnly)
 {
    // Try to read the control information out of the DB.
    if ((CurStat.Flags & FlSHA1) == FlSHA1)
@@ -368,7 +383,7 @@ bool CacheDB::GetSHA1(bool GenOnly)
       return false;
    }
    SHA1Summation SHA1;
-   if (Fd->Seek(0) == false || SHA1.AddFD(Fd->Fd(),CurStat.FileSize) == false)
+   if (Fd->Seek(0) == false || SHA1.AddFD(*Fd, CurStat.FileSize) == false)
       return false;
    
    SHA1Res = SHA1.Result();
@@ -380,7 +395,7 @@ bool CacheDB::GetSHA1(bool GenOnly)
 // CacheDB::GetSHA256 - Get the SHA256 hash                            /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-bool CacheDB::GetSHA256(bool GenOnly)
+bool CacheDB::GetSHA256(bool const &GenOnly)
 {
    // Try to read the control information out of the DB.
    if ((CurStat.Flags & FlSHA256) == FlSHA256)
@@ -399,7 +414,7 @@ bool CacheDB::GetSHA256(bool GenOnly)
       return false;
    }
    SHA256Summation SHA256;
-   if (Fd->Seek(0) == false || SHA256.AddFD(Fd->Fd(),CurStat.FileSize) == false)
+   if (Fd->Seek(0) == false || SHA256.AddFD(*Fd, CurStat.FileSize) == false)
       return false;
    
    SHA256Res = SHA256.Result();
@@ -408,6 +423,37 @@ bool CacheDB::GetSHA256(bool GenOnly)
    return true;
 }
                                                                        /*}}}*/
+// CacheDB::GetSHA256 - Get the SHA256 hash                            /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool CacheDB::GetSHA512(bool const &GenOnly)
+{
+   // Try to read the control information out of the DB.
+   if ((CurStat.Flags & FlSHA512) == FlSHA512)
+   {
+      if (GenOnly == true)
+        return true;
+
+      SHA512Res = bytes2hex(CurStat.SHA512, sizeof(CurStat.SHA512));
+      return true;
+   }
+   
+   Stats.SHA512Bytes += CurStat.FileSize;
+        
+   if (Fd == NULL && OpenFile() == false)
+   {
+      return false;
+   }
+   SHA512Summation SHA512;
+   if (Fd->Seek(0) == false || SHA512.AddFD(*Fd, CurStat.FileSize) == false)
+      return false;
+   
+   SHA512Res = SHA512.Result();
+   hex2bytes(CurStat.SHA512, SHA512Res.data(), sizeof(CurStat.SHA512));
+   CurStat.Flags |= FlSHA512;
+   return true;
+}
+                                                                       /*}}}*/
 // CacheDB::Finish - Write back the cache structure                    /*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -456,13 +502,14 @@ bool CacheDB::Clean()
              stringcmp(Colon + 1, (char *)Key.data+Key.size,"cl") == 0 ||
              stringcmp(Colon + 1, (char *)Key.data+Key.size,"cn") == 0)
         {
-            if (FileExists(string((const char *)Key.data,Colon)) == true)
+            if (FileExists(std::string((const char *)Key.data,Colon)) == true)
                continue;            
         }
       }
       
       Cursor->c_del(Cursor,0);
    }
+   Dbp->compact(Dbp, NULL, NULL, NULL, NULL, DB_FREE_SPACE, NULL);
 
    return true;
 }