You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

580 lines
15 KiB

  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::GetCurStat - Set the CurStat variable. /*{{{*/
  168. // ---------------------------------------------------------------------
  169. /* Sets the CurStat variable. Either to 0 if no database is used
  170. * or to the value in the database if one is used */
  171. bool CacheDB::GetCurStat()
  172. {
  173. memset(&CurStat,0,sizeof(CurStat));
  174. if (DBLoaded)
  175. {
  176. /* First see if there is anything about it
  177. in the database */
  178. /* Get the flags (and mtime) */
  179. InitQueryStats();
  180. // Ensure alignment of the returned structure
  181. Data.data = &CurStat;
  182. Data.ulen = sizeof(CurStat);
  183. Data.flags = DB_DBT_USERMEM;
  184. if (Get() == false)
  185. {
  186. CurStat.Flags = 0;
  187. }
  188. CurStat.Flags = ntohl(CurStat.Flags);
  189. CurStat.FileSize = ntohl(CurStat.FileSize);
  190. }
  191. return true;
  192. }
  193. /*}}}*/
  194. // CacheDB::GetFileInfo - Get all the info about the file /*{{{*/
  195. // ---------------------------------------------------------------------
  196. bool CacheDB::GetFileInfo(std::string const &FileName, bool const &DoControl,
  197. bool const &DoContents,
  198. bool const &GenContentsOnly,
  199. bool const &DoSource,
  200. bool const &DoMD5, bool const &DoSHA1,
  201. bool const &DoSHA256, bool const &DoSHA512,
  202. bool const &checkMtime)
  203. {
  204. bool result = true;
  205. this->FileName = FileName;
  206. if (GetCurStat() == false)
  207. return false;
  208. OldStat = CurStat;
  209. if (GetFileStat(checkMtime) == false)
  210. return false;
  211. /* if mtime changed, update CurStat from disk */
  212. if (checkMtime == true && OldStat.mtime != CurStat.mtime)
  213. CurStat.Flags = FlSize;
  214. Stats.Bytes += CurStat.FileSize;
  215. Stats.Packages++;
  216. if ((DoControl && LoadControl() == false)
  217. || (DoContents && LoadContents(GenContentsOnly) == false)
  218. || (DoSource && LoadSource() == false)
  219. || (DoMD5 && GetMD5(false) == false)
  220. || (DoSHA1 && GetSHA1(false) == false)
  221. || (DoSHA256 && GetSHA256(false) == false)
  222. || (DoSHA512 && GetSHA512(false) == false) )
  223. {
  224. result = false;
  225. }
  226. return result;
  227. }
  228. /*}}}*/
  229. bool CacheDB::LoadSource()
  230. {
  231. // Try to read the control information out of the DB.
  232. if ((CurStat.Flags & FlSource) == FlSource)
  233. {
  234. // Lookup the control information
  235. InitQuerySource();
  236. if (Get() == true && Dsc.TakeDsc(Data.data, Data.size) == true)
  237. {
  238. return true;
  239. }
  240. CurStat.Flags &= ~FlSource;
  241. }
  242. if (OpenFile() == false)
  243. return false;
  244. Stats.Misses++;
  245. if (Dsc.Read(FileName) == false)
  246. return false;
  247. if (Dsc.Data == 0)
  248. return _error->Error(_("Failed to read .dsc"));
  249. // Write back the control information
  250. InitQuerySource();
  251. if (Put(Dsc.Data, Dsc.Length) == true)
  252. CurStat.Flags |= FlSource;
  253. return true;
  254. }
  255. // CacheDB::LoadControl - Load Control information /*{{{*/
  256. // ---------------------------------------------------------------------
  257. /* */
  258. bool CacheDB::LoadControl()
  259. {
  260. // Try to read the control information out of the DB.
  261. if ((CurStat.Flags & FlControl) == FlControl)
  262. {
  263. // Lookup the control information
  264. InitQueryControl();
  265. if (Get() == true && Control.TakeControl(Data.data,Data.size) == true)
  266. return true;
  267. CurStat.Flags &= ~FlControl;
  268. }
  269. if(OpenDebFile() == false)
  270. return false;
  271. Stats.Misses++;
  272. if (Control.Read(*DebFile) == false)
  273. return false;
  274. if (Control.Control == 0)
  275. return _error->Error(_("Archive has no control record"));
  276. // Write back the control information
  277. InitQueryControl();
  278. if (Put(Control.Control,Control.Length) == true)
  279. CurStat.Flags |= FlControl;
  280. return true;
  281. }
  282. /*}}}*/
  283. // CacheDB::LoadContents - Load the File Listing /*{{{*/
  284. // ---------------------------------------------------------------------
  285. /* */
  286. bool CacheDB::LoadContents(bool const &GenOnly)
  287. {
  288. // Try to read the control information out of the DB.
  289. if ((CurStat.Flags & FlContents) == FlContents)
  290. {
  291. if (GenOnly == true)
  292. return true;
  293. // Lookup the contents information
  294. InitQueryContent();
  295. if (Get() == true)
  296. {
  297. if (Contents.TakeContents(Data.data,Data.size) == true)
  298. return true;
  299. }
  300. CurStat.Flags &= ~FlContents;
  301. }
  302. if(OpenDebFile() == false)
  303. return false;
  304. Stats.Misses++;
  305. if (Contents.Read(*DebFile) == false)
  306. return false;
  307. // Write back the control information
  308. InitQueryContent();
  309. if (Put(Contents.Data,Contents.CurSize) == true)
  310. CurStat.Flags |= FlContents;
  311. return true;
  312. }
  313. /*}}}*/
  314. static std::string bytes2hex(uint8_t *bytes, size_t length) {
  315. char buf[3];
  316. std::string space;
  317. space.reserve(length*2 + 1);
  318. for (size_t i = 0; i < length; i++) {
  319. snprintf(buf, sizeof(buf), "%02x", bytes[i]);
  320. space.append(buf);
  321. }
  322. return space;
  323. }
  324. static inline unsigned char xdig2num(char const &dig) {
  325. if (isdigit(dig)) return dig - '0';
  326. if ('a' <= dig && dig <= 'f') return dig - 'a' + 10;
  327. if ('A' <= dig && dig <= 'F') return dig - 'A' + 10;
  328. return 0;
  329. }
  330. static void hex2bytes(uint8_t *bytes, const char *hex, int length) {
  331. while (length-- > 0) {
  332. *bytes = 0;
  333. if (isxdigit(hex[0]) && isxdigit(hex[1])) {
  334. *bytes = xdig2num(hex[0]) * 16 + xdig2num(hex[1]);
  335. hex += 2;
  336. }
  337. bytes++;
  338. }
  339. }
  340. // CacheDB::GetMD5 - Get the MD5 hash /*{{{*/
  341. // ---------------------------------------------------------------------
  342. /* */
  343. bool CacheDB::GetMD5(bool const &GenOnly)
  344. {
  345. // Try to read the control information out of the DB.
  346. if ((CurStat.Flags & FlMD5) == FlMD5)
  347. {
  348. if (GenOnly == true)
  349. return true;
  350. MD5Res = bytes2hex(CurStat.MD5, sizeof(CurStat.MD5));
  351. return true;
  352. }
  353. Stats.MD5Bytes += CurStat.FileSize;
  354. if (OpenFile() == false)
  355. return false;
  356. MD5Summation MD5;
  357. if (Fd->Seek(0) == false || MD5.AddFD(*Fd, CurStat.FileSize) == false)
  358. return false;
  359. MD5Res = MD5.Result();
  360. hex2bytes(CurStat.MD5, MD5Res.data(), sizeof(CurStat.MD5));
  361. CurStat.Flags |= FlMD5;
  362. return true;
  363. }
  364. /*}}}*/
  365. // CacheDB::GetSHA1 - Get the SHA1 hash /*{{{*/
  366. // ---------------------------------------------------------------------
  367. /* */
  368. bool CacheDB::GetSHA1(bool const &GenOnly)
  369. {
  370. // Try to read the control information out of the DB.
  371. if ((CurStat.Flags & FlSHA1) == FlSHA1)
  372. {
  373. if (GenOnly == true)
  374. return true;
  375. SHA1Res = bytes2hex(CurStat.SHA1, sizeof(CurStat.SHA1));
  376. return true;
  377. }
  378. Stats.SHA1Bytes += CurStat.FileSize;
  379. if (OpenFile() == false)
  380. return false;
  381. SHA1Summation SHA1;
  382. if (Fd->Seek(0) == false || SHA1.AddFD(*Fd, CurStat.FileSize) == false)
  383. return false;
  384. SHA1Res = SHA1.Result();
  385. hex2bytes(CurStat.SHA1, SHA1Res.data(), sizeof(CurStat.SHA1));
  386. CurStat.Flags |= FlSHA1;
  387. return true;
  388. }
  389. /*}}}*/
  390. // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
  391. // ---------------------------------------------------------------------
  392. /* */
  393. bool CacheDB::GetSHA256(bool const &GenOnly)
  394. {
  395. // Try to read the control information out of the DB.
  396. if ((CurStat.Flags & FlSHA256) == FlSHA256)
  397. {
  398. if (GenOnly == true)
  399. return true;
  400. SHA256Res = bytes2hex(CurStat.SHA256, sizeof(CurStat.SHA256));
  401. return true;
  402. }
  403. Stats.SHA256Bytes += CurStat.FileSize;
  404. if (OpenFile() == false)
  405. return false;
  406. SHA256Summation SHA256;
  407. if (Fd->Seek(0) == false || SHA256.AddFD(*Fd, CurStat.FileSize) == false)
  408. return false;
  409. SHA256Res = SHA256.Result();
  410. hex2bytes(CurStat.SHA256, SHA256Res.data(), sizeof(CurStat.SHA256));
  411. CurStat.Flags |= FlSHA256;
  412. return true;
  413. }
  414. /*}}}*/
  415. // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
  416. // ---------------------------------------------------------------------
  417. /* */
  418. bool CacheDB::GetSHA512(bool const &GenOnly)
  419. {
  420. // Try to read the control information out of the DB.
  421. if ((CurStat.Flags & FlSHA512) == FlSHA512)
  422. {
  423. if (GenOnly == true)
  424. return true;
  425. SHA512Res = bytes2hex(CurStat.SHA512, sizeof(CurStat.SHA512));
  426. return true;
  427. }
  428. Stats.SHA512Bytes += CurStat.FileSize;
  429. if (OpenFile() == false)
  430. return false;
  431. SHA512Summation SHA512;
  432. if (Fd->Seek(0) == false || SHA512.AddFD(*Fd, CurStat.FileSize) == false)
  433. return false;
  434. SHA512Res = SHA512.Result();
  435. hex2bytes(CurStat.SHA512, SHA512Res.data(), sizeof(CurStat.SHA512));
  436. CurStat.Flags |= FlSHA512;
  437. return true;
  438. }
  439. /*}}}*/
  440. // CacheDB::Finish - Write back the cache structure /*{{{*/
  441. // ---------------------------------------------------------------------
  442. /* */
  443. bool CacheDB::Finish()
  444. {
  445. // Optimize away some writes.
  446. if (CurStat.Flags == OldStat.Flags &&
  447. CurStat.mtime == OldStat.mtime)
  448. return true;
  449. // Write the stat information
  450. CurStat.Flags = htonl(CurStat.Flags);
  451. CurStat.FileSize = htonl(CurStat.FileSize);
  452. InitQueryStats();
  453. Put(&CurStat,sizeof(CurStat));
  454. CurStat.Flags = ntohl(CurStat.Flags);
  455. CurStat.FileSize = ntohl(CurStat.FileSize);
  456. return true;
  457. }
  458. /*}}}*/
  459. // CacheDB::Clean - Clean the Database /*{{{*/
  460. // ---------------------------------------------------------------------
  461. /* Tidy the database by removing files that no longer exist at all. */
  462. bool CacheDB::Clean()
  463. {
  464. if (DBLoaded == false)
  465. return true;
  466. /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
  467. needs the lower one and 2.7.7 needs the upper.. */
  468. DBC *Cursor;
  469. if ((errno = Dbp->cursor(Dbp, NULL, &Cursor, 0)) != 0)
  470. return _error->Error(_("Unable to get a cursor"));
  471. DBT Key;
  472. DBT Data;
  473. memset(&Key,0,sizeof(Key));
  474. memset(&Data,0,sizeof(Data));
  475. while ((errno = Cursor->c_get(Cursor,&Key,&Data,DB_NEXT)) == 0)
  476. {
  477. const char *Colon = (char*)memrchr(Key.data, ':', Key.size);
  478. if (Colon)
  479. {
  480. if (stringcmp(Colon + 1, (char *)Key.data+Key.size,"st") == 0 ||
  481. stringcmp(Colon + 1, (char *)Key.data+Key.size,"cl") == 0 ||
  482. stringcmp(Colon + 1, (char *)Key.data+Key.size,"cs") == 0 ||
  483. stringcmp(Colon + 1, (char *)Key.data+Key.size,"cn") == 0)
  484. {
  485. std::string FileName = std::string((const char *)Key.data,Colon);
  486. if (FileExists(FileName) == true) {
  487. continue;
  488. }
  489. }
  490. }
  491. Cursor->c_del(Cursor,0);
  492. }
  493. int res = Dbp->compact(Dbp, NULL, NULL, NULL, NULL, DB_FREE_SPACE, NULL);
  494. if (res < 0)
  495. _error->Warning("compact failed with result %i", res);
  496. if(_config->FindB("Debug::APT::FTPArchive::Clean", false) == true)
  497. Dbp->stat_print(Dbp, 0);
  498. return true;
  499. }
  500. /*}}}*/