summaryrefslogtreecommitdiff
path: root/apt-pkg/contrib/fileutl.cc
diff options
context:
space:
mode:
authorDavid Kalnischkies <david@kalnischkies.de>2020-05-13 08:42:02 +0200
committerDavid Kalnischkies <david@kalnischkies.de>2020-05-18 15:55:36 +0200
commit37cc8dcda9e97e0b9420d37bb886081fa629847d (patch)
treecdae20c9c041a364d8d2cf2b1a666f5f0064c2f7 /apt-pkg/contrib/fileutl.cc
parentfb6366c55faff93bd7c897d2f299d38c4acf5e89 (diff)
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.
Diffstat (limited to 'apt-pkg/contrib/fileutl.cc')
-rw-r--r--apt-pkg/contrib/fileutl.cc17
1 files changed, 12 insertions, 5 deletions
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;
}