cachedb.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. bool GetFileStat(bool const &doStat = false);
  74. bool GetCurStat();
  75. bool LoadControl();
  76. bool LoadContents(bool const &GenOnly);
  77. bool LoadSource();
  78. bool GetMD5(bool const &GenOnly);
  79. bool GetSHA1(bool const &GenOnly);
  80. bool GetSHA256(bool const &GenOnly);
  81. bool GetSHA512(bool const &GenOnly);
  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. struct StatStore
  88. {
  89. uint32_t Flags;
  90. uint32_t mtime;
  91. uint64_t FileSize;
  92. uint8_t MD5[16];
  93. uint8_t SHA1[20];
  94. uint8_t SHA256[32];
  95. uint8_t SHA512[64];
  96. } CurStat;
  97. struct StatStore OldStat;
  98. // 'set' state
  99. std::string FileName;
  100. FileFd *Fd;
  101. debDebFile *DebFile;
  102. public:
  103. // Data collection helpers
  104. debDebFile::MemControlExtract Control;
  105. ContentsExtract Contents;
  106. DscExtract Dsc;
  107. std::string MD5Res;
  108. std::string SHA1Res;
  109. std::string SHA256Res;
  110. std::string SHA512Res;
  111. // Runtime statistics
  112. struct Stats
  113. {
  114. double Bytes;
  115. double MD5Bytes;
  116. double SHA1Bytes;
  117. double SHA256Bytes;
  118. double SHA512Bytes;
  119. unsigned long Packages;
  120. unsigned long Misses;
  121. unsigned long long DeLinkBytes;
  122. inline void Add(const Stats &S) {
  123. Bytes += S.Bytes;
  124. MD5Bytes += S.MD5Bytes;
  125. SHA1Bytes += S.SHA1Bytes;
  126. SHA256Bytes += S.SHA256Bytes;
  127. SHA512Bytes += S.SHA512Bytes;
  128. Packages += S.Packages;
  129. Misses += S.Misses;
  130. DeLinkBytes += S.DeLinkBytes;
  131. };
  132. Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0),
  133. SHA512Bytes(0),Packages(0), Misses(0), DeLinkBytes(0) {};
  134. } Stats;
  135. bool ReadyDB(std::string const &DB);
  136. inline bool DBFailed() {return Dbp != 0 && DBLoaded == false;};
  137. inline bool Loaded() {return DBLoaded == true;};
  138. inline unsigned long long GetFileSize(void) {return CurStat.FileSize;}
  139. bool SetFile(std::string const &FileName,struct stat St,FileFd *Fd);
  140. // terrible old overloaded interface
  141. bool GetFileInfo(std::string const &FileName,
  142. bool const &DoControl,
  143. bool const &DoContents,
  144. bool const &GenContentsOnly,
  145. bool const &DoSource,
  146. bool const &DoMD5,
  147. bool const &DoSHA1,
  148. bool const &DoSHA256,
  149. bool const &DoSHA512,
  150. bool const &checkMtime = false);
  151. bool Finish();
  152. bool Clean();
  153. CacheDB(std::string const &DB) : Dbp(0), Fd(NULL), DebFile(0) {TmpKey[0]='\0'; ReadyDB(DB);};
  154. ~CacheDB() {ReadyDB(std::string()); delete DebFile;};
  155. };
  156. #endif