debmetaindex.cc 15 KB

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