debmetaindex.cc 15 KB

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