diff --git a/base/files/file_util_starboard.cc b/base/files/file_util_starboard.cc index cdc2c8f9b00d..7b66df154a87 100644 --- a/base/files/file_util_starboard.cc +++ b/base/files/file_util_starboard.cc @@ -34,8 +34,8 @@ #include "base/threading/thread_restrictions.h" #include "base/time/time.h" #include "starboard/configuration_constants.h" +#include "starboard/common/file.h" #include "starboard/directory.h" -#include "starboard/file.h" #include "base/strings/strcat.h" #include "starboard/system.h" @@ -244,12 +244,12 @@ bool PathExists(const FilePath &path) { bool PathIsReadable(const FilePath &path) { internal::AssertBlockingAllowed(); - return SbFileCanOpen(path.value().c_str(), kSbFileOpenAlways | kSbFileRead); + return starboard::FileCanOpen(path.value().c_str(), O_CREAT | O_RDONLY); } bool PathIsWritable(const FilePath &path) { internal::AssertBlockingAllowed(); - return SbFileCanOpen(path.value().c_str(), kSbFileOpenAlways | kSbFileWrite); + return starboard::FileCanOpen(path.value().c_str(), O_CREAT | O_WRONLY); } bool DirectoryExists(const FilePath& path) { diff --git a/cobalt/media/sandbox/format_guesstimator.cc b/cobalt/media/sandbox/format_guesstimator.cc index 843bbd915043..e34c7643b127 100644 --- a/cobalt/media/sandbox/format_guesstimator.cc +++ b/cobalt/media/sandbox/format_guesstimator.cc @@ -37,6 +37,7 @@ #include "media/filters/chunk_demuxer.h" #include "net/base/filename_util.h" #include "net/base/url_util.h" +#include "starboard/common/file.h" #include "starboard/memory.h" #include "starboard/types.h" #include "ui/gfx/geometry/size.h" @@ -81,7 +82,7 @@ base::FilePath ResolvePath(const std::string& path) { base::PathService::Get(base::DIR_TEST_DATA, &content_path); result = content_path.Append(result); } - if (SbFileCanOpen(result.value().c_str(), kSbFileOpenOnly | kSbFileRead)) { + if (starboard::FileCanOpen(result.value().c_str(), O_RDONLY)) { return result; } LOG(WARNING) << "Failed to resolve path \"" << path << "\" as \"" @@ -141,7 +142,7 @@ FormatGuesstimator::FormatGuesstimator(const std::string& path_or_url, return; } base::FilePath path = ResolvePath(path_or_url); - if (path.empty() || !SbFileCanOpen(path.value().c_str(), kSbFileRead)) { + if (path.empty() || !starboard::FileCanOpen(path.value().c_str(), O_RDONLY)) { return; } InitializeAsAdaptive(path, media_module); diff --git a/cobalt/renderer/rasterizer/skia/skia/src/ports/SkOSFile_cobalt.cc b/cobalt/renderer/rasterizer/skia/skia/src/ports/SkOSFile_cobalt.cc index bda27ca7d19e..a8054f90e262 100644 --- a/cobalt/renderer/rasterizer/skia/skia/src/ports/SkOSFile_cobalt.cc +++ b/cobalt/renderer/rasterizer/skia/skia/src/ports/SkOSFile_cobalt.cc @@ -20,6 +20,7 @@ #include "base/optional.h" #include "base/path_service.h" #include "starboard/common/file.h" +#include "starboard/common/file_wrapper.h" // Implement functionality declared in SkOSFile.h via primitives provided // by Chromium. In doing this, we need only ensure that support for Chromium @@ -27,13 +28,13 @@ namespace { -starboard::FilePtr ToFilePtr(FILE* sk_file) { +FilePtr ToFilePtr(FILE* sk_file) { // PlatformFile is a pointer type in Starboard, so we cannot use static_cast // from intptr_t. - return reinterpret_cast(sk_file); + return reinterpret_cast(sk_file); } -FILE* ToFILE(starboard::FilePtr starboard_file) { +FILE* ToFILE(FilePtr starboard_file) { return reinterpret_cast(starboard_file); } @@ -53,11 +54,8 @@ int ToFileFlags(SkFILE_Flags sk_flags) { } // namespace FILE* sk_fopen(const char path[], SkFILE_Flags sk_flags) { - starboard::FilePtr file; - file->fd = open(path, ToFileFlags(sk_flags), S_IRUSR | S_IWUSR); - // TODO: temporarily replace with kSbFileInvalid, will be deprecated with - // SBFile. - if (!file || !starboard::IsValid(file->fd)) { + FilePtr file = file_open(path, ToFileFlags(sk_flags)); + if (!file || file->fd < 0) { return nullptr; } @@ -66,12 +64,12 @@ FILE* sk_fopen(const char path[], SkFILE_Flags sk_flags) { void sk_fclose(FILE* sk_file) { SkASSERT(sk_file); - close(ToFilePtr(sk_file)->fd); + int ret = file_close(ToFilePtr(sk_file)); } size_t sk_fgetsize(FILE* sk_file) { SkASSERT(sk_file); - starboard::FilePtr file = ToFilePtr(sk_file); + FilePtr file = ToFilePtr(sk_file); // Save current position so we can restore it. int64_t current_position = lseek(file->fd, 0, SEEK_CUR); @@ -92,7 +90,7 @@ size_t sk_fgetsize(FILE* sk_file) { size_t sk_fwrite(const void* buffer, size_t byteCount, FILE* sk_file) { SkASSERT(sk_file); - starboard::FilePtr file = ToFilePtr(sk_file); + FilePtr file = ToFilePtr(sk_file); int result = write(file->fd, reinterpret_cast(buffer), byteCount); base::RecordFileWriteStat(result); @@ -101,20 +99,20 @@ size_t sk_fwrite(const void* buffer, size_t byteCount, FILE* sk_file) { void sk_fflush(FILE* sk_file) { SkASSERT(sk_file); - starboard::FilePtr file = ToFilePtr(sk_file); + FilePtr file = ToFilePtr(sk_file); fsync(file->fd); } bool sk_fseek(FILE* sk_file, size_t position) { SkASSERT(sk_file); - starboard::FilePtr file = ToFilePtr(sk_file); + FilePtr file = ToFilePtr(sk_file); int64_t new_position = lseek(file->fd, position, SEEK_SET); return new_position == position; } size_t sk_ftell(FILE* sk_file) { SkASSERT(sk_file); - starboard::FilePtr file = ToFilePtr(sk_file); + FilePtr file = ToFilePtr(sk_file); return lseek(file->fd, 0, SEEK_CUR); } @@ -159,7 +157,7 @@ bool sk_mkdir(const char* path) { void sk_fsync(FILE* f) { SkASSERT(f); - starboard::FilePtr file = ToFilePtr(f); + FilePtr file = ToFilePtr(f); // Technically, flush doesn't have to call sync... but this is the best // effort we can make. fsync(file->fd); @@ -167,7 +165,7 @@ void sk_fsync(FILE* f) { size_t sk_qread(FILE* file, void* buffer, size_t count, size_t offset) { SkASSERT(file); - starboard::FilePtr starboard_file = ToFilePtr(file); + FilePtr starboard_file = ToFilePtr(file); int original_position = lseek(starboard_file->fd, 0, SEEK_CUR); if (original_position < 0) { @@ -191,7 +189,7 @@ size_t sk_qread(FILE* file, void* buffer, size_t count, size_t offset) { size_t sk_fread(void* buffer, size_t byteCount, FILE* file) { SkASSERT(file); - starboard::FilePtr starboard_file = ToFilePtr(file); + FilePtr starboard_file = ToFilePtr(file); return starboard::ReadAll(starboard_file->fd, reinterpret_cast(buffer), byteCount); } diff --git a/components/update_client/action_runner.cc b/components/update_client/action_runner.cc index b456f138e6ce..02dc1a53c1a7 100644 --- a/components/update_client/action_runner.cc +++ b/components/update_client/action_runner.cc @@ -4,6 +4,8 @@ #include "components/update_client/action_runner.h" +#include + #include #include #include @@ -42,11 +44,11 @@ void CleanupDirectory(base::FilePath& dir) { if (info.IsDirectory()) { directories.push(path.value()); } else { - SbFileDelete(path.value().c_str()); + unlink(path.value().c_str()); } } while (!directories.empty()) { - SbFileDelete(directories.top().c_str()); + rmdir(directories.top().c_str()); directories.pop(); } } diff --git a/components/update_client/url_fetcher_downloader.cc b/components/update_client/url_fetcher_downloader.cc index b4949468196f..ad7f64481d10 100644 --- a/components/update_client/url_fetcher_downloader.cc +++ b/components/update_client/url_fetcher_downloader.cc @@ -41,11 +41,11 @@ void CleanupDirectory(base::FilePath& dir) { if (info.IsDirectory()) { directories.push(path.value()); } else { - SbFileDelete(path.value().c_str()); + unlink(path.value().c_str()); } } while (!directories.empty()) { - SbFileDelete(directories.top().c_str()); + rmdir(directories.top().c_str()); directories.pop(); } } diff --git a/starboard/android/shared/file_can_open.cc b/starboard/android/shared/file_can_open.cc index 5aeb5fd1ed00..e05443356ea6 100644 --- a/starboard/android/shared/file_can_open.cc +++ b/starboard/android/shared/file_can_open.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/file.h" #include @@ -41,3 +43,5 @@ bool SbFileCanOpen(const char* path, int flags) { return result; } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/android/shared/file_close.cc b/starboard/android/shared/file_close.cc index 192aedec0f4f..7691e8869b84 100644 --- a/starboard/android/shared/file_close.cc +++ b/starboard/android/shared/file_close.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/file.h" #include @@ -28,3 +30,5 @@ bool SbFileClose(SbFile file) { return ::starboard::shared::posix::impl::FileClose(file); } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/android/shared/file_delete.cc b/starboard/android/shared/file_delete.cc index 9a0ab8b2c8ed..04b70fd2a4bf 100644 --- a/starboard/android/shared/file_delete.cc +++ b/starboard/android/shared/file_delete.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/file.h" #include "starboard/android/shared/file_internal.h" @@ -20,3 +22,5 @@ bool SbFileDelete(const char* path) { return ::starboard::shared::posix::impl::FileDelete(path); } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/android/shared/file_exists.cc b/starboard/android/shared/file_exists.cc index 32dbd3afc5a8..810315828180 100644 --- a/starboard/android/shared/file_exists.cc +++ b/starboard/android/shared/file_exists.cc @@ -13,9 +13,11 @@ // limitations under the License. #if SB_API_VERSION < 16 + #include "starboard/file.h" bool SbFileExists(const char* path) { return SbFileCanOpen(path, kSbFileRead); } -#endif + +#endif // SB_API_VERSION < 16 diff --git a/starboard/android/shared/file_flush.cc b/starboard/android/shared/file_flush.cc index 25634ca4402b..eafc12e9ce0b 100644 --- a/starboard/android/shared/file_flush.cc +++ b/starboard/android/shared/file_flush.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/file.h" #include "starboard/android/shared/file_internal.h" @@ -20,3 +22,5 @@ bool SbFileFlush(SbFile file) { return ::starboard::shared::posix::impl::FileFlush(file); } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/android/shared/file_get_info.cc b/starboard/android/shared/file_get_info.cc index d7e755952cfc..74643998b9d5 100644 --- a/starboard/android/shared/file_get_info.cc +++ b/starboard/android/shared/file_get_info.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/file.h" #include @@ -32,3 +34,5 @@ bool SbFileGetInfo(SbFile file, SbFileInfo* out_info) { return ::starboard::shared::posix::impl::FileGetInfo(file, out_info); } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/android/shared/file_open.cc b/starboard/android/shared/file_open.cc index 8d82ab584359..a8239cea1424 100644 --- a/starboard/android/shared/file_open.cc +++ b/starboard/android/shared/file_open.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/file.h" #include @@ -104,3 +106,5 @@ SbFile SbFileOpen(const char* path, } return kSbFileInvalid; } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/android/shared/file_read.cc b/starboard/android/shared/file_read.cc index d283019f9813..3a99ca93f332 100644 --- a/starboard/android/shared/file_read.cc +++ b/starboard/android/shared/file_read.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/file.h" #include @@ -30,3 +32,5 @@ int SbFileRead(SbFile file, char* data, int size) { return ::starboard::shared::posix::impl::FileRead(file, data, size); } } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/android/shared/file_seek.cc b/starboard/android/shared/file_seek.cc index 590446b92e92..4da94081914b 100644 --- a/starboard/android/shared/file_seek.cc +++ b/starboard/android/shared/file_seek.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/file.h" #include @@ -26,3 +28,5 @@ int64_t SbFileSeek(SbFile file, SbFileWhence whence, int64_t offset) { return ::starboard::shared::posix::impl::FileSeek(file, whence, offset); } } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/android/shared/file_truncate.cc b/starboard/android/shared/file_truncate.cc index c1075e789323..d017dd914248 100644 --- a/starboard/android/shared/file_truncate.cc +++ b/starboard/android/shared/file_truncate.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/file.h" #include "starboard/android/shared/file_internal.h" @@ -20,3 +22,5 @@ bool SbFileTruncate(SbFile file, int64_t length) { return ::starboard::shared::posix::impl::FileTruncate(file, length); } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/android/shared/file_write.cc b/starboard/android/shared/file_write.cc index 77054354de06..6f09a3dede85 100644 --- a/starboard/android/shared/file_write.cc +++ b/starboard/android/shared/file_write.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/file.h" #include "starboard/android/shared/file_internal.h" @@ -20,3 +22,5 @@ int SbFileWrite(SbFile file, const char* data, int size) { return ::starboard::shared::posix::impl::FileWrite(file, data, size); } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/common/BUILD.gn b/starboard/common/BUILD.gn index 11f09f7e4d6d..c89e6fe0518c 100644 --- a/starboard/common/BUILD.gn +++ b/starboard/common/BUILD.gn @@ -101,7 +101,10 @@ static_library("common") { static_library("file_wrapper") { check_includes = false - sources = [ "file_wrapper.h" ] + sources = [ + "file_wrapper.cc", + "file_wrapper.h", + ] } target(gtest_target_type, "common_test") { diff --git a/starboard/common/file.cc b/starboard/common/file.cc index bc358451a7cc..8f200b07d29b 100644 --- a/starboard/common/file.cc +++ b/starboard/common/file.cc @@ -24,9 +24,11 @@ #include "starboard/common/log.h" #include "starboard/common/metrics/stats_tracker.h" +#include "starboard/common/string.h" #include "starboard/configuration_constants.h" #include "starboard/directory.h" #include "starboard/file.h" +#include "starboard/shared/starboard/file_atomic_replace_write_file.h" #include "starboard/string.h" namespace starboard { @@ -42,6 +44,30 @@ bool DirectoryCloseLogFailure(const char* path, SbDirectory dir) { } // namespace +bool FileCanOpen(const char* path, int flags) { + struct stat file_info; + if (stat(path, &file_info) != 0) { + return false; + } + + bool has_read_flag = (flags & O_RDONLY) || (flags & O_RDWR); + bool has_write_flag = (flags & O_WRONLY) || (flags & O_RDWR); + bool can_read = file_info.st_mode & S_IRUSR; + bool can_write = file_info.st_mode & S_IWUSR; + + if (has_read_flag && !can_read) { + errno = EACCES; + return false; + } + + if (has_write_flag && !can_write) { + errno = EACCES; + return false; + } + + return true; +} + bool IsValid(int file) { return file >= 0; } diff --git a/starboard/common/file.h b/starboard/common/file.h index 0d0d17e6a0b0..355df24c24c7 100644 --- a/starboard/common/file.h +++ b/starboard/common/file.h @@ -42,6 +42,8 @@ struct FileStruct { typedef FileStruct* FilePtr; +bool FileCanOpen(const char* path, int flags); + bool IsValid(int file); ssize_t ReadAll(int fd, void* data, int size); diff --git a/starboard/common/file_wrapper.cc b/starboard/common/file_wrapper.cc new file mode 100644 index 000000000000..ed0d5f490422 --- /dev/null +++ b/starboard/common/file_wrapper.cc @@ -0,0 +1,100 @@ +// Copyright 2016 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 "starboard/common/file_wrapper.h" + +#include +#include + +#include "starboard/common/file.h" +#include "starboard/common/log.h" +#include "starboard/common/string.h" + +namespace { + +bool IsUpdate(const char* mode) { + for (const char* m = mode; *m != '\0'; ++m) { + if (*m == '+') { + return true; + } + } + + return false; +} +} // namespace + +extern "C" { + +int FileModeStringToFlags(const char* mode) { + if (!mode) { + return 0; + } + + int length = static_cast(strlen(mode)); + if (length < 1) { + return 0; + } + + int flags = 0; + switch (mode[0]) { + case 'r': + if (IsUpdate(mode + 1)) { + flags |= O_RDWR; + } else { + flags |= O_RDONLY; + } + break; + case 'w': + if (IsUpdate(mode + 1)) { + flags |= O_RDWR; + } else { + flags |= O_WRONLY; + } + flags |= O_CREAT | O_TRUNC; + break; + case 'a': + if (IsUpdate(mode + 1)) { + flags |= O_RDWR; + } else { + flags |= O_WRONLY; + } + flags |= O_CREAT; + break; + default: + SB_NOTREACHED(); + break; + } + return flags; +} + +int file_close(FilePtr file) { + if (!file) { + return -1; + } + int result = -1; + if (file->fd >= 0) { + result = close(file->fd); + } + delete file; + return result; +} + +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; +} + +} // extern "C" diff --git a/starboard/common/file_wrapper.h b/starboard/common/file_wrapper.h index 17625aa80469..03be8198b69e 100644 --- a/starboard/common/file_wrapper.h +++ b/starboard/common/file_wrapper.h @@ -21,4 +21,24 @@ typedef struct FileStruct { typedef FileStruct* FilePtr; +#ifdef __cplusplus +extern "C" { +#endif + +// Converts an ISO |fopen()| mode string into flags that can be equivalently +// passed into POSIX open(). +// +// |mode|: The mode string to be converted into flags. +int FileModeStringToFlags(const char* mode); + +// Wrapper for close() that takes in a FilePtr instead of a file descriptor. +int file_close(FilePtr file); + +// Wrapper for open() that returns a FilePtr instead of a file descriptor. +FilePtr file_open(const char* path, int mode); + +#ifdef __cplusplus +} +#endif + #endif // STARBOARD_COMMON_FILE_WRAPPER_H_ diff --git a/starboard/elf_loader/exported_symbols.cc b/starboard/elf_loader/exported_symbols.cc index 0110fea7491a..7cd034ecf08c 100644 --- a/starboard/elf_loader/exported_symbols.cc +++ b/starboard/elf_loader/exported_symbols.cc @@ -174,23 +174,21 @@ ExportedSymbols::ExportedSymbols() { REGISTER_SYMBOL(SbEventCancel); REGISTER_SYMBOL(SbEventSchedule); REGISTER_SYMBOL(SbFileAtomicReplace); +#if SB_API_VERSION < 16 REGISTER_SYMBOL(SbFileCanOpen); REGISTER_SYMBOL(SbFileClose); REGISTER_SYMBOL(SbFileDelete); -#if SB_API_VERSION < 16 REGISTER_SYMBOL(SbFileExists); -#endif // SB_API_VERSION < 16 REGISTER_SYMBOL(SbFileFlush); REGISTER_SYMBOL(SbFileGetInfo); -#if SB_API_VERSION < 16 REGISTER_SYMBOL(SbFileGetPathInfo); -#endif // SB_API_VERSION < 16 REGISTER_SYMBOL(SbFileModeStringToFlags); REGISTER_SYMBOL(SbFileOpen); REGISTER_SYMBOL(SbFileRead); REGISTER_SYMBOL(SbFileSeek); REGISTER_SYMBOL(SbFileTruncate); REGISTER_SYMBOL(SbFileWrite); +#endif // SB_API_VERSION < 16 REGISTER_SYMBOL(SbGetEglInterface); REGISTER_SYMBOL(SbGetGlesInterface); #if SB_API_VERSION < 16 diff --git a/starboard/file.h b/starboard/file.h index 9a54ef79501f..ddd53fa1b770 100644 --- a/starboard/file.h +++ b/starboard/file.h @@ -127,6 +127,8 @@ static inline bool SbFileIsValid(SbFile file) { return file != kSbFileInvalid; } +#if SB_API_VERSION < 16 + // DEPRECATED with SB_API_VERSION 16 // // Opens the file at |path|, which must be absolute, creating it if specified by @@ -158,6 +160,8 @@ 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 + // 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 // atomic operation. The file will either have all of the data, or none. @@ -169,6 +173,8 @@ SB_EXPORT bool SbFileAtomicReplace(const char* path, const char* data, int64_t data_size); +#if SB_API_VERSION < 16 + // DEPRECATED with SB_API_VERSION 16 // // Changes the current read/write position in |file|. The return value @@ -264,11 +270,11 @@ 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); -#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 // DEPRECATED with SB_API_VERSION 16 @@ -279,6 +285,8 @@ 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 @@ -346,6 +354,8 @@ static inline int SbFileWriteAll(SbFile file, const char* data, int size) { return bytes_written ? bytes_written : rv; } +#endif // SB_API_VERSION < 16 + #ifdef __cplusplus } // extern "C" #endif diff --git a/starboard/loader_app/app_key_files_test.cc b/starboard/loader_app/app_key_files_test.cc index 870fcf17f18d..15207c4a106d 100644 --- a/starboard/loader_app/app_key_files_test.cc +++ b/starboard/loader_app/app_key_files_test.cc @@ -15,6 +15,7 @@ #include "starboard/loader_app/app_key_files.h" #include +#include #include #include @@ -53,24 +54,24 @@ TEST_F(AppKeyFilesTest, TestGoodKeyFile) { std::string file_path = GetGoodAppKeyFilePath(dir_, kTestAppKey); ASSERT_FALSE(file_path.empty()); if (FileExists(file_path.c_str())) { - SbFileDelete(file_path.c_str()); + unlink(file_path.c_str()); } ASSERT_FALSE(FileExists(file_path.c_str())); ASSERT_TRUE(CreateAppKeyFile(file_path)); ASSERT_TRUE(FileExists(file_path.c_str())); - SbFileDelete(file_path.c_str()); + unlink(file_path.c_str()); } TEST_F(AppKeyFilesTest, TestBadKeyFile) { std::string file_path = GetBadAppKeyFilePath(dir_, kTestAppKey); ASSERT_FALSE(file_path.empty()); if (FileExists(file_path.c_str())) { - SbFileDelete(file_path.c_str()); + unlink(file_path.c_str()); } ASSERT_FALSE(FileExists(file_path.c_str())); ASSERT_TRUE(CreateAppKeyFile(file_path)); ASSERT_TRUE(FileExists(file_path.c_str())); - SbFileDelete(file_path.c_str()); + unlink(file_path.c_str()); } TEST_F(AppKeyFilesTest, TestGoodKeyFileInvalidInput) { @@ -96,7 +97,7 @@ TEST_F(AppKeyFilesTest, TestAnyGoodKeyFile) { ASSERT_TRUE(CreateAppKeyFile(file_path)); ASSERT_TRUE(FileExists(file_path.c_str())); ASSERT_TRUE(AnyGoodAppKeyFile(dir_)); - SbFileDelete(file_path.c_str()); + unlink(file_path.c_str()); } } // namespace diff --git a/starboard/loader_app/drain_file.cc b/starboard/loader_app/drain_file.cc index 843339aa9ae1..be45051d9d1f 100644 --- a/starboard/loader_app/drain_file.cc +++ b/starboard/loader_app/drain_file.cc @@ -181,7 +181,7 @@ bool TryDrain(const char* dir, const char* app_key) { int file = open(path.c_str(), O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); SB_DCHECK(file >= 0); - SB_DCHECK(close(file)); + SB_DCHECK(close(file) == 0); SB_LOG(INFO) << "Created drain file at '" << path << "'"; diff --git a/starboard/loader_app/drain_file_helper.cc b/starboard/loader_app/drain_file_helper.cc index 3c25efbf704a..5e883ec4cb3f 100644 --- a/starboard/loader_app/drain_file_helper.cc +++ b/starboard/loader_app/drain_file_helper.cc @@ -15,6 +15,7 @@ #include "starboard/loader_app/drain_file_helper.h" #include +#include #include "starboard/common/file.h" #include "starboard/loader_app/drain_file.h" @@ -41,7 +42,7 @@ ScopedDrainFile::ScopedDrainFile(const std::string& dir, ScopedDrainFile::~ScopedDrainFile() { if (!Exists()) return; - EXPECT_TRUE(SbFileDelete(path_.c_str())); + EXPECT_TRUE(!unlink(path_.c_str())); } bool ScopedDrainFile::Exists() const { diff --git a/starboard/loader_app/drain_file_test.cc b/starboard/loader_app/drain_file_test.cc index 2f23686a6f0f..680cc3554009 100644 --- a/starboard/loader_app/drain_file_test.cc +++ b/starboard/loader_app/drain_file_test.cc @@ -184,13 +184,13 @@ TEST_F(DrainFileTest, SunnyDayRankCorrectlyRanksFiles) { std::vector result(kSbFileMaxName); EXPECT_TRUE(DrainFileRankAndCheck(GetTempDir(), "a")); - EXPECT_TRUE(SbFileDelete(early_and_least.path().c_str())); + EXPECT_TRUE(!unlink(early_and_least.path().c_str())); EXPECT_TRUE(DrainFileRankAndCheck(GetTempDir(), "c")); - EXPECT_TRUE(SbFileDelete(later_and_least.path().c_str())); + EXPECT_TRUE(!unlink(later_and_least.path().c_str())); EXPECT_TRUE(DrainFileRankAndCheck(GetTempDir(), "b")); - EXPECT_TRUE(SbFileDelete(later_and_greatest.path().c_str())); + EXPECT_TRUE(!unlink(later_and_greatest.path().c_str())); } // Ranking drain files should ignore expired files. @@ -206,14 +206,14 @@ TEST_F(DrainFileTest, SunnyDayRankCorrectlyIgnoresExpired) { std::vector result(kSbFileMaxName); EXPECT_TRUE(DrainFileRankAndCheck(GetTempDir(), "c")); - EXPECT_TRUE(SbFileDelete(later_and_least.path().c_str())); + EXPECT_TRUE(!unlink(later_and_least.path().c_str())); EXPECT_TRUE(DrainFileRankAndCheck(GetTempDir(), "b")); - EXPECT_TRUE(SbFileDelete(later_and_greatest.path().c_str())); + EXPECT_TRUE(!unlink(later_and_greatest.path().c_str())); // Even though "a" is still there Rank should find nothing since it's expired. EXPECT_TRUE(DrainFileRankAndCheck(GetTempDir(), "")); - EXPECT_TRUE(SbFileDelete(early_and_expired.path().c_str())); + EXPECT_TRUE(!unlink(early_and_expired.path().c_str())); } // Tests the "racing updaters" scenario. diff --git a/starboard/nplb/BUILD.gn b/starboard/nplb/BUILD.gn index 746814970348..164d7e29b0e0 100644 --- a/starboard/nplb/BUILD.gn +++ b/starboard/nplb/BUILD.gn @@ -146,12 +146,14 @@ target(gtest_target_type, "nplb") { "posix_compliance/posix_directory_create_test.cc", "posix_compliance/posix_directory_get_next_test.cc", "posix_compliance/posix_directory_open_test.cc", + "posix_compliance/posix_file_can_open_test.cc", "posix_compliance/posix_file_close_test.cc", "posix_compliance/posix_file_delete_test.cc", "posix_compliance/posix_file_fsync_test.cc", "posix_compliance/posix_file_ftruncate_test.cc", "posix_compliance/posix_file_get_info_test.cc", "posix_compliance/posix_file_get_path_info_test.cc", + "posix_compliance/posix_file_mode_string_to_flags_test.cc", "posix_compliance/posix_file_open_test.cc", "posix_compliance/posix_file_read_test.cc", "posix_compliance/posix_file_seek_test.cc", @@ -303,6 +305,7 @@ target(gtest_target_type, "nplb") { deps = [ "//starboard:starboard_group", "//starboard/common", + "//starboard/common:file_wrapper", "//starboard/nplb/compiler_compliance:cpp14_supported", "//starboard/shared/starboard/media:media_util", "//starboard/shared/starboard/player:video_dmp", diff --git a/starboard/nplb/file_atomic_replace_test.cc b/starboard/nplb/file_atomic_replace_test.cc index c13330b41035..9b63835db1cd 100644 --- a/starboard/nplb/file_atomic_replace_test.cc +++ b/starboard/nplb/file_atomic_replace_test.cc @@ -12,8 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include +#include "starboard/common/file.h" #include "starboard/file.h" #include "starboard/nplb/file_helpers.h" #include "testing/gtest/include/gtest/gtest.h" @@ -31,17 +33,15 @@ bool CompareFileContentsToString(const char* filename, int size) { char result[kTestContentsLength] = {'\0'}; - SbFileError error; - SbFile file = - SbFileOpen(filename, kSbFileOpenOnly | kSbFileRead, nullptr, &error); + int file = open(filename, O_RDONLY, S_IRUSR | S_IWUSR); - EXPECT_EQ(kSbFileOk, error); + EXPECT_TRUE(starboard::IsValid(file)); // We always try to read kTestContentsLength since the data will at most be // this long. There are test cases where the number of bytes read will be // less. - EXPECT_EQ(size, SbFileReadAll(file, result, kTestContentsLength)); - EXPECT_TRUE(SbFileClose(file)); + EXPECT_EQ(size, starboard::ReadAll(file, result, kTestContentsLength)); + EXPECT_TRUE(!close(file)); return strncmp(str, result, kTestContentsLength) == 0; } diff --git a/starboard/nplb/file_can_open_test.cc b/starboard/nplb/file_can_open_test.cc index 47bf3bb18c8f..f9c7f3c99c3f 100644 --- a/starboard/nplb/file_can_open_test.cc +++ b/starboard/nplb/file_can_open_test.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include #include "starboard/configuration_constants.h" @@ -70,3 +72,5 @@ TEST(SbFileCanOpenTest, ExistingStaticContentFileSucceeds) { } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 16 diff --git a/starboard/nplb/file_close_test.cc b/starboard/nplb/file_close_test.cc index 0c4c5a1c08a8..425bdf6d534d 100644 --- a/starboard/nplb/file_close_test.cc +++ b/starboard/nplb/file_close_test.cc @@ -14,6 +14,8 @@ // SbFileClose is partially tested in file_open_test.cc. +#if SB_API_VERSION < 16 + #include "starboard/file.h" #include "testing/gtest/include/gtest/gtest.h" @@ -29,3 +31,5 @@ TEST(SbFileCloseTest, CloseInvalidFails) { } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 16 diff --git a/starboard/nplb/file_delete_recursive_test.cc b/starboard/nplb/file_delete_recursive_test.cc index 29e8774aaa86..cbd07c7e1909 100644 --- a/starboard/nplb/file_delete_recursive_test.cc +++ b/starboard/nplb/file_delete_recursive_test.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include #include @@ -133,3 +135,5 @@ TEST(SbFileDeleteRecursiveTest, RainyDayNonExistentPathErrors) { } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 16 diff --git a/starboard/nplb/file_delete_test.cc b/starboard/nplb/file_delete_test.cc index 92fbe199dffd..dc40610e2758 100644 --- a/starboard/nplb/file_delete_test.cc +++ b/starboard/nplb/file_delete_test.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include #include @@ -63,3 +65,5 @@ TEST(SbFileDeleteTest, RainyDayNonExistentFileErrors) { } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 16 diff --git a/starboard/nplb/file_flush_test.cc b/starboard/nplb/file_flush_test.cc index d17795f912b3..92337b74928a 100644 --- a/starboard/nplb/file_flush_test.cc +++ b/starboard/nplb/file_flush_test.cc @@ -14,6 +14,8 @@ // SbFileFlush is otherwise tested in SbFileWriteTest. +#if SB_API_VERSION < 16 + #include "starboard/file.h" #include "starboard/nplb/file_helpers.h" #include "testing/gtest/include/gtest/gtest.h" @@ -30,3 +32,5 @@ TEST(SbFileFlushTest, InvalidFileErrors) { } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 16 diff --git a/starboard/nplb/file_get_info_test.cc b/starboard/nplb/file_get_info_test.cc index 6f531b39c8ba..7fe609024676 100644 --- a/starboard/nplb/file_get_info_test.cc +++ b/starboard/nplb/file_get_info_test.cc @@ -14,6 +14,8 @@ // GetInfo is mostly tested in the course of other tests. +#if SB_API_VERSION < 16 + #include #include "starboard/common/time.h" @@ -87,3 +89,5 @@ TEST(SbFileGetInfoTest, WorksOnStaticContentFiles) { } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 16 diff --git a/starboard/nplb/file_helpers.cc b/starboard/nplb/file_helpers.cc index e1715ae44df3..5f979f995ae5 100644 --- a/starboard/nplb/file_helpers.cc +++ b/starboard/nplb/file_helpers.cc @@ -14,16 +14,18 @@ #include "starboard/nplb/file_helpers.h" +#include #include +#include #include #include #include +#include "starboard/common/file.h" #include "starboard/common/log.h" #include "starboard/configuration_constants.h" #include "starboard/directory.h" -#include "starboard/file.h" #include "starboard/system.h" #include "testing/gtest/include/gtest/gtest.h" @@ -135,10 +137,10 @@ std::string ScopedRandomFile::MakeRandomFile(int length) { return filename; } - SbFile file = SbFileOpen(filename.c_str(), kSbFileCreateOnly | kSbFileWrite, - NULL, NULL); - EXPECT_TRUE(SbFileIsValid(file)); - if (!SbFileIsValid(file)) { + int file = + open(filename.c_str(), O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR); + EXPECT_TRUE(starboard::IsValid(file)); + if (!starboard::IsValid(file)) { return ""; } @@ -147,11 +149,11 @@ std::string ScopedRandomFile::MakeRandomFile(int length) { data[i] = static_cast(i & 0xFF); } - int bytes = SbFileWriteAll(file, data, length); + int bytes = starboard::WriteAll(file, data, length); EXPECT_EQ(bytes, length) << "Failed to write " << length << " bytes to " << filename; - bool result = SbFileClose(file); + bool result = !close(file); EXPECT_TRUE(result) << "Failed to close " << filename; delete[] data; return filename; diff --git a/starboard/nplb/file_helpers.h b/starboard/nplb/file_helpers.h index 881073732b6f..2d883932dcbe 100644 --- a/starboard/nplb/file_helpers.h +++ b/starboard/nplb/file_helpers.h @@ -15,6 +15,8 @@ #ifndef STARBOARD_NPLB_FILE_HELPERS_H_ #define STARBOARD_NPLB_FILE_HELPERS_H_ +#include + #include #include @@ -75,7 +77,7 @@ class ScopedRandomFile { (create == kCreate ? MakeRandomFile(size_) : MakeRandomFilePath()); } - ~ScopedRandomFile() { SbFileDelete(filename_.c_str()); } + ~ScopedRandomFile() { unlink(filename_.c_str()); } // Creates and returns a random filename (no path), but does not create the // file. diff --git a/starboard/nplb/file_mode_string_to_flags_test.cc b/starboard/nplb/file_mode_string_to_flags_test.cc index 0525b31b3514..ed159ed413fd 100644 --- a/starboard/nplb/file_mode_string_to_flags_test.cc +++ b/starboard/nplb/file_mode_string_to_flags_test.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include +#if SB_API_VERSION < 16 #include @@ -29,26 +29,37 @@ TEST(SbFileModeStringToFlagsTest, Empties) { } TEST(SbFileModeStringToFlagsTest, Arr) { - EXPECT_EQ(O_RDONLY, SbFileModeStringToFlags("r")); - EXPECT_EQ(O_RDWR, SbFileModeStringToFlags("r+")); - EXPECT_EQ(O_RDWR, SbFileModeStringToFlags("r+b")); - EXPECT_EQ(O_RDWR, SbFileModeStringToFlags("rb+")); + EXPECT_EQ(kSbFileOpenOnly | kSbFileRead, SbFileModeStringToFlags("r")); + EXPECT_EQ(kSbFileOpenOnly | kSbFileRead | kSbFileWrite, + SbFileModeStringToFlags("r+")); + EXPECT_EQ(kSbFileOpenOnly | kSbFileRead | kSbFileWrite, + SbFileModeStringToFlags("r+b")); + EXPECT_EQ(kSbFileOpenOnly | kSbFileRead | kSbFileWrite, + SbFileModeStringToFlags("rb+")); } TEST(SbFileModeStringToFlagsTest, Wuh) { - EXPECT_EQ(O_CREAT | O_TRUNC | O_WRONLY, SbFileModeStringToFlags("w")); - EXPECT_EQ(O_CREAT | O_TRUNC | O_RDWR, SbFileModeStringToFlags("w+")); - EXPECT_EQ(O_CREAT | O_TRUNC | O_RDWR, SbFileModeStringToFlags("w+b")); - EXPECT_EQ(O_CREAT | O_TRUNC | O_RDWR, SbFileModeStringToFlags("wb+")); + EXPECT_EQ(kSbFileCreateAlways | kSbFileWrite, SbFileModeStringToFlags("w")); + EXPECT_EQ(kSbFileCreateAlways | kSbFileWrite | kSbFileRead, + SbFileModeStringToFlags("w+")); + EXPECT_EQ(kSbFileCreateAlways | kSbFileWrite | kSbFileRead, + SbFileModeStringToFlags("w+b")); + EXPECT_EQ(kSbFileCreateAlways | kSbFileWrite | kSbFileRead, + SbFileModeStringToFlags("wb+")); } TEST(SbFileModeStringToFlagsTest, Aah) { - EXPECT_EQ(O_CREAT | O_WRONLY, SbFileModeStringToFlags("a")); - EXPECT_EQ(O_CREAT | O_RDWR, SbFileModeStringToFlags("a+")); - EXPECT_EQ(O_CREAT | O_RDWR, SbFileModeStringToFlags("a+b")); - EXPECT_EQ(O_CREAT | O_RDWR, SbFileModeStringToFlags("ab+")); + EXPECT_EQ(kSbFileOpenAlways | kSbFileWrite, SbFileModeStringToFlags("a")); + EXPECT_EQ(kSbFileOpenAlways | kSbFileWrite | kSbFileRead, + SbFileModeStringToFlags("a+")); + EXPECT_EQ(kSbFileOpenAlways | kSbFileWrite | kSbFileRead, + SbFileModeStringToFlags("a+b")); + EXPECT_EQ(kSbFileOpenAlways | kSbFileWrite | kSbFileRead, + SbFileModeStringToFlags("ab+")); } } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 16 diff --git a/starboard/nplb/file_read_test.cc b/starboard/nplb/file_read_test.cc index f9aa51d9d29a..b6fe296a833c 100644 --- a/starboard/nplb/file_read_test.cc +++ b/starboard/nplb/file_read_test.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include #include "starboard/file.h" @@ -294,3 +296,5 @@ TYPED_TEST(SbFileReadTest, ReadStaticContent) { } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 16 diff --git a/starboard/nplb/file_read_write_all_test.cc b/starboard/nplb/file_read_write_all_test.cc index 4871cea672a5..881ad7273202 100644 --- a/starboard/nplb/file_read_write_all_test.cc +++ b/starboard/nplb/file_read_write_all_test.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/file.h" #include "starboard/nplb/file_helpers.h" #include "testing/gtest/include/gtest/gtest.h" @@ -63,3 +65,5 @@ INSTANTIATE_TEST_CASE_P( } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 16 diff --git a/starboard/nplb/file_seek_test.cc b/starboard/nplb/file_seek_test.cc index 95bbdd4db35d..a39e3a024865 100644 --- a/starboard/nplb/file_seek_test.cc +++ b/starboard/nplb/file_seek_test.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include #include "starboard/file.h" @@ -235,3 +237,5 @@ TEST(SbFileSeekTest, FromBeginInStaticContentWorks) { } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 16 diff --git a/starboard/nplb/file_truncate_test.cc b/starboard/nplb/file_truncate_test.cc index ae2b3dee8255..fe7eb437e6f3 100644 --- a/starboard/nplb/file_truncate_test.cc +++ b/starboard/nplb/file_truncate_test.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include #include "starboard/file.h" @@ -115,3 +117,5 @@ TEST(SbFileTruncateTest, TruncateUpInSize) { } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 16 diff --git a/starboard/nplb/file_write_test.cc b/starboard/nplb/file_write_test.cc index 9f6d87d578ac..364f2e3d6581 100644 --- a/starboard/nplb/file_write_test.cc +++ b/starboard/nplb/file_write_test.cc @@ -15,6 +15,8 @@ // Writing is partially tested by some of the file helpers that create files for // the tests to operate on. +#if SB_API_VERSION < 16 + #include #include "starboard/file.h" @@ -167,3 +169,5 @@ TYPED_TEST(SbFileWriteTest, WriteZeroBytes) { } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 16 diff --git a/starboard/nplb/nplb_evergreen_compat_tests/storage_test.cc b/starboard/nplb/nplb_evergreen_compat_tests/storage_test.cc index 91f47b172e25..b5f03f02c99b 100644 --- a/starboard/nplb/nplb_evergreen_compat_tests/storage_test.cc +++ b/starboard/nplb/nplb_evergreen_compat_tests/storage_test.cc @@ -13,6 +13,7 @@ // limitations under the License. #include +#include #include #include @@ -81,7 +82,7 @@ TEST_F(StorageTest, VerifyStorageDirectory) { ASSERT_EQ('A', buf[i]); } - ASSERT_TRUE(SbFileDelete(file_path.data())); + ASSERT_TRUE(!unlink(file_path.data())); struct stat info; ASSERT_FALSE(stat(file_path.data(), &info) == 0); } diff --git a/starboard/nplb/posix_compliance/posix_file_can_open_test.cc b/starboard/nplb/posix_compliance/posix_file_can_open_test.cc new file mode 100644 index 000000000000..a1747ce4d626 --- /dev/null +++ b/starboard/nplb/posix_compliance/posix_file_can_open_test.cc @@ -0,0 +1,73 @@ +// 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 + +#include + +#include "starboard/common/file.h" +#include "starboard/configuration_constants.h" +#include "starboard/nplb/file_helpers.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace starboard { +namespace nplb { +namespace { + +TEST(PosixFileCanOpenTest, NonExistingFileFails) { + ScopedRandomFile random_file(ScopedRandomFile::kDontCreate); + const std::string& filename = random_file.filename(); + + bool result = starboard::FileCanOpen(filename.c_str(), O_RDONLY); + EXPECT_FALSE(result); + + result = + starboard::FileCanOpen(filename.c_str(), O_CREAT | O_EXCL | O_WRONLY); + EXPECT_FALSE(result); + + result = starboard::FileCanOpen(filename.c_str(), O_CREAT | O_RDWR); + EXPECT_FALSE(result); +} + +TEST(PosixFileCanOpenTest, ExistingFileSucceeds) { + ScopedRandomFile random_file; + const std::string& filename = random_file.filename(); + + bool result = starboard::FileCanOpen(filename.c_str(), O_RDONLY); + EXPECT_TRUE(result); + + result = + starboard::FileCanOpen(filename.c_str(), O_CREAT | O_EXCL | O_WRONLY); + EXPECT_TRUE(result); + + result = starboard::FileCanOpen(filename.c_str(), O_CREAT | O_RDWR); + EXPECT_TRUE(result); +} + +TEST(PosixFileCanOpenTest, NonExistingStaticContentFileFails) { + std::string directory_path = GetFileTestsDataDir(); + std::string missing_file = directory_path + kSbFileSepChar + "missing_file"; + EXPECT_FALSE(starboard::FileCanOpen(missing_file.c_str(), O_RDONLY)); +} + +TEST(PosixFileCanOpenTest, ExistingStaticContentFileSucceeds) { + for (auto path : GetFileTestsFilePaths()) { + EXPECT_TRUE(starboard::FileCanOpen(path.c_str(), O_RDONLY)) + << "Can't open: " << path; + } +} + +} // namespace +} // namespace nplb +} // namespace starboard diff --git a/starboard/nplb/posix_compliance/posix_file_mode_string_to_flags_test.cc b/starboard/nplb/posix_compliance/posix_file_mode_string_to_flags_test.cc new file mode 100644 index 000000000000..b1965b164b9d --- /dev/null +++ b/starboard/nplb/posix_compliance/posix_file_mode_string_to_flags_test.cc @@ -0,0 +1,58 @@ +// 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. + +#if SB_API_VERSION >= 16 + +#include + +#include + +#include "starboard/common/file_wrapper.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace starboard { +namespace nplb { +namespace { + +TEST(PosixFileModeStringToFlagsTest, Empties) { + EXPECT_EQ(0, FileModeStringToFlags(NULL)); + EXPECT_EQ(0, FileModeStringToFlags("")); +} + +TEST(PosixFileModeStringToFlagsTest, Arr) { + EXPECT_EQ(O_RDONLY, FileModeStringToFlags("r")); + EXPECT_EQ(O_RDWR, FileModeStringToFlags("r+")); + EXPECT_EQ(O_RDWR, FileModeStringToFlags("r+b")); + EXPECT_EQ(O_RDWR, FileModeStringToFlags("rb+")); +} + +TEST(PosixFileModeStringToFlagsTest, Wuh) { + EXPECT_EQ(O_CREAT | O_TRUNC | O_WRONLY, FileModeStringToFlags("w")); + EXPECT_EQ(O_CREAT | O_TRUNC | O_RDWR, FileModeStringToFlags("w+")); + EXPECT_EQ(O_CREAT | O_TRUNC | O_RDWR, FileModeStringToFlags("w+b")); + EXPECT_EQ(O_CREAT | O_TRUNC | O_RDWR, FileModeStringToFlags("wb+")); +} + +TEST(PosixFileModeStringToFlagsTest, Aah) { + EXPECT_EQ(O_CREAT | O_WRONLY, FileModeStringToFlags("a")); + EXPECT_EQ(O_CREAT | O_RDWR, FileModeStringToFlags("a+")); + EXPECT_EQ(O_CREAT | O_RDWR, FileModeStringToFlags("a+b")); + EXPECT_EQ(O_CREAT | O_RDWR, FileModeStringToFlags("ab+")); +} + +} // namespace +} // namespace nplb +} // namespace starboard + +#endif // SB_API_VERSION >= 16 diff --git a/starboard/nplb/system_clear_last_error_test.cc b/starboard/nplb/system_clear_last_error_test.cc index 1755799a8447..699656826d5e 100644 --- a/starboard/nplb/system_clear_last_error_test.cc +++ b/starboard/nplb/system_clear_last_error_test.cc @@ -12,7 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "starboard/file.h" +#include + #include "starboard/nplb/file_helpers.h" #include "starboard/system.h" #include "testing/gtest/include/gtest/gtest.h" @@ -24,8 +25,7 @@ namespace { TEST(SbSystemClearLastErrorTest, SunnyDay) { // Opening a non-existent file should generate an error on all platforms. ScopedRandomFile random_file(ScopedRandomFile::kDontCreate); - SbFile file = SbFileOpen(random_file.filename().c_str(), - kSbFileOpenOnly | kSbFileRead, NULL, NULL); + int file = open(random_file.filename().c_str(), O_RDONLY, S_IRUSR | S_IWUSR); EXPECT_NE(0, SbSystemGetLastError()); SbSystemClearLastError(); diff --git a/starboard/nplb/system_get_error_string_test.cc b/starboard/nplb/system_get_error_string_test.cc index 1fb6aa5d28e6..04fa50d84803 100644 --- a/starboard/nplb/system_get_error_string_test.cc +++ b/starboard/nplb/system_get_error_string_test.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + #include "starboard/common/string.h" #include "starboard/file.h" #include "starboard/nplb/file_helpers.h" @@ -25,8 +27,7 @@ namespace { TEST(SbSystemGetErrorStringTest, SunnyDay) { // Opening a non-existent file should generate an error on all platforms. ScopedRandomFile random_file(ScopedRandomFile::kDontCreate); - SbFile file = SbFileOpen(random_file.filename().c_str(), - kSbFileOpenOnly | kSbFileRead, NULL, NULL); + int file = open(random_file.filename().c_str(), O_RDONLY, S_IRUSR | S_IWUSR); SbSystemError error = SbSystemGetLastError(); EXPECT_NE(0, error); diff --git a/starboard/nplb/system_get_last_error_test.cc b/starboard/nplb/system_get_last_error_test.cc index d6f43231f67c..f001a9d23993 100644 --- a/starboard/nplb/system_get_last_error_test.cc +++ b/starboard/nplb/system_get_last_error_test.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + #include "starboard/file.h" #include "starboard/nplb/file_helpers.h" #include "starboard/system.h" @@ -24,8 +26,7 @@ namespace { TEST(SbSystemGetLastErrorTest, SunnyDay) { // Opening a non-existent file should generate an error on all platforms. ScopedRandomFile random_file(ScopedRandomFile::kDontCreate); - SbFile file = SbFileOpen(random_file.filename().c_str(), - kSbFileOpenOnly | kSbFileRead, NULL, NULL); + int file = open(random_file.filename().c_str(), O_RDONLY, S_IRUSR | S_IWUSR); SbSystemError error = SbSystemGetLastError(); EXPECT_NE(0, error); diff --git a/starboard/nplb/system_get_path_test.cc b/starboard/nplb/system_get_path_test.cc index 350a2d4c52a3..002d515e89fc 100644 --- a/starboard/nplb/system_get_path_test.cc +++ b/starboard/nplb/system_get_path_test.cc @@ -15,6 +15,7 @@ #include #include #include +#include #include @@ -132,7 +133,8 @@ TEST(SbSystemGetPathTest, CanCreateAndRemoveDirectoryInCache) { std::string sub_path = kSbFileSepString + ScopedRandomFile::MakeRandomFilename(); EXPECT_GT(starboard::strlcat(path.data(), sub_path.c_str(), kPathSize), 0); - EXPECT_TRUE(SbFileDelete(path.data())); + // rmdir return -1 when directory does not exist. + EXPECT_TRUE(rmdir(path.data()) == 0 || !DirectoryExists(path.data())); EXPECT_FALSE(FileExists(path.data())); // Create the directory and confirm it exists and can be opened. @@ -146,7 +148,7 @@ TEST(SbSystemGetPathTest, CanCreateAndRemoveDirectoryInCache) { // Lastly, close and delete the directory. EXPECT_TRUE(closedir(directory) == 0); - EXPECT_TRUE(SbFileDelete(path.data())); + EXPECT_TRUE(rmdir(path.data()) == 0); EXPECT_FALSE(FileExists(path.data())); } } @@ -165,7 +167,8 @@ TEST(SbSystemGetPathTest, CanWriteAndReadCache) { std::string sub_path = kSbFileSepString + ScopedRandomFile::MakeRandomFilename(); EXPECT_GT(starboard::strlcat(path.data(), sub_path.c_str(), kPathSize), 0); - EXPECT_TRUE(SbFileDelete(path.data())); + // unlink return -1 when directory does not exist. + EXPECT_TRUE(unlink(path.data()) == 0 || !FileExists(path.data())); EXPECT_FALSE(FileExists(path.data())); // Write to the file and check that we can read from it. @@ -192,7 +195,7 @@ TEST(SbSystemGetPathTest, CanWriteAndReadCache) { EXPECT_EQ(content_read, content_to_write); // Lastly, delete the file. - EXPECT_TRUE(SbFileDelete(path.data())); + EXPECT_TRUE(unlink(path.data()) == 0); EXPECT_FALSE(FileExists(path.data())); } } diff --git a/starboard/shared/posix/file_atomic_replace.cc b/starboard/shared/posix/file_atomic_replace.cc index ddec1db37c28..60e696876ccb 100644 --- a/starboard/shared/posix/file_atomic_replace.cc +++ b/starboard/shared/posix/file_atomic_replace.cc @@ -15,6 +15,7 @@ #include "starboard/file.h" #include +#include #include #include @@ -48,7 +49,7 @@ bool SbFileAtomicReplace(const char* path, temp_path.data(), data, data_size)) { return false; } - if (file_exists && !SbFileDelete(path)) { + if (file_exists && unlink(path)) { return false; } if (rename(temp_path.data(), path) != 0) { diff --git a/starboard/shared/posix/file_can_open.cc b/starboard/shared/posix/file_can_open.cc index d0514d78e41d..04391989b8c2 100644 --- a/starboard/shared/posix/file_can_open.cc +++ b/starboard/shared/posix/file_can_open.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/shared/posix/file_internal.h" #include "starboard/shared/posix/impl/file_can_open.h" @@ -19,3 +21,5 @@ bool SbFileCanOpen(const char* path, int flags) { return ::starboard::shared::posix::impl::FileCanOpen(path, flags); } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/posix/file_close.cc b/starboard/shared/posix/file_close.cc index ebda5137605b..0ea2983e1a1e 100644 --- a/starboard/shared/posix/file_close.cc +++ b/starboard/shared/posix/file_close.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/shared/posix/file_internal.h" #include "starboard/shared/posix/impl/file_close.h" @@ -19,3 +21,5 @@ bool SbFileClose(SbFile file) { return ::starboard::shared::posix::impl::FileClose(file); } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/posix/file_delete.cc b/starboard/shared/posix/file_delete.cc index 7830cebc5f88..246cf940e3ec 100644 --- a/starboard/shared/posix/file_delete.cc +++ b/starboard/shared/posix/file_delete.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/shared/posix/file_internal.h" #include "starboard/shared/posix/impl/file_delete.h" @@ -19,3 +21,5 @@ bool SbFileDelete(const char* path) { return ::starboard::shared::posix::impl::FileDelete(path); } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/posix/file_exists.cc b/starboard/shared/posix/file_exists.cc index b418a4692d9b..97774b07573d 100644 --- a/starboard/shared/posix/file_exists.cc +++ b/starboard/shared/posix/file_exists.cc @@ -21,4 +21,5 @@ bool SbFileExists(const char* path) { return ::starboard::shared::posix::impl::FileExists(path); } -#endif + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/posix/file_flush.cc b/starboard/shared/posix/file_flush.cc index e62aeb51db64..2c27255b7b5f 100644 --- a/starboard/shared/posix/file_flush.cc +++ b/starboard/shared/posix/file_flush.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/shared/posix/file_internal.h" #include "starboard/shared/posix/impl/file_flush.h" @@ -19,3 +21,5 @@ bool SbFileFlush(SbFile file) { return ::starboard::shared::posix::impl::FileFlush(file); } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/posix/file_get_info.cc b/starboard/shared/posix/file_get_info.cc index 163462bc3175..6085569c76fd 100644 --- a/starboard/shared/posix/file_get_info.cc +++ b/starboard/shared/posix/file_get_info.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/shared/posix/file_internal.h" #include "starboard/shared/posix/impl/file_get_info.h" @@ -19,3 +21,5 @@ bool SbFileGetInfo(SbFile file, SbFileInfo* out_info) { return ::starboard::shared::posix::impl::FileGetInfo(file, out_info); } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/posix/file_open.cc b/starboard/shared/posix/file_open.cc index b8d76cf2017f..e4c547d399ff 100644 --- a/starboard/shared/posix/file_open.cc +++ b/starboard/shared/posix/file_open.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/shared/posix/file_internal.h" #include "starboard/shared/posix/impl/file_open.h" @@ -23,3 +25,5 @@ SbFile SbFileOpen(const char* path, return ::starboard::shared::posix::impl::FileOpen(path, flags, out_created, out_error); } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/posix/file_read.cc b/starboard/shared/posix/file_read.cc index 33d6debe31e4..68e7f6fab962 100644 --- a/starboard/shared/posix/file_read.cc +++ b/starboard/shared/posix/file_read.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/shared/posix/file_internal.h" #include "starboard/shared/posix/impl/file_read.h" @@ -19,3 +21,5 @@ int SbFileRead(SbFile file, char* data, int size) { return ::starboard::shared::posix::impl::FileRead(file, data, size); } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/posix/file_seek.cc b/starboard/shared/posix/file_seek.cc index 4ece7e8f0712..8be12c9192a8 100644 --- a/starboard/shared/posix/file_seek.cc +++ b/starboard/shared/posix/file_seek.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/shared/posix/file_internal.h" #include "starboard/shared/posix/impl/file_seek.h" @@ -19,3 +21,5 @@ int64_t SbFileSeek(SbFile file, SbFileWhence whence, int64_t offset) { return ::starboard::shared::posix::impl::FileSeek(file, whence, offset); } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/posix/file_truncate.cc b/starboard/shared/posix/file_truncate.cc index 603dd0110fa7..264cc05a07eb 100644 --- a/starboard/shared/posix/file_truncate.cc +++ b/starboard/shared/posix/file_truncate.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/shared/posix/file_internal.h" #include "starboard/shared/posix/impl/file_truncate.h" @@ -19,3 +21,5 @@ bool SbFileTruncate(SbFile file, int64_t length) { return ::starboard::shared::posix::impl::FileTruncate(file, length); } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/posix/file_write.cc b/starboard/shared/posix/file_write.cc index 7b0af3dec16d..0a9a832a3172 100644 --- a/starboard/shared/posix/file_write.cc +++ b/starboard/shared/posix/file_write.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/shared/posix/file_internal.h" #include "starboard/shared/posix/impl/file_write.h" @@ -19,3 +21,5 @@ int SbFileWrite(SbFile file, const char* data, int size) { return ::starboard::shared::posix::impl::FileWrite(file, data, size); } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/starboard/file_mode_string_to_flags.cc b/starboard/shared/starboard/file_mode_string_to_flags.cc index 4a177bf983fd..5105b86afd13 100644 --- a/starboard/shared/starboard/file_mode_string_to_flags.cc +++ b/starboard/shared/starboard/file_mode_string_to_flags.cc @@ -12,14 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include +#if SB_API_VERSION < 16 -#include "starboard/common/file.h" #include "starboard/common/log.h" #include "starboard/common/string.h" +#include "starboard/file.h" namespace { - bool IsUpdate(const char* mode) { for (const char* m = mode; *m != '\0'; ++m) { if (*m == '+') { @@ -31,8 +30,6 @@ bool IsUpdate(const char* mode) { } } // namespace -extern "C" { - int SbFileModeStringToFlags(const char* mode) { if (!mode) { return 0; @@ -47,26 +44,21 @@ int SbFileModeStringToFlags(const char* mode) { switch (mode[0]) { case 'r': if (IsUpdate(mode + 1)) { - flags |= O_RDWR; - } else { - flags |= O_RDONLY; + flags |= kSbFileWrite; } + flags |= kSbFileOpenOnly | kSbFileRead; break; case 'w': if (IsUpdate(mode + 1)) { - flags |= O_RDWR; - } else { - flags |= O_WRONLY; + flags |= kSbFileRead; } - flags |= O_CREAT | O_TRUNC; + flags |= kSbFileCreateAlways | kSbFileWrite; break; case 'a': if (IsUpdate(mode + 1)) { - flags |= O_RDWR; - } else { - flags |= O_WRONLY; + flags |= kSbFileRead; } - flags |= O_CREAT; + flags |= kSbFileOpenAlways | kSbFileWrite; break; default: SB_NOTREACHED(); @@ -75,4 +67,4 @@ int SbFileModeStringToFlags(const char* mode) { return flags; } -} // extern "C" +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/stub/file_can_open.cc b/starboard/shared/stub/file_can_open.cc index 7335af26ad2f..0b05d9d153fb 100644 --- a/starboard/shared/stub/file_can_open.cc +++ b/starboard/shared/stub/file_can_open.cc @@ -12,8 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/file.h" bool SbFileCanOpen(const char* path, int flags) { return false; } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/stub/file_close.cc b/starboard/shared/stub/file_close.cc index 47df9f2080d0..4d73424eb017 100644 --- a/starboard/shared/stub/file_close.cc +++ b/starboard/shared/stub/file_close.cc @@ -12,8 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/file.h" bool SbFileClose(SbFile file) { return false; } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/stub/file_delete.cc b/starboard/shared/stub/file_delete.cc index ff3225fdd7e5..2ca408b893bf 100644 --- a/starboard/shared/stub/file_delete.cc +++ b/starboard/shared/stub/file_delete.cc @@ -12,8 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/file.h" bool SbFileDelete(const char* path) { return false; } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/stub/file_exists.cc b/starboard/shared/stub/file_exists.cc index f446ca18ceee..37ccdd18ab7c 100644 --- a/starboard/shared/stub/file_exists.cc +++ b/starboard/shared/stub/file_exists.cc @@ -19,4 +19,5 @@ bool SbFileExists(const char* path) { return false; } -#endif + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/stub/file_flush.cc b/starboard/shared/stub/file_flush.cc index 5598957a4628..0d7f68661229 100644 --- a/starboard/shared/stub/file_flush.cc +++ b/starboard/shared/stub/file_flush.cc @@ -12,8 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/file.h" bool SbFileFlush(SbFile file) { return false; } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/stub/file_get_info.cc b/starboard/shared/stub/file_get_info.cc index 40cf2ee956ee..80a30cbf415a 100644 --- a/starboard/shared/stub/file_get_info.cc +++ b/starboard/shared/stub/file_get_info.cc @@ -12,8 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/file.h" bool SbFileGetInfo(SbFile file, SbFileInfo* out_info) { return false; } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/stub/file_open.cc b/starboard/shared/stub/file_open.cc index fe3332bdcca0..8685f90554b5 100644 --- a/starboard/shared/stub/file_open.cc +++ b/starboard/shared/stub/file_open.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/file.h" SbFile SbFileOpen(const char* path, @@ -20,3 +22,5 @@ SbFile SbFileOpen(const char* path, SbFileError* out_error) { return kSbFileInvalid; } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/stub/file_read.cc b/starboard/shared/stub/file_read.cc index 4bf60654861f..105b942813fa 100644 --- a/starboard/shared/stub/file_read.cc +++ b/starboard/shared/stub/file_read.cc @@ -12,8 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/file.h" int SbFileRead(SbFile file, char* data, int size) { return 0; } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/stub/file_seek.cc b/starboard/shared/stub/file_seek.cc index ac2dc2402013..b5b8d327c45a 100644 --- a/starboard/shared/stub/file_seek.cc +++ b/starboard/shared/stub/file_seek.cc @@ -12,8 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/file.h" int64_t SbFileSeek(SbFile file, SbFileWhence whence, int64_t offset) { return 0; } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/stub/file_truncate.cc b/starboard/shared/stub/file_truncate.cc index b65a8cce8e41..bd65a0c8f603 100644 --- a/starboard/shared/stub/file_truncate.cc +++ b/starboard/shared/stub/file_truncate.cc @@ -12,8 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/file.h" bool SbFileTruncate(SbFile file, int64_t length) { return false; } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/stub/file_write.cc b/starboard/shared/stub/file_write.cc index d6e785874be8..f7956852de60 100644 --- a/starboard/shared/stub/file_write.cc +++ b/starboard/shared/stub/file_write.cc @@ -12,8 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 16 + #include "starboard/file.h" int SbFileWrite(SbFile file, const char* data, int size) { return 0; } + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/win32/storage_write_record.cc b/starboard/shared/win32/storage_write_record.cc index 49c61c0bd5de..a83a955673c1 100644 --- a/starboard/shared/win32/storage_write_record.cc +++ b/starboard/shared/win32/storage_write_record.cc @@ -52,7 +52,7 @@ bool SbStorageWriteRecord(SbStorageRecord record, int temp_file = open(temp_file_path.data(), O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR); - if (!starboard::IsValid(file)) { + if (!starboard::IsValid(temp_file)) { return false; } diff --git a/third_party/boringssl/BUILD.gn b/third_party/boringssl/BUILD.gn index 8bf265b2d9ac..84dec32885e0 100644 --- a/third_party/boringssl/BUILD.gn +++ b/third_party/boringssl/BUILD.gn @@ -247,6 +247,7 @@ if (!use_cobalt_customizations) { public -= [ "src/include/openssl/opensslconf.h" ] public_deps = [ "//starboard:starboard_headers_only", + "//starboard/common:file_wrapper", ] configs -= [ "//starboard/build/config:size" ] configs += [ "//starboard/build/config:speed" ] diff --git a/third_party/boringssl/src/crypto/bio/file.c b/third_party/boringssl/src/crypto/bio/file.c index 7be615f71417..3a7a14d2e8d1 100644 --- a/third_party/boringssl/src/crypto/bio/file.c +++ b/third_party/boringssl/src/crypto/bio/file.c @@ -96,7 +96,7 @@ #if defined(OPENSSL_SYS_STARBOARD) #include #include -#endif // !defined(OPENSSL_SYS_STARBOARD) +#endif // defined(OPENSSL_SYS_STARBOARD) #include #include @@ -192,7 +192,7 @@ BIO *BIO_new_file(const char *filename, const char *mode) { #if defined(OPENSSL_SYS_STARBOARD) FilePtr sb_file = (FilePtr)malloc(sizeof(struct FileStruct)); memset(sb_file, 0, sizeof(struct FileStruct)); - sb_file->fd = open(filename, SbFileModeStringToFlags(mode), + sb_file->fd = open(filename, FileModeStringToFlags(mode), S_IRUSR | S_IWUSR); if (sb_file->fd < 0) { @@ -286,7 +286,7 @@ static int MS_CALLBACK file_free(BIO *a) { if (a->shutdown) { if ((a->init) && (a->ptr != NULL)) { #if defined(OPENSSL_SYS_STARBOARD) - close((FilePtr)a->ptr); + close(((FilePtr)a->ptr)->fd); #else // defined(OPENSSL_SYS_STARBOARD) // When this file was Starboardized, uplink support was added to the // non-Starboard code paths. But at this point it's not clear where to find @@ -314,7 +314,7 @@ static int MS_CALLBACK file_read(BIO *b, char *out, int outl) { int ret = 0; if (b->init && (out != NULL)) { #if defined(OPENSSL_SYS_STARBOARD) - ret = read((FilePtr)b->ptr, out, outl); + ret = read(((FilePtr)b->ptr)->fd, out, outl); if (ret < 0) { OPENSSL_PUT_SYSTEM_ERROR(); OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB); @@ -355,7 +355,7 @@ static int MS_CALLBACK file_write(BIO *b, const char *in, int inl) { if (b->init && (in != NULL)) { #if defined(OPENSSL_SYS_STARBOARD) - ret = write((FilePtr)b->ptr, in, inl); + ret = write(((FilePtr)b->ptr)->fd, in, inl); #else // defined(OPENSSL_SYS_STARBOARD) #ifndef NATIVE_TARGET_BUILD if (b->flags & BIO_FLAGS_UPLINK) @@ -392,7 +392,7 @@ static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr) { case BIO_C_FILE_SEEK: case BIO_CTRL_RESET: #if defined(OPENSSL_SYS_STARBOARD) - ret = (long)lseek((FilePtr)b->ptr, SEEK_SET, num); + ret = (long)lseek(((FilePtr)b->ptr)->fd, SEEK_SET, num); #else // defined(OPENSSL_SYS_STARBOARD) #ifndef NATIVE_TARGET_BUILD if (b->flags & BIO_FLAGS_UPLINK) @@ -405,8 +405,8 @@ static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr) { break; case BIO_CTRL_EOF: #if defined(OPENSSL_SYS_STARBOARD) - ret = (lseek((FilePtr)b->ptr, SEEK_CUR, 0) >= - lseek((FilePtr)b->ptr, SEEK_END, 0) + ret = (lseek(((FilePtr)b->ptr)->fd, SEEK_CUR, 0) >= + lseek(((FilePtr)b->ptr)->fd, SEEK_END, 0) ? 1 : 0); #else // defined(OPENSSL_SYS_STARBOARD) @@ -422,7 +422,7 @@ static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr) { case BIO_C_FILE_TELL: case BIO_CTRL_INFO: #if defined(OPENSSL_SYS_STARBOARD) - ret = lseek((FilePtr)b->ptr, SEEK_CUR, 0); + ret = lseek(((FilePtr)b->ptr)->fd, SEEK_CUR, 0); #else // defined(OPENSSL_SYS_STARBOARD) #ifndef NATIVE_TARGET_BUILD if (b->flags & BIO_FLAGS_UPLINK) @@ -527,7 +527,7 @@ static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr) { #endif #if defined(OPENSSL_SYS_STARBOARD) { - fp->fd = open((const char *)ptr, SbFileModeStringToFlags(p), + fp->fd = open((const char *)ptr, FileModeStringToFlags(p), S_IRUSR | S_IWUSR); if (fp->fd < 0) { OPENSSL_PUT_SYSTEM_ERROR(); @@ -584,7 +584,7 @@ static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr) { break; case BIO_CTRL_FLUSH: #if defined(OPENSSL_SYS_STARBOARD) - fsync((FilePtr)b->ptr); + fsync(((FilePtr)b->ptr)->fd); #else // defined(OPENSSL_SYS_STARBOARD) #ifndef NATIVE_TARGET_BUILD if (b->flags & BIO_FLAGS_UPLINK) @@ -616,15 +616,15 @@ static int MS_CALLBACK file_gets(BIO *bp, char *buf, int size) { #if defined(OPENSSL_SYS_STARBOARD) ret = -1; struct stat info; - fstat((FilePtr)bp->ptr, &info); - int64_t current = lseek((FilePtr)bp->ptr, SEEK_CUR, 0); + fstat(((FilePtr)bp->ptr)->fd, &info); + int64_t current = lseek(((FilePtr)bp->ptr)->fd, SEEK_CUR, 0); int64_t remaining = info.st_size - current; int64_t max = (size > remaining ? remaining : size - 1); int index = 0; for (; index < max; ++index) { int count = 0; for (;;) { - count = read((FilePtr)bp->ptr, buf + index, 1); + count = read(((FilePtr)bp->ptr)->fd, buf + index, 1); if (count == 0) { continue; } diff --git a/third_party/freetype/BUILD.gn b/third_party/freetype/BUILD.gn index 95e4c8a90a3c..95947c6f6c57 100644 --- a/third_party/freetype/BUILD.gn +++ b/third_party/freetype/BUILD.gn @@ -153,6 +153,7 @@ source_set("freetype_source") { public_configs = [ ":freetype_config" ] deps = [ + "//starboard/common:file_wrapper", "//third_party/libpng", "//third_party/zlib", ] diff --git a/third_party/freetype/src/include/freetype/config/ftstdlib.h b/third_party/freetype/src/include/freetype/config/ftstdlib.h index 7cf7de677006..239858c80ec6 100644 --- a/third_party/freetype/src/include/freetype/config/ftstdlib.h +++ b/third_party/freetype/src/include/freetype/config/ftstdlib.h @@ -110,20 +110,18 @@ #if defined( STARBOARD ) -#include "starboard/file.h" +#include +#include + +#include "starboard/common/file_wrapper.h" #include "starboard/string.h" -#ifndef SEEK_SET -#define SEEK_SET kSbFileFromBegin -#define SEEK_CUR kSbFileFromCurrent -#define SEEK_END kSbFileFromEnd -#endif -#define FT_FILE SbFilePrivate -#define ft_fclose SbFileClose -#define ft_fopen(p, m) SbFileOpen((p), SbFileModeStringToFlags(m), NULL, NULL) -#define ft_fread(b, s, n, f) SbFileRead((f), (char *)(b), (s) * (n)) -#define ft_fseek(f, o, w) SbFileSeek((f), (w), (o)) -#define ft_ftell(f) SbFileSeek((f), kSbFileFromCurrent, 0) +#define FT_FILE FileStruct +#define ft_fclose file_close +#define ft_fopen(p, m) file_open((p), FileModeStringToFlags(m)) +#define ft_fread(b, s, n, f) read((f->fd), (char *)(b), (s) * (n)) +#define ft_fseek(f, o, w) lseek((f->fd), (o), (w)) +#define ft_ftell(f) lseek((f->fd), 0, SEEK_CUR) #define ft_sprintf sprintf #else #include diff --git a/third_party/googletest/src/googletest/include/gtest/internal/gtest-port.h b/third_party/googletest/src/googletest/include/gtest/internal/gtest-port.h index 1acc6292ec3b..f3ac2e6a6f0e 100644 --- a/third_party/googletest/src/googletest/include/gtest/internal/gtest-port.h +++ b/third_party/googletest/src/googletest/include/gtest/internal/gtest-port.h @@ -286,7 +286,7 @@ #if defined __APPLE__ #include #include -#endif +#endif // defined __APPLE__ #include // NOLINT #include @@ -303,6 +303,7 @@ #include #include +#include #include "gtest/internal/custom/gtest-port.h" #include "gtest/internal/gtest-port-arch.h" @@ -2086,7 +2087,7 @@ inline int StrCaseCmp(const char* s1, const char* s2) { #endif //SB_API_VERSION < 16 inline char* StrDup(const char* src) { return strdup(src); } -inline int RmDir(const char* dir) { return SbFileDelete(dir); } +inline int RmDir(const char* dir) { return rmdir(dir); } inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); } inline const char* StrNCpy(char* dest, const char* src, size_t n) { diff --git a/third_party/skia/src/gpu/text/GrAtlasManager.cpp b/third_party/skia/src/gpu/text/GrAtlasManager.cpp index f2b421a1c56b..b40321a4571d 100644 --- a/third_party/skia/src/gpu/text/GrAtlasManager.cpp +++ b/third_party/skia/src/gpu/text/GrAtlasManager.cpp @@ -14,7 +14,7 @@ #include "src/gpu/text/GrStrikeCache.h" #if defined(STARBOARD) -#include "starboard/file.h" +#include #endif GrAtlasManager::GrAtlasManager(GrProxyProvider* proxyProvider, @@ -254,7 +254,7 @@ static bool save_pixels(GrDirectContext* dContext, GrSurfaceProxyView view, GrCo // remove any previous version of this file remove(filename); #else - SbFileDelete(filename); + unlink(filename); #endif SkFILEWStream file(filename); @@ -263,7 +263,7 @@ static bool save_pixels(GrDirectContext* dContext, GrSurfaceProxyView view, GrCo #if !defined(STARBOARD) remove(filename); // remove any partial file #else - SbFileDelete(filename); + unlink(filename); #endif return false; } @@ -273,7 +273,7 @@ static bool save_pixels(GrDirectContext* dContext, GrSurfaceProxyView view, GrCo #if !defined(STARBOARD) remove(filename); // remove any partial file #else - SbFileDelete(filename); + unlink(filename); #endif return false; } diff --git a/third_party/zlib/BUILD.gn b/third_party/zlib/BUILD.gn index 39b801cc5666..187dc3c90c09 100644 --- a/third_party/zlib/BUILD.gn +++ b/third_party/zlib/BUILD.gn @@ -161,7 +161,7 @@ if (use_arm_neon_optimizations) { configs += [ "//starboard/build/config:speed" ] } if (is_starboard) { - deps = [ "//starboard/common:common" ] + deps = [ "//starboard:starboard_headers_only" ] } if (is_starboard && sb_is_modular && target_cpu == "arm" && @@ -481,7 +481,7 @@ static_library("minizip") { "contrib/minizip/iostarboard.c", "contrib/minizip/iostarboard.h", ] - deps += [ "//starboard/common:common" ] + deps += [ "//starboard/common:file_wrapper" ] } else { configs -= [ "//build/config/compiler:chromium_code" ] } diff --git a/third_party/zlib/contrib/minizip/iostarboard.c b/third_party/zlib/contrib/minizip/iostarboard.c index fb11d8af1231..7cbecfb231b3 100644 --- a/third_party/zlib/contrib/minizip/iostarboard.c +++ b/third_party/zlib/contrib/minizip/iostarboard.c @@ -14,9 +14,12 @@ // Starboard IO base function header for compress/uncompress .zip +#include +#include + #include "zlib.h" #include "iostarboard.h" -#include "starboard/file.h" +#include "starboard/common/file_wrapper.h" static voidpf ZCALLBACK starboard_open_file_func OF((voidpf opaque, const char* filename, int mode)); static voidpf ZCALLBACK starboard_open64_file_func OF((voidpf opaque, const void* filename, int mode)); @@ -28,47 +31,47 @@ static int ZCALLBACK starboard_close_file_func OF((voidpf opaque, voidpf str static int ZCALLBACK starboard_error_file_func OF((voidpf opaque, voidpf stream)); static voidpf ZCALLBACK starboard_open_file_func (voidpf opaque, const char* filename, int mode) { - SbFile file = NULL; int flags = 0; if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ) { - flags = kSbFileRead | kSbFileOpenOnly; + flags = O_RDONLY; } else if (mode & ZLIB_FILEFUNC_MODE_EXISTING){ - flags = kSbFileRead | kSbFileWrite | kSbFileOpenOnly; + flags = O_RDWR; } else if (mode & ZLIB_FILEFUNC_MODE_CREATE) { - flags = kSbFileRead | kSbFileWrite | kSbFileCreateAlways; + flags = O_RDWR | O_CREAT | O_TRUNC; } - if ((filename!=NULL) && (flags != 0)) { - file = SbFileOpen(filename, flags, NULL, NULL); + FilePtr file = NULL; + if (filename != NULL) { + file = file_open((const char*)filename, flags); } return file; } static voidpf ZCALLBACK starboard_open64_file_func (voidpf opaque, const void* filename, int mode) { - SbFile file = NULL; int flags = 0; if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ) { - flags = kSbFileRead | kSbFileOpenOnly; + flags = O_RDONLY; } else if (mode & ZLIB_FILEFUNC_MODE_EXISTING){ - flags = kSbFileRead | kSbFileWrite | kSbFileOpenOnly; + flags = O_RDWR; } else if (mode & ZLIB_FILEFUNC_MODE_CREATE) { - flags = kSbFileRead | kSbFileWrite | kSbFileCreateAlways; + flags = O_RDWR | O_CREAT | O_TRUNC; } - if ((filename!=NULL) && (flags != 0)) { - file = SbFileOpen((const char*)filename, flags, NULL, NULL); + FilePtr file = NULL; + if (filename != NULL) { + file = file_open((const char*)filename, flags); } return file; } static uLong ZCALLBACK starboard_read_file_func (voidpf opaque, voidpf stream, void* buf, uLong size) { uLong ret = 0; - SbFile file = NULL; + FilePtr file = NULL; if (stream != NULL) { - file = (SbFile)stream; + file = (FilePtr)stream; } if (file != NULL) { - int bytes_read = SbFileRead(file, (char*)buf, (int)size); + int bytes_read = read(file->fd, (char*)buf, (int)size); ret = (uLong)(bytes_read == -1 ? 0 : bytes_read); } return ret; @@ -76,12 +79,12 @@ static uLong ZCALLBACK starboard_read_file_func (voidpf opaque, voidpf stream, v static uLong ZCALLBACK starboard_write_file_func (voidpf opaque, voidpf stream, const void* buf, uLong size) { uLong ret = 0; - SbFile file = NULL; + FilePtr file = NULL; if (stream != NULL) { - file = (SbFile)stream; + file = (FilePtr)stream; } if (file != NULL) { - int bytes_written = SbFileWrite(file, (const char*)buf, (int)size); + int bytes_written = write(file->fd, (const char*)buf, (int)size); ret = (uLong)(bytes_written == -1 ? 0 : bytes_written); } return ret; @@ -89,51 +92,51 @@ static uLong ZCALLBACK starboard_write_file_func (voidpf opaque, voidpf stream, static long ZCALLBACK starboard_tell_file_func (voidpf opaque, voidpf stream) { long ret = -1; - SbFile file = NULL; + FilePtr file = NULL; if (stream != NULL) { - file = (SbFile)stream; + file = (FilePtr)stream; } if (file != NULL) { - ret = SbFileSeek(file, kSbFileFromCurrent, 0); + ret = lseek(file->fd, 0, SEEK_CUR); } return ret; } static ZPOS64_T ZCALLBACK starboard_tell64_file_func (voidpf opaque, voidpf stream) { ZPOS64_T ret = -1; - SbFile file = NULL; + FilePtr file = NULL; if (stream != NULL) { - file = (SbFile)stream; + file = (FilePtr)stream; } if (file != NULL) { - ret = (ZPOS64_T)SbFileSeek(file, kSbFileFromCurrent, 0); + ret = (ZPOS64_T)lseek(file->fd, 0, SEEK_CUR); } return ret; } static long ZCALLBACK starboard_seek_file_func (voidpf opaque, voidpf stream, uLong offset, int origin) { long ret = -1; - SbFile file = NULL; - SbFileWhence file_whence = 0; + FilePtr file = NULL; + int file_whence = 0; if (stream != NULL) { - file = (SbFile)stream; + file = (FilePtr)stream; } switch (origin) { case ZLIB_FILEFUNC_SEEK_CUR : - file_whence = kSbFileFromCurrent; + file_whence = SEEK_CUR; break; case ZLIB_FILEFUNC_SEEK_END : - file_whence = kSbFileFromEnd; + file_whence = SEEK_END; break; case ZLIB_FILEFUNC_SEEK_SET : - file_whence = kSbFileFromBegin; + file_whence = SEEK_SET; break; default: return -1; } if (file != NULL) { - if (SbFileSeek(file, file_whence, (int64_t)offset) != -1) { + if (lseek(file->fd, (int64_t)offset, file_whence) != -1) { ret = 0; } } @@ -142,27 +145,27 @@ static long ZCALLBACK starboard_seek_file_func (voidpf opaque, voidpf stream, uL static long ZCALLBACK starboard_seek64_file_func (voidpf opaque, voidpf stream, ZPOS64_T offset, int origin) { long ret = -1; - SbFile file = NULL; - SbFileWhence file_whence = 0; + FilePtr file = NULL; + int file_whence = 0; if (stream != NULL) { - file = (SbFile)stream; + file = (FilePtr)stream; } switch (origin) { case ZLIB_FILEFUNC_SEEK_CUR : - file_whence = kSbFileFromCurrent; + file_whence = SEEK_CUR; break; case ZLIB_FILEFUNC_SEEK_END : - file_whence = kSbFileFromEnd; + file_whence = SEEK_END; break; case ZLIB_FILEFUNC_SEEK_SET : - file_whence = kSbFileFromBegin; + file_whence = SEEK_SET; break; default: return -1; } if (file != NULL) { - if (SbFileSeek(file, file_whence, (int64_t)offset) != -1) { + if (lseek(file->fd, (int64_t)offset, file_whence) != -1) { ret = 0; } } @@ -171,11 +174,11 @@ static long ZCALLBACK starboard_seek64_file_func (voidpf opaque, voidpf stream, static int ZCALLBACK starboard_close_file_func (voidpf opaque, voidpf stream) { int ret = -1; - SbFile file = NULL; + FilePtr file = NULL; if (stream != NULL) { - file = (SbFile)stream; + file = (FilePtr)stream; } - if (file != NULL && SbFileClose(file)) { + if (file != NULL && !file_close(file)) { ret = 0; } return ret; @@ -186,9 +189,9 @@ static int ZCALLBACK starboard_error_file_func (voidpf opaque, voidpf stream) { // the file error code in SbFileOpen, but zlib uses this function to get the file error code // only after reading a file (SbFileRead), which doesn't set the error code anyways. int ret = -1; - SbFile file = NULL; + FilePtr file = NULL; if (stream != NULL) { - file = (SbFile)stream; + file = (FilePtr)stream; } if (file != NULL) { ret = 0;