debmetaindex.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. // special case for --print-uris
  206. if (GetAll) {
  207. vector <struct IndexTarget *> *targets = ComputeIndexTargets();
  208. for (vector <struct IndexTarget*>::const_iterator Target = targets->begin(); Target != targets->end(); ++Target) {
  209. new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
  210. (*Target)->ShortDesc, HashString());
  211. }
  212. }
  213. new pkgAcqMetaClearSig(Owner, MetaIndexURI("InRelease"),
  214. MetaIndexInfo("InRelease"), "InRelease",
  215. MetaIndexURI("Release"), MetaIndexInfo("Release"), "Release",
  216. MetaIndexURI("Release.gpg"), MetaIndexInfo("Release.gpg"), "Release.gpg",
  217. ComputeIndexTargets(),
  218. new indexRecords (Dist));
  219. return true;
  220. }
  221. void debReleaseIndex::SetTrusted(bool const Trusted)
  222. {
  223. if (Trusted == true)
  224. this->Trusted = ALWAYS_TRUSTED;
  225. else
  226. this->Trusted = NEVER_TRUSTED;
  227. }
  228. bool debReleaseIndex::IsTrusted() const
  229. {
  230. if (Trusted == ALWAYS_TRUSTED)
  231. return true;
  232. else if (Trusted == NEVER_TRUSTED)
  233. return false;
  234. if(_config->FindB("APT::Authentication::TrustCDROM", false))
  235. if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
  236. return true;
  237. string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
  238. URItoFileName(MetaIndexURI("Release")) + ".gpg";
  239. if (FileExists(VerifiedSigFile))
  240. return true;
  241. VerifiedSigFile = _config->FindDir("Dir::State::lists") +
  242. URItoFileName(MetaIndexURI("InRelease"));
  243. return FileExists(VerifiedSigFile);
  244. }
  245. vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles() {
  246. if (Indexes != NULL)
  247. return Indexes;
  248. Indexes = new vector <pkgIndexFile*>;
  249. map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
  250. if (src != ArchEntries.end()) {
  251. vector<debSectionEntry const*> const SectionEntries = src->second;
  252. for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
  253. I != SectionEntries.end(); ++I)
  254. Indexes->push_back(new debSourcesIndex (URI, Dist, (*I)->Section, IsTrusted()));
  255. }
  256. // Only source release
  257. if (Indexes->empty() == false && ArchEntries.size() == 1)
  258. return Indexes;
  259. std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
  260. map<string, set<string> > sections;
  261. for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
  262. a != ArchEntries.end(); ++a) {
  263. if (a->first == "source")
  264. continue;
  265. for (vector<debSectionEntry const*>::const_iterator I = a->second.begin();
  266. I != a->second.end(); ++I) {
  267. Indexes->push_back(new debPackagesIndex (URI, Dist, (*I)->Section, IsTrusted(), a->first));
  268. sections[(*I)->Section].insert(lang.begin(), lang.end());
  269. }
  270. }
  271. for (map<string, set<string> >::const_iterator s = sections.begin();
  272. s != sections.end(); ++s)
  273. for (set<string>::const_iterator l = s->second.begin();
  274. l != s->second.end(); ++l) {
  275. if (*l == "none") continue;
  276. Indexes->push_back(new debTranslationsIndex(URI,Dist,s->first,(*l).c_str()));
  277. }
  278. return Indexes;
  279. }
  280. void debReleaseIndex::PushSectionEntry(vector<string> const &Archs, const debSectionEntry *Entry) {
  281. for (vector<string>::const_iterator a = Archs.begin();
  282. a != Archs.end(); ++a)
  283. ArchEntries[*a].push_back(new debSectionEntry(Entry->Section, Entry->IsSrc));
  284. delete Entry;
  285. }
  286. void debReleaseIndex::PushSectionEntry(string const &Arch, const debSectionEntry *Entry) {
  287. ArchEntries[Arch].push_back(Entry);
  288. }
  289. void debReleaseIndex::PushSectionEntry(const debSectionEntry *Entry) {
  290. if (Entry->IsSrc == true)
  291. PushSectionEntry("source", Entry);
  292. else {
  293. for (map<string, vector<const debSectionEntry *> >::iterator a = ArchEntries.begin();
  294. a != ArchEntries.end(); ++a) {
  295. a->second.push_back(Entry);
  296. }
  297. }
  298. }
  299. debReleaseIndex::debSectionEntry::debSectionEntry (string const &Section,
  300. bool const &IsSrc): Section(Section), IsSrc(IsSrc)
  301. {}
  302. class debSLTypeDebian : public pkgSourceList::Type
  303. {
  304. protected:
  305. bool CreateItemInternal(vector<metaIndex *> &List, string const &URI,
  306. string const &Dist, string const &Section,
  307. bool const &IsSrc, map<string, string> const &Options) const
  308. {
  309. map<string, string>::const_iterator const arch = Options.find("arch");
  310. vector<string> const Archs =
  311. (arch != Options.end()) ? VectorizeString(arch->second, ',') :
  312. APT::Configuration::getArchitectures();
  313. map<string, string>::const_iterator const trusted = Options.find("trusted");
  314. for (vector<metaIndex *>::const_iterator I = List.begin();
  315. I != List.end(); ++I)
  316. {
  317. // We only worry about debian entries here
  318. if (strcmp((*I)->GetType(), "deb") != 0)
  319. continue;
  320. debReleaseIndex *Deb = (debReleaseIndex *) (*I);
  321. if (trusted != Options.end())
  322. Deb->SetTrusted(StringToBool(trusted->second, false));
  323. /* This check insures that there will be only one Release file
  324. queued for all the Packages files and Sources files it
  325. corresponds to. */
  326. if (Deb->GetURI() == URI && Deb->GetDist() == Dist)
  327. {
  328. if (IsSrc == true)
  329. Deb->PushSectionEntry("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  330. else
  331. {
  332. if (Dist[Dist.size() - 1] == '/')
  333. Deb->PushSectionEntry("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  334. else
  335. Deb->PushSectionEntry(Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
  336. }
  337. return true;
  338. }
  339. }
  340. // No currently created Release file indexes this entry, so we create a new one.
  341. debReleaseIndex *Deb;
  342. if (trusted != Options.end())
  343. Deb = new debReleaseIndex(URI, Dist, StringToBool(trusted->second, false));
  344. else
  345. Deb = new debReleaseIndex(URI, Dist);
  346. if (IsSrc == true)
  347. Deb->PushSectionEntry ("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  348. else
  349. {
  350. if (Dist[Dist.size() - 1] == '/')
  351. Deb->PushSectionEntry ("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
  352. else
  353. Deb->PushSectionEntry (Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
  354. }
  355. List.push_back(Deb);
  356. return true;
  357. }
  358. };
  359. class debSLTypeDeb : public debSLTypeDebian
  360. {
  361. public:
  362. bool CreateItem(vector<metaIndex *> &List, string const &URI,
  363. string const &Dist, string const &Section,
  364. std::map<string, string> const &Options) const
  365. {
  366. return CreateItemInternal(List, URI, Dist, Section, false, Options);
  367. }
  368. debSLTypeDeb()
  369. {
  370. Name = "deb";
  371. Label = "Standard Debian binary tree";
  372. }
  373. };
  374. class debSLTypeDebSrc : public debSLTypeDebian
  375. {
  376. public:
  377. bool CreateItem(vector<metaIndex *> &List, string const &URI,
  378. string const &Dist, string const &Section,
  379. std::map<string, string> const &Options) const
  380. {
  381. return CreateItemInternal(List, URI, Dist, Section, true, Options);
  382. }
  383. debSLTypeDebSrc()
  384. {
  385. Name = "deb-src";
  386. Label = "Standard Debian source tree";
  387. }
  388. };
  389. debSLTypeDeb _apt_DebType;
  390. debSLTypeDebSrc _apt_DebSrcType;