Skip to content

Commit

Permalink
Introduce POSIX open and close on SB16. (#2460)
Browse files Browse the repository at this point in the history
b/302715109
  • Loading branch information
yjzhang111 authored Mar 1, 2024
1 parent 230c7dd commit fa976d1
Show file tree
Hide file tree
Showing 9 changed files with 437 additions and 24 deletions.
4 changes: 4 additions & 0 deletions starboard/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ since the version previous to it.

## Version 16

### Added standard POSIX file open and close APIs.
The standard API `open` can be used from `fcntl.h` and `close` can be used from
<unistd.h>.

### Added standard POSIX socket bind/listen/connect/accept APIs.
The standard API `bind`, `listen`, `connect`, `accept` can be used from
<sys/socket.h> and `getifaddrs`, `freeifaddrs` can be used from <ifaddrs.h>.
Expand Down
2 changes: 2 additions & 0 deletions starboard/elf_loader/exported_symbols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "starboard/elf_loader/exported_symbols.h"

#include <fcntl.h>
#include <ifaddrs.h>
#include <stdlib.h>
#include <sys/mman.h>
Expand Down Expand Up @@ -428,6 +429,7 @@ ExportedSymbols::ExportedSymbols() {
REGISTER_SYMBOL(mprotect);
REGISTER_SYMBOL(msync);
REGISTER_SYMBOL(munmap);
REGISTER_SYMBOL(open);
REGISTER_SYMBOL(posix_memalign);
REGISTER_SYMBOL(realloc);
REGISTER_SYMBOL(setsockopt);
Expand Down
2 changes: 2 additions & 0 deletions starboard/nplb/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ target(gtest_target_type, "nplb") {
"player_test_util.h",
"player_write_sample_test.cc",
"posix_compliance/posix_arpa_inet_test.cc",
"posix_compliance/posix_file_close_test.cc",
"posix_compliance/posix_file_open_test.cc",
"posix_compliance/posix_memory_map_test.cc",
"posix_compliance/posix_mutex_acquire_test.cc",
"posix_compliance/posix_mutex_acquire_try_test.cc",
Expand Down
36 changes: 36 additions & 0 deletions starboard/nplb/posix_compliance/posix_file_close_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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

// close is partially tested in posix_file_open_test.cc.

#include <unistd.h>

#include "testing/gtest/include/gtest/gtest.h"

namespace starboard {
namespace nplb {
namespace {

TEST(PosixFileCloseTest, CloseInvalidFails) {
int fd = -1;
EXPECT_FALSE(close(fd) == 0);
}

} // namespace
} // namespace nplb
} // namespace starboard

#endif // SB_API_VERSION >= 16
69 changes: 69 additions & 0 deletions starboard/nplb/posix_compliance/posix_file_helpers.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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/nplb/posix_compliance/posix_file_helpers.h"

#include <fcntl.h>
#include <unistd.h>

#include <sstream>
#include <string>
#include <vector>

#include "starboard/configuration_constants.h"
#include "starboard/file.h"
#include "starboard/shared/posix/file_internal.h"
#include "starboard/system.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace starboard {
namespace nplb {

// static
std::string ScopedRandomFile::MakeRandomFilePath() {
std::ostringstream filename_stream;
filename_stream << GetTempDir();
if (!filename_stream.tellp()) {
return "";
}

filename_stream << kSbFileSepChar << MakeRandomFilename();
return filename_stream.str();
}

std::string ScopedRandomFile::MakeRandomFile(int length) {
std::string filename = MakeRandomFilePath();
if (filename.empty()) {
return filename;
}

int file = open(filename.c_str(), O_CREAT | O_WRONLY);
EXPECT_TRUE(fcntl(file, F_GETFD));
if (!fcntl(file, F_GETFD)) {
return "";
}

char* data = new char[length];
for (int i = 0; i < length; ++i) {
data[i] = static_cast<char>(i & 0xFF);
}

bool result = close(file);
EXPECT_TRUE(result) << "Failed to close " << filename;
delete[] data;
return filename;
}

} // namespace nplb
} // namespace starboard
101 changes: 101 additions & 0 deletions starboard/nplb/posix_compliance/posix_file_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// 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_NPLB_POSIX_COMPLIANCE_POSIX_FILE_HELPERS_H_
#define STARBOARD_NPLB_POSIX_COMPLIANCE_POSIX_FILE_HELPERS_H_

#include <string>
#include <vector>

#include "starboard/file.h"

namespace starboard {
namespace nplb {

// Gets the temporary directory in which ScopedRandomFile places its files.
std::string GetTempDir();

// Creates a random file of the given length, and deletes it when the instance
// falls out of scope.
class ScopedRandomFile {
public:
enum {
kDefaultLength = 64,
};

enum Create {
kCreate,
kDontCreate,
};

// Will create a file of |kDefaultLength| bytes long.
ScopedRandomFile() : size_(kDefaultLength) {
filename_ = MakeRandomFile(size_);
}

// Will create a file |length| bytes long.
explicit ScopedRandomFile(int length) : size_(length) {
filename_ = MakeRandomFile(size_);
}

// Will either create a file |length| bytes long, or will just generate a
// filename. |create| is whether to create the file or not.
ScopedRandomFile(int length, Create create) : size_(length) {
filename_ =
(create == kCreate ? MakeRandomFile(size_) : MakeRandomFilePath());
}

// Will either create a file of |kDefaultLength| bytes long, or will just
// generate a filename. |create| is whether to create the file or not.
explicit ScopedRandomFile(Create create) : size_(kDefaultLength) {
filename_ =
(create == kCreate ? MakeRandomFile(size_) : MakeRandomFilePath());
}

~ScopedRandomFile() { SbFileDelete(filename_.c_str()); }

// Creates and returns a random filename (no path), but does not create the
// file.
static std::string MakeRandomFilename();

// Returns the filename generated for this file.
const std::string& filename() const { return filename_; }

// Returns the SPECIFIED size of the file (not the size returned by the
// filesystem).
const int size() const { return size_; }

// Checks |buffer| of size |size| against this class's write pattern, offset
// by |pattern_offset|. Failures print the original line number |line|.
static void ExpectPattern(int pattern_offset,
void* buffer,
int size,
int line);

private:
// Creates a file with a random name and |length| bytes, returning the path to
// the new file.
static std::string MakeRandomFile(int length);

// Creates and returns a path to a random file, but does not create the file.
static std::string MakeRandomFilePath();

std::string filename_;
int size_;
};

} // namespace nplb
} // namespace starboard

#endif // STARBOARD_NPLB_POSIX_COMPLIANCE_POSIX_FILE_HELPERS_H_
135 changes: 135 additions & 0 deletions starboard/nplb/posix_compliance/posix_file_open_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// 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 <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

#include "starboard/file.h"
#include "starboard/nplb/posix_compliance/posix_file_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace starboard {
namespace nplb {
namespace {

void BasicTest(bool existing,
int open_flags,
bool expected_created,
bool expected_success,
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();
#define SB_FILE_OPEN_TEST_CONTEXT \
"existing=" << existing << ", flags=0x" << std::hex << open_flags \
<< std::dec << ", expected_created=" << expected_created \
<< ", expected_success=" << expected_success \
<< ", filename=" << filename \
<< ", original_line=" << original_line

if (!existing) {
EXPECT_FALSE(SbFileExists(filename.c_str())) << SB_FILE_OPEN_TEST_CONTEXT;
if (SbFileExists(filename.c_str())) {
return;
}
}

bool created = !expected_created;
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;

// Try to clean up in case test fails.
if (!(fd < 0)) {
close(fd);
}
} else {
EXPECT_TRUE(fd >= 0);
if (fd >= 0) {
int result = close(fd);
EXPECT_TRUE(result == 0) << SB_FILE_OPEN_TEST_CONTEXT;
}
}
#undef SB_FILE_OPEN_TEST_CONTEXT
}

TEST(PosixFileOpenTest, OpenOnlyOpensExistingFile) {
BasicTest(true, O_RDONLY, false, true, __LINE__);
}

TEST(PosixFileOpenTest, OpenOnlyDoesNotOpenNonExistingFile) {
BasicTest(false, O_RDONLY, false, false, __LINE__);
}

TEST(PosixFileOpenTest, CreateOnlyDoesNotCreateExistingFile) {
BasicTest(true, O_CREAT | O_EXCL | O_WRONLY, false, false, __LINE__);
}

TEST(PosixFileOpenTest, CreateOnlyCreatesNonExistingFile) {
BasicTest(false, O_CREAT | O_EXCL | O_WRONLY, true, true, __LINE__);
}

TEST(PosixFileOpenTest, OpenAlwaysOpensExistingFile) {
BasicTest(true, O_CREAT | O_WRONLY, false, true, __LINE__);
}

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__);
}

TEST(PosixFileOpenTest, CreateAlwaysCreatesNonExistingFile) {
BasicTest(false, O_CREAT | O_TRUNC | O_WRONLY, true, true, __LINE__);
}

TEST(PosixFileOpenTest, OpenTruncatedTruncatesExistingFile) {
BasicTest(true, O_TRUNC | O_WRONLY, false, true, __LINE__);
}

TEST(PosixFileOpenTest, OpenTruncatedDoesNotCreateNonExistingFile) {
BasicTest(false, O_TRUNC | O_WRONLY, false, false, __LINE__);
}

} // namespace
} // namespace nplb
} // namespace starboard

#endif // SB_API_VERSION >= 16
Loading

0 comments on commit fa976d1

Please sign in to comment.