Skip to content

Commit

Permalink
Add test case for file modes with no Windows equivalent.
Browse files Browse the repository at this point in the history
Change-Id: I4295eca915f886c816b5cb947800e20ab71a3fdc
  • Loading branch information
yjzhang111 committed Mar 1, 2024
1 parent e99ffe9 commit c75c806
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
27 changes: 23 additions & 4 deletions starboard/nplb/posix_compliance/posix_file_open_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ void BasicTest(bool existing,
int open_flags,
bool expected_created,
bool expected_success,
int original_line) {
int original_line,
mode_t mode = S_IRUSR | S_IWUSR) {
ScopedRandomFile random_file(existing ? ScopedRandomFile::kCreate
: ScopedRandomFile::kDontCreate);
const std::string& filename = random_file.filename();
Expand All @@ -49,9 +50,22 @@ void BasicTest(bool existing,
}

bool created = !expected_created;
mode_t mode = S_IRUSR | S_IWUSR;
int fd = (open_flags & O_CREAT) ? open(filename.c_str(), open_flags, mode)
: open(filename.c_str(), open_flags);
int fd;
#ifdef _WIN32
// File mode is set along with O_CREAT flag.
// Windows only supports 1)_S_IREAD, which is mapped to S_IRUSR, 2) _S_IWRITE,
// which is mapped to S_IWUSR, and 3) _S_IREAD | _S_IWRITE.
if (open_flags & O_CREAT && (open_flags == S_IRUSR || open_flags == S_IWUSR ||
open_flags == (S_IRUSR | S_IWUSR))) {
fd = open(filename.c_str(), open_flags, mode);
} else {
fd = open(filename.c_str(), open_flags);
}
#else
fd = (open_flags & O_CREAT) ? open(filename.c_str(), open_flags, mode)
: open(filename.c_str(), open_flags);
#endif

if (!expected_success) {
EXPECT_FALSE(fd >= 0) << SB_FILE_OPEN_TEST_CONTEXT;

Expand Down Expand Up @@ -93,6 +107,11 @@ TEST(PosixFileOpenTest, OpenAlwaysCreatesNonExistingFile) {
BasicTest(false, O_CREAT | O_WRONLY, true, true, __LINE__);
}

TEST(PosixFileOpenTest, OpenAlwaysWithLinuxSpecificMode) {
BasicTest(false, O_CREAT | O_TRUNC | O_WRONLY, true, true, __LINE__,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
}

TEST(PosixFileOpenTest, CreateAlwaysTruncatesExistingFile) {
BasicTest(true, O_CREAT | O_TRUNC | O_WRONLY, true, true, __LINE__);
}
Expand Down
20 changes: 18 additions & 2 deletions starboard/shared/win32/posix_emu/include/fcntl.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,24 @@

typedef int mode_t;

static const mode_t S_IRUSR = (mode_t)_S_IREAD;
static const mode_t S_IWUSR = (mode_t)_S_IWRITE;
// For the POSIX modes that do not have a Windows equivalent, the modes
// defined here use the POSIX values left shifted 16 bits.
// Passing these into Windows file I/O functions has no effect.

static const mode_t S_ISUID = 0x40000000;
static const mode_t S_ISGID = 0x20000000;
static const mode_t S_ISVTX = 0x10000000;
static const mode_t S_IRWXU = 0x07000000;
static const mode_t S_IRUSR = _S_IREAD; // read by user
static const mode_t S_IWUSR = _S_IWRITE; // write by user
static const mode_t S_IXUSR = 0x01000000;
static const mode_t S_IRGRP = 0x00400000;
static const mode_t S_IWGRP = 0x00200000;
static const mode_t S_IXGRP = 0x00100000;
static const mode_t S_IRWXO = 0x00070000;
static const mode_t S_IROTH = 0x00040000;
static const mode_t S_IWOTH = 0x00020000;
static const mode_t S_IXOTH = 0x00010000;

static const mode_t MS_MODE_MASK = 0x0000ffff;

Expand Down
5 changes: 4 additions & 1 deletion starboard/shared/win32/posix_emu/socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ int open(const char* path, int oflag, ...) {
int close(int fd) {
FileOrSocket handle = handle_db_get(fd, true);

if (!handle.is_file) {
if (!handle.is_file && handle.socket == INVALID_SOCKET) {
// TODO: update errno with file operation error
return -1;
} else if (!handle.is_file) {
return closesocket(handle.socket);
}

Expand Down
1 change: 0 additions & 1 deletion starboard/xb1/platform_configuration/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ config("target") {
"iso_stdio_wide_specifiers.lib",
"mfplat.lib",
"mfuuid.lib",
"oldnames.lib",
"windowsapp.lib",
]
ldflags += [
Expand Down

0 comments on commit c75c806

Please sign in to comment.