PageRenderTime 53ms CodeModel.GetById 36ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/interprocess/detail/tmp_dir_helpers.hpp

http://hadesmem.googlecode.com/
C++ Header | 172 lines | 136 code | 21 blank | 15 comment | 21 complexity | f0d54b8b1238034a56ad5e5aa4f7f1a2 MD5 | raw file
  1//////////////////////////////////////////////////////////////////////////////
  2//
  3// (C) Copyright Ion Gaztanaga 2007-2009. Distributed under the Boost
  4// Software License, Version 1.0. (See accompanying file
  5// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6//
  7// See http://www.boost.org/libs/interprocess for documentation.
  8//
  9//////////////////////////////////////////////////////////////////////////////
 10
 11#ifndef BOOST_INTERPROCESS_DETAIL_TMP_DIR_HELPERS_HPP
 12#define BOOST_INTERPROCESS_DETAIL_TMP_DIR_HELPERS_HPP
 13
 14#include <boost/interprocess/detail/config_begin.hpp>
 15#include <boost/interprocess/detail/workaround.hpp>
 16#include <boost/interprocess/detail/os_file_functions.hpp>
 17#include <boost/interprocess/errors.hpp>
 18#include <boost/interprocess/exceptions.hpp>
 19#include <string>
 20
 21#if defined(BOOST_INTERPROCESS_WINDOWS)
 22   #define BOOST_INTERPROCESS_HAS_WINDOWS_KERNEL_BOOTTIME
 23   #define BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME
 24   #include <boost/interprocess/detail/win32_api.hpp>
 25#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
 26   #include <sys/sysctl.h>
 27   #if defined(CTL_KERN) && defined (KERN_BOOTTIME)
 28      #define BOOST_INTERPROCESS_HAS_BSD_KERNEL_BOOTTIME
 29      #define BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME
 30   #endif
 31#endif
 32
 33namespace boost {
 34namespace interprocess {
 35namespace detail {
 36
 37#if defined (BOOST_INTERPROCESS_HAS_WINDOWS_KERNEL_BOOTTIME)
 38inline void get_bootstamp(std::string &s, bool add = false)
 39{
 40   std::string bootstamp;
 41   winapi::get_last_bootup_time(bootstamp);
 42   if(add){
 43      s += bootstamp;
 44   }
 45   else{
 46      s = bootstamp;
 47   }
 48}
 49#elif defined(BOOST_INTERPROCESS_HAS_BSD_KERNEL_BOOTTIME)
 50inline void get_bootstamp(std::string &s, bool add = false)
 51{
 52   // FreeBSD specific: sysctl "kern.boottime"
 53   int request[2] = { CTL_KERN, KERN_BOOTTIME };
 54   struct ::timeval result;
 55   size_t result_len = sizeof result;
 56
 57   if (::sysctl (request, 2, &result, &result_len, NULL, 0) < 0)
 58      return;
 59
 60   char bootstamp_str[256];
 61
 62   const char Characters [] =
 63      { '0', '1', '2', '3', '4', '5', '6', '7'
 64      , '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
 65
 66   std::size_t char_counter = 0;
 67   long fields[2] = { result.tv_sec, result.tv_usec };
 68   for(std::size_t field = 0; field != 2; ++field){
 69      for(std::size_t i = 0; i != sizeof(long); ++i){
 70         const char *ptr = (const char *)&fields[field];
 71         bootstamp_str[char_counter++] = Characters[(ptr[i]&0xF0)>>4];
 72         bootstamp_str[char_counter++] = Characters[(ptr[i]&0x0F)];
 73      }
 74   }
 75   bootstamp_str[char_counter] = 0;
 76   if(add){
 77      s += bootstamp_str;
 78   }
 79   else{
 80      s = bootstamp_str;
 81   }
 82}
 83#endif
 84
 85inline void get_tmp_base_dir(std::string &tmp_name)
 86{
 87   #if defined (BOOST_INTERPROCESS_WINDOWS)
 88   winapi::get_shared_documents_folder(tmp_name);
 89   if(tmp_name.empty()){
 90      tmp_name = get_temporary_path();
 91   }
 92   #else
 93   tmp_name = get_temporary_path();
 94   #endif
 95   if(tmp_name.empty()){
 96      error_info err = system_error_code();
 97      throw interprocess_exception(err);
 98   }
 99   //Remove final null.
100   tmp_name += "/boost_interprocess";
101}
102
103inline void tmp_folder(std::string &tmp_name)
104{
105   get_tmp_base_dir(tmp_name);
106   #ifdef BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME
107   tmp_name += "/";
108   get_bootstamp(tmp_name, true);
109   #endif
110}
111
112inline void tmp_filename(const char *filename, std::string &tmp_name)
113{
114   tmp_folder(tmp_name);
115   tmp_name += "/";
116   tmp_name += filename;
117}
118
119inline void create_tmp_and_clean_old(std::string &tmp_name)
120{
121   //First get the temp directory
122   std::string root_tmp_name;
123   get_tmp_base_dir(root_tmp_name);
124
125   //If fails, check that it's because already exists
126   if(!create_directory(root_tmp_name.c_str())){
127      error_info info(system_error_code());
128      if(info.get_error_code() != already_exists_error){
129         throw interprocess_exception(info);
130      }
131   }
132
133   #ifdef BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME
134   tmp_folder(tmp_name);
135
136   //If fails, check that it's because already exists
137   if(!create_directory(tmp_name.c_str())){
138      error_info info(system_error_code());
139      if(info.get_error_code() != already_exists_error){
140         throw interprocess_exception(info);
141      }
142   }
143   //Now erase all old directories created in the previous boot sessions
144   std::string subdir = tmp_name;
145   subdir.erase(0, root_tmp_name.size()+1);
146   delete_subdirectories(root_tmp_name, subdir.c_str());
147   #else
148   tmp_name = root_tmp_name;
149   #endif
150}
151
152inline void create_tmp_and_clean_old_and_get_filename(const char *filename, std::string &tmp_name)
153{
154   create_tmp_and_clean_old(tmp_name);
155   tmp_filename(filename, tmp_name);
156}
157
158inline void add_leading_slash(const char *name, std::string &new_name)
159{
160   if(name[0] != '/'){
161      new_name = '/';
162   }
163   new_name += name;
164}
165
166}  //namespace boost{
167}  //namespace interprocess {
168}  //namespace detail {
169
170#include <boost/interprocess/detail/config_end.hpp>
171
172#endif   //ifndef BOOST_INTERPROCESS_DETAIL_TMP_DIR_HELPERS_HPP