1 #include "filesystem.hpp"
15 # include <sys/types.h>
26 using Stat = struct _stat;
27 int do_stat(const char *path, Stat &info) {
28 return _stat(path, &info);
30 bool is_dir(const Stat &info) {
31 return (info.st_mode & _S_IFDIR) != 0;
33 bool is_file(const Stat &info) {
34 return (info.st_mode & _S_IFEG) != 0;
37 using Stat = struct stat;
38 int do_stat(const char *path, Stat &info) {
39 return stat(path, &info);
41 bool is_dir(const Stat &info) {
42 return S_ISDIR(info.st_mode);
44 bool is_file(const Stat &info) {
45 return S_ISREG(info.st_mode);
48 time_t get_mtime(const Stat &info) {
50 return info.st_mtimespec.tv_sec;
57 bool is_dir(const char *path) {
59 if (do_stat(path, info) != 0) {
65 bool is_file(const char *path) {
67 if (do_stat(path, info) != 0) {
73 time_t file_mtime(const char *path) {
75 if (do_stat(path, info) != 0) {
78 return get_mtime(info);
82 bool make_dir(const char *path) {
84 int ret = _mkdir(path);
86 int ret = mkdir(path, 0777);
92 bool make_dirs(const string &path) {
103 auto pos = path.find_last_of("\\/");
105 auto pos = path.find_last_of('/');
107 if (pos == string::npos) {
110 if (pos == path.length() - 1) {
111 // trailing separator, would make final make_dir fail
113 pos = path.find_last_of("\\/", pos - 1);
115 pos = path.find_last_of('/', pos - 1);
117 if (pos == string::npos) {
121 if (!make_dirs(path.substr(0, pos))) {
126 return make_dir(path);
129 // something's there, check if it's a dir and we're good
133 // whatever else went wrong, it can't be good
140 bool remove_file(const string &path) {
141 return remove(path.c_str()) == 0;
145 bool remove_dir(const string &path) {
148 // shamelessly stolen from http://www.codeguru.com/forum/showthread.php?t=239271
149 const string pattern = path + "\\*.*";
150 WIN32_FIND_DATA info;
151 HANDLE file = FindFirstFile(pattern.c_str(), &info);
152 if (file == INVALID_HANDLE_VALUE) {
153 // already non-existing
159 strncmp(info.cFileName, ".", 2) == 0 ||
160 strncmp(info.cFileName, "..", 3) == 0
164 const string sub_path = path + '\\' + info.cFileName;
165 if ((info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
166 if (!remove_dir(sub_path)) {
170 if (!SetFileAttributes(sub_path.c_str(), FILE_ATTRIBUTE_NORMAL)) {
173 if (!remove_file(sub_path)) {
177 } while (FindNextFile(file, &info));
180 DWORD error = GetLastError();
181 if (error != ERROR_NO_MORE_FILES) {
184 // is this (NORMAL vs DIRECTORY) really correct?
185 if (!SetFileAttributes(path.c_str(), FILE_ATTRIBUTE_NORMAL)) {
188 return RemoveDirectory(path.c_str());
192 DIR *dir = opendir(path.c_str());
193 for (dirent *entry = readdir(dir); entry != nullptr; entry = readdir(dir)) {
195 strncmp(entry->d_name, ".", 2) == 0 ||
196 strncmp(entry->d_name, "..", 3) == 0
200 const string sub_path = path + '/' + entry->d_name;
201 if (is_dir(sub_path)) {
202 if (!remove_dir(sub_path)) {
206 if (!remove_file(sub_path)) {
211 return remove(path.c_str()) == 0;
218 #if _DEFAULT_SOURCE || _BSD_SOURCE || _POSIX_C_SOURCE >= 200809L
219 char tmpl[] = "blank.XXXXXX";
220 const char *name = mkdtemp(tmpl);
222 throw runtime_error("unable to create temporary directory");
228 constexpr int max_tries = 10;
230 while (!make_dirs(name) && tries < max_tries) {
234 if (tries == max_tries) {
235 throw runtime_error("unable to create temporary directory");
241 TempDir::~TempDir() {
245 cerr << "warning: could not remove temp dir " << path << endl;