cachedb.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 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 a 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 filei /*{{{*/
  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()
  92. {
  93. if ((CurStat.Flags & FlSize) == FlSize)
  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 thre 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 FileName, bool DoControl, bool DoContents,
  148. bool GenContentsOnly,
  149. bool DoMD5, bool DoSHA1, bool DoSHA256)
  150. {
  151. this->FileName = FileName;
  152. if (GetCurStat() == false)
  153. {
  154. return false;
  155. }
  156. OldStat = CurStat;
  157. if (GetFileStat() == false)
  158. {
  159. delete Fd;
  160. Fd = NULL;
  161. return false;
  162. }
  163. Stats.Bytes += CurStat.FileSize;
  164. Stats.Packages++;
  165. if ((DoControl && LoadControl() == false)
  166. || (DoContents && LoadContents(GenContentsOnly) == false)
  167. || (DoMD5 && GetMD5(false) == false)
  168. || (DoSHA1 && GetSHA1(false) == false)
  169. || (DoSHA256 && GetSHA256(false) == false))
  170. {
  171. delete Fd;
  172. Fd = NULL;
  173. delete DebFile;
  174. DebFile = NULL;
  175. return false;
  176. }
  177. delete Fd;
  178. Fd = NULL;
  179. delete DebFile;
  180. DebFile = NULL;
  181. return true;
  182. }
  183. /*}}}*/
  184. // CacheDB::LoadControl - Load Control information /*{{{*/
  185. // ---------------------------------------------------------------------
  186. /* */
  187. bool CacheDB::LoadControl()
  188. {
  189. // Try to read the control information out of the DB.
  190. if ((CurStat.Flags & FlControl) == FlControl)
  191. {
  192. // Lookup the control information
  193. InitQuery("cl");
  194. if (Get() == true && Control.TakeControl(Data.data,Data.size) == true)
  195. return true;
  196. CurStat.Flags &= ~FlControl;
  197. }
  198. if (Fd == NULL && OpenFile() == false)
  199. {
  200. return false;
  201. }
  202. // Create a deb instance to read the archive
  203. if (DebFile == 0)
  204. {
  205. DebFile = new debDebFile(*Fd);
  206. if (_error->PendingError() == true)
  207. return false;
  208. }
  209. Stats.Misses++;
  210. if (Control.Read(*DebFile) == false)
  211. return false;
  212. if (Control.Control == 0)
  213. return _error->Error(_("Archive has no control record"));
  214. // Write back the control information
  215. InitQuery("cl");
  216. if (Put(Control.Control,Control.Length) == true)
  217. CurStat.Flags |= FlControl;
  218. return true;
  219. }
  220. /*}}}*/
  221. // CacheDB::LoadContents - Load the File Listing /*{{{*/
  222. // ---------------------------------------------------------------------
  223. /* */
  224. bool CacheDB::LoadContents(bool GenOnly)
  225. {
  226. // Try to read the control information out of the DB.
  227. if ((CurStat.Flags & FlContents) == FlContents)
  228. {
  229. if (GenOnly == true)
  230. return true;
  231. // Lookup the contents information
  232. InitQuery("cn");
  233. if (Get() == true)
  234. {
  235. if (Contents.TakeContents(Data.data,Data.size) == true)
  236. return true;
  237. }
  238. CurStat.Flags &= ~FlContents;
  239. }
  240. if (Fd == NULL && OpenFile() == false)
  241. {
  242. return false;
  243. }
  244. // Create a deb instance to read the archive
  245. if (DebFile == 0)
  246. {
  247. DebFile = new debDebFile(*Fd);
  248. if (_error->PendingError() == true)
  249. return false;
  250. }
  251. if (Contents.Read(*DebFile) == false)
  252. return false;
  253. // Write back the control information
  254. InitQuery("cn");
  255. if (Put(Contents.Data,Contents.CurSize) == true)
  256. CurStat.Flags |= FlContents;
  257. return true;
  258. }
  259. /*}}}*/
  260. static string bytes2hex(uint8_t *bytes, size_t length) {
  261. char space[65];
  262. if (length * 2 > sizeof(space) - 1) length = (sizeof(space) - 1) / 2;
  263. for (size_t i = 0; i < length; i++)
  264. snprintf(&space[i*2], 3, "%02x", bytes[i]);
  265. return string(space);
  266. }
  267. static inline unsigned char xdig2num(char dig) {
  268. if (isdigit(dig)) return dig - '0';
  269. if ('a' <= dig && dig <= 'f') return dig - 'a' + 10;
  270. if ('A' <= dig && dig <= 'F') return dig - 'A' + 10;
  271. return 0;
  272. }
  273. static void hex2bytes(uint8_t *bytes, const char *hex, int length) {
  274. while (length-- > 0) {
  275. *bytes = 0;
  276. if (isxdigit(hex[0]) && isxdigit(hex[1])) {
  277. *bytes = xdig2num(hex[0]) * 16 + xdig2num(hex[1]);
  278. hex += 2;
  279. }
  280. bytes++;
  281. }
  282. }
  283. // CacheDB::GetMD5 - Get the MD5 hash /*{{{*/
  284. // ---------------------------------------------------------------------
  285. /* */
  286. bool CacheDB::GetMD5(bool GenOnly)
  287. {
  288. // Try to read the control information out of the DB.
  289. if ((CurStat.Flags & FlMD5) == FlMD5)
  290. {
  291. if (GenOnly == true)
  292. return true;
  293. MD5Res = bytes2hex(CurStat.MD5, sizeof(CurStat.MD5));
  294. return true;
  295. }
  296. Stats.MD5Bytes += CurStat.FileSize;
  297. if (Fd == NULL && OpenFile() == false)
  298. {
  299. return false;
  300. }
  301. MD5Summation MD5;
  302. if (Fd->Seek(0) == false || MD5.AddFD(Fd->Fd(),CurStat.FileSize) == false)
  303. return false;
  304. MD5Res = MD5.Result();
  305. hex2bytes(CurStat.MD5, MD5Res.data(), sizeof(CurStat.MD5));
  306. CurStat.Flags |= FlMD5;
  307. return true;
  308. }
  309. /*}}}*/
  310. // CacheDB::GetSHA1 - Get the SHA1 hash /*{{{*/
  311. // ---------------------------------------------------------------------
  312. /* */
  313. bool CacheDB::GetSHA1(bool GenOnly)
  314. {
  315. // Try to read the control information out of the DB.
  316. if ((CurStat.Flags & FlSHA1) == FlSHA1)
  317. {
  318. if (GenOnly == true)
  319. return true;
  320. SHA1Res = bytes2hex(CurStat.SHA1, sizeof(CurStat.SHA1));
  321. return true;
  322. }
  323. Stats.SHA1Bytes += CurStat.FileSize;
  324. if (Fd == NULL && OpenFile() == false)
  325. {
  326. return false;
  327. }
  328. SHA1Summation SHA1;
  329. if (Fd->Seek(0) == false || SHA1.AddFD(Fd->Fd(),CurStat.FileSize) == false)
  330. return false;
  331. SHA1Res = SHA1.Result();
  332. hex2bytes(CurStat.SHA1, SHA1Res.data(), sizeof(CurStat.SHA1));
  333. CurStat.Flags |= FlSHA1;
  334. return true;
  335. }
  336. /*}}}*/
  337. // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
  338. // ---------------------------------------------------------------------
  339. /* */
  340. bool CacheDB::GetSHA256(bool GenOnly)
  341. {
  342. // Try to read the control information out of the DB.
  343. if ((CurStat.Flags & FlSHA256) == FlSHA256)
  344. {
  345. if (GenOnly == true)
  346. return true;
  347. SHA256Res = bytes2hex(CurStat.SHA256, sizeof(CurStat.SHA256));
  348. return true;
  349. }
  350. Stats.SHA256Bytes += CurStat.FileSize;
  351. if (Fd == NULL && OpenFile() == false)
  352. {
  353. return false;
  354. }
  355. SHA256Summation SHA256;
  356. if (Fd->Seek(0) == false || SHA256.AddFD(Fd->Fd(),CurStat.FileSize) == false)
  357. return false;
  358. SHA256Res = SHA256.Result();
  359. hex2bytes(CurStat.SHA256, SHA256Res.data(), sizeof(CurStat.SHA256));
  360. CurStat.Flags |= FlSHA256;
  361. return true;
  362. }
  363. /*}}}*/
  364. // CacheDB::Finish - Write back the cache structure /*{{{*/
  365. // ---------------------------------------------------------------------
  366. /* */
  367. bool CacheDB::Finish()
  368. {
  369. // Optimize away some writes.
  370. if (CurStat.Flags == OldStat.Flags &&
  371. CurStat.mtime == OldStat.mtime)
  372. return true;
  373. // Write the stat information
  374. CurStat.Flags = htonl(CurStat.Flags);
  375. CurStat.FileSize = htonl(CurStat.FileSize);
  376. InitQuery("st");
  377. Put(&CurStat,sizeof(CurStat));
  378. CurStat.Flags = ntohl(CurStat.Flags);
  379. CurStat.FileSize = ntohl(CurStat.FileSize);
  380. return true;
  381. }
  382. /*}}}*/
  383. // CacheDB::Clean - Clean the Database /*{{{*/
  384. // ---------------------------------------------------------------------
  385. /* Tidy the database by removing files that no longer exist at all. */
  386. bool CacheDB::Clean()
  387. {
  388. if (DBLoaded == false)
  389. return true;
  390. /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
  391. needs the lower one and 2.7.7 needs the upper.. */
  392. DBC *Cursor;
  393. if ((errno = Dbp->cursor(Dbp, NULL, &Cursor, 0)) != 0)
  394. return _error->Error(_("Unable to get a cursor"));
  395. DBT Key;
  396. DBT Data;
  397. memset(&Key,0,sizeof(Key));
  398. memset(&Data,0,sizeof(Data));
  399. while ((errno = Cursor->c_get(Cursor,&Key,&Data,DB_NEXT)) == 0)
  400. {
  401. const char *Colon = (char*)memrchr(Key.data, ':', Key.size);
  402. if (Colon)
  403. {
  404. if (stringcmp(Colon + 1, (char *)Key.data+Key.size,"st") == 0 ||
  405. stringcmp(Colon + 1, (char *)Key.data+Key.size,"cl") == 0 ||
  406. stringcmp(Colon + 1, (char *)Key.data+Key.size,"cn") == 0)
  407. {
  408. if (FileExists(string((const char *)Key.data,Colon)) == true)
  409. continue;
  410. }
  411. }
  412. Cursor->c_del(Cursor,0);
  413. }
  414. Dbp->compact(Dbp, NULL, NULL, NULL, NULL, DB_FREE_SPACE, NULL);
  415. return true;
  416. }
  417. /*}}}*/