cachedb.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: cachedb.cc,v 1.7 2004/05/08 19:41:01 mdz 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 <apti18n.h>
  15. #include <apt-pkg/error.h>
  16. #include <apt-pkg/md5.h>
  17. #include <apt-pkg/sha1.h>
  18. #include <apt-pkg/sha256.h>
  19. #include <apt-pkg/strutl.h>
  20. #include <apt-pkg/configuration.h>
  21. #include <netinet/in.h> // htonl, etc
  22. /*}}}*/
  23. // CacheDB::ReadyDB - Ready the DB2 /*{{{*/
  24. // ---------------------------------------------------------------------
  25. /* This opens the DB2 file for caching package information */
  26. bool CacheDB::ReadyDB(string DB)
  27. {
  28. int err;
  29. ReadOnly = _config->FindB("APT::FTPArchive::ReadOnlyDB",false);
  30. // Close the old DB
  31. if (Dbp != 0)
  32. Dbp->close(Dbp,0);
  33. /* Check if the DB was disabled while running and deal with a
  34. corrupted DB */
  35. if (DBFailed() == true)
  36. {
  37. _error->Warning(_("DB was corrupted, file renamed to %s.old"),DBFile.c_str());
  38. rename(DBFile.c_str(),(DBFile+".old").c_str());
  39. }
  40. DBLoaded = false;
  41. Dbp = 0;
  42. DBFile = string();
  43. if (DB.empty())
  44. return true;
  45. db_create(&Dbp, NULL, 0);
  46. if ((err = Dbp->open(Dbp, NULL, DB.c_str(), NULL, DB_BTREE,
  47. (ReadOnly?DB_RDONLY:DB_CREATE),
  48. 0644)) != 0)
  49. {
  50. if (err == DB_OLD_VERSION)
  51. {
  52. _error->Warning(_("DB is old, attempting to upgrade %s"),DBFile.c_str());
  53. err = Dbp->upgrade(Dbp, DB.c_str(), 0);
  54. if (!err)
  55. err = Dbp->open(Dbp, NULL, DB.c_str(), NULL, DB_HASH,
  56. (ReadOnly?DB_RDONLY:DB_CREATE), 0644);
  57. }
  58. if (err)
  59. {
  60. Dbp = 0;
  61. return _error->Error(_("Unable to open DB file %s: %s"),DB.c_str(), db_strerror(err));
  62. }
  63. }
  64. DBFile = DB;
  65. DBLoaded = true;
  66. return true;
  67. }
  68. /*}}}*/
  69. // CacheDB::OpenFile - Open the filei /*{{{*/
  70. // ---------------------------------------------------------------------
  71. /* */
  72. bool CacheDB::OpenFile()
  73. {
  74. Fd = new FileFd(FileName,FileFd::ReadOnly);
  75. if (_error->PendingError() == true)
  76. {
  77. delete Fd;
  78. Fd = NULL;
  79. return false;
  80. }
  81. return true;
  82. }
  83. /*}}}*/
  84. // CacheDB::GetFileStat - Get stats from the file /*{{{*/
  85. // ---------------------------------------------------------------------
  86. /* This gets the size from the database if it's there. If we need
  87. * to look at the file, also get the mtime from the file. */
  88. bool CacheDB::GetFileStat()
  89. {
  90. if ((CurStat.Flags & FlSize) == FlSize)
  91. {
  92. /* Already worked out the file size */
  93. }
  94. else
  95. {
  96. /* Get it from the file. */
  97. if (Fd == NULL && OpenFile() == false)
  98. {
  99. return false;
  100. }
  101. // Stat the file
  102. struct stat St;
  103. if (fstat(Fd->Fd(),&St) != 0)
  104. {
  105. return _error->Errno("fstat",
  106. _("Failed to stat %s"),FileName.c_str());
  107. }
  108. CurStat.FileSize = St.st_size;
  109. CurStat.mtime = htonl(St.st_mtime);
  110. CurStat.Flags |= FlSize;
  111. }
  112. return true;
  113. }
  114. /*}}}*/
  115. // CacheDB::GetCurStat - Set the CurStat variable. /*{{{*/
  116. // ---------------------------------------------------------------------
  117. /* Sets the CurStat variable. Either to 0 if no database is used
  118. * or to the value in the database if one is used */
  119. bool CacheDB::GetCurStat()
  120. {
  121. memset(&CurStat,0,sizeof(CurStat));
  122. if (DBLoaded)
  123. {
  124. /* First see if thre is anything about it
  125. in the database */
  126. /* Get the flags (and mtime) */
  127. InitQuery("st");
  128. // Ensure alignment of the returned structure
  129. Data.data = &CurStat;
  130. Data.ulen = sizeof(CurStat);
  131. Data.flags = DB_DBT_USERMEM;
  132. if (Get() == false)
  133. {
  134. CurStat.Flags = 0;
  135. }
  136. CurStat.Flags = ntohl(CurStat.Flags);
  137. CurStat.FileSize = ntohl(CurStat.FileSize);
  138. }
  139. return true;
  140. }
  141. /*}}}*/
  142. // CacheDB::GetFileInfo - Get all the info about the file /*{{{*/
  143. // ---------------------------------------------------------------------
  144. bool CacheDB::GetFileInfo(string FileName, bool DoControl, bool DoContents,
  145. bool GenContentsOnly,
  146. bool DoMD5, bool DoSHA1, bool DoSHA256)
  147. {
  148. this->FileName = FileName;
  149. if (GetCurStat() == false)
  150. {
  151. return false;
  152. }
  153. OldStat = CurStat;
  154. if (GetFileStat() == false)
  155. {
  156. delete Fd;
  157. Fd = NULL;
  158. return false;
  159. }
  160. Stats.Bytes += CurStat.FileSize;
  161. Stats.Packages++;
  162. if (DoControl && LoadControl() == false
  163. || DoContents && LoadContents(GenContentsOnly) == false
  164. || DoMD5 && GetMD5(false) == false
  165. || DoSHA1 && GetSHA1(false) == false
  166. || DoSHA256 && GetSHA256(false) == false)
  167. {
  168. delete Fd;
  169. Fd = NULL;
  170. delete DebFile;
  171. DebFile = NULL;
  172. return false;
  173. }
  174. delete Fd;
  175. Fd = NULL;
  176. delete DebFile;
  177. DebFile = NULL;
  178. return true;
  179. }
  180. /*}}}*/
  181. // CacheDB::LoadControl - Load Control information /*{{{*/
  182. // ---------------------------------------------------------------------
  183. /* */
  184. bool CacheDB::LoadControl()
  185. {
  186. // Try to read the control information out of the DB.
  187. if ((CurStat.Flags & FlControl) == FlControl)
  188. {
  189. // Lookup the control information
  190. InitQuery("cl");
  191. if (Get() == true && Control.TakeControl(Data.data,Data.size) == true)
  192. return true;
  193. CurStat.Flags &= ~FlControl;
  194. }
  195. if (Fd == NULL && OpenFile() == false)
  196. {
  197. return false;
  198. }
  199. // Create a deb instance to read the archive
  200. if (DebFile == 0)
  201. {
  202. DebFile = new debDebFile(*Fd);
  203. if (_error->PendingError() == true)
  204. return false;
  205. }
  206. Stats.Misses++;
  207. if (Control.Read(*DebFile) == false)
  208. return false;
  209. if (Control.Control == 0)
  210. return _error->Error(_("Archive has no control record"));
  211. // Write back the control information
  212. InitQuery("cl");
  213. if (Put(Control.Control,Control.Length) == true)
  214. CurStat.Flags |= FlControl;
  215. return true;
  216. }
  217. /*}}}*/
  218. // CacheDB::LoadContents - Load the File Listing /*{{{*/
  219. // ---------------------------------------------------------------------
  220. /* */
  221. bool CacheDB::LoadContents(bool GenOnly)
  222. {
  223. // Try to read the control information out of the DB.
  224. if ((CurStat.Flags & FlContents) == FlContents)
  225. {
  226. if (GenOnly == true)
  227. return true;
  228. // Lookup the contents information
  229. InitQuery("cn");
  230. if (Get() == true)
  231. {
  232. if (Contents.TakeContents(Data.data,Data.size) == true)
  233. return true;
  234. }
  235. CurStat.Flags &= ~FlContents;
  236. }
  237. if (Fd == NULL && OpenFile() == false)
  238. {
  239. return false;
  240. }
  241. // Create a deb instance to read the archive
  242. if (DebFile == 0)
  243. {
  244. DebFile = new debDebFile(*Fd);
  245. if (_error->PendingError() == true)
  246. return false;
  247. }
  248. if (Contents.Read(*DebFile) == false)
  249. return false;
  250. // Write back the control information
  251. InitQuery("cn");
  252. if (Put(Contents.Data,Contents.CurSize) == true)
  253. CurStat.Flags |= FlContents;
  254. return true;
  255. }
  256. /*}}}*/
  257. static string bytes2hex(uint8_t *bytes, size_t length) {
  258. char space[65];
  259. if (length * 2 > sizeof(space) - 1) length = (sizeof(space) - 1) / 2;
  260. for (size_t i = 0; i < length; i++)
  261. snprintf(&space[i*2], 3, "%02x", bytes[i]);
  262. return string(space);
  263. }
  264. static inline unsigned char xdig2num(char dig) {
  265. if (isdigit(dig)) return dig - '0';
  266. if ('a' <= dig && dig <= 'f') return dig - 'a' + 10;
  267. if ('A' <= dig && dig <= 'F') return dig - 'A' + 10;
  268. return 0;
  269. }
  270. static void hex2bytes(uint8_t *bytes, const char *hex, int length) {
  271. while (length-- > 0) {
  272. *bytes = 0;
  273. if (isxdigit(hex[0]) && isxdigit(hex[1])) {
  274. *bytes = xdig2num(hex[0]) * 16 + xdig2num(hex[1]);
  275. hex += 2;
  276. }
  277. bytes++;
  278. }
  279. }
  280. // CacheDB::GetMD5 - Get the MD5 hash /*{{{*/
  281. // ---------------------------------------------------------------------
  282. /* */
  283. bool CacheDB::GetMD5(bool GenOnly)
  284. {
  285. // Try to read the control information out of the DB.
  286. if ((CurStat.Flags & FlMD5) == FlMD5)
  287. {
  288. if (GenOnly == true)
  289. return true;
  290. MD5Res = bytes2hex(CurStat.MD5, sizeof(CurStat.MD5));
  291. return true;
  292. }
  293. Stats.MD5Bytes += CurStat.FileSize;
  294. if (Fd == NULL && OpenFile() == false)
  295. {
  296. return false;
  297. }
  298. MD5Summation MD5;
  299. if (Fd->Seek(0) == false || MD5.AddFD(Fd->Fd(),CurStat.FileSize) == false)
  300. return false;
  301. MD5Res = MD5.Result();
  302. hex2bytes(CurStat.MD5, MD5Res.data(), sizeof(CurStat.MD5));
  303. CurStat.Flags |= FlMD5;
  304. return true;
  305. }
  306. /*}}}*/
  307. // CacheDB::GetSHA1 - Get the SHA1 hash /*{{{*/
  308. // ---------------------------------------------------------------------
  309. /* */
  310. bool CacheDB::GetSHA1(bool GenOnly)
  311. {
  312. // Try to read the control information out of the DB.
  313. if ((CurStat.Flags & FlSHA1) == FlSHA1)
  314. {
  315. if (GenOnly == true)
  316. return true;
  317. SHA1Res = bytes2hex(CurStat.SHA1, sizeof(CurStat.SHA1));
  318. return true;
  319. }
  320. Stats.SHA1Bytes += CurStat.FileSize;
  321. if (Fd == NULL && OpenFile() == false)
  322. {
  323. return false;
  324. }
  325. SHA1Summation SHA1;
  326. if (Fd->Seek(0) == false || SHA1.AddFD(Fd->Fd(),CurStat.FileSize) == false)
  327. return false;
  328. SHA1Res = SHA1.Result();
  329. hex2bytes(CurStat.SHA1, SHA1Res.data(), sizeof(CurStat.SHA1));
  330. CurStat.Flags |= FlSHA1;
  331. return true;
  332. }
  333. /*}}}*/
  334. // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
  335. // ---------------------------------------------------------------------
  336. /* */
  337. bool CacheDB::GetSHA256(bool GenOnly)
  338. {
  339. // Try to read the control information out of the DB.
  340. if ((CurStat.Flags & FlSHA256) == FlSHA256)
  341. {
  342. if (GenOnly == true)
  343. return true;
  344. SHA256Res = bytes2hex(CurStat.SHA256, sizeof(CurStat.SHA256));
  345. return true;
  346. }
  347. Stats.SHA256Bytes += CurStat.FileSize;
  348. if (Fd == NULL && OpenFile() == false)
  349. {
  350. return false;
  351. }
  352. SHA256Summation SHA256;
  353. if (Fd->Seek(0) == false || SHA256.AddFD(Fd->Fd(),CurStat.FileSize) == false)
  354. return false;
  355. SHA256Res = SHA256.Result();
  356. hex2bytes(CurStat.SHA256, SHA256Res.data(), sizeof(CurStat.SHA256));
  357. CurStat.Flags |= FlSHA256;
  358. return true;
  359. }
  360. /*}}}*/
  361. // CacheDB::Finish - Write back the cache structure /*{{{*/
  362. // ---------------------------------------------------------------------
  363. /* */
  364. bool CacheDB::Finish()
  365. {
  366. // Optimize away some writes.
  367. if (CurStat.Flags == OldStat.Flags &&
  368. CurStat.mtime == OldStat.mtime)
  369. return true;
  370. // Write the stat information
  371. CurStat.Flags = htonl(CurStat.Flags);
  372. CurStat.FileSize = htonl(CurStat.FileSize);
  373. InitQuery("st");
  374. Put(&CurStat,sizeof(CurStat));
  375. CurStat.Flags = ntohl(CurStat.Flags);
  376. CurStat.FileSize = ntohl(CurStat.FileSize);
  377. return true;
  378. }
  379. /*}}}*/
  380. // CacheDB::Clean - Clean the Database /*{{{*/
  381. // ---------------------------------------------------------------------
  382. /* Tidy the database by removing files that no longer exist at all. */
  383. bool CacheDB::Clean()
  384. {
  385. if (DBLoaded == false)
  386. return true;
  387. /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
  388. needs the lower one and 2.7.7 needs the upper.. */
  389. DBC *Cursor;
  390. if ((errno = Dbp->cursor(Dbp, NULL, &Cursor, 0)) != 0)
  391. return _error->Error(_("Unable to get a cursor"));
  392. DBT Key;
  393. DBT Data;
  394. memset(&Key,0,sizeof(Key));
  395. memset(&Data,0,sizeof(Data));
  396. while ((errno = Cursor->c_get(Cursor,&Key,&Data,DB_NEXT)) == 0)
  397. {
  398. const char *Colon = (char *)Key.data;
  399. for (; Colon != (char *)Key.data+Key.size && *Colon != ':'; Colon++);
  400. if ((char *)Key.data+Key.size - Colon > 2)
  401. {
  402. if (stringcmp((char *)Key.data,Colon,"st") == 0 ||
  403. stringcmp((char *)Key.data,Colon,"cn") == 0 ||
  404. stringcmp((char *)Key.data,Colon,"cl") == 0)
  405. {
  406. if (FileExists(string(Colon+1,(const char *)Key.data+Key.size)) == true)
  407. continue;
  408. }
  409. }
  410. Cursor->c_del(Cursor,0);
  411. }
  412. return true;
  413. }
  414. /*}}}*/