]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/deb/debmetaindex.cc
only warn about missing/invalid Date field for now
[apt.git] / apt-pkg / deb / debmetaindex.cc
index 419de12e8669817499b0acc012d79d562e89bd11..97f881269dc4b6923ec1040d88879c6edcf29985 100644 (file)
@@ -33,11 +33,13 @@ class APT_HIDDEN debReleaseIndexPrivate                                     /*{{{*/
    public:
    struct APT_HIDDEN debSectionEntry
    {
+      std::string sourcesEntry;
       std::string Name;
       std::vector<std::string> Targets;
       std::vector<std::string> Architectures;
       std::vector<std::string> Languages;
       bool UsePDiffs;
+      std::string UseByHash;
    };
 
    std::vector<debSectionEntry> DebEntries;
@@ -47,6 +49,9 @@ class APT_HIDDEN debReleaseIndexPrivate                                       /*{{{*/
    time_t ValidUntilMin;
    time_t ValidUntilMax;
 
+   std::vector<std::string> Architectures;
+   std::vector<std::string> NoSupportForAll;
+
    debReleaseIndexPrivate() : CheckValidUntil(metaIndex::TRI_UNSET), ValidUntilMin(0), ValidUntilMax(0) {}
 };
                                                                        /*}}}*/
@@ -133,6 +138,21 @@ static void GetIndexTargetsFor(char const * const Type, std::string const &URI,
         DefCompressionTypes = os.str();
       }
    }
+   std::string DefKeepCompressedAs;
+   {
+      std::vector<APT::Configuration::Compressor> comps = APT::Configuration::getCompressors();
+      if (comps.empty() == false)
+      {
+        std::sort(comps.begin(), comps.end(),
+              [](APT::Configuration::Compressor const &a, APT::Configuration::Compressor const &b) { return a.Cost < b.Cost; });
+        std::ostringstream os;
+        for (auto const &c : comps)
+           if (c.Cost != 0)
+              os << c.Extension.substr(1) << ' ';
+        DefKeepCompressedAs = os.str();
+      }
+      DefKeepCompressedAs += "uncompressed";
+   }
    std::string const NativeArch = _config->Find("APT::Architecture");
    bool const GzipIndex = _config->FindB("Acquire::GzipIndexes", false);
    for (std::vector<debReleaseIndexPrivate::debSectionEntry>::const_iterator E = entries.begin(); E != entries.end(); ++E)
