cachedb.cc 15 KB

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