| 15 | extern "C" int mkstemps(char* path, int slen); |
| 16 | |
| 17 | TString MakeTempName(const char* wrkDir, const char* prefix, const char* extension) { |
| 18 | TString filePath; |
| 19 | |
| 20 | if (wrkDir && *wrkDir) { |
| 21 | filePath += wrkDir; |
| 22 | } else { |
| 23 | filePath += GetSystemTempDir(); |
| 24 | } |
| 25 | |
| 26 | #ifdef _win32_ |
| 27 | // https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettempfilenamea?redirectedfrom=MSDN |
| 28 | const unsigned int DirPathMaxLen = 247; |
| 29 | if (filePath.length() <= DirPathMaxLen) { |
| 30 | // it always takes up to 3 characters, no more |
| 31 | char winFilePath[MAX_PATH]; |
| 32 | if (GetTempFileName(filePath.c_str(), (prefix) ? (prefix) : "yan", 0, |
| 33 | winFilePath)) { |
| 34 | return winFilePath; |
| 35 | } |
| 36 | } |
| 37 | #endif // _win32_ |
| 38 | |
| 39 | if (filePath.back() != '/') { |
| 40 | filePath += '/'; |
| 41 | } |
| 42 | |
| 43 | if (prefix) { |
| 44 | filePath += prefix; |
| 45 | } |
| 46 | |
| 47 | filePath += "XXXXXX"; // mkstemps requirement |
| 48 | |
| 49 | size_t extensionPartLength = 0; |
| 50 | if (extension && *extension) { |
| 51 | if (extension[0] != '.') { |
| 52 | filePath += '.'; |
| 53 | extensionPartLength += 1; |
| 54 | } |
| 55 | filePath += extension; |
| 56 | extensionPartLength += strlen(extension); |
| 57 | } |
| 58 | |
| 59 | int fd = mkstemps(const_cast<char*>(filePath.data()), extensionPartLength); |
| 60 | if (fd >= 0) { |
| 61 | close(fd); |
| 62 | return filePath; |
| 63 | } |
| 64 | |
| 65 | ythrow TSystemError() << "can not create temp name(" << wrkDir << ", " << prefix << ", " << extension << ")"; |
| 66 | } |