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.
 
 
 
 
 
 

566 lines
18 KiB

  1. #include <config.h>
  2. #include <apt-pkg/error.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/hashes.h>
  13. #include <apt-pkg/metaindex.h>
  14. #include <apt-pkg/pkgcachegen.h>
  15. #include <apt-pkg/tagfile.h>
  16. #include <apt-pkg/gpgv.h>
  17. #include <apt-pkg/macros.h>
  18. #include <map>
  19. #include <string>
  20. #include <utility>
  21. #include <vector>
  22. #include <set>
  23. #include <algorithm>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <unistd.h>
  27. #include <string.h>
  28. #include <apti18n.h>
  29. class APT_HIDDEN debReleaseIndexPrivate /*{{{*/
  30. {
  31. public:
  32. struct APT_HIDDEN debSectionEntry
  33. {
  34. std::string Name;
  35. std::vector<std::string> Targets;
  36. std::vector<std::string> Architectures;
  37. std::vector<std::string> Languages;
  38. };
  39. std::vector<debSectionEntry> DebEntries;
  40. std::vector<debSectionEntry> DebSrcEntries;
  41. debReleaseIndex::TriState Trusted;
  42. debReleaseIndexPrivate() : Trusted(debReleaseIndex::TRI_UNSET) {}
  43. debReleaseIndexPrivate(bool const pTrusted) : Trusted(pTrusted ? debReleaseIndex::TRI_YES : debReleaseIndex::TRI_NO) {}
  44. };
  45. /*}}}*/
  46. // ReleaseIndex::MetaIndex* - display helpers /*{{{*/
  47. std::string debReleaseIndex::MetaIndexInfo(const char *Type) const
  48. {
  49. std::string Info = ::URI::ArchiveOnly(URI) + ' ';
  50. if (Dist[Dist.size() - 1] == '/')
  51. {
  52. if (Dist != "/")
  53. Info += Dist;
  54. }
  55. else
  56. Info += Dist;
  57. Info += " ";
  58. Info += Type;
  59. return Info;
  60. }
  61. std::string debReleaseIndex::Describe() const
  62. {
  63. return MetaIndexInfo("Release");
  64. }
  65. std::string debReleaseIndex::MetaIndexFile(const char *Type) const
  66. {
  67. return _config->FindDir("Dir::State::lists") +
  68. URItoFileName(MetaIndexURI(Type));
  69. }
  70. std::string debReleaseIndex::MetaIndexURI(const char *Type) const
  71. {
  72. std::string Res;
  73. if (Dist == "/")
  74. Res = URI;
  75. else if (Dist[Dist.size()-1] == '/')
  76. Res = URI + Dist;
  77. else
  78. Res = URI + "dists/" + Dist + "/";
  79. Res += Type;
  80. return Res;
  81. }
  82. /*}}}*/
  83. std::string debReleaseIndex::LocalFileName() const /*{{{*/
  84. {
  85. // see if we have a InRelease file
  86. std::string PathInRelease = MetaIndexFile("InRelease");
  87. if (FileExists(PathInRelease))
  88. return PathInRelease;
  89. // and if not return the normal one
  90. if (FileExists(PathInRelease))
  91. return MetaIndexFile("Release");
  92. return "";
  93. }
  94. /*}}}*/
  95. // ReleaseIndex Con- and Destructors /*{{{*/
  96. debReleaseIndex::debReleaseIndex(std::string const &URI, std::string const &Dist) :
  97. metaIndex(URI, Dist, "deb"), d(new debReleaseIndexPrivate())
  98. {}
  99. debReleaseIndex::debReleaseIndex(std::string const &URI, std::string const &Dist, bool const Trusted) :
  100. metaIndex(URI, Dist, "deb"), d(new debReleaseIndexPrivate(Trusted))
  101. {}
  102. debReleaseIndex::~debReleaseIndex() {
  103. if (d != NULL)
  104. delete d;
  105. }
  106. /*}}}*/
  107. // ReleaseIndex::GetIndexTargets /*{{{*/
  108. static void GetIndexTargetsFor(char const * const Type, std::string const &URI, std::string const &Dist,
  109. std::vector<debReleaseIndexPrivate::debSectionEntry> const &entries,
  110. std::vector<IndexTarget> &IndexTargets)
  111. {
  112. bool const flatArchive = (Dist[Dist.length() - 1] == '/');
  113. std::string baseURI = URI;
  114. if (flatArchive)
  115. {
  116. if (Dist != "/")
  117. baseURI += Dist;
  118. }
  119. else
  120. baseURI += "dists/" + Dist + "/";
  121. std::string const Release = (Dist == "/") ? "" : Dist;
  122. std::string const Site = ::URI::ArchiveOnly(URI);
  123. for (std::vector<debReleaseIndexPrivate::debSectionEntry>::const_iterator E = entries.begin(); E != entries.end(); ++E)
  124. {
  125. for (std::vector<std::string>::const_iterator T = E->Targets.begin(); T != E->Targets.end(); ++T)
  126. {
  127. #define APT_T_CONFIG(X) _config->Find(std::string("APT::Acquire::Targets::") + Type + "::" + *T + "::" + (X))
  128. std::string const tplMetaKey = APT_T_CONFIG(flatArchive ? "flatMetaKey" : "MetaKey");
  129. std::string const tplShortDesc = APT_T_CONFIG("ShortDescription");
  130. std::string const tplLongDesc = APT_T_CONFIG(flatArchive ? "flatDescription" : "Description");
  131. bool const IsOptional = _config->FindB(std::string("APT::Acquire::Targets::deb-src::") + *T + "::Optional", true);
  132. #undef APT_T_CONFIG
  133. if (tplMetaKey.empty())
  134. continue;
  135. for (std::vector<std::string>::const_iterator L = E->Languages.begin(); L != E->Languages.end(); ++L)
  136. {
  137. if (*L == "none" && tplMetaKey.find("$(LANGUAGE)") != std::string::npos)
  138. continue;
  139. for (std::vector<std::string>::const_iterator A = E->Architectures.begin(); A != E->Architectures.end(); ++A)
  140. {
  141. std::map<std::string, std::string> Options;
  142. Options.insert(std::make_pair("SITE", Site));
  143. Options.insert(std::make_pair("RELEASE", Release));
  144. if (tplMetaKey.find("$(COMPONENT)") != std::string::npos)
  145. Options.insert(std::make_pair("COMPONENT", E->Name));
  146. if (tplMetaKey.find("$(LANGUAGE)") != std::string::npos)
  147. Options.insert(std::make_pair("LANGUAGE", *L));
  148. if (tplMetaKey.find("$(ARCHITECTURE)") != std::string::npos)
  149. Options.insert(std::make_pair("ARCHITECTURE", *A));
  150. Options.insert(std::make_pair("BASE_URI", baseURI));
  151. Options.insert(std::make_pair("REPO_URI", URI));
  152. Options.insert(std::make_pair("TARGET_OF", "deb-src"));
  153. Options.insert(std::make_pair("CREATED_BY", *T));
  154. std::string MetaKey = tplMetaKey;
  155. std::string ShortDesc = tplShortDesc;
  156. std::string LongDesc = tplLongDesc;
  157. for (std::map<std::string, std::string>::const_iterator O = Options.begin(); O != Options.end(); ++O)
  158. {
  159. MetaKey = SubstVar(MetaKey, std::string("$(") + O->first + ")", O->second);
  160. ShortDesc = SubstVar(ShortDesc, std::string("$(") + O->first + ")", O->second);
  161. LongDesc = SubstVar(LongDesc, std::string("$(") + O->first + ")", O->second);
  162. }
  163. IndexTarget Target(
  164. MetaKey,
  165. ShortDesc,
  166. LongDesc,
  167. Options.find("BASE_URI")->second + MetaKey,
  168. IsOptional,
  169. Options
  170. );
  171. IndexTargets.push_back(Target);
  172. if (tplMetaKey.find("$(ARCHITECTURE)") == std::string::npos)
  173. break;
  174. }
  175. if (tplMetaKey.find("$(LANGUAGE)") == std::string::npos)
  176. break;
  177. }
  178. }
  179. }
  180. }
  181. std::vector<IndexTarget> debReleaseIndex::GetIndexTargets() const
  182. {
  183. std::vector<IndexTarget> IndexTargets;
  184. GetIndexTargetsFor("deb-src", URI, Dist, d->DebSrcEntries, IndexTargets);
  185. GetIndexTargetsFor("deb", URI, Dist, d->DebEntries, IndexTargets);
  186. return IndexTargets;
  187. }
  188. /*}}}*/
  189. void debReleaseIndex::AddComponent(bool const isSrc, std::string const &Name,/*{{{*/
  190. std::vector<std::string> const &Targets,
  191. std::vector<std::string> const &Architectures,
  192. std::vector<std::string> Languages)
  193. {
  194. if (Languages.empty() == true)
  195. Languages.push_back("none");
  196. debReleaseIndexPrivate::debSectionEntry const entry = {
  197. Name, Targets, Architectures, Languages
  198. };
  199. if (isSrc)
  200. d->DebSrcEntries.push_back(entry);
  201. else
  202. d->DebEntries.push_back(entry);
  203. }
  204. /*}}}*/
  205. bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool const &GetAll) const/*{{{*/
  206. {
  207. indexRecords * const iR = new indexRecords(Dist);
  208. if (d->Trusted == TRI_YES)
  209. iR->SetTrusted(true);
  210. else if (d->Trusted == TRI_NO)
  211. iR->SetTrusted(false);
  212. // special case for --print-uris
  213. std::vector<IndexTarget> const targets = GetIndexTargets();
  214. #define APT_TARGET(X) IndexTarget("", X, MetaIndexInfo(X), MetaIndexURI(X), false, std::map<std::string,std::string>())
  215. pkgAcqMetaClearSig * const TransactionManager = new pkgAcqMetaClearSig(Owner,
  216. APT_TARGET("InRelease"), APT_TARGET("Release"), APT_TARGET("Release.gpg"),
  217. targets, iR);
  218. #undef APT_TARGET
  219. if (GetAll)
  220. {
  221. for (std::vector<IndexTarget>::const_iterator Target = targets.begin(); Target != targets.end(); ++Target)
  222. new pkgAcqIndex(Owner, TransactionManager, *Target);
  223. }
  224. return true;
  225. }
  226. /*}}}*/
  227. // ReleaseIndex::IsTrusted /*{{{*/
  228. bool debReleaseIndex::SetTrusted(TriState const Trusted)
  229. {
  230. if (d->Trusted == TRI_UNSET)
  231. d->Trusted = Trusted;
  232. else if (d->Trusted != Trusted)
  233. // TRANSLATOR: The first is an option name from sources.list manpage, the other two URI and Suite
  234. return _error->Error(_("Conflicting values set for option %s concerning source %s %s"), "Trusted", URI.c_str(), Dist.c_str());
  235. return true;
  236. }
  237. bool debReleaseIndex::IsTrusted() const
  238. {
  239. if (d->Trusted == TRI_YES)
  240. return true;
  241. else if (d->Trusted == TRI_NO)
  242. return false;
  243. if(_config->FindB("APT::Authentication::TrustCDROM", false))
  244. if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
  245. return true;
  246. if (FileExists(MetaIndexFile("Release.gpg")))
  247. return true;
  248. return FileExists(MetaIndexFile("InRelease"));
  249. }
  250. /*}}}*/
  251. std::vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles() /*{{{*/
  252. {
  253. if (Indexes != NULL)
  254. return Indexes;
  255. Indexes = new std::vector<pkgIndexFile*>();
  256. std::vector<IndexTarget> const Targets = GetIndexTargets();
  257. bool const istrusted = IsTrusted();
  258. for (std::vector<IndexTarget>::const_iterator T = Targets.begin(); T != Targets.end(); ++T)
  259. {
  260. std::string const TargetName = T->Option(IndexTarget::CREATED_BY);
  261. if (TargetName == "Packages")
  262. Indexes->push_back(new debPackagesIndex(*T, istrusted));
  263. else if (TargetName == "Sources")
  264. Indexes->push_back(new debSourcesIndex(*T, istrusted));
  265. else if (TargetName == "Translations")
  266. Indexes->push_back(new debTranslationsIndex(*T));
  267. }
  268. return Indexes;
  269. }
  270. /*}}}*/
  271. static bool ReleaseFileName(debReleaseIndex const * const That, std::string &ReleaseFile)/*{{{*/
  272. {
  273. ReleaseFile = That->MetaIndexFile("InRelease");
  274. bool releaseExists = false;
  275. if (FileExists(ReleaseFile) == true)
  276. releaseExists = true;
  277. else
  278. {
  279. ReleaseFile = That->MetaIndexFile("Release");
  280. if (FileExists(ReleaseFile))
  281. releaseExists = true;
  282. }
  283. return releaseExists;
  284. }
  285. /*}}}*/
  286. bool debReleaseIndex::Merge(pkgCacheGenerator &Gen,OpProgress * /*Prog*/) const/*{{{*/
  287. {
  288. std::string ReleaseFile;
  289. bool const releaseExists = ReleaseFileName(this, ReleaseFile);
  290. ::URI Tmp(URI);
  291. if (Gen.SelectReleaseFile(ReleaseFile, Tmp.Host) == false)
  292. return _error->Error("Problem with SelectReleaseFile %s", ReleaseFile.c_str());
  293. if (releaseExists == false)
  294. return true;
  295. FileFd Rel;
  296. // Beware: The 'Release' file might be clearsigned in case the
  297. // signature for an 'InRelease' file couldn't be checked
  298. if (OpenMaybeClearSignedFile(ReleaseFile, Rel) == false)
  299. return false;
  300. if (_error->PendingError() == true)
  301. return false;
  302. // Store the IMS information
  303. pkgCache::RlsFileIterator File = Gen.GetCurRlsFile();
  304. pkgCacheGenerator::Dynamic<pkgCache::RlsFileIterator> DynFile(File);
  305. // Rel can't be used as this is potentially a temporary file
  306. struct stat Buf;
  307. if (stat(ReleaseFile.c_str(), &Buf) != 0)
  308. return _error->Errno("fstat", "Unable to stat file %s", ReleaseFile.c_str());
  309. File->Size = Buf.st_size;
  310. File->mtime = Buf.st_mtime;
  311. pkgTagFile TagFile(&Rel, Rel.Size());
  312. pkgTagSection Section;
  313. if (_error->PendingError() == true || TagFile.Step(Section) == false)
  314. return false;
  315. std::string data;
  316. #define APT_INRELEASE(TYPE, TAG, STORE) \
  317. data = Section.FindS(TAG); \
  318. if (data.empty() == false) \
  319. { \
  320. map_stringitem_t const storage = Gen.StoreString(pkgCacheGenerator::TYPE, data); \
  321. STORE = storage; \
  322. }
  323. APT_INRELEASE(MIXED, "Suite", File->Archive)
  324. APT_INRELEASE(VERSIONNUMBER, "Version", File->Version)
  325. APT_INRELEASE(MIXED, "Origin", File->Origin)
  326. APT_INRELEASE(MIXED, "Codename", File->Codename)
  327. APT_INRELEASE(MIXED, "Label", File->Label)
  328. #undef APT_INRELEASE
  329. Section.FindFlag("NotAutomatic", File->Flags, pkgCache::Flag::NotAutomatic);
  330. Section.FindFlag("ButAutomaticUpgrades", File->Flags, pkgCache::Flag::ButAutomaticUpgrades);
  331. return !_error->PendingError();
  332. }
  333. /*}}}*/
  334. // ReleaseIndex::FindInCache - Find this index /*{{{*/
  335. pkgCache::RlsFileIterator debReleaseIndex::FindInCache(pkgCache &Cache, bool const ModifyCheck) const
  336. {
  337. std::string ReleaseFile;
  338. bool const releaseExists = ReleaseFileName(this, ReleaseFile);
  339. pkgCache::RlsFileIterator File = Cache.RlsFileBegin();
  340. for (; File.end() == false; ++File)
  341. {
  342. if (File->FileName == 0 || ReleaseFile != File.FileName())
  343. continue;
  344. // empty means the file does not exist by "design"
  345. if (ModifyCheck == false || (releaseExists == false && File->Size == 0))
  346. return File;
  347. struct stat St;
  348. if (stat(File.FileName(),&St) != 0)
  349. {
  350. if (_config->FindB("Debug::pkgCacheGen", false))
  351. std::clog << "ReleaseIndex::FindInCache - stat failed on " << File.FileName() << std::endl;
  352. return pkgCache::RlsFileIterator(Cache);
  353. }
  354. if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
  355. {
  356. if (_config->FindB("Debug::pkgCacheGen", false))
  357. std::clog << "ReleaseIndex::FindInCache - size (" << St.st_size << " <> " << File->Size
  358. << ") or mtime (" << St.st_mtime << " <> " << File->mtime
  359. << ") doesn't match for " << File.FileName() << std::endl;
  360. return pkgCache::RlsFileIterator(Cache);
  361. }
  362. return File;
  363. }
  364. return File;
  365. }
  366. /*}}}*/
  367. static std::vector<std::string> parsePlusMinusOptions(std::string const &Name, /*{{{*/
  368. std::map<std::string, std::string> const &Options, std::vector<std::string> const &defaultValues)
  369. {
  370. std::map<std::string, std::string>::const_iterator val = Options.find(Name);
  371. std::vector<std::string> Values;
  372. if (val != Options.end())
  373. Values = VectorizeString(val->second, ',');
  374. else
  375. Values = defaultValues;
  376. if ((val = Options.find(Name + "+")) != Options.end())
  377. {
  378. std::vector<std::string> const plusArch = VectorizeString(val->second, ',');
  379. for (std::vector<std::string>::const_iterator plus = plusArch.begin(); plus != plusArch.end(); ++plus)
  380. if (std::find(Values.begin(), Values.end(), *plus) == Values.end())
  381. Values.push_back(*plus);
  382. }
  383. if ((val = Options.find(Name + "-")) != Options.end())
  384. {
  385. std::vector<std::string> const minusArch = VectorizeString(val->second, ',');
  386. for (std::vector<std::string>::const_iterator minus = minusArch.begin(); minus != minusArch.end(); ++minus)
  387. {
  388. std::vector<std::string>::iterator kill = std::find(Values.begin(), Values.end(), *minus);
  389. if (kill != Values.end())
  390. Values.erase(kill);
  391. }
  392. }
  393. return Values;
  394. }
  395. /*}}}*/
  396. class APT_HIDDEN debSLTypeDebian : public pkgSourceList::Type /*{{{*/
  397. {
  398. protected:
  399. bool CreateItemInternal(std::vector<metaIndex *> &List, std::string const &URI,
  400. std::string const &Dist, std::string const &Section,
  401. bool const &IsSrc, std::map<std::string, std::string> const &Options) const
  402. {
  403. debReleaseIndex *Deb = NULL;
  404. for (std::vector<metaIndex *>::const_iterator I = List.begin();
  405. I != List.end(); ++I)
  406. {
  407. // We only worry about debian entries here
  408. if (strcmp((*I)->GetType(), "deb") != 0)
  409. continue;
  410. /* This check insures that there will be only one Release file
  411. queued for all the Packages files and Sources files it
  412. corresponds to. */
  413. if ((*I)->GetURI() == URI && (*I)->GetDist() == Dist)
  414. {
  415. Deb = dynamic_cast<debReleaseIndex*>(*I);
  416. if (Deb != NULL)
  417. break;
  418. }
  419. }
  420. // No currently created Release file indexes this entry, so we create a new one.
  421. if (Deb == NULL)
  422. {
  423. Deb = new debReleaseIndex(URI, Dist);
  424. List.push_back(Deb);
  425. }
  426. Deb->AddComponent(
  427. IsSrc,
  428. Section,
  429. parsePlusMinusOptions("target", Options, _config->FindVector(std::string("APT::Acquire::Targets::") + Name, "", true)),
  430. parsePlusMinusOptions("arch", Options, APT::Configuration::getArchitectures()),
  431. parsePlusMinusOptions("lang", Options, APT::Configuration::getLanguages(true))
  432. );
  433. std::map<std::string, std::string>::const_iterator const trusted = Options.find("trusted");
  434. if (trusted != Options.end())
  435. {
  436. if (Deb->SetTrusted(StringToBool(trusted->second, false) ? debReleaseIndex::TRI_YES : debReleaseIndex::TRI_NO) == false)
  437. return false;
  438. }
  439. else if (Deb->SetTrusted(debReleaseIndex::TRI_DONTCARE) == false)
  440. return false;
  441. return true;
  442. }
  443. debSLTypeDebian(char const * const Name, char const * const Label) : Type(Name, Label)
  444. {
  445. }
  446. };
  447. /*}}}*/
  448. class APT_HIDDEN debSLTypeDeb : public debSLTypeDebian /*{{{*/
  449. {
  450. public:
  451. bool CreateItem(std::vector<metaIndex *> &List, std::string const &URI,
  452. std::string const &Dist, std::string const &Section,
  453. std::map<std::string, std::string> const &Options) const
  454. {
  455. return CreateItemInternal(List, URI, Dist, Section, false, Options);
  456. }
  457. debSLTypeDeb() : debSLTypeDebian("deb", "Debian binary tree")
  458. {
  459. }
  460. };
  461. /*}}}*/
  462. class APT_HIDDEN debSLTypeDebSrc : public debSLTypeDebian /*{{{*/
  463. {
  464. public:
  465. bool CreateItem(std::vector<metaIndex *> &List, std::string const &URI,
  466. std::string const &Dist, std::string const &Section,
  467. std::map<std::string, std::string> const &Options) const
  468. {
  469. return CreateItemInternal(List, URI, Dist, Section, true, Options);
  470. }
  471. debSLTypeDebSrc() : debSLTypeDebian("deb-src", "Debian source tree")
  472. {
  473. }
  474. };
  475. /*}}}*/
  476. debDebFileMetaIndex::debDebFileMetaIndex(std::string const &DebFile) /*{{{*/
  477. : metaIndex(DebFile, "local-uri", "deb-dist"), d(NULL), DebFile(DebFile)
  478. {
  479. DebIndex = new debDebPkgFileIndex(DebFile);
  480. Indexes = new std::vector<pkgIndexFile *>();
  481. Indexes->push_back(DebIndex);
  482. }
  483. debDebFileMetaIndex::~debDebFileMetaIndex() {}
  484. /*}}}*/
  485. class APT_HIDDEN debSLTypeDebFile : public pkgSourceList::Type /*{{{*/
  486. {
  487. public:
  488. bool CreateItem(std::vector<metaIndex *> &List, std::string const &URI,
  489. std::string const &/*Dist*/, std::string const &/*Section*/,
  490. std::map<std::string, std::string> const &/*Options*/) const
  491. {
  492. metaIndex *mi = new debDebFileMetaIndex(URI);
  493. List.push_back(mi);
  494. return true;
  495. }
  496. debSLTypeDebFile() : Type("deb-file", "Debian local deb file")
  497. {
  498. }
  499. };
  500. /*}}}*/
  501. APT_HIDDEN debSLTypeDeb _apt_DebType;
  502. APT_HIDDEN debSLTypeDebSrc _apt_DebSrcType;
  503. APT_HIDDEN debSLTypeDebFile _apt_DebFileType;