operations.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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/filesystem/file_status.hpp>
  19. #ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  20. // These includes are left for backward compatibility and should be included directly by users, as needed
  21. #include <boost/filesystem/exception.hpp>
  22. #include <boost/filesystem/directory.hpp>
  23. #endif
  24. #include <boost/core/scoped_enum.hpp>
  25. #include <boost/system/error_code.hpp>
  26. #include <boost/cstdint.hpp>
  27. #include <string>
  28. #include <ctime>
  29. #include <boost/config/abi_prefix.hpp> // must be the last #include
  30. //--------------------------------------------------------------------------------------//
  31. namespace boost {
  32. namespace filesystem {
  33. struct space_info
  34. {
  35. // all values are byte counts
  36. boost::uintmax_t capacity;
  37. boost::uintmax_t free; // <= capacity
  38. boost::uintmax_t available; // <= free
  39. };
  40. BOOST_SCOPED_ENUM_DECLARE_BEGIN(copy_option)
  41. {none=0, fail_if_exists = none, overwrite_if_exists}
  42. BOOST_SCOPED_ENUM_DECLARE_END(copy_option)
  43. //--------------------------------------------------------------------------------------//
  44. // implementation details //
  45. //--------------------------------------------------------------------------------------//
  46. namespace detail {
  47. // We cannot pass a BOOST_SCOPED_ENUM to a compled function because it will result
  48. // in an undefined reference if the library is compled with -std=c++0x but the use
  49. // is compiled in C++03 mode, or vice versa. See tickets 6124, 6779, 10038.
  50. enum copy_option {none=0, fail_if_exists = none, overwrite_if_exists};
  51. BOOST_FILESYSTEM_DECL
  52. file_status status(const path&p, system::error_code* ec=0);
  53. BOOST_FILESYSTEM_DECL
  54. file_status symlink_status(const path& p, system::error_code* ec=0);
  55. BOOST_FILESYSTEM_DECL
  56. bool is_empty(const path& p, system::error_code* ec=0);
  57. BOOST_FILESYSTEM_DECL
  58. path initial_path(system::error_code* ec=0);
  59. BOOST_FILESYSTEM_DECL
  60. path canonical(const path& p, const path& base, system::error_code* ec=0);
  61. BOOST_FILESYSTEM_DECL
  62. void copy(const path& from, const path& to, system::error_code* ec=0);
  63. BOOST_FILESYSTEM_DECL
  64. void copy_directory(const path& from, const path& to, system::error_code* ec=0);
  65. BOOST_FILESYSTEM_DECL
  66. void copy_file(const path& from, const path& to, // See ticket #2925
  67. detail::copy_option option, system::error_code* ec=0);
  68. BOOST_FILESYSTEM_DECL
  69. void copy_symlink(const path& existing_symlink, const path& new_symlink, system::error_code* ec=0);
  70. BOOST_FILESYSTEM_DECL
  71. bool create_directories(const path& p, system::error_code* ec=0);
  72. BOOST_FILESYSTEM_DECL
  73. bool create_directory(const path& p, system::error_code* ec=0);
  74. BOOST_FILESYSTEM_DECL
  75. void create_directory_symlink(const path& to, const path& from,
  76. system::error_code* ec=0);
  77. BOOST_FILESYSTEM_DECL
  78. void create_hard_link(const path& to, const path& from, system::error_code* ec=0);
  79. BOOST_FILESYSTEM_DECL
  80. void create_symlink(const path& to, const path& from, system::error_code* ec=0);
  81. BOOST_FILESYSTEM_DECL
  82. path current_path(system::error_code* ec=0);
  83. BOOST_FILESYSTEM_DECL
  84. void current_path(const path& p, system::error_code* ec=0);
  85. BOOST_FILESYSTEM_DECL
  86. bool equivalent(const path& p1, const path& p2, system::error_code* ec=0);
  87. BOOST_FILESYSTEM_DECL
  88. boost::uintmax_t file_size(const path& p, system::error_code* ec=0);
  89. BOOST_FILESYSTEM_DECL
  90. boost::uintmax_t hard_link_count(const path& p, system::error_code* ec=0);
  91. BOOST_FILESYSTEM_DECL
  92. std::time_t last_write_time(const path& p, system::error_code* ec=0);
  93. BOOST_FILESYSTEM_DECL
  94. void last_write_time(const path& p, const std::time_t new_time,
  95. system::error_code* ec=0);
  96. BOOST_FILESYSTEM_DECL
  97. void permissions(const path& p, perms prms, system::error_code* ec=0);
  98. BOOST_FILESYSTEM_DECL
  99. path read_symlink(const path& p, system::error_code* ec=0);
  100. BOOST_FILESYSTEM_DECL
  101. path relative(const path& p, const path& base, system::error_code* ec = 0);
  102. BOOST_FILESYSTEM_DECL
  103. bool remove(const path& p, system::error_code* ec=0);
  104. BOOST_FILESYSTEM_DECL
  105. boost::uintmax_t remove_all(const path& p, system::error_code* ec=0);
  106. BOOST_FILESYSTEM_DECL
  107. void rename(const path& old_p, const path& new_p, system::error_code* ec=0);
  108. BOOST_FILESYSTEM_DECL
  109. void resize_file(const path& p, uintmax_t size, system::error_code* ec=0);
  110. BOOST_FILESYSTEM_DECL
  111. space_info space(const path& p, system::error_code* ec=0);
  112. BOOST_FILESYSTEM_DECL
  113. path system_complete(const path& p, system::error_code* ec=0);
  114. BOOST_FILESYSTEM_DECL
  115. path temp_directory_path(system::error_code* ec=0);
  116. BOOST_FILESYSTEM_DECL
  117. path unique_path(const path& p, system::error_code* ec=0);
  118. BOOST_FILESYSTEM_DECL
  119. path weakly_canonical(const path& p, system::error_code* ec = 0);
  120. } // namespace detail
  121. //--------------------------------------------------------------------------------------//
  122. // //
  123. // status query functions //
  124. // //
  125. //--------------------------------------------------------------------------------------//
  126. inline
  127. file_status status(const path& p) {return detail::status(p);}
  128. inline
  129. file_status status(const path& p, system::error_code& ec)
  130. {return detail::status(p, &ec);}
  131. inline
  132. file_status symlink_status(const path& p) {return detail::symlink_status(p);}
  133. inline
  134. file_status symlink_status(const path& p, system::error_code& ec)
  135. {return detail::symlink_status(p, &ec);}
  136. inline
  137. bool exists(const path& p) {return exists(detail::status(p));}
  138. inline
  139. bool exists(const path& p, system::error_code& ec)
  140. {return exists(detail::status(p, &ec));}
  141. inline
  142. bool is_directory(const path& p) {return is_directory(detail::status(p));}
  143. inline
  144. bool is_directory(const path& p, system::error_code& ec)
  145. {return is_directory(detail::status(p, &ec));}
  146. inline
  147. bool is_regular_file(const path& p) {return is_regular_file(detail::status(p));}
  148. inline
  149. bool is_regular_file(const path& p, system::error_code& ec)
  150. {return is_regular_file(detail::status(p, &ec));}
  151. inline
  152. bool is_other(const path& p) {return is_other(detail::status(p));}
  153. inline
  154. bool is_other(const path& p, system::error_code& ec)
  155. {return is_other(detail::status(p, &ec));}
  156. inline
  157. bool is_symlink(const path& p) {return is_symlink(detail::symlink_status(p));}
  158. inline
  159. bool is_symlink(const path& p, system::error_code& ec)
  160. {return is_symlink(detail::symlink_status(p, &ec));}
  161. #ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  162. inline
  163. bool is_regular(const path& p) {return is_regular(detail::status(p));}
  164. inline
  165. bool is_regular(const path& p, system::error_code& ec)
  166. {return is_regular(detail::status(p, &ec));}
  167. #endif
  168. inline
  169. bool is_empty(const path& p) {return detail::is_empty(p);}
  170. inline
  171. bool is_empty(const path& p, system::error_code& ec)
  172. {return detail::is_empty(p, &ec);}
  173. //--------------------------------------------------------------------------------------//
  174. // //
  175. // operational functions //
  176. // in alphabetical order, unless otherwise noted //
  177. // //
  178. //--------------------------------------------------------------------------------------//
  179. // forward declarations
  180. path current_path(); // fwd declaration
  181. path initial_path();
  182. BOOST_FILESYSTEM_DECL
  183. path absolute(const path& p, const path& base=current_path());
  184. // If base.is_absolute(), throws nothing. Thus no need for ec argument
  185. inline
  186. path canonical(const path& p, const path& base=current_path())
  187. {return detail::canonical(p, base);}
  188. inline
  189. path canonical(const path& p, system::error_code& ec)
  190. {return detail::canonical(p, current_path(), &ec);}
  191. inline
  192. path canonical(const path& p, const path& base, system::error_code& ec)
  193. {return detail::canonical(p, base, &ec);}
  194. #ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  195. inline
  196. path complete(const path& p)
  197. {
  198. return absolute(p, initial_path());
  199. }
  200. inline
  201. path complete(const path& p, const path& base)
  202. {
  203. return absolute(p, base);
  204. }
  205. #endif
  206. inline
  207. void copy(const path& from, const path& to) {detail::copy(from, to);}
  208. inline
  209. void copy(const path& from, const path& to, system::error_code& ec) BOOST_NOEXCEPT
  210. {detail::copy(from, to, &ec);}
  211. inline
  212. void copy_directory(const path& from, const path& to)
  213. {detail::copy_directory(from, to);}
  214. inline
  215. void copy_directory(const path& from, const path& to, system::error_code& ec) BOOST_NOEXCEPT
  216. {detail::copy_directory(from, to, &ec);}
  217. inline
  218. void copy_file(const path& from, const path& to, // See ticket #2925
  219. BOOST_SCOPED_ENUM_NATIVE(copy_option) option)
  220. {
  221. detail::copy_file(from, to, static_cast<detail::copy_option>(option));
  222. }
  223. inline
  224. void copy_file(const path& from, const path& to)
  225. {
  226. detail::copy_file(from, to, detail::fail_if_exists);
  227. }
  228. inline
  229. void copy_file(const path& from, const path& to, // See ticket #2925
  230. BOOST_SCOPED_ENUM_NATIVE(copy_option) option, system::error_code& ec) BOOST_NOEXCEPT
  231. {
  232. detail::copy_file(from, to, static_cast<detail::copy_option>(option), &ec);
  233. }
  234. inline
  235. void copy_file(const path& from, const path& to, system::error_code& ec) BOOST_NOEXCEPT
  236. {
  237. detail::copy_file(from, to, detail::fail_if_exists, &ec);
  238. }
  239. inline
  240. void copy_symlink(const path& existing_symlink,
  241. const path& new_symlink) {detail::copy_symlink(existing_symlink, new_symlink);}
  242. inline
  243. void copy_symlink(const path& existing_symlink, const path& new_symlink,
  244. system::error_code& ec) BOOST_NOEXCEPT
  245. {detail::copy_symlink(existing_symlink, new_symlink, &ec);}
  246. inline
  247. bool create_directories(const path& p) {return detail::create_directories(p);}
  248. inline
  249. bool create_directories(const path& p, system::error_code& ec) BOOST_NOEXCEPT
  250. {return detail::create_directories(p, &ec);}
  251. inline
  252. bool create_directory(const path& p) {return detail::create_directory(p);}
  253. inline
  254. bool create_directory(const path& p, system::error_code& ec) BOOST_NOEXCEPT
  255. {return detail::create_directory(p, &ec);}
  256. inline
  257. void create_directory_symlink(const path& to, const path& from)
  258. {detail::create_directory_symlink(to, from);}
  259. inline
  260. void create_directory_symlink(const path& to, const path& from, system::error_code& ec) BOOST_NOEXCEPT
  261. {detail::create_directory_symlink(to, from, &ec);}
  262. inline
  263. void create_hard_link(const path& to, const path& new_hard_link) {detail::create_hard_link(to, new_hard_link);}
  264. inline
  265. void create_hard_link(const path& to, const path& new_hard_link, system::error_code& ec) BOOST_NOEXCEPT
  266. {detail::create_hard_link(to, new_hard_link, &ec);}
  267. inline
  268. void create_symlink(const path& to, const path& new_symlink) {detail::create_symlink(to, new_symlink);}
  269. inline
  270. void create_symlink(const path& to, const path& new_symlink, system::error_code& ec) BOOST_NOEXCEPT
  271. {detail::create_symlink(to, new_symlink, &ec);}
  272. inline
  273. path current_path() {return detail::current_path();}
  274. inline
  275. path current_path(system::error_code& ec) {return detail::current_path(&ec);}
  276. inline
  277. void current_path(const path& p) {detail::current_path(p);}
  278. inline
  279. void current_path(const path& p, system::error_code& ec) BOOST_NOEXCEPT {detail::current_path(p, &ec);}
  280. inline
  281. bool equivalent(const path& p1, const path& p2) {return detail::equivalent(p1, p2);}
  282. inline
  283. bool equivalent(const path& p1, const path& p2, system::error_code& ec) BOOST_NOEXCEPT
  284. {return detail::equivalent(p1, p2, &ec);}
  285. inline
  286. boost::uintmax_t file_size(const path& p) {return detail::file_size(p);}
  287. inline
  288. boost::uintmax_t file_size(const path& p, system::error_code& ec) BOOST_NOEXCEPT
  289. {return detail::file_size(p, &ec);}
  290. inline
  291. boost::uintmax_t hard_link_count(const path& p) {return detail::hard_link_count(p);}
  292. inline
  293. boost::uintmax_t hard_link_count(const path& p, system::error_code& ec) BOOST_NOEXCEPT
  294. {return detail::hard_link_count(p, &ec);}
  295. inline
  296. path initial_path() {return detail::initial_path();}
  297. inline
  298. path initial_path(system::error_code& ec) {return detail::initial_path(&ec);}
  299. template <class Path>
  300. path initial_path() {return initial_path();}
  301. template <class Path>
  302. path initial_path(system::error_code& ec) {return detail::initial_path(&ec);}
  303. inline
  304. std::time_t last_write_time(const path& p) {return detail::last_write_time(p);}
  305. inline
  306. std::time_t last_write_time(const path& p, system::error_code& ec) BOOST_NOEXCEPT
  307. {return detail::last_write_time(p, &ec);}
  308. inline
  309. void last_write_time(const path& p, const std::time_t new_time)
  310. {detail::last_write_time(p, new_time);}
  311. inline
  312. void last_write_time(const path& p, const std::time_t new_time,
  313. system::error_code& ec) BOOST_NOEXCEPT
  314. {detail::last_write_time(p, new_time, &ec);}
  315. inline
  316. void permissions(const path& p, perms prms)
  317. {detail::permissions(p, prms);}
  318. inline
  319. void permissions(const path& p, perms prms, system::error_code& ec) BOOST_NOEXCEPT
  320. {detail::permissions(p, prms, &ec);}
  321. inline
  322. path read_symlink(const path& p) {return detail::read_symlink(p);}
  323. inline
  324. path read_symlink(const path& p, system::error_code& ec)
  325. {return detail::read_symlink(p, &ec);}
  326. inline
  327. bool remove(const path& p) {return detail::remove(p);}
  328. inline
  329. bool remove(const path& p, system::error_code& ec) BOOST_NOEXCEPT
  330. {return detail::remove(p, &ec);}
  331. inline
  332. boost::uintmax_t remove_all(const path& p) {return detail::remove_all(p);}
  333. inline
  334. boost::uintmax_t remove_all(const path& p, system::error_code& ec) BOOST_NOEXCEPT
  335. {return detail::remove_all(p, &ec);}
  336. inline
  337. void rename(const path& old_p, const path& new_p) {detail::rename(old_p, new_p);}
  338. inline
  339. void rename(const path& old_p, const path& new_p, system::error_code& ec) BOOST_NOEXCEPT
  340. {detail::rename(old_p, new_p, &ec);}
  341. inline // name suggested by Scott McMurray
  342. void resize_file(const path& p, uintmax_t size) {detail::resize_file(p, size);}
  343. inline
  344. void resize_file(const path& p, uintmax_t size, system::error_code& ec) BOOST_NOEXCEPT
  345. {detail::resize_file(p, size, &ec);}
  346. inline
  347. path relative(const path& p, const path& base=current_path())
  348. {return detail::relative(p, base);}
  349. inline
  350. path relative(const path& p, system::error_code& ec)
  351. {return detail::relative(p, current_path(), &ec);}
  352. inline
  353. path relative(const path& p, const path& base, system::error_code& ec)
  354. {return detail::relative(p, base, &ec);}
  355. inline
  356. space_info space(const path& p) {return detail::space(p);}
  357. inline
  358. space_info space(const path& p, system::error_code& ec) BOOST_NOEXCEPT
  359. {return detail::space(p, &ec);}
  360. #ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  361. inline bool symbolic_link_exists(const path& p)
  362. { return is_symlink(filesystem::symlink_status(p)); }
  363. #endif
  364. inline
  365. path system_complete(const path& p) {return detail::system_complete(p);}
  366. inline
  367. path system_complete(const path& p, system::error_code& ec)
  368. {return detail::system_complete(p, &ec);}
  369. inline
  370. path temp_directory_path() {return detail::temp_directory_path();}
  371. inline
  372. path temp_directory_path(system::error_code& ec)
  373. {return detail::temp_directory_path(&ec);}
  374. inline
  375. path unique_path(const path& p="%%%%-%%%%-%%%%-%%%%")
  376. {return detail::unique_path(p);}
  377. inline
  378. path unique_path(const path& p, system::error_code& ec)
  379. {return detail::unique_path(p, &ec);}
  380. inline
  381. path weakly_canonical(const path& p) {return detail::weakly_canonical(p);}
  382. inline
  383. path weakly_canonical(const path& p, system::error_code& ec)
  384. {return detail::weakly_canonical(p, &ec);}
  385. // test helper -----------------------------------------------------------------------//
  386. // Not part of the documented interface since false positives are possible;
  387. // there is no law that says that an OS that has large stat.st_size
  388. // actually supports large file sizes.
  389. namespace detail {
  390. BOOST_FILESYSTEM_DECL bool possible_large_file_size_support();
  391. } // namespace detail
  392. } // namespace filesystem
  393. } // namespace boost
  394. #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
  395. #endif // BOOST_FILESYSTEM3_OPERATIONS_HPP