cachedb.cc 16 KB

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