debmetaindex.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. 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. string debReleaseIndex::IndexURISuffix(const char *Type, string const &Section, string const &Arch) const
  65. {
  66. string Res ="";
  67. if (Dist[Dist.size() - 1] != '/')
  68. {
  69. if (Arch == "native")
  70. Res += Section + "/binary-" + _config->Find("APT::Architecture") + '/';
  71. else
  72. Res += Section + "/binary-" + Arch + '/';
  73. }
  74. return Res + Type;
  75. }
  76. string debReleaseIndex::IndexURI(const char *Type, string const &Section, string const &Arch) const
  77. {
  78. if (Dist[Dist.size() - 1] == '/')
  79. {
  80. string Res;
  81. if (Dist != "/")
  82. Res = URI + Dist;
  83. else
  84. Res = URI;
  85. return Res + Type;
  86. }
  87. else
  88. return URI + "dists/" + Dist + '/' + IndexURISuffix(Type, Section, Arch);
  89. }
  90. string debReleaseIndex::SourceIndexURISuffix(const char *Type, const string &Section) const
  91. {
  92. string Res ="";
  93. if (Dist[Dist.size() - 1] != '/')
  94. Res += Section + "/source/";
  95. return Res + Type;
  96. }
  97. string debReleaseIndex::SourceIndexURI(const char *Type, const string &Section) const
  98. {
  99. string Res;
  100. if (Dist[Dist.size() - 1] == '/')
  101. {
  102. if (Dist != "/")
  103. Res = URI + Dist;
  104. else
  105. Res = URI;
  106. return Res + Type;
  107. }
  108. else
  109. return URI + "dists/" + Dist + "/" + SourceIndexURISuffix(Type, Section);
  110. }
  111. string debReleaseIndex::TranslationIndexURISuffix(const char *Type, const string &Section) const
  112. {
  113. string Res ="";
  114. if (Dist[Dist.size() - 1] != '/')
  115. Res += Section + "/i18n/";
  116. return Res + Type;
  117. }
  118. string debReleaseIndex::TranslationIndexURI(const char *Type, const string &Section) const
  119. {
  120. string Res;
  121. if (Dist[Dist.size() - 1] == '/')
  122. {
  123. if (Dist != "/")
  124. Res = URI + Dist;
  125. else
  126. Res = URI;
  127. return Res + Type;
  128. }
  129. else
  130. return URI + "dists/" + Dist + "/" + TranslationIndexURISuffix(Type, Section);
  131. }
  132. debReleaseIndex::debReleaseIndex(string const &URI, string const &Dist) :
  133. metaIndex(URI, Dist, "deb"), Trusted(CHECK_TRUST)
  134. {}
  135. debReleaseIndex::debReleaseIndex(string const &URI, string const &Dist, bool const Trusted) :
  136. metaIndex(URI, Dist, "deb") {
  137. SetTrusted(Trusted);
  138. }
  139. debReleaseIndex::~debReleaseIndex() {
  140. for (map<string, vector<debSectionEntry const*> >::const_iterator A = ArchEntries.begin();
  141. A != ArchEntries.end(); ++A)
  142. for (vector<const debSectionEntry *>::const_iterator S = A->second.begin();
  143. S != A->second.end(); ++S)
  144. delete *S;
  145. }
  146. vector <struct IndexTarget *>* debReleaseIndex::ComputeIndexTargets() const {
  147. vector <struct IndexTarget *>* IndexTargets = new vector <IndexTarget *>;
  148. map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
  149. if (src != ArchEntries.end()) {
  150. vector<debSectionEntry const*> const SectionEntries = src->second;
  151. for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
  152. I != SectionEntries.end(); ++I) {
  153. IndexTarget * Target = new IndexTarget();
  154. Target->ShortDesc = "Sources";
  155. Target->MetaKey = SourceIndexURISuffix(Target->ShortDesc.c_str(), (*I)->Section);
  156. Target->URI = SourceIndexURI(Target->ShortDesc.c_str(), (*I)->Section);
  157. Target->Description = Info (Target->ShortDesc.c_str(), (*I)->Section);
  158. IndexTargets->push_back (Target);
  159. }
  160. }
  161. // Only source release
  162. if (IndexTargets->empty() == false && ArchEntries.size() == 1)
  163. return IndexTargets;
  164. std::set<std::string> sections;
  165. for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
  166. a != ArchEntries.end(); ++a) {
  167. if (a->first == "source")
  168. continue;
  169. for (vector <const debSectionEntry *>::const_iterator I = a->second.begin();
  170. I != a->second.end(); ++I) {
  171. IndexTarget * Target = new IndexTarget();
  172. Target->ShortDesc = "Packages";
  173. Target->MetaKey = IndexURISuffix(Target->ShortDesc.c_str(), (*I)->Section, a->first);
  174. Target->URI = IndexURI(Target->ShortDesc.c_str(), (*I)->Section, a->first);
  175. Target->Description = Info (Target->ShortDesc.c_str(), (*I)->Section, a->first);
  176. IndexTargets->push_back (Target);
  177. sections.insert((*I)->Section);
  178. }
  179. }
  180. std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
  181. if (lang.empty() == true)
  182. return IndexTargets;
  183. // get the Translations:
  184. // - if its a dists-style repository get the i18n/Index first
  185. // - if its flat try to acquire files by guessing
  186. if (Dist[Dist.size() - 1] == '/') {
  187. for (std::set<std::string>::const_iterator s = sections.begin();
  188. s != sections.end(); ++s) {
  189. for (std::vector<std::string>::const_iterator l = lang.begin();
  190. l != lang.end(); ++l) {
  191. if (*l == "none") continue;
  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. } else {
  201. for (std::set<std::string>::const_iterator s = sections.begin();
  202. s != sections.end(); ++s) {
  203. IndexTarget * Target = new OptionalIndexTarget();
  204. Target->ShortDesc = "TranslationIndex";
  205. Target->MetaKey = TranslationIndexURISuffix("Index", *s);
  206. Target->URI = TranslationIndexURI("Index", *s);
  207. Target->Description = Info (Target->ShortDesc.c_str(), *s);
  208. IndexTargets->push_back (Target);
  209. }
  210. }
  211. return IndexTargets;
  212. }
  213. /*}}}*/
  214. bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool const &GetAll) const
  215. {
  216. // special case for --print-uris
  217. if (GetAll) {
  218. vector <struct IndexTarget *> *targets = ComputeIndexTargets();
  219. for (vector <struct IndexTarget*>::const_iterator Target = targets->begin(); Target != targets->end(); ++Target) {
  220. new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
  221. (*Target)->ShortDesc, HashString());
  222. }
  223. }
  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. return true;
  231. }
  232. void debReleaseIndex::SetTrusted(bool const Trusted)
  233. {
  234. if (Trusted == true)
  235. this->Trusted = ALWAYS_TRUSTED;
  236. else
  237. this->Trusted = NEVER_TRUSTED;
  238. }
  239. bool debReleaseIndex::IsTrusted() const
  240. {
  241. if (Trusted == ALWAYS_TRUSTED)
  242. return true;
  243. else if (Trusted == NEVER_TRUSTED)
  244. return false;
  245. if(_config->FindB("APT::Authentication::TrustCDROM", false))
  246. if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
  247. return true;
  248. string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
  249. URItoFileName(MetaIndexURI("Release")) + ".gpg";
  250. if (FileExists(VerifiedSigFile))
  251. return true;
  252. VerifiedSigFile = _config->FindDir("Dir::State::lists") +
  253. URItoFileName(MetaIndexURI("InRelease"));
  254. return FileExists(VerifiedSigFile);
  255. }
  256. vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles() {
  257. if (Indexes != NULL)
  258. return Indexes;
  259. Indexes = new vector <pkgIndexFile*>;
  260. map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
  261. if (src != ArchEntries.end()) {
  262. vector<debSectionEntry const*> const SectionEntries = src->second;
  263. for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
  264. I != SectionEntries.end(); ++I)
  265. Indexes->push_back(new debSourcesIndex (URI, Dist, (*I)->Section, IsTrusted()));
  266. }
  267. // Only source release
  268. if (Indexes->empty() == false && ArchEntries.size() == 1)
  269. return Indexes;
  270. std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
  271. map<string, set<string> > sections;
  272. for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
  273. a != ArchEntries.end(); ++a) {
  274. if (a->first == "source")
  275. continue;
  276. for (vector<debSectionEntry const*>::const_iterator I = a->second.begin();
  277. I != a->second.end(); ++I) {
  278. Indexes->push_back(new debPackagesIndex (URI, Dist, (*I)->Section, IsTrusted(), a->first));
  279. sections[(*I)->Section].insert(lang.begin(), lang.end());
  280. }
  281. }
  282. for (map<string, set<string> >::const_iterator s = sections.begin();
  283. s != sections.end(); ++s)
  284. for (set<string>::const_iterator l = s->second.begin();
  285. l != s->second.end(); ++l) {
  286. if (*l == "none") continue;
  287. Indexes->push_back(new debTranslationsIndex(URI,Dist,s->first,(*l).c_str()));
  288. }
  289. return Indexes;
  290. }
  291. void debReleaseIndex::PushSectionEntry(vector<string> const &Archs, const debSectionEntry *Entry) {
  292. for (vector<string>::const_iterator a = Archs.begin();
  293. a != Archs.end(); ++a)
  294. ArchEntries[*a].push_back(new debSectionEntry(Entry->Section, Entry->IsSrc));
  295. delete Entry;
  296. }
  297. void debReleaseIndex::PushSectionEntry(string const &Arch, const debSectionEntry *Entry) {
  298. ArchEntries[Arch].push_back(Entry);
  299. }
  300. void debReleaseIndex::PushSectionEntry(const debSectionEntry *Entry) {
  301. if (Entry->IsSrc == true)
  302. PushSectionEntry("source", Entry);
  303. else {
  304. for (map<string, vector<const debSectionEntry *> >::iterator a = ArchEntries.begin();
  305. a != ArchEntries.end(); ++a) {
  306. a->second.push_back(Entry);
  307. }
  308. }
  309. }
  310. debReleaseIndex::debSectionEntry::debSectionEntry (string const &Section,
  311. bool const &IsSrc): Section(Section), IsSrc(IsSrc)
  312. {}
  313. class debSLTypeDebian : public pkgSourceList::Type
  314. {
  315. protected:
  316. bool CreateItemInternal(vector<metaIndex *> &List, string const &URI,
  317. string const &Dist, string const &Section,
  318. bool const &IsSrc, map<string, string> const &Options) const
  319. {
  320. map<string, string>::const_iterator const arch = Options.find("arch");
  321. vector<string> const Archs =
  322. (arch != Options.end()) ? VectorizeString(arch->second, ',') :
  323. APT::Configuration::getArchitectures();
  324. map<string, string>::const_iterator const trusted = Options.find("trusted");
  325. for (vector<metaIndex *>::const_iterator I = List.begin();
  326. I != List.end(); ++I)
  327. {
  328. // We only worry about debian entries here
  329. if (strcmp((*I)->GetType(), "deb") != 0)
  330. continue;
  331. debReleaseIndex *Deb = (debReleaseIndex *) (*I);
  332. if (trusted != Options.end())
  333. Deb->SetTrusted(StringToBool(trusted->second, false));
  334. /* This check insures that there will be only one Release file
  335. queued for all the Packages files and Sources files it
  336. corresponds to. */
  337. if (Deb->GetURI() == URI && Deb->GetDist() == Dist)
  338. {
  339. if (IsSrc == true)
  340. Deb->PushSectionEntry("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  341. else
  342. {
  343. if (Dist[Dist.size() - 1] == '/')
  344. Deb->PushSectionEntry("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  345. else
  346. Deb->PushSectionEntry(Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
  347. }
  348. return true;
  349. }
  350. }
  351. // No currently created Release file indexes this entry, so we create a new one.
  352. debReleaseIndex *Deb;
  353. if (trusted != Options.end())
  354. Deb = new debReleaseIndex(URI, Dist, StringToBool(trusted->second, false));
  355. else
  356. Deb = new debReleaseIndex(URI, Dist);
  357. if (IsSrc == true)
  358. Deb->PushSectionEntry ("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  359. else
  360. {
  361. if (Dist[Dist.size() - 1] == '/')
  362. Deb->PushSectionEntry ("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  363. else
  364. Deb->PushSectionEntry (Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
  365. }
  366. List.push_back(Deb);
  367. return true;
  368. }
  369. };
  370. class debSLTypeDeb : public debSLTypeDebian
  371. {
  372. public:
  373. bool CreateItem(vector<metaIndex *> &List, string const &URI,
  374. string const &Dist, string const &Section,
  375. std::map<string, string> const &Options) const
  376. {
  377. return CreateItemInternal(List, URI, Dist, Section, false, Options);
  378. }
  379. debSLTypeDeb()
  380. {
  381. Name = "deb";
  382. Label = "Standard Debian binary tree";
  383. }
  384. };
  385. class debSLTypeDebSrc : public debSLTypeDebian
  386. {
  387. public:
  388. bool CreateItem(vector<metaIndex *> &List, string const &URI,
  389. string const &Dist, string const &Section,
  390. std::map<string, string> const &Options) const
  391. {
  392. return CreateItemInternal(List, URI, Dist, Section, true, Options);
  393. }
  394. debSLTypeDebSrc()
  395. {
  396. Name = "deb-src";
  397. Label = "Standard Debian source tree";
  398. }
  399. };
  400. debSLTypeDeb _apt_DebType;
  401. debSLTypeDebSrc _apt_DebSrcType;