Skip to content

Commit

Permalink
Change lseek, read, and write in windows emulation layer to signature…
Browse files Browse the repository at this point in the history
… defined in io.h

Change-Id: I4ed6d4e2c2d21965a9dbd32e794d53991ce3a235
  • Loading branch information
yjzhang111 committed May 9, 2024
1 parent 69603e7 commit ab5fe36
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 30 deletions.
1 change: 1 addition & 0 deletions starboard/elf_loader/exported_symbols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ ExportedSymbols::ExportedSymbols() {
REGISTER_SYMBOL(vfwprintf);
REGISTER_SYMBOL(vsnprintf);
REGISTER_SYMBOL(vsscanf);
REGISTER_SYMBOL(write);

// Custom mapped POSIX APIs to compatibility wrappers.
// These will rely on Starboard-side implementations that properly translate
Expand Down
1 change: 1 addition & 0 deletions starboard/nplb/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ target(gtest_target_type, "nplb") {
"posix_compliance/posix_file_open_test.cc",
"posix_compliance/posix_file_read_test.cc",
"posix_compliance/posix_file_seek_test.cc",
"posix_compliance/posix_file_write_test.cc",
"posix_compliance/posix_memory_allocate_aligned_test.cc",
"posix_compliance/posix_memory_allocate_test.cc",
"posix_compliance/posix_memory_deallocate_aligned_test.cc",
Expand Down
3 changes: 2 additions & 1 deletion starboard/nplb/posix_compliance/posix_file_write_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ TYPED_TEST(PosixFileWriteTest, WriteZeroBytes) {
ScopedRandomFile random_file(0, ScopedRandomFile::kDontCreate);
const std::string& filename = random_file.filename();

int file = open(filename.c_str(), O_CREAT | O_EXCL | O_WRONLY);
int file =
open(filename.c_str(), O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR);
ASSERT_TRUE(file >= 0);

char buffer[kBufferLength] = {0};
Expand Down
9 changes: 3 additions & 6 deletions starboard/shared/win32/posix_emu/include/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@ 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
long lseek(int fd, long offset, int origin); // NOLINT

ssize_t sb_read(int fildes, void* buf, size_t nbyte);
#define read sb_read
int read(int fd, void* buffer, unsigned int buffer_size);

ssize_t sb_write(int fd, const void* buf, size_t nbyte);
#define write sb_write
int write(int fd, const void* buffer, unsigned int count);

int usleep(unsigned int useconds);

Expand Down
16 changes: 8 additions & 8 deletions starboard/shared/win32/posix_emu/socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -305,36 +305,36 @@ int ftruncate(int fd, off_t length) {
return _chsize(handle.file, length);
}

int sb_fstat(int fd, struct stat* buffer) {
long lseek(int fd, long offset, int origin) { // NOLINT
FileOrSocket handle = handle_db_get(fd, false);
if (!handle.is_file) {
return -1;
}
return _fstat(handle.file, (struct _stat*)buffer);
return _lseek(handle.file, offset, origin);
}

off_t sb_lseek(int fd, off_t offset, int origin) {
int sb_fstat(int fd, struct stat* buffer) {
FileOrSocket handle = handle_db_get(fd, false);
if (!handle.is_file) {
return -1;
}
return _lseek(handle.file, offset, origin);
return _fstat(handle.file, (struct _stat*)buffer);
}

ssize_t sb_read(int fd, void* buf, size_t nbyte) {
int read(int fd, void* buffer, unsigned int buffer_size) {
FileOrSocket handle = handle_db_get(fd, false);
if (!handle.is_file) {
return -1;
}
return _read(handle.file, buf, nbyte);
return _read(handle.file, buffer, buffer_size);
}

ssize_t sb_write(int fd, const void* buf, size_t nbyte) {
int write(int fd, const void* buffer, unsigned int count) {
FileOrSocket handle = handle_db_get(fd, false);
if (!handle.is_file) {
return -1;
}
return write(handle.file, buf, nbyte);
return _write(handle.file, buffer, count);
}

int sb_bind(int socket, const struct sockaddr* address, socklen_t address_len) {
Expand Down
1 change: 1 addition & 0 deletions starboard/tools/api_leak_detector/api_leak_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
'pthread_setname_np',
'pthread_getname_np',
'usleep',
'write',
]


Expand Down
43 changes: 28 additions & 15 deletions third_party/musl/src/starboard/network/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,29 @@ int open(const char* path, int oflag, ...) {
memset(value, 0, sizeof(struct FileOrSocket));
value->is_file = true;

int sbFileFlags = 0;
int accessModeFlag = 0;
// Check if mode is specified. Mode is hard-coded to S_IRUSR | S_IWUSR in
// SbFileOpen. Any other modes are not supported.
if (oflag & O_CREAT) {
mode_t sb_file_mode = S_IRUSR | S_IWUSR;
va_list args;
va_start(args, oflag);
mode_t mode = va_arg(args, int);
if (mode != sb_file_mode) {
out_error = kSbFileErrorFailed;
return -1;
}
}

int sb_file_flags = 0;
int access_mode_flag = 0;

if ((oflag & O_ACCMODE) == O_RDONLY) {
accessModeFlag |= kSbFileRead;
access_mode_flag |= kSbFileRead;
} else if ((oflag & O_ACCMODE) == O_WRONLY) {
accessModeFlag |= kSbFileWrite;
access_mode_flag |= kSbFileWrite;
oflag &= ~O_WRONLY;
} else if ((oflag & O_ACCMODE) == O_RDWR) {
accessModeFlag |= kSbFileRead | kSbFileWrite;
access_mode_flag |= kSbFileRead | kSbFileWrite;
oflag &= ~O_RDWR;
} else {
// Applications shall specify exactly one of the first three file access
Expand All @@ -360,33 +373,33 @@ int open(const char* path, int oflag, ...) {
}

if (!oflag) {
sbFileFlags = kSbFileOpenOnly;
sb_file_flags = kSbFileOpenOnly;
}

if (oflag & O_CREAT && oflag & O_EXCL) {
sbFileFlags = kSbFileCreateOnly;
sb_file_flags = kSbFileCreateOnly;
oflag &= ~(O_CREAT | O_EXCL);
}
if (oflag & O_CREAT && oflag & O_TRUNC) {
sbFileFlags = kSbFileCreateAlways;
sb_file_flags = kSbFileCreateAlways;
oflag &= ~(O_CREAT | O_TRUNC);
}
if (oflag & O_CREAT) {
sbFileFlags = kSbFileOpenAlways;
sb_file_flags = kSbFileOpenAlways;
oflag &= ~O_CREAT;
}
if (oflag & O_TRUNC) {
sbFileFlags = kSbFileOpenTruncated;
sb_file_flags = kSbFileOpenTruncated;
oflag &= ~O_TRUNC;
}

// SbFileOpen does not support any other combination of flags.
if (oflag || !sbFileFlags) {
if (oflag || !sb_file_flags) {
out_error = kSbFileErrorFailed;
return -1;
}

int open_flags = sbFileFlags | accessModeFlag;
int open_flags = sb_file_flags | access_mode_flag;

value->file = SbFileOpen(path, open_flags, &out_created, &out_error);
if (!SbFileIsValid(value->file)){
Expand Down Expand Up @@ -423,7 +436,7 @@ ssize_t read(int fildes, void* buf, size_t nbyte) {
return (ssize_t)SbFileRead(fileOrSock->file, buf, (int)nbyte);
}

ssize_t write(int fildes, const void* buf, size_t nbyte) {
ssize_t write(int fildes, const void* buf, size_t nbyte) {
if (fildes < 0) {
errno = EBADF;
return -1;
Expand All @@ -439,8 +452,8 @@ ssize_t read(int fildes, void* buf, size_t nbyte) {
errno = EBADF;
return -1;
}
return (ssize_t)SbFileWrite(fileOrSock->file, buf, (int)nbyte);
}
return (ssize_t)SbFileWrite(fileOrSock->file, buf, (int)nbyte);
}

int socket(int domain, int type, int protocol){
int address_type, socket_protocol;
Expand Down

0 comments on commit ab5fe36

Please sign in to comment.