Skip to content

Commit

Permalink
Clean up more directory references (youtube#3840)
Browse files Browse the repository at this point in the history
b/302730696
  • Loading branch information
madhurajayaraman authored Jul 24, 2024
1 parent d17e167 commit 81077fc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
29 changes: 23 additions & 6 deletions base/files/file_enumerator_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/threading/thread_restrictions.h"
#include "starboard/common/log.h"
#include "starboard/common/string.h"
#include "starboard/configuration_constants.h"
#include "starboard/directory.h"
Expand Down Expand Up @@ -112,10 +113,14 @@ FileEnumerator::~FileEnumerator() = default;
std::vector<FileEnumerator::FileInfo> FileEnumerator::ReadDirectory(
const FilePath& source) {
internal::AssertBlockingAllowed();
SbFileError error;
SbDirectory dir = SbDirectoryOpen(source.value().c_str(), &error);
if (!SbDirectoryIsValid(dir)) {
error_ = static_cast<File::Error>(error);

DIR* directory = opendir(source.value().c_str());
if (!(directory)) {
#if BUILDFLAG(IS_WIN)
error_ = File::Error::FILE_ERROR_FAILED;
#else
error_ = File::Error::FILE_ERROR_NOT_A_DIRECTORY;
#endif // BUILDFLAG(IS_WIN)
return std::vector<FileEnumerator::FileInfo>();
}

Expand All @@ -141,19 +146,31 @@ std::vector<FileEnumerator::FileInfo> FileEnumerator::ReadDirectory(

std::vector<char> entry(kSbFileMaxName);

while (SbDirectoryGetNext(dir, entry.data(), entry.size())) {
struct dirent dirent_buffer;
struct dirent* dirent;
while (true) {
if (entry.size() < kSbFileMaxName || !directory || !entry.data()) {
break;
}
int result = readdir_r(directory, &dirent_buffer, &dirent);
if (result || !dirent) {
break;
}
starboard::strlcpy(entry.data(), dirent->d_name, entry.size());

const char dot_dot_str[] = "..";
if (!strncmp(entry.data(), dot_dot_str, sizeof(dot_dot_str))) {
found_dot_dot = true;
}
ret.push_back(GenerateEntry(entry.data()));

}

if ((INCLUDE_DOT_DOT & file_type_) && !found_dot_dot) {
ret.push_back(GenerateEntry(".."));
}

SbDirectoryClose(dir);
closedir(directory);
return ret;
}

Expand Down
9 changes: 6 additions & 3 deletions starboard/shared/win32/posix_emu/dirent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ int readdir_r(DIR* __restrict dir,
struct dirent* __restrict dirent_buf,
struct dirent** __restrict dirent) {
if (!dir || !dirent_buf || !dirent) {
*dirent = NULL;
if (dirent) {
*dirent = nullptr;
}
return EBADF;
}

Expand All @@ -218,16 +220,17 @@ int readdir_r(DIR* __restrict dir,
}

if (next_directory_entries.empty()) {
*dirent = NULL;
*dirent = nullptr;
return ENOENT;
}

if (starboard::strlcpy(dirent_buf->d_name,
next_directory_entries.rbegin()->c_str(),
kSbFileMaxName) >= kSbFileMaxName) {
*dirent = NULL;
*dirent = nullptr;
return ENOENT;
}

*dirent = dirent_buf;
next_directory_entries.pop_back();
handle_db_replace(dir->fd, next_directory_entries);
Expand Down
3 changes: 0 additions & 3 deletions starboard/win/win32/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@

# TODO: b/349109647 Disable flaky test.
'SbPlayerGetMediaTimeTests/*',

# PosixDirectory tests are crashing flakily on windows.
'PosixDirectory*',
],
'player_filter_tests': [
# These tests fail on our VMs for win-win32 builds due to missing
Expand Down

0 comments on commit 81077fc

Please sign in to comment.