-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Revert "Deprecate SbDirectoryOpen and SbDirectoryClose APIs"" (…
…#3603) Re-land #2773 Reverts #3599 b/302730696 --------- Co-authored-by: Madhura Jayaraman <[email protected]>
- Loading branch information
1 parent
91b8c25
commit c9cfaa8
Showing
24 changed files
with
995 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.