diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/system/mktemp.cpp | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/system/mktemp.cpp')
-rw-r--r-- | util/system/mktemp.cpp | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/util/system/mktemp.cpp b/util/system/mktemp.cpp new file mode 100644 index 0000000000..505b7b4a4b --- /dev/null +++ b/util/system/mktemp.cpp @@ -0,0 +1,73 @@ +#include "tempfile.h" + +#include <util/folder/dirut.h> +#include <util/generic/yexception.h> +#include <util/stream/file.h> + +#include <cerrno> +#include <cstring> + +#ifdef _win32_ + #include "winint.h" + #include <io.h> +#else + #include <unistd.h> + #include <stdlib.h> +#endif + +extern "C" int mkstemps(char* path, int slen); + +TString MakeTempName(const char* wrkDir, const char* prefix, const char* extension) { +#ifndef _win32_ + TString filePath; + + if (wrkDir && *wrkDir) { + filePath += wrkDir; + } else { + filePath += GetSystemTempDir(); + } + + if (filePath.back() != '/') { + filePath += '/'; + } + + if (prefix) { + filePath += prefix; + } + + filePath += "XXXXXX"; // mkstemps requirement + + size_t extensionPartLength = 0; + if (extension && *extension) { + if (extension[0] != '.') { + filePath += '.'; + extensionPartLength += 1; + } + filePath += extension; + extensionPartLength += strlen(extension); + } + + int fd = mkstemps(const_cast<char*>(filePath.data()), extensionPartLength); + if (fd >= 0) { + close(fd); + return filePath; + } +#else + char tmpDir[MAX_PATH + 1]; // +1 -- for terminating null character + char filePath[MAX_PATH]; + const char* pDir = 0; + + if (wrkDir && *wrkDir) { + pDir = wrkDir; + } else if (GetTempPath(MAX_PATH + 1, tmpDir)) { + pDir = tmpDir; + } + + // it always takes up to 3 characters, no more + if (GetTempFileName(pDir, (prefix) ? (prefix) : "yan", 0, filePath)) { + return filePath; + } +#endif + + ythrow TSystemError() << "can not create temp name(" << wrkDir << ", " << prefix << ", " << extension << ")"; +} |