diff --git a/examples/client_test.cpp b/examples/client_test.cpp index 7aa9932b4..fc77c9e85 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -682,13 +682,10 @@ std::vector list_dir(std::string path return ret; } - struct dirent de; - dirent* dummy; - while (readdir_r(handle, &de, &dummy) == 0) + struct dirent* de; + while ((de = readdir(handle))) { - if (dummy == nullptr) break; - - lt::string_view p(de.d_name); + lt::string_view p(de->d_name); if (filter_fun(p)) ret.push_back(p.to_string()); } diff --git a/include/libtorrent/file.hpp b/include/libtorrent/file.hpp index d0714259a..e171b0f3a 100644 --- a/include/libtorrent/file.hpp +++ b/include/libtorrent/file.hpp @@ -109,11 +109,8 @@ namespace libtorrent { WIN32_FIND_DATAW m_fd; #else DIR* m_handle; - // the dirent struct contains a zero-sized - // array at the end, it will end up referring - // to the m_name field - struct dirent m_dirent; - char m_name[TORRENT_MAX_PATH + 1]; // +1 to make room for terminating 0 + ino_t m_inode; + std::string m_name; #endif bool m_done; }; diff --git a/src/file.cpp b/src/file.cpp index 5bd1ebae4..e5d8b32a8 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -363,9 +363,6 @@ static_assert(!(open_mode::sparse & open_mode::attribute_mask), "internal flags } #else - std::memset(&m_dirent, 0, sizeof(dirent)); - m_name[0] = 0; - m_handle = ::opendir(f.c_str()); if (m_handle == nullptr) { @@ -390,11 +387,7 @@ static_assert(!(open_mode::sparse & open_mode::attribute_mask), "internal flags std::uint64_t directory::inode() const { -#ifdef TORRENT_WINDOWS return m_inode; -#else - return m_dirent.d_ino; -#endif } std::string directory::file() const @@ -402,7 +395,7 @@ static_assert(!(open_mode::sparse & open_mode::attribute_mask), "internal flags #ifdef TORRENT_WINDOWS return convert_from_native_path(m_fd.cFileName); #else - return convert_from_native(m_dirent.d_name); + return convert_from_native(m_name); #endif } @@ -419,13 +412,18 @@ static_assert(!(open_mode::sparse & open_mode::attribute_mask), "internal flags } ++m_inode; #else - dirent* dummy; - if (readdir_r(m_handle, &m_dirent, &dummy) != 0) + struct dirent* de; + errno = 0; + if ((de = ::readdir(m_handle))) { - ec.assign(errno, system_category()); + m_inode = de->d_ino; + m_name = de->d_name; + } + else + { + if (errno) ec.assign(errno, system_category()); m_done = true; } - if (dummy == nullptr) m_done = true; #endif }