Skip to content

Commit

Permalink
Replace PrintStatus with SendMessage usage
Browse files Browse the repository at this point in the history
varg API is a nightmare as the symbols seems different on ever other
arch, but more importantly SendMessage does a few checks on the content
of the message and it is all outputted via C++ iostreams and not mixed
in FILE* which is handy for overriding the streams.
  • Loading branch information
DonKult committed Feb 4, 2021
1 parent 35af71b commit 96dc40b
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 43 deletions.
44 changes: 38 additions & 6 deletions apt-pkg/acquire-method.cc
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,25 @@ void pkgAcqMethod::PrintStatus(char const * const header, const char* Format,
void pkgAcqMethod::Log(const char *Format,...)
{
va_list args;
va_start(args,Format);
PrintStatus("101 Log", Format, args);
va_end(args);
ssize_t size = 400;
std::ostringstream outstr;
while (true) {
bool ret;
va_start(args,Format);
ret = iovprintf(outstr, Format, args, size);
va_end(args);
if (ret == true)
break;
}
std::unordered_map<std::string, std::string> fields;
if (Queue != 0)
try_emplace(fields, "URI", Queue->Uri);
else
try_emplace(fields, "URI", "<UNKNOWN>");
if (not UsedMirror.empty())
try_emplace(fields, "UsedMirror", UsedMirror);
try_emplace(fields, "Message", outstr.str());
SendMessage("101 Log", std::move(fields));
}
/*}}}*/
// AcqMethod::Status - Send a status message /*{{{*/
Expand All @@ -493,9 +509,25 @@ void pkgAcqMethod::Log(const char *Format,...)
void pkgAcqMethod::Status(const char *Format,...)
{
va_list args;
va_start(args,Format);
PrintStatus("102 Status", Format, args);
va_end(args);
ssize_t size = 400;
std::ostringstream outstr;
while (true) {
bool ret;
va_start(args,Format);
ret = iovprintf(outstr, Format, args, size);
va_end(args);
if (ret == true)
break;
}
std::unordered_map<std::string, std::string> fields;
if (Queue != 0)
try_emplace(fields, "URI", Queue->Uri);
else
try_emplace(fields, "URI", "<UNKNOWN>");
if (not UsedMirror.empty())
try_emplace(fields, "UsedMirror", UsedMirror);
try_emplace(fields, "Message", outstr.str());
SendMessage("102 Status", std::move(fields));
}
/*}}}*/
// AcqMethod::Redirect - Send a redirect message /*{{{*/
Expand Down
2 changes: 1 addition & 1 deletion apt-pkg/acquire-method.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class APT_PUBLIC pkgAcqMethod
bool MediaFail(std::string Required,std::string Drive);
virtual void Exit() {};

void PrintStatus(char const * const header, const char* Format, va_list &args) const;
APT_DEPRECATED_MSG("Use SendMessage instead") void PrintStatus(char const * const header, const char* Format, va_list &args) const;

public:
enum CnfFlags
Expand Down
15 changes: 0 additions & 15 deletions apt-pkg/contrib/gpgv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,6 @@ static bool operator!=(LineBuffer const &buf, APT::StringView const exp) noexcep
And as a cherry on the cake, we use our apt-key wrapper to do part
of the lifting in regards to merging keyrings. Fun for the whole family.
*/
static bool iovprintf(std::ostream &out, const char *format,
va_list &args, ssize_t &size) {
auto S = make_unique_char(malloc(size));
ssize_t const n = vsnprintf(S.get(), size, format, args);
if (n > -1 && n < size) {
out << S.get();
return true;
} else {
if (n > -1)
size = n + 1;
else
size *= 2;
}
return false;
}
static void APT_PRINTF(4) apt_error(std::ostream &outterm, int const statusfd, int fd[2], const char *format, ...)
{
std::ostringstream outstr;
Expand Down
11 changes: 5 additions & 6 deletions apt-pkg/contrib/strutl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <limits>
#include <locale>
#include <sstream>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
Expand Down Expand Up @@ -1414,21 +1415,19 @@ unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin,
// ---------------------------------------------------------------------
/* This is used to make the internationalization strings easier to translate
and to allow reordering of parameters */
static bool iovprintf(ostream &out, const char *format,
bool iovprintf(std::ostream &out, const char *format,
va_list &args, ssize_t &size) {
char *S = (char*)malloc(size);
ssize_t const n = vsnprintf(S, size, format, args);
auto S = std::unique_ptr<char,decltype(&free)>{static_cast<char*>(malloc(size)), &free};
ssize_t const n = vsnprintf(S.get(), size, format, args);
if (n > -1 && n < size) {
out << S;
free(S);
out << S.get();
return true;
} else {
if (n > -1)
size = n + 1;
else
size *= 2;
}
free(S);
return false;
}
void ioprintf(ostream &out,const char *format,...)
Expand Down
2 changes: 2 additions & 0 deletions apt-pkg/contrib/strutl.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ APT_PUBLIC std::vector<std::string> StringSplit(std::string const &input,
std::string const &sep,
unsigned int maxsplit=std::numeric_limits<unsigned int>::max()) APT_PURE;


APT_HIDDEN bool iovprintf(std::ostream &out, const char *format, va_list &args, ssize_t &size);
APT_PUBLIC void ioprintf(std::ostream &out,const char *format,...) APT_PRINTF(2);
APT_PUBLIC void strprintf(std::string &out,const char *format,...) APT_PRINTF(2);
APT_PUBLIC char *safe_snprintf(char *Buffer,char *End,const char *Format,...) APT_PRINTF(3);
Expand Down
30 changes: 18 additions & 12 deletions methods/aptmethod.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,11 @@ class aptMethod : public pkgAcqMethod

rc = seccomp_load(ctx);
if (rc == -EINVAL)
Warning("aptMethod::Configuration: could not load seccomp policy: %s", strerror(-rc));
{
std::string msg;
strprintf(msg, "aptMethod::Configuration: could not load seccomp policy: %s", strerror(-rc));
Warning(std::move(msg));
}
else if (rc != 0)
return _error->FatalE("aptMethod::Configuration", "could not load seccomp policy: %s", strerror(-rc));

Expand Down Expand Up @@ -380,12 +384,17 @@ class aptMethod : public pkgAcqMethod
return true;
}

void Warning(const char *Format,...)
void Warning(std::string &&msg)
{
va_list args;
va_start(args,Format);
PrintStatus("104 Warning", Format, args);
va_end(args);
std::unordered_map<std::string, std::string> fields;
if (Queue != 0)
fields.emplace("URI", Queue->Uri);
else
fields.emplace("URI", "<UNKNOWN>");
if (not UsedMirror.empty())
fields.emplace("UsedMirror", UsedMirror);
fields.emplace("Message", std::move(msg));
SendMessage("104 Warning", std::move(fields));
}

std::vector<std::string> methodNames;
Expand Down Expand Up @@ -560,14 +569,11 @@ class aptAuthConfMethod : public aptMethod
result &= MaybeAddAuth(*authconf, uri);
}

if (not _error->empty())
while (not _error->empty())
{
std::string message;
while (not _error->empty())
{
_error->PopMessage(message);
Warning("%s", message.c_str());
}
_error->PopMessage(message);
Warning(std::move(message));
}
_error->RevertToStack();

Expand Down
4 changes: 3 additions & 1 deletion methods/basehttp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,9 @@ int BaseHttpMethod::Loop()
// yes, he did! Disable pipelining and rewrite queue
if (Server->Pipeline == true)
{
Warning(_("Automatically disabled %s due to incorrect response from server/proxy. (man 5 apt.conf)"), "Acquire::http::Pipeline-Depth");
std::string msg;
strprintf(msg, _("Automatically disabled %s due to incorrect response from server/proxy. (man 5 apt.conf)"), "Acquire::http::Pipeline-Depth");
Warning(std::move(msg));
Server->Pipeline = false;
Server->PipelineAllowed = false;
// we keep the PipelineDepth value so that the rest of the queue can be fixed up as well
Expand Down
8 changes: 6 additions & 2 deletions methods/gpgv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ string GPGVMethod::VerifyGetSigners(const char *file, const char *outfile,
SubKeyMapping[tokens[9]].emplace_back(sig);
}
else if (strncmp(buffer, APTKEYWARNING, sizeof(APTKEYWARNING)-1) == 0)
Warning("%s", buffer + sizeof(APTKEYWARNING));
Warning(buffer + sizeof(APTKEYWARNING));
else if (strncmp(buffer, APTKEYERROR, sizeof(APTKEYERROR)-1) == 0)
_error->Error("%s", buffer + sizeof(APTKEYERROR));
}
Expand Down Expand Up @@ -442,8 +442,12 @@ bool GPGVMethod::URIAcquire(std::string const &Message, FetchItem *Itm)
}))
{
for (auto const & Signer : Signers.SoonWorthless)
{
std::string msg;
// TRANSLATORS: The second %s is the reason and is untranslated for repository owners.
Warning(_("Signature by key %s uses weak digest algorithm (%s)"), Signer.key.c_str(), Signer.note.c_str());
strprintf(msg, _("Signature by key %s uses weak digest algorithm (%s)"), Signer.key.c_str(), Signer.note.c_str());
Warning(std::move(msg));
}
}

if (Signers.Good.empty() || !Signers.Bad.empty() || !Signers.NoPubKey.empty())
Expand Down

0 comments on commit 96dc40b

Please sign in to comment.