diff options
| author | наб <nabijaczleweli@nabijaczleweli.xyz> | 2024-11-14 18:55:49 +0100 |
|---|---|---|
| committer | наб <nabijaczleweli@nabijaczleweli.xyz> | 2024-11-14 19:22:39 +0100 |
| commit | 6e80412fad93ef685559953719b82a28c1dcea13 (patch) | |
| tree | 62d9e96f600a8c472a5fba133c63ec89e99ec41b /apt-pkg | |
| parent | 47d490e78107a5209c1f4f5e4afca7379d42c152 (diff) | |
DEFER() more
Diffstat (limited to 'apt-pkg')
| -rw-r--r-- | apt-pkg/contrib/cdromutl.cc | 32 | ||||
| -rw-r--r-- | apt-pkg/contrib/error.cc | 9 | ||||
| -rw-r--r-- | apt-pkg/contrib/fileutl.cc | 8 | ||||
| -rw-r--r-- | apt-pkg/contrib/macros.h | 15 | ||||
| -rw-r--r-- | apt-pkg/deb/debsystem.cc | 26 | ||||
| -rw-r--r-- | apt-pkg/deb/dpkgpm.cc | 12 |
6 files changed, 44 insertions, 58 deletions
diff --git a/apt-pkg/contrib/cdromutl.cc b/apt-pkg/contrib/cdromutl.cc index 5e2255fa3..ff8fb4835 100644 --- a/apt-pkg/contrib/cdromutl.cc +++ b/apt-pkg/contrib/cdromutl.cc @@ -1,11 +1,11 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ /* ###################################################################### - + CDROM Utilities - Some functions to manipulate CDROM mounts. - + These are here for the cdrom method and apt-cdrom. - + ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ @@ -39,7 +39,7 @@ using std::string; // --------------------------------------------------------------------- /* This is a simple algorithm that should always work, we stat the mount point and the '..' file in the mount point and see if they are on the same device. - By definition if they are the same then it is not mounted. This should + By definition if they are the same then it is not mounted. This should account for symlinked mount points as well. */ bool IsMounted(string &Path) { @@ -59,7 +59,7 @@ bool IsMounted(string &Path) stating the path and the previous directory (careful of links!) and comparing their device fields. */ struct stat Buf,Buf2; - if (stat(Path.c_str(),&Buf) != 0 || + if (stat(Path.c_str(),&Buf) != 0 || stat((Path + "../").c_str(),&Buf2) != 0) return _error->Errno("stat",_("Unable to stat the mount point %s"),Path.c_str()); @@ -70,7 +70,7 @@ bool IsMounted(string &Path) /*}}}*/ // UnmountCdrom - Unmount a cdrom /*{{{*/ // --------------------------------------------------------------------- -/* Forking umount works much better than the umount syscall which can +/* Forking umount works much better than the umount syscall which can leave /etc/mtab inconsistent. We drop all messages this produces. */ bool UnmountCdrom(string Path) { @@ -85,7 +85,7 @@ bool UnmountCdrom(string Path) for (int i=0;i<3;i++) { - + int Child = ExecFork(); // The child @@ -100,7 +100,7 @@ bool UnmountCdrom(string Path) { if (system(_config->Find("Acquire::cdrom::"+Path+"::UMount").c_str()) != 0) _exit(100); - _exit(0); + _exit(0); } else { @@ -151,13 +151,13 @@ bool MountCdrom(string Path, string DeviceName) { if (system(_config->Find("Acquire::cdrom::"+Path+"::Mount").c_str()) != 0) _exit(100); - _exit(0); + _exit(0); } else { const char *Args[10]; Args[0] = "mount"; - if (DeviceName == "") + if (DeviceName == "") { Args[1] = Path.c_str(); Args[2] = 0; @@ -166,9 +166,9 @@ bool MountCdrom(string Path, string DeviceName) Args[2] = Path.c_str(); Args[3] = 0; } - execvp(Args[0],(char **)Args); + execvp(Args[0],(char **)Args); _exit(100); - } + } } // Wait for mount @@ -265,11 +265,11 @@ string FindMountPointForDevice(const char *devnode) std::vector<std::string> const mounts = _config->FindVector("Dir::state::MountPoints", "/etc/mtab,/proc/mount"); for (std::vector<std::string>::const_iterator m = mounts.begin(); m != mounts.end(); ++m) - if (FileExists(*m) == true) + if (FILE * f = fopen(m->c_str(), "r")) { char * line = NULL; size_t line_len = 0; - FILE * f = fopen(m->c_str(), "r"); + DEFER([&] { fclose(f); free(line); }); while(getline(&line, &line_len, f) != -1) { char * out[] = { NULL, NULL, NULL }; @@ -278,14 +278,10 @@ string FindMountPointForDevice(const char *devnode) continue; if (strcmp(out[0], devnode) != 0) continue; - fclose(f); // unescape the \0XXX chars in the path string mount_point = out[1]; - free(line); return DeEscapeString(mount_point); } - fclose(f); - free(line); } return string(); diff --git a/apt-pkg/contrib/error.cc b/apt-pkg/contrib/error.cc index 054435b73..d08d5e008 100644 --- a/apt-pkg/contrib/error.cc +++ b/apt-pkg/contrib/error.cc @@ -89,6 +89,7 @@ bool GlobalError::InsertErrno(MsgType type, const char* Function, const char* Description, va_list &args, int const errsv, size_t &msgSize) { char* S = (char*) malloc(msgSize); + DEFER([&] { free(S); }); int const n = snprintf(S, msgSize, "%s - %s (%i: %s)", Description, Function, errsv, strerror(errsv)); if (n > -1 && ((unsigned int) n) < msgSize); @@ -97,13 +98,10 @@ bool GlobalError::InsertErrno(MsgType type, const char* Function, msgSize = n + 1; else msgSize *= 2; - free(S); return true; } - bool const geins = Insert(type, S, args, msgSize); - free(S); - return geins; + return Insert(type, S, args, msgSize); } /*}}}*/ // GlobalError::Fatal, Error, Warning, Notice and Debug - Add to the list/*{{{*/ @@ -145,6 +143,7 @@ bool GlobalError::Insert(MsgType const &type, const char *Description,...) bool GlobalError::Insert(MsgType type, const char* Description, va_list &args, size_t &msgSize) { char* S = (char*) malloc(msgSize); + DEFER([&] { free(S); }); int const n = vsnprintf(S, msgSize, Description, args); if (n > -1 && ((unsigned int) n) < msgSize); else { @@ -152,7 +151,6 @@ bool GlobalError::Insert(MsgType type, const char* Description, msgSize = n + 1; else msgSize *= 2; - free(S); return true; } @@ -165,7 +163,6 @@ bool GlobalError::Insert(MsgType type, const char* Description, if (type == FATAL || type == DEBUG) std::clog << m << std::endl; - free(S); return false; } /*}}}*/ diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index bd7076701..0a35f7afb 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -999,24 +999,18 @@ bool StartsWithGPGClearTextSignature(string const &FileName) char * lineptr = nullptr; size_t n = 0; errno = 0; + DEFER([&] { fclose(gpg); free(lineptr); }); ssize_t const result = getline(&lineptr, &n, gpg); if (errno != 0) { _error->Errno("getline", "Could not read from %s", FileName.c_str()); - fclose(gpg); - free(lineptr); return false; } - fclose(gpg); _strrstrip(lineptr); static const char* SIGMSG = "-----BEGIN PGP SIGNED MESSAGE-----"; if (result == -1 || strcmp(lineptr, SIGMSG) != 0) - { - free(lineptr); return false; - } - free(lineptr); return true; } /*}}}*/ diff --git a/apt-pkg/contrib/macros.h b/apt-pkg/contrib/macros.h index c08cd24d9..d6f9c2f98 100644 --- a/apt-pkg/contrib/macros.h +++ b/apt-pkg/contrib/macros.h @@ -2,7 +2,7 @@ // SPDX-License-Identifier: GPL-2.0+ // Description /*{{{*/ /* ###################################################################### - + Macros Header - Various useful macro definitions This file had this historic note, but now includes further changes @@ -10,7 +10,7 @@ This source is placed in the Public Domain, do with it what you will It was originally written by Brian C. White. - + ##################################################################### */ /*}}}*/ // Private header @@ -128,4 +128,15 @@ /* Should be a multiple of the common page size (4096) */ static constexpr unsigned long long APT_BUFFER_SIZE = 64 * 1024; +template <class F> +struct AptScopeWrapper { + F func; + ~AptScopeWrapper() { func(); } +}; +template <class F> +AptScopeWrapper(F) -> AptScopeWrapper<F>; +#define PASTE2(a, b) a##b +#define PASTE(a, b) PASTE2(a, b) +#define DEFER(lambda) AptScopeWrapper PASTE(defer, __LINE__){lambda}; + #endif diff --git a/apt-pkg/deb/debsystem.cc b/apt-pkg/deb/debsystem.cc index a218005dc..052179fd7 100644 --- a/apt-pkg/deb/debsystem.cc +++ b/apt-pkg/deb/debsystem.cc @@ -5,7 +5,7 @@ System - Abstraction for running on different systems. Basic general structure.. - + ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ @@ -53,7 +53,7 @@ public: int FrontendLockFD; int LockFD; unsigned LockCount; - + debStatusIndex *StatusFile; }; @@ -146,7 +146,7 @@ bool debSystem::Lock(OpProgress *const Progress) close(d->FrontendLockFD); return false; } - + // See if we need to abort with a dirty journal if (CheckUpdates() == true) { @@ -166,7 +166,7 @@ bool debSystem::Lock(OpProgress *const Progress) } d->LockCount++; - + return true; } @@ -193,7 +193,7 @@ bool debSystem::UnLock(bool NoErrors) { if (d->LockCount == 0 && NoErrors == true) return false; - + if (d->LockCount < 1) return _error->Error(_("Not locked")); if (--d->LockCount == 0) @@ -202,7 +202,7 @@ bool debSystem::UnLock(bool NoErrors) close(d->FrontendLockFD); d->LockCount = 0; } - + return true; } bool debSystem::UnLockInner(bool NoErrors) { @@ -222,7 +222,7 @@ bool debSystem::IsLocked() /*}}}*/ // System::CheckUpdates - Check if the updates dir is dirty /*{{{*/ // --------------------------------------------------------------------- -/* This does a check of the updates directory (dpkg journal) to see if it has +/* This does a check of the updates directory (dpkg journal) to see if it has any entries in it. */ bool debSystem::CheckUpdates() { @@ -231,8 +231,8 @@ bool debSystem::CheckUpdates() DIR *DirP = opendir(File.c_str()); if (DirP == 0) return false; - - /* We ignore any files that are not all digits, this skips .,.. and + + /* We ignore any files that are not all digits, this skips .,.. and some tmp files dpkg will leave behind.. */ bool Damaged = false; for (struct dirent *Ent = readdir(DirP); Ent != 0; Ent = readdir(DirP)) @@ -373,7 +373,7 @@ bool debSystem::FindIndex(pkgCache::PkgFileIterator File, Found = d->StatusFile; return true; } - + return false; } /*}}}*/ @@ -528,10 +528,10 @@ std::vector<std::string> debSystem::ArchitecturesSupported() const /*{{{*/ if (dpkgMultiArch == -1) return archs; - FILE *dpkg = fdopen(outputFd, "r"); - if(dpkg != NULL) { + if(FILE *dpkg = fdopen(outputFd, "r")) { char* buf = NULL; size_t bufsize = 0; + DEFER([&] { fclose(dpkg); free(buf); }); while (getline(&buf, &bufsize, dpkg) != -1) { char* tok_saveptr; @@ -548,8 +548,6 @@ std::vector<std::string> debSystem::ArchitecturesSupported() const /*{{{*/ arch = strtok_r(NULL, " ", &tok_saveptr); } } - free(buf); - fclose(dpkg); } ExecWait(dpkgMultiArch, "dpkg --print-foreign-architectures", true); return archs; diff --git a/apt-pkg/deb/dpkgpm.cc b/apt-pkg/deb/dpkgpm.cc index 273b6d845..085a686d6 100644 --- a/apt-pkg/deb/dpkgpm.cc +++ b/apt-pkg/deb/dpkgpm.cc @@ -2243,16 +2243,6 @@ 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>; -#define PASTE(a, b) a##b -#define DEFER(lambda) AptScopeWrapper PASTE(defer, __LINE__){lambda}; - static void CopyIndented(const char *header, FILE *from, FILE *to) { if (!from) @@ -2378,7 +2368,7 @@ void pkgDPkgPM::WriteApportReport(const char *pkgpath, const char *errormsg) pkgver = Ver.VerStr() == NULL ? "unknown" : Ver.VerStr(); // if the file exists already, we check: - // - if it was reported already (touched by apport). + // - if it was reported already (touched by apport). // If not, we do nothing, otherwise // we overwrite it. This is the same behaviour as apport // - if we have a report with the same pkgversion already |
