operations.hpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. // boost/filesystem/operations.hpp ---------------------------------------------------//
  2. // Copyright Beman Dawes 2002-2009
  3. // Copyright Jan Langer 2002
  4. // Copyright Dietmar Kuehl 2001
  5. // Copyright Vladimir Prus 2002
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // See http://www.boost.org/LICENSE_1_0.txt
  8. // Library home page: http://www.boost.org/libs/filesystem
  9. //--------------------------------------------------------------------------------------//
  10. #ifndef BOOST_FILESYSTEM3_OPERATIONS_HPP
  11. #define BOOST_FILESYSTEM3_OPERATIONS_HPP
  12. #include <boost/config.hpp>
  13. # if defined( BOOST_NO_STD_WSTRING )
  14. # error Configuration not supported: Boost.Filesystem V3 and later requires std::wstring support
  15. # endif
  16. #include <boost/filesystem/config.hpp>
  17. #include <boost/filesystem/path.hpp>
  18. #include <boost/detail/scoped_enum_emulation.hpp>
  19. #include <boost/detail/bitmask.hpp>
  20. #include <boost/system/error_code.hpp>
  21. #include <boost/system/system_error.hpp>
  22. #include <boost/shared_ptr.hpp>
  23. #include <boost/utility/enable_if.hpp>
  24. #include <boost/type_traits/is_same.hpp>
  25. #include <boost/iterator.hpp>
  26. #include <boost/cstdint.hpp>
  27. #include <boost/assert.hpp>
  28. #include <string>
  29. #include <utility> // for pair
  30. #include <ctime>
  31. #include <vector>
  32. #include <stack>
  33. #ifdef BOOST_WINDOWS_API
  34. # include <fstream>
  35. #endif
  36. #include <boost/config/abi_prefix.hpp> // must be the last #include
  37. //--------------------------------------------------------------------------------------//
  38. namespace boost
  39. {
  40. namespace filesystem
  41. {
  42. //--------------------------------------------------------------------------------------//
  43. // file_type //
  44. //--------------------------------------------------------------------------------------//
  45. enum file_type
  46. {
  47. status_error,
  48. # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  49. status_unknown = status_error,
  50. # endif
  51. file_not_found,
  52. regular_file,
  53. directory_file,
  54. // the following may not apply to some operating systems or file systems
  55. symlink_file,
  56. block_file,
  57. character_file,
  58. fifo_file,
  59. socket_file,
  60. reparse_file, // Windows: FILE_ATTRIBUTE_REPARSE_POINT that is not a symlink
  61. type_unknown, // file does exist, but isn't one of the above types or
  62. // we don't have strong enough permission to find its type
  63. _detail_directory_symlink // internal use only; never exposed to users
  64. };
  65. //--------------------------------------------------------------------------------------//
  66. // perms //
  67. //--------------------------------------------------------------------------------------//
  68. enum perms
  69. {
  70. no_perms = 0, // file_not_found is no_perms rather than perms_not_known
  71. // POSIX equivalent macros given in comments.
  72. // Values are from POSIX and are given in octal per the POSIX standard.
  73. // permission bits
  74. owner_read = 0400, // S_IRUSR, Read permission, owner
  75. owner_write = 0200, // S_IWUSR, Write permission, owner
  76. owner_exe = 0100, // S_IXUSR, Execute/search permission, owner
  77. owner_all = 0700, // S_IRWXU, Read, write, execute/search by owner
  78. group_read = 040, // S_IRGRP, Read permission, group
  79. group_write = 020, // S_IWGRP, Write permission, group
  80. group_exe = 010, // S_IXGRP, Execute/search permission, group
  81. group_all = 070, // S_IRWXG, Read, write, execute/search by group
  82. others_read = 04, // S_IROTH, Read permission, others
  83. others_write = 02, // S_IWOTH, Write permission, others
  84. others_exe = 01, // S_IXOTH, Execute/search permission, others
  85. others_all = 07, // S_IRWXO, Read, write, execute/search by others
  86. all_all = owner_all|group_all|others_all, // 0777
  87. // other POSIX bits
  88. set_uid_on_exe = 04000, // S_ISUID, Set-user-ID on execution
  89. set_gid_on_exe = 02000, // S_ISGID, Set-group-ID on execution
  90. sticky_bit = 01000, // S_ISVTX,
  91. // (POSIX XSI) On directories, restricted deletion flag
  92. // (V7) 'sticky bit': save swapped text even after use
  93. // (SunOS) On non-directories: don't cache this file
  94. // (SVID-v4.2) On directories: restricted deletion flag
  95. // Also see http://en.wikipedia.org/wiki/Sticky_bit
  96. perms_mask = all_all|set_uid_on_exe|set_gid_on_exe|sticky_bit, // 07777
  97. perms_not_known = 0xFFFF, // present when directory_entry cache not loaded
  98. // options for permissions() function
  99. add_perms = 0x1000, // adds the given permission bits to the current bits
  100. remove_perms = 0x2000, // removes the given permission bits from the current bits;
  101. // choose add_perms or remove_perms, not both; if neither add_perms
  102. // nor remove_perms is given, replace the current bits with
  103. // the given bits.
  104. symlink_perms = 0x4000 // on POSIX, don't resolve symlinks; implied on Windows
  105. };
  106. BOOST_BITMASK(perms)
  107. //--------------------------------------------------------------------------------------//
  108. // file_status //
  109. //--------------------------------------------------------------------------------------//
  110. class BOOST_FILESYSTEM_DECL file_status
  111. {
  112. public:
  113. file_status() : m_value(status_error), m_perms(perms_not_known) {}
  114. explicit file_status(file_type v, perms prms = perms_not_known)
  115. : m_value(v), m_perms(prms) {}
  116. // observers
  117. file_type type() const { return m_value; }
  118. perms permissions() const { return m_perms; }
  119. // modifiers
  120. void type(file_type v) { m_value = v; }
  121. void permissions(perms prms) { m_perms = prms; }
  122. bool operator==(const file_status& rhs) const { return type() == rhs.type() &&
  123. permissions() == rhs.permissions(); }
  124. bool operator!=(const file_status& rhs) const { return !(*this == rhs); }
  125. private:
  126. file_type m_value;
  127. enum perms m_perms;
  128. };
  129. inline bool type_present(file_status f) { return f.type() != status_error; }
  130. inline bool permissions_present(file_status f)
  131. {return f.permissions() != perms_not_known;}
  132. inline bool status_known(file_status f) { return type_present(f) && permissions_present(f); }
  133. inline bool exists(file_status f) { return f.type() != status_error
  134. && f.type() != file_not_found; }
  135. inline bool is_regular_file(file_status f){ return f.type() == regular_file; }
  136. inline bool is_directory(file_status f) { return f.type() == directory_file; }
  137. inline bool is_symlink(file_status f) { return f.type() == symlink_file; }
  138. inline bool is_other(file_status f) { return exists(f) && !is_regular_file(f)
  139. && !is_directory(f) && !is_symlink(f); }
  140. # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  141. inline bool is_regular(file_status f) { return f.type() == regular_file; }
  142. # endif
  143. struct space_info
  144. {
  145. // all values are byte counts
  146. boost::uintmax_t capacity;
  147. boost::uintmax_t free; // <= capacity
  148. boost::uintmax_t available; // <= free
  149. };
  150. BOOST_SCOPED_ENUM_START(copy_option)
  151. {none, fail_if_exists = none, overwrite_if_exists};
  152. BOOST_SCOPED_ENUM_END
  153. //--------------------------------------------------------------------------------------//
  154. // implementation details //
  155. //--------------------------------------------------------------------------------------//
  156. namespace detail
  157. {
  158. BOOST_FILESYSTEM_DECL
  159. file_status status(const path&p, system::error_code* ec=0);
  160. BOOST_FILESYSTEM_DECL
  161. file_status symlink_status(const path& p, system::error_code* ec=0);
  162. BOOST_FILESYSTEM_DECL
  163. bool is_empty(const path& p, system::error_code* ec=0);
  164. BOOST_FILESYSTEM_DECL
  165. path initial_path(system::error_code* ec=0);
  166. BOOST_FILESYSTEM_DECL
  167. path canonical(const path& p, const path& base, system::error_code* ec=0);
  168. BOOST_FILESYSTEM_DECL
  169. void copy(const path& from, const path& to, system::error_code* ec=0);
  170. BOOST_FILESYSTEM_DECL
  171. void copy_directory(const path& from, const path& to, system::error_code* ec=0);
  172. BOOST_FILESYSTEM_DECL
  173. void copy_file(const path& from, const path& to,
  174. BOOST_SCOPED_ENUM(copy_option) option, // See ticket #2925
  175. system::error_code* ec=0);
  176. BOOST_FILESYSTEM_DECL
  177. void copy_symlink(const path& existing_symlink, const path& new_symlink, system::error_code* ec=0);
  178. BOOST_FILESYSTEM_DECL
  179. bool create_directories(const path& p, system::error_code* ec=0);
  180. BOOST_FILESYSTEM_DECL
  181. bool create_directory(const path& p, system::error_code* ec=0);
  182. BOOST_FILESYSTEM_DECL
  183. void create_directory_symlink(const path& to, const path& from,
  184. system::error_code* ec=0);
  185. BOOST_FILESYSTEM_DECL
  186. void create_hard_link(const path& to, const path& from, system::error_code* ec=0);
  187. BOOST_FILESYSTEM_DECL
  188. void create_symlink(const path& to, const path& from, system::error_code* ec=0);
  189. BOOST_FILESYSTEM_DECL
  190. path current_path(system::error_code* ec=0);
  191. BOOST_FILESYSTEM_DECL
  192. void current_path(const path& p, system::error_code* ec=0);
  193. BOOST_FILESYSTEM_DECL
  194. bool equivalent(const path& p1, const path& p2, system::error_code* ec=0);
  195. BOOST_FILESYSTEM_DECL
  196. boost::uintmax_t file_size(const path& p, system::error_code* ec=0);
  197. BOOST_FILESYSTEM_DECL
  198. boost::uintmax_t hard_link_count(const path& p, system::error_code* ec=0);
  199. BOOST_FILESYSTEM_DECL
  200. std::time_t last_write_time(const path& p, system::error_code* ec=0);
  201. BOOST_FILESYSTEM_DECL
  202. void last_write_time(const path& p, const std::time_t new_time,
  203. system::error_code* ec=0);
  204. BOOST_FILESYSTEM_DECL
  205. void permissions(const path& p, perms prms, system::error_code* ec=0);
  206. BOOST_FILESYSTEM_DECL
  207. path read_symlink(const path& p, system::error_code* ec=0);
  208. BOOST_FILESYSTEM_DECL
  209. // For standardization, if the committee doesn't like "remove", consider "eliminate"
  210. bool remove(const path& p, system::error_code* ec=0);
  211. BOOST_FILESYSTEM_DECL
  212. boost::uintmax_t remove_all(const path& p, system::error_code* ec=0);
  213. BOOST_FILESYSTEM_DECL
  214. void rename(const path& old_p, const path& new_p, system::error_code* ec=0);
  215. BOOST_FILESYSTEM_DECL
  216. void resize_file(const path& p, uintmax_t size, system::error_code* ec=0);
  217. BOOST_FILESYSTEM_DECL
  218. space_info space(const path& p, system::error_code* ec=0);
  219. BOOST_FILESYSTEM_DECL
  220. path system_complete(const path& p, system::error_code* ec=0);
  221. BOOST_FILESYSTEM_DECL
  222. path temp_directory_path(system::error_code* ec=0);
  223. BOOST_FILESYSTEM_DECL
  224. path unique_path(const path& p, system::error_code* ec=0);
  225. } // namespace detail
  226. //--------------------------------------------------------------------------------------//
  227. // //
  228. // status query functions //
  229. // //
  230. //--------------------------------------------------------------------------------------//
  231. inline
  232. file_status status(const path& p) {return detail::status(p);}
  233. inline
  234. file_status status(const path& p, system::error_code& ec)
  235. {return detail::status(p, &ec);}
  236. inline
  237. file_status symlink_status(const path& p) {return detail::symlink_status(p);}
  238. inline
  239. file_status symlink_status(const path& p, system::error_code& ec)
  240. {return detail::symlink_status(p, &ec);}
  241. inline
  242. bool exists(const path& p) {return exists(detail::status(p));}
  243. inline
  244. bool exists(const path& p, system::error_code& ec)
  245. {return exists(detail::status(p, &ec));}
  246. inline
  247. bool is_directory(const path& p) {return is_directory(detail::status(p));}
  248. inline
  249. bool is_directory(const path& p, system::error_code& ec)
  250. {return is_directory(detail::status(p, &ec));}
  251. inline
  252. bool is_regular_file(const path& p) {return is_regular_file(detail::status(p));}
  253. inline
  254. bool is_regular_file(const path& p, system::error_code& ec)
  255. {return is_regular_file(detail::status(p, &ec));}
  256. inline
  257. bool is_other(const path& p) {return is_other(detail::status(p));}
  258. inline
  259. bool is_other(const path& p, system::error_code& ec)
  260. {return is_other(detail::status(p, &ec));}
  261. inline
  262. bool is_symlink(const path& p) {return is_symlink(detail::symlink_status(p));}
  263. inline
  264. bool is_symlink(const path& p, system::error_code& ec)
  265. {return is_symlink(detail::symlink_status(p, &ec));}
  266. # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  267. inline
  268. bool is_regular(const path& p) {return is_regular(detail::status(p));}
  269. inline
  270. bool is_regular(const path& p, system::error_code& ec)
  271. {return is_regular(detail::status(p, &ec));}
  272. # endif
  273. inline
  274. bool is_empty(const path& p) {return detail::is_empty(p);}
  275. inline
  276. bool is_empty(const path& p, system::error_code& ec)
  277. {return detail::is_empty(p, &ec);}
  278. //--------------------------------------------------------------------------------------//
  279. // //
  280. // operational functions //
  281. // in alphabetical order, unless otherwise noted //
  282. // //
  283. //--------------------------------------------------------------------------------------//
  284. // forward declarations
  285. path current_path(); // fwd declaration
  286. path initial_path();
  287. BOOST_FILESYSTEM_DECL
  288. path absolute(const path& p, const path& base=current_path());
  289. // If base.is_absolute(), throws nothing. Thus no need for ec argument
  290. inline
  291. path canonical(const path& p, const path& base=current_path())
  292. {return detail::canonical(p, base);}
  293. inline
  294. path canonical(const path& p, system::error_code& ec)
  295. {return detail::canonical(p, current_path(), &ec);}
  296. inline
  297. path canonical(const path& p, const path& base, system::error_code& ec)
  298. {return detail::canonical(p, base, &ec);}
  299. # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  300. inline
  301. path complete(const path& p)
  302. {
  303. return absolute(p, initial_path());
  304. }
  305. inline
  306. path complete(const path& p, const path& base)
  307. {
  308. return absolute(p, base);
  309. }
  310. # endif
  311. inline
  312. void copy(const path& from, const path& to) {detail::copy(from, to);}
  313. inline
  314. void copy(const path& from, const path& to, system::error_code& ec)
  315. {detail::copy(from, to, &ec);}
  316. inline
  317. void copy_directory(const path& from, const path& to)
  318. {detail::copy_directory(from, to);}
  319. inline
  320. void copy_directory(const path& from, const path& to, system::error_code& ec)
  321. {detail::copy_directory(from, to, &ec);}
  322. inline
  323. void copy_file(const path& from, const path& to, // See ticket #2925
  324. BOOST_SCOPED_ENUM(copy_option) option)
  325. {detail::copy_file(from, to, option);}
  326. inline
  327. void copy_file(const path& from, const path& to)
  328. {detail::copy_file(from, to, copy_option::fail_if_exists);}
  329. inline
  330. void copy_file(const path& from, const path& to, // See ticket #2925
  331. BOOST_SCOPED_ENUM(copy_option) option, system::error_code& ec)
  332. {detail::copy_file(from, to, option, &ec);}
  333. inline
  334. void copy_file(const path& from, const path& to, system::error_code& ec)
  335. {detail::copy_file(from, to, copy_option::fail_if_exists, &ec);}
  336. inline
  337. void copy_symlink(const path& existing_symlink, const path& new_symlink) {detail::copy_symlink(existing_symlink, new_symlink);}
  338. inline
  339. void copy_symlink(const path& existing_symlink, const path& new_symlink, system::error_code& ec)
  340. {detail::copy_symlink(existing_symlink, new_symlink, &ec);}
  341. inline
  342. bool create_directories(const path& p) {return detail::create_directories(p);}
  343. inline
  344. bool create_directories(const path& p, system::error_code& ec)
  345. {return detail::create_directories(p, &ec);}
  346. inline
  347. bool create_directory(const path& p) {return detail::create_directory(p);}
  348. inline
  349. bool create_directory(const path& p, system::error_code& ec)
  350. {return detail::create_directory(p, &ec);}
  351. inline
  352. void create_directory_symlink(const path& to, const path& from)
  353. {detail::create_directory_symlink(to, from);}
  354. inline
  355. void create_directory_symlink(const path& to, const path& from, system::error_code& ec)
  356. {detail::create_directory_symlink(to, from, &ec);}
  357. inline
  358. void create_hard_link(const path& to, const path& new_hard_link) {detail::create_hard_link(to, new_hard_link);}
  359. inline
  360. void create_hard_link(const path& to, const path& new_hard_link, system::error_code& ec)
  361. {detail::create_hard_link(to, new_hard_link, &ec);}
  362. inline
  363. void create_symlink(const path& to, const path& new_symlink) {detail::create_symlink(to, new_symlink);}
  364. inline
  365. void create_symlink(const path& to, const path& new_symlink, system::error_code& ec)
  366. {detail::create_symlink(to, new_symlink, &ec);}
  367. inline
  368. path current_path() {return detail::current_path();}
  369. inline
  370. path current_path(system::error_code& ec) {return detail::current_path(&ec);}
  371. inline
  372. void current_path(const path& p) {detail::current_path(p);}
  373. inline
  374. void current_path(const path& p, system::error_code& ec) {detail::current_path(p, &ec);}
  375. inline
  376. bool equivalent(const path& p1, const path& p2) {return detail::equivalent(p1, p2);}
  377. inline
  378. bool equivalent(const path& p1, const path& p2, system::error_code& ec)
  379. {return detail::equivalent(p1, p2, &ec);}
  380. inline
  381. boost::uintmax_t file_size(const path& p) {return detail::file_size(p);}
  382. inline
  383. boost::uintmax_t file_size(const path& p, system::error_code& ec)
  384. {return detail::file_size(p, &ec);}
  385. inline
  386. boost::uintmax_t hard_link_count(const path& p) {return detail::hard_link_count(p);}
  387. inline
  388. boost::uintmax_t hard_link_count(const path& p, system::error_code& ec)
  389. {return detail::hard_link_count(p, &ec);}
  390. inline
  391. path initial_path() {return detail::initial_path();}
  392. inline
  393. path initial_path(system::error_code& ec) {return detail::initial_path(&ec);}
  394. template <class Path>
  395. path initial_path() {return initial_path();}
  396. template <class Path>
  397. path initial_path(system::error_code& ec) {return detail::initial_path(&ec);}
  398. inline
  399. std::time_t last_write_time(const path& p) {return detail::last_write_time(p);}
  400. inline
  401. std::time_t last_write_time(const path& p, system::error_code& ec)
  402. {return detail::last_write_time(p, &ec);}
  403. inline
  404. void last_write_time(const path& p, const std::time_t new_time)
  405. {detail::last_write_time(p, new_time);}
  406. inline
  407. void last_write_time(const path& p, const std::time_t new_time, system::error_code& ec)
  408. {detail::last_write_time(p, new_time, &ec);}
  409. inline
  410. void permissions(const path& p, perms prms)
  411. {detail::permissions(p, prms);}
  412. inline
  413. void permissions(const path& p, perms prms, system::error_code& ec)
  414. {detail::permissions(p, prms, &ec);}
  415. inline
  416. path read_symlink(const path& p) {return detail::read_symlink(p);}
  417. inline
  418. path read_symlink(const path& p, system::error_code& ec)
  419. {return detail::read_symlink(p, &ec);}
  420. inline
  421. // For standardization, if the committee doesn't like "remove", consider "eliminate"
  422. bool remove(const path& p) {return detail::remove(p);}
  423. inline
  424. bool remove(const path& p, system::error_code& ec) {return detail::remove(p, &ec);}
  425. inline
  426. boost::uintmax_t remove_all(const path& p) {return detail::remove_all(p);}
  427. inline
  428. boost::uintmax_t remove_all(const path& p, system::error_code& ec)
  429. {return detail::remove_all(p, &ec);}
  430. inline
  431. void rename(const path& old_p, const path& new_p) {detail::rename(old_p, new_p);}
  432. inline
  433. void rename(const path& old_p, const path& new_p, system::error_code& ec)
  434. {detail::rename(old_p, new_p, &ec);}
  435. inline // name suggested by Scott McMurray
  436. void resize_file(const path& p, uintmax_t size) {detail::resize_file(p, size);}
  437. inline
  438. void resize_file(const path& p, uintmax_t size, system::error_code& ec)
  439. {detail::resize_file(p, size, &ec);}
  440. inline
  441. space_info space(const path& p) {return detail::space(p);}
  442. inline
  443. space_info space(const path& p, system::error_code& ec) {return detail::space(p, &ec);}
  444. # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  445. inline bool symbolic_link_exists(const path& p)
  446. { return is_symlink(symlink_status(p)); }
  447. # endif
  448. inline
  449. path system_complete(const path& p) {return detail::system_complete(p);}
  450. inline
  451. path system_complete(const path& p, system::error_code& ec)
  452. {return detail::system_complete(p, &ec);}
  453. inline
  454. path temp_directory_path() {return detail::temp_directory_path();}
  455. inline
  456. path temp_directory_path(system::error_code& ec)
  457. {return detail::temp_directory_path(&ec);}
  458. inline
  459. path unique_path(const path& p="%%%%-%%%%-%%%%-%%%%")
  460. { return detail::unique_path(p); }
  461. inline
  462. path unique_path(const path& p, system::error_code& ec)
  463. { return detail::unique_path(p, &ec); }
  464. //--------------------------------------------------------------------------------------//
  465. // //
  466. // directory_entry //
  467. // //
  468. //--------------------------------------------------------------------------------------//
  469. // GCC has a problem with a member function named path within a namespace or
  470. // sub-namespace that also has a class named path. The workaround is to always
  471. // fully qualify the name path when it refers to the class name.
  472. class BOOST_FILESYSTEM_DECL directory_entry
  473. {
  474. public:
  475. // compiler generated copy constructor, copy assignment, and destructor apply
  476. directory_entry() {}
  477. explicit directory_entry(const boost::filesystem::path& p,
  478. file_status st = file_status(), file_status symlink_st=file_status())
  479. : m_path(p), m_status(st), m_symlink_status(symlink_st)
  480. {}
  481. void assign(const boost::filesystem::path& p,
  482. file_status st = file_status(), file_status symlink_st = file_status())
  483. { m_path = p; m_status = st; m_symlink_status = symlink_st; }
  484. void replace_filename(const boost::filesystem::path& p,
  485. file_status st = file_status(), file_status symlink_st = file_status())
  486. {
  487. m_path.remove_filename();
  488. m_path /= p;
  489. m_status = st;
  490. m_symlink_status = symlink_st;
  491. }
  492. # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  493. void replace_leaf(const boost::filesystem::path& p,
  494. file_status st, file_status symlink_st)
  495. { replace_filename(p, st, symlink_st); }
  496. # endif
  497. const boost::filesystem::path& path() const {return m_path;}
  498. file_status status() const {return m_get_status();}
  499. file_status status(system::error_code& ec) const {return m_get_status(&ec);}
  500. file_status symlink_status() const {return m_get_symlink_status();}
  501. file_status symlink_status(system::error_code& ec) const {return m_get_symlink_status(&ec);}
  502. bool operator==(const directory_entry& rhs) {return m_path == rhs.m_path;}
  503. bool operator!=(const directory_entry& rhs) {return m_path != rhs.m_path;}
  504. bool operator< (const directory_entry& rhs) {return m_path < rhs.m_path;}
  505. bool operator<=(const directory_entry& rhs) {return m_path <= rhs.m_path;}
  506. bool operator> (const directory_entry& rhs) {return m_path > rhs.m_path;}
  507. bool operator>=(const directory_entry& rhs) {return m_path >= rhs.m_path;}
  508. private:
  509. boost::filesystem::path m_path;
  510. mutable file_status m_status; // stat()-like
  511. mutable file_status m_symlink_status; // lstat()-like
  512. file_status m_get_status(system::error_code* ec=0) const;
  513. file_status m_get_symlink_status(system::error_code* ec=0) const;
  514. }; // directory_entry
  515. //--------------------------------------------------------------------------------------//
  516. // //
  517. // directory_iterator helpers //
  518. // //
  519. //--------------------------------------------------------------------------------------//
  520. class directory_iterator;
  521. namespace detail
  522. {
  523. BOOST_FILESYSTEM_DECL
  524. system::error_code dir_itr_close(// never throws()
  525. void *& handle
  526. # if defined(BOOST_POSIX_API)
  527. , void *& buffer
  528. # endif
  529. );
  530. struct dir_itr_imp
  531. {
  532. directory_entry dir_entry;
  533. void* handle;
  534. # ifdef BOOST_POSIX_API
  535. void* buffer; // see dir_itr_increment implementation
  536. # endif
  537. dir_itr_imp() : handle(0)
  538. # ifdef BOOST_POSIX_API
  539. , buffer(0)
  540. # endif
  541. {}
  542. ~dir_itr_imp() // never throws
  543. {
  544. dir_itr_close(handle
  545. # if defined(BOOST_POSIX_API)
  546. , buffer
  547. # endif
  548. );
  549. }
  550. };
  551. // see path::iterator: comment below
  552. BOOST_FILESYSTEM_DECL void directory_iterator_construct(directory_iterator& it,
  553. const path& p, system::error_code* ec);
  554. BOOST_FILESYSTEM_DECL void directory_iterator_increment(directory_iterator& it,
  555. system::error_code* ec);
  556. } // namespace detail
  557. //--------------------------------------------------------------------------------------//
  558. // //
  559. // directory_iterator //
  560. // //
  561. //--------------------------------------------------------------------------------------//
  562. class directory_iterator
  563. : public boost::iterator_facade< directory_iterator,
  564. directory_entry,
  565. boost::single_pass_traversal_tag >
  566. {
  567. public:
  568. directory_iterator(){} // creates the "end" iterator
  569. // iterator_facade derived classes don't seem to like implementations in
  570. // separate translation unit dll's, so forward to detail functions
  571. explicit directory_iterator(const path& p)
  572. : m_imp(new detail::dir_itr_imp)
  573. { detail::directory_iterator_construct(*this, p, 0); }
  574. directory_iterator(const path& p, system::error_code& ec)
  575. : m_imp(new detail::dir_itr_imp)
  576. { detail::directory_iterator_construct(*this, p, &ec); }
  577. ~directory_iterator() {} // never throws
  578. directory_iterator& increment(system::error_code& ec)
  579. {
  580. detail::directory_iterator_increment(*this, &ec);
  581. return *this;
  582. }
  583. private:
  584. friend struct detail::dir_itr_imp;
  585. friend BOOST_FILESYSTEM_DECL void detail::directory_iterator_construct(directory_iterator& it,
  586. const path& p, system::error_code* ec);
  587. friend BOOST_FILESYSTEM_DECL void detail::directory_iterator_increment(directory_iterator& it,
  588. system::error_code* ec);
  589. // shared_ptr provides shallow-copy semantics required for InputIterators.
  590. // m_imp.get()==0 indicates the end iterator.
  591. boost::shared_ptr< detail::dir_itr_imp > m_imp;
  592. friend class boost::iterator_core_access;
  593. boost::iterator_facade<
  594. directory_iterator,
  595. directory_entry,
  596. boost::single_pass_traversal_tag >::reference dereference() const
  597. {
  598. BOOST_ASSERT_MSG(m_imp.get(), "attempt to dereference end iterator");
  599. return m_imp->dir_entry;
  600. }
  601. void increment() { detail::directory_iterator_increment(*this, 0); }
  602. bool equal(const directory_iterator& rhs) const
  603. { return m_imp == rhs.m_imp; }
  604. };
  605. //--------------------------------------------------------------------------------------//
  606. // //
  607. // recursive_directory_iterator helpers //
  608. // //
  609. //--------------------------------------------------------------------------------------//
  610. BOOST_SCOPED_ENUM_START(symlink_option)
  611. {
  612. none,
  613. no_recurse = none, // don't follow directory symlinks (default behavior)
  614. recurse, // follow directory symlinks
  615. _detail_no_push = recurse << 1 // internal use only
  616. };
  617. BOOST_SCOPED_ENUM_END
  618. BOOST_BITMASK(BOOST_SCOPED_ENUM(symlink_option))
  619. namespace detail
  620. {
  621. struct recur_dir_itr_imp
  622. {
  623. typedef directory_iterator element_type;
  624. std::stack< element_type, std::vector< element_type > > m_stack;
  625. int m_level;
  626. BOOST_SCOPED_ENUM(symlink_option) m_options;
  627. recur_dir_itr_imp() : m_level(0), m_options(symlink_option::none) {}
  628. void increment(system::error_code* ec); // ec == 0 means throw on error
  629. void pop();
  630. };
  631. // Implementation is inline to avoid dynamic linking difficulties with m_stack:
  632. // Microsoft warning C4251, m_stack needs to have dll-interface to be used by
  633. // clients of struct 'boost::filesystem::detail::recur_dir_itr_imp'
  634. inline
  635. void recur_dir_itr_imp::increment(system::error_code* ec)
  636. // ec == 0 means throw on error
  637. {
  638. if ((m_options & symlink_option::_detail_no_push) == symlink_option::_detail_no_push)
  639. m_options &= ~symlink_option::_detail_no_push;
  640. else
  641. {
  642. // Logic for following predicate was contributed by Daniel Aarno to handle cyclic
  643. // symlinks correctly and efficiently, fixing ticket #5652.
  644. // if (((m_options & symlink_option::recurse) == symlink_option::recurse
  645. // || !is_symlink(m_stack.top()->symlink_status()))
  646. // && is_directory(m_stack.top()->status())) ...
  647. // The predicate code has since been rewritten to pass error_code arguments,
  648. // per ticket #5653.
  649. bool or_pred = (m_options & symlink_option::recurse) == symlink_option::recurse
  650. || (ec == 0 ? !is_symlink(m_stack.top()->symlink_status())
  651. : !is_symlink(m_stack.top()->symlink_status(*ec)));
  652. if (ec != 0 && *ec)
  653. return;
  654. bool and_pred = or_pred && (ec == 0 ? is_directory(m_stack.top()->status())
  655. : is_directory(m_stack.top()->status(*ec)));
  656. if (ec != 0 && *ec)
  657. return;
  658. if (and_pred)
  659. {
  660. if (ec == 0)
  661. m_stack.push(directory_iterator(m_stack.top()->path()));
  662. else
  663. {
  664. m_stack.push(directory_iterator(m_stack.top()->path(), *ec));
  665. if (*ec)
  666. return;
  667. }
  668. if (m_stack.top() != directory_iterator())
  669. {
  670. ++m_level;
  671. return;
  672. }
  673. m_stack.pop();
  674. }
  675. }
  676. while (!m_stack.empty() && ++m_stack.top() == directory_iterator())
  677. {
  678. m_stack.pop();
  679. --m_level;
  680. }
  681. }
  682. inline
  683. void recur_dir_itr_imp::pop()
  684. {
  685. BOOST_ASSERT_MSG(m_level > 0,
  686. "pop() on recursive_directory_iterator with level < 1");
  687. do
  688. {
  689. m_stack.pop();
  690. --m_level;
  691. }
  692. while (!m_stack.empty() && ++m_stack.top() == directory_iterator());
  693. }
  694. } // namespace detail
  695. //--------------------------------------------------------------------------------------//
  696. // //
  697. // recursive_directory_iterator //
  698. // //
  699. //--------------------------------------------------------------------------------------//
  700. class recursive_directory_iterator
  701. : public boost::iterator_facade<
  702. recursive_directory_iterator,
  703. directory_entry,
  704. boost::single_pass_traversal_tag >
  705. {
  706. public:
  707. recursive_directory_iterator(){} // creates the "end" iterator
  708. explicit recursive_directory_iterator(const path& dir_path,
  709. BOOST_SCOPED_ENUM(symlink_option) opt = symlink_option::none)
  710. : m_imp(new detail::recur_dir_itr_imp)
  711. {
  712. m_imp->m_options = opt;
  713. m_imp->m_stack.push(directory_iterator(dir_path));
  714. if (m_imp->m_stack.top() == directory_iterator())
  715. { m_imp.reset (); }
  716. }
  717. recursive_directory_iterator(const path& dir_path,
  718. BOOST_SCOPED_ENUM(symlink_option) opt,
  719. system::error_code & ec)
  720. : m_imp(new detail::recur_dir_itr_imp)
  721. {
  722. m_imp->m_options = opt;
  723. m_imp->m_stack.push(directory_iterator(dir_path, ec));
  724. if (m_imp->m_stack.top() == directory_iterator())
  725. { m_imp.reset (); }
  726. }
  727. recursive_directory_iterator(const path& dir_path,
  728. system::error_code & ec)
  729. : m_imp(new detail::recur_dir_itr_imp)
  730. {
  731. m_imp->m_options = symlink_option::none;
  732. m_imp->m_stack.push(directory_iterator(dir_path, ec));
  733. if (m_imp->m_stack.top() == directory_iterator())
  734. { m_imp.reset (); }
  735. }
  736. recursive_directory_iterator& increment(system::error_code& ec)
  737. {
  738. BOOST_ASSERT_MSG(m_imp.get(),
  739. "increment() on end recursive_directory_iterator");
  740. m_imp->increment(&ec);
  741. if (m_imp->m_stack.empty())
  742. m_imp.reset(); // done, so make end iterator
  743. return *this;
  744. }
  745. int level() const
  746. {
  747. BOOST_ASSERT_MSG(m_imp.get(),
  748. "level() on end recursive_directory_iterator");
  749. return m_imp->m_level;
  750. }
  751. bool no_push_pending() const
  752. {
  753. BOOST_ASSERT_MSG(m_imp.get(),
  754. "is_no_push_requested() on end recursive_directory_iterator");
  755. return (m_imp->m_options & symlink_option::_detail_no_push)
  756. == symlink_option::_detail_no_push;
  757. }
  758. # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  759. bool no_push_request() const { return no_push_pending(); }
  760. # endif
  761. void pop()
  762. {
  763. BOOST_ASSERT_MSG(m_imp.get(),
  764. "pop() on end recursive_directory_iterator");
  765. m_imp->pop();
  766. if (m_imp->m_stack.empty()) m_imp.reset(); // done, so make end iterator
  767. }
  768. void no_push(bool value=true)
  769. {
  770. BOOST_ASSERT_MSG(m_imp.get(),
  771. "no_push() on end recursive_directory_iterator");
  772. if (value)
  773. m_imp->m_options |= symlink_option::_detail_no_push;
  774. else
  775. m_imp->m_options &= ~symlink_option::_detail_no_push;
  776. }
  777. file_status status() const
  778. {
  779. BOOST_ASSERT_MSG(m_imp.get(),
  780. "status() on end recursive_directory_iterator");
  781. return m_imp->m_stack.top()->status();
  782. }
  783. file_status symlink_status() const
  784. {
  785. BOOST_ASSERT_MSG(m_imp.get(),
  786. "symlink_status() on end recursive_directory_iterator");
  787. return m_imp->m_stack.top()->symlink_status();
  788. }
  789. private:
  790. // shared_ptr provides shallow-copy semantics required for InputIterators.
  791. // m_imp.get()==0 indicates the end iterator.
  792. boost::shared_ptr< detail::recur_dir_itr_imp > m_imp;
  793. friend class boost::iterator_core_access;
  794. boost::iterator_facade<
  795. recursive_directory_iterator,
  796. directory_entry,
  797. boost::single_pass_traversal_tag >::reference
  798. dereference() const
  799. {
  800. BOOST_ASSERT_MSG(m_imp.get(),
  801. "dereference of end recursive_directory_iterator");
  802. return *m_imp->m_stack.top();
  803. }
  804. void increment()
  805. {
  806. BOOST_ASSERT_MSG(m_imp.get(),
  807. "increment of end recursive_directory_iterator");
  808. m_imp->increment(0);
  809. if (m_imp->m_stack.empty())
  810. m_imp.reset(); // done, so make end iterator
  811. }
  812. bool equal(const recursive_directory_iterator& rhs) const
  813. { return m_imp == rhs.m_imp; }
  814. };
  815. # if !defined(BOOST_FILESYSTEM_NO_DEPRECATED)
  816. typedef recursive_directory_iterator wrecursive_directory_iterator;
  817. # endif
  818. //--------------------------------------------------------------------------------------//
  819. // //
  820. // class filesystem_error //
  821. // //
  822. //--------------------------------------------------------------------------------------//
  823. class BOOST_SYMBOL_VISIBLE filesystem_error : public system::system_error
  824. {
  825. // see http://www.boost.org/more/error_handling.html for design rationale
  826. // all functions are inline to avoid issues with crossing dll boundaries
  827. public:
  828. // compiler generates copy constructor and copy assignment
  829. filesystem_error(
  830. const std::string & what_arg, system::error_code ec)
  831. : system::system_error(ec, what_arg)
  832. {
  833. try
  834. {
  835. m_imp_ptr.reset(new m_imp);
  836. }
  837. catch (...) { m_imp_ptr.reset(); }
  838. }
  839. filesystem_error(
  840. const std::string & what_arg, const path& path1_arg,
  841. system::error_code ec)
  842. : system::system_error(ec, what_arg)
  843. {
  844. try
  845. {
  846. m_imp_ptr.reset(new m_imp);
  847. m_imp_ptr->m_path1 = path1_arg;
  848. }
  849. catch (...) { m_imp_ptr.reset(); }
  850. }
  851. filesystem_error(
  852. const std::string & what_arg, const path& path1_arg,
  853. const path& path2_arg, system::error_code ec)
  854. : system::system_error(ec, what_arg)
  855. {
  856. try
  857. {
  858. m_imp_ptr.reset(new m_imp);
  859. m_imp_ptr->m_path1 = path1_arg;
  860. m_imp_ptr->m_path2 = path2_arg;
  861. }
  862. catch (...) { m_imp_ptr.reset(); }
  863. }
  864. ~filesystem_error() throw() {}
  865. const path& path1() const
  866. {
  867. static const path empty_path;
  868. return m_imp_ptr.get() ? m_imp_ptr->m_path1 : empty_path ;
  869. }
  870. const path& path2() const
  871. {
  872. static const path empty_path;
  873. return m_imp_ptr.get() ? m_imp_ptr->m_path2 : empty_path ;
  874. }
  875. const char* what() const throw()
  876. {
  877. if (!m_imp_ptr.get())
  878. return system::system_error::what();
  879. try
  880. {
  881. if (m_imp_ptr->m_what.empty())
  882. {
  883. m_imp_ptr->m_what = system::system_error::what();
  884. if (!m_imp_ptr->m_path1.empty())
  885. {
  886. m_imp_ptr->m_what += ": \"";
  887. m_imp_ptr->m_what += m_imp_ptr->m_path1.string();
  888. m_imp_ptr->m_what += "\"";
  889. }
  890. if (!m_imp_ptr->m_path2.empty())
  891. {
  892. m_imp_ptr->m_what += ", \"";
  893. m_imp_ptr->m_what += m_imp_ptr->m_path2.string();
  894. m_imp_ptr->m_what += "\"";
  895. }
  896. }
  897. return m_imp_ptr->m_what.c_str();
  898. }
  899. catch (...)
  900. {
  901. return system::system_error::what();
  902. }
  903. }
  904. private:
  905. struct m_imp
  906. {
  907. path m_path1; // may be empty()
  908. path m_path2; // may be empty()
  909. std::string m_what; // not built until needed
  910. };
  911. boost::shared_ptr<m_imp> m_imp_ptr;
  912. };
  913. // test helper -----------------------------------------------------------------------//
  914. // Not part of the documented interface since false positives are possible;
  915. // there is no law that says that an OS that has large stat.st_size
  916. // actually supports large file sizes.
  917. namespace detail
  918. {
  919. BOOST_FILESYSTEM_DECL bool possible_large_file_size_support();
  920. }
  921. } // namespace filesystem
  922. } // namespace boost
  923. #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
  924. #endif // BOOST_FILESYSTEM3_OPERATIONS_HPP