summaryrefslogtreecommitdiff
path: root/apt-pkg
diff options
context:
space:
mode:
authorнаб <nabijaczleweli@nabijaczleweli.xyz>2024-11-11 16:17:35 +0100
committerнаб <nabijaczleweli@nabijaczleweli.xyz>2024-11-12 13:46:17 +0100
commit9f81a8dd0ee585a360ae0ae7786959630a8d0be0 (patch)
treec2a0c3e85acae1f396316ffbff422d80846b4024 /apt-pkg
parent78e503bc1d79bc4fc29bc9cbe1dfb68ae4d16acf (diff)
dpkgpm: lift out CopyIndented() and make_unique_*() for FILEs; use getline() for reading lines instead of 1k buffer
Diffstat (limited to 'apt-pkg')
-rw-r--r--apt-pkg/contrib/fileutl.h29
-rw-r--r--apt-pkg/contrib/gpgv.cc12
-rw-r--r--apt-pkg/deb/dpkgpm.cc74
3 files changed, 55 insertions, 60 deletions
diff --git a/apt-pkg/contrib/fileutl.h b/apt-pkg/contrib/fileutl.h
index 11f4871f6..938d1d8bc 100644
--- a/apt-pkg/contrib/fileutl.h
+++ b/apt-pkg/contrib/fileutl.h
@@ -29,6 +29,7 @@
#include <ctime>
#include <set>
+#include <memory>
#include <string>
#include <vector>
#include <sys/stat.h>
@@ -284,4 +285,32 @@ APT_HIDDEN bool OpenConfigurationFileFd(std::string const &File, FileFd &Fd);
APT_HIDDEN int Inhibit(const char *what, const char *who, const char *why, const char *mode);
+
+namespace {
+ struct FILEFcloseDeleter {
+ void operator()(FILE *p) {
+ fclose(p);
+ }
+ };
+ struct FILEPcloseDeleter {
+ void operator()(FILE *p) {
+ pclose(p);
+ }
+ };
+
+ [[maybe_unused]] std::unique_ptr<FILE, FILEFcloseDeleter> make_unique_FILE(const char *const filename, char const *const mode)
+ {
+ return {fopen(filename, mode), {}};
+ }
+ [[maybe_unused]] std::unique_ptr<FILE, FILEFcloseDeleter> make_unique_FILE(std::string const &filename, char const *const mode)
+ {
+ return make_unique_FILE(filename.c_str(), mode);
+ }
+
+ [[maybe_unused]] std::unique_ptr<FILE, FILEPcloseDeleter> make_unique_popen(const char *program, char const *const mode)
+ {
+ return {popen(program, mode), {}};
+ }
+}
+
#endif
diff --git a/apt-pkg/contrib/gpgv.cc b/apt-pkg/contrib/gpgv.cc
index 9ab3e066e..c5ea19aa8 100644
--- a/apt-pkg/contrib/gpgv.cc
+++ b/apt-pkg/contrib/gpgv.cc
@@ -28,18 +28,6 @@
#include <apti18n.h>
/*}}}*/
-namespace {
- struct FILEDeleter {
- void operator()(FILE *p) {
- fclose(p);
- }
- };
-}
-
-static std::unique_ptr<FILE, FILEDeleter> make_unique_FILE(std::string const &filename, char const *const mode)
-{
- return {fopen(filename.c_str(), mode), {}};
-}
class LineBuffer /*{{{*/
{
diff --git a/apt-pkg/deb/dpkgpm.cc b/apt-pkg/deb/dpkgpm.cc
index 82035ee65..31f81af7c 100644
--- a/apt-pkg/deb/dpkgpm.cc
+++ b/apt-pkg/deb/dpkgpm.cc
@@ -33,6 +33,7 @@
#include <dirent.h>
#include <fcntl.h>
#include <grp.h>
+#include <limits.h>
#include <pwd.h>
#include <sys/ioctl.h>
#include <sys/select.h>
@@ -2233,6 +2234,27 @@ void pkgDPkgPM::Reset()
{
List.erase(List.begin(),List.end());
}
+
+template <class F>
+struct AptScopeWrapper {
+ F func;
+ ~AptScopeWrapper() { func(); }
+};
+template <class F>
+AptScopeWrapper(F) -> AptScopeWrapper<F>;
+
+static void CopyIndented(const char *header, FILE *from, FILE *to)
+{
+ if (!from)
+ return;
+
+ fputs(header, to);
+ char *line{};
+ size_t linelen;
+ AptScopeWrapper line_deleter{[&] { free(line); }};
+ while (getline(&line, &linelen, from) != -1)
+ fprintf(to, " %s", line);
+}
/*}}}*/
// pkgDpkgPM::WriteApportReport - write out error report pkg failure /*{{{*/
// ---------------------------------------------------------------------
@@ -2406,35 +2428,12 @@ void pkgDPkgPM::WriteApportReport(const char *pkgpath, const char *errormsg)
// attach terminal log it if we have it
string logfile_name = _config->FindFile("Dir::Log::Terminal", "/dev/null");
if (logfile_name != "/dev/null")
- {
- FILE *log = NULL;
-
- fprintf(report, "DpkgTerminalLog:\n");
- log = fopen(logfile_name.c_str(),"r");
- if(log != NULL)
- {
- char buf[1024];
- while( fgets(buf, sizeof(buf), log) != NULL)
- fprintf(report, " %s", buf);
- fprintf(report, " \n");
- fclose(log);
- }
- }
+ CopyIndented("DpkgTerminalLog:\n", make_unique_FILE(logfile_name, "r").get(), report);
// attach history log it if we have it
string histfile_name = _config->FindFile("Dir::Log::History", "/dev/null");
if (histfile_name != "/dev/null")
- {
- fprintf(report, "DpkgHistoryLog:\n");
- FILE* log = fopen(histfile_name.c_str(),"r");
- if(log != NULL)
- {
- char buf[1024];
- while( fgets(buf, sizeof(buf), log) != NULL)
- fprintf(report, " %s", buf);
- fclose(log);
- }
- }
+ CopyIndented("DpkgHistoryLog:\n", make_unique_FILE(histfile_name, "r").get(), report);
// log the ordering, see dpkgpm.h and the "Ops" enum there
fprintf(report, "AptOrdering:\n");
@@ -2458,32 +2457,11 @@ void pkgDPkgPM::WriteApportReport(const char *pkgpath, const char *errormsg)
// attach dmesg log (to learn about segfaults)
if (FileExists("/bin/dmesg"))
- {
- fprintf(report, "Dmesg:\n");
- FILE *log = popen("/bin/dmesg","r");
- if(log != NULL)
- {
- char buf[1024];
- while( fgets(buf, sizeof(buf), log) != NULL)
- fprintf(report, " %s", buf);
- pclose(log);
- }
- }
+ CopyIndented("Dmesg:\n", make_unique_popen("/bin/dmesg","r").get(), report);
// attach df -l log (to learn about filesystem status)
if (FileExists("/bin/df"))
- {
-
- fprintf(report, "Df:\n");
- FILE *log = popen("/bin/df -l -x squashfs","r");
- if(log != NULL)
- {
- char buf[1024];
- while( fgets(buf, sizeof(buf), log) != NULL)
- fprintf(report, " %s", buf);
- pclose(log);
- }
- }
+ CopyIndented("Df:\n", make_unique_popen("/bin/df -l -x squashfs","r").get(), report);
fclose(report);