1 // boost/filesystem/operations.hpp ---------------------------------------------------//
3 // Copyright Beman Dawes 2002-2009
4 // Copyright Jan Langer 2002
5 // Copyright Dietmar Kuehl 2001
6 // Copyright Vladimir Prus 2002
7 // Copyright Andrey Semashev 2020-2021
9 // Distributed under the Boost Software License, Version 1.0.
10 // See http://www.boost.org/LICENSE_1_0.txt
12 // Library home page: http://www.boost.org/libs/filesystem
14 //--------------------------------------------------------------------------------------//
16 #ifndef BOOST_FILESYSTEM_OPERATIONS_HPP
17 #define BOOST_FILESYSTEM_OPERATIONS_HPP
19 #include <boost/filesystem/config.hpp>
20 #include <boost/filesystem/path.hpp>
21 #include <boost/filesystem/file_status.hpp>
23 #ifndef BOOST_FILESYSTEM_NO_DEPRECATED
24 // These includes are left for backward compatibility and should be included directly by users, as needed
25 #include <boost/filesystem/exception.hpp>
26 #include <boost/filesystem/directory.hpp>
29 #include <boost/detail/bitmask.hpp>
30 #include <boost/core/scoped_enum.hpp>
31 #include <boost/system/error_code.hpp>
32 #include <boost/cstdint.hpp>
36 #include <boost/filesystem/detail/header.hpp> // must be the last #include
38 //--------------------------------------------------------------------------------------//
41 namespace filesystem {
45 // all values are byte counts
46 boost::uintmax_t capacity;
47 boost::uintmax_t free; // <= capacity
48 boost::uintmax_t available; // <= free
51 BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(copy_options, unsigned int)
53 none = 0u, // Default. For copy_file: error if the target file exists. For copy: do not recurse, follow symlinks, copy file contents.
56 skip_existing = 1u, // Don't overwrite the existing target file, don't report an error
57 overwrite_existing = 1u << 1, // Overwrite existing file
58 update_existing = 1u << 2, // Overwrite existing file if its last write time is older than the replacement file
59 synchronize_data = 1u << 3, // Flush all buffered data written to the target file to permanent storage
60 synchronize = 1u << 4, // Flush all buffered data and attributes written to the target file to permanent storage
63 recursive = 1u << 8, // Recurse into sub-directories
64 copy_symlinks = 1u << 9, // Copy symlinks as symlinks instead of copying the referenced file
65 skip_symlinks = 1u << 10, // Don't copy symlinks
66 directories_only = 1u << 11, // Only copy directory structure, do not copy non-directory files
67 create_symlinks = 1u << 12, // Create symlinks instead of copying files
68 create_hard_links = 1u << 13, // Create hard links instead of copying files
69 _detail_recursing = 1u << 14 // Internal use only, do not use
71 BOOST_SCOPED_ENUM_DECLARE_END(copy_options)
73 BOOST_BITMASK(BOOST_SCOPED_ENUM_NATIVE(copy_options))
75 #if !defined(BOOST_FILESYSTEM_NO_DEPRECATED)
76 BOOST_SCOPED_ENUM_DECLARE_BEGIN(copy_option)
78 none = static_cast< unsigned int >(copy_options::none),
79 fail_if_exists = none,
80 overwrite_if_exists = static_cast< unsigned int >(copy_options::overwrite_existing)
82 BOOST_SCOPED_ENUM_DECLARE_END(copy_option)
85 //--------------------------------------------------------------------------------------//
86 // implementation details //
87 //--------------------------------------------------------------------------------------//
92 path absolute(path const& p, path const& base, system::error_code* ec = 0);
94 file_status status(path const& p, system::error_code* ec = 0);
96 file_status symlink_status(path const& p, system::error_code* ec = 0);
98 bool is_empty(path const& p, system::error_code* ec = 0);
100 path initial_path(system::error_code* ec = 0);
101 BOOST_FILESYSTEM_DECL
102 path canonical(path const& p, path const& base, system::error_code* ec = 0);
103 BOOST_FILESYSTEM_DECL
104 void copy(path const& from, path const& to, unsigned int options, system::error_code* ec = 0);
105 #if !defined(BOOST_FILESYSTEM_NO_DEPRECATED)
106 BOOST_FILESYSTEM_DECL
107 void copy_directory(path const& from, path const& to, system::error_code* ec = 0);
109 BOOST_FILESYSTEM_DECL
110 bool copy_file(path const& from, path const& to, // See ticket #2925
111 unsigned int options, system::error_code* ec = 0); // see copy_options for options
112 BOOST_FILESYSTEM_DECL
113 void copy_symlink(path const& existing_symlink, path const& new_symlink, system::error_code* ec = 0);
114 BOOST_FILESYSTEM_DECL
115 bool create_directories(path const& p, system::error_code* ec = 0);
116 BOOST_FILESYSTEM_DECL
117 bool create_directory(path const& p, const path* existing, system::error_code* ec = 0);
118 BOOST_FILESYSTEM_DECL
119 void create_directory_symlink(path const& to, path const& from, system::error_code* ec = 0);
120 BOOST_FILESYSTEM_DECL
121 void create_hard_link(path const& to, path const& from, system::error_code* ec = 0);
122 BOOST_FILESYSTEM_DECL
123 void create_symlink(path const& to, path const& from, system::error_code* ec = 0);
124 BOOST_FILESYSTEM_DECL
125 path current_path(system::error_code* ec = 0);
126 BOOST_FILESYSTEM_DECL
127 void current_path(path const& p, system::error_code* ec = 0);
128 BOOST_FILESYSTEM_DECL
129 bool equivalent(path const& p1, path const& p2, system::error_code* ec = 0);
130 BOOST_FILESYSTEM_DECL
131 boost::uintmax_t file_size(path const& p, system::error_code* ec = 0);
132 BOOST_FILESYSTEM_DECL
133 boost::uintmax_t hard_link_count(path const& p, system::error_code* ec = 0);
134 BOOST_FILESYSTEM_DECL
135 std::time_t creation_time(path const& p, system::error_code* ec = 0);
136 BOOST_FILESYSTEM_DECL
137 std::time_t last_write_time(path const& p, system::error_code* ec = 0);
138 BOOST_FILESYSTEM_DECL
139 void last_write_time(path const& p, const std::time_t new_time, system::error_code* ec = 0);
140 BOOST_FILESYSTEM_DECL
141 void permissions(path const& p, perms prms, system::error_code* ec = 0);
142 BOOST_FILESYSTEM_DECL
143 path read_symlink(path const& p, system::error_code* ec = 0);
144 BOOST_FILESYSTEM_DECL
145 path relative(path const& p, path const& base, system::error_code* ec = 0);
146 BOOST_FILESYSTEM_DECL
147 bool remove(path const& p, system::error_code* ec = 0);
148 BOOST_FILESYSTEM_DECL
149 boost::uintmax_t remove_all(path const& p, system::error_code* ec = 0);
150 BOOST_FILESYSTEM_DECL
151 void rename(path const& old_p, path const& new_p, system::error_code* ec = 0);
152 BOOST_FILESYSTEM_DECL
153 void resize_file(path const& p, uintmax_t size, system::error_code* ec = 0);
154 BOOST_FILESYSTEM_DECL
155 space_info space(path const& p, system::error_code* ec = 0);
156 BOOST_FILESYSTEM_DECL
157 path system_complete(path const& p, system::error_code* ec = 0);
158 BOOST_FILESYSTEM_DECL
159 path temp_directory_path(system::error_code* ec = 0);
160 BOOST_FILESYSTEM_DECL
161 path unique_path(path const& p, system::error_code* ec = 0);
162 BOOST_FILESYSTEM_DECL
163 path weakly_canonical(path const& p, path const& base, system::error_code* ec = 0);
165 } // namespace detail
167 //--------------------------------------------------------------------------------------//
169 // status query functions //
171 //--------------------------------------------------------------------------------------//
173 inline file_status status(path const& p)
175 return detail::status(p);
178 inline file_status status(path const& p, system::error_code& ec)
180 return detail::status(p, &ec);
183 inline file_status symlink_status(path const& p)
185 return detail::symlink_status(p);
188 inline file_status symlink_status(path const& p, system::error_code& ec)
190 return detail::symlink_status(p, &ec);
193 inline bool exists(path const& p)
195 return exists(detail::status(p));
198 inline bool exists(path const& p, system::error_code& ec)
200 return exists(detail::status(p, &ec));
203 inline bool is_directory(path const& p)
205 return is_directory(detail::status(p));
208 inline bool is_directory(path const& p, system::error_code& ec)
210 return is_directory(detail::status(p, &ec));
213 inline bool is_regular_file(path const& p)
215 return is_regular_file(detail::status(p));
218 inline bool is_regular_file(path const& p, system::error_code& ec)
220 return is_regular_file(detail::status(p, &ec));
223 inline bool is_other(path const& p)
225 return is_other(detail::status(p));
228 inline bool is_other(path const& p, system::error_code& ec)
230 return is_other(detail::status(p, &ec));
233 inline bool is_symlink(path const& p)
235 return is_symlink(detail::symlink_status(p));
238 inline bool is_symlink(path const& p, system::error_code& ec)
240 return is_symlink(detail::symlink_status(p, &ec));
243 #ifndef BOOST_FILESYSTEM_NO_DEPRECATED
244 inline bool is_regular(path const& p)
246 return is_regular(detail::status(p));
249 inline bool is_regular(path const& p, system::error_code& ec)
251 return is_regular(detail::status(p, &ec));
255 inline bool is_empty(path const& p)
257 return detail::is_empty(p);
260 inline bool is_empty(path const& p, system::error_code& ec)
262 return detail::is_empty(p, &ec);
265 //--------------------------------------------------------------------------------------//
267 // operational functions //
269 //--------------------------------------------------------------------------------------//
271 inline path initial_path()
273 return detail::initial_path();
276 inline path initial_path(system::error_code& ec)
278 return detail::initial_path(&ec);
281 template< class Path >
284 return initial_path();
286 template< class Path >
287 path initial_path(system::error_code& ec)
289 return detail::initial_path(&ec);
292 inline path current_path()
294 return detail::current_path();
297 inline path current_path(system::error_code& ec)
299 return detail::current_path(&ec);
302 inline void current_path(path const& p)
304 detail::current_path(p);
307 inline void current_path(path const& p, system::error_code& ec) BOOST_NOEXCEPT
309 detail::current_path(p, &ec);
312 inline path absolute(path const& p, path const& base = current_path())
314 return detail::absolute(p, base);
317 inline path absolute(path const& p, system::error_code& ec)
319 path base = current_path(ec);
322 return detail::absolute(p, base, &ec);
325 inline path absolute(path const& p, path const& base, system::error_code& ec)
327 return detail::absolute(p, base, &ec);
330 inline path canonical(path const& p, path const& base = current_path())
332 return detail::canonical(p, base);
335 inline path canonical(path const& p, system::error_code& ec)
337 path base = current_path(ec);
340 return detail::canonical(p, base, &ec);
343 inline path canonical(path const& p, path const& base, system::error_code& ec)
345 return detail::canonical(p, base, &ec);
348 #ifndef BOOST_FILESYSTEM_NO_DEPRECATED
349 inline path complete(path const& p)
351 return absolute(p, initial_path());
354 inline path complete(path const& p, path const& base)
356 return absolute(p, base);
360 inline void copy(path const& from, path const& to)
362 detail::copy(from, to, static_cast< unsigned int >(copy_options::none));
365 inline void copy(path const& from, path const& to, system::error_code& ec) BOOST_NOEXCEPT
367 detail::copy(from, to, static_cast< unsigned int >(copy_options::none), &ec);
370 inline void copy(path const& from, path const& to, BOOST_SCOPED_ENUM_NATIVE(copy_options) options)
372 detail::copy(from, to, static_cast< unsigned int >(options));
375 inline void copy(path const& from, path const& to, BOOST_SCOPED_ENUM_NATIVE(copy_options) options, system::error_code& ec) BOOST_NOEXCEPT
377 detail::copy(from, to, static_cast< unsigned int >(options), &ec);
380 #if !defined(BOOST_FILESYSTEM_NO_DEPRECATED)
381 inline void copy_directory(path const& from, path const& to)
383 detail::copy_directory(from, to);
386 inline void copy_directory(path const& from, path const& to, system::error_code& ec) BOOST_NOEXCEPT
388 detail::copy_directory(from, to, &ec);
392 inline bool copy_file(path const& from, path const& to)
394 return detail::copy_file(from, to, static_cast< unsigned int >(copy_options::none));
397 inline bool copy_file(path const& from, path const& to, system::error_code& ec) BOOST_NOEXCEPT
399 return detail::copy_file(from, to, static_cast< unsigned int >(copy_options::none), &ec);
402 inline bool copy_file(path const& from, path const& to, // See ticket #2925
403 BOOST_SCOPED_ENUM_NATIVE(copy_options) options)
405 return detail::copy_file(from, to, static_cast< unsigned int >(options));
408 inline bool copy_file(path const& from, path const& to, // See ticket #2925
409 BOOST_SCOPED_ENUM_NATIVE(copy_options) options, system::error_code& ec) BOOST_NOEXCEPT
411 return detail::copy_file(from, to, static_cast< unsigned int >(options), &ec);
414 #if !defined(BOOST_FILESYSTEM_NO_DEPRECATED)
415 inline bool copy_file(path const& from, path const& to, // See ticket #2925
416 BOOST_SCOPED_ENUM_NATIVE(copy_option) options)
418 return detail::copy_file(from, to, static_cast< unsigned int >(options));
421 inline bool copy_file(path const& from, path const& to, // See ticket #2925
422 BOOST_SCOPED_ENUM_NATIVE(copy_option) options, system::error_code& ec) BOOST_NOEXCEPT
424 return detail::copy_file(from, to, static_cast< unsigned int >(options), &ec);
426 #endif // !defined(BOOST_FILESYSTEM_NO_DEPRECATED)
428 inline void copy_symlink(path const& existing_symlink, path const& new_symlink)
430 detail::copy_symlink(existing_symlink, new_symlink);
433 inline void copy_symlink(path const& existing_symlink, path const& new_symlink, system::error_code& ec) BOOST_NOEXCEPT
435 detail::copy_symlink(existing_symlink, new_symlink, &ec);
438 inline bool create_directories(path const& p)
440 return detail::create_directories(p);
443 inline bool create_directories(path const& p, system::error_code& ec) BOOST_NOEXCEPT
445 return detail::create_directories(p, &ec);
448 inline bool create_directory(path const& p)
450 return detail::create_directory(p, 0);
453 inline bool create_directory(path const& p, system::error_code& ec) BOOST_NOEXCEPT
455 return detail::create_directory(p, 0, &ec);
458 inline bool create_directory(path const& p, path const& existing)
460 return detail::create_directory(p, &existing);
463 inline bool create_directory(path const& p, path const& existing, system::error_code& ec) BOOST_NOEXCEPT
465 return detail::create_directory(p, &existing, &ec);
468 inline void create_directory_symlink(path const& to, path const& from)
470 detail::create_directory_symlink(to, from);
473 inline void create_directory_symlink(path const& to, path const& from, system::error_code& ec) BOOST_NOEXCEPT
475 detail::create_directory_symlink(to, from, &ec);
478 inline void create_hard_link(path const& to, path const& new_hard_link)
480 detail::create_hard_link(to, new_hard_link);
483 inline void create_hard_link(path const& to, path const& new_hard_link, system::error_code& ec) BOOST_NOEXCEPT
485 detail::create_hard_link(to, new_hard_link, &ec);
488 inline void create_symlink(path const& to, path const& new_symlink)
490 detail::create_symlink(to, new_symlink);
493 inline void create_symlink(path const& to, path const& new_symlink, system::error_code& ec) BOOST_NOEXCEPT
495 detail::create_symlink(to, new_symlink, &ec);
498 inline bool equivalent(path const& p1, path const& p2)
500 return detail::equivalent(p1, p2);
503 inline bool equivalent(path const& p1, path const& p2, system::error_code& ec) BOOST_NOEXCEPT
505 return detail::equivalent(p1, p2, &ec);
508 inline boost::uintmax_t file_size(path const& p)
510 return detail::file_size(p);
513 inline boost::uintmax_t file_size(path const& p, system::error_code& ec) BOOST_NOEXCEPT
515 return detail::file_size(p, &ec);
518 inline boost::uintmax_t hard_link_count(path const& p)
520 return detail::hard_link_count(p);
523 inline boost::uintmax_t hard_link_count(path const& p, system::error_code& ec) BOOST_NOEXCEPT
525 return detail::hard_link_count(p, &ec);
528 inline std::time_t creation_time(path const& p)
530 return detail::creation_time(p);
533 inline std::time_t creation_time(path const& p, system::error_code& ec) BOOST_NOEXCEPT
535 return detail::creation_time(p, &ec);
538 inline std::time_t last_write_time(path const& p)
540 return detail::last_write_time(p);
543 inline std::time_t last_write_time(path const& p, system::error_code& ec) BOOST_NOEXCEPT
545 return detail::last_write_time(p, &ec);
548 inline void last_write_time(path const& p, const std::time_t new_time)
550 detail::last_write_time(p, new_time);
553 inline void last_write_time(path const& p, const std::time_t new_time, system::error_code& ec) BOOST_NOEXCEPT
555 detail::last_write_time(p, new_time, &ec);
558 inline void permissions(path const& p, perms prms)
560 detail::permissions(p, prms);
563 inline void permissions(path const& p, perms prms, system::error_code& ec) BOOST_NOEXCEPT
565 detail::permissions(p, prms, &ec);
568 inline path read_symlink(path const& p)
570 return detail::read_symlink(p);
573 inline path read_symlink(path const& p, system::error_code& ec)
575 return detail::read_symlink(p, &ec);
578 inline bool remove(path const& p)
580 return detail::remove(p);
583 inline bool remove(path const& p, system::error_code& ec) BOOST_NOEXCEPT
585 return detail::remove(p, &ec);
588 inline boost::uintmax_t remove_all(path const& p)
590 return detail::remove_all(p);
593 inline boost::uintmax_t remove_all(path const& p, system::error_code& ec) BOOST_NOEXCEPT
595 return detail::remove_all(p, &ec);
598 inline void rename(path const& old_p, path const& new_p)
600 detail::rename(old_p, new_p);
603 inline void rename(path const& old_p, path const& new_p, system::error_code& ec) BOOST_NOEXCEPT
605 detail::rename(old_p, new_p, &ec);
608 // name suggested by Scott McMurray
609 inline void resize_file(path const& p, uintmax_t size)
611 detail::resize_file(p, size);
614 inline void resize_file(path const& p, uintmax_t size, system::error_code& ec) BOOST_NOEXCEPT
616 detail::resize_file(p, size, &ec);
619 inline path relative(path const& p, path const& base = current_path())
621 return detail::relative(p, base);
624 inline path relative(path const& p, system::error_code& ec)
626 path base = current_path(ec);
629 return detail::relative(p, base, &ec);
632 inline path relative(path const& p, path const& base, system::error_code& ec)
634 return detail::relative(p, base, &ec);
637 inline space_info space(path const& p)
639 return detail::space(p);
642 inline space_info space(path const& p, system::error_code& ec) BOOST_NOEXCEPT
644 return detail::space(p, &ec);
647 #ifndef BOOST_FILESYSTEM_NO_DEPRECATED
648 inline bool symbolic_link_exists(path const& p)
650 return is_symlink(filesystem::symlink_status(p));
654 inline path system_complete(path const& p)
656 return detail::system_complete(p);
659 inline path system_complete(path const& p, system::error_code& ec)
661 return detail::system_complete(p, &ec);
664 inline path temp_directory_path()
666 return detail::temp_directory_path();
669 inline path temp_directory_path(system::error_code& ec)
671 return detail::temp_directory_path(&ec);
674 inline path unique_path(path const& p = "%%%%-%%%%-%%%%-%%%%")
676 return detail::unique_path(p);
679 inline path unique_path(path const& p, system::error_code& ec)
681 return detail::unique_path(p, &ec);
684 inline path weakly_canonical(path const& p, path const& base = current_path())
686 return detail::weakly_canonical(p, base);
689 inline path weakly_canonical(path const& p, system::error_code& ec)
691 path base = current_path(ec);
694 return detail::weakly_canonical(p, base, &ec);
697 inline path weakly_canonical(path const& p, path const& base, system::error_code& ec)
699 return detail::weakly_canonical(p, base, &ec);
702 // test helper -----------------------------------------------------------------------//
704 // Not part of the documented interface since false positives are possible;
705 // there is no law that says that an OS that has large stat.st_size
706 // actually supports large file sizes.
710 BOOST_FILESYSTEM_DECL bool possible_large_file_size_support();
712 } // namespace detail
714 } // namespace filesystem
717 #include <boost/filesystem/detail/footer.hpp>
719 #endif // BOOST_FILESYSTEM_OPERATIONS_HPP