Tests if the directory is empty
| 1091 | |
| 1092 | //! Tests if the directory is empty |
| 1093 | bool is_empty_directory(boost::scope::unique_fd&& fd, path const& p, error_code* ec) |
| 1094 | { |
| 1095 | #if !defined(BOOST_FILESYSTEM_USE_READDIR_R) |
| 1096 | // Use a more optimal implementation without the overhead of constructing the iterator state |
| 1097 | |
| 1098 | struct closedir_deleter |
| 1099 | { |
| 1100 | using result_type = void; |
| 1101 | result_type operator() (DIR* dir) const noexcept |
| 1102 | { |
| 1103 | ::closedir(dir); |
| 1104 | } |
| 1105 | }; |
| 1106 | |
| 1107 | int err; |
| 1108 | |
| 1109 | #if defined(BOOST_FILESYSTEM_HAS_FDOPENDIR_NOFOLLOW) |
| 1110 | std::unique_ptr< DIR, closedir_deleter > dir(::fdopendir(fd.get())); |
| 1111 | if (BOOST_UNLIKELY(!dir)) |
| 1112 | { |
| 1113 | err = errno; |
| 1114 | fail: |
| 1115 | emit_error(err, p, ec, "boost::filesystem::is_empty"); |
| 1116 | return false; |
| 1117 | } |
| 1118 | |
| 1119 | // At this point fd will be closed by closedir |
| 1120 | fd.release(); |
| 1121 | #else // defined(BOOST_FILESYSTEM_HAS_FDOPENDIR_NOFOLLOW) |
| 1122 | std::unique_ptr< DIR, closedir_deleter > dir(::opendir(p.c_str())); |
| 1123 | if (BOOST_UNLIKELY(!dir)) |
| 1124 | { |
| 1125 | err = errno; |
| 1126 | fail: |
| 1127 | emit_error(err, p, ec, "boost::filesystem::is_empty"); |
| 1128 | return false; |
| 1129 | } |
| 1130 | #endif // defined(BOOST_FILESYSTEM_HAS_FDOPENDIR_NOFOLLOW) |
| 1131 | |
| 1132 | while (true) |
| 1133 | { |
| 1134 | errno = 0; |
| 1135 | struct dirent* const ent = ::readdir(dir.get()); |
| 1136 | if (!ent) |
| 1137 | { |
| 1138 | err = errno; |
| 1139 | if (err != 0) |
| 1140 | goto fail; |
| 1141 | |
| 1142 | return true; |
| 1143 | } |
| 1144 | |
| 1145 | // Skip dot and dot-dot entries |
| 1146 | if (!(ent->d_name[0] == path::dot |
| 1147 | && (ent->d_name[1] == static_cast< path::string_type::value_type >('\0') || |
| 1148 | (ent->d_name[1] == path::dot && ent->d_name[2] == static_cast< path::string_type::value_type >('\0'))))) |
| 1149 | { |
| 1150 | return false; |
no test coverage detected