7 #ifndef DMLC_FILESYSTEM_H_ 8 #define DMLC_FILESYSTEM_H_ 10 #include <dmlc/logging.h> 22 #pragma comment(lib, "Shlwapi.lib") 26 #include <sys/types.h> 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";
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 {
79 return letters[dis(gen)];
82 char tmpdir[MAX_PATH] = {0};
83 PathCombineA(tmpdir, tmproot, uniqstr.c_str());
84 if (!CreateDirectoryA(tmpdir, NULL)) {
85 LOG(FATAL) <<
"TemporaryDirectory(): " 86 <<
"Could not create temporary directory";
88 path = std::string(tmpdir);
91 std::string dirtemplate;
94 const char* tmpenv = getenv(
"TMPDIR");
96 tmproot = std::string(tmpenv);
98 while (tmproot.length() != 0 && tmproot[tmproot.length() - 1] ==
'/') {
99 tmproot.resize(tmproot.length() - 1);
105 dirtemplate = tmproot +
"/tmpdir.XXXXXX";
106 std::vector<char> dirtemplate_buf(dirtemplate.begin(), dirtemplate.end());
107 dirtemplate_buf.push_back(
'\0');
108 char* tmpdir = mkdtemp(&dirtemplate_buf[0]);
110 LOG(FATAL) <<
"TemporaryDirectory(): " 111 <<
"Could not create temporary directory";
113 path = std::string(tmpdir);
116 LOG(INFO) <<
"Created temporary directory " <<
path;
122 RecursiveDelete(
path);
136 inline bool IsSymlink(
const std::string& path) {
138 DWORD attr = GetFileAttributesA(path.c_str());
139 CHECK_NE(attr, INVALID_FILE_ATTRIBUTES)
140 <<
"dmlc::TemporaryDirectory::IsSymlink(): Unable to read file attributes";
141 return attr & FILE_ATTRIBUTE_REPARSE_POINT;
144 CHECK_EQ(lstat(path.c_str(), &sb), 0)
145 <<
"dmlc::TemporaryDirectory::IsSymlink(): Unable to read file attributes";
146 return S_ISLNK(sb.st_mode);
154 void RecursiveDelete(
const std::string& path);
158 #endif // DMLC_FILESYSTEM_H_ std::string path
Full path of the temporary directory.
Definition: filesystem.h:126
TemporaryDirectory(bool verbose=false)
Default constructor. Creates a new temporary directory with a unique name.
Definition: filesystem.h:61
namespace for dmlc
Definition: array_view.h:12
~TemporaryDirectory()
Destructor. Will perform recursive deletion via RecursiveDelete()
Definition: filesystem.h:121
Manager class for temporary directories. Whenever a new TemporaryDirectory object is constructed...
Definition: filesystem.h:54