Skip to content

Commit

Permalink
Clean up SbFile references.
Browse files Browse the repository at this point in the history
  • Loading branch information
yjzhang111 committed Jul 13, 2024
1 parent a285aff commit 6952922
Show file tree
Hide file tree
Showing 36 changed files with 366 additions and 293 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
72 changes: 39 additions & 33 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,103 @@
#include "base/files/file_util.h"
#include "base/optional.h"
#include "base/path_service.h"
#include "starboard/common/file.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) {
starboard::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<starboard::FilePtr>(sk_file);
}

FILE* ToFILE(SbFile starboard_file) {
FILE* ToFILE(starboard::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);
starboard::FilePtr file;
file->fd = open(path, ToFileFlags(sk_flags), S_IRUSR | S_IWUSR);
// TODO: temporarily replace with kSbFileInvalid, will be deprecated with
// SBFile.
if (starboard_file == kSbFileInvalid) {
if (!file || !starboard::IsValid(file->fd)) {
return nullptr;
}

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

void sk_fclose(FILE* sk_file) {
SkASSERT(sk_file);
SbFileClose(ToSbFile(sk_file));
close(ToFilePtr(sk_file)->fd);
}

size_t sk_fgetsize(FILE* sk_file) {
SkASSERT(sk_file);
SbFile file = ToSbFile(sk_file);
starboard::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);
starboard::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);
starboard::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);
starboard::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);
starboard::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 +159,29 @@ bool sk_mkdir(const char* path) {

void sk_fsync(FILE* f) {
SkASSERT(f);
SbFile file = ToSbFile(f);
starboard::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);
starboard::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 +191,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);
starboard::FilePtr starboard_file = ToFilePtr(file);
return starboard::ReadAll(starboard_file->fd, reinterpret_cast<char*>(buffer),
byteCount);
}
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
35 changes: 11 additions & 24 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 @@ -41,45 +43,30 @@ static SB_C_FORCE_INLINE time_t WindowsUsecToTimeTAndroid(int64_t time) {
return posix_time;
}

static void MapSbFileInfoToStat(SbFileInfo* file_info, struct stat* stat_info) {
stat_info->st_mode = 0;
if (file_info->is_directory) {
stat_info->st_mode = S_IFDIR;
} else if (file_info->is_symbolic_link) {
stat_info->st_mode = S_IFLNK;
}

stat_info->st_ctime = WindowsUsecToTimeTAndroid(file_info->creation_time);
stat_info->st_atime = WindowsUsecToTimeTAndroid(file_info->last_accessed);
stat_info->st_mtime = WindowsUsecToTimeTAndroid(file_info->last_modified);
stat_info->st_size = file_info->size;
}

// This needs to be exported to ensure shared_library targets include it.
int __wrap_stat(const char* path, struct stat* info) {
// SbFileExists(path) implementation for Android
if (!IsAndroidAssetPath(path)) {
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);
MapSbFileInfoToStat(&out_info, info);
SbFileClose(file);
bool result = !fstat(file, &out_info);
close(file);
return 0;
}

// Values from SbFileGetPathInfo
if (IsAndroidAssetPath(path)) {
AAssetDir* asset_dir = OpenAndroidAssetDir(path);
if (asset_dir) {
info->st_mode = S_IFDIR;
info->st_ctime = 0;
info->st_atime = 0;
info->st_mtime = 0;
info->st_size = 0;
out_info.st_mode = S_IFDIR;
out_info.st_ctime = 0;
out_info.st_atime = 0;
out_info.st_mtime = 0;
out_info.st_size = 0;
AAssetDir_close(asset_dir);
return 0;
}
Expand Down
5 changes: 5 additions & 0 deletions starboard/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ static_library("common") {
]
}

static_library("file_wrapper") {
check_includes = false
sources = [ "file_wrapper.h" ]
}

target(gtest_target_type, "common_test") {
testonly = true
sources = [
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
Loading

0 comments on commit 6952922

Please sign in to comment.