diff --git a/base/files/file_enumerator_starboard.cc b/base/files/file_enumerator_starboard.cc index 4f269e28dd4a..71010bf0d6f0 100644 --- a/base/files/file_enumerator_starboard.cc +++ b/base/files/file_enumerator_starboard.cc @@ -126,7 +126,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/starboard/android/shared/android_main.cc b/starboard/android/shared/android_main.cc index 4de29227a1d9..870d6482f8e7 100644 --- a/starboard/android/shared/android_main.cc +++ b/starboard/android/shared/android_main.cc @@ -12,7 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include +#include #include "game-activity/GameActivity.h" #include "starboard/android/shared/application_android.h" @@ -103,52 +105,49 @@ bool CopyDirContents(const std::string& src_dir_path, filename_buffer.size())) { 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); } SbDirectoryClose(src_dir); diff --git a/starboard/android/shared/posix_emu/stat.cc b/starboard/android/shared/posix_emu/stat.cc index 5f7864c6ab33..bcc5982e1fca 100644 --- a/starboard/android/shared/posix_emu/stat.cc +++ b/starboard/android/shared/posix_emu/stat.cc @@ -12,7 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include +#include #include @@ -62,12 +64,12 @@ 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; + int file = open(path, O_RDONLY, S_IRUSR | S_IWUSR); + struct stat out_info; if (file) { - bool result = SbFileGetInfo(file, &out_info); + bool result = !fstat(file, &out_info); MapSbFileInfoToStat(&out_info, info); - SbFileClose(file); + close(file); return 0; } diff --git a/starboard/common/file.cc b/starboard/common/file.cc index 9cb288ccb652..bc358451a7cc 100644 --- a/starboard/common/file.cc +++ b/starboard/common/file.cc @@ -42,6 +42,10 @@ bool DirectoryCloseLogFailure(const char* path, SbDirectory dir) { } // namespace +bool IsValid(int file) { + return file >= 0; +} + ssize_t ReadAll(int fd, void* data, int size) { if (fd < 0 || size < 0) { return -1; diff --git a/starboard/common/file.h b/starboard/common/file.h index 15b8f4227850..6647d3ea7aba 100644 --- a/starboard/common/file.h +++ b/starboard/common/file.h @@ -38,6 +38,8 @@ namespace starboard { +bool IsValid(int file); + ssize_t ReadAll(int fd, void* data, int size); void RecordFileWriteStat(int write_file_result); @@ -77,7 +79,7 @@ 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)); 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/loader_app/drain_file.cc b/starboard/loader_app/drain_file.cc index 153553ee65ea..843339aa9ae1 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,7 +178,6 @@ bool TryDrain(const char* dir, const char* app_key) { path.append(kSbFileSepString); path.append(filename); - SbFileError error = kSbFileOk; int file = open(path.c_str(), O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); SB_DCHECK(file >= 0); @@ -207,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 << "'"; } @@ -224,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/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 25c993db78db..d29dd4928d25 100644 --- a/starboard/loader_app/slot_management_test.cc +++ b/starboard/loader_app/slot_management_test.cc @@ -14,7 +14,9 @@ #include "starboard/loader_app/slot_management.h" +#include #include +#include #include #include @@ -223,10 +225,9 @@ class SlotManagementTest : public testing::TestWithParam { path += kSbFileSepString; path += "libcobalt"; AddFileExtension(path); - SbFile sb_file = SbFileOpen(path.c_str(), kSbFileOpenAlways | kSbFileRead, - nullptr, nullptr); + int sb_file = open(path.c_str(), O_CREAT | O_RDONLY); EXPECT_TRUE(SbFileIsValid(sb_file)); - SbFileClose(sb_file); + close(sb_file); return !top_created_dir.empty() ? top_created_dir : path; } 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/storage_write_record.cc b/starboard/shared/posix/storage_write_record.cc index 7a23c6621490..4205cabb32b8 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"; @@ -45,24 +47,23 @@ bool SbStorageWriteRecord(SbStorageRecord record, 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 +71,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..ab4ec9bf3e88 100644 --- a/starboard/shared/starboard/file_mode_string_to_flags.cc +++ b/starboard/shared/starboard/file_mode_string_to_flags.cc @@ -12,9 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + +#include "starboard/common/file.h" #include "starboard/common/log.h" #include "starboard/common/string.h" -#include "starboard/file.h" namespace { bool IsUpdate(const char* mode) { @@ -42,21 +44,26 @@ int SbFileModeStringToFlags(const char* mode) { switch (mode[0]) { case 'r': if (IsUpdate(mode + 1)) { - flags |= kSbFileWrite; + flags |= O_RDWR; + } else { + flags |= O_RDONLY; } - flags |= kSbFileOpenOnly | kSbFileRead; break; case 'w': if (IsUpdate(mode + 1)) { - flags |= kSbFileRead; + flags |= O_RDWR; + } else { + flags |= O_WRONLY; } - flags |= kSbFileCreateAlways | kSbFileWrite; + flags |= O_CREAT | O_TRUNC; break; case 'a': if (IsUpdate(mode + 1)) { - flags |= kSbFileRead; + flags |= O_RDONLY; + } else { + flags |= O_RDWR; } - flags |= kSbFileOpenAlways | kSbFileWrite; + flags |= O_CREAT; break; default: SB_NOTREACHED(); 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..961e67e35994 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); - if (!success) { + 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..0d9f4842c85d 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,17 @@ 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 +96,7 @@ VideoDmpWriter::VideoDmpWriter() : file_(kSbFileInvalid) { } VideoDmpWriter::~VideoDmpWriter() { - SbFileClose(file_); + close(file_); } // static @@ -188,7 +191,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/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/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..af1a0b186904 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,22 @@ 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 file = open(path.data(), O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR); + if (!IsValid(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 +73,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 +95,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/src/crypto/bio/file.c b/third_party/boringssl/src/crypto/bio/file.c index 6f75687f7c59..a9614e098f42 100644 --- a/third_party/boringssl/src/crypto/bio/file.c +++ b/third_party/boringssl/src/crypto/bio/file.c @@ -177,7 +177,6 @@ BIO *BIO_new_file(const char *filename, const char *mode) { #if defined(OPENSSL_SYS_STARBOARD) SbFile sb_file = kSbFileInvalid; SbFileError error = kSbFileOk; - int file = open(filename, SbFileModeStringToFlags(mode)); sb_file = SbFileOpen(filename, SbFileModeStringToFlags(mode), NULL, &error); if (!SbFileIsValid(sb_file)) { OPENSSL_PUT_SYSTEM_ERROR(); diff --git a/third_party/freetype/src/include/freetype/config/ftstdlib.h b/third_party/freetype/src/include/freetype/config/ftstdlib.h index 42c0e3f09df6..02bd18a6ee29 100644 --- a/third_party/freetype/src/include/freetype/config/ftstdlib.h +++ b/third_party/freetype/src/include/freetype/config/ftstdlib.h @@ -209,4 +209,4 @@ ft_getenv( const char* name ) #endif /* FTSTDLIB_H_ */ -/* END */ +/* END */ \ No newline at end of file diff --git a/third_party/icu/BUILD.gn b/third_party/icu/BUILD.gn index 61fe28297568..4ab93f1d14c8 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", + ] defines += [ "U_HAVE_NL_LANGINFO_CODESET=0", "U_HAVE_NL_LANGINFO=0" diff --git a/third_party/icu/source/common/umapfile.cpp b/third_party/icu/source/common/umapfile.cpp index cb8256d6d90b..5fc689c25890 100644 --- a/third_party/icu/source/common/umapfile.cpp +++ b/third_party/icu/source/common/umapfile.cpp @@ -100,9 +100,13 @@ typedef HANDLE MemoryMap; # define IS_MAP(map) ((map)!=nullptr) #elif MAP_IMPLEMENTATION==MAP_STARBOARD -# include "starboard/file.h" +# include "starboard/common/file.h" # include "cmemory.h" +# include +# include # include +# include +# include typedef void *MemoryMap; @@ -362,21 +366,21 @@ typedef HANDLE MemoryMap; UDataMemory_init(pData); /* Clear the output struct. */ /* open the input file */ - SbFile file = SbFileOpen(path, kSbFileOpenOnly | kSbFileRead, NULL, NULL); - if (!SbFileIsValid(file)) { + int file = open(path, O_RDONLY, S_IRUSR | S_IWUSR); + if (!starboard::IsValid(file)) { return FALSE; } /* get the file length */ - SbFileInfo info; - if (!SbFileGetInfo(file, &info)) { - SbFileClose(file); + struct stat info; + if (fstat(file, &info)) { + close(file); return FALSE; } - int32_t fileLength = info.size; + int32_t fileLength = info.st_size; if (fileLength <= 20) { - SbFileClose(file); + close(file); return FALSE; } @@ -396,7 +400,7 @@ typedef HANDLE MemoryMap; pData->map=p; pData->pHeader=(const DataHeader *)p; pData->mapAddr=p; - SbFileClose(file); + close(file); return TRUE; } // If mmap extension didn't work, fall back to allocating @@ -405,18 +409,18 @@ typedef HANDLE MemoryMap; /* allocate the memory to hold the file data */ void *p = uprv_malloc(fileLength); if (!p) { - SbFileClose(file); + close(file); return FALSE; } /* read the file */ - if (fileLength != SbFileReadAll(file, static_cast(p), fileLength)) { + if (fileLength != starboard::ReadAll(file, static_cast(p), fileLength)) { uprv_free(p); - SbFileClose(file); + close(file); return FALSE; } - SbFileClose(file); + close(file); pData->map=p; pData->pHeader=(const DataHeader *)p; pData->mapAddr=p;