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
8 // Distributed under the Boost Software License, Version 1.0.
9 // See http://www.boost.org/LICENSE_1_0.txt
11 // Library home page: http://www.boost.org/libs/filesystem
13 //--------------------------------------------------------------------------------------//
15 #ifndef BOOST_FILESYSTEM3_OPERATIONS_HPP
16 #define BOOST_FILESYSTEM3_OPERATIONS_HPP
18 #include <boost/config.hpp>
20 # if defined( BOOST_NO_STD_WSTRING )
21 # error Configuration not supported: Boost.Filesystem V3 and later requires std::wstring support
24 #include <boost/filesystem/config.hpp>
25 #include <boost/filesystem/path.hpp>
27 #include <boost/detail/scoped_enum_emulation.hpp>
28 #include <boost/detail/bitmask.hpp>
29 #include <boost/system/error_code.hpp>
30 #include <boost/system/system_error.hpp>
31 #include <boost/shared_ptr.hpp>
32 #include <boost/utility/enable_if.hpp>
33 #include <boost/type_traits/is_same.hpp>
34 #include <boost/cstdint.hpp>
35 #include <boost/range/mutable_iterator.hpp>
36 #include <boost/range/const_iterator.hpp>
37 #include <boost/assert.hpp>
39 #include <utility> // for pair
44 #ifdef BOOST_WINDOWS_API
48 #include <boost/config/abi_prefix.hpp> // must be the last #include
50 //--------------------------------------------------------------------------------------//
57 //--------------------------------------------------------------------------------------//
59 // class filesystem_error //
61 //--------------------------------------------------------------------------------------//
63 class BOOST_SYMBOL_VISIBLE filesystem_error : public system::system_error
65 // see http://www.boost.org/more/error_handling.html for design rationale
67 // all functions are inline to avoid issues with crossing dll boundaries
69 // functions previously throw() are now BOOST_NOEXCEPT_OR_NOTHROW
70 // functions previously without throw() are now BOOST_NOEXCEPT
73 // compiler generates copy constructor and copy assignment
76 const std::string & what_arg, system::error_code ec) BOOST_NOEXCEPT
77 : system::system_error(ec, what_arg)
81 m_imp_ptr.reset(new m_imp);
83 catch (...) { m_imp_ptr.reset(); }
87 const std::string & what_arg, const path& path1_arg,
88 system::error_code ec) BOOST_NOEXCEPT
89 : system::system_error(ec, what_arg)
93 m_imp_ptr.reset(new m_imp);
94 m_imp_ptr->m_path1 = path1_arg;
96 catch (...) { m_imp_ptr.reset(); }
100 const std::string & what_arg, const path& path1_arg,
101 const path& path2_arg, system::error_code ec) BOOST_NOEXCEPT
102 : system::system_error(ec, what_arg)
106 m_imp_ptr.reset(new m_imp);
107 m_imp_ptr->m_path1 = path1_arg;
108 m_imp_ptr->m_path2 = path2_arg;
110 catch (...) { m_imp_ptr.reset(); }
113 ~filesystem_error() BOOST_NOEXCEPT_OR_NOTHROW{}
115 const path& path1() const BOOST_NOEXCEPT
117 static const path empty_path;
118 return m_imp_ptr.get() ? m_imp_ptr->m_path1 : empty_path;
120 const path& path2() const BOOST_NOEXCEPT
122 static const path empty_path;
123 return m_imp_ptr.get() ? m_imp_ptr->m_path2 : empty_path;
126 const char* what() const BOOST_NOEXCEPT_OR_NOTHROW
128 if (!m_imp_ptr.get())
129 return system::system_error::what();
133 if (m_imp_ptr->m_what.empty())
135 m_imp_ptr->m_what = system::system_error::what();
136 if (!m_imp_ptr->m_path1.empty())
138 m_imp_ptr->m_what += ": \"";
139 m_imp_ptr->m_what += m_imp_ptr->m_path1.string();
140 m_imp_ptr->m_what += "\"";
142 if (!m_imp_ptr->m_path2.empty())
144 m_imp_ptr->m_what += ", \"";
145 m_imp_ptr->m_what += m_imp_ptr->m_path2.string();
146 m_imp_ptr->m_what += "\"";
149 return m_imp_ptr->m_what.c_str();
153 return system::system_error::what();
160 path m_path1; // may be empty()
161 path m_path2; // may be empty()
162 std::string m_what; // not built until needed
164 boost::shared_ptr<m_imp> m_imp_ptr;
167 //--------------------------------------------------------------------------------------//
169 //--------------------------------------------------------------------------------------//
174 # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
175 status_unknown = status_error,
180 // the following may not apply to some operating systems or file systems
186 reparse_file, // Windows: FILE_ATTRIBUTE_REPARSE_POINT that is not a symlink
187 type_unknown, // file does exist, but isn't one of the above types or
188 // we don't have strong enough permission to find its type
190 _detail_directory_symlink // internal use only; never exposed to users
193 //--------------------------------------------------------------------------------------//
195 //--------------------------------------------------------------------------------------//
199 no_perms = 0, // file_not_found is no_perms rather than perms_not_known
201 // POSIX equivalent macros given in comments.
202 // Values are from POSIX and are given in octal per the POSIX standard.
206 owner_read = 0400, // S_IRUSR, Read permission, owner
207 owner_write = 0200, // S_IWUSR, Write permission, owner
208 owner_exe = 0100, // S_IXUSR, Execute/search permission, owner
209 owner_all = 0700, // S_IRWXU, Read, write, execute/search by owner
211 group_read = 040, // S_IRGRP, Read permission, group
212 group_write = 020, // S_IWGRP, Write permission, group
213 group_exe = 010, // S_IXGRP, Execute/search permission, group
214 group_all = 070, // S_IRWXG, Read, write, execute/search by group
216 others_read = 04, // S_IROTH, Read permission, others
217 others_write = 02, // S_IWOTH, Write permission, others
218 others_exe = 01, // S_IXOTH, Execute/search permission, others
219 others_all = 07, // S_IRWXO, Read, write, execute/search by others
221 all_all = 0777, // owner_all|group_all|others_all
225 set_uid_on_exe = 04000, // S_ISUID, Set-user-ID on execution
226 set_gid_on_exe = 02000, // S_ISGID, Set-group-ID on execution
227 sticky_bit = 01000, // S_ISVTX,
228 // (POSIX XSI) On directories, restricted deletion flag
229 // (V7) 'sticky bit': save swapped text even after use
230 // (SunOS) On non-directories: don't cache this file
231 // (SVID-v4.2) On directories: restricted deletion flag
232 // Also see http://en.wikipedia.org/wiki/Sticky_bit
234 perms_mask = 07777, // all_all|set_uid_on_exe|set_gid_on_exe|sticky_bit
236 perms_not_known = 0xFFFF, // present when directory_entry cache not loaded
238 // options for permissions() function
240 add_perms = 0x1000, // adds the given permission bits to the current bits
241 remove_perms = 0x2000, // removes the given permission bits from the current bits;
242 // choose add_perms or remove_perms, not both; if neither add_perms
243 // nor remove_perms is given, replace the current bits with
246 symlink_perms = 0x4000 // on POSIX, don't resolve symlinks; implied on Windows
251 //--------------------------------------------------------------------------------------//
253 //--------------------------------------------------------------------------------------//
255 class BOOST_FILESYSTEM_DECL file_status
258 file_status() BOOST_NOEXCEPT
259 : m_value(status_error), m_perms(perms_not_known) {}
260 explicit file_status(file_type v) BOOST_NOEXCEPT
261 : m_value(v), m_perms(perms_not_known) {}
262 file_status(file_type v, perms prms) BOOST_NOEXCEPT
263 : m_value(v), m_perms(prms) {}
265 // As of October 2015 the interaction between noexcept and =default is so troublesome
266 // for VC++, GCC, and probably other compilers, that =default is not used with noexcept
267 // functions. GCC is not even consistent for the same release on different platforms.
269 file_status(const file_status& rhs) BOOST_NOEXCEPT
270 : m_value(rhs.m_value), m_perms(rhs.m_perms) {}
271 file_status& operator=(const file_status& rhs) BOOST_NOEXCEPT
273 m_value = rhs.m_value;
274 m_perms = rhs.m_perms;
278 # if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
279 file_status(file_status&& rhs) BOOST_NOEXCEPT
281 m_value = std::move(rhs.m_value);
282 m_perms = std::move(rhs.m_perms);
284 file_status& operator=(file_status&& rhs) BOOST_NOEXCEPT
286 m_value = std::move(rhs.m_value);
287 m_perms = std::move(rhs.m_perms);
294 file_type type() const BOOST_NOEXCEPT { return m_value; }
295 perms permissions() const BOOST_NOEXCEPT { return m_perms; }
298 void type(file_type v) BOOST_NOEXCEPT { m_value = v; }
299 void permissions(perms prms) BOOST_NOEXCEPT { m_perms = prms; }
301 bool operator==(const file_status& rhs) const BOOST_NOEXCEPT
302 { return type() == rhs.type() &&
303 permissions() == rhs.permissions(); }
304 bool operator!=(const file_status& rhs) const BOOST_NOEXCEPT
305 { return !(*this == rhs); }
312 inline bool type_present(file_status f) BOOST_NOEXCEPT
313 { return f.type() != status_error; }
314 inline bool permissions_present(file_status f) BOOST_NOEXCEPT
315 {return f.permissions() != perms_not_known;}
316 inline bool status_known(file_status f) BOOST_NOEXCEPT
317 { return type_present(f) && permissions_present(f); }
318 inline bool exists(file_status f) BOOST_NOEXCEPT
319 { return f.type() != status_error
320 && f.type() != file_not_found; }
321 inline bool is_regular_file(file_status f) BOOST_NOEXCEPT
322 { return f.type() == regular_file; }
323 inline bool is_directory(file_status f) BOOST_NOEXCEPT
324 { return f.type() == directory_file; }
325 inline bool is_symlink(file_status f) BOOST_NOEXCEPT
326 { return f.type() == symlink_file; }
327 inline bool is_other(file_status f) BOOST_NOEXCEPT
328 { return exists(f) && !is_regular_file(f)
329 && !is_directory(f) && !is_symlink(f); }
331 # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
332 inline bool is_regular(file_status f) BOOST_NOEXCEPT { return f.type() == regular_file; }
337 // all values are byte counts
338 boost::uintmax_t capacity;
339 boost::uintmax_t free; // <= capacity
340 boost::uintmax_t available; // <= free
343 BOOST_SCOPED_ENUM_START(copy_option)
344 {none=0, fail_if_exists = none, overwrite_if_exists};
345 BOOST_SCOPED_ENUM_END
347 //--------------------------------------------------------------------------------------//
348 // implementation details //
349 //--------------------------------------------------------------------------------------//
353 // We cannot pass a BOOST_SCOPED_ENUM to a compled function because it will result
354 // in an undefined reference if the library is compled with -std=c++0x but the use
355 // is compiled in C++03 mode, or visa versa. See tickets 6124, 6779, 10038.
356 enum copy_option {none=0, fail_if_exists = none, overwrite_if_exists};
358 BOOST_FILESYSTEM_DECL
359 file_status status(const path&p, system::error_code* ec=0);
360 BOOST_FILESYSTEM_DECL
361 file_status symlink_status(const path& p, system::error_code* ec=0);
362 BOOST_FILESYSTEM_DECL
363 bool is_empty(const path& p, system::error_code* ec=0);
364 BOOST_FILESYSTEM_DECL
365 path initial_path(system::error_code* ec=0);
366 BOOST_FILESYSTEM_DECL
367 path canonical(const path& p, const path& base, system::error_code* ec=0);
368 BOOST_FILESYSTEM_DECL
369 void copy(const path& from, const path& to, system::error_code* ec=0);
370 BOOST_FILESYSTEM_DECL
371 void copy_directory(const path& from, const path& to, system::error_code* ec=0);
372 BOOST_FILESYSTEM_DECL
373 void copy_file(const path& from, const path& to, // See ticket #2925
374 detail::copy_option option, system::error_code* ec=0);
375 BOOST_FILESYSTEM_DECL
376 void copy_symlink(const path& existing_symlink, const path& new_symlink, system::error_code* ec=0);
377 BOOST_FILESYSTEM_DECL
378 bool create_directories(const path& p, system::error_code* ec=0);
379 BOOST_FILESYSTEM_DECL
380 bool create_directory(const path& p, system::error_code* ec=0);
381 BOOST_FILESYSTEM_DECL
382 void create_directory_symlink(const path& to, const path& from,
383 system::error_code* ec=0);
384 BOOST_FILESYSTEM_DECL
385 void create_hard_link(const path& to, const path& from, system::error_code* ec=0);
386 BOOST_FILESYSTEM_DECL
387 void create_symlink(const path& to, const path& from, system::error_code* ec=0);
388 BOOST_FILESYSTEM_DECL
389 path current_path(system::error_code* ec=0);
390 BOOST_FILESYSTEM_DECL
391 void current_path(const path& p, system::error_code* ec=0);
392 BOOST_FILESYSTEM_DECL
393 bool equivalent(const path& p1, const path& p2, system::error_code* ec=0);
394 BOOST_FILESYSTEM_DECL
395 boost::uintmax_t file_size(const path& p, system::error_code* ec=0);
396 BOOST_FILESYSTEM_DECL
397 boost::uintmax_t hard_link_count(const path& p, system::error_code* ec=0);
398 BOOST_FILESYSTEM_DECL
399 std::time_t last_write_time(const path& p, system::error_code* ec=0);
400 BOOST_FILESYSTEM_DECL
401 void last_write_time(const path& p, const std::time_t new_time,
402 system::error_code* ec=0);
403 BOOST_FILESYSTEM_DECL
404 void permissions(const path& p, perms prms, system::error_code* ec=0);
405 BOOST_FILESYSTEM_DECL
406 path read_symlink(const path& p, system::error_code* ec=0);
407 BOOST_FILESYSTEM_DECL
408 path relative(const path& p, const path& base, system::error_code* ec = 0);
409 BOOST_FILESYSTEM_DECL
410 bool remove(const path& p, system::error_code* ec=0);
411 BOOST_FILESYSTEM_DECL
412 boost::uintmax_t remove_all(const path& p, system::error_code* ec=0);
413 BOOST_FILESYSTEM_DECL
414 void rename(const path& old_p, const path& new_p, system::error_code* ec=0);
415 BOOST_FILESYSTEM_DECL
416 void resize_file(const path& p, uintmax_t size, system::error_code* ec=0);
417 BOOST_FILESYSTEM_DECL
418 space_info space(const path& p, system::error_code* ec=0);
419 BOOST_FILESYSTEM_DECL
420 path system_complete(const path& p, system::error_code* ec=0);
421 BOOST_FILESYSTEM_DECL
422 path temp_directory_path(system::error_code* ec=0);
423 BOOST_FILESYSTEM_DECL
424 path unique_path(const path& p, system::error_code* ec=0);
425 BOOST_FILESYSTEM_DECL
426 path weakly_canonical(const path& p, system::error_code* ec = 0);
427 } // namespace detail
429 //--------------------------------------------------------------------------------------//
431 // status query functions //
433 //--------------------------------------------------------------------------------------//
436 file_status status(const path& p) {return detail::status(p);}
438 file_status status(const path& p, system::error_code& ec)
439 {return detail::status(p, &ec);}
441 file_status symlink_status(const path& p) {return detail::symlink_status(p);}
443 file_status symlink_status(const path& p, system::error_code& ec)
444 {return detail::symlink_status(p, &ec);}
446 bool exists(const path& p) {return exists(detail::status(p));}
448 bool exists(const path& p, system::error_code& ec)
449 {return exists(detail::status(p, &ec));}
451 bool is_directory(const path& p) {return is_directory(detail::status(p));}
453 bool is_directory(const path& p, system::error_code& ec)
454 {return is_directory(detail::status(p, &ec));}
456 bool is_regular_file(const path& p) {return is_regular_file(detail::status(p));}
458 bool is_regular_file(const path& p, system::error_code& ec)
459 {return is_regular_file(detail::status(p, &ec));}
461 bool is_other(const path& p) {return is_other(detail::status(p));}
463 bool is_other(const path& p, system::error_code& ec)
464 {return is_other(detail::status(p, &ec));}
466 bool is_symlink(const path& p) {return is_symlink(detail::symlink_status(p));}
468 bool is_symlink(const path& p, system::error_code& ec)
469 {return is_symlink(detail::symlink_status(p, &ec));}
470 # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
472 bool is_regular(const path& p) {return is_regular(detail::status(p));}
474 bool is_regular(const path& p, system::error_code& ec)
475 {return is_regular(detail::status(p, &ec));}
479 bool is_empty(const path& p) {return detail::is_empty(p);}
481 bool is_empty(const path& p, system::error_code& ec)
482 {return detail::is_empty(p, &ec);}
484 //--------------------------------------------------------------------------------------//
486 // operational functions //
487 // in alphabetical order, unless otherwise noted //
489 //--------------------------------------------------------------------------------------//
491 // forward declarations
492 path current_path(); // fwd declaration
495 BOOST_FILESYSTEM_DECL
496 path absolute(const path& p, const path& base=current_path());
497 // If base.is_absolute(), throws nothing. Thus no need for ec argument
500 path canonical(const path& p, const path& base=current_path())
501 {return detail::canonical(p, base);}
503 path canonical(const path& p, system::error_code& ec)
504 {return detail::canonical(p, current_path(), &ec);}
506 path canonical(const path& p, const path& base, system::error_code& ec)
507 {return detail::canonical(p, base, &ec);}
509 # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
511 path complete(const path& p)
513 return absolute(p, initial_path());
517 path complete(const path& p, const path& base)
519 return absolute(p, base);
524 void copy(const path& from, const path& to) {detail::copy(from, to);}
527 void copy(const path& from, const path& to, system::error_code& ec) BOOST_NOEXCEPT
528 {detail::copy(from, to, &ec);}
530 void copy_directory(const path& from, const path& to)
531 {detail::copy_directory(from, to);}
533 void copy_directory(const path& from, const path& to, system::error_code& ec) BOOST_NOEXCEPT
534 {detail::copy_directory(from, to, &ec);}
536 void copy_file(const path& from, const path& to, // See ticket #2925
537 BOOST_SCOPED_ENUM(copy_option) option)
539 detail::copy_file(from, to, static_cast<detail::copy_option>(option));
542 void copy_file(const path& from, const path& to)
544 detail::copy_file(from, to, detail::fail_if_exists);
547 void copy_file(const path& from, const path& to, // See ticket #2925
548 BOOST_SCOPED_ENUM(copy_option) option, system::error_code& ec) BOOST_NOEXCEPT
550 detail::copy_file(from, to, static_cast<detail::copy_option>(option), &ec);
553 void copy_file(const path& from, const path& to, system::error_code& ec) BOOST_NOEXCEPT
555 detail::copy_file(from, to, detail::fail_if_exists, &ec);
558 void copy_symlink(const path& existing_symlink,
559 const path& new_symlink) {detail::copy_symlink(existing_symlink, new_symlink);}
562 void copy_symlink(const path& existing_symlink, const path& new_symlink,
563 system::error_code& ec) BOOST_NOEXCEPT
564 {detail::copy_symlink(existing_symlink, new_symlink, &ec);}
566 bool create_directories(const path& p) {return detail::create_directories(p);}
569 bool create_directories(const path& p, system::error_code& ec) BOOST_NOEXCEPT
570 {return detail::create_directories(p, &ec);}
572 bool create_directory(const path& p) {return detail::create_directory(p);}
575 bool create_directory(const path& p, system::error_code& ec) BOOST_NOEXCEPT
576 {return detail::create_directory(p, &ec);}
578 void create_directory_symlink(const path& to, const path& from)
579 {detail::create_directory_symlink(to, from);}
581 void create_directory_symlink(const path& to, const path& from, system::error_code& ec) BOOST_NOEXCEPT
582 {detail::create_directory_symlink(to, from, &ec);}
584 void create_hard_link(const path& to, const path& new_hard_link) {detail::create_hard_link(to, new_hard_link);}
587 void create_hard_link(const path& to, const path& new_hard_link, system::error_code& ec) BOOST_NOEXCEPT
588 {detail::create_hard_link(to, new_hard_link, &ec);}
590 void create_symlink(const path& to, const path& new_symlink) {detail::create_symlink(to, new_symlink);}
593 void create_symlink(const path& to, const path& new_symlink, system::error_code& ec) BOOST_NOEXCEPT
594 {detail::create_symlink(to, new_symlink, &ec);}
596 path current_path() {return detail::current_path();}
599 path current_path(system::error_code& ec) BOOST_NOEXCEPT {return detail::current_path(&ec);}
602 void current_path(const path& p) {detail::current_path(p);}
605 void current_path(const path& p, system::error_code& ec) BOOST_NOEXCEPT {detail::current_path(p, &ec);}
608 bool equivalent(const path& p1, const path& p2) {return detail::equivalent(p1, p2);}
611 bool equivalent(const path& p1, const path& p2, system::error_code& ec) BOOST_NOEXCEPT
612 {return detail::equivalent(p1, p2, &ec);}
614 boost::uintmax_t file_size(const path& p) {return detail::file_size(p);}
617 boost::uintmax_t file_size(const path& p, system::error_code& ec) BOOST_NOEXCEPT
618 {return detail::file_size(p, &ec);}
620 boost::uintmax_t hard_link_count(const path& p) {return detail::hard_link_count(p);}
623 boost::uintmax_t hard_link_count(const path& p, system::error_code& ec) BOOST_NOEXCEPT
624 {return detail::hard_link_count(p, &ec);}
626 path initial_path() {return detail::initial_path();}
629 path initial_path(system::error_code& ec) {return detail::initial_path(&ec);}
631 template <class Path>
632 path initial_path() {return initial_path();}
633 template <class Path>
634 path initial_path(system::error_code& ec) {return detail::initial_path(&ec);}
637 std::time_t last_write_time(const path& p) {return detail::last_write_time(p);}
640 std::time_t last_write_time(const path& p, system::error_code& ec) BOOST_NOEXCEPT
641 {return detail::last_write_time(p, &ec);}
643 void last_write_time(const path& p, const std::time_t new_time)
644 {detail::last_write_time(p, new_time);}
646 void last_write_time(const path& p, const std::time_t new_time,
647 system::error_code& ec) BOOST_NOEXCEPT
648 {detail::last_write_time(p, new_time, &ec);}
650 void permissions(const path& p, perms prms)
651 {detail::permissions(p, prms);}
653 void permissions(const path& p, perms prms, system::error_code& ec) BOOST_NOEXCEPT
654 {detail::permissions(p, prms, &ec);}
657 path read_symlink(const path& p) {return detail::read_symlink(p);}
660 path read_symlink(const path& p, system::error_code& ec)
661 {return detail::read_symlink(p, &ec);}
663 // For standardization, if the committee doesn't like "remove", consider "eliminate"
664 bool remove(const path& p) {return detail::remove(p);}
667 bool remove(const path& p, system::error_code& ec) BOOST_NOEXCEPT
668 {return detail::remove(p, &ec);}
671 boost::uintmax_t remove_all(const path& p) {return detail::remove_all(p);}
674 boost::uintmax_t remove_all(const path& p, system::error_code& ec) BOOST_NOEXCEPT
675 {return detail::remove_all(p, &ec);}
677 void rename(const path& old_p, const path& new_p) {detail::rename(old_p, new_p);}
680 void rename(const path& old_p, const path& new_p, system::error_code& ec) BOOST_NOEXCEPT
681 {detail::rename(old_p, new_p, &ec);}
682 inline // name suggested by Scott McMurray
683 void resize_file(const path& p, uintmax_t size) {detail::resize_file(p, size);}
686 void resize_file(const path& p, uintmax_t size, system::error_code& ec) BOOST_NOEXCEPT
687 {detail::resize_file(p, size, &ec);}
689 path relative(const path& p, const path& base=current_path())
690 {return detail::relative(p, base);}
692 path relative(const path& p, system::error_code& ec)
693 {return detail::relative(p, current_path(), &ec);}
695 path relative(const path& p, const path& base, system::error_code& ec)
696 {return detail::relative(p, base, &ec);}
698 space_info space(const path& p) {return detail::space(p);}
701 space_info space(const path& p, system::error_code& ec) BOOST_NOEXCEPT
702 {return detail::space(p, &ec);}
704 # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
705 inline bool symbolic_link_exists(const path& p)
706 { return is_symlink(symlink_status(p)); }
710 path system_complete(const path& p) {return detail::system_complete(p);}
713 path system_complete(const path& p, system::error_code& ec)
714 {return detail::system_complete(p, &ec);}
716 path temp_directory_path() {return detail::temp_directory_path();}
719 path temp_directory_path(system::error_code& ec)
720 {return detail::temp_directory_path(&ec);}
722 path unique_path(const path& p="%%%%-%%%%-%%%%-%%%%")
723 {return detail::unique_path(p);}
725 path unique_path(const path& p, system::error_code& ec)
726 {return detail::unique_path(p, &ec);}
728 path weakly_canonical(const path& p) {return detail::weakly_canonical(p);}
731 path weakly_canonical(const path& p, system::error_code& ec)
732 {return detail::weakly_canonical(p, &ec);}
734 //--------------------------------------------------------------------------------------//
736 // directory_entry //
738 //--------------------------------------------------------------------------------------//
740 // GCC has a problem with a member function named path within a namespace or
741 // sub-namespace that also has a class named path. The workaround is to always
742 // fully qualify the name path when it refers to the class name.
744 class BOOST_FILESYSTEM_DECL directory_entry
747 typedef boost::filesystem::path::value_type value_type; // enables class path ctor taking directory_entry
749 directory_entry() BOOST_NOEXCEPT {}
750 explicit directory_entry(const boost::filesystem::path& p)
751 : m_path(p), m_status(file_status()), m_symlink_status(file_status())
753 directory_entry(const boost::filesystem::path& p,
754 file_status st, file_status symlink_st = file_status())
755 : m_path(p), m_status(st), m_symlink_status(symlink_st) {}
757 directory_entry(const directory_entry& rhs)
758 : m_path(rhs.m_path), m_status(rhs.m_status), m_symlink_status(rhs.m_symlink_status){}
760 directory_entry& operator=(const directory_entry& rhs)
763 m_status = rhs.m_status;
764 m_symlink_status = rhs.m_symlink_status;
768 // As of October 2015 the interaction between noexcept and =default is so troublesome
769 // for VC++, GCC, and probably other compilers, that =default is not used with noexcept
770 // functions. GCC is not even consistent for the same release on different platforms.
772 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
773 directory_entry(directory_entry&& rhs) BOOST_NOEXCEPT
775 m_path = std::move(rhs.m_path);
776 m_status = std::move(rhs.m_status);
777 m_symlink_status = std::move(rhs.m_symlink_status);
779 directory_entry& operator=(directory_entry&& rhs) BOOST_NOEXCEPT
781 m_path = std::move(rhs.m_path);
782 m_status = std::move(rhs.m_status);
783 m_symlink_status = std::move(rhs.m_symlink_status);
788 void assign(const boost::filesystem::path& p,
789 file_status st = file_status(), file_status symlink_st = file_status())
790 { m_path = p; m_status = st; m_symlink_status = symlink_st; }
792 void replace_filename(const boost::filesystem::path& p,
793 file_status st = file_status(), file_status symlink_st = file_status())
795 m_path.remove_filename();
798 m_symlink_status = symlink_st;
801 # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
802 void replace_leaf(const boost::filesystem::path& p,
803 file_status st, file_status symlink_st)
804 { replace_filename(p, st, symlink_st); }
807 const boost::filesystem::path& path() const BOOST_NOEXCEPT {return m_path;}
808 operator const boost::filesystem::path&() const BOOST_NOEXCEPT
810 file_status status() const {return m_get_status();}
811 file_status status(system::error_code& ec) const BOOST_NOEXCEPT
812 {return m_get_status(&ec); }
813 file_status symlink_status() const {return m_get_symlink_status();}
814 file_status symlink_status(system::error_code& ec) const BOOST_NOEXCEPT
815 {return m_get_symlink_status(&ec); }
817 bool operator==(const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path == rhs.m_path; }
818 bool operator!=(const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path != rhs.m_path;}
819 bool operator< (const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path < rhs.m_path;}
820 bool operator<=(const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path <= rhs.m_path;}
821 bool operator> (const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path > rhs.m_path;}
822 bool operator>=(const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path >= rhs.m_path;}
825 boost::filesystem::path m_path;
826 mutable file_status m_status; // stat()-like
827 mutable file_status m_symlink_status; // lstat()-like
829 file_status m_get_status(system::error_code* ec=0) const;
830 file_status m_get_symlink_status(system::error_code* ec=0) const;
831 }; // directory_entry
833 //--------------------------------------------------------------------------------------//
835 // directory_iterator helpers //
837 //--------------------------------------------------------------------------------------//
839 class directory_iterator;
843 BOOST_FILESYSTEM_DECL
844 system::error_code dir_itr_close(// never throws()
846 # if defined(BOOST_POSIX_API)
853 directory_entry dir_entry;
856 # ifdef BOOST_POSIX_API
857 void* buffer; // see dir_itr_increment implementation
860 dir_itr_imp() : handle(0)
861 # ifdef BOOST_POSIX_API
866 ~dir_itr_imp() // never throws
869 # if defined(BOOST_POSIX_API)
876 // see path::iterator: comment below
877 BOOST_FILESYSTEM_DECL void directory_iterator_construct(directory_iterator& it,
878 const path& p, system::error_code* ec);
879 BOOST_FILESYSTEM_DECL void directory_iterator_increment(directory_iterator& it,
880 system::error_code* ec);
882 } // namespace detail
884 //--------------------------------------------------------------------------------------//
886 // directory_iterator //
888 //--------------------------------------------------------------------------------------//
890 class directory_iterator
891 : public boost::iterator_facade< directory_iterator,
893 boost::single_pass_traversal_tag >
897 directory_iterator() BOOST_NOEXCEPT {} // creates the "end" iterator
899 // iterator_facade derived classes don't seem to like implementations in
900 // separate translation unit dll's, so forward to detail functions
901 explicit directory_iterator(const path& p)
902 : m_imp(new detail::dir_itr_imp)
903 { detail::directory_iterator_construct(*this, p, 0); }
905 directory_iterator(const path& p, system::error_code& ec) BOOST_NOEXCEPT
906 : m_imp(new detail::dir_itr_imp)
907 { detail::directory_iterator_construct(*this, p, &ec); }
909 ~directory_iterator() {}
911 directory_iterator& increment(system::error_code& ec) BOOST_NOEXCEPT
913 detail::directory_iterator_increment(*this, &ec);
918 friend struct detail::dir_itr_imp;
919 friend BOOST_FILESYSTEM_DECL void detail::directory_iterator_construct(directory_iterator& it,
920 const path& p, system::error_code* ec);
921 friend BOOST_FILESYSTEM_DECL void detail::directory_iterator_increment(directory_iterator& it,
922 system::error_code* ec);
924 // shared_ptr provides shallow-copy semantics required for InputIterators.
925 // m_imp.get()==0 indicates the end iterator.
926 boost::shared_ptr< detail::dir_itr_imp > m_imp;
928 friend class boost::iterator_core_access;
930 boost::iterator_facade<
933 boost::single_pass_traversal_tag >::reference dereference() const
935 BOOST_ASSERT_MSG(m_imp.get(), "attempt to dereference end iterator");
936 return m_imp->dir_entry;
939 void increment() { detail::directory_iterator_increment(*this, 0); }
941 bool equal(const directory_iterator& rhs) const
942 { return m_imp == rhs.m_imp; }
944 }; // directory_iterator
946 // enable directory_iterator C++11 range-base for statement use --------------------//
948 // begin() and end() are only used by a range-based for statement in the context of
949 // auto - thus the top-level const is stripped - so returning const is harmless and
950 // emphasizes begin() is just a pass through.
952 const directory_iterator& begin(const directory_iterator& iter) BOOST_NOEXCEPT
955 directory_iterator end(const directory_iterator&) BOOST_NOEXCEPT
956 {return directory_iterator();}
958 // enable directory_iterator BOOST_FOREACH -----------------------------------------//
961 directory_iterator& range_begin(directory_iterator& iter) BOOST_NOEXCEPT
964 directory_iterator range_begin(const directory_iterator& iter) BOOST_NOEXCEPT
967 directory_iterator range_end(const directory_iterator&) BOOST_NOEXCEPT
968 {return directory_iterator();}
969 } // namespace filesystem
971 // namespace boost template specializations
973 struct range_mutable_iterator<boost::filesystem::directory_iterator>
974 { typedef boost::filesystem::directory_iterator type; };
976 struct range_const_iterator <boost::filesystem::directory_iterator>
977 { typedef boost::filesystem::directory_iterator type; };
982 //--------------------------------------------------------------------------------------//
984 // recursive_directory_iterator helpers //
986 //--------------------------------------------------------------------------------------//
988 BOOST_SCOPED_ENUM_START(symlink_option)
991 no_recurse = none, // don't follow directory symlinks (default behavior)
992 recurse, // follow directory symlinks
993 _detail_no_push = recurse << 1 // internal use only
995 BOOST_SCOPED_ENUM_END
997 BOOST_BITMASK(BOOST_SCOPED_ENUM(symlink_option))
1001 struct recur_dir_itr_imp
1003 typedef directory_iterator element_type;
1004 std::stack< element_type, std::vector< element_type > > m_stack;
1006 BOOST_SCOPED_ENUM(symlink_option) m_options;
1008 recur_dir_itr_imp() : m_level(0), m_options(symlink_option::none) {}
1010 void increment(system::error_code* ec); // ec == 0 means throw on error
1012 bool push_directory(system::error_code& ec) BOOST_NOEXCEPT;
1018 // Implementation is inline to avoid dynamic linking difficulties with m_stack:
1019 // Microsoft warning C4251, m_stack needs to have dll-interface to be used by
1020 // clients of struct 'boost::filesystem::detail::recur_dir_itr_imp'
1023 bool recur_dir_itr_imp::push_directory(system::error_code& ec) BOOST_NOEXCEPT
1024 // Returns: true if push occurs, otherwise false. Always returns false on error.
1028 // Discover if the iterator is for a directory that needs to be recursed into,
1029 // taking symlinks and options into account.
1031 if ((m_options & symlink_option::_detail_no_push) == symlink_option::_detail_no_push)
1032 m_options &= ~symlink_option::_detail_no_push;
1036 // Logic for following predicate was contributed by Daniel Aarno to handle cyclic
1037 // symlinks correctly and efficiently, fixing ticket #5652.
1038 // if (((m_options & symlink_option::recurse) == symlink_option::recurse
1039 // || !is_symlink(m_stack.top()->symlink_status()))
1040 // && is_directory(m_stack.top()->status())) ...
1041 // The predicate code has since been rewritten to pass error_code arguments,
1042 // per ticket #5653.
1044 file_status symlink_stat;
1046 if ((m_options & symlink_option::recurse) != symlink_option::recurse)
1048 symlink_stat = m_stack.top()->symlink_status(ec);
1053 if ((m_options & symlink_option::recurse) == symlink_option::recurse
1054 || !is_symlink(symlink_stat))
1056 file_status stat = m_stack.top()->status(ec);
1057 if (ec || !is_directory(stat))
1060 directory_iterator next(m_stack.top()->path(), ec);
1061 if (!ec && next != directory_iterator())
1073 void recur_dir_itr_imp::increment(system::error_code* ec)
1074 // ec == 0 means throw on error
1076 // Invariant: On return, the top of the iterator stack is the next valid (possibly
1077 // end) iterator, regardless of whether or not an error is reported, and regardless of
1078 // whether any error is reported by exception or error code. In other words, progress
1079 // is always made so a loop on the iterator will always eventually terminate
1080 // regardless of errors.
1082 system::error_code ec_push_directory;
1084 // if various conditions are met, push a directory_iterator into the iterator stack
1085 if (push_directory(ec_push_directory))
1092 // Do the actual increment operation on the top iterator in the iterator
1093 // stack, popping the stack if necessary, until either the stack is empty or a
1094 // non-end iterator is reached.
1095 while (!m_stack.empty() && ++m_stack.top() == directory_iterator())
1101 // report errors if any
1102 if (ec_push_directory)
1105 *ec = ec_push_directory;
1108 BOOST_FILESYSTEM_THROW(filesystem_error(
1109 "filesystem::recursive_directory_iterator directory error",
1110 ec_push_directory));
1118 void recur_dir_itr_imp::pop()
1120 BOOST_ASSERT_MSG(m_level > 0,
1121 "pop() on recursive_directory_iterator with level < 1");
1128 while (!m_stack.empty() && ++m_stack.top() == directory_iterator());
1130 } // namespace detail
1132 //--------------------------------------------------------------------------------------//
1134 // recursive_directory_iterator //
1136 //--------------------------------------------------------------------------------------//
1138 class recursive_directory_iterator
1139 : public boost::iterator_facade<
1140 recursive_directory_iterator,
1142 boost::single_pass_traversal_tag >
1146 recursive_directory_iterator() BOOST_NOEXCEPT {} // creates the "end" iterator
1148 explicit recursive_directory_iterator(const path& dir_path) // throws if !exists()
1149 : m_imp(new detail::recur_dir_itr_imp)
1151 m_imp->m_options = symlink_option::none;
1152 m_imp->m_stack.push(directory_iterator(dir_path));
1153 if (m_imp->m_stack.top() == directory_iterator())
1157 recursive_directory_iterator(const path& dir_path,
1158 BOOST_SCOPED_ENUM(symlink_option) opt) // throws if !exists()
1159 : m_imp(new detail::recur_dir_itr_imp)
1161 m_imp->m_options = opt;
1162 m_imp->m_stack.push(directory_iterator(dir_path));
1163 if (m_imp->m_stack.top() == directory_iterator())
1167 recursive_directory_iterator(const path& dir_path,
1168 BOOST_SCOPED_ENUM(symlink_option) opt,
1169 system::error_code & ec) BOOST_NOEXCEPT
1170 : m_imp(new detail::recur_dir_itr_imp)
1172 m_imp->m_options = opt;
1173 m_imp->m_stack.push(directory_iterator(dir_path, ec));
1174 if (m_imp->m_stack.top() == directory_iterator())
1178 recursive_directory_iterator(const path& dir_path,
1179 system::error_code & ec) BOOST_NOEXCEPT
1180 : m_imp(new detail::recur_dir_itr_imp)
1182 m_imp->m_options = symlink_option::none;
1183 m_imp->m_stack.push(directory_iterator(dir_path, ec));
1184 if (m_imp->m_stack.top() == directory_iterator())
1188 recursive_directory_iterator& increment(system::error_code& ec) BOOST_NOEXCEPT
1190 BOOST_ASSERT_MSG(m_imp.get(),
1191 "increment() on end recursive_directory_iterator");
1192 m_imp->increment(&ec);
1193 if (m_imp->m_stack.empty())
1194 m_imp.reset(); // done, so make end iterator
1198 int depth() const BOOST_NOEXCEPT
1200 BOOST_ASSERT_MSG(m_imp.get(),
1201 "depth() on end recursive_directory_iterator");
1202 return m_imp->m_level;
1205 int level() const BOOST_NOEXCEPT { return depth(); }
1207 bool recursion_pending() const BOOST_NOEXCEPT
1209 BOOST_ASSERT_MSG(m_imp.get(),
1210 "is_no_push_requested() on end recursive_directory_iterator");
1211 return (m_imp->m_options & symlink_option::_detail_no_push)
1212 == symlink_option::_detail_no_push;
1215 bool no_push_pending() const BOOST_NOEXCEPT { return recursion_pending(); }
1217 # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
1218 bool no_push_request() const BOOST_NOEXCEPT { return no_push_pending(); }
1223 BOOST_ASSERT_MSG(m_imp.get(),
1224 "pop() on end recursive_directory_iterator");
1226 if (m_imp->m_stack.empty()) m_imp.reset(); // done, so make end iterator
1229 void disable_recursion_pending(bool value=true) BOOST_NOEXCEPT
1231 BOOST_ASSERT_MSG(m_imp.get(),
1232 "no_push() on end recursive_directory_iterator");
1234 m_imp->m_options |= symlink_option::_detail_no_push;
1236 m_imp->m_options &= ~symlink_option::_detail_no_push;
1239 void no_push(bool value=true) BOOST_NOEXCEPT { disable_recursion_pending(value); }
1241 file_status status() const
1243 BOOST_ASSERT_MSG(m_imp.get(),
1244 "status() on end recursive_directory_iterator");
1245 return m_imp->m_stack.top()->status();
1248 file_status symlink_status() const
1250 BOOST_ASSERT_MSG(m_imp.get(),
1251 "symlink_status() on end recursive_directory_iterator");
1252 return m_imp->m_stack.top()->symlink_status();
1257 // shared_ptr provides shallow-copy semantics required for InputIterators.
1258 // m_imp.get()==0 indicates the end iterator.
1259 boost::shared_ptr< detail::recur_dir_itr_imp > m_imp;
1261 friend class boost::iterator_core_access;
1263 boost::iterator_facade<
1264 recursive_directory_iterator,
1266 boost::single_pass_traversal_tag >::reference
1269 BOOST_ASSERT_MSG(m_imp.get(),
1270 "dereference of end recursive_directory_iterator");
1271 return *m_imp->m_stack.top();
1276 BOOST_ASSERT_MSG(m_imp.get(),
1277 "increment of end recursive_directory_iterator");
1278 m_imp->increment(0);
1279 if (m_imp->m_stack.empty())
1280 m_imp.reset(); // done, so make end iterator
1283 bool equal(const recursive_directory_iterator& rhs) const
1284 { return m_imp == rhs.m_imp; }
1286 }; // recursive directory iterator
1288 // enable recursive directory iterator C++11 range-base for statement use ----------//
1290 // begin() and end() are only used by a range-based for statement in the context of
1291 // auto - thus the top-level const is stripped - so returning const is harmless and
1292 // emphasizes begin() is just a pass through.
1294 const recursive_directory_iterator&
1295 begin(const recursive_directory_iterator& iter) BOOST_NOEXCEPT
1298 recursive_directory_iterator end(const recursive_directory_iterator&) BOOST_NOEXCEPT
1299 {return recursive_directory_iterator();}
1301 // enable recursive directory iterator BOOST_FOREACH -------------------------------//
1304 recursive_directory_iterator&
1305 range_begin(recursive_directory_iterator& iter) BOOST_NOEXCEPT
1308 recursive_directory_iterator
1309 range_begin(const recursive_directory_iterator& iter) BOOST_NOEXCEPT
1312 recursive_directory_iterator range_end(const recursive_directory_iterator&) BOOST_NOEXCEPT
1313 {return recursive_directory_iterator();}
1314 } // namespace filesystem
1316 // namespace boost template specializations
1318 struct range_mutable_iterator<boost::filesystem::recursive_directory_iterator>
1319 { typedef boost::filesystem::recursive_directory_iterator type; };
1321 struct range_const_iterator <boost::filesystem::recursive_directory_iterator>
1322 { typedef boost::filesystem::recursive_directory_iterator type; };
1324 namespace filesystem
1327 # if !defined(BOOST_FILESYSTEM_NO_DEPRECATED)
1328 typedef recursive_directory_iterator wrecursive_directory_iterator;
1331 // test helper -----------------------------------------------------------------------//
1333 // Not part of the documented interface since false positives are possible;
1334 // there is no law that says that an OS that has large stat.st_size
1335 // actually supports large file sizes.
1339 BOOST_FILESYSTEM_DECL bool possible_large_file_size_support();
1342 } // namespace filesystem
1343 } // namespace boost
1345 #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
1346 #endif // BOOST_FILESYSTEM3_OPERATIONS_HPP