diff --git a/base/files/file_enumerator_starboard.cc b/base/files/file_enumerator_starboard.cc index bec49f41f724..a4ebb01e4f9a 100644 --- a/base/files/file_enumerator_starboard.cc +++ b/base/files/file_enumerator_starboard.cc @@ -131,7 +131,7 @@ std::vector FileEnumerator::ReadDirectory( FilePath full_name = source.Append(filename); // TODO: Make sure this follows symlinks on relevant platforms. if (stat(full_name.value().c_str(), &info.stat_) != 0) { - DPLOG(ERROR) << "Couldn't SbFileGetInfo on " << full_name.value(); + DPLOG(ERROR) << "Couldn't stat on " << full_name.value(); memset(&info.stat_, 0, sizeof(info.stat_)); } return info; diff --git a/base/files/file_util_starboard.cc b/base/files/file_util_starboard.cc index fbd9eaa34744..bcec62a16edd 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" @@ -248,12 +248,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 e108d495b346..a8054f90e262 100644 --- a/cobalt/renderer/rasterizer/skia/skia/src/ports/SkOSFile_cobalt.cc +++ b/cobalt/renderer/rasterizer/skia/skia/src/ports/SkOSFile_cobalt.cc @@ -7,6 +7,9 @@ #include "cobalt/renderer/rasterizer/skia/skia/src/ports/SkOSFile_cobalt.h" +#include +#include + #include "SkString.h" #include "SkTFitsIn.h" #include "SkTemplates.h" @@ -16,6 +19,8 @@ #include "base/files/file_util.h" #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 @@ -23,25 +28,25 @@ namespace { -SbFile ToSbFile(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(SbFile starboard_file) { +FILE* ToFILE(FilePtr starboard_file) { return reinterpret_cast(starboard_file); } -int ToSbFileFlags(SkFILE_Flags sk_flags) { +int ToFileFlags(SkFILE_Flags sk_flags) { int flags = 0; if (sk_flags & kRead_SkFILE_Flag) { if (sk_flags & kWrite_SkFILE_Flag) { - flags |= kSbFileWrite; + flags |= O_WRONLY; } - flags |= kSbFileOpenOnly | kSbFileRead; + flags |= O_RDONLY; } else if (sk_flags & kWrite_SkFILE_Flag) { - flags |= kSbFileOpenAlways | kSbFileWrite; + flags |= O_CREAT | O_WRONLY; } return flags; } @@ -49,68 +54,66 @@ int ToSbFileFlags(SkFILE_Flags sk_flags) { } // namespace FILE* sk_fopen(const char path[], SkFILE_Flags sk_flags) { - SbFile starboard_file = SbFileOpen(path, ToSbFileFlags(sk_flags), NULL, NULL); - // TODO: temporarily replace with kSbFileInvalid, will be deprecated with - // SBFile. - if (starboard_file == kSbFileInvalid) { + FilePtr file = file_open(path, ToFileFlags(sk_flags)); + if (!file || file->fd < 0) { return nullptr; } - return ToFILE(starboard_file); + return ToFILE(file); } void sk_fclose(FILE* sk_file) { SkASSERT(sk_file); - SbFileClose(ToSbFile(sk_file)); + int ret = file_close(ToFilePtr(sk_file)); } size_t sk_fgetsize(FILE* sk_file) { SkASSERT(sk_file); - SbFile file = ToSbFile(sk_file); + FilePtr file = ToFilePtr(sk_file); // Save current position so we can restore it. - int64_t current_position = SbFileSeek(file, kSbFileFromCurrent, 0); + int64_t current_position = lseek(file->fd, 0, SEEK_CUR); if (current_position < 0) { return 0; } // Find the file size by seeking to the end. - int64_t size = SbFileSeek(file, kSbFileFromEnd, 0); + int64_t size = lseek(file->fd, 0, SEEK_END); if (size < 0) { size = 0; } // Restore original file position. - SbFileSeek(file, kSbFileFromBegin, current_position); + lseek(file->fd, current_position, SEEK_SET); return size; } size_t sk_fwrite(const void* buffer, size_t byteCount, FILE* sk_file) { SkASSERT(sk_file); - SbFile file = ToSbFile(sk_file); + FilePtr file = ToFilePtr(sk_file); int result = - SbFileWrite(file, reinterpret_cast(buffer), byteCount); + write(file->fd, reinterpret_cast(buffer), byteCount); base::RecordFileWriteStat(result); return result; } void sk_fflush(FILE* sk_file) { SkASSERT(sk_file); - SbFile file = ToSbFile(sk_file); - SbFileFlush(file); + FilePtr file = ToFilePtr(sk_file); + fsync(file->fd); } bool sk_fseek(FILE* sk_file, size_t position) { SkASSERT(sk_file); - SbFile file = ToSbFile(sk_file); - int64_t new_position = SbFileSeek(file, kSbFileFromBegin, position); + 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); - SbFile file = ToSbFile(sk_file); - return SbFileSeek(file, kSbFileFromCurrent, 0); + FilePtr file = ToFilePtr(sk_file); + return lseek(file->fd, 0, SEEK_CUR); } void* sk_fmmap(FILE* sk_file, size_t* length) { @@ -154,28 +157,29 @@ bool sk_mkdir(const char* path) { void sk_fsync(FILE* f) { SkASSERT(f); - SbFile file = ToSbFile(f); + FilePtr file = ToFilePtr(f); // Technically, flush doesn't have to call sync... but this is the best // effort we can make. - SbFileFlush(file); + fsync(file->fd); } size_t sk_qread(FILE* file, void* buffer, size_t count, size_t offset) { SkASSERT(file); - SbFile starboard_file = ToSbFile(file); + FilePtr starboard_file = ToFilePtr(file); - int original_position = SbFileSeek(starboard_file, kSbFileFromCurrent, 0); + int original_position = lseek(starboard_file->fd, 0, SEEK_CUR); if (original_position < 0) { return SIZE_MAX; } - int position = SbFileSeek(starboard_file, kSbFileFromBegin, offset); + int position = lseek(starboard_file->fd, offset, SEEK_SET); int result = 0; if (position == offset) { - result = SbFileReadAll(starboard_file, reinterpret_cast(buffer), + result = + starboard::ReadAll(starboard_file->fd, reinterpret_cast(buffer), static_cast(count)); } - position = SbFileSeek(starboard_file, kSbFileFromBegin, original_position); + position = lseek(starboard_file->fd, original_position, SEEK_SET); if (result < 0 || position < 0) { return SIZE_MAX; } else { @@ -185,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); - SbFile starboard_file = ToSbFile(file); - return SbFileReadAll(starboard_file, reinterpret_cast(buffer), - byteCount); + 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/android_main.cc b/starboard/android/shared/android_main.cc index e9836570a25f..af76d2dc9977 100644 --- a/starboard/android/shared/android_main.cc +++ b/starboard/android/shared/android_main.cc @@ -13,7 +13,9 @@ // limitations under the License. #include +#include #include +#include #include "game-activity/GameActivity.h" #include "starboard/android/shared/application_android.h" @@ -119,52 +121,49 @@ bool CopyDirContents(const std::string& src_dir_path, std::string filename(filename_buffer.begin(), filename_buffer.end()); std::string path_to_src_file = src_dir_path + kSbFileSepString + filename; - SbFile src_file = - SbFileOpen(path_to_src_file.c_str(), kSbFileOpenOnly | kSbFileRead, - nullptr, nullptr); - if (src_file == kSbFileInvalid) { + int src_file = open(path_to_src_file.c_str(), O_RDONLY, S_IRUSR | S_IWUSR); + if (!IsValid(src_file)) { SB_LOG(WARNING) << "Failed to open file=" << path_to_src_file; return false; } - SbFileInfo info; - if (!SbFileGetInfo(src_file, &info)) { + struct stat info; + if (fstat(src_file, &info)) { SB_LOG(WARNING) << "Failed to get info for file=" << path_to_src_file; - SbFileClose(src_file); + close(src_file); return false; } - int file_size = static_cast(info.size); + int file_size = static_cast(info.st_size); // Read in bytes from src file char file_contents_buffer[file_size]; - int read = SbFileReadAll(src_file, file_contents_buffer, file_size); + int read = ReadAll(src_file, file_contents_buffer, file_size); if (read == -1) { - SB_LOG(WARNING) << "SbFileReadAll failed for file=" << path_to_src_file; + SB_LOG(WARNING) << "ReadAll failed for file=" << path_to_src_file; return false; } const std::string file_contents = std::string(file_contents_buffer, file_size); - SbFileClose(src_file); + close(src_file); // Write bytes out to dst file std::string path_to_dst_file = dst_dir_path; path_to_dst_file.append(kSbFileSepString); path_to_dst_file.append(filename); - SbFile dst_file = - SbFileOpen(path_to_dst_file.c_str(), kSbFileCreateAlways | kSbFileWrite, - NULL, NULL); - if (dst_file == kSbFileInvalid) { + int dst_file = open(path_to_dst_file.c_str(), O_CREAT | O_TRUNC | O_WRONLY, + S_IRUSR | S_IWUSR); + if (!IsValid(dst_file)) { SB_LOG(WARNING) << "Failed to open file=" << path_to_dst_file; return false; } - int wrote = SbFileWriteAll(dst_file, file_contents.c_str(), file_size); + int wrote = WriteAll(dst_file, file_contents.c_str(), file_size); RecordFileWriteStat(wrote); if (wrote == -1) { - SB_LOG(WARNING) << "SbFileWriteAll failed for file=" << path_to_dst_file; + SB_LOG(WARNING) << "WriteAll failed for file=" << path_to_dst_file; return false; } - SbFileClose(dst_file); + close(dst_file); } closedir(src_dir); diff --git a/starboard/android/shared/asset_manager.cc b/starboard/android/shared/asset_manager.cc index a386544390db..201b956e7ba6 100644 --- a/starboard/android/shared/asset_manager.cc +++ b/starboard/android/shared/asset_manager.cc @@ -15,6 +15,7 @@ #include "starboard/android/shared/asset_manager.h" #include +#include #include #include #include @@ -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); @@ -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; } diff --git a/starboard/android/shared/asset_manager.h b/starboard/android/shared/asset_manager.h index 10f1a8b5d526..8be4cf8fe90d 100644 --- a/starboard/android/shared/asset_manager.h +++ b/starboard/android/shared/asset_manager.h @@ -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; diff --git a/starboard/android/shared/file_can_open.cc b/starboard/android/shared/file_can_open.cc index 5aeb5fd1ed00..db7155fe3d2f 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 < 17 + #include "starboard/file.h" #include @@ -41,3 +43,5 @@ bool SbFileCanOpen(const char* path, int flags) { return result; } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/android/shared/file_close.cc b/starboard/android/shared/file_close.cc index 192aedec0f4f..b44eeca4ae62 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 < 17 + #include "starboard/file.h" #include @@ -28,3 +30,5 @@ bool SbFileClose(SbFile file) { return ::starboard::shared::posix::impl::FileClose(file); } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/android/shared/file_delete.cc b/starboard/android/shared/file_delete.cc index 9a0ab8b2c8ed..477b6ecc4bf0 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 < 17 + #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 < 17 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..7ab036bb0f5c 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 < 17 + #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 < 17 diff --git a/starboard/android/shared/file_get_info.cc b/starboard/android/shared/file_get_info.cc index d7e755952cfc..c7979fbee19a 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 < 17 + #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 < 17 diff --git a/starboard/android/shared/file_open.cc b/starboard/android/shared/file_open.cc index 8d82ab584359..8102b58f32d8 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 < 17 + #include "starboard/file.h" #include @@ -104,3 +106,5 @@ SbFile SbFileOpen(const char* path, } return kSbFileInvalid; } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/android/shared/file_read.cc b/starboard/android/shared/file_read.cc index d283019f9813..2d90d4f8d5b3 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 < 17 + #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 < 17 diff --git a/starboard/android/shared/file_seek.cc b/starboard/android/shared/file_seek.cc index 590446b92e92..e64f7fc5b6e9 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 < 17 + #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 < 17 diff --git a/starboard/android/shared/file_truncate.cc b/starboard/android/shared/file_truncate.cc index c1075e789323..fe96b632a924 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 < 17 + #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 < 17 diff --git a/starboard/android/shared/file_write.cc b/starboard/android/shared/file_write.cc index 77054354de06..e6c99cb85515 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 < 17 + #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 < 17 diff --git a/starboard/android/shared/posix_emu/file.cc b/starboard/android/shared/posix_emu/file.cc index d18ebefb3997..6f876e5856d9 100644 --- a/starboard/android/shared/posix_emu/file.cc +++ b/starboard/android/shared/posix_emu/file.cc @@ -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" diff --git a/starboard/android/shared/posix_emu/stat.cc b/starboard/android/shared/posix_emu/stat.cc index 5f7864c6ab33..572fca2f2dbc 100644 --- a/starboard/android/shared/posix_emu/stat.cc +++ b/starboard/android/shared/posix_emu/stat.cc @@ -12,12 +12,15 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include +#include #include #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; @@ -41,20 +44,6 @@ static SB_C_FORCE_INLINE time_t WindowsUsecToTimeTAndroid(int64_t time) { return posix_time; } -static void MapSbFileInfoToStat(SbFileInfo* file_info, struct stat* stat_info) { - stat_info->st_mode = 0; - if (file_info->is_directory) { - stat_info->st_mode = S_IFDIR; - } else if (file_info->is_symbolic_link) { - stat_info->st_mode = S_IFLNK; - } - - stat_info->st_ctime = WindowsUsecToTimeTAndroid(file_info->creation_time); - stat_info->st_atime = WindowsUsecToTimeTAndroid(file_info->last_accessed); - stat_info->st_mtime = WindowsUsecToTimeTAndroid(file_info->last_modified); - stat_info->st_size = file_info->size; -} - // This needs to be exported to ensure shared_library targets include it. int __wrap_stat(const char* path, struct stat* info) { // SbFileExists(path) implementation for Android @@ -62,13 +51,11 @@ int __wrap_stat(const char* path, struct stat* info) { return __real_stat(path, info); // Using system level stat call } - SbFile file = SbFileOpen(path, kSbFileRead | kSbFileOpenOnly, NULL, NULL); - SbFileInfo out_info; - if (file) { - bool result = SbFileGetInfo(file, &out_info); - MapSbFileInfoToStat(&out_info, info); - SbFileClose(file); - return 0; + int file = open(path, O_RDONLY, S_IRUSR | S_IWUSR); + if (file >= 0) { + int result = fstat(file, info); + close(file); + return result; } // Values from SbFileGetPathInfo diff --git a/starboard/common/BUILD.gn b/starboard/common/BUILD.gn index bbf938f8173b..efe6da76e6fe 100644 --- a/starboard/common/BUILD.gn +++ b/starboard/common/BUILD.gn @@ -94,6 +94,14 @@ static_library("common") { ] } +static_library("file_wrapper") { + check_includes = false + sources = [ + "file_wrapper.cc", + "file_wrapper.h", + ] +} + target(gtest_target_type, "common_test") { testonly = true sources = [ diff --git a/starboard/common/file.cc b/starboard/common/file.cc index 99cb273f9b02..4e5b30608c52 100644 --- a/starboard/common/file.cc +++ b/starboard/common/file.cc @@ -29,6 +29,7 @@ #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 { @@ -44,6 +45,34 @@ bool DirectoryCloseLogFailure(const char* path, DIR* 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; +} + ssize_t ReadAll(int fd, void* data, int size) { if (fd < 0 || size < 0) { return -1; @@ -85,11 +114,9 @@ bool SbFileDeleteRecursive(const char* path, bool preserve_root) { // The |path| points to a file. Remove it and return. if (!dir) { - return SbFileDelete(path); + return (unlink(path) == 0); } - SbFileInfo info; - std::vector entry(kSbFileMaxName); struct dirent dirent_buffer; @@ -120,7 +147,7 @@ bool SbFileDeleteRecursive(const char* path, bool preserve_root) { // Don't forget to close and remove the directory before returning! if (DirectoryCloseLogFailure(path, dir)) { - return preserve_root ? true : SbFileDelete(path); + return preserve_root ? true : (rmdir(path) == 0); } return false; } diff --git a/starboard/common/file.h b/starboard/common/file.h index 15b8f4227850..b599894c89ed 100644 --- a/starboard/common/file.h +++ b/starboard/common/file.h @@ -20,8 +20,6 @@ #ifndef STARBOARD_COMMON_FILE_H_ #define STARBOARD_COMMON_FILE_H_ -#include "starboard/file.h" - #include #include #include @@ -38,6 +36,10 @@ namespace starboard { +bool FileCanOpen(const char* path, int flags); + +bool IsValid(int file); + ssize_t ReadAll(int fd, void* data, int size); void RecordFileWriteStat(int write_file_result); @@ -56,9 +58,9 @@ bool SbFileDeleteRecursive(const char* path, bool preserve_root); ssize_t WriteAll(int fd, const void* data, int size); -// A class that opens an SbFile in its constructor and closes it in its +// A class that opens an file descriptor in its constructor and closes it in its // destructor, so the file is open for the lifetime of the object. Member -// functions call the corresponding SbFile function. +// functions call the corresponding file function. class ScopedFile { public: ScopedFile(const char* path, int flags, int mode) : file_(-1) { @@ -77,10 +79,10 @@ class ScopedFile { int file() const { return file_; } - bool IsValid() const { return file_ >= 0; } + bool IsValid() const { return starboard::IsValid(file_); } - int64_t Seek(SbFileWhence whence, int64_t offset) const { - return lseek(file_, static_cast(offset), static_cast(whence)); + int64_t Seek(int64_t offset, int whence) const { + return lseek(file_, static_cast(offset), whence); } int Read(char* data, int size) const { return read(file_, data, size); } diff --git a/starboard/common/file_wrapper.cc b/starboard/common/file_wrapper.cc new file mode 100644 index 000000000000..12d38064f1ae --- /dev/null +++ b/starboard/common/file_wrapper.cc @@ -0,0 +1,99 @@ +// 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 "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); + return file; +} + +} // extern "C" diff --git a/starboard/common/file_wrapper.h b/starboard/common/file_wrapper.h new file mode 100644 index 000000000000..03be8198b69e --- /dev/null +++ b/starboard/common/file_wrapper.h @@ -0,0 +1,44 @@ +// 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. + +#ifndef STARBOARD_COMMON_FILE_WRAPPER_H_ +#define STARBOARD_COMMON_FILE_WRAPPER_H_ + +typedef struct FileStruct { + int fd; +} 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 4f32cf9fba0d..e47f8ffb93a0 100644 --- a/starboard/elf_loader/exported_symbols.cc +++ b/starboard/elf_loader/exported_symbols.cc @@ -174,23 +174,29 @@ ExportedSymbols::ExportedSymbols() { REGISTER_SYMBOL(SbEventCancel); REGISTER_SYMBOL(SbEventSchedule); REGISTER_SYMBOL(SbFileAtomicReplace); +#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 < 17 REGISTER_SYMBOL(SbGetEglInterface); REGISTER_SYMBOL(SbGetGlesInterface); #if SB_API_VERSION < 16 diff --git a/starboard/elf_loader/file_impl.cc b/starboard/elf_loader/file_impl.cc index 951db5fdbc09..96a0ec81cc4c 100644 --- a/starboard/elf_loader/file_impl.cc +++ b/starboard/elf_loader/file_impl.cc @@ -14,6 +14,10 @@ #include "starboard/elf_loader/file_impl.h" +#include +#include + +#include "starboard/common/file.h" #include "starboard/common/log.h" #include "starboard/elf_loader/log.h" @@ -31,7 +35,7 @@ void LogLastError(const char* msg) { namespace starboard { namespace elf_loader { -FileImpl::FileImpl() : file_(NULL) {} +FileImpl::FileImpl() : file_(-1) {} FileImpl::~FileImpl() { Close(); @@ -40,7 +44,7 @@ FileImpl::~FileImpl() { bool FileImpl::Open(const char* name) { SB_DLOG(INFO) << "Loading: " << name; name_ = name; - file_ = SbFileOpen(name, kSbFileOpenOnly | kSbFileRead, NULL, NULL); + file_ = open(name, O_RDONLY, S_IRUSR | S_IWUSR); if (!file_) { return false; } @@ -51,17 +55,17 @@ bool FileImpl::ReadFromOffset(int64_t offset, char* buffer, int size) { if (!file_) { return false; } - int64_t ret = SbFileSeek(file_, kSbFileFromBegin, offset); - SB_DLOG(INFO) << "SbFileSeek: ret=" << ret; + int64_t ret = lseek(file_, offset, SEEK_SET); + SB_DLOG(INFO) << "lseek: ret=" << ret; if (ret == -1) { - LogLastError("SbFileSeek: failed"); + LogLastError("lseek: failed"); return false; } - int count = SbFileReadAll(file_, buffer, size); - SB_DLOG(INFO) << "SbFileReadAll: count=" << count; + int count = starboard::ReadAll(file_, buffer, size); + SB_DLOG(INFO) << "ReadAll: count=" << count; if (count == -1) { - LogLastError("SbFileReadAll failed"); + LogLastError("ReadAll failed"); return false; } return true; @@ -69,7 +73,7 @@ bool FileImpl::ReadFromOffset(int64_t offset, char* buffer, int size) { void FileImpl::Close() { if (file_) { - SbFileClose(file_); + close(file_); } } diff --git a/starboard/elf_loader/file_impl.h b/starboard/elf_loader/file_impl.h index a6a30bdc8cf2..a1a2f9525be5 100644 --- a/starboard/elf_loader/file_impl.h +++ b/starboard/elf_loader/file_impl.h @@ -35,7 +35,7 @@ class FileImpl : public File { const std::string& GetName() override; protected: - SbFile file_; + int file_; std::string name_; FileImpl(const FileImpl&) = delete; diff --git a/starboard/elf_loader/lz4_file_impl.cc b/starboard/elf_loader/lz4_file_impl.cc index 1ffbdfa921ab..9285f62ce2d5 100644 --- a/starboard/elf_loader/lz4_file_impl.cc +++ b/starboard/elf_loader/lz4_file_impl.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + #include #include @@ -73,9 +75,9 @@ bool LZ4FileImpl::Open(const char* name) { return false; } - SbFileInfo file_info; + struct stat file_info; - if (!FileImpl::Open(name) || !SbFileGetInfo(file_, &file_info)) { + if (!FileImpl::Open(name) || fstat(file_, &file_info)) { return false; } @@ -111,7 +113,7 @@ bool LZ4FileImpl::Open(const char* name) { // uncompressed block size. int max_compressed_buffer_size = GetBlockSize(&frame_info); - bool result = Decompress(file_info.size, header_size, + bool result = Decompress(file_info.st_size, header_size, max_compressed_buffer_size, source_bytes_hint); int64_t decompression_end_time_us = CurrentMonotonicTime(); diff --git a/starboard/file.h b/starboard/file.h index 9a54ef79501f..ead07597879a 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 < 17 + // 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 < 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 // 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 < 17 + // DEPRECATED with SB_API_VERSION 16 // // Changes the current read/write position in |file|. The return value @@ -264,6 +270,8 @@ 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|. // @@ -271,6 +279,8 @@ SB_EXPORT bool SbFileDelete(const char* path); 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|. @@ -346,6 +356,8 @@ static inline int SbFileWriteAll(SbFile file, const char* data, int size) { return bytes_written ? bytes_written : rv; } +#endif // SB_API_VERSION < 17 + #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 cc4c57c3daec..be45051d9d1f 100644 --- a/starboard/loader_app/drain_file.cc +++ b/starboard/loader_app/drain_file.cc @@ -15,6 +15,8 @@ #include "starboard/loader_app/drain_file.h" #include +#include +#include #include #include @@ -176,12 +178,10 @@ bool TryDrain(const char* dir, const char* app_key) { path.append(kSbFileSepString); path.append(filename); - SbFileError error = kSbFileOk; - SbFile file = SbFileOpen(path.c_str(), kSbFileCreateAlways | kSbFileWrite, - NULL, &error); + int file = open(path.c_str(), O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); - SB_DCHECK(error == kSbFileOk); - SB_DCHECK(SbFileClose(file)); + SB_DCHECK(file >= 0); + SB_DCHECK(close(file) == 0); SB_LOG(INFO) << "Created drain file at '" << path << "'"; @@ -208,7 +208,7 @@ void ClearExpired(const char* dir) { continue; } const std::string path = dir + std::string(kSbFileSepString) + filename; - if (!SbFileDelete(path.c_str())) { + if (unlink(path.c_str())) { SB_LOG(ERROR) << "Failed to remove expired drain file at '" << path << "'"; } @@ -225,7 +225,7 @@ void ClearForApp(const char* dir, const char* app_key) { for (const auto& filename : filenames) { const std::string path = dir + std::string(kSbFileSepString) + filename; - if (!SbFileDelete(path.c_str())) { + if (unlink(path.c_str())) { SB_LOG(ERROR) << "Failed to remove 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/loader_app/installation_manager.cc b/starboard/loader_app/installation_manager.cc index e8ad17169043..73ef2cf2f3b6 100644 --- a/starboard/loader_app/installation_manager.cc +++ b/starboard/loader_app/installation_manager.cc @@ -14,7 +14,9 @@ #include "starboard/loader_app/installation_manager.h" +#include #include +#include #include #include @@ -28,7 +30,6 @@ #include "starboard/configuration_constants.h" #include "starboard/directory.h" #include "starboard/extension/loader_app_metrics.h" -#include "starboard/file.h" #include "starboard/loader_app/installation_store.pb.h" #if !SB_IS(EVERGREEN_COMPATIBLE_LITE) #include "starboard/loader_app/pending_restart.h" // nogncheck @@ -657,28 +658,27 @@ void InstallationManager::ValidatePriorities() { } bool InstallationManager::LoadInstallationStore() { - SbFile file; + int file; SB_LOG(INFO) << "StorePath=" << store_path_; - file = SbFileOpen(store_path_.c_str(), kSbFileOpenOnly | kSbFileRead, NULL, - NULL); - if (!file) { + file = open(store_path_.c_str(), O_RDONLY, S_IRUSR | S_IWUSR); + if (!starboard::IsValid(file)) { SB_LOG(WARNING) << "Failed to open file: " << store_path_; return false; } char buf[IM_MAX_INSTALLATION_STORE_SIZE]; - int count = SbFileReadAll(file, buf, IM_MAX_INSTALLATION_STORE_SIZE); - SB_DLOG(INFO) << "SbFileReadAll: count=" << count; + int count = starboard::ReadAll(file, buf, IM_MAX_INSTALLATION_STORE_SIZE); + SB_DLOG(INFO) << "ReadAll: count=" << count; if (count == -1) { - LogLastSystemError("SbFileReadAll failed"); + LogLastSystemError("ReadAll failed"); return false; } if (!installation_store_.ParseFromArray(buf, count)) { SB_LOG(ERROR) << "LoadInstallationStore: Unable to parse storage"; return false; } - SbFileClose(file); + close(file); return true; } diff --git a/starboard/loader_app/installation_manager_test.cc b/starboard/loader_app/installation_manager_test.cc index 1a3564a5c24d..daec5c62b0a5 100644 --- a/starboard/loader_app/installation_manager_test.cc +++ b/starboard/loader_app/installation_manager_test.cc @@ -15,7 +15,9 @@ #include "starboard/loader_app/installation_manager.h" #include +#include #include +#include #include #include @@ -71,18 +73,17 @@ class InstallationManagerTest : public ::testing::TestWithParam { } void ReadStorageState(cobalt::loader::InstallationStore* installation_store) { - SbFile file; + int file; - file = SbFileOpen(installation_store_path_.c_str(), - kSbFileOpenOnly | kSbFileRead, NULL, NULL); - ASSERT_TRUE(file); + file = open(installation_store_path_.c_str(), O_RDONLY, S_IRUSR | S_IWUSR); + ASSERT_TRUE(file >= 0); char buf[IM_MAX_INSTALLATION_STORE_SIZE]; - int count = SbFileReadAll(file, buf, IM_MAX_INSTALLATION_STORE_SIZE); - SB_DLOG(INFO) << "SbFileReadAll: count=" << count; + int count = starboard::ReadAll(file, buf, IM_MAX_INSTALLATION_STORE_SIZE); + SB_DLOG(INFO) << "ReadAll: count=" << count; ASSERT_NE(-1, count); ASSERT_TRUE(installation_store->ParseFromArray(buf, count)); - SbFileClose(file); + close(file); } // Roll forward to |index| installation in a |max_num_installations| @@ -200,11 +201,11 @@ class InstallationManagerTest : public ::testing::TestWithParam { std::string full_path = storage_path_; full_path += kSbFileSepString; full_path += dir_entry.data(); - SbFileDelete(full_path.c_str()); + unlink(full_path.c_str()); } closedir(directory); - SbFileDelete(storage_path_.c_str()); + rmdir(storage_path_.c_str()); } protected: diff --git a/starboard/loader_app/slot_management_test.cc b/starboard/loader_app/slot_management_test.cc index e867175d2c1e..8b98ed9c22de 100644 --- a/starboard/loader_app/slot_management_test.cc +++ b/starboard/loader_app/slot_management_test.cc @@ -14,8 +14,10 @@ #include "starboard/loader_app/slot_management.h" +#include #include #include +#include #include #include @@ -244,10 +246,9 @@ class SlotManagementTest : public testing::TestWithParam { path += kSbFileSepString; path += "libcobalt"; AddFileExtension(path); - SbFile sb_file = SbFileOpen(path.c_str(), kSbFileOpenAlways | kSbFileRead, - nullptr, nullptr); - EXPECT_TRUE(SbFileIsValid(sb_file)); - SbFileClose(sb_file); + int sb_file = open(path.c_str(), O_CREAT | O_RDONLY); + EXPECT_TRUE(starboard::IsValid(sb_file)); + close(sb_file); return !top_created_dir.empty() ? top_created_dir : path; } diff --git a/starboard/nplb/BUILD.gn b/starboard/nplb/BUILD.gn index ed65f5e9cfcd..c1a8f8401fd2 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", @@ -298,6 +300,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..93572606c15e 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 < 17 + #include #include "starboard/configuration_constants.h" @@ -70,3 +72,5 @@ TEST(SbFileCanOpenTest, ExistingStaticContentFileSucceeds) { } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 17 diff --git a/starboard/nplb/file_close_test.cc b/starboard/nplb/file_close_test.cc index 0c4c5a1c08a8..2a40d4274b36 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 < 17 + #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 < 17 diff --git a/starboard/nplb/file_delete_recursive_test.cc b/starboard/nplb/file_delete_recursive_test.cc index 29e8774aaa86..3ed6d0e69792 100644 --- a/starboard/nplb/file_delete_recursive_test.cc +++ b/starboard/nplb/file_delete_recursive_test.cc @@ -69,8 +69,7 @@ TEST(SbFileDeleteRecursiveTest, SunnyDayDeleteExistingPath) { EXPECT_TRUE(DirectoryExists(path.c_str())); } - SbFileError err = kSbFileOk; - SbFile file = kSbFileInvalid; + int file = -1; // Create files in our directory tree. for (size_t i = 0; i < kFileCount; ++i) { @@ -78,11 +77,10 @@ TEST(SbFileDeleteRecursiveTest, SunnyDayDeleteExistingPath) { EXPECT_FALSE(FileExists(path.c_str())); - file = SbFileOpen(path.c_str(), kSbFileCreateAlways | kSbFileWrite, NULL, - &err); + file = open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR); - EXPECT_EQ(kSbFileOk, err); - EXPECT_TRUE(SbFileClose(file)); + EXPECT_TRUE(file >= 0); + EXPECT_EQ(close(file), 0); EXPECT_TRUE(FileExists(path.c_str())); } @@ -110,14 +108,12 @@ TEST(SbFileDeleteRecursiveTest, RainyDayDeleteFileIgnoresPreserveRoot) { EXPECT_FALSE(FileExists(path.c_str())); - SbFileError err = kSbFileOk; - SbFile file = kSbFileInvalid; + int file = -1; - file = - SbFileOpen(path.c_str(), kSbFileCreateAlways | kSbFileWrite, NULL, &err); + file = open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR); - EXPECT_EQ(kSbFileOk, err); - EXPECT_TRUE(SbFileClose(file)); + EXPECT_TRUE(file >= 0); + EXPECT_EQ(close(file), 0); EXPECT_TRUE(FileExists(path.c_str())); EXPECT_TRUE(SbFileDeleteRecursive(path.c_str(), true)); EXPECT_FALSE(FileExists(path.c_str())); diff --git a/starboard/nplb/file_delete_test.cc b/starboard/nplb/file_delete_test.cc index 92fbe199dffd..95052867700d 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 < 17 + #include #include @@ -63,3 +65,5 @@ TEST(SbFileDeleteTest, RainyDayNonExistentFileErrors) { } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 17 diff --git a/starboard/nplb/file_flush_test.cc b/starboard/nplb/file_flush_test.cc index d17795f912b3..7b8b3143ccbd 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 < 17 + #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 < 17 diff --git a/starboard/nplb/file_get_info_test.cc b/starboard/nplb/file_get_info_test.cc index 6f531b39c8ba..2bfc5da8120a 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 < 17 + #include #include "starboard/common/time.h" @@ -87,3 +89,5 @@ TEST(SbFileGetInfoTest, WorksOnStaticContentFiles) { } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 17 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 cc6c48f86870..9efdf3a2163e 100644 --- a/starboard/nplb/file_mode_string_to_flags_test.cc +++ b/starboard/nplb/file_mode_string_to_flags_test.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 17 + #include #include "starboard/file.h" @@ -59,3 +61,5 @@ TEST(SbFileModeStringToFlagsTest, Aah) { } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 17 diff --git a/starboard/nplb/file_open_test.cc b/starboard/nplb/file_open_test.cc index ae19dec24c1e..4a99d746c886 100644 --- a/starboard/nplb/file_open_test.cc +++ b/starboard/nplb/file_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 < 17 + #include #include @@ -165,3 +167,5 @@ TEST(SbFileOpenTest, OpenOnlyDoesNotOpenNonExistingStaticContentFile) { } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 17 diff --git a/starboard/nplb/file_read_test.cc b/starboard/nplb/file_read_test.cc index f9aa51d9d29a..7d4d4a0a0d4e 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 < 17 + #include #include "starboard/file.h" @@ -294,3 +296,5 @@ TYPED_TEST(SbFileReadTest, ReadStaticContent) { } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 17 diff --git a/starboard/nplb/file_read_write_all_test.cc b/starboard/nplb/file_read_write_all_test.cc index 4871cea672a5..6dc621363818 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 < 17 + #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 < 17 diff --git a/starboard/nplb/file_seek_test.cc b/starboard/nplb/file_seek_test.cc index 95bbdd4db35d..35789796c1e0 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 < 17 + #include #include "starboard/file.h" @@ -235,3 +237,5 @@ TEST(SbFileSeekTest, FromBeginInStaticContentWorks) { } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 17 diff --git a/starboard/nplb/file_truncate_test.cc b/starboard/nplb/file_truncate_test.cc index ae2b3dee8255..6a316d2d7694 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 < 17 + #include #include "starboard/file.h" @@ -115,3 +117,5 @@ TEST(SbFileTruncateTest, TruncateUpInSize) { } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 17 diff --git a/starboard/nplb/file_write_test.cc b/starboard/nplb/file_write_test.cc index 9f6d87d578ac..6493a783bc30 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 < 17 + #include #include "starboard/file.h" @@ -167,3 +169,5 @@ TYPED_TEST(SbFileWriteTest, WriteZeroBytes) { } // namespace } // namespace nplb } // namespace starboard + +#endif // SB_API_VERSION < 17 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..66349e21c7bd --- /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, ReadMode) { + 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, WriteMode) { + 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, AppendMode) { + 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/linux/system_get_random_data.cc b/starboard/shared/linux/system_get_random_data.cc index 94d22a61cea2..5bdbac84dc17 100644 --- a/starboard/shared/linux/system_get_random_data.cc +++ b/starboard/shared/linux/system_get_random_data.cc @@ -16,11 +16,13 @@ #include "starboard/system.h" +#include #include +#include +#include "starboard/common/file.h" #include "starboard/common/log.h" #include "starboard/common/mutex.h" -#include "starboard/file.h" namespace { @@ -29,17 +31,16 @@ namespace { class URandomFile { public: URandomFile() { - file_ = - SbFileOpen("/dev/urandom", kSbFileOpenOnly | kSbFileRead, NULL, NULL); - SB_DCHECK(SbFileIsValid(file_)) << "Cannot open /dev/urandom"; + file_ = open("/dev/urandom", O_RDONLY, S_IRUSR | S_IWUSR); + SB_DCHECK(starboard::IsValid(file_)) << "Cannot open /dev/urandom"; } - ~URandomFile() { SbFileClose(file_); } + ~URandomFile() { close(file_); } - SbFile file() const { return file_; } + int file() const { return file_; } private: - SbFile file_; + int file_; }; // A file that will produce any number of very random bytes. @@ -63,12 +64,12 @@ void SbSystemGetRandomData(void* out_buffer, int buffer_size) { int once_result = pthread_once(&g_urandom_file_once, &InitializeRandom); SB_DCHECK(once_result == 0); - SbFile file = g_urandom_file->file(); + int file = g_urandom_file->file(); do { // This is unsynchronized access to the File that could happen from multiple // threads. It doesn't appear that there is any locking in the Chromium // POSIX implementation that is very similar. - int result = SbFileRead(file, buffer, remaining); + int result = read(file, buffer, remaining); if (result <= 0) break; 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..896e5faf8b56 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 < 17 + #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 < 17 diff --git a/starboard/shared/posix/file_close.cc b/starboard/shared/posix/file_close.cc index ebda5137605b..f9a6f5d638e6 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 < 17 + #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 < 17 diff --git a/starboard/shared/posix/file_delete.cc b/starboard/shared/posix/file_delete.cc index 7830cebc5f88..3ad7ea095407 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 < 17 + #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 < 17 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..3f1a4c3c9751 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 < 17 + #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 < 17 diff --git a/starboard/shared/posix/file_get_info.cc b/starboard/shared/posix/file_get_info.cc index 163462bc3175..2443587bbce9 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 < 17 + #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 < 17 diff --git a/starboard/shared/posix/file_open.cc b/starboard/shared/posix/file_open.cc index b8d76cf2017f..56675c63fd7a 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 < 17 + #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 < 17 diff --git a/starboard/shared/posix/file_read.cc b/starboard/shared/posix/file_read.cc index 33d6debe31e4..3721a8b9b500 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 < 17 + #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 < 17 diff --git a/starboard/shared/posix/file_seek.cc b/starboard/shared/posix/file_seek.cc index 4ece7e8f0712..ba6ba4ab47b9 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 < 17 + #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 < 17 diff --git a/starboard/shared/posix/file_truncate.cc b/starboard/shared/posix/file_truncate.cc index 603dd0110fa7..f119fb3d0f4d 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 < 17 + #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 < 17 diff --git a/starboard/shared/posix/file_write.cc b/starboard/shared/posix/file_write.cc index 7b0af3dec16d..118d183de2b6 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 < 17 + #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 < 17 diff --git a/starboard/shared/posix/storage_write_record.cc b/starboard/shared/posix/storage_write_record.cc index 7a23c6621490..ffae1d79c5ca 100644 --- a/starboard/shared/posix/storage_write_record.cc +++ b/starboard/shared/posix/storage_write_record.cc @@ -14,13 +14,15 @@ #include "starboard/common/storage.h" +#include + #include #include +#include "starboard/common/file.h" #include "starboard/common/log.h" #include "starboard/common/string.h" #include "starboard/configuration_constants.h" -#include "starboard/file.h" #include "starboard/shared/starboard/file_storage/storage_internal.h" const char kTempFileSuffix[] = ".temp"; @@ -44,25 +46,23 @@ bool SbStorageWriteRecord(SbStorageRecord record, kSbFileMaxPath); starboard::strlcat(temp_file_path.data(), kTempFileSuffix, kSbFileMaxPath); - SbFileError error; - SbFile temp_file = SbFileOpen( - temp_file_path.data(), kSbFileCreateAlways | kSbFileWrite | kSbFileRead, - NULL, &error); - if (error != kSbFileOk) { + int temp_file = open(temp_file_path.data(), O_CREAT | O_TRUNC | O_RDWR, + S_IRUSR | S_IWUSR); + if (!starboard::IsValid(temp_file)) { return false; } - SbFileTruncate(temp_file, 0); + ftruncate(temp_file, 0); const char* source = data; int64_t to_write = data_size; while (to_write > 0) { int to_write_max = static_cast(std::min(to_write, static_cast(kSbInt32Max))); - int bytes_written = SbFileWrite(temp_file, source, to_write_max); + int bytes_written = write(temp_file, source, to_write_max); if (bytes_written < 0) { - SbFileClose(temp_file); - SbFileDelete(temp_file_path.data()); + close(temp_file); + unlink(temp_file_path.data()); return false; } @@ -70,24 +70,24 @@ bool SbStorageWriteRecord(SbStorageRecord record, to_write -= bytes_written; } - SbFileFlush(temp_file); + fsync(temp_file); - if (SbFileIsValid(record->file) && !SbFileClose(record->file)) { - SbFileClose(temp_file); - SbFileDelete(temp_file_path.data()); + if (starboard::IsValid(record->file) && (close(record->file) < 0)) { + close(temp_file); + unlink(temp_file_path.data()); return false; } - record->file = kSbFileInvalid; + record->file = -1; - if ((!SbFileDelete(original_file_path.data())) || + if (unlink(original_file_path.data()) || (rename(temp_file_path.data(), original_file_path.data()) != 0)) { - SbFileClose(temp_file); - SbFileDelete(temp_file_path.data()); + close(temp_file); + unlink(temp_file_path.data()); return false; } - SbFileFlush(temp_file); + fsync(temp_file); record->file = temp_file; diff --git a/starboard/shared/starboard/file_atomic_replace_write_file.cc b/starboard/shared/starboard/file_atomic_replace_write_file.cc index 5d7e48925853..3b14875e2055 100644 --- a/starboard/shared/starboard/file_atomic_replace_write_file.cc +++ b/starboard/shared/starboard/file_atomic_replace_write_file.cc @@ -14,6 +14,9 @@ #include "starboard/shared/starboard/file_atomic_replace_write_file.h" +#include +#include + #include #include "starboard/common/file.h" @@ -27,15 +30,13 @@ namespace starboard { bool SbFileAtomicReplaceWriteFile(const char* path, const char* data, int64_t data_size) { - SbFileError error; - SbFile temp_file = SbFileOpen( - path, kSbFileCreateAlways | kSbFileWrite | kSbFileRead, NULL, &error); + int temp_file = open(path, O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR); - if (error != kSbFileOk) { + if (temp_file < 0) { return false; } - SbFileTruncate(temp_file, 0); + ftruncate(temp_file, 0); const char* source = data; int64_t to_write = data_size; @@ -43,12 +44,12 @@ bool SbFileAtomicReplaceWriteFile(const char* path, while (to_write > 0) { const int to_write_max = static_cast(std::min(to_write, static_cast(kSbInt32Max))); - const int bytes_written = SbFileWrite(temp_file, source, to_write_max); + const int bytes_written = write(temp_file, source, to_write_max); RecordFileWriteStat(bytes_written); if (bytes_written < 0) { - SbFileClose(temp_file); - SbFileDelete(path); + close(temp_file); + unlink(path); return false; } @@ -56,9 +57,9 @@ bool SbFileAtomicReplaceWriteFile(const char* path, to_write -= bytes_written; } - SbFileFlush(temp_file); + fsync(temp_file); - if (!SbFileClose(temp_file)) { + if (close(temp_file)) { return false; } return true; diff --git a/starboard/shared/starboard/file_mode_string_to_flags.cc b/starboard/shared/starboard/file_mode_string_to_flags.cc index b69d9741e86b..d6c217869a45 100644 --- a/starboard/shared/starboard/file_mode_string_to_flags.cc +++ b/starboard/shared/starboard/file_mode_string_to_flags.cc @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if SB_API_VERSION < 17 + #include "starboard/common/log.h" #include "starboard/common/string.h" #include "starboard/file.h" @@ -64,3 +66,5 @@ int SbFileModeStringToFlags(const char* mode) { } return flags; } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/shared/starboard/file_storage/storage_close_record.cc b/starboard/shared/starboard/file_storage/storage_close_record.cc index 70f850a6386d..0f39d69bad71 100644 --- a/starboard/shared/starboard/file_storage/storage_close_record.cc +++ b/starboard/shared/starboard/file_storage/storage_close_record.cc @@ -14,7 +14,9 @@ #include "starboard/common/storage.h" -#include "starboard/file.h" +#include + +#include "starboard/common/file.h" #include "starboard/shared/starboard/file_storage/storage_internal.h" bool SbStorageCloseRecord(SbStorageRecord record) { @@ -22,8 +24,8 @@ bool SbStorageCloseRecord(SbStorageRecord record) { return false; } - if (SbFileIsValid(record->file)) { - SbFileClose(record->file); + if (starboard::IsValid(record->file)) { + close(record->file); } delete record; diff --git a/starboard/shared/starboard/file_storage/storage_delete_record.cc b/starboard/shared/starboard/file_storage/storage_delete_record.cc index 3d32365883c6..0d1bf129c858 100644 --- a/starboard/shared/starboard/file_storage/storage_delete_record.cc +++ b/starboard/shared/starboard/file_storage/storage_delete_record.cc @@ -14,10 +14,11 @@ #include "starboard/common/storage.h" +#include + #include #include "starboard/configuration_constants.h" -#include "starboard/file.h" #include "starboard/shared/starboard/file_storage/storage_internal.h" #if SB_API_VERSION < 16 @@ -33,5 +34,5 @@ bool SbStorageDeleteRecord(const char* name) { return false; } - return SbFileDelete(path.data()); + return !unlink(path.data()); } diff --git a/starboard/shared/starboard/file_storage/storage_get_record_size.cc b/starboard/shared/starboard/file_storage/storage_get_record_size.cc index deda29f21bff..4e9207e1b7c1 100644 --- a/starboard/shared/starboard/file_storage/storage_get_record_size.cc +++ b/starboard/shared/starboard/file_storage/storage_get_record_size.cc @@ -14,7 +14,7 @@ #include "starboard/common/storage.h" -#include "starboard/file.h" +#include "starboard/common/file.h" #include "starboard/shared/starboard/file_storage/storage_internal.h" int64_t SbStorageGetRecordSize(SbStorageRecord record) { @@ -22,15 +22,15 @@ int64_t SbStorageGetRecordSize(SbStorageRecord record) { return -1; } - if (!SbFileIsValid(record->file)) { + if (!starboard::IsValid(record->file)) { return -1; } - SbFileInfo info; - bool success = SbFileGetInfo(record->file, &info); + struct stat info; + bool success = !fstat(record->file, &info); if (!success) { return -1; } - return info.size; + return info.st_size; } diff --git a/starboard/shared/starboard/file_storage/storage_internal.h b/starboard/shared/starboard/file_storage/storage_internal.h index c59edf50d377..268a2d1f462e 100644 --- a/starboard/shared/starboard/file_storage/storage_internal.h +++ b/starboard/shared/starboard/file_storage/storage_internal.h @@ -21,13 +21,12 @@ #include "starboard/common/storage.h" #include "starboard/common/string.h" -#include "starboard/file.h" #include "starboard/shared/internal_only.h" #include "starboard/shared/starboard/get_home_directory.h" struct SbStorageRecordPrivate { void* unused_user; // deprecated in SB 16 - SbFile file; + int file; std::string name; }; diff --git a/starboard/shared/starboard/file_storage/storage_open_record.cc b/starboard/shared/starboard/file_storage/storage_open_record.cc index 5eee17162270..eaa32c79790e 100644 --- a/starboard/shared/starboard/file_storage/storage_open_record.cc +++ b/starboard/shared/starboard/file_storage/storage_open_record.cc @@ -14,11 +14,13 @@ #include "starboard/common/storage.h" +#include + #include +#include "starboard/common/file.h" #include "starboard/common/log.h" #include "starboard/configuration_constants.h" -#include "starboard/file.h" #include "starboard/shared/starboard/file_storage/storage_internal.h" #if SB_API_VERSION < 16 @@ -36,9 +38,8 @@ SbStorageRecord SbStorageOpenRecord(const char* name) { // This will always create the storage file, even if it is just opened and // closed without doing any operation. - SbFile file = SbFileOpen( - path.data(), kSbFileOpenAlways | kSbFileRead | kSbFileWrite, NULL, NULL); - if (!SbFileIsValid(file)) { + int file = open(path.data(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); + if (!starboard::IsValid(file)) { return kSbStorageInvalidRecord; } diff --git a/starboard/shared/starboard/file_storage/storage_read_record.cc b/starboard/shared/starboard/file_storage/storage_read_record.cc index ff316e1abdd2..7dc3745dc960 100644 --- a/starboard/shared/starboard/file_storage/storage_read_record.cc +++ b/starboard/shared/starboard/file_storage/storage_read_record.cc @@ -14,8 +14,11 @@ #include "starboard/common/storage.h" +#include + #include +#include "starboard/common/file.h" #include "starboard/common/log.h" #include "starboard/common/string.h" #include "starboard/shared/starboard/file_storage/storage_internal.h" @@ -27,16 +30,16 @@ int64_t SbStorageReadRecord(SbStorageRecord record, return -1; } - if (!SbFileIsValid(record->file)) { + if (!starboard::IsValid(record->file)) { return -1; } - int64_t total = SbFileSeek(record->file, kSbFileFromEnd, 0); + int64_t total = lseek(record->file, 0, SEEK_END); if (total > data_size) { total = data_size; } - int64_t position = SbFileSeek(record->file, kSbFileFromBegin, 0); + int64_t position = lseek(record->file, 0, SEEK_SET); if (position != 0) { return -1; } @@ -46,7 +49,7 @@ int64_t SbStorageReadRecord(SbStorageRecord record, while (to_read > 0) { int to_read_max = static_cast(std::min(to_read, static_cast(kSbInt32Max))); - int bytes_read = SbFileRead(record->file, destination, to_read_max); + int bytes_read = read(record->file, destination, to_read_max); if (bytes_read < 0) { return -1; } diff --git a/starboard/shared/starboard/player/video_dmp_writer.cc b/starboard/shared/starboard/player/video_dmp_writer.cc index 9d6b2bdc45fb..de5ce1bc461d 100644 --- a/starboard/shared/starboard/player/video_dmp_writer.cc +++ b/starboard/shared/starboard/player/video_dmp_writer.cc @@ -14,6 +14,9 @@ #include "starboard/shared/starboard/player/video_dmp_writer.h" +#include +#include + #include #include #include @@ -71,17 +74,16 @@ SB_ONCE_INITIALIZE_FUNCTION(PlayerToWriterMap, GetOrCreatePlayerToWriterMap); } // namespace -VideoDmpWriter::VideoDmpWriter() : file_(kSbFileInvalid) { +VideoDmpWriter::VideoDmpWriter() : file_(-1) { int index = 0; std::string file_name; - while (!SbFileIsValid(file_)) { + while (!IsValid(file_)) { std::stringstream ss; ss << "video_" << index << ".dmp"; file_name = ss.str(); - bool created = false; - file_ = SbFileOpen(file_name.c_str(), kSbFileCreateOnly | kSbFileWrite, - &created, NULL); + file_ = + open(file_name.c_str(), O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR); ++index; } SB_LOG(INFO) << "Dump video content to " << file_name; @@ -93,7 +95,7 @@ VideoDmpWriter::VideoDmpWriter() : file_(kSbFileInvalid) { } VideoDmpWriter::~VideoDmpWriter() { - SbFileClose(file_); + close(file_); } // static @@ -188,7 +190,7 @@ void VideoDmpWriter::DumpAccessUnit( } int VideoDmpWriter::WriteToFile(const void* buffer, int size) { - int result = SbFileWrite(file_, static_cast(buffer), size); + int result = write(file_, static_cast(buffer), size); RecordFileWriteStat(result); return result; } diff --git a/starboard/shared/starboard/player/video_dmp_writer.h b/starboard/shared/starboard/player/video_dmp_writer.h index 11484d527a4e..4869f160ac5a 100644 --- a/starboard/shared/starboard/player/video_dmp_writer.h +++ b/starboard/shared/starboard/player/video_dmp_writer.h @@ -52,7 +52,7 @@ class VideoDmpWriter { void DumpAccessUnit(const scoped_refptr& input_buffer); int WriteToFile(const void* buffer, int size); - SbFile file_; + int file_; WriteCB write_cb_; }; diff --git a/starboard/shared/stub/file_can_open.cc b/starboard/shared/stub/file_can_open.cc index 7335af26ad2f..132438067b99 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 < 17 + #include "starboard/file.h" bool SbFileCanOpen(const char* path, int flags) { return false; } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/shared/stub/file_close.cc b/starboard/shared/stub/file_close.cc index 47df9f2080d0..768d28e26435 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 < 17 + #include "starboard/file.h" bool SbFileClose(SbFile file) { return false; } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/shared/stub/file_delete.cc b/starboard/shared/stub/file_delete.cc index ff3225fdd7e5..298b5e0912bf 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 < 17 + #include "starboard/file.h" bool SbFileDelete(const char* path) { return false; } + +#endif // SB_API_VERSION < 17 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..48268327c115 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 < 17 + #include "starboard/file.h" bool SbFileFlush(SbFile file) { return false; } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/shared/stub/file_get_info.cc b/starboard/shared/stub/file_get_info.cc index 40cf2ee956ee..aa8d32f762f2 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 < 17 + #include "starboard/file.h" bool SbFileGetInfo(SbFile file, SbFileInfo* out_info) { return false; } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/shared/stub/file_open.cc b/starboard/shared/stub/file_open.cc index fe3332bdcca0..084fbd7eb37b 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 < 17 + #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 < 17 diff --git a/starboard/shared/stub/file_read.cc b/starboard/shared/stub/file_read.cc index 4bf60654861f..8db62d342148 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 < 17 + #include "starboard/file.h" int SbFileRead(SbFile file, char* data, int size) { return 0; } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/shared/stub/file_seek.cc b/starboard/shared/stub/file_seek.cc index ac2dc2402013..3655631fd497 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 < 17 + #include "starboard/file.h" int64_t SbFileSeek(SbFile file, SbFileWhence whence, int64_t offset) { return 0; } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/shared/stub/file_truncate.cc b/starboard/shared/stub/file_truncate.cc index b65a8cce8e41..21c63ca67f52 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 < 17 + #include "starboard/file.h" bool SbFileTruncate(SbFile file, int64_t length) { return false; } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/shared/stub/file_write.cc b/starboard/shared/stub/file_write.cc index d6e785874be8..35e1314a784c 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 < 17 + #include "starboard/file.h" int SbFileWrite(SbFile file, const char* data, int size) { return 0; } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/shared/uwp/application_uwp.cc b/starboard/shared/uwp/application_uwp.cc index cfb746796a90..472a03878e43 100644 --- a/starboard/shared/uwp/application_uwp.cc +++ b/starboard/shared/uwp/application_uwp.cc @@ -231,7 +231,7 @@ std::vector ParseStarboardUri(const std::string& uri) { } void AddArgumentsFromFile(const char* path, std::vector* args) { - ScopedFile file(path, kSbFileOpenOnly | kSbFileRead); + ScopedFile file(path, 0); if (!file.IsValid()) { SB_LOG(INFO) << path << " is not valid for arguments."; return; @@ -719,8 +719,7 @@ ref class App sealed : public IFrameworkView { std::stringstream ss; ss << platformStringToString( Windows::Storage::ApplicationData::Current->LocalCacheFolder->Path); - ss << "\\" - << "" << command_line->GetSwitchValue(kLogPathSwitch); + ss << "\\" << "" << command_line->GetSwitchValue(kLogPathSwitch); std::string full_path_log_file = ss.str(); shared::uwp::OpenLogFileWin32(full_path_log_file.c_str()); } else { diff --git a/starboard/shared/uwp/watchdog_log.cc b/starboard/shared/uwp/watchdog_log.cc index 207ed111b92d..5b6f745f5076 100644 --- a/starboard/shared/uwp/watchdog_log.cc +++ b/starboard/shared/uwp/watchdog_log.cc @@ -14,6 +14,7 @@ #include "starboard/shared/uwp/watchdog_log.h" +#include #include #include @@ -47,12 +48,9 @@ class WatchDogThread : public Thread { void Run() override { static const int64_t kSleepTime = 250'000; // 250ms int counter = 0; - bool created_ok = false; - SbFileError out_error = kSbFileOk; - SbFile file_handle = - SbFileOpen(file_path_.c_str(), kSbFileCreateAlways | kSbFileWrite, - &created_ok, &out_error); - if (!created_ok) { + int file_handle = open(file_path_.c_str(), O_CREAT | O_TRUNC | O_WRONLY, + S_IRUSR | S_IWUSR); + if (!IsValid(file_handle)) { SB_LOG(ERROR) << "Could not create watchdog file " << file_path_; return; } @@ -61,17 +59,16 @@ class WatchDogThread : public Thread { ss << "alive: " << counter++ << "\n"; std::string str = ss.str(); int result = - SbFileWrite(file_handle, str.c_str(), static_cast(str.size())); + write(file_handle, str.c_str(), static_cast(str.size())); RecordFileWriteStat(result); - SbFileFlush(file_handle); + fsync(file_handle); } const char kDone[] = "done\n"; - int result = - SbFileWrite(file_handle, kDone, static_cast(strlen(kDone))); + int result = write(file_handle, kDone, static_cast(strlen(kDone))); RecordFileWriteStat(result); - SbFileFlush(file_handle); + fsync(file_handle); usleep(50'000); - bool closed = SbFileClose(file_handle); + bool closed = !close(file_handle); SB_LOG_IF(ERROR, closed) << "Could not close file " << file_path_; } diff --git a/starboard/shared/win32/file_can_open.cc b/starboard/shared/win32/file_can_open.cc index 8f830b250e3e..7744b103eec5 100644 --- a/starboard/shared/win32/file_can_open.cc +++ b/starboard/shared/win32/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 < 17 + #include "starboard/file.h" #include @@ -52,3 +54,5 @@ bool SbFileCanOpen(const char* path, int flags) { return can_open; } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/shared/win32/file_close.cc b/starboard/shared/win32/file_close.cc index afd99ae3c9c5..fa47181459dc 100644 --- a/starboard/shared/win32/file_close.cc +++ b/starboard/shared/win32/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 < 17 + #include "starboard/file.h" #include @@ -36,3 +38,5 @@ bool SbFileClose(SbFile file) { return success; } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/shared/win32/file_delete.cc b/starboard/shared/win32/file_delete.cc index 2d5aa0cba3bd..d20cd80ed217 100644 --- a/starboard/shared/win32/file_delete.cc +++ b/starboard/shared/win32/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 < 17 + #include "starboard/file.h" #include @@ -37,3 +39,5 @@ bool SbFileDelete(const char* path) { return DeleteFileW(path_wstring.c_str()) || RemoveDirectoryW(path_wstring.c_str()); } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/shared/win32/file_exists.cc b/starboard/shared/win32/file_exists.cc index 3072769ecbea..70093daf6e29 100644 --- a/starboard/shared/win32/file_exists.cc +++ b/starboard/shared/win32/file_exists.cc @@ -52,4 +52,5 @@ bool SbFileExists(const char* path) { return PathEndsWith(path_wstring, find_data.cFileName); } -#endif + +#endif // SB_API_VERSION < 16 diff --git a/starboard/shared/win32/file_flush.cc b/starboard/shared/win32/file_flush.cc index c42e69e5db98..ad566da0cc73 100644 --- a/starboard/shared/win32/file_flush.cc +++ b/starboard/shared/win32/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 < 17 + #include "starboard/file.h" #include @@ -26,3 +28,5 @@ bool SbFileFlush(SbFile file) { return FlushFileBuffers(file->file_handle); } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/shared/win32/file_get_info.cc b/starboard/shared/win32/file_get_info.cc index 6a3bff46d956..f4176e5b014f 100644 --- a/starboard/shared/win32/file_get_info.cc +++ b/starboard/shared/win32/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 < 17 + #include "starboard/file.h" #include @@ -56,3 +58,5 @@ bool SbFileGetInfo(SbFile file, SbFileInfo* out_info) { return true; } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/shared/win32/file_open.cc b/starboard/shared/win32/file_open.cc index 2aa61824e32d..d632c234a363 100644 --- a/starboard/shared/win32/file_open.cc +++ b/starboard/shared/win32/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 < 17 + #include "starboard/file.h" #include "starboard/shared/win32/file_internal.h" @@ -39,3 +41,5 @@ SbFile SbFileOpen(const char* path, return new SbFilePrivate(file_handle); } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/shared/win32/file_read.cc b/starboard/shared/win32/file_read.cc index e0efce44825e..f8415ae71765 100644 --- a/starboard/shared/win32/file_read.cc +++ b/starboard/shared/win32/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 < 17 + #include "starboard/file.h" #include @@ -45,3 +47,5 @@ int SbFileRead(SbFile file, char* data, int size) { return number_bytes_read; } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/shared/win32/file_seek.cc b/starboard/shared/win32/file_seek.cc index 640059430da7..117df70b36a7 100644 --- a/starboard/shared/win32/file_seek.cc +++ b/starboard/shared/win32/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 < 17 + #include "starboard/file.h" #include @@ -36,3 +38,5 @@ int64_t SbFileSeek(SbFile file, SbFileWhence whence, int64_t offset) { return new_file_pointer.QuadPart; } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/shared/win32/file_truncate.cc b/starboard/shared/win32/file_truncate.cc index 7e77ab97ca38..21fef69fa87e 100644 --- a/starboard/shared/win32/file_truncate.cc +++ b/starboard/shared/win32/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 < 17 + #include "starboard/file.h" #include @@ -100,3 +102,5 @@ bool SbFileTruncate(SbFile file, int64_t length) { return return_value; } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/shared/win32/file_write.cc b/starboard/shared/win32/file_write.cc index 6e56f68f9ffc..b95460c8b0f6 100644 --- a/starboard/shared/win32/file_write.cc +++ b/starboard/shared/win32/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 < 17 + #include "starboard/file.h" #include @@ -40,3 +42,5 @@ int SbFileWrite(SbFile file, const char* data, int size) { return bytes_written; } + +#endif // SB_API_VERSION < 17 diff --git a/starboard/shared/win32/log_file_impl.cc b/starboard/shared/win32/log_file_impl.cc index 022f7d259880..9158e6e70ec4 100644 --- a/starboard/shared/win32/log_file_impl.cc +++ b/starboard/shared/win32/log_file_impl.cc @@ -14,7 +14,9 @@ #include "starboard/shared/win32/log_file_impl.h" +#include #include +#include #include #include @@ -32,15 +34,15 @@ namespace { pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER; -SbFile log_file = kSbFileInvalid; +int log_file = -1; // SbMutex is not reentrant, so factor out close log file functionality for use // by other functions. void CloseLogFileWithoutLock() { - if (SbFileIsValid(log_file)) { - SbFileFlush(log_file); - SbFileClose(log_file); - log_file = kSbFileInvalid; + if (starboard::IsValid(log_file)) { + fsync(log_file); + close(log_file); + log_file = -1; } } @@ -57,8 +59,8 @@ void CloseLogFile() { } void OpenLogInCacheDirectory(const char* log_file_name, int creation_flags) { - SB_DCHECK((creation_flags & kSbFileOpenAlways) || - (creation_flags & kSbFileCreateAlways)); + SB_DCHECK((creation_flags & O_CREAT) || + ((creation_flags & O_CREAT) && (creation_flags & O_TRUNC))); SB_DCHECK(strlen(log_file_name) != 0); SB_DCHECK(strchr(log_file_name, kSbFileSepChar) == nullptr); std::vector out_path(kSbFileMaxPath + 1); @@ -92,8 +94,8 @@ void OpenLogFile(const char* path, const int creation_flags) { pthread_mutex_lock(&log_mutex); CloseLogFileWithoutLock(); if ((path != nullptr) && (path[0] != '\0')) { - log_file = SbFileOpen(path, flags, nullptr, nullptr); - SB_DCHECK(SbFileIsValid(log_file)); + log_file = open(path, flags, S_IRUSR | S_IWUSR); + SB_DCHECK(starboard::IsValid(log_file)); } pthread_mutex_unlock(&log_mutex); @@ -104,16 +106,16 @@ void WriteToLogFile(const char* text, const int text_length) { return; } pthread_mutex_lock(&log_mutex); - if (!SbFileIsValid(log_file)) { + if (!starboard::IsValid(log_file)) { pthread_mutex_unlock(&log_mutex); return; } - int bytes_written = SbFileWriteAll(log_file, text, text_length); + int bytes_written = starboard::WriteAll(log_file, text, text_length); RecordFileWriteStat(bytes_written); SB_DCHECK(text_length == bytes_written); - SbFileFlush(log_file); + fsync(log_file); pthread_mutex_unlock(&log_mutex); } diff --git a/starboard/shared/win32/storage_write_record.cc b/starboard/shared/win32/storage_write_record.cc index 012d94bb4b72..a83a955673c1 100644 --- a/starboard/shared/win32/storage_write_record.cc +++ b/starboard/shared/win32/storage_write_record.cc @@ -14,15 +14,17 @@ #include "starboard/common/storage.h" +#include +#include #include #include #include +#include "starboard/common/file.h" #include "starboard/common/log.h" #include "starboard/common/string.h" #include "starboard/configuration_constants.h" -#include "starboard/file.h" #include "starboard/shared/starboard/file_storage/storage_internal.h" #include "starboard/shared/win32/file_internal.h" #include "starboard/shared/win32/wchar_utils.h" @@ -48,25 +50,23 @@ bool SbStorageWriteRecord(SbStorageRecord record, kSbFileMaxPath); starboard::strlcat(temp_file_path.data(), kTempFileSuffix, kSbFileMaxPath); - SbFileError error; - SbFile temp_file = SbFileOpen( - temp_file_path.data(), kSbFileCreateAlways | kSbFileWrite | kSbFileRead, - NULL, &error); - if (error != kSbFileOk) { + int temp_file = open(temp_file_path.data(), O_CREAT | O_TRUNC | O_RDWR, + S_IRUSR | S_IWUSR); + if (!starboard::IsValid(temp_file)) { return false; } - SbFileTruncate(temp_file, 0); + ftruncate(temp_file, 0); const char* source = data; int64_t to_write = data_size; while (to_write > 0) { int to_write_max = static_cast(std::min(to_write, static_cast(kSbInt32Max))); - int bytes_written = SbFileWrite(temp_file, source, to_write_max); + int bytes_written = write(temp_file, source, to_write_max); if (bytes_written < 0) { - SbFileClose(temp_file); - SbFileDelete(temp_file_path.data()); + close(temp_file); + unlink(temp_file_path.data()); return false; } @@ -74,15 +74,15 @@ bool SbStorageWriteRecord(SbStorageRecord record, to_write -= bytes_written; } - SbFileFlush(temp_file); + fsync(temp_file); - if (SbFileIsValid(record->file) && !SbFileClose(record->file)) { + if (starboard::IsValid(record->file) && close(record->file)) { return false; } - record->file = kSbFileInvalid; + record->file = -1; - if (!SbFileClose(temp_file)) { + if (close(temp_file)) { return false; } @@ -96,9 +96,8 @@ bool SbStorageWriteRecord(SbStorageRecord record, NULL, 0, NULL, NULL) == 0) { return false; } - SbFile new_record_file = - SbFileOpen(original_file_path.data(), - kSbFileOpenOnly | kSbFileWrite | kSbFileRead, NULL, NULL); + int new_record_file = + open(original_file_path.data(), O_RDWR, S_IRUSR | S_IWUSR); record->file = new_record_file; return true; 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 a9614e098f42..f9947d610530 100644 --- a/third_party/boringssl/src/crypto/bio/file.c +++ b/third_party/boringssl/src/crypto/bio/file.c @@ -81,12 +81,23 @@ #define BIO_FLAGS_UPLINK 0 #endif -#if !defined(OPENSSL_SYS_STARBOARD) +#if defined(OPENSSL_SYS_STARBOARD) +#include +#endif // defined(OPENSSL_SYS_STARBOARD) + #include + +#if !defined(OPENSSL_SYS_STARBOARD) #include #endif // !defined(OPENSSL_SYS_STARBOARD) + #include +#if defined(OPENSSL_SYS_STARBOARD) +#include +#include +#endif // defined(OPENSSL_SYS_STARBOARD) + #include #include @@ -96,6 +107,10 @@ #include #endif +#if defined(OPENSSL_SYS_STARBOARD) +#include "starboard/common/file_wrapper.h" +#endif // defined(OPENSSL_SYS_STARBOARD) + #define BIO_FP_READ 0x02 #define BIO_FP_WRITE 0x04 #define BIO_FP_APPEND 0x08 @@ -175,23 +190,27 @@ static FILE *file_fopen(const char *filename, const char *mode) { BIO *BIO_new_file(const char *filename, const char *mode) { BIO *ret = NULL; #if defined(OPENSSL_SYS_STARBOARD) - SbFile sb_file = kSbFileInvalid; - SbFileError error = kSbFileOk; - sb_file = SbFileOpen(filename, SbFileModeStringToFlags(mode), NULL, &error); - if (!SbFileIsValid(sb_file)) { + FilePtr sb_file = (FilePtr)malloc(sizeof(struct FileStruct)); + memset(sb_file, 0, sizeof(struct FileStruct)); + sb_file->fd = open(filename, FileModeStringToFlags(mode), + S_IRUSR | S_IWUSR); + + if (sb_file->fd < 0) { OPENSSL_PUT_SYSTEM_ERROR(); - ERR_add_error_data(5, "SbFileOpen('", filename, "','", mode, "')"); - if (error == kSbFileErrorNotFound) { + ERR_add_error_data(5, "open('", filename, "','", mode, "')"); + if (errno == ENOENT) { OPENSSL_PUT_ERROR(BIO, BIO_R_NO_SUCH_FILE); } else { OPENSSL_PUT_ERROR(BIO, BIO_R_SYS_LIB); } + free(sb_file); return (NULL); } ret = BIO_new(BIO_s_file()); if (ret == NULL) { - SbFileClose(sb_file); + close(sb_file->fd); + free(sb_file); return (NULL); } @@ -267,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) - SbFileClose((SbFile)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 @@ -295,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 = SbFileRead((SbFile)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); @@ -336,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 = SbFileWrite((SbFile)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) @@ -361,8 +380,8 @@ static int MS_CALLBACK file_write(BIO *b, const char *in, int inl) { static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr) { long ret = 1; #if defined(OPENSSL_SYS_STARBOARD) - SbFile fp = (SbFile)b->ptr; - SbFile *fpp; + FilePtr fp = (FilePtr)b->ptr; + FilePtr *fpp; #else // defined(OPENSSL_SYS_STARBOARD) FILE *fp = (FILE *)b->ptr; FILE **fpp; @@ -373,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)SbFileSeek((SbFile)b->ptr, num, kSbFileFromBegin); + 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) @@ -386,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 = (SbFileSeek((SbFile)b->ptr, 0, kSbFileFromCurrent) >= - SbFileSeek((SbFile)b->ptr, 0, kSbFileFromEnd) + ret = (lseek(((FilePtr)b->ptr)->fd, SEEK_CUR, 0) >= + lseek(((FilePtr)b->ptr)->fd, SEEK_END, 0) ? 1 : 0); #else // defined(OPENSSL_SYS_STARBOARD) @@ -403,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 = SbFileSeek((SbFile)b->ptr, 0, kSbFileFromCurrent); + ret = lseek(((FilePtr)b->ptr)->fd, SEEK_CUR, 0); #else // defined(OPENSSL_SYS_STARBOARD) #ifndef NATIVE_TARGET_BUILD if (b->flags & BIO_FLAGS_UPLINK) @@ -508,12 +527,11 @@ static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr) { #endif #if defined(OPENSSL_SYS_STARBOARD) { - SbFileError error = kSbFileOk; - fp = SbFileOpen((const char *)ptr, SbFileModeStringToFlags(p), NULL, - &error); - if (!SbFileIsValid(fp)) { + fp->fd = open((const char *)ptr, FileModeStringToFlags(p), + S_IRUSR | S_IWUSR); + if (fp->fd < 0) { OPENSSL_PUT_SYSTEM_ERROR(); - ERR_add_error_data(5, "SbFileOpen('", ptr, "','", p, "')"); + ERR_add_error_data(5, "open('", ptr, "','", p, "')"); OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB); ret = 0; break; @@ -545,10 +563,10 @@ static long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr) { break; case BIO_C_GET_FILE_PTR: #if defined(OPENSSL_SYS_STARBOARD) - /* the ptr parameter is actually a SbFile * in this case. */ + /* the ptr parameter is actually a FilePtr * in this case. */ if (ptr != NULL) { - fpp = (SbFile *)ptr; - *fpp = (SbFile)b->ptr; + fpp = (FilePtr *)ptr; + *fpp = (FilePtr)b->ptr; } #else // defined(OPENSSL_SYS_STARBOARD) // the ptr parameter is actually a FILE ** in this case. @@ -566,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) - SbFileFlush((SbFile)b->ptr); + fsync(((FilePtr)b->ptr)->fd); #else // defined(OPENSSL_SYS_STARBOARD) #ifndef NATIVE_TARGET_BUILD if (b->flags & BIO_FLAGS_UPLINK) @@ -597,16 +615,16 @@ static int MS_CALLBACK file_gets(BIO *bp, char *buf, int size) { buf[0] = '\0'; #if defined(OPENSSL_SYS_STARBOARD) ret = -1; - SbFileInfo info; - SbFileGetInfo((SbFile)bp->ptr, &info); - int64_t current = SbFileSeek((SbFile)bp->ptr, kSbFileFromCurrent, 0); - int64_t remaining = info.size - current; + struct stat info; + fstat(((FilePtr)bp->ptr)->fd, &info); + int64_t current = lseek(((FilePtr)bp->ptr)->fd, 0, SEEK_CUR); + 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 = SbFileRead((SbFile)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 42c0e3f09df6..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 +#include + +#include "starboard/common/file_wrapper.h" #include "starboard/string.h" -#include "starboard/file.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/icu/BUILD.gn b/third_party/icu/BUILD.gn index 9f7c79dd899a..3123368e47a0 100644 --- a/third_party/icu/BUILD.gn +++ b/third_party/icu/BUILD.gn @@ -342,7 +342,10 @@ template("generate_icuuc") { defines += [ "U_ICUDATAENTRY_IN_COMMON" ] if (is_starboard) { - public_deps = [ "//starboard:starboard_headers_only" ] + public_deps = [ + "//starboard:starboard_headers_only", + "//starboard/common:common", + ] defines += [ "U_HAVE_NL_LANGINFO_CODESET=0", "U_HAVE_NL_LANGINFO=0" diff --git a/third_party/musl/src/starboard/sys/stat.c b/third_party/musl/src/starboard/sys/stat.c index a41d4fe2d39f..81da1b228d2f 100644 --- a/third_party/musl/src/starboard/sys/stat.c +++ b/third_party/musl/src/starboard/sys/stat.c @@ -37,11 +37,13 @@ int stat(const char *path, struct stat *file_info) return -1; } - file_info -> st_mode = 0; + // In SB_API_VERSION < 16, all files are opened with S_IRUSR | S_IWUSR. + // See starboard/shared/posix/impl/file_open.h. + file_info->st_mode = S_IRUSR | S_IWUSR; if (out_info.is_directory){ - file_info->st_mode = S_IFDIR; + file_info->st_mode |= S_IFDIR; } else if (out_info.is_symbolic_link){ - file_info->st_mode = S_IFLNK; + file_info->st_mode |= S_IFLNK; } file_info->st_ctime = WindowsUsecToTimeT(out_info.creation_time); 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/skia/src/ports/SkFontHost_FreeType_common.cpp b/third_party/skia/src/ports/SkFontHost_FreeType_common.cpp index 92a16991402f..9e6f38155d67 100644 --- a/third_party/skia/src/ports/SkFontHost_FreeType_common.cpp +++ b/third_party/skia/src/ports/SkFontHost_FreeType_common.cpp @@ -49,6 +49,9 @@ #endif #endif +// To avoid SkPath->close() being redef to sb_close. +#undef close + // FT_LOAD_COLOR and the corresponding FT_Pixel_Mode::FT_PIXEL_MODE_BGRA // were introduced in FreeType 2.5.0. // The following may be removed once FreeType 2.5.0 is required to build. diff --git a/third_party/zlib/BUILD.gn b/third_party/zlib/BUILD.gn index ba3510419e7b..1bf80a1cd2f8 100644 --- a/third_party/zlib/BUILD.gn +++ b/third_party/zlib/BUILD.gn @@ -481,7 +481,7 @@ static_library("minizip") { "contrib/minizip/iostarboard.c", "contrib/minizip/iostarboard.h", ] - deps += [ "//starboard:starboard_headers_only" ] + 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;