cachedb.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: cachedb.h,v 1.4 2004/05/08 19:41:01 mdz Exp $
  4. /* ######################################################################
  5. CacheDB
  6. Simple uniform interface to a cache database.
  7. ##################################################################### */
  8. /*}}}*/
  9. #ifndef CACHEDB_H
  10. #define CACHEDB_H
  11. #include <db.h>
  12. #include <string>
  13. #include <apt-pkg/debfile.h>
  14. #include <inttypes.h>
  15. #include <sys/stat.h>
  16. #include <errno.h>
  17. #include "contents.h"
  18. class CacheDB
  19. {
  20. protected:
  21. // Database state/access
  22. DBT Key;
  23. DBT Data;
  24. char TmpKey[600];
  25. DB *Dbp;
  26. bool DBLoaded;
  27. bool ReadOnly;
  28. std::string DBFile;
  29. // Generate a key for the DB of a given type
  30. inline void InitQuery(const char *Type)
  31. {
  32. memset(&Key,0,sizeof(Key));
  33. memset(&Data,0,sizeof(Data));
  34. Key.data = TmpKey;
  35. Key.size = snprintf(TmpKey,sizeof(TmpKey),"%s:%s",FileName.c_str(), Type);
  36. }
  37. inline bool Get()
  38. {
  39. return Dbp->get(Dbp,0,&Key,&Data,0) == 0;
  40. };
  41. inline bool Put(const void *In,unsigned long const &Length)
  42. {
  43. if (ReadOnly == true)
  44. return true;
  45. Data.size = Length;
  46. Data.data = (void *)In;
  47. if (DBLoaded == true && (errno = Dbp->put(Dbp,0,&Key,&Data,0)) != 0)
  48. {
  49. DBLoaded = false;
  50. return false;
  51. }
  52. return true;
  53. }
  54. bool OpenFile();
  55. bool GetFileStat(bool const &doStat = false);
  56. bool GetCurStat();
  57. bool LoadControl();
  58. bool LoadContents(bool const &GenOnly);
  59. bool GetMD5(bool const &GenOnly);
  60. bool GetSHA1(bool const &GenOnly);
  61. bool GetSHA256(bool const &GenOnly);
  62. bool GetSHA512(bool const &GenOnly);
  63. // Stat info stored in the DB, Fixed types since it is written to disk.
  64. enum FlagList {FlControl = (1<<0),FlMD5=(1<<1),FlContents=(1<<2),
  65. FlSize=(1<<3), FlSHA1=(1<<4), FlSHA256=(1<<5),
  66. FlSHA512=(1<<6)};
  67. struct StatStore
  68. {
  69. uint32_t Flags;
  70. uint32_t mtime;
  71. uint64_t FileSize;
  72. uint8_t MD5[16];
  73. uint8_t SHA1[20];
  74. uint8_t SHA256[32];
  75. uint8_t SHA512[64];
  76. } CurStat;
  77. struct StatStore OldStat;
  78. // 'set' state
  79. std::string FileName;
  80. FileFd *Fd;
  81. debDebFile *DebFile;
  82. public:
  83. // Data collection helpers
  84. debDebFile::MemControlExtract Control;
  85. ContentsExtract Contents;
  86. std::string MD5Res;
  87. std::string SHA1Res;
  88. std::string SHA256Res;
  89. std::string SHA512Res;
  90. // Runtime statistics
  91. struct Stats
  92. {
  93. double Bytes;
  94. double MD5Bytes;
  95. double SHA1Bytes;
  96. double SHA256Bytes;
  97. double SHA512Bytes;
  98. unsigned long Packages;
  99. unsigned long Misses;
  100. unsigned long long DeLinkBytes;
  101. inline void Add(const Stats &S) {
  102. Bytes += S.Bytes;
  103. MD5Bytes += S.MD5Bytes;
  104. SHA1Bytes += S.SHA1Bytes;
  105. SHA256Bytes += S.SHA256Bytes;
  106. SHA512Bytes += S.SHA512Bytes;
  107. Packages += S.Packages;
  108. Misses += S.Misses;
  109. DeLinkBytes += S.DeLinkBytes;
  110. };
  111. Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0), Packages(0), Misses(0), DeLinkBytes(0) {};
  112. } Stats;
  113. bool ReadyDB(std::string const &DB);
  114. inline bool DBFailed() {return Dbp != 0 && DBLoaded == false;};
  115. inline bool Loaded() {return DBLoaded == true;};
  116. inline unsigned long long GetFileSize(void) {return CurStat.FileSize;}
  117. bool SetFile(std::string const &FileName,struct stat St,FileFd *Fd);
  118. bool GetFileInfo(std::string const &FileName, bool const &DoControl, bool const &DoContents, bool const &GenContentsOnly,
  119. bool const &DoMD5, bool const &DoSHA1, bool const &DoSHA256, bool const &DoSHA512, bool const &checkMtime = false);
  120. bool Finish();
  121. bool Clean();
  122. CacheDB(std::string const &DB) : Dbp(0), Fd(NULL), DebFile(0) {ReadyDB(DB);};
  123. ~CacheDB() {ReadyDB(std::string()); delete DebFile;};
  124. };
  125. #endif