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.
 
 
 
 
 
 

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