summaryrefslogtreecommitdiff
path: root/apt-pkg/contrib
diff options
context:
space:
mode:
authorDavid Kalnischkies <david@kalnischkies.de>2020-05-18 11:55:54 +0200
committerDavid Kalnischkies <david@kalnischkies.de>2020-05-18 15:55:36 +0200
commitc470d92366d7c3c239a689f0a10d6d0d9daafbff (patch)
tree955534f65524c3f81f36119012b6cd995a9619d0 /apt-pkg/contrib
parent37cc8dcda9e97e0b9420d37bb886081fa629847d (diff)
Allow prefix to be a complete filename for GetTempFile
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.
Diffstat (limited to 'apt-pkg/contrib')
-rw-r--r--apt-pkg/contrib/fileutl.cc36
1 files changed, 24 insertions, 12 deletions
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<char> 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;