debmetaindex.cc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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/sourcelist.h>
  11. #include <apt-pkg/hashes.h>
  12. #include <apt-pkg/metaindex.h>
  13. #include <apt-pkg/pkgcachegen.h>
  14. #include <apt-pkg/tagfile.h>
  15. #include <apt-pkg/gpgv.h>
  16. #include <apt-pkg/macros.h>
  17. #include <map>
  18. #include <string>
  19. #include <utility>
  20. #include <vector>
  21. #include <algorithm>
  22. #include <sstream>
  23. #include <sys/stat.h>
  24. #include <string.h>
  25. #include <apti18n.h>
  26. class APT_HIDDEN debReleaseIndexPrivate /*{{{*/
  27. {
  28. public:
  29. struct APT_HIDDEN debSectionEntry
  30. {
  31. std::string Name;
  32. std::vector<std::string> Targets;
  33. std::vector<std::string> Architectures;
  34. std::vector<std::string> Languages;
  35. bool UsePDiffs;
  36. };
  37. std::vector<debSectionEntry> DebEntries;
  38. std::vector<debSectionEntry> DebSrcEntries;
  39. metaIndex::TriState CheckValidUntil;
  40. time_t ValidUntilMin;
  41. time_t ValidUntilMax;
  42. debReleaseIndexPrivate() : CheckValidUntil(metaIndex::TRI_UNSET), ValidUntilMin(0), ValidUntilMax(0) {}
  43. };
  44. /*}}}*/
  45. // ReleaseIndex::MetaIndex* - display helpers /*{{{*/
  46. std::string debReleaseIndex::MetaIndexInfo(const char *Type) const
  47. {
  48. std::string Info = ::URI::ArchiveOnly(URI) + ' ';
  49. if (Dist[Dist.size() - 1] == '/')
  50. {
  51. if (Dist != "/")
  52. Info += Dist;
  53. }
  54. else
  55. Info += Dist;
  56. Info += " ";
  57. Info += Type;
  58. return Info;
  59. }
  60. std::string debReleaseIndex::Describe() const
  61. {
  62. return MetaIndexInfo("Release");
  63. }
  64. std::string debReleaseIndex::MetaIndexFile(const char *Type) const
  65. {
  66. return _config->FindDir("Dir::State::lists") +
  67. URItoFileName(MetaIndexURI(Type));
  68. }
  69. std::string debReleaseIndex::MetaIndexURI(const char *Type) const
  70. {
  71. std::string Res;
  72. if (Dist == "/")
  73. Res = URI;
  74. else if (Dist[Dist.size()-1] == '/')
  75. Res = URI + Dist;
  76. else
  77. Res = URI + "dists/" + Dist + "/";
  78. Res += Type;
  79. return Res;
  80. }
  81. /*}}}*/
  82. // ReleaseIndex Con- and Destructors /*{{{*/
  83. debReleaseIndex::debReleaseIndex(std::string const &URI, std::string const &Dist) :
  84. metaIndex(URI, Dist, "deb"), d(new debReleaseIndexPrivate())
  85. {}
  86. debReleaseIndex::debReleaseIndex(std::string const &URI, std::string const &Dist, bool const pTrusted) :
  87. metaIndex(URI, Dist, "deb"), d(new debReleaseIndexPrivate())
  88. {
  89. Trusted = pTrusted ? TRI_YES : TRI_NO;
  90. }
  91. debReleaseIndex::~debReleaseIndex() {
  92. if (d != NULL)
  93. delete d;
  94. }
  95. /*}}}*/
  96. // ReleaseIndex::GetIndexTargets /*{{{*/
  97. static void GetIndexTargetsFor(char const * const Type, std::string const &URI, std::string const &Dist,
  98. std::vector<debReleaseIndexPrivate::debSectionEntry> const &entries,
  99. std::vector<IndexTarget> &IndexTargets)
  100. {
  101. bool const flatArchive = (Dist[Dist.length() - 1] == '/');
  102. std::string baseURI = URI;
  103. if (flatArchive)
  104. {
  105. if (Dist != "/")
  106. baseURI += Dist;
  107. }
  108. else
  109. baseURI += "dists/" + Dist + "/";
  110. std::string const Release = (Dist == "/") ? "" : Dist;
  111. std::string const Site = ::URI::ArchiveOnly(URI);
  112. std::string DefCompressionTypes;
  113. {
  114. std::vector<std::string> types = APT::Configuration::getCompressionTypes();
  115. if (types.empty() == false)
  116. {
  117. std::ostringstream os;
  118. std::copy(types.begin(), types.end()-1, std::ostream_iterator<std::string>(os, " "));
  119. os << *types.rbegin();
  120. DefCompressionTypes = os.str();
  121. }
  122. }
  123. std::string const NativeArch = _config->Find("APT::Architecture");
  124. bool const GzipIndex = _config->FindB("Acquire::GzipIndexes", false);
  125. for (std::vector<debReleaseIndexPrivate::debSectionEntry>::const_iterator E = entries.begin(); E != entries.end(); ++E)
  126. {
  127. for (std::vector<std::string>::const_iterator T = E->Targets.begin(); T != E->Targets.end(); ++T)
  128. {
  129. #define APT_T_CONFIG_STR(X, Y) _config->Find(std::string("Acquire::IndexTargets::") + Type + "::" + *T + "::" + (X), (Y))
  130. #define APT_T_CONFIG_BOOL(X, Y) _config->FindB(std::string("Acquire::IndexTargets::") + Type + "::" + *T + "::" + (X), (Y))
  131. std::string const tplMetaKey = APT_T_CONFIG_STR(flatArchive ? "flatMetaKey" : "MetaKey", "");
  132. std::string const tplShortDesc = APT_T_CONFIG_STR("ShortDescription", "");
  133. std::string const tplLongDesc = "$(SITE) " + APT_T_CONFIG_STR(flatArchive ? "flatDescription" : "Description", "");
  134. bool const IsOptional = APT_T_CONFIG_BOOL("Optional", true);
  135. bool const KeepCompressed = APT_T_CONFIG_BOOL("KeepCompressed", GzipIndex);
  136. bool const DefaultEnabled = APT_T_CONFIG_BOOL("DefaultEnabled", true);
  137. bool const UsePDiffs = APT_T_CONFIG_BOOL("PDiffs", E->UsePDiffs);
  138. std::string const CompressionTypes = APT_T_CONFIG_STR("CompressionTypes", DefCompressionTypes);
  139. #undef APT_T_CONFIG_BOOL
  140. #undef APT_T_CONFIG_STR
  141. if (tplMetaKey.empty())
  142. continue;
  143. for (std::vector<std::string>::const_iterator L = E->Languages.begin(); L != E->Languages.end(); ++L)
  144. {
  145. if (*L == "none" && tplMetaKey.find("$(LANGUAGE)") != std::string::npos)
  146. continue;
  147. for (std::vector<std::string>::const_iterator A = E->Architectures.begin(); A != E->Architectures.end(); ++A)
  148. {
  149. // available in templates
  150. std::map<std::string, std::string> Options;
  151. Options.insert(std::make_pair("SITE", Site));
  152. Options.insert(std::make_pair("RELEASE", Release));
  153. if (tplMetaKey.find("$(COMPONENT)") != std::string::npos)
  154. Options.insert(std::make_pair("COMPONENT", E->Name));
  155. if (tplMetaKey.find("$(LANGUAGE)") != std::string::npos)
  156. Options.insert(std::make_pair("LANGUAGE", *L));
  157. if (tplMetaKey.find("$(ARCHITECTURE)") != std::string::npos)
  158. Options.insert(std::make_pair("ARCHITECTURE", *A));
  159. else if (tplMetaKey.find("$(NATIVE_ARCHITECTURE)") != std::string::npos)
  160. Options.insert(std::make_pair("ARCHITECTURE", NativeArch));
  161. if (tplMetaKey.find("$(NATIVE_ARCHITECTURE)") != std::string::npos)
  162. Options.insert(std::make_pair("NATIVE_ARCHITECTURE", NativeArch));
  163. std::string MetaKey = tplMetaKey;
  164. std::string ShortDesc = tplShortDesc;
  165. std::string LongDesc = tplLongDesc;
  166. for (std::map<std::string, std::string>::const_iterator O = Options.begin(); O != Options.end(); ++O)
  167. {
  168. MetaKey = SubstVar(MetaKey, std::string("$(") + O->first + ")", O->second);
  169. ShortDesc = SubstVar(ShortDesc, std::string("$(") + O->first + ")", O->second);
  170. LongDesc = SubstVar(LongDesc, std::string("$(") + O->first + ")", O->second);
  171. }
  172. // not available in templates, but in the indextarget
  173. Options.insert(std::make_pair("BASE_URI", baseURI));
  174. Options.insert(std::make_pair("REPO_URI", URI));
  175. Options.insert(std::make_pair("TARGET_OF", Type));
  176. Options.insert(std::make_pair("CREATED_BY", *T));
  177. Options.insert(std::make_pair("PDIFFS", UsePDiffs ? "yes" : "no"));
  178. Options.insert(std::make_pair("DEFAULTENABLED", DefaultEnabled ? "yes" : "no"));
  179. Options.insert(std::make_pair("COMPRESSIONTYPES", CompressionTypes));
  180. IndexTarget Target(
  181. MetaKey,
  182. ShortDesc,
  183. LongDesc,
  184. Options.find("BASE_URI")->second + MetaKey,
  185. IsOptional,
  186. KeepCompressed,
  187. Options
  188. );
  189. IndexTargets.push_back(Target);
  190. if (tplMetaKey.find("$(ARCHITECTURE)") == std::string::npos)
  191. break;
  192. }
  193. if (tplMetaKey.find("$(LANGUAGE)") == std::string::npos)
  194. break;
  195. }
  196. }
  197. }
  198. }
  199. std::vector<IndexTarget> debReleaseIndex::GetIndexTargets() const
  200. {
  201. std::vector<IndexTarget> IndexTargets;
  202. GetIndexTargetsFor("deb-src", URI, Dist, d->DebSrcEntries, IndexTargets);
  203. GetIndexTargetsFor("deb", URI, Dist, d->DebEntries, IndexTargets);
  204. return IndexTargets;
  205. }
  206. /*}}}*/
  207. void debReleaseIndex::AddComponent(bool const isSrc, std::string const &Name,/*{{{*/
  208. std::vector<std::string> const &Targets,
  209. std::vector<std::string> const &Architectures,
  210. std::vector<std::string> Languages,
  211. bool const usePDiffs)
  212. {
  213. if (Languages.empty() == true)
  214. Languages.push_back("none");
  215. debReleaseIndexPrivate::debSectionEntry const entry = {
  216. Name, Targets, Architectures, Languages, usePDiffs
  217. };
  218. if (isSrc)
  219. d->DebSrcEntries.push_back(entry);
  220. else
  221. d->DebEntries.push_back(entry);
  222. }
  223. /*}}}*/
  224. bool debReleaseIndex::Load(std::string const &Filename, std::string * const ErrorText)/*{{{*/
  225. {
  226. LoadedSuccessfully = TRI_NO;
  227. FileFd Fd;
  228. if (OpenMaybeClearSignedFile(Filename, Fd) == false)
  229. return false;
  230. pkgTagFile TagFile(&Fd, Fd.Size());
  231. if (_error->PendingError() == true)
  232. {
  233. if (ErrorText != NULL)
  234. strprintf(*ErrorText, _("Unable to parse Release file %s"),Filename.c_str());
  235. return false;
  236. }
  237. pkgTagSection Section;
  238. const char *Start, *End;
  239. if (TagFile.Step(Section) == false)
  240. {
  241. if (ErrorText != NULL)
  242. strprintf(*ErrorText, _("No sections in Release file %s"), Filename.c_str());
  243. return false;
  244. }
  245. // FIXME: find better tag name
  246. SupportsAcquireByHash = Section.FindB("Acquire-By-Hash", false);
  247. Suite = Section.FindS("Suite");
  248. Codename = Section.FindS("Codename");
  249. bool FoundHashSum = false;
  250. for (int i=0;HashString::SupportedHashes()[i] != NULL; i++)
  251. {
  252. if (!Section.Find(HashString::SupportedHashes()[i], Start, End))
  253. continue;
  254. std::string Name;
  255. std::string Hash;
  256. unsigned long long Size;
  257. while (Start < End)
  258. {
  259. if (!parseSumData(Start, End, Name, Hash, Size))
  260. return false;
  261. if (Entries.find(Name) == Entries.end())
  262. {
  263. metaIndex::checkSum *Sum = new metaIndex::checkSum;
  264. Sum->MetaKeyFilename = Name;
  265. Sum->Size = Size;
  266. Sum->Hashes.FileSize(Size);
  267. APT_IGNORE_DEPRECATED(Sum->Hash = HashString(HashString::SupportedHashes()[i],Hash);)
  268. Entries[Name] = Sum;
  269. }
  270. Entries[Name]->Hashes.push_back(HashString(HashString::SupportedHashes()[i],Hash));
  271. FoundHashSum = true;
  272. }
  273. }
  274. if(FoundHashSum == false)
  275. {
  276. if (ErrorText != NULL)
  277. strprintf(*ErrorText, _("No Hash entry in Release file %s"), Filename.c_str());
  278. return false;
  279. }
  280. std::string const StrDate = Section.FindS("Date");
  281. if (RFC1123StrToTime(StrDate.c_str(), Date) == false)
  282. {
  283. if (ErrorText != NULL)
  284. strprintf(*ErrorText, _("Invalid 'Date' entry in Release file %s"), Filename.c_str());
  285. return false;
  286. }
  287. bool CheckValidUntil = _config->FindB("Acquire::Check-Valid-Until", true);
  288. if (d->CheckValidUntil == metaIndex::TRI_NO)
  289. CheckValidUntil = false;
  290. else if (d->CheckValidUntil == metaIndex::TRI_YES)
  291. CheckValidUntil = true;
  292. if (CheckValidUntil == true)
  293. {
  294. std::string const Label = Section.FindS("Label");
  295. std::string const StrValidUntil = Section.FindS("Valid-Until");
  296. // if we have a Valid-Until header in the Release file, use it as default
  297. if (StrValidUntil.empty() == false)
  298. {
  299. if(RFC1123StrToTime(StrValidUntil.c_str(), ValidUntil) == false)
  300. {
  301. if (ErrorText != NULL)
  302. strprintf(*ErrorText, _("Invalid 'Valid-Until' entry in Release file %s"), Filename.c_str());
  303. return false;
  304. }
  305. }
  306. // get the user settings for this archive and use what expires earlier
  307. time_t MaxAge = d->ValidUntilMax;
  308. if (MaxAge == 0)
  309. {
  310. MaxAge = _config->FindI("Acquire::Max-ValidTime", 0);
  311. if (Label.empty() == false)
  312. MaxAge = _config->FindI(("Acquire::Max-ValidTime::" + Label).c_str(), MaxAge);
  313. }
  314. time_t MinAge = d->ValidUntilMin;
  315. if (MinAge == 0)
  316. {
  317. MinAge = _config->FindI("Acquire::Min-ValidTime", 0);
  318. if (Label.empty() == false)
  319. MinAge = _config->FindI(("Acquire::Min-ValidTime::" + Label).c_str(), MinAge);
  320. }
  321. if (MinAge != 0 && ValidUntil != 0) {
  322. time_t const min_date = Date + MinAge;
  323. if (ValidUntil < min_date)
  324. ValidUntil = min_date;
  325. }
  326. if (MaxAge != 0) {
  327. time_t const max_date = Date + MaxAge;
  328. if (ValidUntil == 0 || ValidUntil > max_date)
  329. ValidUntil = max_date;
  330. }
  331. }
  332. LoadedSuccessfully = TRI_YES;
  333. return true;
  334. }
  335. /*}}}*/
  336. metaIndex * debReleaseIndex::UnloadedClone() const /*{{{*/
  337. {
  338. if (Trusted == TRI_NO)
  339. return new debReleaseIndex(URI, Dist, false);
  340. else if (Trusted == TRI_YES)
  341. return new debReleaseIndex(URI, Dist, true);
  342. else
  343. return new debReleaseIndex(URI, Dist);
  344. }
  345. /*}}}*/
  346. bool debReleaseIndex::parseSumData(const char *&Start, const char *End, /*{{{*/
  347. std::string &Name, std::string &Hash, unsigned long long &Size)
  348. {
  349. Name = "";
  350. Hash = "";
  351. Size = 0;
  352. /* Skip over the first blank */
  353. while ((*Start == '\t' || *Start == ' ' || *Start == '\n' || *Start == '\r')
  354. && Start < End)
  355. Start++;
  356. if (Start >= End)
  357. return false;
  358. /* Move EntryEnd to the end of the first entry (the hash) */
  359. const char *EntryEnd = Start;
  360. while ((*EntryEnd != '\t' && *EntryEnd != ' ')
  361. && EntryEnd < End)
  362. EntryEnd++;
  363. if (EntryEnd == End)
  364. return false;
  365. Hash.append(Start, EntryEnd-Start);
  366. /* Skip over intermediate blanks */
  367. Start = EntryEnd;
  368. while (*Start == '\t' || *Start == ' ')
  369. Start++;
  370. if (Start >= End)
  371. return false;
  372. EntryEnd = Start;
  373. /* Find the end of the second entry (the size) */
  374. while ((*EntryEnd != '\t' && *EntryEnd != ' ' )
  375. && EntryEnd < End)
  376. EntryEnd++;
  377. if (EntryEnd == End)
  378. return false;
  379. Size = strtoull (Start, NULL, 10);
  380. /* Skip over intermediate blanks */
  381. Start = EntryEnd;
  382. while (*Start == '\t' || *Start == ' ')
  383. Start++;
  384. if (Start >= End)
  385. return false;
  386. EntryEnd = Start;
  387. /* Find the end of the third entry (the filename) */
  388. while ((*EntryEnd != '\t' && *EntryEnd != ' ' &&
  389. *EntryEnd != '\n' && *EntryEnd != '\r')
  390. && EntryEnd < End)
  391. EntryEnd++;
  392. Name.append(Start, EntryEnd-Start);
  393. Start = EntryEnd; //prepare for the next round
  394. return true;
  395. }
  396. /*}}}*/
  397. bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool const &GetAll)/*{{{*/
  398. {
  399. std::vector<IndexTarget> const targets = GetIndexTargets();
  400. #define APT_TARGET(X) IndexTarget("", X, MetaIndexInfo(X), MetaIndexURI(X), false, false, std::map<std::string,std::string>())
  401. pkgAcqMetaClearSig * const TransactionManager = new pkgAcqMetaClearSig(Owner,
  402. APT_TARGET("InRelease"), APT_TARGET("Release"), APT_TARGET("Release.gpg"),
  403. targets, this);
  404. #undef APT_TARGET
  405. // special case for --print-uris
  406. if (GetAll)
  407. for (auto const &Target: targets)
  408. new pkgAcqIndex(Owner, TransactionManager, Target);
  409. return true;
  410. }
  411. /*}}}*/
  412. // ReleaseIndex::Set* TriState options /*{{{*/
  413. bool debReleaseIndex::SetTrusted(TriState const pTrusted)
  414. {
  415. if (Trusted == TRI_UNSET)
  416. Trusted = pTrusted;
  417. else if (Trusted != pTrusted)
  418. // TRANSLATOR: The first is an option name from sources.list manpage, the other two URI and Suite
  419. return _error->Error(_("Conflicting values set for option %s concerning source %s %s"), "Trusted", URI.c_str(), Dist.c_str());
  420. return true;
  421. }
  422. bool debReleaseIndex::SetCheckValidUntil(TriState const pCheckValidUntil)
  423. {
  424. if (d->CheckValidUntil == TRI_UNSET)
  425. d->CheckValidUntil = pCheckValidUntil;
  426. else if (d->CheckValidUntil != pCheckValidUntil)
  427. return _error->Error(_("Conflicting values set for option %s concerning source %s %s"), "Check-Valid-Until", URI.c_str(), Dist.c_str());
  428. return true;
  429. }
  430. bool debReleaseIndex::SetValidUntilMin(time_t const Valid)
  431. {
  432. if (d->ValidUntilMin == 0)
  433. d->ValidUntilMin = Valid;
  434. else if (d->ValidUntilMin != Valid)
  435. return _error->Error(_("Conflicting values set for option %s concerning source %s %s"), "Min-ValidTime", URI.c_str(), Dist.c_str());
  436. return true;
  437. }
  438. bool debReleaseIndex::SetValidUntilMax(time_t const Valid)
  439. {
  440. if (d->ValidUntilMax == 0)
  441. d->ValidUntilMax = Valid;
  442. else if (d->ValidUntilMax != Valid)
  443. return _error->Error(_("Conflicting values set for option %s concerning source %s %s"), "Max-ValidTime", URI.c_str(), Dist.c_str());
  444. return true;
  445. }
  446. bool debReleaseIndex::SetSignedBy(std::string const &pSignedBy)
  447. {
  448. if (SignedBy.empty() == true && pSignedBy.empty() == false)
  449. {
  450. if (pSignedBy[0] == '/') // no check for existence as we could be chrooting later or such things
  451. ; // absolute path to a keyring file
  452. else
  453. {
  454. // we could go all fancy and allow short/long/string matches as gpgv/apt-key does,
  455. // but fingerprints are harder to fake than the others and this option is set once,
  456. // not interactively all the time so easy to type is not really a concern.
  457. std::string finger = pSignedBy;
  458. finger.erase(std::remove(finger.begin(), finger.end(), ' '), finger.end());
  459. std::transform(finger.begin(), finger.end(), finger.begin(), ::toupper);
  460. if (finger.length() != 40 || finger.find_first_not_of("0123456789ABCDEF") != std::string::npos)
  461. return _error->Error(_("Invalid value set for option %s concerning source %s %s (%s)"), "Signed-By", URI.c_str(), Dist.c_str(), "not a fingerprint");
  462. }
  463. SignedBy = pSignedBy;
  464. }
  465. else if (SignedBy != pSignedBy)
  466. return _error->Error(_("Conflicting values set for option %s concerning source %s %s"), "Signed-By", URI.c_str(), Dist.c_str());
  467. return true;
  468. }
  469. /*}}}*/
  470. // ReleaseIndex::IsTrusted /*{{{*/
  471. bool debReleaseIndex::IsTrusted() const
  472. {
  473. if (Trusted == TRI_YES)
  474. return true;
  475. else if (Trusted == TRI_NO)
  476. return false;
  477. if(_config->FindB("APT::Authentication::TrustCDROM", false))
  478. if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
  479. return true;
  480. if (FileExists(MetaIndexFile("Release.gpg")))
  481. return true;
  482. return FileExists(MetaIndexFile("InRelease"));
  483. }
  484. /*}}}*/
  485. std::vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles() /*{{{*/
  486. {
  487. if (Indexes != NULL)
  488. return Indexes;
  489. Indexes = new std::vector<pkgIndexFile*>();
  490. bool const istrusted = IsTrusted();
  491. for (auto const &T: GetIndexTargets())
  492. {
  493. std::string const TargetName = T.Option(IndexTarget::CREATED_BY);
  494. if (TargetName == "Packages")
  495. Indexes->push_back(new debPackagesIndex(T, istrusted));
  496. else if (TargetName == "Sources")
  497. Indexes->push_back(new debSourcesIndex(T, istrusted));
  498. else if (TargetName == "Translations")
  499. Indexes->push_back(new debTranslationsIndex(T));
  500. }
  501. return Indexes;
  502. }
  503. /*}}}*/
  504. static bool ReleaseFileName(debReleaseIndex const * const That, std::string &ReleaseFile)/*{{{*/
  505. {
  506. ReleaseFile = That->MetaIndexFile("InRelease");
  507. bool releaseExists = false;
  508. if (FileExists(ReleaseFile) == true)
  509. releaseExists = true;
  510. else
  511. {
  512. ReleaseFile = That->MetaIndexFile("Release");
  513. if (FileExists(ReleaseFile))
  514. releaseExists = true;
  515. }
  516. return releaseExists;
  517. }
  518. /*}}}*/
  519. bool debReleaseIndex::Merge(pkgCacheGenerator &Gen,OpProgress * /*Prog*/) const/*{{{*/
  520. {
  521. std::string ReleaseFile;
  522. bool const releaseExists = ReleaseFileName(this, ReleaseFile);
  523. ::URI Tmp(URI);
  524. if (Gen.SelectReleaseFile(ReleaseFile, Tmp.Host) == false)
  525. return _error->Error("Problem with SelectReleaseFile %s", ReleaseFile.c_str());
  526. if (releaseExists == false)
  527. return true;
  528. FileFd Rel;
  529. // Beware: The 'Release' file might be clearsigned in case the
  530. // signature for an 'InRelease' file couldn't be checked
  531. if (OpenMaybeClearSignedFile(ReleaseFile, Rel) == false)
  532. return false;
  533. if (_error->PendingError() == true)
  534. return false;
  535. // Store the IMS information
  536. pkgCache::RlsFileIterator File = Gen.GetCurRlsFile();
  537. pkgCacheGenerator::Dynamic<pkgCache::RlsFileIterator> DynFile(File);
  538. // Rel can't be used as this is potentially a temporary file
  539. struct stat Buf;
  540. if (stat(ReleaseFile.c_str(), &Buf) != 0)
  541. return _error->Errno("fstat", "Unable to stat file %s", ReleaseFile.c_str());
  542. File->Size = Buf.st_size;
  543. File->mtime = Buf.st_mtime;
  544. pkgTagFile TagFile(&Rel, Rel.Size());
  545. pkgTagSection Section;
  546. if (_error->PendingError() == true || TagFile.Step(Section) == false)
  547. return false;
  548. std::string data;
  549. #define APT_INRELEASE(TYPE, TAG, STORE) \
  550. data = Section.FindS(TAG); \
  551. if (data.empty() == false) \
  552. { \
  553. map_stringitem_t const storage = Gen.StoreString(pkgCacheGenerator::TYPE, data); \
  554. STORE = storage; \
  555. }
  556. APT_INRELEASE(MIXED, "Suite", File->Archive)
  557. APT_INRELEASE(VERSIONNUMBER, "Version", File->Version)
  558. APT_INRELEASE(MIXED, "Origin", File->Origin)
  559. APT_INRELEASE(MIXED, "Codename", File->Codename)
  560. APT_INRELEASE(MIXED, "Label", File->Label)
  561. #undef APT_INRELEASE
  562. Section.FindFlag("NotAutomatic", File->Flags, pkgCache::Flag::NotAutomatic);
  563. Section.FindFlag("ButAutomaticUpgrades", File->Flags, pkgCache::Flag::ButAutomaticUpgrades);
  564. return !_error->PendingError();
  565. }
  566. /*}}}*/
  567. // ReleaseIndex::FindInCache - Find this index /*{{{*/
  568. pkgCache::RlsFileIterator debReleaseIndex::FindInCache(pkgCache &Cache, bool const ModifyCheck) const
  569. {
  570. std::string ReleaseFile;
  571. bool const releaseExists = ReleaseFileName(this, ReleaseFile);
  572. pkgCache::RlsFileIterator File = Cache.RlsFileBegin();
  573. for (; File.end() == false; ++File)
  574. {
  575. if (File->FileName == 0 || ReleaseFile != File.FileName())
  576. continue;
  577. // empty means the file does not exist by "design"
  578. if (ModifyCheck == false || (releaseExists == false && File->Size == 0))
  579. return File;
  580. struct stat St;
  581. if (stat(File.FileName(),&St) != 0)
  582. {
  583. if (_config->FindB("Debug::pkgCacheGen", false))
  584. std::clog << "ReleaseIndex::FindInCache - stat failed on " << File.FileName() << std::endl;
  585. return pkgCache::RlsFileIterator(Cache);
  586. }
  587. if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
  588. {
  589. if (_config->FindB("Debug::pkgCacheGen", false))
  590. std::clog << "ReleaseIndex::FindInCache - size (" << St.st_size << " <> " << File->Size
  591. << ") or mtime (" << St.st_mtime << " <> " << File->mtime
  592. << ") doesn't match for " << File.FileName() << std::endl;
  593. return pkgCache::RlsFileIterator(Cache);
  594. }
  595. return File;
  596. }
  597. return File;
  598. }
  599. /*}}}*/
  600. static std::vector<std::string> parsePlusMinusOptions(std::string const &Name, /*{{{*/
  601. std::map<std::string, std::string> const &Options, std::vector<std::string> const &defaultValues)
  602. {
  603. std::map<std::string, std::string>::const_iterator val = Options.find(Name);
  604. std::vector<std::string> Values;
  605. if (val != Options.end())
  606. Values = VectorizeString(val->second, ',');
  607. else
  608. Values = defaultValues;
  609. if ((val = Options.find(Name + "+")) != Options.end())
  610. {
  611. std::vector<std::string> const plus = VectorizeString(val->second, ',');
  612. std::copy_if(plus.begin(), plus.end(), std::back_inserter(Values), [&Values](std::string const &v) {
  613. return std::find(Values.begin(), Values.end(), v) == Values.end();
  614. });
  615. }
  616. if ((val = Options.find(Name + "-")) != Options.end())
  617. {
  618. std::vector<std::string> const minus = VectorizeString(val->second, ',');
  619. Values.erase(std::remove_if(Values.begin(), Values.end(), [&minus](std::string const &v) {
  620. return std::find(minus.begin(), minus.end(), v) != minus.end();
  621. }), Values.end());
  622. }
  623. return Values;
  624. }
  625. /*}}}*/
  626. class APT_HIDDEN debSLTypeDebian : public pkgSourceList::Type /*{{{*/
  627. {
  628. metaIndex::TriState GetTriStateOption(std::map<std::string, std::string>const &Options, char const * const name) const
  629. {
  630. std::map<std::string, std::string>::const_iterator const opt = Options.find(name);
  631. if (opt != Options.end())
  632. return StringToBool(opt->second, false) ? metaIndex::TRI_YES : metaIndex::TRI_NO;
  633. return metaIndex::TRI_DONTCARE;
  634. }
  635. time_t GetTimeOption(std::map<std::string, std::string>const &Options, char const * const name) const
  636. {
  637. std::map<std::string, std::string>::const_iterator const opt = Options.find(name);
  638. if (opt == Options.end())
  639. return 0;
  640. return strtoull(opt->second.c_str(), NULL, 10);
  641. }
  642. protected:
  643. bool CreateItemInternal(std::vector<metaIndex *> &List, std::string const &URI,
  644. std::string const &Dist, std::string const &Section,
  645. bool const &IsSrc, std::map<std::string, std::string> const &Options) const
  646. {
  647. debReleaseIndex *Deb = NULL;
  648. for (std::vector<metaIndex *>::const_iterator I = List.begin();
  649. I != List.end(); ++I)
  650. {
  651. // We only worry about debian entries here
  652. if (strcmp((*I)->GetType(), "deb") != 0)
  653. continue;
  654. /* This check insures that there will be only one Release file
  655. queued for all the Packages files and Sources files it
  656. corresponds to. */
  657. if ((*I)->GetURI() == URI && (*I)->GetDist() == Dist)
  658. {
  659. Deb = dynamic_cast<debReleaseIndex*>(*I);
  660. if (Deb != NULL)
  661. break;
  662. }
  663. }
  664. // No currently created Release file indexes this entry, so we create a new one.
  665. if (Deb == NULL)
  666. {
  667. Deb = new debReleaseIndex(URI, Dist);
  668. List.push_back(Deb);
  669. }
  670. std::vector<std::string> const alltargets = _config->FindVector(std::string("Acquire::IndexTargets::") + Name, "", true);
  671. std::vector<std::string> deftargets;
  672. deftargets.reserve(alltargets.size());
  673. std::copy_if(alltargets.begin(), alltargets.end(), std::back_inserter(deftargets), [&](std::string const &t) {
  674. std::string c = "Acquire::IndexTargets::";
  675. c.append(Name).append("::").append(t).append("::DefaultEnabled");
  676. return _config->FindB(c, true);
  677. });
  678. std::vector<std::string> mytargets = parsePlusMinusOptions("target", Options, deftargets);
  679. for (auto const &target : alltargets)
  680. {
  681. std::map<std::string, std::string>::const_iterator const opt = Options.find(target);
  682. if (opt == Options.end())
  683. continue;
  684. auto const tarItr = std::find(mytargets.begin(), mytargets.end(), target);
  685. bool const optValue = StringToBool(opt->second);
  686. if (optValue == true && tarItr == mytargets.end())
  687. mytargets.push_back(target);
  688. else if (optValue == false && tarItr != mytargets.end())
  689. mytargets.erase(std::remove(mytargets.begin(), mytargets.end(), target), mytargets.end());
  690. }
  691. bool UsePDiffs = _config->FindB("Acquire::PDiffs", true);
  692. {
  693. std::map<std::string, std::string>::const_iterator const opt = Options.find("pdiffs");
  694. if (opt != Options.end())
  695. UsePDiffs = StringToBool(opt->second);
  696. }
  697. Deb->AddComponent(
  698. IsSrc,
  699. Section,
  700. mytargets,
  701. parsePlusMinusOptions("arch", Options, APT::Configuration::getArchitectures()),
  702. parsePlusMinusOptions("lang", Options, APT::Configuration::getLanguages(true)),
  703. UsePDiffs
  704. );
  705. if (Deb->SetTrusted(GetTriStateOption(Options, "trusted")) == false ||
  706. Deb->SetCheckValidUntil(GetTriStateOption(Options, "check-valid-until")) == false ||
  707. Deb->SetValidUntilMax(GetTimeOption(Options, "valid-until-max")) == false ||
  708. Deb->SetValidUntilMin(GetTimeOption(Options, "valid-until-min")) == false)
  709. return false;
  710. std::map<std::string, std::string>::const_iterator const signedby = Options.find("signed-by");
  711. if (signedby == Options.end())
  712. {
  713. if (Deb->SetSignedBy("") == false)
  714. return false;
  715. }
  716. else
  717. {
  718. if (Deb->SetSignedBy(signedby->second) == false)
  719. return false;
  720. }
  721. return true;
  722. }
  723. debSLTypeDebian(char const * const Name, char const * const Label) : Type(Name, Label)
  724. {
  725. }
  726. };
  727. /*}}}*/
  728. class APT_HIDDEN debSLTypeDeb : public debSLTypeDebian /*{{{*/
  729. {
  730. public:
  731. bool CreateItem(std::vector<metaIndex *> &List, std::string const &URI,
  732. std::string const &Dist, std::string const &Section,
  733. std::map<std::string, std::string> const &Options) const APT_OVERRIDE
  734. {
  735. return CreateItemInternal(List, URI, Dist, Section, false, Options);
  736. }
  737. debSLTypeDeb() : debSLTypeDebian("deb", "Debian binary tree")
  738. {
  739. }
  740. };
  741. /*}}}*/
  742. class APT_HIDDEN debSLTypeDebSrc : public debSLTypeDebian /*{{{*/
  743. {
  744. public:
  745. bool CreateItem(std::vector<metaIndex *> &List, std::string const &URI,
  746. std::string const &Dist, std::string const &Section,
  747. std::map<std::string, std::string> const &Options) const APT_OVERRIDE
  748. {
  749. return CreateItemInternal(List, URI, Dist, Section, true, Options);
  750. }
  751. debSLTypeDebSrc() : debSLTypeDebian("deb-src", "Debian source tree")
  752. {
  753. }
  754. };
  755. /*}}}*/
  756. APT_HIDDEN debSLTypeDeb _apt_DebType;
  757. APT_HIDDEN debSLTypeDebSrc _apt_DebSrcType;