| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#ifndef DMLC_FILESYSTEM_H_ |
| 8 |
#define DMLC_FILESYSTEM_H_ |
| 9 |
|
| 10 |
#include <algorithm> |
| 11 |
#include <random> |
| 12 |
#include <string> |
| 13 |
#include <vector> |
| 14 |
|
| 15 |
#include <dmlc/io.h> |
| 16 |
#include <dmlc/logging.h> |
| 17 |
|
| 18 |
|
| 19 |
#ifdef _WIN32 |
| 20 |
#define NOMINMAX |
| 21 |
#include <shlwapi.h> |
| 22 |
#include <windows.h> |
| 23 |
#pragma comment(lib, "shlwapi.lib") |
| 24 |
#else |
| 25 |
#include <sys/stat.h> |
| 26 |
#include <sys/types.h> |
| 27 |
#include <unistd.h> |
| 28 |
#endif |
| 29 |
|
| 30 |
namespace dmlc { |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
class TemporaryDirectory { |
| 56 |
public: |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
explicit TemporaryDirectory(bool verbose = false) : verbose_(verbose) { |
| 63 |
#if _WIN32 |
| 64 |
|
| 65 |
char tmproot[MAX_PATH] = {0}; |
| 66 |
const DWORD dw_retval = GetTempPathA(MAX_PATH, tmproot); |
| 67 |
if (dw_retval > MAX_PATH || dw_retval == 0) { |
| 68 |
LOG(FATAL) << "TemporaryDirectory(): " |
| 69 |
<< "Could not create temporary directory"; |
| 70 |
} |
| 71 |
|
| 72 |
const std::string letters = "abcdefghijklmnopqrstuvwxyz0123456789_"; |
| 73 |
std::string uniqstr(8, '\0'); |
| 74 |
std::random_device rd; |
| 75 |
std::mt19937 gen(rd()); |
| 76 |
std::uniform_int_distribution<int> dis(0, letters.length() - 1); |
| 77 |
std::generate(uniqstr.begin(), uniqstr.end(), |
| 78 |
[&dis, &gen, &letters]() -> char { return letters[dis(gen)]; }); |
| 79 |
|
| 80 |
char tmpdir[MAX_PATH] = {0}; |
| 81 |
PathCombineA(tmpdir, tmproot, uniqstr.c_str()); |
| 82 |
if (!CreateDirectoryA(tmpdir, NULL)) { |
| 83 |
LOG(FATAL) << "TemporaryDirectory(): " |
| 84 |
<< "Could not create temporary directory"; |
| 85 |
} |
| 86 |
path = std::string(tmpdir); |
| 87 |
#else |
| 88 |
std::string tmproot; |
| 89 |
std::string dirtemplate; |
| 90 |
|
| 91 |
{ |
| 92 |
const char *tmpenv = getenv("TMPDIR"); |
| 93 |
if (tmpenv) { |
| 94 |
tmproot = std::string(tmpenv); |
| 95 |
|
| 96 |
while (tmproot.length() != 0 && tmproot[tmproot.length() - 1] == '/') { |
| 97 |
tmproot.resize(tmproot.length() - 1); |
| 98 |
} |
| 99 |
} else { |
| 100 |
tmproot = "/tmp"; |
| 101 |
} |
| 102 |
} |
| 103 |
dirtemplate = tmproot + "/tmpdir.XXXXXX"; |
| 104 |
std::vector<char> dirtemplate_buf(dirtemplate.begin(), dirtemplate.end()); |
| 105 |
dirtemplate_buf.push_back('\0'); |
| 106 |
char *tmpdir = mkdtemp(&dirtemplate_buf[0]); |
| 107 |
if (!tmpdir) { |
| 108 |
LOG(FATAL) << "TemporaryDirectory(): " |
| 109 |
<< "Could not create temporary directory"; |
| 110 |
} |
| 111 |
path = std::string(tmpdir); |
| 112 |
#endif |
| 113 |
if (verbose_) { |
| 114 |
LOG(INFO) << "Created temporary directory " << path; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
~TemporaryDirectory() { |
| 120 |
RecursiveDelete(path); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
std::string path; |
| 125 |
|
| 126 |
private: |
| 127 |
|
| 128 |
bool verbose_; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
inline bool IsSymlink(const std::string &path) { |
| 135 |
#ifdef _WIN32 |
| 136 |
DWORD attr = GetFileAttributesA(path.c_str()); |
| 137 |
CHECK_NE(attr, INVALID_FILE_ATTRIBUTES) |
| 138 |
<< "dmlc::TemporaryDirectory::IsSymlink(): Unable to read file attributes"; |
| 139 |
return attr & FILE_ATTRIBUTE_REPARSE_POINT; |
| 140 |
#else |
| 141 |
struct stat sb; |
| 142 |
CHECK_EQ(lstat(path.c_str(), &sb), 0) |
| 143 |
<< "dmlc::TemporaryDirectory::IsSymlink(): Unable to read file attributes"; |
| 144 |
return S_ISLNK(sb.st_mode); |
| 145 |
#endif |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
void RecursiveDelete(const std::string &path); |
| 153 |
}; |
| 154 |
|
| 155 |
} |
| 156 |
#endif |