debmetaindex.cc 14 KB

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