diff --git a/include/boost/interprocess/detail/os_file_functions.hpp b/include/boost/interprocess/detail/os_file_functions.hpp index 79d47fa..7f389c3 100644 --- a/include/boost/interprocess/detail/os_file_functions.hpp +++ b/include/boost/interprocess/detail/os_file_functions.hpp @@ -101,9 +101,23 @@ inline file_handle_t file_handle_from_mapping_handle(mapping_handle_t hnd) { return hnd.handle; } template -inline bool create_directory(const CharT *path, bool = false) +inline bool create_directory(const CharT *path) { return winapi::create_directory(path); } +template +inline bool open_or_create_directory(const CharT *path) +{ + //If fails, check that it's because it already exists + return create_directory(path) + || error_info(system_error_code()).get_error_code() == already_exists_error; +} + +template +inline bool open_or_create_shared_directory(const CharT *path) +{ + return open_or_create_directory(path); +} + template inline bool remove_directory(const CharT *path) { return winapi::remove_directory(path); } @@ -493,10 +507,23 @@ inline mapping_handle_t mapping_handle_from_file_handle(file_handle_t hnd) inline file_handle_t file_handle_from_mapping_handle(mapping_handle_t hnd) { return hnd.handle; } -inline bool create_directory(const char *path, bool is_shared_dir = false) +inline bool create_directory(const char *path) { - ::mode_t m = is_shared_dir ? 01777 : 0777; - return ::mkdir(path, m) == 0 && ::chmod(path, m) == 0; + ::mode_t m = ::mode_t(0777); + return ::mkdir(path, m) == 0; +} + +inline bool open_or_create_directory(const char *path) +{ + ::mode_t m = ::mode_t(0777); + return ::mkdir(path, m) == 0 || (errno == EEXIST); +} + +inline bool open_or_create_shared_directory(const char *path) +{ + ::mode_t m = ::mode_t(01777); + const bool created_or_exists = (::mkdir(path, m) == 0) || (errno == EEXIST); + return created_or_exists && (::chmod(path, m) == 0); } inline bool remove_directory(const char *path) @@ -785,18 +812,6 @@ inline bool delete_subdirectories(const std::string &refcstrRootDirectory, const #endif //#if defined (BOOST_INTERPROCESS_WINDOWS) -inline bool open_or_create_directory(const char *dir_name, bool is_shared_dir = false) -{ - //If fails, check that it's because it already exists - if(!create_directory(dir_name, is_shared_dir)){ - error_info info(system_error_code()); - if(info.get_error_code() != already_exists_error){ - return false; - } - } - return true; -} - inline std::string get_temporary_path() { std::size_t required_len = 0; diff --git a/include/boost/interprocess/detail/portable_intermodule_singleton.hpp b/include/boost/interprocess/detail/portable_intermodule_singleton.hpp index 12fd0e7..32e196a 100644 --- a/include/boost/interprocess/detail/portable_intermodule_singleton.hpp +++ b/include/boost/interprocess/detail/portable_intermodule_singleton.hpp @@ -52,7 +52,7 @@ static void create_tmp_subdir_and_get_pid_based_filepath create_shared_dir_and_clean_old(s); s += "/"; s += subdir_name; - if(!open_or_create_directory(s.c_str(), true)){ + if(!open_or_create_shared_directory(s.c_str())){ error_info err = system_error_code(); throw interprocess_exception(err); } diff --git a/include/boost/interprocess/detail/shared_dir_helpers.hpp b/include/boost/interprocess/detail/shared_dir_helpers.hpp index 17c4fd8..8d9d36f 100644 --- a/include/boost/interprocess/detail/shared_dir_helpers.hpp +++ b/include/boost/interprocess/detail/shared_dir_helpers.hpp @@ -209,22 +209,18 @@ inline void create_shared_dir_and_clean_old(std::basic_string &shared_dir get_shared_dir_root(root_shared_dir); //If fails, check that it's because already exists - if(!create_directory(root_shared_dir.c_str(), true)){ + if(!open_or_create_shared_directory(root_shared_dir.c_str())){ error_info info(system_error_code()); - if(info.get_error_code() != already_exists_error){ - throw interprocess_exception(info); - } + throw interprocess_exception(info); } #if defined(BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME) get_shared_dir(shared_dir); //If fails, check that it's because already exists - if(!create_directory(shared_dir.c_str(), true)){ + if(!open_or_create_shared_directory(shared_dir.c_str())){ error_info info(system_error_code()); - if(info.get_error_code() != already_exists_error){ - throw interprocess_exception(info); - } + throw interprocess_exception(info); } //Now erase all old directories created in the previous boot sessions std::basic_string subdir = shared_dir; diff --git a/test/windows_eventlog_stamp_shared_memory_test.cpp b/test/windows_eventlog_stamp_shared_memory_test.cpp index 0ae8755..16a0d45 100644 --- a/test/windows_eventlog_stamp_shared_memory_test.cpp +++ b/test/windows_eventlog_stamp_shared_memory_test.cpp @@ -38,7 +38,7 @@ inline void get_shared_dir(std::string &shared_dir) shared_dir += "/boostipctesteventlog_"; shared_dir += boost::interprocess::test::get_process_id_name(); if(!dir_created) - ipcdetail::create_directory(shared_dir.c_str(), true); + ipcdetail::open_or_create_shared_directory(shared_dir.c_str()); dir_created = true; } @@ -48,7 +48,7 @@ inline void get_shared_dir(std::wstring &shared_dir) shared_dir += L"/boostipctesteventlog_"; shared_dir += boost::interprocess::test::get_process_id_wname(); if(!dir_created) - ipcdetail::create_directory(shared_dir.c_str(), true); + ipcdetail::open_or_create_shared_directory(shared_dir.c_str()); dir_created = true; } diff --git a/test/windows_shared_dir_func.cpp b/test/windows_shared_dir_func.cpp index 2cb1313..e2f1898 100644 --- a/test/windows_shared_dir_func.cpp +++ b/test/windows_shared_dir_func.cpp @@ -31,7 +31,7 @@ inline void get_shared_dir(std::string &shared_dir) shared_dir += "/boostipctest_"; shared_dir += boost::interprocess::test::get_process_id_name(); if(!dir_created) - ipcdetail::create_directory(shared_dir.c_str(), true); + ipcdetail::open_or_create_shared_directory(shared_dir.c_str()); dir_created = true; } @@ -41,7 +41,7 @@ inline void get_shared_dir(std::wstring &shared_dir) shared_dir += L"/boostipctest_"; shared_dir += boost::interprocess::test::get_process_id_wname(); if(!dir_created) - ipcdetail::create_directory(shared_dir.c_str(), true); + ipcdetail::open_or_create_shared_directory(shared_dir.c_str()); dir_created = true; }