cachedb.cc 14 KB

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