os_file_functions.hpp 24 KB

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