1 #include "filesystem.hpp"
3 #include "../app/error.hpp"
17 # include <sys/types.h>
28 using Stat = struct _stat;
29 int do_stat(const char *path, Stat &info) {
30 return _stat(path, &info);
32 bool is_dir(const Stat &info) {
33 return (info.st_mode & _S_IFDIR) != 0;
35 bool is_file(const Stat &info) {
36 return (info.st_mode & _S_IFEG) != 0;
39 using Stat = struct stat;
40 int do_stat(const char *path, Stat &info) {
41 return stat(path, &info);
43 bool is_dir(const Stat &info) {
44 return S_ISDIR(info.st_mode);
46 bool is_file(const Stat &info) {
47 return S_ISREG(info.st_mode);
50 time_t get_mtime(const Stat &info) {
52 return info.st_mtimespec.tv_sec;
59 bool is_dir(const char *path) {
61 if (do_stat(path, info) != 0) {
67 bool is_file(const char *path) {
69 if (do_stat(path, info) != 0) {
75 time_t file_mtime(const char *path) {
77 if (do_stat(path, info) != 0) {
80 return get_mtime(info);
84 bool make_dir(const char *path) {
86 int ret = _mkdir(path);
88 int ret = mkdir(path, 0777);
94 bool make_dirs(const string &path) {
105 auto pos = path.find_last_of("\\/");
107 auto pos = path.find_last_of('/');
109 if (pos == string::npos) {
112 if (pos == path.length() - 1) {
113 // trailing separator, would make final make_dir fail
115 pos = path.find_last_of("\\/", pos - 1);
117 pos = path.find_last_of('/', pos - 1);
119 if (pos == string::npos) {
123 if (!make_dirs(path.substr(0, pos))) {
128 return make_dir(path);
131 // something's there, check if it's a dir and we're good
135 // whatever else went wrong, it can't be good
142 bool remove_file(const string &path) {
143 return remove(path.c_str()) == 0;
147 bool remove_dir(const string &path) {
150 // shamelessly stolen from http://www.codeguru.com/forum/showthread.php?t=239271
151 const string pattern = path + "\\*.*";
152 WIN32_FIND_DATA info;
153 HANDLE file = FindFirstFile(pattern.c_str(), &info);
154 if (file == INVALID_HANDLE_VALUE) {
155 // already non-existing
161 strncmp(info.cFileName, ".", 2) == 0 ||
162 strncmp(info.cFileName, "..", 3) == 0
166 const string sub_path = path + '\\' + info.cFileName;
167 if ((info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
168 if (!remove_dir(sub_path)) {
172 if (!SetFileAttributes(sub_path.c_str(), FILE_ATTRIBUTE_NORMAL)) {
175 if (!remove_file(sub_path)) {
179 } while (FindNextFile(file, &info));
182 DWORD error = GetLastError();
183 if (error != ERROR_NO_MORE_FILES) {
186 // is this (NORMAL vs DIRECTORY) really correct?
187 if (!SetFileAttributes(path.c_str(), FILE_ATTRIBUTE_NORMAL)) {
190 return RemoveDirectory(path.c_str());
194 DIR *dir = opendir(path.c_str());
195 for (dirent *entry = readdir(dir); entry != nullptr; entry = readdir(dir)) {
197 strncmp(entry->d_name, ".", 2) == 0 ||
198 strncmp(entry->d_name, "..", 3) == 0
202 const string sub_path = path + '/' + entry->d_name;
203 if (is_dir(sub_path)) {
204 if (!remove_dir(sub_path)) {
208 if (!remove_file(sub_path)) {
213 return remove(path.c_str()) == 0;
220 #if _DEFAULT_SOURCE || _BSD_SOURCE || _POSIX_C_SOURCE >= 200809L
221 char tmpl[] = "blank.XXXXXX";
222 const char *name = mkdtemp(tmpl);
224 throw SysError("unable to create temporary directory");
230 constexpr int max_tries = 10;
232 while (!make_dirs(name) && tries < max_tries) {
236 if (tries == max_tries) {
237 throw runtime_error("unable to create temporary directory");
243 TempDir::~TempDir() {
247 cerr << "warning: could not remove temp dir " << path << endl;