cachedb.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #ifdef __GNUG__
  12. #pragma interface "cachedb.h"
  13. #endif
  14. #include <db.h>
  15. #include <string>
  16. #include <apt-pkg/debfile.h>
  17. #include <inttypes.h>
  18. #include <sys/stat.h>
  19. #include <errno.h>
  20. #include "contents.h"
  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. string DBFile;
  32. // Generate a key for the DB of a given type
  33. inline 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. inline bool Get()
  41. {
  42. return Dbp->get(Dbp,0,&Key,&Data,0) == 0;
  43. };
  44. inline bool Put(const void *In,unsigned long Length)
  45. {
  46. if (ReadOnly == true)
  47. return true;
  48. Data.size = Length;
  49. Data.data = (void *)In;
  50. if (DBLoaded == true && (errno = Dbp->put(Dbp,0,&Key,&Data,0)) != 0)
  51. {
  52. DBLoaded = false;
  53. return false;
  54. }
  55. return true;
  56. }
  57. bool OpenFile();
  58. bool GetFileStat();
  59. bool GetCurStat();
  60. bool LoadControl();
  61. bool LoadContents(bool GenOnly);
  62. bool GetMD5(bool GenOnly);
  63. bool GetSHA1(bool GenOnly);
  64. bool GetSHA256(bool GenOnly);
  65. // Stat info stored in the DB, Fixed types since it is written to disk.
  66. enum FlagList {FlControl = (1<<0),FlMD5=(1<<1),FlContents=(1<<2),
  67. FlSize=(1<<3), FlSHA1=(1<<4), FlSHA256=(1<<5)};
  68. struct StatStore
  69. {
  70. uint32_t Flags;
  71. uint32_t mtime;
  72. uint32_t FileSize;
  73. uint8_t MD5[16];
  74. uint8_t SHA1[20];
  75. uint8_t SHA256[32];
  76. } CurStat;
  77. struct StatStore OldStat;
  78. // 'set' state
  79. string FileName;
  80. FileFd *Fd;
  81. debDebFile *DebFile;
  82. public:
  83. // Data collection helpers
  84. debDebFile::MemControlExtract Control;
  85. ContentsExtract Contents;
  86. string MD5Res;
  87. string SHA1Res;
  88. string SHA256Res;
  89. // Runtime statistics
  90. struct Stats
  91. {
  92. double Bytes;
  93. double MD5Bytes;
  94. double SHA1Bytes;
  95. double SHA256Bytes;
  96. unsigned long Packages;
  97. unsigned long Misses;
  98. unsigned long DeLinkBytes;
  99. inline void Add(const Stats &S) {
  100. Bytes += S.Bytes; MD5Bytes += S.MD5Bytes; SHA1Bytes += S.SHA1Bytes;
  101. SHA256Bytes += S.SHA256Bytes;
  102. Packages += S.Packages; Misses += S.Misses; DeLinkBytes += S.DeLinkBytes;};
  103. Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0), Packages(0), Misses(0), DeLinkBytes(0) {};
  104. } Stats;
  105. bool ReadyDB(string DB);
  106. inline bool DBFailed() {return Dbp != 0 && DBLoaded == false;};
  107. inline bool Loaded() {return DBLoaded == true;};
  108. inline off_t GetFileSize(void) {return CurStat.FileSize;}
  109. bool SetFile(string FileName,struct stat St,FileFd *Fd);
  110. bool GetFileInfo(string FileName, bool DoControl, bool DoContents,
  111. bool GenContentsOnly, bool DoMD5, bool DoSHA1, bool DoSHA256);
  112. bool Finish();
  113. bool Clean();
  114. CacheDB(string DB) : Dbp(0), Fd(NULL), DebFile(0) {ReadyDB(DB);};
  115. ~CacheDB() {ReadyDB(string()); delete DebFile;};
  116. };
  117. #endif