cachedb.cc 13 KB

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