Skip to content

Commit

Permalink
Clean up SbFile references.
Browse files Browse the repository at this point in the history
Change-Id: I216b6067fb2dcdb95424a19a4a720b6ebf07fac3
  • Loading branch information
yjzhang111 committed Jul 9, 2024
1 parent e6c2d61 commit 1fa2328
Show file tree
Hide file tree
Showing 32 changed files with 230 additions and 193 deletions.
2 changes: 1 addition & 1 deletion base/files/file_enumerator_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ std::vector<FileEnumerator::FileInfo> 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;
Expand Down
35 changes: 17 additions & 18 deletions starboard/android/shared/android_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

#include "game-activity/GameActivity.h"
#include "starboard/android/shared/application_android.h"
Expand Down Expand Up @@ -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<int>(info.size);
int file_size = static_cast<int>(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);
Expand Down
10 changes: 6 additions & 4 deletions starboard/android/shared/posix_emu/stat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

#include <android/asset_manager.h>

Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 4 additions & 0 deletions starboard/common/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion starboard/common/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<off_t>(offset), static_cast<int>(whence));
Expand Down
22 changes: 13 additions & 9 deletions starboard/elf_loader/file_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

#include "starboard/elf_loader/file_impl.h"

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

#include "starboard/common/file.h"
#include "starboard/common/log.h"
#include "starboard/elf_loader/log.h"

Expand All @@ -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();
Expand All @@ -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;
}
Expand All @@ -51,25 +55,25 @@ 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;
}

void FileImpl::Close() {
if (file_) {
SbFileClose(file_);
close(file_);
}
}

Expand Down
2 changes: 1 addition & 1 deletion starboard/elf_loader/file_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions starboard/elf_loader/lz4_file_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <sys/stat.h>

#include <algorithm>
#include <vector>

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();
Expand Down
7 changes: 4 additions & 3 deletions starboard/loader_app/drain_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "starboard/loader_app/drain_file.h"

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

#include <algorithm>
#include <cstring>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
<< "'";
}
Expand All @@ -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 << "'";
}
}
Expand Down
18 changes: 9 additions & 9 deletions starboard/loader_app/installation_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

#include "starboard/loader_app/installation_manager.h"

#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

#include <memory>
#include <set>
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
19 changes: 10 additions & 9 deletions starboard/loader_app/installation_manager_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#include "starboard/loader_app/installation_manager.h"

#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

#include <string>
#include <vector>
Expand Down Expand Up @@ -71,18 +73,17 @@ class InstallationManagerTest : public ::testing::TestWithParam<int> {
}

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|
Expand Down Expand Up @@ -200,11 +201,11 @@ class InstallationManagerTest : public ::testing::TestWithParam<int> {
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:
Expand Down
Loading

0 comments on commit 1fa2328

Please sign in to comment.