@@ -148,12 +168,37 @@ static void GetIndexTargetsFor(char const * const Type, std::string const &URI,
         bool const KeepCompressed = APT_T_CONFIG_BOOL("KeepCompressed", GzipIndex);
         bool const DefaultEnabled = APT_T_CONFIG_BOOL("DefaultEnabled", true);
         bool const UsePDiffs = APT_T_CONFIG_BOOL("PDiffs", E->UsePDiffs);
+        std::string const UseByHash = APT_T_CONFIG_STR("By-Hash", E->UseByHash);
         std::string const CompressionTypes = APT_T_CONFIG_STR("CompressionTypes", DefCompressionTypes);
+        std::string KeepCompressedAs = APT_T_CONFIG_STR("KeepCompressedAs", "");
 #undef APT_T_CONFIG_BOOL
 #undef APT_T_CONFIG_STR
         if (tplMetaKey.empty())
            continue;
 
+        if (KeepCompressedAs.empty())
+           KeepCompressedAs = DefKeepCompressedAs;
+        else
+        {
+           std::vector<std::string> const defKeep = VectorizeString(DefKeepCompressedAs, ' ');
+           std::vector<std::string> const valKeep = VectorizeString(KeepCompressedAs, ' ');
+           std::vector<std::string> keep;
+           for (auto const &val : valKeep)
+           {
+              if (val.empty())
+                 continue;
+              if (std::find(defKeep.begin(), defKeep.end(), val) == defKeep.end())
+                 continue;
+              keep.push_back(val);
+           }
+           if (std::find(keep.begin(), keep.end(), "uncompressed") == keep.end())
+              keep.push_back("uncompressed");
+           std::ostringstream os;
+           std::copy(keep.begin(), keep.end()-1, std::ostream_iterator<std::string>(os, " "));
+           os << *keep.rbegin();
+           KeepCompressedAs = os.str();
+        }
+
         for (std::vector<std::string>::const_iterator L = E->Languages.begin(); L != E->Languages.end(); ++L)
         {
            if (*L == "none" && tplMetaKey.find("$(LANGUAGE)") != std::string::npos)
@@ -186,21 +231,84 @@ static void GetIndexTargetsFor(char const * const Type, std::string const &URI,
                  LongDesc = SubstVar(LongDesc, std::string("$(") + O->first + ")", O->second);
               }
 
+              {
+                 auto const dup = std::find_if(IndexTargets.begin(), IndexTargets.end(), [&](IndexTarget const &IT) {
+                    return MetaKey == IT.MetaKey && baseURI == IT.Option(IndexTarget::BASE_URI) &&
+                       E->sourcesEntry == IT.Option(IndexTarget::SOURCESENTRY) && *T == IT.Option(IndexTarget::CREATED_BY);
+                 });
+                 if (dup != IndexTargets.end())
+                 {
+                    if (tplMetaKey.find("$(ARCHITECTURE)") == std::string::npos)
+                       break;
+                    continue;
+                 }
+              }
+
+              {
+                 auto const dup = std::find_if(IndexTargets.begin(), IndexTargets.end(), [&](IndexTarget const &IT) {
+                    return MetaKey == IT.MetaKey && baseURI == IT.Option(IndexTarget::BASE_URI) &&
+                       E->sourcesEntry == IT.Option(IndexTarget::SOURCESENTRY) && *T != IT.Option(IndexTarget::CREATED_BY);
+                 });
+                 if (dup != IndexTargets.end())
+                 {
+                    std::string const dupT = dup->Option(IndexTarget::CREATED_BY);
+                    std::string const dupEntry = dup->Option(IndexTarget::SOURCESENTRY);
+                    //TRANSLATOR: an identifier like Packages; Releasefile key indicating
+                    // a file like main/binary-amd64/Packages; another identifier like Contents;
+                    // filename and linenumber of the sources.list entry currently parsed
+                    _error->Warning(_("Target %s wants to acquire the same file (%s) as %s from source %s"),
+                          T->c_str(), MetaKey.c_str(), dupT.c_str(), dupEntry.c_str());
+                    if (tplMetaKey.find("$(ARCHITECTURE)") == std::string::npos)
+                       break;
+                    continue;
+                 }
+              }
+
+              {
+                 auto const dup = std::find_if(IndexTargets.begin(), IndexTargets.end(), [&](IndexTarget const &T) {
+                    return MetaKey == T.MetaKey && baseURI == T.Option(IndexTarget::BASE_URI) &&
+                       E->sourcesEntry != T.Option(IndexTarget::SOURCESENTRY);
+                 });
+                 if (dup != IndexTargets.end())
+                 {
+                    std::string const dupEntry = dup->Option(IndexTarget::SOURCESENTRY);
+                    //TRANSLATOR: an identifier like Packages; Releasefile key indicating
+                    // a file like main/binary-amd64/Packages; filename and linenumber of
+                    // two sources.list entries
+                    _error->Warning(_("Target %s (%s) is configured multiple times in %s and %s"),
+                          T->c_str(), MetaKey.c_str(), dupEntry.c_str(), E->sourcesEntry.c_str());
+                    if (tplMetaKey.find("$(ARCHITECTURE)") == std::string::npos)
+                       break;
+                    continue;
+                 }
+              }
+
               // not available in templates, but in the indextarget
               Options.insert(std::make_pair("BASE_URI", baseURI));
               Options.insert(std::make_pair("REPO_URI", URI));
               Options.insert(std::make_pair("TARGET_OF", Type));
               Options.insert(std::make_pair("CREATED_BY", *T));
               Options.insert(std::make_pair("PDIFFS", UsePDiffs ? "yes" : "no"));
+              Options.insert(std::make_pair("BY_HASH", UseByHash));
               Options.insert(std::make_pair("DEFAULTENABLED", DefaultEnabled ? "yes" : "no"));
               Options.insert(std::make_pair("COMPRESSIONTYPES", CompressionTypes));
+              Options.insert(std::make_pair("KEEPCOMPRESSEDAS", KeepCompressedAs));
+              Options.insert(std::make_pair("SOURCESENTRY", E->sourcesEntry));
+
+              bool IsOpt = IsOptional;
+              if (IsOpt == false)
+              {
+                 auto const arch = Options.find("ARCHITECTURE");
+                 if (arch != Options.end() && arch->second == "all")
+                    IsOpt = true;
+              }
 
               IndexTarget Target(
                     MetaKey,
                     ShortDesc,
                     LongDesc,
                     Options.find("BASE_URI")->second + MetaKey,
-                    IsOptional,
+                    IsOpt,
                     KeepCompressed,
                     Options
                     );
@@ -227,16 +335,17 @@ std::vector<IndexTarget> debReleaseIndex::GetIndexTargets() const
    return IndexTargets;
 }
                                                                        /*}}}*/
-void debReleaseIndex::AddComponent(bool const isSrc, std::string const &Name,/*{{{*/
+void debReleaseIndex::AddComponent(std::string const &sourcesEntry,    /*{{{*/
+        bool const isSrc, std::string const &Name,
         std::vector<std::string> const &Targets,
         std::vector<std::string> const &Architectures,
         std::vector<std::string> Languages,
-        bool const usePDiffs)
+        bool const usePDiffs, std::string const &useByHash)
 {
    if (Languages.empty() == true)
       Languages.push_back("none");
    debReleaseIndexPrivate::debSectionEntry const entry = {
-      Name, Targets, Architectures, Languages, usePDiffs
+      sourcesEntry, Name, Targets, Architectures, Languages, usePDiffs, useByHash
    };
    if (isSrc)
       d->DebSrcEntries.push_back(entry);
@@ -253,7 +362,7 @@ bool debReleaseIndex::Load(std::string const &Filename, std::string * const Erro
       return false;
 
    pkgTagFile TagFile(&Fd, Fd.Size());
-   if (_error->PendingError() == true)
+   if (Fd.IsOpen() == false || Fd.Failed())
    {
       if (ErrorText != NULL)
         strprintf(*ErrorText, _("Unable to parse Release file %s"),Filename.c_str());
@@ -273,11 +382,23 @@ bool debReleaseIndex::Load(std::string const &Filename, std::string * const Erro
 
    Suite = Section.FindS("Suite");
    Codename = Section.FindS("Codename");
+   {
+      std::string const archs = Section.FindS("Architectures");
+      if (archs.empty() == false)
+        d->Architectures = VectorizeString(archs, ' ');
+   }
+   {
+      std::string const targets = Section.FindS("No-Support-for-Architecture-all");
+      if (targets.empty() == false)
+        d->NoSupportForAll = VectorizeString(targets, ' ');
+   }
 
    bool FoundHashSum = false;
-   for (int i=0;HashString::SupportedHashes()[i] != NULL; i++)
+   bool FoundStrongHashSum = false;
+   auto const SupportedHashes = HashString::SupportedHashes();
+   for (int i=0; SupportedHashes[i] != NULL; i++)
    {
-      if (!Section.Find(HashString::SupportedHashes()[i], Start, End))
+      if (!Section.Find(SupportedHashes[i], Start, End))
         continue;
 
       std::string Name;
@@ -288,17 +409,20 @@ bool debReleaseIndex::Load(std::string const &Filename, std::string * const Erro
         if (!parseSumData(Start, End, Name, Hash, Size))
            return false;
 
+        HashString const hs(SupportedHashes[i], Hash);
          if (Entries.find(Name) == Entries.end())
          {
             metaIndex::checkSum *Sum = new metaIndex::checkSum;
             Sum->MetaKeyFilename = Name;
             Sum->Size = Size;
            Sum->Hashes.FileSize(Size);
-            APT_IGNORE_DEPRECATED(Sum->Hash = HashString(HashString::SupportedHashes()[i],Hash);)
+            APT_IGNORE_DEPRECATED(Sum->Hash = hs;)
             Entries[Name] = Sum;
          }
-         Entries[Name]->Hashes.push_back(HashString(HashString::SupportedHashes()[i],Hash));
+         Entries[Name]->Hashes.push_back(hs);
          FoundHashSum = true;
+        if (FoundStrongHashSum == false && hs.usable() == true)
+           FoundStrongHashSum = true;
       }
    }
 
@@ -308,13 +432,18 @@ bool debReleaseIndex::Load(std::string const &Filename, std::string * const Erro
         strprintf(*ErrorText, _("No Hash entry in Release file %s"), Filename.c_str());
       return false;
    }
+   if(FoundStrongHashSum == false)
+   {
+      if (ErrorText != NULL)
+        strprintf(*ErrorText, _("No Hash entry in Release file %s, which is considered strong enough for security purposes"), Filename.c_str());
+      return false;
+   }
 
    std::string const StrDate = Section.FindS("Date");
    if (RFC1123StrToTime(StrDate.c_str(), Date) == false)
    {
-      if (ErrorText != NULL)
-        strprintf(*ErrorText, _("Invalid 'Date' entry in Release file %s"), Filename.c_str());
-      return false;
+      _error->Warning( _("Invalid 'Date' entry in Release file %s"), Filename.c_str());
+      Date = 0;
    }
 
    bool CheckValidUntil = _config->FindB("Acquire::Check-Valid-Until", true);
@@ -354,15 +483,18 @@ bool debReleaseIndex::Load(std::string const &Filename, std::string * const Erro
            MinAge = _config->FindI(("Acquire::Min-ValidTime::" + Label).c_str(), MinAge);
       }
 
-      if (MinAge != 0 && ValidUntil != 0) {
-        time_t const min_date = Date + MinAge;
-        if (ValidUntil < min_date)
-           ValidUntil = min_date;
-      }
-      if (MaxAge != 0) {
-        time_t const max_date = Date + MaxAge;
-        if (ValidUntil == 0 || ValidUntil > max_date)
-           ValidUntil = max_date;
+      if (MinAge != 0 || ValidUntil != 0 || MaxAge != 0)
+      {
+        if (MinAge != 0 && ValidUntil != 0) {
+           time_t const min_date = Date + MinAge;
+           if (ValidUntil < min_date)
+              ValidUntil = min_date;
+        }
+        if (MaxAge != 0 && Date != 0) {
+           time_t const max_date = Date + MaxAge;
+           if (ValidUntil == 0 || ValidUntil > max_date)
+              ValidUntil = max_date;
+        }
       }
    }
 
@@ -463,7 +595,7 @@ bool debReleaseIndex::SetTrusted(TriState const pTrusted)
       Trusted = pTrusted;
    else if (Trusted != pTrusted)
       // TRANSLATOR: The first is an option name from sources.list manpage, the other two URI and Suite
-      return _error->Error(_("Conflicting values set for option %s concerning source %s %s"), "Trusted", URI.c_str(), Dist.c_str());
+      return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Trusted", URI.c_str(), Dist.c_str());
    return true;
 }
 bool debReleaseIndex::SetCheckValidUntil(TriState const pCheckValidUntil)
@@ -471,7 +603,7 @@ bool debReleaseIndex::SetCheckValidUntil(TriState const pCheckValidUntil)
    if (d->CheckValidUntil == TRI_UNSET)
       d->CheckValidUntil = pCheckValidUntil;
    else if (d->CheckValidUntil != pCheckValidUntil)
-      return _error->Error(_("Conflicting values set for option %s concerning source %s %s"), "Check-Valid-Until", URI.c_str(), Dist.c_str());
+      return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Check-Valid-Until", URI.c_str(), Dist.c_str());
    return true;
 }
 bool debReleaseIndex::SetValidUntilMin(time_t const Valid)
@@ -479,7 +611,7 @@ bool debReleaseIndex::SetValidUntilMin(time_t const Valid)
    if (d->ValidUntilMin == 0)
       d->ValidUntilMin = Valid;
    else if (d->ValidUntilMin != Valid)
-      return _error->Error(_("Conflicting values set for option %s concerning source %s %s"), "Min-ValidTime", URI.c_str(), Dist.c_str());
+      return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Min-ValidTime", URI.c_str(), Dist.c_str());
    return true;
 }
 bool debReleaseIndex::SetValidUntilMax(time_t const Valid)
@@ -487,7 +619,7 @@ bool debReleaseIndex::SetValidUntilMax(time_t const Valid)
    if (d->ValidUntilMax == 0)
       d->ValidUntilMax = Valid;
    else if (d->ValidUntilMax != Valid)
-      return _error->Error(_("Conflicting values set for option %s concerning source %s %s"), "Max-ValidTime", URI.c_str(), Dist.c_str());
+      return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Max-ValidTime", URI.c_str(), Dist.c_str());
    return true;
 }
 bool debReleaseIndex::SetSignedBy(std::string const &pSignedBy)
@@ -505,12 +637,12 @@ bool debReleaseIndex::SetSignedBy(std::string const &pSignedBy)
         finger.erase(std::remove(finger.begin(), finger.end(), ' '), finger.end());
         std::transform(finger.begin(), finger.end(), finger.begin(), ::toupper);
         if (finger.length() != 40 || finger.find_first_not_of("0123456789ABCDEF") != std::string::npos)
-           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");
+           return _error->Error(_("Invalid value set for option %s regarding source %s %s (%s)"), "Signed-By", URI.c_str(), Dist.c_str(), "not a fingerprint");
       }
       SignedBy = pSignedBy;
    }
    else if (SignedBy != pSignedBy)
