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.
 
 
 
 
 
 

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