os_file_functions.hpp 26 KB

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