Skip to content

Commit

Permalink
Separate a special function for creating shared folders as permission…
Browse files Browse the repository at this point in the history
…s and logic is different from normal folder open/create logic in Unix
  • Loading branch information
igaztanaga committed Nov 8, 2021
1 parent aea16e7 commit 8578684
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 deletions.
47 changes: 31 additions & 16 deletions include/boost/interprocess/detail/os_file_functions.hpp
Expand Up @@ -101,9 +101,23 @@ inline file_handle_t file_handle_from_mapping_handle(mapping_handle_t hnd)
{ return hnd.handle; }

template<class CharT>
inline bool create_directory(const CharT *path, bool = false)
inline bool create_directory(const CharT *path)
{ return winapi::create_directory(path); }

template<class CharT>
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<class CharT>
inline bool open_or_create_shared_directory(const CharT *path)
{
return open_or_create_directory(path);
}

template <class CharT>
inline bool remove_directory(const CharT *path)
{ return winapi::remove_directory(path); }
Expand Down Expand Up @@ -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 = ::mode_t(0777);
return ::mkdir(path, m) == 0;
}

inline bool open_or_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 || (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)
Expand Down Expand Up @@ -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;
Expand Down
Expand Up @@ -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);
}
Expand Down
12 changes: 4 additions & 8 deletions include/boost/interprocess/detail/shared_dir_helpers.hpp
Expand Up @@ -209,22 +209,18 @@ inline void create_shared_dir_and_clean_old(std::basic_string<CharT> &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<CharT> subdir = shared_dir;
Expand Down
4 changes: 2 additions & 2 deletions test/windows_eventlog_stamp_shared_memory_test.cpp
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions test/windows_shared_dir_func.cpp
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down

0 comments on commit 8578684

Please sign in to comment.