commit bad9e8b36a2d9f8447652807b6f4d65941b5b2cd
parent 5c6561bfe727d206140023c32dbd1e13e2a77b50
Author: cfillion <cfillion@users.noreply.github.com>
Date: Sat, 17 Jun 2017 20:25:51 -0400
filesystem: implement custom directory creation logic
...to avoid OS-dependent return value inconsistencies in REAPER's implementation
of RecuriveCreateDirectory. See <http://forum.cockos.com/showthread.php?p=1856571>.
Diffstat:
2 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/src/filesystem.cpp b/src/filesystem.cpp
@@ -175,8 +175,23 @@ bool FS::mkdir(const Path &path)
if(exists(path))
return true;
- const Path &fullPath = Path::prefixRoot(path);
- return RecursiveCreateDirectory(fullPath.join().c_str(), 0) == 0;
+ Path fullPath = Path::root();
+
+ for(const string &dir : path) {
+ fullPath.append(dir);
+
+ const auto_string &joined = make_autostring(fullPath.join());
+
+#ifdef _WIN32
+ if(!CreateDirectory(joined.c_str(), nullptr) && GetLastError() != ERROR_PATH_NOT_FOUND)
+ return false;
+#else
+ if(::mkdir(joined.c_str(), 0777) && errno != EEXIST)
+ return false;
+#endif
+ }
+
+ return true;
}
string FS::lastError()
diff --git a/src/path.hpp b/src/path.hpp
@@ -32,10 +32,11 @@ public:
static Path prefixRoot(const Path &p) { return s_root + p; }
static Path prefixRoot(const std::string &p) { return s_root + p; }
+ static const Path &root() { return s_root; }
Path(const std::string &path = std::string());
- void append(const std::string &part, bool traversal = true);
+ void append(const std::string &parts, bool traversal = true);
void append(const Path &other);
void remove(size_t pos, size_t count);
void removeLast();
@@ -52,6 +53,9 @@ public:
std::string last() const;
bool startsWith(const Path &) const;
+ std::list<std::string>::const_iterator begin() const { return m_parts.begin(); }
+ std::list<std::string>::const_iterator end() const { return m_parts.end(); }
+
bool operator==(const Path &) const;
bool operator!=(const Path &) const;
bool operator<(const Path &) const;