os_file_functions.hpp 21 KB

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