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.
 
 
 
 
 
 

285 lines
7.5 KiB

  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: cachedb.cc,v 1.3 2001/05/29 03:50:33 jgg Exp $
  4. /* ######################################################################
  5. CacheDB
  6. Simple uniform interface to a cache database.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #ifdef __GNUG__
  11. #pragma implementation "cachedb.h"
  12. #endif
  13. #include "cachedb.h"
  14. #include <apt-pkg/error.h>
  15. #include <apt-pkg/md5.h>
  16. #include <apt-pkg/strutl.h>
  17. #include <apt-pkg/configuration.h>
  18. #include <netinet/in.h> // htonl, etc
  19. /*}}}*/
  20. // CacheDB::ReadyDB - Ready the DB2 /*{{{*/
  21. // ---------------------------------------------------------------------
  22. /* This opens the DB2 file for caching package information */
  23. bool CacheDB::ReadyDB(string DB)
  24. {
  25. ReadOnly = _config->FindB("APT::FTPArchive::ReadOnlyDB",false);
  26. // Close the old DB
  27. if (Dbp != 0)
  28. Dbp->close(Dbp,0);
  29. /* Check if the DB was disabled while running and deal with a
  30. corrupted DB */
  31. if (DBFailed() == true)
  32. {
  33. _error->Warning("DB was corrupted, file renamed to %s.old",DBFile.c_str());
  34. rename(DBFile.c_str(),(DBFile+".old").c_str());
  35. }
  36. DBLoaded = false;
  37. Dbp = 0;
  38. DBFile = string();
  39. if (DB.empty())
  40. return true;
  41. if ((errno = db_open(DB.c_str(),DB_HASH,
  42. (ReadOnly?DB_RDONLY:DB_CREATE),
  43. 0644,0,0,&Dbp)) != 0)
  44. {
  45. Dbp = 0;
  46. return _error->Errno("db_open","Unable to open DB2 file %s",DB.c_str());
  47. }
  48. DBFile = DB;
  49. DBLoaded = true;
  50. return true;
  51. }
  52. /*}}}*/
  53. // CacheDB::SetFile - Select a file to be working with /*{{{*/
  54. // ---------------------------------------------------------------------
  55. /* All future actions will be performed against this file */
  56. bool CacheDB::SetFile(string FileName,struct stat St,FileFd *Fd)
  57. {
  58. delete DebFile;
  59. DebFile = 0;
  60. this->FileName = FileName;
  61. this->Fd = Fd;
  62. this->FileStat = St;
  63. FileStat = St;
  64. memset(&CurStat,0,sizeof(CurStat));
  65. Stats.Bytes += St.st_size;
  66. Stats.Packages++;
  67. if (DBLoaded == false)
  68. return true;
  69. InitQuery("st");
  70. // Ensure alignment of the returned structure
  71. Data.data = &CurStat;
  72. Data.ulen = sizeof(CurStat);
  73. Data.flags = DB_DBT_USERMEM;
  74. // Lookup the stat info and confirm the file is unchanged
  75. if (Get() == true)
  76. {
  77. if (CurStat.st_mtime != htonl(St.st_mtime))
  78. {
  79. CurStat.st_mtime = htonl(St.st_mtime);
  80. CurStat.Flags = 0;
  81. _error->Warning("File date has changed %s",FileName.c_str());
  82. }
  83. }
  84. else
  85. {
  86. CurStat.st_mtime = htonl(St.st_mtime);
  87. CurStat.Flags = 0;
  88. }
  89. CurStat.Flags = ntohl(CurStat.Flags);
  90. OldStat = CurStat;
  91. return true;
  92. }
  93. /*}}}*/
  94. // CacheDB::LoadControl - Load Control information /*{{{*/
  95. // ---------------------------------------------------------------------
  96. /* */
  97. bool CacheDB::LoadControl()
  98. {
  99. // Try to read the control information out of the DB.
  100. if ((CurStat.Flags & FlControl) == FlControl)
  101. {
  102. // Lookup the control information
  103. InitQuery("cl");
  104. if (Get() == true && Control.TakeControl(Data.data,Data.size) == true)
  105. return true;
  106. CurStat.Flags &= ~FlControl;
  107. }
  108. // Create a deb instance to read the archive
  109. if (DebFile == 0)
  110. {
  111. DebFile = new debDebFile(*Fd);
  112. if (_error->PendingError() == true)
  113. return false;
  114. }
  115. Stats.Misses++;
  116. if (Control.Read(*DebFile) == false)
  117. return false;
  118. if (Control.Control == 0)
  119. return _error->Error("Archive has no control record");
  120. // Write back the control information
  121. InitQuery("cl");
  122. if (Put(Control.Control,Control.Length) == true)
  123. CurStat.Flags |= FlControl;
  124. return true;
  125. }
  126. /*}}}*/
  127. // CacheDB::LoadContents - Load the File Listing /*{{{*/
  128. // ---------------------------------------------------------------------
  129. /* */
  130. bool CacheDB::LoadContents(bool GenOnly)
  131. {
  132. // Try to read the control information out of the DB.
  133. if ((CurStat.Flags & FlContents) == FlContents)
  134. {
  135. if (GenOnly == true)
  136. return true;
  137. // Lookup the contents information
  138. InitQuery("cn");
  139. if (Get() == true)
  140. {
  141. if (Contents.TakeContents(Data.data,Data.size) == true)
  142. return true;
  143. }
  144. CurStat.Flags &= ~FlContents;
  145. }
  146. // Create a deb instance to read the archive
  147. if (DebFile == 0)
  148. {
  149. DebFile = new debDebFile(*Fd);
  150. if (_error->PendingError() == true)
  151. return false;
  152. }
  153. if (Contents.Read(*DebFile) == false)
  154. return false;
  155. // Write back the control information
  156. InitQuery("cn");
  157. if (Put(Contents.Data,Contents.CurSize) == true)
  158. CurStat.Flags |= FlContents;
  159. return true;
  160. }
  161. /*}}}*/
  162. // CacheDB::GetMD5 - Get the MD5 hash /*{{{*/
  163. // ---------------------------------------------------------------------
  164. /* */
  165. bool CacheDB::GetMD5(string &MD5Res,bool GenOnly)
  166. {
  167. // Try to read the control information out of the DB.
  168. if ((CurStat.Flags & FlMD5) == FlMD5)
  169. {
  170. if (GenOnly == true)
  171. return true;
  172. InitQuery("m5");
  173. if (Get() == true)
  174. {
  175. MD5Res = string((char *)Data.data,Data.size);
  176. return true;
  177. }
  178. CurStat.Flags &= ~FlMD5;
  179. }
  180. Stats.MD5Bytes += FileStat.st_size;
  181. MD5Summation MD5;
  182. if (Fd->Seek(0) == false || MD5.AddFD(Fd->Fd(),FileStat.st_size) == false)
  183. return false;
  184. MD5Res = MD5.Result();
  185. InitQuery("m5");
  186. if (Put(MD5Res.c_str(),MD5Res.length()) == true)
  187. CurStat.Flags |= FlMD5;
  188. return true;
  189. }
  190. /*}}}*/
  191. // CacheDB::Finish - Write back the cache structure /*{{{*/
  192. // ---------------------------------------------------------------------
  193. /* */
  194. bool CacheDB::Finish()
  195. {
  196. // Optimize away some writes.
  197. if (CurStat.Flags == OldStat.Flags &&
  198. CurStat.st_mtime == OldStat.st_mtime)
  199. return true;
  200. // Write the stat information
  201. CurStat.Flags = htonl(CurStat.Flags);
  202. InitQuery("st");
  203. Put(&CurStat,sizeof(CurStat));
  204. CurStat.Flags = ntohl(CurStat.Flags);
  205. return true;
  206. }
  207. /*}}}*/
  208. // CacheDB::Clean - Clean the Database /*{{{*/
  209. // ---------------------------------------------------------------------
  210. /* Tidy the database by removing files that no longer exist at all. */
  211. bool CacheDB::Clean()
  212. {
  213. if (DBLoaded == false)
  214. return true;
  215. /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
  216. needs the lower one and 2.7.7 needs the upper.. */
  217. #if DB_VERSION_MAJOR >= 2 && DB_VERSION_MINOR >= 7
  218. DBC *Cursor;
  219. if ((errno = Dbp->cursor(Dbp,0,&Cursor,0)) != 0)
  220. return _error->Error("Unable to get a cursor");
  221. #else
  222. DBC *Cursor;
  223. if ((errno = Dbp->cursor(Dbp,0,&Cursor)) != 0)
  224. return _error->Error("Unable to get a cursor");
  225. #endif
  226. DBT Key;
  227. DBT Data;
  228. memset(&Key,0,sizeof(Key));
  229. memset(&Data,0,sizeof(Data));
  230. while ((errno = Cursor->c_get(Cursor,&Key,&Data,DB_NEXT)) == 0)
  231. {
  232. const char *Colon = (char *)Key.data;
  233. for (; Colon != (char *)Key.data+Key.size && *Colon != ':'; Colon++);
  234. if ((char *)Key.data+Key.size - Colon > 2)
  235. {
  236. if (stringcmp((char *)Key.data,Colon,"st") == 0 ||
  237. stringcmp((char *)Key.data,Colon,"cn") == 0 ||
  238. stringcmp((char *)Key.data,Colon,"m5") == 0 ||
  239. stringcmp((char *)Key.data,Colon,"cl") == 0)
  240. {
  241. if (FileExists(string(Colon+1,(const char *)Key.data+Key.size)) == true)
  242. continue;
  243. }
  244. }
  245. Cursor->c_del(Cursor,0);
  246. }
  247. return true;
  248. }
  249. /*}}}*/