cachedb.h 4.7 KB

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