os_file_functions.hpp 22 KB

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