]> git.tdb.fi Git - libs/core.git/blobdiff - source/fs/windows/dir.cpp
Split getcwd and chdir to platform files
[libs/core.git] / source / fs / windows / dir.cpp
index 6b63b471d5bc939fe11a78ccbc668cdb02573c8c..151cc1b0944c4b455655c1f426da09c0ee81f89f 100644 (file)
@@ -1,4 +1,5 @@
 #include <shlobj.h>
+#include <msp/core/mutex.h>
 #include <msp/core/systemerror.h>
 #include "dir.h"
 
@@ -19,6 +20,43 @@ void rmdir(const Path &path)
                throw system_error("RemoveDirectory");
 }
 
+/* Windows documentation says Get/SetCurrentDirectory use a global buffer and
+are not thread safe. */
+static Mutex &cwd_mutex()
+{
+       static Mutex mutex;
+       return mutex;
+}
+
+Path getcwd()
+{
+       MutexLock lock(cwd_mutex());
+
+       char buf[1024];
+       Path result;
+       DWORD len = GetCurrentDirectory(sizeof(buf), buf);
+       if(len>=sizeof(buf))
+       {
+               vector<char> buf2(len+1);
+               len = GetCurrentDirectory(buf2.size(), buf2.data());
+               result = buf2.data();
+       }
+       else
+               result = buf;
+
+       if(!len)
+               throw system_error("GetCurrentDirectory");
+
+       return result;
+}
+
+void chdir(const Path &path)
+{
+       MutexLock lock(cwd_mutex());
+       if(!SetCurrentDirectory(path.c_str()))
+               throw system_error("SetCurrentDirectory");
+}
+
 Path get_home_dir()
 {
        char home[MAX_PATH];