X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ffs%2Fwindows%2Fdir.cpp;h=3d2bf6e65a8f5fd461a48d51d595265c1bad2fec;hb=f635ee3173be05375cd7d5c7a8edfbfb61f70d60;hp=6b63b471d5bc939fe11a78ccbc668cdb02573c8c;hpb=cc7c798b275b8ea9992eb82d68177ecfd50e0974;p=libs%2Fcore.git diff --git a/source/fs/windows/dir.cpp b/source/fs/windows/dir.cpp index 6b63b47..3d2bf6e 100644 --- a/source/fs/windows/dir.cpp +++ b/source/fs/windows/dir.cpp @@ -1,4 +1,5 @@ -#include +#include +#include #include #include "dir.h" @@ -19,22 +20,41 @@ void rmdir(const Path &path) throw system_error("RemoveDirectory"); } -Path get_home_dir() +/* Windows documentation says Get/SetCurrentDirectory use a global buffer and +are not thread safe. */ +static Mutex &cwd_mutex() { - char home[MAX_PATH]; - if(SHGetFolderPath(0, CSIDL_PERSONAL, 0, 0, home)==S_OK) - return home; - return "."; + static Mutex mutex; + return mutex; } -Path get_user_data_dir(const string &appname) +Path getcwd() { - if(appname.empty()) - throw invalid_argument("get_user_data_dir"); - char datadir[MAX_PATH]; - if(SHGetFolderPath(0, CSIDL_LOCAL_APPDATA, 0, 0, datadir)==S_OK) - return Path(datadir)/appname; - return "."; + MutexLock lock(cwd_mutex()); + + char buf[1024]; + Path result; + DWORD len = GetCurrentDirectory(sizeof(buf), buf); + if(len>=sizeof(buf)) + { + vector 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"); } } // namespace FS