Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaup SbFile usage. #3225

Merged
merged 6 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion base/files/file_enumerator_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,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
6 changes: 3 additions & 3 deletions base/files/file_util_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "starboard/configuration_constants.h"
#include "starboard/common/file.h"
#include "starboard/directory.h"
#include "starboard/file.h"
#include "base/strings/strcat.h"
#include "starboard/system.h"

Expand Down Expand Up @@ -248,12 +248,12 @@ bool PathExists(const FilePath &path) {

bool PathIsReadable(const FilePath &path) {
internal::AssertBlockingAllowed();
return SbFileCanOpen(path.value().c_str(), kSbFileOpenAlways | kSbFileRead);
return starboard::FileCanOpen(path.value().c_str(), O_CREAT | O_RDONLY);
}

bool PathIsWritable(const FilePath &path) {
internal::AssertBlockingAllowed();
return SbFileCanOpen(path.value().c_str(), kSbFileOpenAlways | kSbFileWrite);
return starboard::FileCanOpen(path.value().c_str(), O_CREAT | O_WRONLY);
}

bool DirectoryExists(const FilePath& path) {
Expand Down
5 changes: 3 additions & 2 deletions cobalt/media/sandbox/format_guesstimator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "media/filters/chunk_demuxer.h"
#include "net/base/filename_util.h"
#include "net/base/url_util.h"
#include "starboard/common/file.h"
#include "starboard/memory.h"
#include "starboard/types.h"
#include "ui/gfx/geometry/size.h"
Expand Down Expand Up @@ -81,7 +82,7 @@ base::FilePath ResolvePath(const std::string& path) {
base::PathService::Get(base::DIR_TEST_DATA, &content_path);
result = content_path.Append(result);
}
if (SbFileCanOpen(result.value().c_str(), kSbFileOpenOnly | kSbFileRead)) {
if (starboard::FileCanOpen(result.value().c_str(), O_RDONLY)) {
return result;
}
LOG(WARNING) << "Failed to resolve path \"" << path << "\" as \""
Expand Down Expand Up @@ -141,7 +142,7 @@ FormatGuesstimator::FormatGuesstimator(const std::string& path_or_url,
return;
}
base::FilePath path = ResolvePath(path_or_url);
if (path.empty() || !SbFileCanOpen(path.value().c_str(), kSbFileRead)) {
if (path.empty() || !starboard::FileCanOpen(path.value().c_str(), O_RDONLY)) {
return;
}
InitializeAsAdaptive(path, media_module);
Expand Down
74 changes: 39 additions & 35 deletions cobalt/renderer/rasterizer/skia/skia/src/ports/SkOSFile_cobalt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

#include "cobalt/renderer/rasterizer/skia/skia/src/ports/SkOSFile_cobalt.h"

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

#include "SkString.h"
#include "SkTFitsIn.h"
#include "SkTemplates.h"
Expand All @@ -16,101 +19,101 @@
#include "base/files/file_util.h"
#include "base/optional.h"
#include "base/path_service.h"
#include "starboard/common/file.h"
#include "starboard/common/file_wrapper.h"

// Implement functionality declared in SkOSFile.h via primitives provided
// by Chromium. In doing this, we need only ensure that support for Chromium
// file utilities is working and then Skia file utilities will also work.

namespace {

SbFile ToSbFile(FILE* sk_file) {
FilePtr ToFilePtr(FILE* sk_file) {
// PlatformFile is a pointer type in Starboard, so we cannot use static_cast
// from intptr_t.
return reinterpret_cast<SbFile>(sk_file);
return reinterpret_cast<FilePtr>(sk_file);
}

FILE* ToFILE(SbFile starboard_file) {
FILE* ToFILE(FilePtr starboard_file) {
return reinterpret_cast<FILE*>(starboard_file);
}

int ToSbFileFlags(SkFILE_Flags sk_flags) {
int ToFileFlags(SkFILE_Flags sk_flags) {
int flags = 0;
if (sk_flags & kRead_SkFILE_Flag) {
if (sk_flags & kWrite_SkFILE_Flag) {
flags |= kSbFileWrite;
flags |= O_WRONLY;
}
flags |= kSbFileOpenOnly | kSbFileRead;
flags |= O_RDONLY;
} else if (sk_flags & kWrite_SkFILE_Flag) {
flags |= kSbFileOpenAlways | kSbFileWrite;
flags |= O_CREAT | O_WRONLY;
}
return flags;
}

} // namespace

FILE* sk_fopen(const char path[], SkFILE_Flags sk_flags) {
SbFile starboard_file = SbFileOpen(path, ToSbFileFlags(sk_flags), NULL, NULL);
// TODO: temporarily replace with kSbFileInvalid, will be deprecated with
// SBFile.
if (starboard_file == kSbFileInvalid) {
FilePtr file = file_open(path, ToFileFlags(sk_flags));
if (!file || file->fd < 0) {
return nullptr;
}

return ToFILE(starboard_file);
return ToFILE(file);
}

void sk_fclose(FILE* sk_file) {
SkASSERT(sk_file);
SbFileClose(ToSbFile(sk_file));
int ret = file_close(ToFilePtr(sk_file));
}

size_t sk_fgetsize(FILE* sk_file) {
SkASSERT(sk_file);
SbFile file = ToSbFile(sk_file);
FilePtr file = ToFilePtr(sk_file);

// Save current position so we can restore it.
int64_t current_position = SbFileSeek(file, kSbFileFromCurrent, 0);
int64_t current_position = lseek(file->fd, 0, SEEK_CUR);
if (current_position < 0) {
return 0;
}

// Find the file size by seeking to the end.
int64_t size = SbFileSeek(file, kSbFileFromEnd, 0);
int64_t size = lseek(file->fd, 0, SEEK_END);
if (size < 0) {
size = 0;
}

// Restore original file position.
SbFileSeek(file, kSbFileFromBegin, current_position);
lseek(file->fd, current_position, SEEK_SET);
return size;
}

size_t sk_fwrite(const void* buffer, size_t byteCount, FILE* sk_file) {
SkASSERT(sk_file);
SbFile file = ToSbFile(sk_file);
FilePtr file = ToFilePtr(sk_file);
int result =
SbFileWrite(file, reinterpret_cast<const char*>(buffer), byteCount);
write(file->fd, reinterpret_cast<const char*>(buffer), byteCount);
base::RecordFileWriteStat(result);
return result;
}

void sk_fflush(FILE* sk_file) {
SkASSERT(sk_file);
SbFile file = ToSbFile(sk_file);
SbFileFlush(file);
FilePtr file = ToFilePtr(sk_file);
fsync(file->fd);
}

bool sk_fseek(FILE* sk_file, size_t position) {
SkASSERT(sk_file);
SbFile file = ToSbFile(sk_file);
int64_t new_position = SbFileSeek(file, kSbFileFromBegin, position);
FilePtr file = ToFilePtr(sk_file);
int64_t new_position = lseek(file->fd, position, SEEK_SET);
return new_position == position;
}

size_t sk_ftell(FILE* sk_file) {
SkASSERT(sk_file);
SbFile file = ToSbFile(sk_file);
return SbFileSeek(file, kSbFileFromCurrent, 0);
FilePtr file = ToFilePtr(sk_file);
return lseek(file->fd, 0, SEEK_CUR);
}

void* sk_fmmap(FILE* sk_file, size_t* length) {
Expand Down Expand Up @@ -154,28 +157,29 @@ bool sk_mkdir(const char* path) {

void sk_fsync(FILE* f) {
SkASSERT(f);
SbFile file = ToSbFile(f);
FilePtr file = ToFilePtr(f);
// Technically, flush doesn't have to call sync... but this is the best
// effort we can make.
SbFileFlush(file);
fsync(file->fd);
}

size_t sk_qread(FILE* file, void* buffer, size_t count, size_t offset) {
SkASSERT(file);
SbFile starboard_file = ToSbFile(file);
FilePtr starboard_file = ToFilePtr(file);

int original_position = SbFileSeek(starboard_file, kSbFileFromCurrent, 0);
int original_position = lseek(starboard_file->fd, 0, SEEK_CUR);
if (original_position < 0) {
return SIZE_MAX;
}

int position = SbFileSeek(starboard_file, kSbFileFromBegin, offset);
int position = lseek(starboard_file->fd, offset, SEEK_SET);
int result = 0;
if (position == offset) {
result = SbFileReadAll(starboard_file, reinterpret_cast<char*>(buffer),
result =
starboard::ReadAll(starboard_file->fd, reinterpret_cast<char*>(buffer),
static_cast<int>(count));
}
position = SbFileSeek(starboard_file, kSbFileFromBegin, original_position);
position = lseek(starboard_file->fd, original_position, SEEK_SET);
if (result < 0 || position < 0) {
return SIZE_MAX;
} else {
Expand All @@ -185,7 +189,7 @@ size_t sk_qread(FILE* file, void* buffer, size_t count, size_t offset) {

size_t sk_fread(void* buffer, size_t byteCount, FILE* file) {
SkASSERT(file);
SbFile starboard_file = ToSbFile(file);
return SbFileReadAll(starboard_file, reinterpret_cast<char*>(buffer),
byteCount);
FilePtr starboard_file = ToFilePtr(file);
return starboard::ReadAll(starboard_file->fd, reinterpret_cast<char*>(buffer),
byteCount);
}
6 changes: 4 additions & 2 deletions components/update_client/action_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "components/update_client/action_runner.h"

#include <unistd.h>

#include <iterator>
#include <stack>
#include <utility>
Expand Down Expand Up @@ -42,11 +44,11 @@ void CleanupDirectory(base::FilePath& dir) {
if (info.IsDirectory()) {
directories.push(path.value());
} else {
SbFileDelete(path.value().c_str());
unlink(path.value().c_str());
}
}
while (!directories.empty()) {
SbFileDelete(directories.top().c_str());
rmdir(directories.top().c_str());
directories.pop();
}
}
Expand Down
4 changes: 2 additions & 2 deletions components/update_client/url_fetcher_downloader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ void CleanupDirectory(base::FilePath& dir) {
if (info.IsDirectory()) {
directories.push(path.value());
} else {
SbFileDelete(path.value().c_str());
unlink(path.value().c_str());
}
}
while (!directories.empty()) {
SbFileDelete(directories.top().c_str());
rmdir(directories.top().c_str());
directories.pop();
}
}
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 @@ -13,7 +13,9 @@
// limitations under the License.

#include <dirent.h>
#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 @@ -122,52 +124,49 @@ bool CopyDirContents(const std::string& src_dir_path,

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

closedir(src_dir);
Expand Down
Loading
Loading