recurse_dir_iter_5403.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Boost Filesystem recurse_dir_iter_test.cpp ----------------------------------------//
  2. // Copyright Beman Dawes 2014.
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // See http://www.boost.org/LICENSE_1_0.txt
  5. // Library home page: http://www.boost.org/libs/filesystem
  6. #include <boost/config/warning_disable.hpp>
  7. // See deprecated_test for tests of deprecated features
  8. #ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  9. # define BOOST_FILESYSTEM_NO_DEPRECATED
  10. #endif
  11. #ifndef BOOST_SYSTEM_NO_DEPRECATED
  12. # define BOOST_SYSTEM_NO_DEPRECATED
  13. #endif
  14. #include <boost/filesystem/operations.hpp>
  15. #include <boost/config.hpp>
  16. # if defined( BOOST_NO_STD_WSTRING )
  17. # error Configuration not supported: Boost.Filesystem V3 and later requires std::wstring support
  18. # endif
  19. #include <boost/cerrno.hpp>
  20. #include <boost/detail/lightweight_test.hpp>
  21. #include <boost/detail/lightweight_main.hpp>
  22. namespace fs = boost::filesystem;
  23. using boost::system::error_code;
  24. using boost::system::system_category;
  25. using boost::system::system_error;
  26. #include <iostream>
  27. using std::cout;
  28. using std::endl;
  29. #ifdef BOOST_WINDOWS_API
  30. # include <windows.h>
  31. #endif
  32. namespace
  33. {
  34. typedef int errno_t;
  35. std::string platform(BOOST_PLATFORM);
  36. bool report_throws = false;
  37. bool cleanup = true;
  38. bool skip_long_windows_tests = false;
  39. unsigned short language_id; // 0 except for Windows
  40. } // unnamed namespace
  41. //------------------------------------------------------------------------------------//
  42. // //
  43. // main //
  44. // //
  45. //------------------------------------------------------------------------------------//
  46. int cpp_main(int argc, char* argv[])
  47. {
  48. // document state of critical macros
  49. #ifdef BOOST_POSIX_API
  50. cout << "BOOST_POSIX_API is defined\n";
  51. #endif
  52. #ifdef BOOST_WINDOWS_API
  53. cout << "BOOST_WINDOWS_API is defined\n";
  54. #endif
  55. for (; argc > 1; --argc, ++argv)
  56. {
  57. //if (*argv[1]=='-' && *(argv[1]+1)=='t')
  58. // report_throws = true;
  59. //else if (*argv[1]=='-' && *(argv[1]+1)=='x')
  60. // cleanup = false;
  61. //else if (*argv[1]=='-' && *(argv[1]+1)=='w')
  62. // skip_long_windows_tests = true;
  63. }
  64. // The choice of platform to test is made at runtime rather than compile-time
  65. // so that compile errors for all platforms will be detected even though
  66. // only the current platform is runtime tested.
  67. # if defined(BOOST_POSIX_API)
  68. platform = "POSIX";
  69. # elif defined(BOOST_WINDOWS_API)
  70. platform = "Windows";
  71. # if !defined(__MINGW32__) && !defined(__CYGWIN__)
  72. language_id = ::GetUserDefaultUILanguage();
  73. # else
  74. language_id = 0x0409; // Assume US English
  75. # endif
  76. # else
  77. # error neither BOOST_POSIX_API nor BOOST_WINDOWS_API is defined. See boost/system/api_config.hpp
  78. # endif
  79. cout << "API is " << platform << endl;
  80. cout << "initial_path() is " << fs::initial_path() << endl;
  81. fs::path ip = fs::initial_path();
  82. for (fs::path::const_iterator it = ip.begin(); it != ip.end(); ++it)
  83. {
  84. if (it != ip.begin())
  85. cout << ", ";
  86. cout << *it;
  87. }
  88. cout << endl;
  89. // From the root, walk the directory tree looking for a permissions error
  90. fs::recursive_directory_iterator it("/");
  91. fs::recursive_directory_iterator end_it;
  92. // The increment function has an invarient that it always makes progress,
  93. // so even if an error occurs this loop will eventually terminate.
  94. while (it != end_it)
  95. {
  96. error_code ec;
  97. fs::path init_path = it->path();
  98. it.increment(ec);
  99. if (ec)
  100. {
  101. cout << "initial path: " << init_path << endl;
  102. cout << "error_code: " << ec.value() << " with msg: " << ec.message() << endl;
  103. if (it != end_it)
  104. cout << "post-increment path: " << it->path() << endl;
  105. }
  106. }
  107. cout << "returning from main()" << endl;
  108. return ::boost::report_errors();
  109. } // main