Skip to content

Commit

Permalink
Fix Android file and stat wrapper.
Browse files Browse the repository at this point in the history
Change-Id: I6f4e922c73d8a2fc32c9da0ab8e97a780cf635be
  • Loading branch information
yjzhang111 committed Aug 1, 2024
1 parent 319f696 commit 89e0160
Show file tree
Hide file tree
Showing 62 changed files with 210 additions and 125 deletions.
45 changes: 44 additions & 1 deletion starboard/android/shared/asset_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "starboard/android/shared/asset_manager.h"

#include <android/asset_manager.h>
#include <errno.h>
#include <fcntl.h>
#include <ftw.h>
#include <linux/limits.h>
Expand All @@ -33,6 +34,44 @@ namespace starboard {
namespace android {
namespace shared {

namespace {

// Returns the fallback for the given asset path, or an empty string if none.
// NOTE: While Cobalt now provides a mechanism for loading system fonts through
// SbSystemGetPath(), using the fallback logic within SbFileOpen() is
// still preferred for Android's fonts. The reason for this is that the
// Android OS actually allows fonts to be loaded from two locations: one
// that it provides; and one that the devices running its OS, which it
// calls vendors, can provide. Rather than including the full Android font
// package, vendors have the option of using a smaller Android font
// package and supplementing it with their own fonts.
//
// If Android were to use SbSystemGetPath() for its fonts, vendors would
// have no way of providing those supplemental fonts to Cobalt, which
// could result in a limited selection of fonts being available. By
// treating Android's fonts as Cobalt's fonts, Cobalt can still offer a
// straightforward mechanism for including vendor fonts via
// SbSystemGetPath().
std::string FallbackPath(const std::string& path) {
// We don't package most font files in Cobalt content and fallback to the
// system font file of the same name.
const std::string fonts_xml("fonts.xml");
const std::string system_fonts_dir("/system/fonts/");
const std::string cobalt_fonts_dir("/cobalt/assets/fonts/");

// Fonts fallback to the system fonts.
if (path.compare(0, cobalt_fonts_dir.length(), cobalt_fonts_dir) == 0) {
std::string file_name = path.substr(cobalt_fonts_dir.length());
// fonts.xml doesn't fallback.
if (file_name != fonts_xml) {
return system_fonts_dir + file_name;
}
}
return std::string();
}

} // namespace

// static
SB_ONCE_INITIALIZE_FUNCTION(AssetManager, AssetManager::GetInstance);

Expand Down Expand Up @@ -60,13 +99,17 @@ std::string AssetManager::TempFilepath(uint64_t internal_fd) const {
return tmp_root_ + "/" + std::to_string(internal_fd);
}

int AssetManager::Open(const char* path) {
int AssetManager::Open(const char* path, int oflag) {
if (!path) {
return -1;
}

AAsset* asset = OpenAndroidAsset(path);
if (!asset) {
std::string fallback_path = FallbackPath(path);
if (!fallback_path.empty()) {
return open(fallback_path.c_str(), oflag);
}
SB_LOG(WARNING) << "Asset path not found within package: " << path;
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion starboard/android/shared/asset_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace shared {
class AssetManager {
public:
static AssetManager* GetInstance();
int Open(const char* path);
int Open(const char* path, int oflag);
int Close(int fd);
bool IsAssetFd(int fd) const;

Expand Down
4 changes: 2 additions & 2 deletions starboard/android/shared/file_can_open.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if SB_API_VERSION < 16
#if SB_API_VERSION < 17

#include "starboard/file.h"

Expand Down Expand Up @@ -45,4 +45,4 @@ bool SbFileCanOpen(const char* path, int flags) {
return result;
}

#endif // SB_API_VERSION < 16
#endif // SB_API_VERSION < 17
4 changes: 2 additions & 2 deletions starboard/android/shared/file_close.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if SB_API_VERSION < 16
#if SB_API_VERSION < 17

#include "starboard/file.h"

Expand All @@ -31,4 +31,4 @@ bool SbFileClose(SbFile file) {
return ::starboard::shared::posix::impl::FileClose(file);
}

#endif // SB_API_VERSION < 16
#endif // SB_API_VERSION < 17
4 changes: 2 additions & 2 deletions starboard/android/shared/file_delete.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if SB_API_VERSION < 16
#if SB_API_VERSION < 17

#include "starboard/file.h"

Expand All @@ -23,4 +23,4 @@ bool SbFileDelete(const char* path) {
return ::starboard::shared::posix::impl::FileDelete(path);
}

#endif // SB_API_VERSION < 16
#endif // SB_API_VERSION < 17
4 changes: 2 additions & 2 deletions starboard/android/shared/file_flush.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if SB_API_VERSION < 16
#if SB_API_VERSION < 17

#include "starboard/file.h"

Expand All @@ -23,4 +23,4 @@ bool SbFileFlush(SbFile file) {
return ::starboard::shared::posix::impl::FileFlush(file);
}

#endif // SB_API_VERSION < 16
#endif // SB_API_VERSION < 17
4 changes: 2 additions & 2 deletions starboard/android/shared/file_get_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if SB_API_VERSION < 16
#if SB_API_VERSION < 17

#include "starboard/file.h"

Expand All @@ -35,4 +35,4 @@ bool SbFileGetInfo(SbFile file, SbFileInfo* out_info) {
return ::starboard::shared::posix::impl::FileGetInfo(file, out_info);
}

#endif // SB_API_VERSION < 16
#endif // SB_API_VERSION < 17
4 changes: 2 additions & 2 deletions starboard/android/shared/file_open.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if SB_API_VERSION < 16
#if SB_API_VERSION < 17

#include "starboard/file.h"

Expand Down Expand Up @@ -107,4 +107,4 @@ SbFile SbFileOpen(const char* path,
return kSbFileInvalid;
}

#endif // SB_API_VERSION < 16
#endif // SB_API_VERSION < 17
4 changes: 2 additions & 2 deletions starboard/android/shared/file_read.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if SB_API_VERSION < 16
#if SB_API_VERSION < 17

#include "starboard/file.h"

Expand All @@ -33,4 +33,4 @@ int SbFileRead(SbFile file, char* data, int size) {
}
}

#endif // SB_API_VERSION < 16
#endif // SB_API_VERSION < 17
4 changes: 2 additions & 2 deletions starboard/android/shared/file_seek.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if SB_API_VERSION < 16
#if SB_API_VERSION < 17

#include "starboard/file.h"

Expand All @@ -29,4 +29,4 @@ int64_t SbFileSeek(SbFile file, SbFileWhence whence, int64_t offset) {
}
}

#endif // SB_API_VERSION < 16
#endif // SB_API_VERSION < 17
4 changes: 2 additions & 2 deletions starboard/android/shared/file_truncate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if SB_API_VERSION < 16
#if SB_API_VERSION < 17

#include "starboard/file.h"

Expand All @@ -23,4 +23,4 @@ bool SbFileTruncate(SbFile file, int64_t length) {
return ::starboard::shared::posix::impl::FileTruncate(file, length);
}

#endif // SB_API_VERSION < 16
#endif // SB_API_VERSION < 17
4 changes: 2 additions & 2 deletions starboard/android/shared/file_write.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if SB_API_VERSION < 16
#if SB_API_VERSION < 17

#include "starboard/file.h"

Expand All @@ -23,4 +23,4 @@ int SbFileWrite(SbFile file, const char* data, int size) {
return ::starboard::shared::posix::impl::FileWrite(file, data, size);
}

#endif // SB_API_VERSION < 16
#endif // SB_API_VERSION < 17
2 changes: 1 addition & 1 deletion starboard/android/shared/posix_emu/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int __wrap_open(const char* path, int oflag, ...) {
return __real_open(path, oflag);
}
}
return AssetManager::GetInstance()->Open(path);
return AssetManager::GetInstance()->Open(path, oflag);
}

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

#include "starboard/android/shared/directory_internal.h"
#include "starboard/android/shared/file_internal.h"
#include "starboard/common/log.h"
#include "starboard/directory.h"

using starboard::android::shared::IsAndroidAssetPath;
Expand Down Expand Up @@ -51,22 +52,21 @@ int __wrap_stat(const char* path, struct stat* info) {
}

int file = open(path, O_RDONLY, S_IRUSR | S_IWUSR);
struct stat out_info;
if (file) {
bool result = !fstat(file, &out_info);
if (file >= 0) {
int result = fstat(file, info);
close(file);
return 0;
return result;
}

// Values from SbFileGetPathInfo
if (IsAndroidAssetPath(path)) {
AAssetDir* asset_dir = OpenAndroidAssetDir(path);
if (asset_dir) {
out_info.st_mode = S_IFDIR;
out_info.st_ctime = 0;
out_info.st_atime = 0;
out_info.st_mtime = 0;
out_info.st_size = 0;
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;
}
Expand Down
1 change: 0 additions & 1 deletion starboard/common/file_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ int file_close(FilePtr file) {
FilePtr file_open(const char* path, int mode) {
FilePtr file = new FileStruct();
file->fd = open(path, mode, S_IRUSR | S_IWUSR);
SB_LOG(INFO) << "yyHERE IN FILE_OPEN, VALUE OF OPEN IS: " << file->fd << "\n";
return file;
}

Expand Down
12 changes: 10 additions & 2 deletions starboard/elf_loader/exported_symbols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,21 +178,29 @@ ExportedSymbols::ExportedSymbols() {
REGISTER_SYMBOL(SbEventCancel);
REGISTER_SYMBOL(SbEventSchedule);
REGISTER_SYMBOL(SbFileAtomicReplace);
#if SB_API_VERSION < 16
#if SB_API_VERSION < 17
REGISTER_SYMBOL(SbFileCanOpen);
REGISTER_SYMBOL(SbFileClose);
REGISTER_SYMBOL(SbFileDelete);
#endif // SB_API_VERSION < 17
#if SB_API_VERSION < 16
REGISTER_SYMBOL(SbFileExists);
#endif // SB_API_VERSION < 16
#if SB_API_VERSION < 17
REGISTER_SYMBOL(SbFileFlush);
REGISTER_SYMBOL(SbFileGetInfo);
#endif // SB_API_VERSION < 17
#if SB_API_VERSION < 16
REGISTER_SYMBOL(SbFileGetPathInfo);
#endif // SB_API_VERSION < 16
#if SB_API_VERSION < 17
REGISTER_SYMBOL(SbFileModeStringToFlags);
REGISTER_SYMBOL(SbFileOpen);
REGISTER_SYMBOL(SbFileRead);
REGISTER_SYMBOL(SbFileSeek);
REGISTER_SYMBOL(SbFileTruncate);
REGISTER_SYMBOL(SbFileWrite);
#endif // SB_API_VERSION < 16
#endif // SB_API_VERSION < 17
REGISTER_SYMBOL(SbGetEglInterface);
REGISTER_SYMBOL(SbGetGlesInterface);
#if SB_API_VERSION < 16
Expand Down
16 changes: 9 additions & 7 deletions starboard/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ static inline bool SbFileIsValid(SbFile file) {
return file != kSbFileInvalid;
}

#if SB_API_VERSION < 16
#if SB_API_VERSION < 17

// DEPRECATED with SB_API_VERSION 16
//
Expand Down Expand Up @@ -160,7 +160,7 @@ SB_EXPORT SbFile SbFileOpen(const char* path,
// |file|: The absolute path of the file to be closed.
SB_EXPORT bool SbFileClose(SbFile file);

#endif // SB_API_VERSION < 16
#endif // SB_API_VERSION < 17

// Replaces the content of the file at |path| with |data|. Returns whether the
// contents of the file were replaced. The replacement of the content is an
Expand All @@ -173,7 +173,7 @@ SB_EXPORT bool SbFileAtomicReplace(const char* path,
const char* data,
int64_t data_size);

#if SB_API_VERSION < 16
#if SB_API_VERSION < 17

// DEPRECATED with SB_API_VERSION 16
//
Expand Down Expand Up @@ -270,13 +270,17 @@ SB_EXPORT bool SbFileGetPathInfo(const char* path, SbFileInfo* out_info);
// |path|: The absolute path of the file, symlink, or directory to be deleted.
SB_EXPORT bool SbFileDelete(const char* path);

#endif // SB_API_VERSION < 17

#if SB_API_VERSION < 16
// Indicates whether a file or directory exists at |path|.
//
// |path|: The absolute path of the file or directory being checked.
SB_EXPORT bool SbFileExists(const char* path);

#endif // SB_API_VERSION < 16

#if SB_API_VERSION < 17

// DEPRECATED with SB_API_VERSION 16
//
// Indicates whether SbFileOpen() with the given |flags| is allowed for |path|.
Expand All @@ -285,8 +289,6 @@ SB_EXPORT bool SbFileExists(const char* path);
// |flags|: The flags that are being evaluated for the given |path|.
SB_EXPORT bool SbFileCanOpen(const char* path, int flags);

#if SB_API_VERSION < 16

// DEPRECATED with SB_API_VERSION 16
//
// Converts an ISO |fopen()| mode string into flags that can be equivalently
Expand Down Expand Up @@ -354,7 +356,7 @@ static inline int SbFileWriteAll(SbFile file, const char* data, int size) {
return bytes_written ? bytes_written : rv;
}

#endif // SB_API_VERSION < 16
#endif // SB_API_VERSION < 17

#ifdef __cplusplus
} // extern "C"
Expand Down
4 changes: 2 additions & 2 deletions starboard/nplb/file_can_open_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#if SB_API_VERSION < 16
#if SB_API_VERSION < 17

#include <string>

Expand Down Expand Up @@ -73,4 +73,4 @@ TEST(SbFileCanOpenTest, ExistingStaticContentFileSucceeds) {
} // namespace nplb
} // namespace starboard

#endif // SB_API_VERSION < 16
#endif // SB_API_VERSION < 17
Loading

0 comments on commit 89e0160

Please sign in to comment.