debmetaindex.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. #include <config.h>
  2. #include <apt-pkg/debmetaindex.h>
  3. #include <apt-pkg/debindexfile.h>
  4. #include <apt-pkg/strutl.h>
  5. #include <apt-pkg/fileutl.h>
  6. #include <apt-pkg/acquire-item.h>
  7. #include <apt-pkg/configuration.h>
  8. #include <apt-pkg/aptconfiguration.h>
  9. #include <apt-pkg/indexrecords.h>
  10. #include <apt-pkg/sourcelist.h>
  11. #include <apt-pkg/hashes.h>
  12. #include <apt-pkg/macros.h>
  13. #include <apt-pkg/metaindex.h>
  14. #include <string.h>
  15. #include <map>
  16. #include <string>
  17. #include <utility>
  18. #include <vector>
  19. #include <set>
  20. #include <algorithm>
  21. using namespace std;
  22. string debReleaseIndex::Info(const char *Type, string const &Section, string const &Arch) const
  23. {
  24. string Info = ::URI::SiteOnly(URI) + ' ';
  25. if (Dist[Dist.size() - 1] == '/')
  26. {
  27. if (Dist != "/")
  28. Info += Dist;
  29. }
  30. else
  31. {
  32. Info += Dist + '/' + Section;
  33. if (Arch.empty() != true)
  34. Info += " " + Arch;
  35. }
  36. Info += " ";
  37. Info += Type;
  38. return Info;
  39. }
  40. string debReleaseIndex::MetaIndexInfo(const char *Type) const
  41. {
  42. string Info = ::URI::SiteOnly(URI) + ' ';
  43. if (Dist[Dist.size() - 1] == '/')
  44. {
  45. if (Dist != "/")
  46. Info += Dist;
  47. }
  48. else
  49. Info += Dist;
  50. Info += " ";
  51. Info += Type;
  52. return Info;
  53. }
  54. string debReleaseIndex::MetaIndexFile(const char *Type) const
  55. {
  56. return _config->FindDir("Dir::State::lists") +
  57. URItoFileName(MetaIndexURI(Type));
  58. }
  59. string debReleaseIndex::MetaIndexURI(const char *Type) const
  60. {
  61. string Res;
  62. if (Dist == "/")
  63. Res = URI;
  64. else if (Dist[Dist.size()-1] == '/')
  65. Res = URI + Dist;
  66. else
  67. Res = URI + "dists/" + Dist + "/";
  68. Res += Type;
  69. return Res;
  70. }
  71. std::string debReleaseIndex::LocalFileName() const
  72. {
  73. // see if we have a InRelease file
  74. std::string PathInRelease = MetaIndexFile("InRelease");
  75. if (FileExists(PathInRelease))
  76. return PathInRelease;
  77. // and if not return the normal one
  78. if (FileExists(PathInRelease))
  79. return MetaIndexFile("Release");
  80. return "";
  81. }
  82. string debReleaseIndex::IndexURISuffix(const char *Type, string const &Section, string const &Arch) const
  83. {
  84. string Res ="";
  85. if (Dist[Dist.size() - 1] != '/')
  86. {
  87. if (Arch == "native")
  88. Res += Section + "/binary-" + _config->Find("APT::Architecture") + '/';
  89. else
  90. Res += Section + "/binary-" + Arch + '/';
  91. }
  92. return Res + Type;
  93. }
  94. string debReleaseIndex::IndexURI(const char *Type, string const &Section, string const &Arch) const
  95. {
  96. if (Dist[Dist.size() - 1] == '/')
  97. {
  98. string Res;
  99. if (Dist != "/")
  100. Res = URI + Dist;
  101. else
  102. Res = URI;
  103. return Res + Type;
  104. }
  105. else
  106. return URI + "dists/" + Dist + '/' + IndexURISuffix(Type, Section, Arch);
  107. }
  108. string debReleaseIndex::SourceIndexURISuffix(const char *Type, const string &Section) const
  109. {
  110. string Res ="";
  111. if (Dist[Dist.size() - 1] != '/')
  112. Res += Section + "/source/";
  113. return Res + Type;
  114. }
  115. string debReleaseIndex::SourceIndexURI(const char *Type, const string &Section) const
  116. {
  117. string Res;
  118. if (Dist[Dist.size() - 1] == '/')
  119. {
  120. if (Dist != "/")
  121. Res = URI + Dist;
  122. else
  123. Res = URI;
  124. return Res + Type;
  125. }
  126. else
  127. return URI + "dists/" + Dist + "/" + SourceIndexURISuffix(Type, Section);
  128. }
  129. string debReleaseIndex::TranslationIndexURISuffix(const char *Type, const string &Section) const
  130. {
  131. string Res ="";
  132. if (Dist[Dist.size() - 1] != '/')
  133. Res += Section + "/i18n/Translation-";
  134. return Res + Type;
  135. }
  136. string debReleaseIndex::TranslationIndexURI(const char *Type, const string &Section) const
  137. {
  138. string Res;
  139. if (Dist[Dist.size() - 1] == '/')
  140. {
  141. if (Dist != "/")
  142. Res = URI + Dist;
  143. else
  144. Res = URI;
  145. return Res + Type;
  146. }
  147. else
  148. return URI + "dists/" + Dist + "/" + TranslationIndexURISuffix(Type, Section);
  149. }
  150. debReleaseIndex::debReleaseIndex(string const &URI, string const &Dist) :
  151. metaIndex(URI, Dist, "deb"), Trusted(CHECK_TRUST)
  152. {}
  153. debReleaseIndex::debReleaseIndex(string const &URI, string const &Dist, bool const Trusted) :
  154. metaIndex(URI, Dist, "deb") {
  155. SetTrusted(Trusted);
  156. }
  157. debReleaseIndex::~debReleaseIndex() {
  158. for (map<string, vector<debSectionEntry const*> >::const_iterator A = ArchEntries.begin();
  159. A != ArchEntries.end(); ++A)
  160. for (vector<const debSectionEntry *>::const_iterator S = A->second.begin();
  161. S != A->second.end(); ++S)
  162. delete *S;
  163. }
  164. vector <IndexTarget *>* debReleaseIndex::ComputeIndexTargets() const {
  165. vector <IndexTarget *>* IndexTargets = new vector <IndexTarget *>;
  166. map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
  167. if (src != ArchEntries.end()) {
  168. vector<debSectionEntry const*> const SectionEntries = src->second;
  169. for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
  170. I != SectionEntries.end(); ++I) {
  171. IndexTarget * Target = new IndexTarget();
  172. Target->ShortDesc = "Sources";
  173. Target->MetaKey = SourceIndexURISuffix(Target->ShortDesc.c_str(), (*I)->Section);
  174. Target->URI = SourceIndexURI(Target->ShortDesc.c_str(), (*I)->Section);
  175. Target->Description = Info (Target->ShortDesc.c_str(), (*I)->Section);
  176. IndexTargets->push_back (Target);
  177. }
  178. }
  179. // Only source release
  180. if (IndexTargets->empty() == false && ArchEntries.size() == 1)
  181. return IndexTargets;
  182. std::set<std::string> sections;
  183. for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
  184. a != ArchEntries.end(); ++a) {
  185. if (a->first == "source")
  186. continue;
  187. for (vector <const debSectionEntry *>::const_iterator I = a->second.begin();
  188. I != a->second.end(); ++I) {
  189. IndexTarget * Target = new IndexTarget();
  190. Target->ShortDesc = "Packages";
  191. Target->MetaKey = IndexURISuffix(Target->ShortDesc.c_str(), (*I)->Section, a->first);
  192. Target->URI = IndexURI(Target->ShortDesc.c_str(), (*I)->Section, a->first);
  193. Target->Description = Info (Target->ShortDesc.c_str(), (*I)->Section, a->first);
  194. IndexTargets->push_back (Target);
  195. sections.insert((*I)->Section);
  196. }
  197. }
  198. std::vector<std::string> lang = APT::Configuration::getLanguages(true);
  199. std::vector<std::string>::iterator lend = std::remove(lang.begin(), lang.end(), "none");
  200. if (lend != lang.end())
  201. lang.erase(lend);
  202. if (lang.empty() == true)
  203. return IndexTargets;
  204. // get the Translation-* files, later we will skip download of non-existent if we have an index
  205. for (std::set<std::string>::const_iterator s = sections.begin();
  206. s != sections.end(); ++s) {
  207. for (std::vector<std::string>::const_iterator l = lang.begin();
  208. l != lang.end(); ++l) {
  209. IndexTarget * Target = new OptionalIndexTarget();
  210. Target->ShortDesc = "Translation-" + *l;
  211. Target->MetaKey = TranslationIndexURISuffix(l->c_str(), *s);
  212. Target->URI = TranslationIndexURI(l->c_str(), *s);
  213. Target->Description = Info (Target->ShortDesc.c_str(), *s);
  214. IndexTargets->push_back(Target);
  215. }
  216. }
  217. return IndexTargets;
  218. }
  219. /*}}}*/
  220. bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool const &GetAll) const
  221. {
  222. bool const tryInRelease = _config->FindB("Acquire::TryInRelease", true);
  223. indexRecords * const iR = new indexRecords(Dist);
  224. if (Trusted == ALWAYS_TRUSTED)
  225. iR->SetTrusted(true);
  226. else if (Trusted == NEVER_TRUSTED)
  227. iR->SetTrusted(false);
  228. // special case for --print-uris
  229. if (GetAll) {
  230. vector <IndexTarget *> *targets = ComputeIndexTargets();
  231. for (vector <IndexTarget*>::const_iterator Target = targets->begin(); Target != targets->end(); ++Target) {
  232. new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
  233. (*Target)->ShortDesc, HashStringList());
  234. }
  235. delete targets;
  236. // this is normally created in pkgAcqMetaSig, but if we run
  237. // in --print-uris mode, we add it here
  238. if (tryInRelease == false)
  239. new pkgAcqMetaIndex(Owner, NULL,
  240. MetaIndexURI("Release"),
  241. MetaIndexInfo("Release"), "Release",
  242. MetaIndexURI("Release.gpg"), MetaIndexInfo("Release.gpg"), "Release.gpg",
  243. ComputeIndexTargets(),
  244. iR);
  245. }
  246. if (tryInRelease == true)
  247. new pkgAcqMetaClearSig(Owner,
  248. MetaIndexURI("InRelease"), MetaIndexInfo("InRelease"), "InRelease",
  249. MetaIndexURI("Release"), MetaIndexInfo("Release"), "Release",
  250. MetaIndexURI("Release.gpg"), MetaIndexInfo("Release.gpg"), "Release.gpg",
  251. ComputeIndexTargets(),
  252. iR);
  253. else
  254. new pkgAcqMetaIndex(Owner, NULL,
  255. MetaIndexURI("Release"), MetaIndexInfo("Release"), "Release",
  256. MetaIndexURI("Release.gpg"), MetaIndexInfo("Release.gpg"), "Release.gpg",
  257. ComputeIndexTargets(),
  258. iR);
  259. return true;
  260. }
  261. void debReleaseIndex::SetTrusted(bool const Trusted)
  262. {
  263. if (Trusted == true)
  264. this->Trusted = ALWAYS_TRUSTED;
  265. else
  266. this->Trusted = NEVER_TRUSTED;
  267. }
  268. bool debReleaseIndex::IsTrusted() const
  269. {
  270. if (Trusted == ALWAYS_TRUSTED)
  271. return true;
  272. else if (Trusted == NEVER_TRUSTED)
  273. return false;
  274. if(_config->FindB("APT::Authentication::TrustCDROM", false))
  275. if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
  276. return true;
  277. string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
  278. URItoFileName(MetaIndexURI("Release")) + ".gpg";
  279. if (FileExists(VerifiedSigFile))
  280. return true;
  281. VerifiedSigFile = _config->FindDir("Dir::State::lists") +
  282. URItoFileName(MetaIndexURI("InRelease"));
  283. return FileExists(VerifiedSigFile);
  284. }
  285. vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles() {
  286. if (Indexes != NULL)
  287. return Indexes;
  288. Indexes = new vector <pkgIndexFile*>;
  289. map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
  290. if (src != ArchEntries.end()) {
  291. vector<debSectionEntry const*> const SectionEntries = src->second;
  292. for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
  293. I != SectionEntries.end(); ++I)
  294. Indexes->push_back(new debSourcesIndex (URI, Dist, (*I)->Section, IsTrusted()));
  295. }
  296. // Only source release
  297. if (Indexes->empty() == false && ArchEntries.size() == 1)
  298. return Indexes;
  299. std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
  300. map<string, set<string> > sections;
  301. for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
  302. a != ArchEntries.end(); ++a) {
  303. if (a->first == "source")
  304. continue;
  305. for (vector<debSectionEntry const*>::const_iterator I = a->second.begin();
  306. I != a->second.end(); ++I) {
  307. Indexes->push_back(new debPackagesIndex (URI, Dist, (*I)->Section, IsTrusted(), a->first));
  308. sections[(*I)->Section].insert(lang.begin(), lang.end());
  309. }
  310. }
  311. for (map<string, set<string> >::const_iterator s = sections.begin();
  312. s != sections.end(); ++s)
  313. for (set<string>::const_iterator l = s->second.begin();
  314. l != s->second.end(); ++l) {
  315. if (*l == "none") continue;
  316. Indexes->push_back(new debTranslationsIndex(URI,Dist,s->first,(*l).c_str()));
  317. }
  318. return Indexes;
  319. }
  320. void debReleaseIndex::PushSectionEntry(vector<string> const &Archs, const debSectionEntry *Entry) {
  321. for (vector<string>::const_iterator a = Archs.begin();
  322. a != Archs.end(); ++a)
  323. ArchEntries[*a].push_back(new debSectionEntry(Entry->Section, Entry->IsSrc));
  324. delete Entry;
  325. }
  326. void debReleaseIndex::PushSectionEntry(string const &Arch, const debSectionEntry *Entry) {
  327. ArchEntries[Arch].push_back(Entry);
  328. }
  329. void debReleaseIndex::PushSectionEntry(const debSectionEntry *Entry) {
  330. if (Entry->IsSrc == true)
  331. PushSectionEntry("source", Entry);
  332. else {
  333. for (map<string, vector<const debSectionEntry *> >::iterator a = ArchEntries.begin();
  334. a != ArchEntries.end(); ++a) {
  335. a->second.push_back(Entry);
  336. }
  337. }
  338. }
  339. debReleaseIndex::debSectionEntry::debSectionEntry (string const &Section,
  340. bool const &IsSrc): Section(Section), IsSrc(IsSrc)
  341. {}
  342. class APT_HIDDEN debSLTypeDebian : public pkgSourceList::Type
  343. {
  344. protected:
  345. bool CreateItemInternal(vector<metaIndex *> &List, string const &URI,
  346. string const &Dist, string const &Section,
  347. bool const &IsSrc, map<string, string> const &Options) const
  348. {
  349. // parse arch=, arch+= and arch-= settings
  350. map<string, string>::const_iterator arch = Options.find("arch");
  351. vector<string> Archs =
  352. (arch != Options.end()) ? VectorizeString(arch->second, ',') :
  353. APT::Configuration::getArchitectures();
  354. if ((arch = Options.find("arch+")) != Options.end())
  355. {
  356. std::vector<std::string> const plusArch = VectorizeString(arch->second, ',');
  357. for (std::vector<std::string>::const_iterator plus = plusArch.begin(); plus != plusArch.end(); ++plus)
  358. if (std::find(Archs.begin(), Archs.end(), *plus) == Archs.end())
  359. Archs.push_back(*plus);
  360. }
  361. if ((arch = Options.find("arch-")) != Options.end())
  362. {
  363. std::vector<std::string> const minusArch = VectorizeString(arch->second, ',');
  364. for (std::vector<std::string>::const_iterator minus = minusArch.begin(); minus != minusArch.end(); ++minus)
  365. {
  366. std::vector<std::string>::iterator kill = std::find(Archs.begin(), Archs.end(), *minus);
  367. if (kill != Archs.end())
  368. Archs.erase(kill);
  369. }
  370. }
  371. map<string, string>::const_iterator const trusted = Options.find("trusted");
  372. for (vector<metaIndex *>::const_iterator I = List.begin();
  373. I != List.end(); ++I)
  374. {
  375. // We only worry about debian entries here
  376. if (strcmp((*I)->GetType(), "deb") != 0)
  377. continue;
  378. debReleaseIndex *Deb = (debReleaseIndex *) (*I);
  379. if (trusted != Options.end())
  380. Deb->SetTrusted(StringToBool(trusted->second, false));
  381. /* This check insures that there will be only one Release file
  382. queued for all the Packages files and Sources files it
  383. corresponds to. */
  384. if (Deb->GetURI() == URI && Deb->GetDist() == Dist)
  385. {
  386. if (IsSrc == true)
  387. Deb->PushSectionEntry("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  388. else
  389. {
  390. if (Dist[Dist.size() - 1] == '/')
  391. Deb->PushSectionEntry("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  392. else
  393. Deb->PushSectionEntry(Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
  394. }
  395. return true;
  396. }
  397. }
  398. // No currently created Release file indexes this entry, so we create a new one.
  399. debReleaseIndex *Deb;
  400. if (trusted != Options.end())
  401. Deb = new debReleaseIndex(URI, Dist, StringToBool(trusted->second, false));
  402. else
  403. Deb = new debReleaseIndex(URI, Dist);
  404. if (IsSrc == true)
  405. Deb->PushSectionEntry ("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  406. else
  407. {
  408. if (Dist[Dist.size() - 1] == '/')
  409. Deb->PushSectionEntry ("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  410. else
  411. Deb->PushSectionEntry (Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
  412. }
  413. List.push_back(Deb);
  414. return true;
  415. }
  416. };
  417. debDebFileMetaIndex::debDebFileMetaIndex(std::string const &DebFile)
  418. : metaIndex(DebFile, "local-uri", "deb-dist"), DebFile(DebFile)
  419. {
  420. DebIndex = new debDebPkgFileIndex(DebFile);
  421. Indexes = new vector<pkgIndexFile *>();
  422. Indexes->push_back(DebIndex);
  423. }
  424. class APT_HIDDEN debSLTypeDeb : public debSLTypeDebian
  425. {
  426. public:
  427. bool CreateItem(vector<metaIndex *> &List, string const &URI,
  428. string const &Dist, string const &Section,
  429. std::map<string, string> const &Options) const
  430. {
  431. return CreateItemInternal(List, URI, Dist, Section, false, Options);
  432. }
  433. debSLTypeDeb()
  434. {
  435. Name = "deb";
  436. Label = "Standard Debian binary tree";
  437. }
  438. };
  439. class APT_HIDDEN debSLTypeDebSrc : public debSLTypeDebian
  440. {
  441. public:
  442. bool CreateItem(vector<metaIndex *> &List, string const &URI,
  443. string const &Dist, string const &Section,
  444. std::map<string, string> const &Options) const
  445. {
  446. return CreateItemInternal(List, URI, Dist, Section, true, Options);
  447. }
  448. debSLTypeDebSrc()
  449. {
  450. Name = "deb-src";
  451. Label = "Standard Debian source tree";
  452. }
  453. };
  454. class APT_HIDDEN debSLTypeDebFile : public pkgSourceList::Type
  455. {
  456. public:
  457. bool CreateItem(vector<metaIndex *> &List, string const &URI,
  458. string const &/*Dist*/, string const &/*Section*/,
  459. std::map<string, string> const &/*Options*/) const
  460. {
  461. metaIndex *mi = new debDebFileMetaIndex(URI);
  462. List.push_back(mi);
  463. return true;
  464. }
  465. debSLTypeDebFile()
  466. {
  467. Name = "deb-file";
  468. Label = "Debian Deb File";
  469. }
  470. };
  471. APT_HIDDEN debSLTypeDeb _apt_DebType;
  472. APT_HIDDEN debSLTypeDebSrc _apt_DebSrcType;
  473. APT_HIDDEN debSLTypeDebFile _apt_DebFileType;