annotate DEPENDENCIES/generic/include/boost/interprocess/detail/shared_dir_helpers.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents f46d142149f5
children
rev   line source
Chris@102 1 //////////////////////////////////////////////////////////////////////////////
Chris@102 2 //
Chris@102 3 // (C) Copyright Ion Gaztanaga 2007-2014. Distributed under the Boost
Chris@102 4 // Software License, Version 1.0. (See accompanying file
Chris@102 5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@102 6 //
Chris@102 7 // See http://www.boost.org/libs/interprocess for documentation.
Chris@102 8 //
Chris@102 9 //////////////////////////////////////////////////////////////////////////////
Chris@102 10
Chris@102 11 #ifndef BOOST_INTERPROCESS_DETAIL_SHARED_DIR_HELPERS_HPP
Chris@102 12 #define BOOST_INTERPROCESS_DETAIL_SHARED_DIR_HELPERS_HPP
Chris@102 13
Chris@102 14 #ifndef BOOST_CONFIG_HPP
Chris@102 15 # include <boost/config.hpp>
Chris@102 16 #endif
Chris@102 17 #
Chris@102 18 #if defined(BOOST_HAS_PRAGMA_ONCE)
Chris@102 19 # pragma once
Chris@102 20 #endif
Chris@102 21
Chris@102 22 #include <boost/interprocess/detail/config_begin.hpp>
Chris@102 23 #include <boost/interprocess/detail/workaround.hpp>
Chris@102 24 #include <boost/interprocess/detail/os_file_functions.hpp>
Chris@102 25 #include <boost/interprocess/errors.hpp>
Chris@102 26 #include <boost/interprocess/exceptions.hpp>
Chris@102 27 #include <string>
Chris@102 28
Chris@102 29 #if defined(BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME) && defined(BOOST_INTERPROCESS_WINDOWS)
Chris@102 30 #include <boost/interprocess/detail/windows_intermodule_singleton.hpp>
Chris@102 31 #endif
Chris@102 32
Chris@102 33 namespace boost {
Chris@102 34 namespace interprocess {
Chris@102 35 namespace ipcdetail {
Chris@102 36
Chris@102 37 #if defined(BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME)
Chris@102 38 #if defined(BOOST_INTERPROCESS_WINDOWS)
Chris@102 39 //This type will initialize the stamp
Chris@102 40 struct windows_bootstamp
Chris@102 41 {
Chris@102 42 windows_bootstamp()
Chris@102 43 {
Chris@102 44 //Throw if bootstamp not available
Chris@102 45 if(!winapi::get_last_bootup_time(stamp)){
Chris@102 46 error_info err = system_error_code();
Chris@102 47 throw interprocess_exception(err);
Chris@102 48 }
Chris@102 49 }
Chris@102 50 //Use std::string. Even if this will be constructed in shared memory, all
Chris@102 51 //modules/dlls are from this process so internal raw pointers to heap are always valid
Chris@102 52 std::string stamp;
Chris@102 53 };
Chris@102 54
Chris@102 55 inline void get_bootstamp(std::string &s, bool add = false)
Chris@102 56 {
Chris@102 57 const windows_bootstamp &bootstamp = windows_intermodule_singleton<windows_bootstamp>::get();
Chris@102 58 if(add){
Chris@102 59 s += bootstamp.stamp;
Chris@102 60 }
Chris@102 61 else{
Chris@102 62 s = bootstamp.stamp;
Chris@102 63 }
Chris@102 64 }
Chris@102 65 #elif defined(BOOST_INTERPROCESS_HAS_BSD_KERNEL_BOOTTIME)
Chris@102 66 inline void get_bootstamp(std::string &s, bool add = false)
Chris@102 67 {
Chris@102 68 // FreeBSD specific: sysctl "kern.boottime"
Chris@102 69 int request[2] = { CTL_KERN, KERN_BOOTTIME };
Chris@102 70 struct ::timeval result;
Chris@102 71 std::size_t result_len = sizeof result;
Chris@102 72
Chris@102 73 if (::sysctl (request, 2, &result, &result_len, 0, 0) < 0)
Chris@102 74 return;
Chris@102 75
Chris@102 76 char bootstamp_str[256];
Chris@102 77
Chris@102 78 const char Characters [] =
Chris@102 79 { '0', '1', '2', '3', '4', '5', '6', '7'
Chris@102 80 , '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
Chris@102 81
Chris@102 82 std::size_t char_counter = 0;
Chris@102 83 //32 bit values to allow 32 and 64 bit process IPC
Chris@102 84 boost::uint32_t fields[2] = { boost::uint32_t(result.tv_sec), boost::uint32_t(result.tv_usec) };
Chris@102 85 for(std::size_t field = 0; field != 2; ++field){
Chris@102 86 for(std::size_t i = 0; i != sizeof(fields[0]); ++i){
Chris@102 87 const char *ptr = (const char *)&fields[field];
Chris@102 88 bootstamp_str[char_counter++] = Characters[(ptr[i]&0xF0)>>4];
Chris@102 89 bootstamp_str[char_counter++] = Characters[(ptr[i]&0x0F)];
Chris@102 90 }
Chris@102 91 }
Chris@102 92 bootstamp_str[char_counter] = 0;
Chris@102 93 if(add){
Chris@102 94 s += bootstamp_str;
Chris@102 95 }
Chris@102 96 else{
Chris@102 97 s = bootstamp_str;
Chris@102 98 }
Chris@102 99 }
Chris@102 100 #else
Chris@102 101 #error "BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME defined with no known implementation"
Chris@102 102 #endif
Chris@102 103 #endif //#if defined(BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME)
Chris@102 104
Chris@102 105 inline void get_shared_dir_root(std::string &dir_path)
Chris@102 106 {
Chris@102 107 #if defined (BOOST_INTERPROCESS_WINDOWS)
Chris@102 108 winapi::get_shared_documents_folder(dir_path);
Chris@102 109 #else
Chris@102 110 dir_path = "/tmp";
Chris@102 111 #endif
Chris@102 112 //We always need this path, so throw on error
Chris@102 113 if(dir_path.empty()){
Chris@102 114 error_info err = system_error_code();
Chris@102 115 throw interprocess_exception(err);
Chris@102 116 }
Chris@102 117 //Remove final null.
Chris@102 118 dir_path += "/boost_interprocess";
Chris@102 119 }
Chris@102 120
Chris@102 121 inline void get_shared_dir(std::string &shared_dir)
Chris@102 122 {
Chris@102 123 #if defined(BOOST_INTERPROCESS_SHARED_DIR_PATH)
Chris@102 124 shared_dir = BOOST_INTERPROCESS_SHARED_DIR_PATH;
Chris@102 125 #else
Chris@102 126 get_shared_dir_root(shared_dir);
Chris@102 127 #if defined(BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME)
Chris@102 128 shared_dir += "/";
Chris@102 129 get_bootstamp(shared_dir, true);
Chris@102 130 #endif
Chris@102 131 #endif
Chris@102 132 }
Chris@102 133
Chris@102 134 inline void shared_filepath(const char *filename, std::string &filepath)
Chris@102 135 {
Chris@102 136 get_shared_dir(filepath);
Chris@102 137 filepath += "/";
Chris@102 138 filepath += filename;
Chris@102 139 }
Chris@102 140
Chris@102 141 inline void create_shared_dir_and_clean_old(std::string &shared_dir)
Chris@102 142 {
Chris@102 143 #if defined(BOOST_INTERPROCESS_SHARED_DIR_PATH)
Chris@102 144 shared_dir = BOOST_INTERPROCESS_SHARED_DIR_PATH;
Chris@102 145 #else
Chris@102 146 //First get the temp directory
Chris@102 147 std::string root_shared_dir;
Chris@102 148 get_shared_dir_root(root_shared_dir);
Chris@102 149
Chris@102 150 //If fails, check that it's because already exists
Chris@102 151 if(!create_directory(root_shared_dir.c_str())){
Chris@102 152 error_info info(system_error_code());
Chris@102 153 if(info.get_error_code() != already_exists_error){
Chris@102 154 throw interprocess_exception(info);
Chris@102 155 }
Chris@102 156 }
Chris@102 157
Chris@102 158 #if defined(BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME)
Chris@102 159 get_shared_dir(shared_dir);
Chris@102 160
Chris@102 161 //If fails, check that it's because already exists
Chris@102 162 if(!create_directory(shared_dir.c_str())){
Chris@102 163 error_info info(system_error_code());
Chris@102 164 if(info.get_error_code() != already_exists_error){
Chris@102 165 throw interprocess_exception(info);
Chris@102 166 }
Chris@102 167 }
Chris@102 168 //Now erase all old directories created in the previous boot sessions
Chris@102 169 std::string subdir = shared_dir;
Chris@102 170 subdir.erase(0, root_shared_dir.size()+1);
Chris@102 171 delete_subdirectories(root_shared_dir, subdir.c_str());
Chris@102 172 #else
Chris@102 173 shared_dir = root_shared_dir;
Chris@102 174 #endif
Chris@102 175 #endif
Chris@102 176 }
Chris@102 177
Chris@102 178 inline void create_shared_dir_cleaning_old_and_get_filepath(const char *filename, std::string &shared_dir)
Chris@102 179 {
Chris@102 180 create_shared_dir_and_clean_old(shared_dir);
Chris@102 181 shared_dir += "/";
Chris@102 182 shared_dir += filename;
Chris@102 183 }
Chris@102 184
Chris@102 185 inline void add_leading_slash(const char *name, std::string &new_name)
Chris@102 186 {
Chris@102 187 if(name[0] != '/'){
Chris@102 188 new_name = '/';
Chris@102 189 }
Chris@102 190 new_name += name;
Chris@102 191 }
Chris@102 192
Chris@102 193 } //namespace boost{
Chris@102 194 } //namespace interprocess {
Chris@102 195 } //namespace ipcdetail {
Chris@102 196
Chris@102 197 #include <boost/interprocess/detail/config_end.hpp>
Chris@102 198
Chris@102 199 #endif //ifndef BOOST_INTERPROCESS_DETAIL_SHARED_DIR_HELPERS_HPP