cachedb.cc 17 KB

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