You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

482 lines
15 KiB

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