-      return _error->Error(_("Conflicting values set for option %s concerning source %s %s"), "Signed-By", URI.c_str(), Dist.c_str());
+      return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Signed-By", URI.c_str(), Dist.c_str());
    return true;
 }
                                                                        /*}}}*/
@@ -533,6 +665,20 @@ bool debReleaseIndex::IsTrusted() const
    return FileExists(MetaIndexFile("InRelease"));
 }
                                                                        /*}}}*/
+bool debReleaseIndex::IsArchitectureSupported(std::string const &arch) const/*{{{*/
+{
+   if (d->Architectures.empty())
+      return true;
+   return std::find(d->Architectures.begin(), d->Architectures.end(), arch) != d->Architectures.end();
+}
+                                                                       /*}}}*/
+bool debReleaseIndex::IsArchitectureAllSupportedFor(IndexTarget const &target) const/*{{{*/
+{
+   if (d->NoSupportForAll.empty())
+      return true;
+   return std::find(d->NoSupportForAll.begin(), d->NoSupportForAll.end(), target.Option(IndexTarget::CREATED_BY)) == d->NoSupportForAll.end();
+}
+                                                                       /*}}}*/
 std::vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles()         /*{{{*/
 {
    if (Indexes != NULL)
@@ -586,8 +732,6 @@ bool debReleaseIndex::Merge(pkgCacheGenerator &Gen,OpProgress * /*Prog*/) const/
    // signature for an 'InRelease' file couldn't be checked
    if (OpenMaybeClearSignedFile(ReleaseFile, Rel) == false)
       return false;
-   if (_error->PendingError() == true)
-      return false;
 
    // Store the IMS information
    pkgCache::RlsFileIterator File = Gen.GetCurRlsFile();
@@ -601,7 +745,7 @@ bool debReleaseIndex::Merge(pkgCacheGenerator &Gen,OpProgress * /*Prog*/) const/
 
    pkgTagFile TagFile(&Rel, Rel.Size());
    pkgTagSection Section;
-   if (_error->PendingError() == true || TagFile.Step(Section) == false)
+   if (Rel.IsOpen() == false || Rel.Failed() || TagFile.Step(Section) == false)
       return false;
 
    std::string data;
@@ -610,6 +754,7 @@ bool debReleaseIndex::Merge(pkgCacheGenerator &Gen,OpProgress * /*Prog*/) const/
    if (data.empty() == false) \
    { \
       map_stringitem_t const storage = Gen.StoreString(pkgCacheGenerator::TYPE, data); \
+      if (storage == 0) return false; \
       STORE = storage; \
    }
    APT_INRELEASE(MIXED, "Suite", File->Archive)
@@ -621,7 +766,7 @@ bool debReleaseIndex::Merge(pkgCacheGenerator &Gen,OpProgress * /*Prog*/) const/
    Section.FindFlag("NotAutomatic", File->Flags, pkgCache::Flag::NotAutomatic);
    Section.FindFlag("ButAutomaticUpgrades", File->Flags, pkgCache::Flag::ButAutomaticUpgrades);
 
-   return !_error->PendingError();
+   return true;
 }
                                                                        /*}}}*/
 // ReleaseIndex::FindInCache - Find this index                         /*{{{*/
