From 37cc8dcda9e97e0b9420d37bb886081fa629847d Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 13 May 2020 08:42:02 +0200 Subject: Prefer use of O_TMPFILE in GetTempFile if available Not all filesystems implement this feature in all versions of Linux, so this open call can fail & we have to fallback to our old method. --- apt-pkg/contrib/fileutl.cc | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 045dbe17d..68dd8265c 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -3154,11 +3154,18 @@ FileFd* GetTempFile(std::string const &Prefix, bool ImmediateUnlink, FileFd * co FileFd * const Fd = TmpFd == nullptr ? new FileFd() : TmpFd; std::string const tempdir = GetTempDir(); - snprintf(fn, sizeof(fn), "%s/%s.XXXXXX", - tempdir.c_str(), Prefix.c_str()); - int const fd = mkstemp(fn); + int fd = -1; +#ifdef O_TMPFILE if (ImmediateUnlink) - unlink(fn); + fd = open(tempdir.c_str(), O_RDWR|O_TMPFILE|O_EXCL|O_CLOEXEC, 0600); + if (fd < 0) +#endif + { + snprintf(fn, sizeof(fn), "%s/%s.XXXXXX", tempdir.c_str(), Prefix.c_str()); + fd = mkstemp(fn); + if (ImmediateUnlink) + unlink(fn); + } if (fd < 0) { _error->Errno("GetTempFile",_("Unable to mkstemp %s"), fn); @@ -3173,7 +3180,7 @@ FileFd* GetTempFile(std::string const &Prefix, bool ImmediateUnlink, FileFd * co delete Fd; return nullptr; } - if (ImmediateUnlink == false) + if (not ImmediateUnlink) Fd->SetFileName(fn); return Fd; } -- cgit v1.2.3