Skip to content

Commit

Permalink
Revert "Revert "Deprecate SbDirectoryOpen and SbDirectoryClose APIs"" (
Browse files Browse the repository at this point in the history
…#3603)

Re-land #2773

Reverts #3599

b/302730696

---------

Co-authored-by: Madhura Jayaraman <[email protected]>
  • Loading branch information
kaidokert and madhurajayaraman authored Jun 22, 2024
1 parent 91b8c25 commit c9cfaa8
Show file tree
Hide file tree
Showing 24 changed files with 995 additions and 38 deletions.
2 changes: 2 additions & 0 deletions base/files/file_enumerator_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "base/files/file_enumerator.h"

#include <dirent.h>

#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/threading/thread_restrictions.h"
Expand Down
18 changes: 14 additions & 4 deletions cobalt/media/sandbox/fuzzer_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,31 @@ void FuzzerApp::CollectFiles(const std::string& path_name, double min_ratio,
double max_ratio, int initial_seed) {
file_entries_.clear();

SbDirectory directory = SbDirectoryOpen(path_name.c_str(), NULL);
if (!SbDirectoryIsValid(directory)) {
DIR* directory = opendir(path_name.c_str());
if (!directory) {
// Assuming it is a file.
AddFile(path_name, min_ratio, max_ratio, initial_seed);
return;
}

std::vector<char> entry(kSbFileMaxName);
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());

while (SbDirectoryGetNext(directory, entry.data(), entry.size())) {
std::string file_name = path_name + kSbFileSepString + entry.data();
AddFile(file_name, min_ratio, max_ratio, initial_seed);
}

SbDirectoryClose(directory);
closedir(directory);
}

void FuzzerApp::AddFile(const std::string& file_name, double min_ratio,
Expand Down
25 changes: 19 additions & 6 deletions net/cert/internal/trust_store_in_memory_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "net/cert/internal/trust_store_in_memory_starboard.h"

#include <dirent.h>

#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/path_service.h"
Expand All @@ -23,6 +25,7 @@
#include "net/cert/pem.h"
#include "net/cert/x509_certificate.h"
#include "net/cert/x509_util.h"
#include "starboard/common/string.h"
#include "starboard/configuration_constants.h"
#include "starboard/directory.h"
#include "starboard/file.h"
Expand Down Expand Up @@ -66,9 +69,9 @@ base::FilePath GetCertificateDirPath() {
}

std::unordered_set<std::string> GetCertNamesOnDisk() {
auto sb_certs_directory =
SbDirectoryOpen(GetCertificateDirPath().value().c_str(), nullptr);
if (!SbDirectoryIsValid(sb_certs_directory)) {
DIR* sb_certs_directory =
opendir(GetCertificateDirPath().value().c_str());
if (!sb_certs_directory) {
// Unit tests, for example, do not use production certificates.
#if defined(STARBOARD_BUILD_TYPE_QA) || defined(STARBOARD_BUILD_TYPE_GOLD)
SB_CHECK(false);
Expand All @@ -81,14 +84,24 @@ std::unordered_set<std::string> GetCertNamesOnDisk() {
std::unordered_set<std::string> trusted_certs_on_disk;
std::vector<char> dir_entry(kSbFileMaxName);

while (SbDirectoryGetNext(sb_certs_directory, dir_entry.data(),
dir_entry.size())) {
struct dirent dirent_buffer;
struct dirent* dirent;

while (true) {
if (dir_entry.size() < kSbFileMaxName || !sb_certs_directory || !dir_entry.data()) {
break;
}
int result = readdir_r(sb_certs_directory, &dirent_buffer, &dirent);
if (result || !dirent) {
break;
}
starboard::strlcpy(dir_entry.data(), dirent->d_name, dir_entry.size());
if (strlen(dir_entry.data()) != kCertFileNameLength) {
continue;
}
trusted_certs_on_disk.emplace(dir_entry.data());
}
SbDirectoryClose(sb_certs_directory);
closedir(sb_certs_directory);
return std::move(trusted_certs_on_disk);
}
} // namespace
Expand Down
1 change: 1 addition & 0 deletions starboard/android/shared/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ static_library("starboard_platform") {
"player_set_max_video_input_size.cc",
"player_set_max_video_input_size.h",
"player_set_playback_rate.cc",
"posix_emu/dirent.cc",
"posix_emu/errno.cc",
"posix_emu/file.cc",
"posix_emu/pthread.cc",
Expand Down
3 changes: 3 additions & 0 deletions starboard/android/shared/platform_configuration/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ config("platform_configuration") {
"-Wl,--wrap=close",
"-Wl,--wrap=open",
"-Wl,--wrap=stat",
"-Wl,--wrap=opendir",
"-Wl,--wrap=closedir",
"-Wl,--wrap=readdir_r",
]
}
}
Expand Down
163 changes: 163 additions & 0 deletions starboard/android/shared/posix_emu/dirent.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <dirent.h>

#include <android/asset_manager.h>

#include <map>

#include "starboard/android/shared/directory_internal.h"
#include "starboard/android/shared/file_internal.h"
#include "starboard/common/mutex.h"
#include "starboard/common/string.h"
#include "starboard/configuration_constants.h"

using starboard::Mutex;
using starboard::ScopedLock;
using starboard::android::shared::IsAndroidAssetPath;
using starboard::android::shared::OpenAndroidAsset;
using starboard::android::shared::OpenAndroidAssetDir;

///////////////////////////////////////////////////////////////////////////////
// Implementations below exposed externally in pure C for emulation.
///////////////////////////////////////////////////////////////////////////////

extern "C" {
DIR* __real_opendir(const char* path);

int __real_closedir(DIR* dir);

int __real_readdir_r(DIR* __restrict dir,
struct dirent* __restrict dirent_buf,
struct dirent** __restrict dirent);

// AAssetDir does not have a file descriptor so we must generate one
// https://android.googlesource.com/platform/frameworks/base/+/master/native/android/asset_manager.cpp
static int gen_fd() {
static int fd = 100;
fd++;
if (fd == 0x7FFFFFFF) {
fd = 100;
}
return fd;
}

Mutex mutex_;
static std::map<int, AAssetDir*>* asset_map = nullptr;

static int handle_db_put(AAssetDir* assetDir) {
ScopedLock scoped_lock(mutex_);
if (asset_map == nullptr) {
asset_map = new std::map<int, AAssetDir*>();
}

int fd = gen_fd();
// Go through the map and make sure there isn't duplicated index
// already.
while (asset_map->find(fd) != asset_map->end()) {
fd = gen_fd();
}
asset_map->insert({fd, assetDir});

return fd;
}

static AAssetDir* handle_db_get(int fd, bool erase) {
ScopedLock scoped_lock(mutex_);
if (fd < 0) {
return nullptr;
}
if (asset_map == nullptr) {
return nullptr;
}

auto itr = asset_map->find(fd);
if (itr == asset_map->end()) {
return nullptr;
}

AAssetDir* asset_dir = itr->second;
if (erase) {
asset_map->erase(itr);
}
return asset_dir;
}

struct _PosixEmuAndroidAssetDir {
int64_t magic;
int fd;
};

constexpr int64_t kMagicNum = -1;

DIR* __wrap_opendir(const char* path) {
if (!IsAndroidAssetPath(path)) {
return __real_opendir(path);
}

AAssetDir* asset_dir = OpenAndroidAssetDir(path);
if (asset_dir) {
int descriptor = handle_db_put(asset_dir);
struct _PosixEmuAndroidAssetDir* retdir =
reinterpret_cast<_PosixEmuAndroidAssetDir*>(
malloc(sizeof(struct _PosixEmuAndroidAssetDir)));
retdir->magic = kMagicNum;
retdir->fd = descriptor;
return reinterpret_cast<DIR*>(retdir);
}
return NULL;
}

int __wrap_closedir(DIR* dir) {
if (!dir) {
return -1;
}
struct _PosixEmuAndroidAssetDir* adir = (struct _PosixEmuAndroidAssetDir*)dir;
if (adir->magic == kMagicNum) { // This is an Asset
int descriptor = adir->fd;
AAssetDir* asset_dir = handle_db_get(descriptor, true);
if (asset_dir != nullptr) {
AAssetDir_close(asset_dir);
}
free(adir);
return 0;
}
return __real_closedir(dir);
}

int __wrap_readdir_r(DIR* __restrict dir,
struct dirent* __restrict dirent_buf,
struct dirent** __restrict dirent) {
if (!dir) {
return -1;
}

struct _PosixEmuAndroidAssetDir* adir = (struct _PosixEmuAndroidAssetDir*)dir;
if (adir->magic == kMagicNum) { // This is an Asset
int descriptor = adir->fd;
AAssetDir* asset_dir = handle_db_get(descriptor, false);
const char* file_name = AAssetDir_getNextFileName(asset_dir);
if (file_name == NULL) {
return -1;
}
*dirent = dirent_buf;
starboard::strlcpy((*dirent)->d_name, file_name, kSbFileMaxName);
return 0;
}

return __real_readdir_r(dir, dirent_buf, dirent);
}

} // extern "C"
21 changes: 12 additions & 9 deletions starboard/android/shared/posix_emu/stat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

using starboard::android::shared::IsAndroidAssetPath;
using starboard::android::shared::OpenAndroidAsset;
using starboard::android::shared::OpenAndroidAssetDir;

///////////////////////////////////////////////////////////////////////////////
// Implementations below exposed externally in pure C for emulation.
Expand Down Expand Up @@ -71,15 +72,17 @@ int __wrap_stat(const char* path, struct stat* info) {
}

// Values from SbFileGetPathInfo
SbDirectory directory = SbDirectoryOpen(path, NULL);
if (directory && directory->asset_dir) {
info->st_mode = S_IFDIR;
info->st_ctime = 0;
info->st_atime = 0;
info->st_mtime = 0;
info->st_size = 0;
SbDirectoryClose(directory);
return 0;
if (IsAndroidAssetPath(path)) {
AAssetDir* asset_dir = OpenAndroidAssetDir(path);
if (asset_dir) {
info->st_mode = S_IFDIR;
info->st_ctime = 0;
info->st_atime = 0;
info->st_mtime = 0;
info->st_size = 0;
AAssetDir_close(asset_dir);
return 0;
}
}

return -1;
Expand Down
4 changes: 4 additions & 0 deletions starboard/elf_loader/exported_symbols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "starboard/elf_loader/exported_symbols.h"

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <ifaddrs.h>
Expand Down Expand Up @@ -461,6 +462,7 @@ ExportedSymbols::ExportedSymbols() {
REGISTER_SYMBOL(bind);
REGISTER_SYMBOL(calloc);
REGISTER_SYMBOL(close);
REGISTER_SYMBOL(closedir);
REGISTER_SYMBOL(connect);
REGISTER_SYMBOL(fcntl);
REGISTER_SYMBOL(free);
Expand All @@ -480,8 +482,10 @@ ExportedSymbols::ExportedSymbols() {
REGISTER_SYMBOL(msync);
REGISTER_SYMBOL(munmap);
REGISTER_SYMBOL(open);
REGISTER_SYMBOL(opendir);
REGISTER_SYMBOL(posix_memalign);
REGISTER_SYMBOL(read);
REGISTER_SYMBOL(readdir_r);
REGISTER_SYMBOL(realloc);
REGISTER_SYMBOL(recv);
REGISTER_SYMBOL(send);
Expand Down
Loading

0 comments on commit c9cfaa8

Please sign in to comment.