#include <string>
#include <utility>
#include <vector>
+#include <sstream>
+#include <numeric>
#include <apti18n.h>
/*}}}*/
using namespace std;
+APT_PURE static string
+AptHistoryRequestingUser()
+{
+ const char* EnvKeys[]{"SUDO_UID", "PKEXEC_UID", "PACKAGEKIT_CALLER_UID"};
+
+ for (const auto &Key: EnvKeys)
+ {
+ if (getenv(Key) != nullptr)
+ {
+ int uid = atoi(getenv(Key));
+ if (uid > 0) {
+ struct passwd pwd;
+ struct passwd *result;
+ char buf[255];
+ if (getpwuid_r(uid, &pwd, buf, sizeof(buf), &result) == 0 && result != NULL) {
+ std::string res;
+ strprintf(res, "%s (%d)", pwd.pw_name, uid);
+ return res;
+ }
+ }
+ }
+ }
+ return "";
+}
+
APT_PURE static unsigned int
EnvironmentSize()
{
bool stdin_is_dev_null;
// the buffer we use for the dpkg status-fd reading
char dpkgbuf[1024];
- int dpkgbuf_pos;
+ size_t dpkgbuf_pos;
FILE *term_out;
FILE *history_out;
string dpkg_error;
const char *target;
public:
- MatchProcessingOp(const char *the_target)
+ explicit MatchProcessingOp(const char *the_target)
: target(the_target)
{
}
}
/*}}}*/
// DPkgPM::DoDpkgStatusFd /*{{{*/
-// ---------------------------------------------------------------------
-/*
- */
void pkgDPkgPM::DoDpkgStatusFd(int statusfd)
{
- char *p, *q;
- int len;
-
- len=read(statusfd, &d->dpkgbuf[d->dpkgbuf_pos], sizeof(d->dpkgbuf)-d->dpkgbuf_pos);
- d->dpkgbuf_pos += len;
+ ssize_t const len = read(statusfd, &d->dpkgbuf[d->dpkgbuf_pos],
+ (sizeof(d->dpkgbuf)/sizeof(d->dpkgbuf[0])) - d->dpkgbuf_pos);
if(len <= 0)
return;
+ d->dpkgbuf_pos += (len / sizeof(d->dpkgbuf[0]));
- // process line by line if we have a buffer
- p = q = d->dpkgbuf;
- while((q=(char*)memchr(p, '\n', d->dpkgbuf+d->dpkgbuf_pos-p)) != NULL)
+ // process line by line from the buffer
+ char *p = d->dpkgbuf, *q = nullptr;
+ while((q=(char*)memchr(p, '\n', (d->dpkgbuf + d->dpkgbuf_pos) - p)) != nullptr)
{
- *q = 0;
+ *q = '\0';
ProcessDpkgStatusLine(p);
- p=q+1; // continue with next line
+ p = q + 1; // continue with next line
}
- // now move the unprocessed bits (after the final \n that is now a 0x0)
- // to the start and update d->dpkgbuf_pos
- p = (char*)memrchr(d->dpkgbuf, 0, d->dpkgbuf_pos);
- if(p == NULL)
+ // check if we stripped the buffer clean
+ if (p > (d->dpkgbuf + d->dpkgbuf_pos))
+ {
+ d->dpkgbuf_pos = 0;
return;
+ }
- // we are interessted in the first char *after* 0x0
- p++;
-
- // move the unprocessed tail to the start and update pos
- memmove(d->dpkgbuf, p, p-d->dpkgbuf);
- d->dpkgbuf_pos = d->dpkgbuf+d->dpkgbuf_pos-p;
+ // otherwise move the unprocessed tail to the start and update pos
+ memmove(d->dpkgbuf, p, (p - d->dpkgbuf));
+ d->dpkgbuf_pos = (d->dpkgbuf + d->dpkgbuf_pos) - p;
}
/*}}}*/
// DPkgPM::WriteHistoryTag /*{{{*/
}
if (_config->Exists("Commandline::AsString") == true)
WriteHistoryTag("Commandline", _config->Find("Commandline::AsString"));
+ std::string RequestingUser = AptHistoryRequestingUser();
+ if (RequestingUser != "")
+ WriteHistoryTag("Requested-By", RequestingUser);
WriteHistoryTag("Install", install);
WriteHistoryTag("Reinstall", reinstall);
WriteHistoryTag("Upgrade", upgrade);
std::vector<const char *> Args(sArgs.size(), NULL);
std::transform(sArgs.begin(), sArgs.end(), Args.begin(),
[](std::string const &s) { return s.c_str(); });
- unsigned long long const StartSize = std::accumulate(sArgs.begin(), sArgs.end(), 0,
+ unsigned long long const StartSize = std::accumulate(sArgs.begin(), sArgs.end(), 0llu,
[](unsigned long long const i, std::string const &s) { return i + s.length(); });
size_t const BaseArgs = Args.size();
// Check for an error code.
if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
{
- // if it was set to "keep-dpkg-runing" then we won't return
+ // if it was set to "keep-dpkg-running" then we won't return
// here but keep the loop going and just report it as a error
// for later
bool const stopOnError = _config->FindB("Dpkg::StopOnError",true);
{
std::string const oldpkgcache = _config->FindFile("Dir::cache::pkgcache");
if (oldpkgcache.empty() == false && RealFileExists(oldpkgcache) == true &&
- unlink(oldpkgcache.c_str()) == 0)
+ RemoveFile("pkgDPkgPM::Go", oldpkgcache))
{
std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache");
if (srcpkgcache.empty() == false && RealFileExists(srcpkgcache) == true)