From ce1f3a2c616b86da657c1c796efa5f4d18c30c39 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Mon, 2 Nov 2015 18:49:52 +0100 Subject: wrap every unlink call to check for != /dev/null Unlinking /dev/null is bad, we shouldn't do that. Also, we should print at least a warning if we tried to unlink a file but didn't manage to pull it of (ignoring the case were the file is /dev/null or doesn't exist in the first place). This got triggered by a relatively unlikely to cause problem in pkgAcquire::Worker::PrepareFiles which would while temporary uncompressed files (which are set to keep compressed) figure out that to files are the same and prepare for sharing by deleting them. Bad move. That also shows why not printing a warning is a bad idea as this hide the error for in non-root test runs. Git-Dch: Ignore --- apt-pkg/acquire-item.cc | 14 -------------- apt-pkg/acquire-worker.cc | 6 +++--- apt-pkg/acquire.cc | 8 ++++---- apt-pkg/cachefile.cc | 8 ++++---- apt-pkg/cdrom.cc | 6 +++--- apt-pkg/contrib/fileutl.cc | 22 ++++++++++++++++++---- apt-pkg/contrib/fileutl.h | 1 + apt-pkg/deb/dpkgpm.cc | 2 +- apt-pkg/edsp/edsplistparser.cc | 6 ++---- apt-pkg/edsp/edspsystem.cc | 4 ++-- 10 files changed, 38 insertions(+), 39 deletions(-) (limited to 'apt-pkg') diff --git a/apt-pkg/acquire-item.cc b/apt-pkg/acquire-item.cc index 6cf23daae..834776404 100644 --- a/apt-pkg/acquire-item.cc +++ b/apt-pkg/acquire-item.cc @@ -147,20 +147,6 @@ static bool BootstrapPDiffWith(std::string const &PartialFile, std::string const return typeItr != types.cend(); } /*}}}*/ -static bool RemoveFile(char const * const Function, std::string const &FileName)/*{{{*/ -{ - if (FileName == "/dev/null") - return true; - errno = 0; - if (unlink(FileName.c_str()) != 0) - { - if (errno == ENOENT) - return true; - return _error->WarningE(Function, "Removal of file %s failed", FileName.c_str()); - } - return true; -} - /*}}}*/ static bool MessageInsecureRepository(bool const isError, std::string const &msg)/*{{{*/ { diff --git a/apt-pkg/acquire-worker.cc b/apt-pkg/acquire-worker.cc index d8bdf5699..b5f52a3ca 100644 --- a/apt-pkg/acquire-worker.cc +++ b/apt-pkg/acquire-worker.cc @@ -742,9 +742,9 @@ void pkgAcquire::Worker::PrepareFiles(char const * const caller, pkgAcquire::Que for (pkgAcquire::Queue::QItem::owner_iterator O = Itm->Owners.begin(); O != Itm->Owners.end(); ++O) { pkgAcquire::Item const * const Owner = *O; - if (Owner->DestFile == filename) + if (Owner->DestFile == filename || filename == "/dev/null") continue; - unlink(Owner->DestFile.c_str()); + RemoveFile("PrepareFiles", Owner->DestFile); if (link(filename.c_str(), Owner->DestFile.c_str()) != 0) { // different mounts can't happen for us as we download to lists/ by default, @@ -759,7 +759,7 @@ void pkgAcquire::Worker::PrepareFiles(char const * const caller, pkgAcquire::Que else { for (pkgAcquire::Queue::QItem::owner_iterator O = Itm->Owners.begin(); O != Itm->Owners.end(); ++O) - unlink((*O)->DestFile.c_str()); + RemoveFile("PrepareFiles", (*O)->DestFile); } } /*}}}*/ diff --git a/apt-pkg/acquire.cc b/apt-pkg/acquire.cc index a74f1c2f6..81faee5d8 100644 --- a/apt-pkg/acquire.cc +++ b/apt-pkg/acquire.cc @@ -645,7 +645,7 @@ bool pkgAcquire::Clean(string Dir) // Nothing found, nuke it if (I == Items.end()) - unlink(Dir->d_name); + RemoveFile("Clean", Dir->d_name); }; closedir(D); @@ -993,15 +993,15 @@ void pkgAcquire::Queue::QItem::SyncDestinationFiles() const /*{{{*/ if (lstat((*O)->DestFile.c_str(),&file) == 0) { if ((file.st_mode & S_IFREG) == 0) - unlink((*O)->DestFile.c_str()); + RemoveFile("SyncDestinationFiles", (*O)->DestFile); else if (supersize < file.st_size) { supersize = file.st_size; - unlink(superfile.c_str()); + RemoveFile("SyncDestinationFiles", superfile); rename((*O)->DestFile.c_str(), superfile.c_str()); } else - unlink((*O)->DestFile.c_str()); + RemoveFile("SyncDestinationFiles", (*O)->DestFile); if (symlink(superfile.c_str(), (*O)->DestFile.c_str()) != 0) { ; // not a problem per-se and no real alternative diff --git a/apt-pkg/cachefile.cc b/apt-pkg/cachefile.cc index 1c9bc694b..aaa2436c5 100644 --- a/apt-pkg/cachefile.cc +++ b/apt-pkg/cachefile.cc @@ -191,9 +191,9 @@ void pkgCacheFile::RemoveCaches() std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache"); if (pkgcache.empty() == false && RealFileExists(pkgcache) == true) - unlink(pkgcache.c_str()); + RemoveFile("RemoveCaches", pkgcache); if (srcpkgcache.empty() == false && RealFileExists(srcpkgcache) == true) - unlink(srcpkgcache.c_str()); + RemoveFile("RemoveCaches", srcpkgcache); if (pkgcache.empty() == false) { std::string cachedir = flNotFile(pkgcache); @@ -207,7 +207,7 @@ void pkgCacheFile::RemoveCaches() std::string nuke = flNotDir(*file); if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0) continue; - unlink(file->c_str()); + RemoveFile("RemoveCaches", *file); } } } @@ -226,7 +226,7 @@ void pkgCacheFile::RemoveCaches() std::string nuke = flNotDir(*file); if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0) continue; - unlink(file->c_str()); + RemoveFile("RemoveCaches", *file); } } /*}}}*/ diff --git a/apt-pkg/cdrom.cc b/apt-pkg/cdrom.cc index b950648b9..29eeca066 100644 --- a/apt-pkg/cdrom.cc +++ b/apt-pkg/cdrom.cc @@ -426,8 +426,8 @@ bool pkgCdrom::WriteDatabase(Configuration &Cnf) { string DFile = _config->FindFile("Dir::State::cdroms"); string NewFile = DFile + ".new"; - - unlink(NewFile.c_str()); + + RemoveFile("WriteDatabase", NewFile); ofstream Out(NewFile.c_str()); if (!Out) return _error->Errno("ofstream::ofstream", @@ -468,7 +468,7 @@ bool pkgCdrom::WriteSourceList(string Name,vector &List,bool Source) return _error->Errno("ifstream::ifstream","Opening %s",File.c_str()); string NewFile = File + ".new"; - unlink(NewFile.c_str()); + RemoveFile("WriteDatabase", NewFile); ofstream Out(NewFile.c_str()); if (!Out) return _error->Errno("ofstream::ofstream", diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 368380af7..e52c8f219 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -169,6 +169,21 @@ bool CopyFile(FileFd &From,FileFd &To) return false; } while (ToRead != 0); + return true; +} + /*}}}*/ +bool RemoveFile(char const * const Function, std::string const &FileName)/*{{{*/ +{ + if (FileName == "/dev/null") + return true; + errno = 0; + if (unlink(FileName.c_str()) != 0) + { + if (errno == ENOENT) + return true; + + return _error->WarningE(Function,_("Problem unlinking the file %s"), FileName.c_str()); + } return true; } /*}}}*/ @@ -1135,13 +1150,13 @@ bool FileFd::Open(string FileName,unsigned int const Mode,APT::Configuration::Co else if ((OpenMode & (Exclusive | Create)) == (Exclusive | Create)) { // for atomic, this will be done by rename in Close() - unlink(FileName.c_str()); + RemoveFile("FileFd::Open", FileName); } if ((OpenMode & Empty) == Empty) { struct stat Buf; if (lstat(FileName.c_str(),&Buf) == 0 && S_ISLNK(Buf.st_mode)) - unlink(FileName.c_str()); + RemoveFile("FileFd::Open", FileName); } int fileflags = 0; @@ -2025,8 +2040,7 @@ bool FileFd::Close() if ((Flags & Fail) == Fail && (Flags & DelOnFail) == DelOnFail && FileName.empty() == false) - if (unlink(FileName.c_str()) != 0) - Res &= _error->WarningE("unlnk",_("Problem unlinking the file %s"), FileName.c_str()); + Res &= RemoveFile("FileFd::Close", FileName); if (Res == false) Flags |= Fail; diff --git a/apt-pkg/contrib/fileutl.h b/apt-pkg/contrib/fileutl.h index cddfe2b45..7176e4dea 100644 --- a/apt-pkg/contrib/fileutl.h +++ b/apt-pkg/contrib/fileutl.h @@ -150,6 +150,7 @@ class FileFd bool RunScripts(const char *Cnf); bool CopyFile(FileFd &From,FileFd &To); +bool RemoveFile(char const * const Function, std::string const &FileName); int GetLock(std::string File,bool Errors = true); bool FileExists(std::string File); bool RealFileExists(std::string File); diff --git a/apt-pkg/deb/dpkgpm.cc b/apt-pkg/deb/dpkgpm.cc index ccc4b5a6c..1446826d6 100644 --- a/apt-pkg/deb/dpkgpm.cc +++ b/apt-pkg/deb/dpkgpm.cc @@ -1597,7 +1597,7 @@ bool pkgDPkgPM::Go(APT::Progress::PackageManager *progress) { 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) diff --git a/apt-pkg/edsp/edsplistparser.cc b/apt-pkg/edsp/edsplistparser.cc index 77a0edc22..85a57479e 100644 --- a/apt-pkg/edsp/edsplistparser.cc +++ b/apt-pkg/edsp/edsplistparser.cc @@ -31,12 +31,10 @@ public: edspListParserPrivate() { std::string const states = _config->FindFile("Dir::State::extended_states"); - if (states != "/dev/null") - unlink(states.c_str()); + RemoveFile("edspListParserPrivate", states); extendedstates.Open(states, FileFd::WriteOnly | FileFd::Create | FileFd::Exclusive, 0600); std::string const prefs = _config->FindFile("Dir::Etc::preferences"); - if (prefs != "/dev/null") - unlink(prefs.c_str()); + RemoveFile("edspListParserPrivate", prefs); preferences.Open(prefs, FileFd::WriteOnly | FileFd::Create | FileFd::Exclusive, 0600); } }; diff --git a/apt-pkg/edsp/edspsystem.cc b/apt-pkg/edsp/edspsystem.cc index 4c16f76d2..95abc1527 100644 --- a/apt-pkg/edsp/edspsystem.cc +++ b/apt-pkg/edsp/edspsystem.cc @@ -57,8 +57,8 @@ public: if (tempDir.empty()) return; - unlink(tempStatesFile.c_str()); - unlink(tempPrefsFile.c_str()); + RemoveFile("DeInitialize", tempStatesFile); + RemoveFile("DeInitialize", tempPrefsFile); rmdir(tempDir.c_str()); } -- cgit v1.2.3