cachedb.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 <apt-pkg/debfile.h>
  12. #include <db.h>
  13. #include <errno.h>
  14. #include <string>
  15. #include <string.h>
  16. #include <stdint.h>
  17. #include <stdio.h>
  18. #include "contents.h"
  19. #include "sources.h"
  20. class FileFd;
  21. class CacheDB
  22. {
  23. protected:
  24. // Database state/access
  25. DBT Key;
  26. DBT Data;
  27. char TmpKey[600];
  28. DB *Dbp;
  29. bool DBLoaded;
  30. bool ReadOnly;
  31. std::string DBFile;
  32. // Generate a key for the DB of a given type
  33. void _InitQuery(const char *Type)
  34. {
  35. memset(&Key,0,sizeof(Key));
  36. memset(&Data,0,sizeof(Data));
  37. Key.data = TmpKey;
  38. Key.size = snprintf(TmpKey,sizeof(TmpKey),"%s:%s",FileName.c_str(), Type);
  39. }
  40. void InitQueryStats() {
  41. _InitQuery("st");
  42. }
  43. void InitQuerySource() {
  44. _InitQuery("cs");
  45. }
  46. void InitQueryControl() {
  47. _InitQuery("cl");
  48. }
  49. void InitQueryContent() {
  50. _InitQuery("cn");
  51. }
  52. inline bool Get()
  53. {
  54. return Dbp->get(Dbp,0,&Key,&Data,0) == 0;
  55. };
  56. inline bool Put(const void *In,unsigned long const &Length)
  57. {
  58. if (ReadOnly == true)
  59. return true;
  60. Data.size = Length;
  61. Data.data = (void *)In;
  62. if (DBLoaded == true && (errno = Dbp->put(Dbp,0,&Key,&Data,0)) != 0)
  63. {
  64. DBLoaded = false;
  65. return false;
  66. }
  67. return true;
  68. }
  69. bool OpenFile();
  70. void CloseFile();
  71. bool OpenDebFile();
  72. void CloseDebFile();
  73. // GetCurStat needs some compat code, see lp #1274466)
  74. bool GetCurStatCompatOldFormat();
  75. bool GetCurStatCompatNewFormat();
  76. bool GetCurStat();
  77. bool GetFileStat(bool const &doStat = false);
  78. bool LoadControl();
  79. bool LoadContents(bool const &GenOnly);
  80. bool LoadSource();
  81. bool GetMD5(bool const &GenOnly);
  82. bool GetSHA1(bool const &GenOnly);
  83. bool GetSHA256(bool const &GenOnly);
  84. bool GetSHA512(bool const &GenOnly);
  85. // Stat info stored in the DB, Fixed types since it is written to disk.
  86. enum FlagList {FlControl = (1<<0),FlMD5=(1<<1),FlContents=(1<<2),
  87. FlSize=(1<<3), FlSHA1=(1<<4), FlSHA256=(1<<5),
  88. FlSHA512=(1<<6), FlSource=(1<<7),
  89. };
  90. // the on-disk format changed (FileSize increased to 64bit) in
  91. // commit 650faab0 which will lead to corruption with old caches
  92. struct StatStoreOldFormat
  93. {
  94. uint32_t Flags;
  95. uint32_t mtime;
  96. uint32_t FileSize;
  97. uint8_t MD5[16];
  98. uint8_t SHA1[20];
  99. uint8_t SHA256[32];
  100. } CurStatOldFormat;
  101. // WARNING: this struct is read/written to the DB so do not change the
  102. // layout of the fields (see lp #1274466), only append to it
  103. struct StatStore
  104. {
  105. uint32_t Flags;
  106. uint32_t mtime;
  107. uint64_t FileSize;
  108. uint8_t MD5[16];
  109. uint8_t SHA1[20];
  110. uint8_t SHA256[32];
  111. uint8_t SHA512[64];
  112. } CurStat;
  113. struct StatStore OldStat;
  114. // 'set' state
  115. std::string FileName;
  116. FileFd *Fd;
  117. debDebFile *DebFile;
  118. public:
  119. // Data collection helpers
  120. debDebFile::MemControlExtract Control;
  121. ContentsExtract Contents;
  122. DscExtract Dsc;
  123. std::string MD5Res;
  124. std::string SHA1Res;
  125. std::string SHA256Res;
  126. std::string SHA512Res;
  127. // Runtime statistics
  128. struct Stats
  129. {
  130. double Bytes;
  131. double MD5Bytes;
  132. double SHA1Bytes;
  133. double SHA256Bytes;
  134. double SHA512Bytes;
  135. unsigned long Packages;
  136. unsigned long Misses;
  137. unsigned long long DeLinkBytes;
  138. inline void Add(const Stats &S) {
  139. Bytes += S.Bytes;
  140. MD5Bytes += S.MD5Bytes;
  141. SHA1Bytes += S.SHA1Bytes;
  142. SHA256Bytes += S.SHA256Bytes;
  143. SHA512Bytes += S.SHA512Bytes;
  144. Packages += S.Packages;
  145. Misses += S.Misses;
  146. DeLinkBytes += S.DeLinkBytes;
  147. };
  148. Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0),
  149. SHA512Bytes(0),Packages(0), Misses(0), DeLinkBytes(0) {};
  150. } Stats;
  151. bool ReadyDB(std::string const &DB = "");
  152. inline bool DBFailed() {return Dbp != 0 && DBLoaded == false;};
  153. inline bool Loaded() {return DBLoaded == true;};
  154. inline unsigned long long GetFileSize(void) {return CurStat.FileSize;}
  155. bool SetFile(std::string const &FileName,struct stat St,FileFd *Fd);
  156. // terrible old overloaded interface
  157. bool GetFileInfo(std::string const &FileName,
  158. bool const &DoControl,
  159. bool const &DoContents,
  160. bool const &GenContentsOnly,
  161. bool const &DoSource,
  162. bool const &DoMD5,
  163. bool const &DoSHA1,
  164. bool const &DoSHA256,
  165. bool const &DoSHA512,
  166. bool const &checkMtime = false);
  167. bool Finish();
  168. bool Clean();
  169. CacheDB(std::string const &DB);
  170. ~CacheDB();
  171. };
  172. #endif