You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

196 lines
4.7 KiB

  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. CacheDB
  5. Simple uniform interface to a cache database.
  6. ##################################################################### */
  7. /*}}}*/
  8. #ifndef CACHEDB_H
  9. #define CACHEDB_H
  10. #include <apt-pkg/debfile.h>
  11. #include <apt-pkg/hashes.h>
  12. #include <db.h>
  13. #include <string>
  14. #include <errno.h>
  15. #include <stdint.h>
  16. #include <stdio.h>
  17. #include <string.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 GetHashes(bool const GenOnly, unsigned int const DoHashes);
  82. // Stat info stored in the DB, Fixed types since it is written to disk.
  83. enum FlagList {FlControl = (1<<0),FlMD5=(1<<1),FlContents=(1<<2),
  84. FlSize=(1<<3), FlSHA1=(1<<4), FlSHA256=(1<<5),
  85. FlSHA512=(1<<6), FlSource=(1<<7)
  86. };
  87. // the on-disk format changed (FileSize increased to 64bit) in
  88. // commit 650faab0 which will lead to corruption with old caches
  89. struct StatStoreOldFormat
  90. {
  91. uint32_t Flags;
  92. uint32_t mtime;
  93. uint32_t FileSize;
  94. uint8_t MD5[16];
  95. uint8_t SHA1[20];
  96. uint8_t SHA256[32];
  97. } CurStatOldFormat;
  98. // WARNING: this struct is read/written to the DB so do not change the
  99. // layout of the fields (see lp #1274466), only append to it
  100. struct StatStore
  101. {
  102. uint32_t Flags;
  103. uint32_t mtime;
  104. uint64_t FileSize;
  105. uint8_t MD5[16];
  106. uint8_t SHA1[20];
  107. uint8_t SHA256[32];
  108. uint8_t SHA512[64];
  109. } CurStat;
  110. struct StatStore OldStat;
  111. // 'set' state
  112. std::string FileName;
  113. FileFd *Fd;
  114. debDebFile *DebFile;
  115. public:
  116. // Data collection helpers
  117. debDebFile::MemControlExtract Control;
  118. ContentsExtract Contents;
  119. DscExtract Dsc;
  120. HashStringList HashesList;
  121. // Runtime statistics
  122. struct Stats
  123. {
  124. double Bytes;
  125. double MD5Bytes;
  126. double SHA1Bytes;
  127. double SHA256Bytes;
  128. double SHA512Bytes;
  129. unsigned long Packages;
  130. unsigned long Misses;
  131. unsigned long long DeLinkBytes;
  132. inline void Add(const Stats &S) {
  133. Bytes += S.Bytes;
  134. MD5Bytes += S.MD5Bytes;
  135. SHA1Bytes += S.SHA1Bytes;
  136. SHA256Bytes += S.SHA256Bytes;
  137. SHA512Bytes += S.SHA512Bytes;
  138. Packages += S.Packages;
  139. Misses += S.Misses;
  140. DeLinkBytes += S.DeLinkBytes;
  141. };
  142. Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0),
  143. SHA512Bytes(0),Packages(0), Misses(0), DeLinkBytes(0) {};
  144. } Stats;
  145. bool ReadyDB(std::string const &DB = "");
  146. inline bool DBFailed() {return Dbp != 0 && DBLoaded == false;};
  147. inline bool Loaded() {return DBLoaded == true;};
  148. inline unsigned long long GetFileSize(void) {return CurStat.FileSize;}
  149. bool SetFile(std::string const &FileName,struct stat St,FileFd *Fd);
  150. // terrible old overloaded interface
  151. bool GetFileInfo(std::string const &FileName,
  152. bool const &DoControl,
  153. bool const &DoContents,
  154. bool const &GenContentsOnly,
  155. bool const DoSource,
  156. unsigned int const DoHashes,
  157. bool const &checkMtime = false);
  158. bool Finish();
  159. bool Clean();
  160. explicit CacheDB(std::string const &DB);
  161. ~CacheDB();
  162. };
  163. #endif