os_file_functions.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2012. 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. #ifndef BOOST_INTERPROCESS_DETAIL_OS_FILE_FUNCTIONS_HPP
  11. #define BOOST_INTERPROCESS_DETAIL_OS_FILE_FUNCTIONS_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #
  16. #if defined(BOOST_HAS_PRAGMA_ONCE)
  17. # pragma once
  18. #endif
  19. #include <boost/interprocess/detail/config_begin.hpp>
  20. #include <boost/interprocess/detail/workaround.hpp>
  21. #include <boost/interprocess/errors.hpp>
  22. #include <boost/interprocess/permissions.hpp>
  23. #include <boost/static_assert.hpp>
  24. #include <climits>
  25. #include <string>
  26. #include <boost/move/detail/type_traits.hpp> //make_unsigned
  27. #if defined (BOOST_INTERPROCESS_WINDOWS)
  28. # include <boost/interprocess/detail/win32_api.hpp>
  29. # include <wchar.h> //wcsxxx()
  30. #else
  31. # ifdef BOOST_HAS_UNISTD_H
  32. # include <fcntl.h>
  33. # include <unistd.h>
  34. # include <sys/types.h>
  35. # include <sys/stat.h>
  36. # include <dirent.h>
  37. # include <cerrno>
  38. # include <cstdio>
  39. # if 0
  40. # include <sys/file.h>
  41. # endif
  42. # else
  43. # error Unknown platform
  44. # endif
  45. #endif
  46. #include <cstring>
  47. #include <cstdlib>
  48. namespace boost {
  49. namespace interprocess {
  50. #if defined (BOOST_INTERPROCESS_WINDOWS)
  51. typedef void * file_handle_t;
  52. typedef __int64 offset_t;
  53. typedef struct mapping_handle_impl_t{
  54. void * handle;
  55. bool is_shm;
  56. } mapping_handle_t;
  57. typedef enum { read_only = winapi::generic_read
  58. , read_write = winapi::generic_read | winapi::generic_write
  59. , copy_on_write
  60. , read_private
  61. , invalid_mode = 0xffff
  62. } mode_t;
  63. typedef enum { file_begin = winapi::file_begin
  64. , file_end = winapi::file_end
  65. , file_current = winapi::file_current
  66. } file_pos_t;
  67. typedef unsigned long map_options_t;
  68. static const map_options_t default_map_options = map_options_t(-1);
  69. namespace ipcdetail{
  70. inline mapping_handle_t mapping_handle_from_file_handle(file_handle_t hnd)
  71. {
  72. mapping_handle_t ret;
  73. ret.handle = hnd;
  74. ret.is_shm = false;
  75. return ret;
  76. }
  77. inline mapping_handle_t mapping_handle_from_shm_handle(file_handle_t hnd)
  78. {
  79. mapping_handle_t ret;
  80. ret.handle = hnd;
  81. ret.is_shm = true;
  82. return ret;
  83. }
  84. inline file_handle_t file_handle_from_mapping_handle(mapping_handle_t hnd)
  85. { return hnd.handle; }
  86. template<class CharT>
  87. inline bool create_directory(const CharT *path)
  88. { return winapi::create_directory(path); }
  89. template<class CharT>
  90. inline bool open_or_create_directory(const CharT *path)
  91. {
  92. //If fails, check that it's because it already exists
  93. return create_directory(path)
  94. || error_info(system_error_code()).get_error_code() == already_exists_error;
  95. }
  96. template<class CharT>
  97. inline bool open_or_create_shared_directory(const CharT *path)
  98. {
  99. return open_or_create_directory(path);
  100. }
  101. template <class CharT>
  102. inline bool remove_directory(const CharT *path)
  103. { return winapi::remove_directory(path); }
  104. inline bool get_temporary_path(char *buffer, std::size_t buf_len, std::size_t &required_len)
  105. {
  106. required_len = 0;
  107. //std::size_t is always bigger or equal than unsigned long in Windows systems
  108. //In case std::size_t is bigger than unsigned long
  109. unsigned long buf = buf_len;
  110. if(buf_len != buf){ //maybe overflowed
  111. return false;
  112. }
  113. required_len = winapi::get_temp_path(buf_len, buffer);
  114. const bool ret = required_len && (buf_len > required_len);
  115. if(ret && buffer[required_len-1] == '\\'){
  116. buffer[required_len-1] = '\0';
  117. }
  118. return ret;
  119. }
  120. inline bool get_temporary_path(wchar_t *buffer, std::size_t buf_len, std::size_t &required_len)
  121. {
  122. required_len = 0;
  123. //std::size_t is always bigger or equal than unsigned long in Windows systems
  124. //In case std::size_t is bigger than unsigned long
  125. unsigned long buf = buf_len;
  126. if(buf_len != buf){ //maybe overflowed
  127. return false;
  128. }
  129. required_len = winapi::get_temp_path(buf_len, buffer);
  130. const bool ret = !(buf_len < required_len);
  131. if(ret && buffer[required_len-1] == L'\\'){
  132. buffer[required_len-1] = L'\0';
  133. }
  134. return ret;
  135. }
  136. template<class CharT>
  137. inline file_handle_t create_new_file
  138. (const CharT *name, mode_t mode, const permissions & perm = permissions(), bool temporary = false)
  139. {
  140. unsigned long attr = temporary ? winapi::file_attribute_temporary : 0;
  141. return winapi::create_file
  142. ( name, (unsigned int)mode, winapi::create_new, attr
  143. , (winapi::interprocess_security_attributes*)perm.get_permissions());
  144. }
  145. template <class CharT>
  146. inline file_handle_t create_or_open_file
  147. (const CharT *name, mode_t mode, const permissions & perm = permissions(), bool temporary = false)
  148. {
  149. unsigned long attr = temporary ? winapi::file_attribute_temporary : 0;
  150. return winapi::create_file
  151. ( name, (unsigned int)mode, winapi::open_always, attr
  152. , (winapi::interprocess_security_attributes*)perm.get_permissions());
  153. }
  154. template<class CharT>
  155. inline file_handle_t open_existing_file
  156. (const CharT *name, mode_t mode, bool temporary = false)
  157. {
  158. unsigned long attr = temporary ? winapi::file_attribute_temporary : 0;
  159. return winapi::create_file
  160. (name, (unsigned int)mode, winapi::open_existing, attr, 0);
  161. }
  162. inline bool delete_file(const char *name)
  163. { return winapi::unlink_file(name); }
  164. inline bool delete_file(const wchar_t *name)
  165. { return winapi::unlink_file(name); }
  166. inline bool truncate_file (file_handle_t hnd, std::size_t size)
  167. {
  168. offset_t filesize;
  169. if(!winapi::get_file_size(hnd, filesize))
  170. return false;
  171. typedef ::boost::move_detail::make_unsigned<offset_t>::type uoffset_t;
  172. const uoffset_t max_filesize = uoffset_t(-1)/2u;
  173. const uoffset_t uoff_size = uoffset_t(size);
  174. //Avoid unused variable warnings in 32 bit systems
  175. if(uoff_size > max_filesize){
  176. winapi::set_last_error(winapi::error_file_too_large);
  177. return false;
  178. }
  179. if(offset_t(size) > filesize){
  180. if(!winapi::set_file_pointer(hnd, filesize, 0, winapi::file_begin)){
  181. return false;
  182. }
  183. //We will write zeros in the end of the file
  184. //since set_end_of_file does not guarantee this
  185. for(std::size_t remaining = size - std::size_t(filesize), write_size = 0
  186. ;remaining > 0
  187. ;remaining -= write_size){
  188. const std::size_t DataSize = 512;
  189. static char data [DataSize];
  190. write_size = DataSize < remaining ? DataSize : remaining;
  191. unsigned long written;
  192. winapi::write_file(hnd, data, (unsigned long)write_size, &written, 0);
  193. if(written != write_size){
  194. return false;
  195. }
  196. }
  197. }
  198. else{
  199. if(!winapi::set_file_pointer(hnd, size, 0, winapi::file_begin)){
  200. return false;
  201. }
  202. if(!winapi::set_end_of_file(hnd)){
  203. return false;
  204. }
  205. }
  206. return true;
  207. }
  208. inline bool get_file_size(file_handle_t hnd, offset_t &size)
  209. { return winapi::get_file_size(hnd, size); }
  210. inline bool set_file_pointer(file_handle_t hnd, offset_t off, file_pos_t pos)
  211. { return winapi::set_file_pointer(hnd, off, 0, (unsigned long) pos); }
  212. inline bool get_file_pointer(file_handle_t hnd, offset_t &off)
  213. { return winapi::set_file_pointer(hnd, 0, &off, winapi::file_current); }
  214. inline bool write_file(file_handle_t hnd, const void *data, std::size_t numdata)
  215. {
  216. unsigned long written;
  217. return 0 != winapi::write_file(hnd, data, (unsigned long)numdata, &written, 0);
  218. }
  219. inline file_handle_t invalid_file()
  220. { return winapi::invalid_handle_value; }
  221. inline bool close_file(file_handle_t hnd)
  222. { return 0 != winapi::close_handle(hnd); }
  223. inline bool acquire_file_lock(file_handle_t hnd)
  224. {
  225. static winapi::interprocess_overlapped overlapped;
  226. const unsigned long len = ((unsigned long)-1);
  227. // winapi::interprocess_overlapped overlapped;
  228. // std::memset(&overlapped, 0, sizeof(overlapped));
  229. return winapi::lock_file_ex
  230. (hnd, winapi::lockfile_exclusive_lock, 0, len, len, &overlapped);
  231. }
  232. inline bool try_acquire_file_lock(file_handle_t hnd, bool &acquired)
  233. {
  234. const unsigned long len = ((unsigned long)-1);
  235. winapi::interprocess_overlapped overlapped;
  236. std::memset(&overlapped, 0, sizeof(overlapped));
  237. if(!winapi::lock_file_ex
  238. (hnd, winapi::lockfile_exclusive_lock | winapi::lockfile_fail_immediately,
  239. 0, len, len, &overlapped)){
  240. return winapi::get_last_error() == winapi::error_lock_violation ?
  241. acquired = false, true : false;
  242. }
  243. acquired = true;
  244. return true;
  245. }
  246. inline bool release_file_lock(file_handle_t hnd)
  247. {
  248. const unsigned long len = ((unsigned long)-1);
  249. winapi::interprocess_overlapped overlapped;
  250. std::memset(&overlapped, 0, sizeof(overlapped));
  251. return winapi::unlock_file_ex(hnd, 0, len, len, &overlapped);
  252. }
  253. inline bool acquire_file_lock_sharable(file_handle_t hnd)
  254. {
  255. const unsigned long len = ((unsigned long)-1);
  256. winapi::interprocess_overlapped overlapped;
  257. std::memset(&overlapped, 0, sizeof(overlapped));
  258. return winapi::lock_file_ex(hnd, 0, 0, len, len, &overlapped);
  259. }
  260. inline bool try_acquire_file_lock_sharable(file_handle_t hnd, bool &acquired)
  261. {
  262. const unsigned long len = ((unsigned long)-1);
  263. winapi::interprocess_overlapped overlapped;
  264. std::memset(&overlapped, 0, sizeof(overlapped));
  265. if(!winapi::lock_file_ex
  266. (hnd, winapi::lockfile_fail_immediately, 0, len, len, &overlapped)){
  267. return winapi::get_last_error() == winapi::error_lock_violation ?
  268. acquired = false, true : false;
  269. }
  270. acquired = true;
  271. return true;
  272. }
  273. inline bool release_file_lock_sharable(file_handle_t hnd)
  274. { return release_file_lock(hnd); }
  275. template<class CharT>
  276. struct os_file_traits;
  277. template<>
  278. struct os_file_traits<char>
  279. {
  280. static const char *any_file()
  281. { return "\\*.*"; }
  282. static const char *backslash()
  283. { return "\\"; }
  284. static char dot()
  285. { return '.'; }
  286. typedef winapi::win32_find_data_a win32_find_data_t;
  287. static int cmp(const char *a, const char *b)
  288. { return std::strcmp(a, b); }
  289. };
  290. template<>
  291. struct os_file_traits<wchar_t>
  292. {
  293. static const wchar_t *any_file()
  294. { return L"\\*.*"; }
  295. static const wchar_t *backslash()
  296. { return L"\\"; }
  297. static wchar_t dot()
  298. { return L'.'; }
  299. typedef winapi::win32_find_data_w win32_find_data_t;
  300. static int cmp(const wchar_t *a, const wchar_t *b)
  301. { return std::wcscmp(a, b); }
  302. };
  303. template<class CharT>
  304. inline bool delete_subdirectories_recursive
  305. (const std::basic_string<CharT> &refcstrRootDirectory, const CharT *dont_delete_this, unsigned int count)
  306. {
  307. bool bSubdirectory = false; // Flag, indicating whether
  308. // subdirectories have been found
  309. void * hFile; // Handle to directory
  310. std::basic_string<CharT> strFilePath; // Filepath
  311. std::basic_string<CharT> strPattern; // Pattern
  312. typedef os_file_traits<CharT> traits_t;
  313. typename traits_t::win32_find_data_t FileInformation; // File information
  314. //Find all files and directories
  315. strPattern = refcstrRootDirectory + traits_t::any_file();
  316. hFile = winapi::find_first_file(strPattern.c_str(), &FileInformation);
  317. if(hFile != winapi::invalid_handle_value){
  318. do{
  319. //If it's not "." or ".." or the pointed root_level dont_delete_this erase it
  320. if(FileInformation.cFileName[0] != traits_t::dot() &&
  321. !(dont_delete_this && count == 0 && traits_t::cmp(dont_delete_this, FileInformation.cFileName) == 0)){
  322. strFilePath.erase();
  323. strFilePath = refcstrRootDirectory + traits_t::backslash() + FileInformation.cFileName;
  324. //If it's a directory, go recursive
  325. if(FileInformation.dwFileAttributes & winapi::file_attribute_directory){
  326. // Delete subdirectory
  327. if(!delete_subdirectories_recursive(strFilePath, dont_delete_this, count+1)){
  328. winapi::find_close(hFile);
  329. return false;
  330. }
  331. }
  332. //If it's a file, just delete it
  333. else{
  334. // Set file attributes
  335. //if(::SetFileAttributes(strFilePath.c_str(), winapi::file_attribute_normal) == 0)
  336. //return winapi::get_last_error();
  337. // Delete file
  338. winapi::unlink_file(strFilePath.c_str());
  339. }
  340. }
  341. //Go to the next file
  342. } while(winapi::find_next_file(hFile, &FileInformation) == 1);
  343. // Close handle
  344. winapi::find_close(hFile);
  345. //See if the loop has ended with an error or just because we've traversed all the files
  346. if(winapi::get_last_error() != winapi::error_no_more_files){
  347. return false;
  348. }
  349. else
  350. {
  351. //Erase empty subdirectories or original refcstrRootDirectory
  352. if(!bSubdirectory && count)
  353. {
  354. // Set directory attributes
  355. //if(::SetFileAttributes(refcstrRootDirectory.c_str(), FILE_ATTRIBUTE_NORMAL) == 0)
  356. //return ::GetLastError();
  357. // Delete directory
  358. if(winapi::remove_directory(refcstrRootDirectory.c_str()) == 0)
  359. return false;
  360. }
  361. }
  362. }
  363. return true;
  364. }
  365. //This function erases all the subdirectories of a directory except the one pointed by "dont_delete_this"
  366. template <class CharT>
  367. inline bool delete_subdirectories(const std::basic_string<CharT> &refcstrRootDirectory, const CharT *dont_delete_this)
  368. {
  369. return delete_subdirectories_recursive(refcstrRootDirectory, dont_delete_this, 0u);
  370. }
  371. template<class Function>
  372. inline bool for_each_file_in_dir(const char *dir, Function f)
  373. {
  374. void * hFile; // Handle to directory
  375. winapi::win32_find_data_a FileInformation; // File information
  376. //Get base directory
  377. std::string str(dir);
  378. const std::size_t base_root_dir_len = str.size();
  379. //Find all files and directories
  380. str += "\\*.*";
  381. hFile = winapi::find_first_file(str.c_str(), &FileInformation);
  382. if(hFile != winapi::invalid_handle_value){
  383. do{ //Now loop every file
  384. str.erase(base_root_dir_len);
  385. //If it's not "." or ".." skip it
  386. if(FileInformation.cFileName[0] != '.'){
  387. str += "\\"; str += FileInformation.cFileName;
  388. //If it's a file, apply erase logic
  389. if(!(FileInformation.dwFileAttributes & winapi::file_attribute_directory)){
  390. f(str.c_str(), FileInformation.cFileName);
  391. }
  392. }
  393. //Go to the next file
  394. } while(winapi::find_next_file(hFile, &FileInformation) == 1);
  395. // Close handle and see if the loop has ended with an error
  396. winapi::find_close(hFile);
  397. if(winapi::get_last_error() != winapi::error_no_more_files){
  398. return false;
  399. }
  400. }
  401. return true;
  402. }
  403. #else //#if defined (BOOST_INTERPROCESS_WINDOWS)
  404. typedef int file_handle_t;
  405. typedef off_t offset_t;
  406. typedef struct mapping_handle_impl_t
  407. {
  408. file_handle_t handle;
  409. bool is_xsi;
  410. } mapping_handle_t;
  411. typedef enum { read_only = O_RDONLY
  412. , read_write = O_RDWR
  413. , copy_on_write
  414. , read_private
  415. , invalid_mode = 0xffff
  416. } mode_t;
  417. typedef enum { file_begin = SEEK_SET
  418. , file_end = SEEK_END
  419. , file_current = SEEK_CUR
  420. } file_pos_t;
  421. typedef int map_options_t;
  422. static const map_options_t default_map_options = map_options_t(-1);
  423. namespace ipcdetail{
  424. inline mapping_handle_t mapping_handle_from_file_handle(file_handle_t hnd)
  425. {
  426. mapping_handle_t ret;
  427. ret.handle = hnd;
  428. ret.is_xsi = false;
  429. return ret;
  430. }
  431. inline file_handle_t file_handle_from_mapping_handle(mapping_handle_t hnd)
  432. { return hnd.handle; }
  433. inline bool create_directory(const char *path)
  434. {
  435. ::mode_t m = ::mode_t(0777);
  436. return ::mkdir(path, m) == 0;
  437. }
  438. inline bool open_or_create_directory(const char *path)
  439. {
  440. ::mode_t m = ::mode_t(0777);
  441. return ::mkdir(path, m) == 0 || (errno == EEXIST);
  442. }
  443. inline bool open_or_create_shared_directory(const char *path)
  444. {
  445. const ::mode_t m = ::mode_t(01777);
  446. const bool created = ::mkdir(path, m) == 0;
  447. const bool created_or_exists = created || (errno == EEXIST);
  448. //Try to maximize the chance that the sticky bit is set in shared dirs
  449. //created with old versions that did not set it (for security reasons)
  450. const bool chmoded = ::chmod(path, m) == 0;
  451. return created ? chmoded : created_or_exists;
  452. }
  453. inline bool remove_directory(const char *path)
  454. { return ::rmdir(path) == 0; }
  455. inline bool get_temporary_path(char *buffer, std::size_t buf_len, std::size_t &required_len)
  456. {
  457. required_len = 5u;
  458. if(buf_len < required_len)
  459. return false;
  460. else{
  461. std::strcpy(buffer, "/tmp");
  462. }
  463. return true;
  464. }
  465. inline file_handle_t create_new_file
  466. (const char *name, mode_t mode, const permissions & perm = permissions(), bool temporary = false)
  467. {
  468. (void)temporary;
  469. int ret = ::open(name, ((int)mode) | O_EXCL | O_CREAT, perm.get_permissions());
  470. if(ret >= 0){
  471. ::fchmod(ret, perm.get_permissions());
  472. }
  473. return ret;
  474. }
  475. inline file_handle_t create_or_open_file
  476. (const char *name, mode_t mode, const permissions & perm = permissions(), bool temporary = false)
  477. {
  478. (void)temporary;
  479. int ret = -1;
  480. //We need a loop to change permissions correctly using fchmod, since
  481. //with "O_CREAT only" ::open we don't know if we've created or opened the file.
  482. while(true){
  483. ret = ::open(name, ((int)mode) | O_EXCL | O_CREAT, perm.get_permissions());
  484. if(ret >= 0){
  485. ::fchmod(ret, perm.get_permissions());
  486. break;
  487. }
  488. else if(errno == EEXIST){
  489. if((ret = ::open(name, (int)mode)) >= 0 || errno != ENOENT){
  490. break;
  491. }
  492. }
  493. else{
  494. break;
  495. }
  496. }
  497. return ret;
  498. }
  499. inline file_handle_t open_existing_file
  500. (const char *name, mode_t mode, bool temporary = false)
  501. {
  502. (void)temporary;
  503. return ::open(name, (int)mode);
  504. }
  505. inline bool delete_file(const char *name)
  506. { return ::unlink(name) == 0; }
  507. inline bool truncate_file (file_handle_t hnd, std::size_t size)
  508. {
  509. typedef boost::move_detail::make_unsigned<off_t>::type uoff_t;
  510. BOOST_STATIC_ASSERT(( sizeof(uoff_t) >= sizeof(std::size_t) ));
  511. if( uoff_t(-1)/2u < uoff_t(size) ){
  512. errno = EINVAL;
  513. return false;
  514. }
  515. return 0 == ::ftruncate(hnd, off_t(size));
  516. }
  517. inline bool get_file_size(file_handle_t hnd, offset_t &size)
  518. {
  519. struct stat data;
  520. bool ret = 0 == ::fstat(hnd, &data);
  521. if(ret){
  522. size = data.st_size;
  523. }
  524. return ret;
  525. }
  526. inline bool set_file_pointer(file_handle_t hnd, offset_t off, file_pos_t pos)
  527. { return ((off_t)(-1)) != ::lseek(hnd, off, (int)pos); }
  528. inline bool get_file_pointer(file_handle_t hnd, offset_t &off)
  529. {
  530. off = ::lseek(hnd, 0, SEEK_CUR);
  531. return off != ((off_t)-1);
  532. }
  533. inline bool write_file(file_handle_t hnd, const void *data, std::size_t numdata)
  534. { return (ssize_t(numdata)) == ::write(hnd, data, numdata); }
  535. inline file_handle_t invalid_file()
  536. { return -1; }
  537. inline bool close_file(file_handle_t hnd)
  538. { return ::close(hnd) == 0; }
  539. inline bool acquire_file_lock(file_handle_t hnd)
  540. {
  541. struct ::flock lock;
  542. lock.l_type = F_WRLCK;
  543. lock.l_whence = SEEK_SET;
  544. lock.l_start = 0;
  545. lock.l_len = 0;
  546. return -1 != ::fcntl(hnd, F_SETLKW, &lock);
  547. }
  548. inline bool try_acquire_file_lock(file_handle_t hnd, bool &acquired)
  549. {
  550. struct ::flock lock;
  551. lock.l_type = F_WRLCK;
  552. lock.l_whence = SEEK_SET;
  553. lock.l_start = 0;
  554. lock.l_len = 0;
  555. int ret = ::fcntl(hnd, F_SETLK, &lock);
  556. if(ret == -1){
  557. return (errno == EAGAIN || errno == EACCES) ?
  558. (acquired = false, true) : false;
  559. }
  560. return (acquired = true);
  561. }
  562. inline bool release_file_lock(file_handle_t hnd)
  563. {
  564. struct ::flock lock;
  565. lock.l_type = F_UNLCK;
  566. lock.l_whence = SEEK_SET;
  567. lock.l_start = 0;
  568. lock.l_len = 0;
  569. return -1 != ::fcntl(hnd, F_SETLK, &lock);
  570. }
  571. inline bool acquire_file_lock_sharable(file_handle_t hnd)
  572. {
  573. struct ::flock lock;
  574. lock.l_type = F_RDLCK;
  575. lock.l_whence = SEEK_SET;
  576. lock.l_start = 0;
  577. lock.l_len = 0;
  578. return -1 != ::fcntl(hnd, F_SETLKW, &lock);
  579. }
  580. inline bool try_acquire_file_lock_sharable(file_handle_t hnd, bool &acquired)
  581. {
  582. struct flock lock;
  583. lock.l_type = F_RDLCK;
  584. lock.l_whence = SEEK_SET;
  585. lock.l_start = 0;
  586. lock.l_len = 0;
  587. int ret = ::fcntl(hnd, F_SETLK, &lock);
  588. if(ret == -1){
  589. return (errno == EAGAIN || errno == EACCES) ?
  590. (acquired = false, true) : false;
  591. }
  592. return (acquired = true);
  593. }
  594. inline bool release_file_lock_sharable(file_handle_t hnd)
  595. { return release_file_lock(hnd); }
  596. #if 0
  597. inline bool acquire_file_lock(file_handle_t hnd)
  598. { return 0 == ::flock(hnd, LOCK_EX); }
  599. inline bool try_acquire_file_lock(file_handle_t hnd, bool &acquired)
  600. {
  601. int ret = ::flock(hnd, LOCK_EX | LOCK_NB);
  602. acquired = ret == 0;
  603. return (acquired || errno == EWOULDBLOCK);
  604. }
  605. inline bool release_file_lock(file_handle_t hnd)
  606. { return 0 == ::flock(hnd, LOCK_UN); }
  607. inline bool acquire_file_lock_sharable(file_handle_t hnd)
  608. { return 0 == ::flock(hnd, LOCK_SH); }
  609. inline bool try_acquire_file_lock_sharable(file_handle_t hnd, bool &acquired)
  610. {
  611. int ret = ::flock(hnd, LOCK_SH | LOCK_NB);
  612. acquired = ret == 0;
  613. return (acquired || errno == EWOULDBLOCK);
  614. }
  615. inline bool release_file_lock_sharable(file_handle_t hnd)
  616. { return 0 == ::flock(hnd, LOCK_UN); }
  617. #endif
  618. inline bool delete_subdirectories_recursive
  619. (const std::string &refcstrRootDirectory, const char *dont_delete_this)
  620. {
  621. DIR *d = opendir(refcstrRootDirectory.c_str());
  622. if(!d) {
  623. return false;
  624. }
  625. struct dir_close
  626. {
  627. DIR *d_;
  628. dir_close(DIR *d) : d_(d) {}
  629. ~dir_close() { ::closedir(d_); }
  630. } dc(d); (void)dc;
  631. struct ::dirent *de;
  632. struct ::stat st;
  633. std::string fn;
  634. while((de=::readdir(d))) {
  635. if( de->d_name[0] == '.' && ( de->d_name[1] == '\0'
  636. || (de->d_name[1] == '.' && de->d_name[2] == '\0' )) ){
  637. continue;
  638. }
  639. if(dont_delete_this && std::strcmp(dont_delete_this, de->d_name) == 0){
  640. continue;
  641. }
  642. fn = refcstrRootDirectory;
  643. fn += '/';
  644. fn += de->d_name;
  645. if(std::remove(fn.c_str())) {
  646. if(::stat(fn.c_str(), & st)) {
  647. return false;
  648. }
  649. if(S_ISDIR(st.st_mode)) {
  650. if(!delete_subdirectories_recursive(fn, 0) ){
  651. return false;
  652. }
  653. } else {
  654. return false;
  655. }
  656. }
  657. }
  658. return std::remove(refcstrRootDirectory.c_str()) ? false : true;
  659. }
  660. template<class Function>
  661. inline bool for_each_file_in_dir(const char *dir, Function f)
  662. {
  663. std::string refcstrRootDirectory(dir);
  664. DIR *d = opendir(refcstrRootDirectory.c_str());
  665. if(!d) {
  666. return false;
  667. }
  668. struct dir_close
  669. {
  670. DIR *d_;
  671. dir_close(DIR *d) : d_(d) {}
  672. ~dir_close() { ::closedir(d_); }
  673. } dc(d); (void)dc;
  674. struct ::dirent *de;
  675. struct ::stat st;
  676. std::string fn;
  677. while((de=::readdir(d))) {
  678. if( de->d_name[0] == '.' && ( de->d_name[1] == '\0'
  679. || (de->d_name[1] == '.' && de->d_name[2] == '\0' )) ){
  680. continue;
  681. }
  682. fn = refcstrRootDirectory;
  683. fn += '/';
  684. fn += de->d_name;
  685. if(::stat(fn.c_str(), & st)) {
  686. return false;
  687. }
  688. //If it's a file, apply erase logic
  689. if(!S_ISDIR(st.st_mode)) {
  690. f(fn.c_str(), de->d_name);
  691. }
  692. }
  693. return true;
  694. }
  695. //This function erases all the subdirectories of a directory except the one pointed by "dont_delete_this"
  696. inline bool delete_subdirectories(const std::string &refcstrRootDirectory, const char *dont_delete_this)
  697. {
  698. return delete_subdirectories_recursive(refcstrRootDirectory, dont_delete_this );
  699. }
  700. #endif //#if defined (BOOST_INTERPROCESS_WINDOWS)
  701. inline std::string get_temporary_path()
  702. {
  703. std::size_t required_len = 0;
  704. get_temporary_path((char*)0, 0, required_len);
  705. std::string ret_str(required_len, char(0));
  706. get_temporary_path(&ret_str[0], ret_str.size(), required_len);
  707. while(!ret_str.empty() && !ret_str[ret_str.size()-1]){
  708. ret_str.erase(ret_str.size()-1);
  709. }
  710. return ret_str;
  711. }
  712. #ifdef BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES
  713. inline std::wstring get_temporary_wpath()
  714. {
  715. std::size_t required_len = 0;
  716. get_temporary_path((wchar_t*)0, 0, required_len);
  717. std::wstring ret_str(required_len, char(0));
  718. get_temporary_path(&ret_str[0], ret_str.size(), required_len);
  719. while(!ret_str.empty() && !ret_str[ret_str.size()-1]){
  720. ret_str.erase(ret_str.size()-1);
  721. }
  722. return ret_str;
  723. }
  724. #endif
  725. } //namespace ipcdetail{
  726. } //namespace interprocess {
  727. } //namespace boost {
  728. #include <boost/interprocess/detail/config_end.hpp>
  729. #endif //BOOST_INTERPROCESS_DETAIL_OS_FILE_FUNCTIONS_HPP