From 79adf53a067382d8935bb1faa57b5ae422e8e7fc Mon Sep 17 00:00:00 2001 From: Yijia Zhang <45114178+yjzhang111@users.noreply.github.com> Date: Wed, 8 May 2024 17:29:55 -0700 Subject: [PATCH] Add tests for POSIX file flush, truncate, and delete. (#3139) b/302715109 Change-Id: I0547db9a11a812d49d48b821c0f9690a110ede2f --- starboard/elf_loader/exported_symbols.cc | 8 +- starboard/nplb/BUILD.gn | 3 + .../posix_file_delete_test.cc | 67 ++++++++++ .../posix_compliance/posix_file_fsync_test.cc | 32 +++++ .../posix_file_ftruncate_test.cc | 117 ++++++++++++++++++ .../posix_compliance/posix_file_read_test.cc | 2 +- starboard/shared/modular/BUILD.gn | 6 +- ...cobalt_layer_posix_unistd_abi_wrappers.cc} | 6 + ...rboard_layer_posix_unistd_abi_wrappers.cc} | 6 +- ...arboard_layer_posix_unistd_abi_wrappers.h} | 8 +- .../shared/win32/posix_emu/include/unistd.h | 4 + starboard/shared/win32/posix_emu/socket.cc | 32 +++-- .../api_leak_detector/api_leak_detector.py | 4 + .../musl/src/starboard/network/socket.c | 51 +++++++- .../musl/src/starboard/unistd/unistd.c | 19 +++ 15 files changed, 342 insertions(+), 23 deletions(-) create mode 100644 starboard/nplb/posix_compliance/posix_file_delete_test.cc create mode 100644 starboard/nplb/posix_compliance/posix_file_fsync_test.cc create mode 100644 starboard/nplb/posix_compliance/posix_file_ftruncate_test.cc rename starboard/shared/modular/{cobalt_layer_posix_file_abi_wrappers.cc => cobalt_layer_posix_unistd_abi_wrappers.cc} (87%) rename starboard/shared/modular/{starboard_layer_posix_file_abi_wrappers.cc => starboard_layer_posix_unistd_abi_wrappers.cc} (80%) rename starboard/shared/modular/{starboard_layer_posix_file_abi_wrappers.h => starboard_layer_posix_unistd_abi_wrappers.h} (83%) diff --git a/starboard/elf_loader/exported_symbols.cc b/starboard/elf_loader/exported_symbols.cc index 818010c42422..a5a0c113acf0 100644 --- a/starboard/elf_loader/exported_symbols.cc +++ b/starboard/elf_loader/exported_symbols.cc @@ -52,12 +52,12 @@ #include "starboard/mutex.h" #include "starboard/player.h" #if SB_API_VERSION >= 16 -#include "starboard/shared/modular/starboard_layer_posix_file_abi_wrappers.h" #include "starboard/shared/modular/starboard_layer_posix_mmap_abi_wrappers.h" #include "starboard/shared/modular/starboard_layer_posix_pthread_abi_wrappers.h" #include "starboard/shared/modular/starboard_layer_posix_socket_abi_wrappers.h" #include "starboard/shared/modular/starboard_layer_posix_stat_abi_wrappers.h" #include "starboard/shared/modular/starboard_layer_posix_time_abi_wrappers.h" +#include "starboard/shared/modular/starboard_layer_posix_unistd_abi_wrappers.h" #endif // SB_API_VERSION >= 16 #include "starboard/socket.h" #include "starboard/socket_waiter.h" @@ -466,6 +466,8 @@ ExportedSymbols::ExportedSymbols() { REGISTER_SYMBOL(freeaddrinfo); REGISTER_SYMBOL(freeifaddrs); REGISTER_SYMBOL(fstat); + REGISTER_SYMBOL(fsync); + REGISTER_SYMBOL(ftruncate); REGISTER_SYMBOL(getaddrinfo); REGISTER_SYMBOL(getifaddrs); REGISTER_SYMBOL(getsockname); @@ -483,6 +485,7 @@ ExportedSymbols::ExportedSymbols() { REGISTER_SYMBOL(recv); REGISTER_SYMBOL(send); REGISTER_SYMBOL(recvfrom); + REGISTER_SYMBOL(rmdir); REGISTER_SYMBOL(sched_yield); REGISTER_SYMBOL(sendto); REGISTER_SYMBOL(setsockopt); @@ -490,10 +493,11 @@ ExportedSymbols::ExportedSymbols() { REGISTER_SYMBOL(snprintf); REGISTER_SYMBOL(sprintf); REGISTER_SYMBOL(stat); + REGISTER_SYMBOL(unlink); + REGISTER_SYMBOL(usleep); REGISTER_SYMBOL(vfwprintf); REGISTER_SYMBOL(vsnprintf); REGISTER_SYMBOL(vsscanf); - REGISTER_SYMBOL(usleep); // Custom mapped POSIX APIs to compatibility wrappers. // These will rely on Starboard-side implementations that properly translate diff --git a/starboard/nplb/BUILD.gn b/starboard/nplb/BUILD.gn index 7eb86fb4a7d1..a87086e19636 100644 --- a/starboard/nplb/BUILD.gn +++ b/starboard/nplb/BUILD.gn @@ -144,6 +144,9 @@ target(gtest_target_type, "nplb") { "posix_compliance/posix_directory_can_open_test.cc", "posix_compliance/posix_directory_create_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_open_test.cc", diff --git a/starboard/nplb/posix_compliance/posix_file_delete_test.cc b/starboard/nplb/posix_compliance/posix_file_delete_test.cc new file mode 100644 index 000000000000..9b9db4ae01df --- /dev/null +++ b/starboard/nplb/posix_compliance/posix_file_delete_test.cc @@ -0,0 +1,67 @@ +// 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 + +#include "starboard/directory.h" +#include "starboard/file.h" +#include "starboard/nplb/file_helpers.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace starboard { +namespace nplb { +namespace { + +bool FileExists(const char* path) { + struct stat info; + return stat(path, &info) == 0; +} + +bool DirectoryExists(const char* path) { + struct stat info; + return stat(path, &info) == 0 && S_ISDIR(info.st_mode); +} + +TEST(PosixFileDeleteTest, SunnyDayDeleteExistingFile) { + ScopedRandomFile file; + + EXPECT_TRUE(FileExists(file.filename().c_str())); + EXPECT_TRUE(unlink(file.filename().c_str()) == 0); +} + +TEST(PosixFileDeleteTest, SunnyDayDeleteExistingDirectory) { + ScopedRandomFile file(ScopedRandomFile::kDontCreate); + + const std::string& path = file.filename(); + + EXPECT_FALSE(FileExists(path.c_str())); + EXPECT_TRUE(mkdir(path.c_str(), 0700) == 0 || DirectoryExists(path.c_str())); + EXPECT_TRUE(DirectoryExists(path.c_str())); + EXPECT_TRUE(rmdir(path.c_str()) == 0); +} + +TEST(PosixFileDeleteTest, RainyDayNonExistentDirErrors) { + ScopedRandomFile file(ScopedRandomFile::kDontCreate); + + EXPECT_FALSE(FileExists(file.filename().c_str())); + EXPECT_TRUE(rmdir(file.filename().c_str())); +} + +} // namespace +} // namespace nplb +} // namespace starboard diff --git a/starboard/nplb/posix_compliance/posix_file_fsync_test.cc b/starboard/nplb/posix_compliance/posix_file_fsync_test.cc new file mode 100644 index 000000000000..c6f7c2d4f5a4 --- /dev/null +++ b/starboard/nplb/posix_compliance/posix_file_fsync_test.cc @@ -0,0 +1,32 @@ +// 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. + +// flush() is otherwise tested in PosixFileWriteTest. + +#include + +#include "testing/gtest/include/gtest/gtest.h" + +namespace starboard { +namespace nplb { +namespace { + +TEST(PosixFileFsyncTest, InvalidFileErrors) { + int result = fsync(-1); + EXPECT_FALSE(result == 0); +} + +} // namespace +} // namespace nplb +} // namespace starboard diff --git a/starboard/nplb/posix_compliance/posix_file_ftruncate_test.cc b/starboard/nplb/posix_compliance/posix_file_ftruncate_test.cc new file mode 100644 index 000000000000..a21eec392621 --- /dev/null +++ b/starboard/nplb/posix_compliance/posix_file_ftruncate_test.cc @@ -0,0 +1,117 @@ +// Copyright 2024 The Cobalt Authors. All Rights Reserved. +// PosixFileWriteTest +// 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 + +#include + +#include "starboard/common/file.h" +#include "starboard/nplb/file_helpers.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace starboard { +namespace nplb { +namespace { + +TEST(PosixFileFtruncateTest, InvalidFileErrors) { + int result = ftruncate(-1, 0); + EXPECT_FALSE(result == 0); + + result = ftruncate(-1, -1); + EXPECT_FALSE(result == 0); + + result = ftruncate(-1, 100); + EXPECT_FALSE(result == 0); +} + +TEST(PosixFileFtruncateTest, TruncateToZero) { + const int kStartSize = 123; + const int kEndSize = 0; + ScopedRandomFile random_file(kStartSize); + const std::string& filename = random_file.filename(); + + int file = open(filename.c_str(), O_RDWR); + ASSERT_TRUE(file >= 0); + + { + struct stat info = {0}; + int result = fstat(file, &info); + EXPECT_EQ(kStartSize, info.st_size); + } + + int result = ftruncate(file, kEndSize); + EXPECT_TRUE(result == 0); + + { + struct stat info = {0}; + result = fstat(file, &info); + EXPECT_EQ(kEndSize, info.st_size); + } + + result = close(file); + EXPECT_TRUE(result == 0); +} + +TEST(PosixFileFtruncateTest, TruncateUpInSize) { + // "Truncate," I don't think that word means what you think it means. + const int kStartSize = 123; + const int kEndSize = kStartSize * 2; + ScopedRandomFile random_file(kStartSize); + const std::string& filename = random_file.filename(); + + int file = open(filename.c_str(), O_RDWR); + ASSERT_TRUE(file >= 0); + + { + struct stat info = {0}; + int result = fstat(file, &info); + EXPECT_TRUE(result == 0); + EXPECT_EQ(kStartSize, info.st_size); + } + + int position = static_cast(lseek(file, 0, SEEK_CUR)); + EXPECT_EQ(0, position); + + int result = ftruncate(file, kEndSize); + EXPECT_TRUE(result == 0); + + position = static_cast(lseek(file, 0, SEEK_CUR)); + EXPECT_EQ(0, position); + + { + struct stat info = {0}; + result = fstat(file, &info); + EXPECT_TRUE(result == 0); + EXPECT_EQ(kEndSize, info.st_size); + } + + char buffer[kEndSize] = {0}; + int bytes = static_cast(ReadAll(file, buffer, kEndSize)); + EXPECT_EQ(kEndSize, bytes); + + ScopedRandomFile::ExpectPattern(0, buffer, kStartSize, __LINE__); + + for (int i = kStartSize; i < kEndSize; ++i) { + EXPECT_EQ(0, buffer[i]); + } + + result = close(file); + EXPECT_TRUE(result == 0); +} + +} // namespace +} // namespace nplb +} // namespace starboard diff --git a/starboard/nplb/posix_compliance/posix_file_read_test.cc b/starboard/nplb/posix_compliance/posix_file_read_test.cc index b6ce49e7957d..1106ef8ccc80 100644 --- a/starboard/nplb/posix_compliance/posix_file_read_test.cc +++ b/starboard/nplb/posix_compliance/posix_file_read_test.cc @@ -39,7 +39,7 @@ class PosixRead { class PosixReadAll { public: - static ssize_t Read(int file, void* data, int size) { + static ssize_t Read(int file, void* data, size_t size) { return ReadAll(file, data, size); } }; diff --git a/starboard/shared/modular/BUILD.gn b/starboard/shared/modular/BUILD.gn index 6149fbf4f45f..59d627a18485 100644 --- a/starboard/shared/modular/BUILD.gn +++ b/starboard/shared/modular/BUILD.gn @@ -19,8 +19,6 @@ if ((sb_is_modular || sb_is_evergreen_compatible) && build_with_separate_cobalt_toolchain)) { source_set("starboard_layer_posix_abi_wrappers") { sources = [ - "starboard_layer_posix_file_abi_wrappers.cc", - "starboard_layer_posix_file_abi_wrappers.h", "starboard_layer_posix_mmap_abi_wrappers.cc", "starboard_layer_posix_mmap_abi_wrappers.h", "starboard_layer_posix_pthread_abi_wrappers.cc", @@ -31,6 +29,8 @@ if ((sb_is_modular || sb_is_evergreen_compatible) && "starboard_layer_posix_stat_abi_wrappers.h", "starboard_layer_posix_time_abi_wrappers.cc", "starboard_layer_posix_time_abi_wrappers.h", + "starboard_layer_posix_unistd_abi_wrappers.cc", + "starboard_layer_posix_unistd_abi_wrappers.h", ] configs += [ "//starboard/build/config:starboard_implementation" ] @@ -43,12 +43,12 @@ if (sb_is_modular && !sb_is_evergreen && current_toolchain == cobalt_toolchain) { source_set("cobalt_layer_posix_abi_wrappers") { sources = [ - "cobalt_layer_posix_file_abi_wrappers.cc", "cobalt_layer_posix_mmap_abi_wrappers.cc", "cobalt_layer_posix_pthread_abi_wrappers.cc", "cobalt_layer_posix_socket_abi_wrappers.cc", "cobalt_layer_posix_stat_abi_wrappers.cc", "cobalt_layer_posix_time_abi_wrappers.cc", + "cobalt_layer_posix_unistd_abi_wrappers.cc", ] } } diff --git a/starboard/shared/modular/cobalt_layer_posix_file_abi_wrappers.cc b/starboard/shared/modular/cobalt_layer_posix_unistd_abi_wrappers.cc similarity index 87% rename from starboard/shared/modular/cobalt_layer_posix_file_abi_wrappers.cc rename to starboard/shared/modular/cobalt_layer_posix_unistd_abi_wrappers.cc index 4901f49c4613..b5678ed9045e 100644 --- a/starboard/shared/modular/cobalt_layer_posix_file_abi_wrappers.cc +++ b/starboard/shared/modular/cobalt_layer_posix_unistd_abi_wrappers.cc @@ -18,6 +18,12 @@ extern "C" { +int __abi_wrap_ftruncate(int fildes, off_t length); + +int ftruncate(int fildes, off_t length) { + return __abi_wrap_ftruncate(fildes, length); +} + off_t __abi_wrap_lseek(int fildes, off_t offset, int whence); off_t lseek(int fildes, off_t offset, int whence) { diff --git a/starboard/shared/modular/starboard_layer_posix_file_abi_wrappers.cc b/starboard/shared/modular/starboard_layer_posix_unistd_abi_wrappers.cc similarity index 80% rename from starboard/shared/modular/starboard_layer_posix_file_abi_wrappers.cc rename to starboard/shared/modular/starboard_layer_posix_unistd_abi_wrappers.cc index a02e6a9068bb..0c8604da5c12 100644 --- a/starboard/shared/modular/starboard_layer_posix_file_abi_wrappers.cc +++ b/starboard/shared/modular/starboard_layer_posix_unistd_abi_wrappers.cc @@ -14,11 +14,15 @@ #if SB_API_VERSION >= 16 -#include "starboard/shared/modular/starboard_layer_posix_file_abi_wrappers.h" +#include "starboard/shared/modular/starboard_layer_posix_unistd_abi_wrappers.h" #include #include +int __abi_wrap_ftruncate(int fildes, musl_off_t length) { + return ftruncate(fildes, static_cast(length)); +} + musl_off_t __abi_wrap_lseek(int fildes, musl_off_t offset, int whence) { return static_cast(lseek(fildes, static_cast(offset), whence)); } diff --git a/starboard/shared/modular/starboard_layer_posix_file_abi_wrappers.h b/starboard/shared/modular/starboard_layer_posix_unistd_abi_wrappers.h similarity index 83% rename from starboard/shared/modular/starboard_layer_posix_file_abi_wrappers.h rename to starboard/shared/modular/starboard_layer_posix_unistd_abi_wrappers.h index a47269f42825..624c9183c623 100644 --- a/starboard/shared/modular/starboard_layer_posix_file_abi_wrappers.h +++ b/starboard/shared/modular/starboard_layer_posix_unistd_abi_wrappers.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef STARBOARD_SHARED_MODULAR_STARBOARD_LAYER_POSIX_FILE_ABI_WRAPPERS_H_ -#define STARBOARD_SHARED_MODULAR_STARBOARD_LAYER_POSIX_FILE_ABI_WRAPPERS_H_ +#ifndef STARBOARD_SHARED_MODULAR_STARBOARD_LAYER_POSIX_UNISTD_ABI_WRAPPERS_H_ +#define STARBOARD_SHARED_MODULAR_STARBOARD_LAYER_POSIX_UNISTD_ABI_WRAPPERS_H_ #include #include @@ -50,6 +50,8 @@ typedef int32_t ssize_t; extern "C" { #endif +SB_EXPORT int __abi_wrap_ftruncate(int fildes, musl_off_t length); + SB_EXPORT musl_off_t __abi_wrap_lseek(int fildes, musl_off_t offset, int whence); @@ -60,4 +62,4 @@ SB_EXPORT ssize_t __abi_wrap_read(int fildes, void* buf, size_t nbyte); } // extern "C" #endif -#endif // STARBOARD_SHARED_MODULAR_STARBOARD_LAYER_POSIX_FILE_ABI_WRAPPERS_H_ +#endif // STARBOARD_SHARED_MODULAR_STARBOARD_LAYER_POSIX_UNISTD_ABI_WRAPPERS_H_ diff --git a/starboard/shared/win32/posix_emu/include/unistd.h b/starboard/shared/win32/posix_emu/include/unistd.h index 9cce6cd430b6..c90b90d1e6aa 100644 --- a/starboard/shared/win32/posix_emu/include/unistd.h +++ b/starboard/shared/win32/posix_emu/include/unistd.h @@ -26,6 +26,10 @@ extern "C" { // This function will handle both files and sockets. int close(int fd); +int fsync(int fd); + +int ftruncate(int fd, off_t length); + off_t sb_lseek(int fd, off_t offset, int origin); #define lseek sb_lseek diff --git a/starboard/shared/win32/posix_emu/socket.cc b/starboard/shared/win32/posix_emu/socket.cc index 75ff40840cd6..d28fb30d677a 100644 --- a/starboard/shared/win32/posix_emu/socket.cc +++ b/starboard/shared/win32/posix_emu/socket.cc @@ -289,6 +289,30 @@ int close(int fd) { return _close(handle.file); } +int fsync(int fd) { + FileOrSocket handle = handle_db_get(fd, false); + if (!handle.is_file) { + return -1; + } + return _commit(handle.file); +} + +int ftruncate(int fd, off_t length) { + FileOrSocket handle = handle_db_get(fd, false); + if (!handle.is_file) { + return -1; + } + return _chsize(handle.file, length); +} + +int sb_fstat(int fd, struct stat* buffer) { + FileOrSocket handle = handle_db_get(fd, false); + if (!handle.is_file) { + return -1; + } + return _fstat(handle.file, (struct _stat*)buffer); +} + off_t sb_lseek(int fd, off_t offset, int origin) { FileOrSocket handle = handle_db_get(fd, false); if (!handle.is_file) { @@ -465,12 +489,4 @@ int sb_fcntl(int fd, int cmd, ... /*arg*/) { return 0; } -int sb_fstat(int fd, struct stat* buffer) { - FileOrSocket handle = handle_db_get(fd, false); - if (!handle.is_file) { - return -1; - } - return _fstat(handle.file, (struct _stat*)buffer); -} - } // extern "C" diff --git a/starboard/tools/api_leak_detector/api_leak_detector.py b/starboard/tools/api_leak_detector/api_leak_detector.py index 6a23ff89b6fc..4ca3ee75281f 100755 --- a/starboard/tools/api_leak_detector/api_leak_detector.py +++ b/starboard/tools/api_leak_detector/api_leak_detector.py @@ -98,6 +98,8 @@ 'freeifaddrs', 'freeaddrinfo', 'fstat', + 'fsync', + 'ftruncate', 'gettimeofday', 'getifaddrs', 'getaddrinfo', @@ -110,6 +112,7 @@ 'recv', 'recvfrom', 'realloc', + 'rmdir', 'setsockopt', 'send', 'sendto', @@ -117,6 +120,7 @@ 'strncasecmp', 'socket', 'time', + 'unlink', 'mmap', 'munmap', 'mprotect', diff --git a/third_party/musl/src/starboard/network/socket.c b/third_party/musl/src/starboard/network/socket.c index 23b633fe18d7..988babd5dc72 100644 --- a/third_party/musl/src/starboard/network/socket.c +++ b/third_party/musl/src/starboard/network/socket.c @@ -257,6 +257,48 @@ int fstat(int fildes, struct stat* buf) { return 0; } +int fsync(int fildes) { + if (fildes < 0) { + errno = EBADF; + return -1; + } + + FileOrSocket* fileOrSock = NULL; + if (get(fildes, false, &fileOrSock) != 0) { + errno = EBADF; + return -1; + } + + if (fileOrSock == NULL || !fileOrSock->is_file) { + errno = EBADF; + return -1; + } + + int result = SbFileFlush(fileOrSock->file) ? 0 : -1; + return result; +} + +int ftruncate(int fildes, off_t length) { + if (fildes < 0) { + errno = EBADF; + return -1; + } + + FileOrSocket* fileOrSock = NULL; + if (get(fildes, false, &fileOrSock) != 0) { + errno = EBADF; + return -1; + } + + if (fileOrSock == NULL || !fileOrSock->is_file) { + errno = EBADF; + return -1; + } + + int result = SbFileTruncate(fileOrSock->file, length) ? 0 : -1; + return result; +} + off_t lseek(int fildes, off_t offset, int whence) { if (fildes < 0) { errno = EBADF; @@ -284,7 +326,6 @@ off_t lseek(int fildes, off_t offset, int whence) { } else { return -1; } - return (off_t)SbFileSeek(fileOrSock->file, sbWhence, (int64_t)offset); } @@ -305,9 +346,6 @@ int open(const char* path, int oflag, ...) { if ((oflag & O_ACCMODE) == O_RDONLY) { accessModeFlag |= kSbFileRead; - if (oflag == O_RDONLY) { - sbFileFlags = kSbFileOpenOnly; - } } else if ((oflag & O_ACCMODE) == O_WRONLY) { accessModeFlag |= kSbFileWrite; oflag &= ~O_WRONLY; @@ -321,6 +359,10 @@ int open(const char* path, int oflag, ...) { return -1; } + if (!oflag) { + sbFileFlags = kSbFileOpenOnly; + } + if (oflag & O_CREAT && oflag & O_EXCL) { sbFileFlags = kSbFileCreateOnly; oflag &= ~(O_CREAT | O_EXCL); @@ -378,7 +420,6 @@ ssize_t read(int fildes, void* buf, size_t nbyte) { errno = EBADF; return -1; } - return (ssize_t)SbFileRead(fileOrSock->file, buf, (int)nbyte); } diff --git a/third_party/musl/src/starboard/unistd/unistd.c b/third_party/musl/src/starboard/unistd/unistd.c index f493b65d450d..20b36eafcb28 100644 --- a/third_party/musl/src/starboard/unistd/unistd.c +++ b/third_party/musl/src/starboard/unistd/unistd.c @@ -15,8 +15,27 @@ #if SB_API_VERSION < 16 #include +#include "starboard/file.h" #include "starboard/thread.h" +int rmdir(const char* path) { + SbFileInfo out_info; + if (!SbFileGetPathInfo(path, &out_info)) { + return -1; + } + int result = SbFileDelete(path) ? 0 : -1; + return result; +} + +int unlink(const char* path) { + SbFileInfo out_info; + if (!SbFileGetPathInfo(path, &out_info)) { + return -1; + } + int result = SbFileDelete(path) ? 0 : -1; + return result; +} + int usleep(useconds_t useconds) { SbThreadSleep(useconds); return 0;