debindexfile.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: debindexfile.cc,v 1.5.2.3 2004/01/04 19:11:00 mdz Exp $
  4. /* ######################################################################
  5. Debian Specific sources.list types and the three sorts of Debian
  6. index files.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #include <apt-pkg/debindexfile.h>
  11. #include <apt-pkg/debsrcrecords.h>
  12. #include <apt-pkg/deblistparser.h>
  13. #include <apt-pkg/debrecords.h>
  14. #include <apt-pkg/sourcelist.h>
  15. #include <apt-pkg/configuration.h>
  16. #include <apt-pkg/progress.h>
  17. #include <apt-pkg/error.h>
  18. #include <apt-pkg/strutl.h>
  19. #include <apt-pkg/acquire-item.h>
  20. #include <apt-pkg/debmetaindex.h>
  21. #include <sys/stat.h>
  22. /*}}}*/
  23. // SourcesIndex::debSourcesIndex - Constructor /*{{{*/
  24. // ---------------------------------------------------------------------
  25. /* */
  26. debSourcesIndex::debSourcesIndex(string URI,string Dist,string Section,bool Trusted) :
  27. pkgIndexFile(Trusted), URI(URI), Dist(Dist), Section(Section)
  28. {
  29. }
  30. /*}}}*/
  31. // SourcesIndex::SourceInfo - Short 1 liner describing a source /*{{{*/
  32. // ---------------------------------------------------------------------
  33. /* The result looks like:
  34. http://foo/ stable/main src 1.1.1 (dsc) */
  35. string debSourcesIndex::SourceInfo(pkgSrcRecords::Parser const &Record,
  36. pkgSrcRecords::File const &File) const
  37. {
  38. string Res;
  39. Res = ::URI::SiteOnly(URI) + ' ';
  40. if (Dist[Dist.size() - 1] == '/')
  41. {
  42. if (Dist != "/")
  43. Res += Dist;
  44. }
  45. else
  46. Res += Dist + '/' + Section;
  47. Res += " ";
  48. Res += Record.Package();
  49. Res += " ";
  50. Res += Record.Version();
  51. if (File.Type.empty() == false)
  52. Res += " (" + File.Type + ")";
  53. return Res;
  54. }
  55. /*}}}*/
  56. // SourcesIndex::CreateSrcParser - Get a parser for the source files /*{{{*/
  57. // ---------------------------------------------------------------------
  58. /* */
  59. pkgSrcRecords::Parser *debSourcesIndex::CreateSrcParser() const
  60. {
  61. string SourcesURI = URItoFileName(IndexURI("Sources"));
  62. return new debSrcRecordParser(_config->FindDir("Dir::State::lists") +
  63. SourcesURI,this);
  64. }
  65. /*}}}*/
  66. // SourcesIndex::Describe - Give a descriptive path to the index /*{{{*/
  67. // ---------------------------------------------------------------------
  68. /* */
  69. string debSourcesIndex::Describe(bool Short) const
  70. {
  71. char S[300];
  72. if (Short == true)
  73. snprintf(S,sizeof(S),"%s",Info("Sources").c_str());
  74. else
  75. snprintf(S,sizeof(S),"%s (%s)",Info("Sources").c_str(),
  76. IndexFile("Sources").c_str());
  77. return S;
  78. }
  79. /*}}}*/
  80. // SourcesIndex::Info - One liner describing the index URI /*{{{*/
  81. // ---------------------------------------------------------------------
  82. /* */
  83. string debSourcesIndex::Info(const char *Type) const
  84. {
  85. string Info = ::URI::SiteOnly(URI) + ' ';
  86. if (Dist[Dist.size() - 1] == '/')
  87. {
  88. if (Dist != "/")
  89. Info += Dist;
  90. }
  91. else
  92. Info += Dist + '/' + Section;
  93. Info += " ";
  94. Info += Type;
  95. return Info;
  96. }
  97. /*}}}*/
  98. // SourcesIndex::Index* - Return the URI to the index files /*{{{*/
  99. // ---------------------------------------------------------------------
  100. /* */
  101. inline string debSourcesIndex::IndexFile(const char *Type) const
  102. {
  103. return URItoFileName(IndexURI(Type));
  104. }
  105. string debSourcesIndex::IndexURI(const char *Type) const
  106. {
  107. string Res;
  108. if (Dist[Dist.size() - 1] == '/')
  109. {
  110. if (Dist != "/")
  111. Res = URI + Dist;
  112. else
  113. Res = URI;
  114. }
  115. else
  116. Res = URI + "dists/" + Dist + '/' + Section +
  117. "/source/";
  118. Res += Type;
  119. return Res;
  120. }
  121. /*}}}*/
  122. // SourcesIndex::Exists - Check if the index is available /*{{{*/
  123. // ---------------------------------------------------------------------
  124. /* */
  125. bool debSourcesIndex::Exists() const
  126. {
  127. return FileExists(IndexFile("Sources"));
  128. }
  129. /*}}}*/
  130. // SourcesIndex::Size - Return the size of the index /*{{{*/
  131. // ---------------------------------------------------------------------
  132. /* */
  133. unsigned long debSourcesIndex::Size() const
  134. {
  135. struct stat S;
  136. if (stat(IndexFile("Sources").c_str(),&S) != 0)
  137. return 0;
  138. return S.st_size;
  139. }
  140. /*}}}*/
  141. // PackagesIndex::debPackagesIndex - Contructor /*{{{*/
  142. // ---------------------------------------------------------------------
  143. /* */
  144. debPackagesIndex::debPackagesIndex(string URI,string Dist,string Section,bool Trusted) :
  145. pkgIndexFile(Trusted), URI(URI), Dist(Dist), Section(Section)
  146. {
  147. }
  148. /*}}}*/
  149. // PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
  150. // ---------------------------------------------------------------------
  151. /* This is a shorter version that is designed to be < 60 chars or so */
  152. string debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver) const
  153. {
  154. string Res = ::URI::SiteOnly(URI) + ' ';
  155. if (Dist[Dist.size() - 1] == '/')
  156. {
  157. if (Dist != "/")
  158. Res += Dist;
  159. }
  160. else
  161. Res += Dist + '/' + Section;
  162. Res += " ";
  163. Res += Ver.ParentPkg().Name();
  164. Res += " ";
  165. Res += Ver.VerStr();
  166. return Res;
  167. }
  168. /*}}}*/
  169. // PackagesIndex::Describe - Give a descriptive path to the index /*{{{*/
  170. // ---------------------------------------------------------------------
  171. /* This should help the user find the index in the sources.list and
  172. in the filesystem for problem solving */
  173. string debPackagesIndex::Describe(bool Short) const
  174. {
  175. char S[300];
  176. if (Short == true)
  177. snprintf(S,sizeof(S),"%s",Info("Packages").c_str());
  178. else
  179. snprintf(S,sizeof(S),"%s (%s)",Info("Packages").c_str(),
  180. IndexFile("Packages").c_str());
  181. return S;
  182. }
  183. /*}}}*/
  184. // PackagesIndex::Info - One liner describing the index URI /*{{{*/
  185. // ---------------------------------------------------------------------
  186. /* */
  187. string debPackagesIndex::Info(const char *Type) const
  188. {
  189. string Info = ::URI::SiteOnly(URI) + ' ';
  190. if (Dist[Dist.size() - 1] == '/')
  191. {
  192. if (Dist != "/")
  193. Info += Dist;
  194. }
  195. else
  196. Info += Dist + '/' + Section;
  197. Info += " ";
  198. Info += Type;
  199. return Info;
  200. }
  201. /*}}}*/
  202. // PackagesIndex::Index* - Return the URI to the index files /*{{{*/
  203. // ---------------------------------------------------------------------
  204. /* */
  205. inline string debPackagesIndex::IndexFile(const char *Type) const
  206. {
  207. return _config->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type));
  208. }
  209. string debPackagesIndex::IndexURI(const char *Type) const
  210. {
  211. string Res;
  212. if (Dist[Dist.size() - 1] == '/')
  213. {
  214. if (Dist != "/")
  215. Res = URI + Dist;
  216. else
  217. Res = URI;
  218. }
  219. else
  220. Res = URI + "dists/" + Dist + '/' + Section +
  221. "/binary-" + _config->Find("APT::Architecture") + '/';
  222. Res += Type;
  223. return Res;
  224. }
  225. /*}}}*/
  226. // PackagesIndex::Exists - Check if the index is available /*{{{*/
  227. // ---------------------------------------------------------------------
  228. /* */
  229. bool debPackagesIndex::Exists() const
  230. {
  231. return FileExists(IndexFile("Packages"));
  232. }
  233. /*}}}*/
  234. // PackagesIndex::Size - Return the size of the index /*{{{*/
  235. // ---------------------------------------------------------------------
  236. /* This is really only used for progress reporting. */
  237. unsigned long debPackagesIndex::Size() const
  238. {
  239. struct stat S;
  240. if (stat(IndexFile("Packages").c_str(),&S) != 0)
  241. return 0;
  242. return S.st_size;
  243. }
  244. /*}}}*/
  245. // PackagesIndex::Merge - Load the index file into a cache /*{{{*/
  246. // ---------------------------------------------------------------------
  247. /* */
  248. bool debPackagesIndex::Merge(pkgCacheGenerator &Gen,OpProgress &Prog) const
  249. {
  250. string PackageFile = IndexFile("Packages");
  251. FileFd Pkg(PackageFile,FileFd::ReadOnly);
  252. debListParser Parser(&Pkg);
  253. if (_error->PendingError() == true)
  254. return _error->Error("Problem opening %s",PackageFile.c_str());
  255. Prog.SubProgress(0,Info("Packages"));
  256. ::URI Tmp(URI);
  257. if (Gen.SelectFile(PackageFile,Tmp.Host,*this) == false)
  258. return _error->Error("Problem with SelectFile %s",PackageFile.c_str());
  259. // Store the IMS information
  260. pkgCache::PkgFileIterator File = Gen.GetCurFile();
  261. struct stat St;
  262. if (fstat(Pkg.Fd(),&St) != 0)
  263. return _error->Errno("fstat","Failed to stat");
  264. File->Size = St.st_size;
  265. File->mtime = St.st_mtime;
  266. if (Gen.MergeList(Parser) == false)
  267. return _error->Error("Problem with MergeList %s",PackageFile.c_str());
  268. // Check the release file
  269. string ReleaseFile = debReleaseIndex(URI,Dist).MetaIndexFile("Release");
  270. if (FileExists(ReleaseFile) == true)
  271. {
  272. FileFd Rel(ReleaseFile,FileFd::ReadOnly);
  273. if (_error->PendingError() == true)
  274. return false;
  275. Parser.LoadReleaseInfo(File,Rel,Section);
  276. }
  277. return true;
  278. }
  279. /*}}}*/
  280. // PackagesIndex::FindInCache - Find this index /*{{{*/
  281. // ---------------------------------------------------------------------
  282. /* */
  283. pkgCache::PkgFileIterator debPackagesIndex::FindInCache(pkgCache &Cache) const
  284. {
  285. string FileName = IndexFile("Packages");
  286. pkgCache::PkgFileIterator File = Cache.FileBegin();
  287. for (; File.end() == false; File++)
  288. {
  289. if (File.FileName() == NULL || FileName != File.FileName())
  290. continue;
  291. struct stat St;
  292. if (stat(File.FileName(),&St) != 0)
  293. return pkgCache::PkgFileIterator(Cache);
  294. if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
  295. return pkgCache::PkgFileIterator(Cache);
  296. return File;
  297. }
  298. return File;
  299. }
  300. /*}}}*/
  301. // TranslationsIndex::debTranslationsIndex - Contructor /*{{{*/
  302. // ---------------------------------------------------------------------
  303. /* */
  304. debTranslationsIndex::debTranslationsIndex(string URI,string Dist,string Section,
  305. char const * const Translation) :
  306. pkgIndexFile(true), URI(URI), Dist(Dist), Section(Section),
  307. Language(Translation)
  308. {}
  309. /*}}}*/
  310. // TranslationIndex::Trans* - Return the URI to the translation files /*{{{*/
  311. // ---------------------------------------------------------------------
  312. /* */
  313. inline string debTranslationsIndex::IndexFile(const char *Type) const
  314. {
  315. return _config->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type));
  316. }
  317. string debTranslationsIndex::IndexURI(const char *Type) const
  318. {
  319. string Res;
  320. if (Dist[Dist.size() - 1] == '/')
  321. {
  322. if (Dist != "/")
  323. Res = URI + Dist;
  324. else
  325. Res = URI;
  326. }
  327. else
  328. Res = URI + "dists/" + Dist + '/' + Section +
  329. "/i18n/Translation-";
  330. Res += Type;
  331. return Res;
  332. }
  333. /*}}}*/
  334. // TranslationsIndex::GetIndexes - Fetch the index files /*{{{*/
  335. // ---------------------------------------------------------------------
  336. /* */
  337. bool debTranslationsIndex::GetIndexes(pkgAcquire *Owner) const
  338. {
  339. if (TranslationsAvailable()) {
  340. string const TranslationFile = string("Translation-").append(Language);
  341. new pkgAcqIndexTrans(Owner, IndexURI(Language),
  342. Info(TranslationFile.c_str()),
  343. TranslationFile);
  344. }
  345. return true;
  346. }
  347. /*}}}*/
  348. // TranslationsIndex::Describe - Give a descriptive path to the index /*{{{*/
  349. // ---------------------------------------------------------------------
  350. /* This should help the user find the index in the sources.list and
  351. in the filesystem for problem solving */
  352. string debTranslationsIndex::Describe(bool Short) const
  353. {
  354. char S[300];
  355. if (Short == true)
  356. snprintf(S,sizeof(S),"%s",Info(TranslationFile().c_str()).c_str());
  357. else
  358. snprintf(S,sizeof(S),"%s (%s)",Info(TranslationFile().c_str()).c_str(),
  359. IndexFile(Language).c_str());
  360. return S;
  361. }
  362. /*}}}*/
  363. // TranslationsIndex::Info - One liner describing the index URI /*{{{*/
  364. // ---------------------------------------------------------------------
  365. /* */
  366. string debTranslationsIndex::Info(const char *Type) const
  367. {
  368. string Info = ::URI::SiteOnly(URI) + ' ';
  369. if (Dist[Dist.size() - 1] == '/')
  370. {
  371. if (Dist != "/")
  372. Info += Dist;
  373. }
  374. else
  375. Info += Dist + '/' + Section;
  376. Info += " ";
  377. Info += Type;
  378. return Info;
  379. }
  380. /*}}}*/
  381. bool debTranslationsIndex::HasPackages() const /*{{{*/
  382. {
  383. if(!TranslationsAvailable())
  384. return false;
  385. return FileExists(IndexFile(Language));
  386. }
  387. /*}}}*/
  388. // TranslationsIndex::Exists - Check if the index is available /*{{{*/
  389. // ---------------------------------------------------------------------
  390. /* */
  391. bool debTranslationsIndex::Exists() const
  392. {
  393. return FileExists(IndexFile(Language));
  394. }
  395. /*}}}*/
  396. // TranslationsIndex::Size - Return the size of the index /*{{{*/
  397. // ---------------------------------------------------------------------
  398. /* This is really only used for progress reporting. */
  399. unsigned long debTranslationsIndex::Size() const
  400. {
  401. struct stat S;
  402. if (stat(IndexFile(Language).c_str(),&S) != 0)
  403. return 0;
  404. return S.st_size;
  405. }
  406. /*}}}*/
  407. // TranslationsIndex::Merge - Load the index file into a cache /*{{{*/
  408. // ---------------------------------------------------------------------
  409. /* */
  410. bool debTranslationsIndex::Merge(pkgCacheGenerator &Gen,OpProgress &Prog) const
  411. {
  412. // Check the translation file, if in use
  413. string TranslationFile = IndexFile(Language);
  414. if (TranslationsAvailable() && FileExists(TranslationFile))
  415. {
  416. FileFd Trans(TranslationFile,FileFd::ReadOnly);
  417. debListParser TransParser(&Trans);
  418. if (_error->PendingError() == true)
  419. return false;
  420. Prog.SubProgress(0, Info(TranslationFile.c_str()));
  421. if (Gen.SelectFile(TranslationFile,string(),*this) == false)
  422. return _error->Error("Problem with SelectFile %s",TranslationFile.c_str());
  423. // Store the IMS information
  424. pkgCache::PkgFileIterator TransFile = Gen.GetCurFile();
  425. struct stat TransSt;
  426. if (fstat(Trans.Fd(),&TransSt) != 0)
  427. return _error->Errno("fstat","Failed to stat");
  428. TransFile->Size = TransSt.st_size;
  429. TransFile->mtime = TransSt.st_mtime;
  430. if (Gen.MergeList(TransParser) == false)
  431. return _error->Error("Problem with MergeList %s",TranslationFile.c_str());
  432. }
  433. return true;
  434. }
  435. /*}}}*/
  436. // TranslationsIndex::FindInCache - Find this index /*{{{*/
  437. // ---------------------------------------------------------------------
  438. /* */
  439. pkgCache::PkgFileIterator debTranslationsIndex::FindInCache(pkgCache &Cache) const
  440. {
  441. string FileName = IndexFile(Language);
  442. pkgCache::PkgFileIterator File = Cache.FileBegin();
  443. for (; File.end() == false; File++)
  444. {
  445. if (FileName != File.FileName())
  446. continue;
  447. struct stat St;
  448. if (stat(File.FileName(),&St) != 0)
  449. return pkgCache::PkgFileIterator(Cache);
  450. if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
  451. return pkgCache::PkgFileIterator(Cache);
  452. return File;
  453. }
  454. return File;
  455. }
  456. /*}}}*/
  457. // StatusIndex::debStatusIndex - Constructor /*{{{*/
  458. // ---------------------------------------------------------------------
  459. /* */
  460. debStatusIndex::debStatusIndex(string File) : pkgIndexFile(true), File(File)
  461. {
  462. }
  463. /*}}}*/
  464. // StatusIndex::Size - Return the size of the index /*{{{*/
  465. // ---------------------------------------------------------------------
  466. /* */
  467. unsigned long debStatusIndex::Size() const
  468. {
  469. struct stat S;
  470. if (stat(File.c_str(),&S) != 0)
  471. return 0;
  472. return S.st_size;
  473. }
  474. /*}}}*/
  475. // StatusIndex::Merge - Load the index file into a cache /*{{{*/
  476. // ---------------------------------------------------------------------
  477. /* */
  478. bool debStatusIndex::Merge(pkgCacheGenerator &Gen,OpProgress &Prog) const
  479. {
  480. FileFd Pkg(File,FileFd::ReadOnly);
  481. if (_error->PendingError() == true)
  482. return false;
  483. debListParser Parser(&Pkg);
  484. if (_error->PendingError() == true)
  485. return false;
  486. Prog.SubProgress(0,File);
  487. if (Gen.SelectFile(File,string(),*this,pkgCache::Flag::NotSource) == false)
  488. return _error->Error("Problem with SelectFile %s",File.c_str());
  489. // Store the IMS information
  490. pkgCache::PkgFileIterator CFile = Gen.GetCurFile();
  491. struct stat St;
  492. if (fstat(Pkg.Fd(),&St) != 0)
  493. return _error->Errno("fstat","Failed to stat");
  494. CFile->Size = St.st_size;
  495. CFile->mtime = St.st_mtime;
  496. CFile->Archive = Gen.WriteUniqString("now");
  497. if (Gen.MergeList(Parser) == false)
  498. return _error->Error("Problem with MergeList %s",File.c_str());
  499. return true;
  500. }
  501. /*}}}*/
  502. // StatusIndex::FindInCache - Find this index /*{{{*/
  503. // ---------------------------------------------------------------------
  504. /* */
  505. pkgCache::PkgFileIterator debStatusIndex::FindInCache(pkgCache &Cache) const
  506. {
  507. pkgCache::PkgFileIterator File = Cache.FileBegin();
  508. for (; File.end() == false; File++)
  509. {
  510. if (this->File != File.FileName())
  511. continue;
  512. struct stat St;
  513. if (stat(File.FileName(),&St) != 0)
  514. return pkgCache::PkgFileIterator(Cache);
  515. if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
  516. return pkgCache::PkgFileIterator(Cache);
  517. return File;
  518. }
  519. return File;
  520. }
  521. /*}}}*/
  522. // StatusIndex::Exists - Check if the index is available /*{{{*/
  523. // ---------------------------------------------------------------------
  524. /* */
  525. bool debStatusIndex::Exists() const
  526. {
  527. // Abort if the file does not exist.
  528. return true;
  529. }
  530. /*}}}*/
  531. // Index File types for Debian /*{{{*/
  532. class debIFTypeSrc : public pkgIndexFile::Type
  533. {
  534. public:
  535. debIFTypeSrc() {Label = "Debian Source Index";};
  536. };
  537. class debIFTypePkg : public pkgIndexFile::Type
  538. {
  539. public:
  540. virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator File) const
  541. {
  542. return new debRecordParser(File.FileName(),*File.Cache());
  543. };
  544. debIFTypePkg() {Label = "Debian Package Index";};
  545. };
  546. class debIFTypeTrans : public debIFTypePkg
  547. {
  548. public:
  549. debIFTypeTrans() {Label = "Debian Translation Index";};
  550. };
  551. class debIFTypeStatus : public pkgIndexFile::Type
  552. {
  553. public:
  554. virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator File) const
  555. {
  556. return new debRecordParser(File.FileName(),*File.Cache());
  557. };
  558. debIFTypeStatus() {Label = "Debian dpkg status file";};
  559. };
  560. static debIFTypeSrc _apt_Src;
  561. static debIFTypePkg _apt_Pkg;
  562. static debIFTypeTrans _apt_Trans;
  563. static debIFTypeStatus _apt_Status;
  564. const pkgIndexFile::Type *debSourcesIndex::GetType() const
  565. {
  566. return &_apt_Src;
  567. }
  568. const pkgIndexFile::Type *debPackagesIndex::GetType() const
  569. {
  570. return &_apt_Pkg;
  571. }
  572. const pkgIndexFile::Type *debTranslationsIndex::GetType() const
  573. {
  574. return &_apt_Trans;
  575. }
  576. const pkgIndexFile::Type *debStatusIndex::GetType() const
  577. {
  578. return &_apt_Status;
  579. }
  580. /*}}}*/