From c470d92366d7c3c239a689f0a10d6d0d9daafbff Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Mon, 18 May 2020 11:55:54 +0200 Subject: Allow prefix to be a complete filename for GetTempFile MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Our testcases had their own implementation of GetTempFile with the feature of a temporary file with a choosen suffix. Merging this into GetTempFile lets us drop this duplicate and hence test more our code rather than testing our helpers for test implementation. And then hashsums_test had another implementationā€¦ and extracttar wasn't even trying to use a real tempfileā€¦ one GetTempFile to rule them all! That also ensures that these tempfiles are created in a temporary directory rather than the current directory which is a nice touch and tries a little harder to clean up those tempfiles. --- apt-pkg/contrib/fileutl.cc | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 68dd8265c..e91c1acc3 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -3150,9 +3150,7 @@ FileFd* GetTempFile(std::string const &Prefix, bool ImmediateUnlink, FileFd * co } FileFd* GetTempFile(std::string const &Prefix, bool ImmediateUnlink, FileFd * const TmpFd, bool Buffered) { - char fn[512]; - FileFd * const Fd = TmpFd == nullptr ? new FileFd() : TmpFd; - + std::string fn; std::string const tempdir = GetTempDir(); int fd = -1; #ifdef O_TMPFILE @@ -3161,21 +3159,35 @@ FileFd* GetTempFile(std::string const &Prefix, bool ImmediateUnlink, FileFd * co if (fd < 0) #endif { - snprintf(fn, sizeof(fn), "%s/%s.XXXXXX", tempdir.c_str(), Prefix.c_str()); - fd = mkstemp(fn); - if (ImmediateUnlink) - unlink(fn); + auto const suffix = Prefix.find(".XXXXXX."); + std::vector buffer(tempdir.length() + 1 + Prefix.length() + (suffix == std::string::npos ? 7 : 0) + 1, '\0'); + if (suffix != std::string::npos) + { + if (snprintf(buffer.data(), buffer.size(), "%s/%s", tempdir.c_str(), Prefix.c_str()) > 0) + { + ssize_t const suffixlen = (buffer.size() - 1) - (tempdir.length() + 1 + suffix + 7); + if (likely(suffixlen > 0)) + fd = mkstemps(buffer.data(), suffixlen); + } + } + else + { + if (snprintf(buffer.data(), buffer.size(), "%s/%s.XXXXXX", tempdir.c_str(), Prefix.c_str()) > 0) + fd = mkstemp(buffer.data()); + } + fn.assign(buffer.data(), buffer.size() - 1); + if (ImmediateUnlink && fd != -1) + unlink(fn.c_str()); } if (fd < 0) { - _error->Errno("GetTempFile",_("Unable to mkstemp %s"), fn); - if (TmpFd == nullptr) - delete Fd; + _error->Errno("GetTempFile",_("Unable to mkstemp %s"), fn.c_str()); return nullptr; } - if (!Fd->OpenDescriptor(fd, FileFd::ReadWrite | (Buffered ? FileFd::BufferedWrite : 0), FileFd::None, true)) + FileFd * const Fd = TmpFd == nullptr ? new FileFd() : TmpFd; + if (not Fd->OpenDescriptor(fd, FileFd::ReadWrite | (Buffered ? FileFd::BufferedWrite : 0), FileFd::None, true)) { - _error->Errno("GetTempFile",_("Unable to write to %s"),fn); + _error->Errno("GetTempFile",_("Unable to write to %s"),fn.c_str()); if (TmpFd == nullptr) delete Fd; return nullptr; -- cgit v1.2.3