MCPcopy Create free account
hub / github.com/boostorg/filesystem / is_empty_directory

Function is_empty_directory

src/directory.cpp:1093–1166  ·  view source on GitHub ↗

Tests if the directory is empty

Source from the content-addressed store, hash-verified

1091
1092//! Tests if the directory is empty
1093bool 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;

Callers 1

is_emptyFunction · 0.85

Calls 5

emit_errorFunction · 0.85
directory_iteratorClass · 0.85
getMethod · 0.80
c_strMethod · 0.45

Tested by

no test coverage detected