From 61843f539513b3e661dac55717e6d7cc0b8f9b0c Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 18 Jul 2013 16:52:31 +0200 Subject: skip all Description fields in apt-cache, not just first MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Given a Packages file like: […] Description: foo bar baz moo moo moo Multi-Arch: foreign Description-md5: 97e204a9f4ad8c681dbd54ec7c505251 […] We have to display the Multi-Arch flag field as well as the fields after the Description-md5, but not this field itself, as we already have one printed alongside the Description we display. Closes: 717254 --- cmdline/apt-cache.cc | 52 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 17 deletions(-) (limited to 'cmdline') diff --git a/cmdline/apt-cache.cc b/cmdline/apt-cache.cc index fb4467c2c..336ac544b 100644 --- a/cmdline/apt-cache.cc +++ b/cmdline/apt-cache.cc @@ -1127,6 +1127,22 @@ bool Dotty(CommandLine &CmdL) // --------------------------------------------------------------------- /* This displays the package record from the proper package index file. It is not used by DumpAvail for performance reasons. */ + +static unsigned char const* skipDescriptionFields(unsigned char const * DescP) +{ + while ((DescP = (unsigned char*)strchr((char*)DescP, '\n')) != NULL) + { + if (DescP[1] == ' ') + DescP += 2; + else if (strncmp((char*)DescP, "\nDescription", strlen("\nDescription")) == 0) + DescP += strlen("\nDescription"); + else + break; + } + if (DescP != NULL) + ++DescP; + return DescP; +} bool DisplayRecord(pkgCacheFile &CacheFile, pkgCache::VerIterator V) { pkgCache *Cache = CacheFile.GetPkgCache(); @@ -1184,32 +1200,34 @@ bool DisplayRecord(pkgCacheFile &CacheFile, pkgCache::VerIterator V) cout << std::endl << "Description-md5: " << Desc.md5() << std::endl; // Find the first field after the description (if there is any) - while ((DescP = (unsigned char*)strchr((char*)DescP, '\n')) != NULL) - { - if (DescP[1] == ' ') - DescP += 2; - else if (strncmp((char*)DescP, "\nDescription", strlen("\nDescription")) == 0) - DescP += strlen("\nDescription"); - else - break; - } - if (DescP != NULL) - ++DescP; + DescP = skipDescriptionFields(DescP); } - // if we have no translation, we found a lonely Description-md5, so don't skip it + // else we have no translation, so we found a lonely Description-md5 -> don't skip it - if (DescP != NULL) + // write the rest of the buffer, but skip mixed in Descriptions* fields + while (DescP != NULL) { - // write the rest of the buffer - const unsigned char *end=&Buffer[V.FileList()->Size]; - if (fwrite(DescP,1,end-DescP,stdout) < (size_t)(end-DescP)) + const unsigned char * const Start = DescP; + const unsigned char *End = (unsigned char*)strstr((char*)DescP, "\nDescription"); + if (End == NULL) + { + End = &Buffer[V.FileList()->Size]; + DescP = NULL; + } + else + { + ++End; // get the newline into the output + DescP = skipDescriptionFields(End + strlen("Description")); + } + size_t const length = End - Start; + if (fwrite(Start, 1, length, stdout) < length) { delete [] Buffer; return false; } } - // write a final newline (after the description) + // write a final newline after the last field cout< Date: Thu, 18 Jul 2013 19:22:29 +0200 Subject: fix 'apt-cache search' crash with missing description Beside the earlier fixed 'apt-cache show', 'showpkg' and 'search' deal with descriptions. 'showpkg' was fixed by fixing the cache generation for 'show', but 'search' still segfaulted. On the upside, it doesn't segfault any longer, on the downside, if a package has no description at all (aka: not in the Packages file and not in a Translation-* file) the package can't be found with 'search', even if we search only by name. That is a shortcoming in the code, but fixing it means rewriting it completely for dubious gain at best. So this commit just skips packages without a description and is done. Closes: 647590 --- cmdline/apt-cache.cc | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'cmdline') diff --git a/cmdline/apt-cache.cc b/cmdline/apt-cache.cc index 336ac544b..5d1ee5615 100644 --- a/cmdline/apt-cache.cc +++ b/cmdline/apt-cache.cc @@ -1130,12 +1130,14 @@ bool Dotty(CommandLine &CmdL) static unsigned char const* skipDescriptionFields(unsigned char const * DescP) { + char const * const TagName = "\nDescription"; + size_t const TagLen = strlen(TagName); while ((DescP = (unsigned char*)strchr((char*)DescP, '\n')) != NULL) { if (DescP[1] == ' ') DescP += 2; - else if (strncmp((char*)DescP, "\nDescription", strlen("\nDescription")) == 0) - DescP += strlen("\nDescription"); + else if (strncmp((char*)DescP, TagName, TagLen) == 0) + DescP += TagLen; else break; } @@ -1166,11 +1168,12 @@ bool DisplayRecord(pkgCacheFile &CacheFile, pkgCache::VerIterator V) if (PkgF.Open(I.FileName(), FileFd::ReadOnly, FileFd::Extension) == false) return false; - // Read the record - unsigned char *Buffer = new unsigned char[Cache->HeaderP->MaxVerFileSize+1]; - Buffer[V.FileList()->Size] = '\n'; - if (PkgF.Seek(V.FileList()->Offset) == false || - PkgF.Read(Buffer,V.FileList()->Size) == false) + // Read the record (and ensure that it ends with a newline and NUL) + unsigned char *Buffer = new unsigned char[Cache->HeaderP->MaxVerFileSize+2]; + Buffer[Vf->Size] = '\n'; + Buffer[Vf->Size+1] = '\0'; + if (PkgF.Seek(Vf->Offset) == false || + PkgF.Read(Buffer,Vf->Size) == false) { delete [] Buffer; return false; @@ -1181,7 +1184,7 @@ bool DisplayRecord(pkgCacheFile &CacheFile, pkgCache::VerIterator V) if (DescP != NULL) ++DescP; else - DescP = Buffer + V.FileList()->Size; + DescP = Buffer + Vf->Size; // Write all but Description if (fwrite(Buffer,1,DescP - Buffer,stdout) < (size_t)(DescP - Buffer)) @@ -1211,7 +1214,7 @@ bool DisplayRecord(pkgCacheFile &CacheFile, pkgCache::VerIterator V) const unsigned char *End = (unsigned char*)strstr((char*)DescP, "\nDescription"); if (End == NULL) { - End = &Buffer[V.FileList()->Size]; + End = &Buffer[Vf->Size]; DescP = NULL; } else @@ -1318,7 +1321,11 @@ bool Search(CommandLine &CmdL) pkgCache::VerIterator V = Plcy->GetCandidateVer(P); if (V.end() == false) { - DFList[G->ID].Df = V.TranslatedDescription().FileList(); + pkgCache::DescIterator const D = V.TranslatedDescription(); + //FIXME: packages without a description can't be found + if (D.end() == true) + continue; + DFList[G->ID].Df = D.FileList(); DFList[G->ID].ID = G->ID; } @@ -1333,7 +1340,11 @@ bool Search(CommandLine &CmdL) continue; unsigned long id = Prv.OwnerPkg().Group()->ID; - DFList[id].Df = V.TranslatedDescription().FileList(); + pkgCache::DescIterator const D = V.TranslatedDescription(); + //FIXME: packages without a description can't be found + if (D.end() == true) + continue; + DFList[id].Df = D.FileList(); DFList[id].ID = id; size_t const PrvPatternOffset = id * NumPatterns; -- cgit v1.2.3 From d832304e06838275446a0b787899591061f39160 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Fri, 26 Jul 2013 11:42:02 +0200 Subject: use FileFd::Write instead of fwrite for errors Using the static FileFd::Write method gives us error messages for free so we use it here to avoid failing silently (with a fail silent error). Git-Dch: Ignore --- cmdline/apt-cache.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'cmdline') diff --git a/cmdline/apt-cache.cc b/cmdline/apt-cache.cc index 5d1ee5615..e847de875 100644 --- a/cmdline/apt-cache.cc +++ b/cmdline/apt-cache.cc @@ -1187,7 +1187,8 @@ bool DisplayRecord(pkgCacheFile &CacheFile, pkgCache::VerIterator V) DescP = Buffer + Vf->Size; // Write all but Description - if (fwrite(Buffer,1,DescP - Buffer,stdout) < (size_t)(DescP - Buffer)) + size_t const length = DescP - Buffer; + if (length != 0 && FileFd::Write(STDOUT_FILENO, Buffer, length) == false) { delete [] Buffer; return false; @@ -1223,7 +1224,7 @@ bool DisplayRecord(pkgCacheFile &CacheFile, pkgCache::VerIterator V) DescP = skipDescriptionFields(End + strlen("Description")); } size_t const length = End - Start; - if (fwrite(Start, 1, length, stdout) < length) + if (length != 0 && FileFd::Write(STDOUT_FILENO, Start, length) == false) { delete [] Buffer; return false; @@ -1232,8 +1233,8 @@ bool DisplayRecord(pkgCacheFile &CacheFile, pkgCache::VerIterator V) // write a final newline after the last field cout< Date: Thu, 1 Aug 2013 12:58:20 +0200 Subject: unset LANGUAGE for showing [Y/n] answer hints nl_langinfo is used to acquire the YESEXPR of the language used, but it will return the one from LC_MESSAGES, which might be different from the language chosen for display of the question (based on LANGUAGE) so this commit removes the [Y/n] help text from the questions itself and moves it to the prompt creation in which the usage of LC_MESSAGES is forced for it, so that the helptext shown actually represents the characters accepted as input for the question. There is still room for problems of course starting with an untranslated "[Y/n]" but a translated YESEXPR or the problem that the question is asked in a completely different language which might have a conflicting definition of [Y/n] input or the user simple ignores the helptext and assumes that an answer matching the question language is accepted, but the mayority of users will never have this problem to begin with, so we should be fine (or at least a bit finer than before). Closes nothing really, but should at least help a bit with bugs like deb:194614, deb:471102, lp:1205578, and countless others. --- cmdline/apt-get.cc | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'cmdline') diff --git a/cmdline/apt-get.cc b/cmdline/apt-get.cc index 73b396795..4b7691d93 100644 --- a/cmdline/apt-get.cc +++ b/cmdline/apt-get.cc @@ -130,13 +130,42 @@ class CacheFile : public pkgCacheFile /* Returns true on a Yes.*/ bool YnPrompt(bool Default=true) { + /* nl_langinfo does not support LANGUAGE setting, so we unset it here + to have the help-message (hopefully) match the expected characters */ + char * language = getenv("LANGUAGE"); + if (language != NULL) + language = strdup(language); + if (language != NULL) + unsetenv("LANGUAGE"); + + if (Default == true) + // TRANSLATOR: Yes/No question help-text: defaulting to Y[es] + // e.g. "Do you want to continue? [Y/n] " + // The user has to answer with an input matching the + // YESEXPR/NOEXPR defined in your l10n. + c2out << " " << _("[Y/n]") << " " << std::flush; + else + // TRANSLATOR: Yes/No question help-text: defaulting to N[o] + // e.g. "Should this file be removed? [y/N] " + // The user has to answer with an input matching the + // YESEXPR/NOEXPR defined in your l10n. + c2out << " " << _("[y/N]") << " " << std::flush; + + if (language != NULL) + { + setenv("LANGUAGE", language, 0); + free(language); + } + if (_config->FindB("APT::Get::Assume-Yes",false) == true) { + // TRANSLATOR: "Yes" answer printed for a yes/no question if --assume-yes is set c1out << _("Y") << endl; return true; } else if (_config->FindB("APT::Get::Assume-No",false) == true) { + // TRANSLATOR: "No" answer printed for a yes/no question if --assume-no is set c1out << _("N") << endl; return false; } @@ -1076,7 +1105,7 @@ static bool CheckAuth(pkgAcquire& Fetcher) if (_config->FindI("quiet",0) < 2 && _config->FindB("APT::Get::Assume-Yes",false) == false) { - c2out << _("Install these packages without verification [y/N]? ") << flush; + c2out << _("Install these packages without verification?") << flush; if (!YnPrompt(false)) return _error->Error(_("Some packages could not be authenticated")); @@ -1281,8 +1310,8 @@ bool InstallPackages(CacheFile &Cache,bool ShwKept,bool Ask = true, if (_config->FindI("quiet",0) < 2 && _config->FindB("APT::Get::Assume-Yes",false) == false) { - c2out << _("Do you want to continue [Y/n]? ") << flush; - + c2out << _("Do you want to continue?") << flush; + if (YnPrompt() == false) { c2out << _("Abort.") << endl; -- cgit v1.2.3