diff options
author | David Kalnischkies <david@kalnischkies.de> | 2021-03-09 11:40:59 +0100 |
---|---|---|
committer | David Kalnischkies <david@kalnischkies.de> | 2021-03-09 13:36:52 +0100 |
commit | 3312607159410eda3c634a0c6fe3c2b1a4fd3cc3 (patch) | |
tree | 1a80b34793516daefc587716d5b09a65be3d267a /apt-pkg | |
parent | a5346a50c4f3ef15e71c16a9bf88723ae1153776 (diff) |
Use RAII to clean up tmp dir for dpkg recursive install
One less thing to remember to do in all branches.
Diffstat (limited to 'apt-pkg')
-rw-r--r-- | apt-pkg/deb/dpkgpm.cc | 17 |
1 files changed, 7 insertions, 10 deletions
diff --git a/apt-pkg/deb/dpkgpm.cc b/apt-pkg/deb/dpkgpm.cc index eb549023f..ba5c3a11a 100644 --- a/apt-pkg/deb/dpkgpm.cc +++ b/apt-pkg/deb/dpkgpm.cc @@ -1826,7 +1826,7 @@ bool pkgDPkgPM::Go(APT::Progress::PackageManager *progress) break; } - char * tmpdir_to_free = nullptr; + std::unique_ptr<char, decltype(&cleanUpTmpDir)> tmpdir_for_dpkg_recursive{nullptr, &cleanUpTmpDir}; // Write in the file or package names if (I->Op == Item::Install) @@ -1836,9 +1836,9 @@ bool pkgDPkgPM::Go(APT::Progress::PackageManager *progress) { std::string tmpdir; strprintf(tmpdir, "%s/apt-dpkg-install-XXXXXX", GetTempDir().c_str()); - tmpdir_to_free = strndup(tmpdir.data(), tmpdir.length()); - if (mkdtemp(tmpdir_to_free) == nullptr) - return _error->Errno("DPkg::Go", "mkdtemp of %s failed in preparation of calling dpkg unpack", tmpdir_to_free); + tmpdir_for_dpkg_recursive.reset(strndup(tmpdir.data(), tmpdir.length())); + if (mkdtemp(tmpdir_for_dpkg_recursive.get()) == nullptr) + return _error->Errno("DPkg::Go", "mkdtemp of %s failed in preparation of calling dpkg unpack", tmpdir_for_dpkg_recursive.get()); char p = 1; for (auto c = installsToDo - 1; (c = c/10) != 0; ++p); @@ -1851,14 +1851,14 @@ bool pkgDPkgPM::Go(APT::Progress::PackageManager *progress) file.append(".deb"); std::string linkpath; if (dpkg_recursive_install_numbered) - strprintf(linkpath, "%s/%.*lu-%s", tmpdir_to_free, p, n, file.c_str()); + strprintf(linkpath, "%s/%.*lu-%s", tmpdir_for_dpkg_recursive.get(), p, n, file.c_str()); else - strprintf(linkpath, "%s/%s", tmpdir_to_free, file.c_str()); + strprintf(linkpath, "%s/%s", tmpdir_for_dpkg_recursive.get(), file.c_str()); if (symlink(I->File.c_str(), linkpath.c_str()) != 0) return _error->Errno("DPkg::Go", "Symlinking %s to %s failed!", I->File.c_str(), linkpath.c_str()); } AddArg("--recursive"); - AddArg(tmpdir_to_free); + AddArg(tmpdir_for_dpkg_recursive.get()); } else { @@ -1957,7 +1957,6 @@ bool pkgDPkgPM::Go(APT::Progress::PackageManager *progress) Packages.clear(); close(fd[0]); close(fd[1]); - cleanUpTmpDir(tmpdir_to_free); continue; } Args.push_back(NULL); @@ -2111,8 +2110,6 @@ bool pkgDPkgPM::Go(APT::Progress::PackageManager *progress) signal(SIGINT,old_SIGINT); signal(SIGHUP,old_SIGHUP); - cleanUpTmpDir(tmpdir_to_free); - if (waitpid_failure == true) { strprintf(d->dpkg_error, "Sub-process %s couldn't be waited for.",Args[0]); |