X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/5ad0096a4e19e191b59634e8a8817995ec4045ad..15e1ed52a741ff82deb0a4cd6150cf10e23b7368:/apt-pkg/sourcelist.cc diff --git a/apt-pkg/sourcelist.cc b/apt-pkg/sourcelist.cc index 0502f0e1d..df2e0b46d 100644 --- a/apt-pkg/sourcelist.cc +++ b/apt-pkg/sourcelist.cc @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -20,6 +21,8 @@ #include #include #include +#include +#include #include #include @@ -93,26 +96,39 @@ bool pkgSourceList::Type::ParseStanza(vector &List, /*{{{*/ if (Enabled.empty() == false && StringToBool(Enabled) == false) return true; - std::map mapping; + std::map > mapping; #define APT_PLUSMINUS(X, Y) \ - mapping.insert(std::make_pair(X, Y)); \ - mapping.insert(std::make_pair(X "Add", Y "+")); \ - mapping.insert(std::make_pair(X "Remove", Y "-")) + mapping.insert(std::make_pair(X, std::make_pair(Y, true))); \ + mapping.insert(std::make_pair(X "-Add", std::make_pair(Y "+", true))); \ + mapping.insert(std::make_pair(X "-Remove", std::make_pair(Y "-", true))) APT_PLUSMINUS("Architectures", "arch"); APT_PLUSMINUS("Languages", "lang"); APT_PLUSMINUS("Targets", "target"); #undef APT_PLUSMINUS - mapping.insert(std::make_pair("Trusted", "trusted")); - - for (std::map::const_iterator m = mapping.begin(); m != mapping.end(); ++m) + mapping.insert(std::make_pair("Trusted", std::make_pair("trusted", false))); + mapping.insert(std::make_pair("Check-Valid-Until", std::make_pair("check-valid-until", false))); + mapping.insert(std::make_pair("Valid-Until-Min", std::make_pair("valid-until-min", false))); + mapping.insert(std::make_pair("Valid-Until-Max", std::make_pair("valid-until-max", false))); + mapping.insert(std::make_pair("Signed-By", std::make_pair("signed-by", false))); + mapping.insert(std::make_pair("PDiffs", std::make_pair("pdiffs", false))); + mapping.insert(std::make_pair("By-Hash", std::make_pair("by-hash", false))); + + for (std::map >::const_iterator m = mapping.begin(); m != mapping.end(); ++m) if (Tags.Exists(m->first)) { - // for deb822 the " " is the delimiter, but the backend expects "," - std::string option = Tags.FindS(m->first); - std::replace(option.begin(), option.end(), ' ', ','); - Options[m->second] = option; + std::string option = Tags.FindS(m->first); + // for deb822 the " " is the delimiter, but the backend expects "," + if (m->second.second == true) + std::replace(option.begin(), option.end(), ' ', ','); + Options[m->second.first] = option; } + { + std::string entry; + strprintf(entry, "%s:%i", Fd.Name().c_str(), i); + Options["sourceslist-entry"] = entry; + } + // now create one item per suite/section string Suite = Tags.FindS("Suites"); Suite = SubstVar(Suite,"$(ARCH)",_config->Find("APT::Architecture")); @@ -180,6 +196,11 @@ bool pkgSourceList::Type::ParseLine(vector &List, // Parse option field if it exists // e.g.: [ option1=value1 option2=value2 ] map Options; + { + std::string entry; + strprintf(entry, "%s:%i", File.c_str(), CurLine); + Options["sourceslist-entry"] = entry; + } if (Buffer != 0 && Buffer[0] == '[') { ++Buffer; // ignore the [ @@ -269,6 +290,10 @@ pkgSourceList::~pkgSourceList() { for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I) delete *I; + SrcList.clear(); + for (auto F = VolatileFiles.begin(); F != VolatileFiles.end(); ++F) + delete (*F); + VolatileFiles.clear(); } /*}}}*/ // SourceList::ReadMainList - Read the main source list from etc /*{{{*/ @@ -287,21 +312,25 @@ bool pkgSourceList::ReadMainList() Reset(); // CNC:2003-11-28 - Entries in sources.list have priority over // entries in sources.list.d. - string Main = _config->FindFile("Dir::Etc::sourcelist"); - string Parts = _config->FindDir("Dir::Etc::sourceparts"); + string Main = _config->FindFile("Dir::Etc::sourcelist", "/dev/null"); + string Parts = _config->FindDir("Dir::Etc::sourceparts", "/dev/null"); if (RealFileExists(Main) == true) Res &= ReadAppend(Main); - else if (DirectoryExists(Parts) == false) + else if (DirectoryExists(Parts) == false && APT::String::Endswith(Parts, "/dev/null") == false) // Only warn if there are no sources.list.d. _error->WarningE("DirectoryExists", _("Unable to read %s"), Parts.c_str()); if (DirectoryExists(Parts) == true) Res &= ReadSourceDir(Parts); - else if (RealFileExists(Main) == false) + else if (Main.empty() == false && RealFileExists(Main) == false && + APT::String::Endswith(Parts, "/dev/null") == false) // Only warn if there is no sources.list file. _error->WarningE("RealFileExists", _("Unable to read %s"), Main.c_str()); + for (auto && file: _config->FindVector("APT::Sources::With")) + Res &= AddVolatileFile(file, nullptr); + return Res; } /*}}}*/ @@ -312,7 +341,7 @@ void pkgSourceList::Reset() { for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I) delete *I; - SrcList.erase(SrcList.begin(),SrcList.end()); + SrcList.clear(); } /*}}}*/ // SourceList::Read - Parse the sourcelist file /*{{{*/ @@ -334,7 +363,7 @@ bool pkgSourceList::ReadAppend(string const &File) else return ParseFileOldStyle(File); } - + /*}}}*/ // SourceList::ReadFileOldStyle - Read Traditional style sources.list /*{{{*/ // --------------------------------------------------------------------- /* */ @@ -375,7 +404,7 @@ bool pkgSourceList::ParseFileOldStyle(std::string const &File) continue; // Grok it - std::string const LineType = Buffer.substr(0, Buffer.find(' ')); + std::string const LineType = Buffer.substr(0, Buffer.find_first_of(" \t\v")); if (LineType.empty() || LineType == Buffer) return _error->Error(_("Malformed line %u in source list %s (type)"),CurLine,File.c_str()); @@ -394,18 +423,18 @@ bool pkgSourceList::ParseFileOldStyle(std::string const &File) /* Returns: the number of stanzas parsed*/ bool pkgSourceList::ParseFileDeb822(string const &File) { - pkgUserTagSection Tags; - unsigned int i = 1; - // see if we can read the file FileFd Fd(File, FileFd::ReadOnly); - pkgTagFile Sources(&Fd); - if (_error->PendingError() == true) - return _error->Error(_("Malformed stanza %u in source list %s (type)"),i,File.c_str()); + pkgTagFile Sources(&Fd, pkgTagFile::SUPPORT_COMMENTS); + if (Fd.IsOpen() == false || Fd.Failed()) + return _error->Error(_("Malformed stanza %u in source list %s (type)"),0,File.c_str()); // read step by step + pkgTagSection Tags; + unsigned int i = 0; while (Sources.Step(Tags) == true) { + ++i; if(Tags.Exists("Types") == false) return _error->Error(_("Malformed stanza %u in source list %s (type)"),i,File.c_str()); @@ -423,34 +452,32 @@ bool pkgSourceList::ParseFileDeb822(string const &File) if (!Parse->ParseStanza(SrcList, Tags, i, Fd)) return false; - - ++i; } } return true; } /*}}}*/ // SourceList::FindIndex - Get the index associated with a file /*{{{*/ -// --------------------------------------------------------------------- -/* */ +static bool FindInIndexFileContainer(std::vector const &Cont, pkgCache::PkgFileIterator const &File, pkgIndexFile *&Found) +{ + auto const J = std::find_if(Cont.begin(), Cont.end(), [&File](pkgIndexFile const * const J) { + return J->FindInCache(*File.Cache()) == File; + }); + if (J != Cont.end()) + { + Found = (*J); + return true; + } + return false; +} bool pkgSourceList::FindIndex(pkgCache::PkgFileIterator File, pkgIndexFile *&Found) const { for (const_iterator I = SrcList.begin(); I != SrcList.end(); ++I) - { - vector *Indexes = (*I)->GetIndexFiles(); - for (vector::const_iterator J = Indexes->begin(); - J != Indexes->end(); ++J) - { - if ((*J)->FindInCache(*File.Cache()) == File) - { - Found = (*J); - return true; - } - } - } + if (FindInIndexFileContainer(*(*I)->GetIndexFiles(), File, Found)) + return true; - return false; + return FindInIndexFileContainer(VolatileFiles, File, Found); } /*}}}*/ // SourceList::GetIndexes - Load the index files into the downloader /*{{{*/ @@ -499,11 +526,125 @@ time_t pkgSourceList::GetLastModifiedTime() List = GetListOfFilesInDir(Parts, "list", true); // calculate the time - time_t mtime_sources = GetModificationTime(Main); - for (vector::const_iterator I = List.begin(); I != List.end(); ++I) - mtime_sources = std::max(mtime_sources, GetModificationTime(*I)); - - return mtime_sources; + std::vector modtimes; + modtimes.reserve(1 + List.size()); + modtimes.push_back(GetModificationTime(Main)); + std::transform(List.begin(), List.end(), std::back_inserter(modtimes), GetModificationTime); + auto const maxmtime = std::max_element(modtimes.begin(), modtimes.end()); + return *maxmtime; } /*}}}*/ +std::vector pkgSourceList::GetVolatileFiles() const /*{{{*/ +{ + return VolatileFiles; +} + /*}}}*/ +void pkgSourceList::AddVolatileFile(pkgIndexFile * const File) /*{{{*/ +{ + if (File != nullptr) + VolatileFiles.push_back(File); +} + /*}}}*/ +static bool fileNameMatches(std::string const &filename, std::string const &idxtype)/*{{{*/ +{ + for (auto && type: APT::Configuration::getCompressionTypes()) + { + if (type == "uncompressed") + { + if (filename == idxtype || APT::String::Endswith(filename, '_' + idxtype)) + return true; + } + else if (filename == idxtype + '.' + type || + APT::String::Endswith(filename, '_' + idxtype + '.' + type)) + return true; + } + return false; +} + /*}}}*/ +bool pkgSourceList::AddVolatileFile(std::string const &File, std::vector * const VolatileCmdL)/*{{{*/ +{ + // Note: FileExists matches directories and links, too! + if (File.empty() || FileExists(File) == false) + return false; + std::string const ext = flExtension(File); + // udeb is not included as installing it is usually a mistake rather than intended + if (ext == "deb" || ext == "ddeb") + AddVolatileFile(new debDebPkgFileIndex(File)); + else if (ext == "dsc") + AddVolatileFile(new debDscFileIndex(File)); + else if (FileExists(flCombine(File, "debian/control"))) + AddVolatileFile(new debDscFileIndex(flCombine(File, "debian/control"))); + else if (ext == "changes") + { + debDscRecordParser changes(File, nullptr); + std::vector fileslst; + if (changes.Files2(fileslst) == false || fileslst.empty()) + return false; + auto const basedir = flNotFile(File); + for (auto && file: fileslst) + { + auto const name = flCombine(basedir, file.Path); + AddVolatileFile(name, VolatileCmdL); + if (file.Hashes.VerifyFile(name) == false) + return _error->Error("The file %s does not match with the hashes in the %s file!", name.c_str(), File.c_str()); + } + return true; + } + else + { + auto const filename = flNotDir(File); + auto const Target = IndexTarget(File, filename, File, "file:" + File, false, true, { + { "FILENAME", File }, + { "REPO_URI", "file:" + flAbsPath(flNotFile(File)) + '/' }, + { "COMPONENT", "volatile-packages-file" }, + }); + if (fileNameMatches(filename, "Packages")) + AddVolatileFile(new debPackagesIndex(Target, true)); + else if (fileNameMatches(filename, "Sources")) + AddVolatileFile(new debSourcesIndex(Target, true)); + else + return false; + } + + if (VolatileCmdL != nullptr) + VolatileCmdL->push_back(File); + return true; +} +bool pkgSourceList::AddVolatileFile(std::string const &File) +{ + return AddVolatileFile(File, nullptr); +} + /*}}}*/ +void pkgSourceList::AddVolatileFiles(CommandLine &CmdL, std::vector * const VolatileCmdL)/*{{{*/ +{ + std::remove_if(CmdL.FileList + 1, CmdL.FileList + 1 + CmdL.FileSize(), [&](char const * const I) { + if (I != nullptr && (I[0] == '/' || (I[0] == '.' && (I[1] == '\0' || (I[1] == '.' && (I[2] == '\0' || I[2] == '/')) || I[1] == '/')))) + { + if (AddVolatileFile(I, VolatileCmdL)) + ; + else + _error->Error(_("Unsupported file %s given on commandline"), I); + return true; + } + return false; + }); +} +void pkgSourceList::AddVolatileFiles(CommandLine &CmdL, std::vector * const VolatileCmdL) +{ + std::remove_if(CmdL.FileList + 1, CmdL.FileList + 1 + CmdL.FileSize(), [&](char const * const I) { + if (I != nullptr && (I[0] == '/' || (I[0] == '.' && (I[1] == '\0' || (I[1] == '.' && (I[2] == '\0' || I[2] == '/')) || I[1] == '/')))) + { + if (AddVolatileFile(I)) + { + if (VolatileCmdL != nullptr) + VolatileCmdL->push_back(I); + } + else + _error->Error(_("Unsupported file %s given on commandline"), I); + return true; + } + return false; + }); +} + /*}}}*/