@@ -672,6 +817,10 @@ static std::vector<std::string> parsePlusMinusOptions(std::string const &Name, /
    else
       Values = defaultValues;
 
+   // all is a very special architecture users shouldn't be concerned with explicitly
+   if (Name == "arch" && std::find(Values.begin(), Values.end(), "all") == Values.end())
+      Values.push_back("all");
+
    if ((val = Options.find(Name + "+")) != Options.end())
    {
       std::vector<std::string> const plus = VectorizeString(val->second, ',');
@@ -768,13 +917,27 @@ class APT_HIDDEN debSLTypeDebian : public pkgSourceList::Type             /*{{{*/
            UsePDiffs = StringToBool(opt->second);
       }
 
+      std::string UseByHash = _config->Find("APT::Acquire::By-Hash", "yes");
+      UseByHash = _config->Find("Acquire::By-Hash", UseByHash);
+      {
+        std::string const host = ::URI(URI).Host;
+        UseByHash = _config->Find("APT::Acquire::" + host + "::By-Hash", UseByHash);
+        UseByHash = _config->Find("Acquire::" + host + "::By-Hash", UseByHash);
+        std::map<std::string, std::string>::const_iterator const opt = Options.find("by-hash");
+        if (opt != Options.end())
+           UseByHash = opt->second;
+      }
+
+      auto const entry = Options.find("sourceslist-entry");
       Deb->AddComponent(
+           entry->second,
            IsSrc,
            Section,
            mytargets,
            parsePlusMinusOptions("arch", Options, APT::Configuration::getArchitectures()),
            parsePlusMinusOptions("lang", Options, APT::Configuration::getLanguages(true)),
-           UsePDiffs
+           UsePDiffs,
+           UseByHash
            );
 
       if (Deb->SetTrusted(GetTriStateOption(Options, "trusted")) == false ||