Skip to content

Commit

Permalink
Cherry pick PR #4012: Deprecate SbFile, SbFileError, SbFileInfo types. (
Browse files Browse the repository at this point in the history
#4042)

Refer to the original PR: #4012

b/302715109

---------

Co-authored-by: Yijia Zhang <[email protected]>
  • Loading branch information
cobalt-github-releaser-bot and yjzhang111 committed Aug 27, 2024
1 parent dd1af1d commit e019703
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 41 deletions.
4 changes: 0 additions & 4 deletions base/files/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,6 @@ class BASE_EXPORT File {
#endif
};

#if defined(STARBOARD)
void SetLastFileError(File::Error error);
#endif

} // namespace base

#endif // BASE_FILES_FILE_H_
15 changes: 3 additions & 12 deletions base/files/file_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@

namespace base {

namespace {
SbFileError g_sb_file_error = kSbFileOk;
} // namespace

// TODO: remove SbFileError
void SetLastFileError(File::Error error) {
g_sb_file_error = static_cast<SbFileError>(error);
}

void RecordFileWriteStat(int write_file_result) {
auto& stats_tracker =
starboard::StatsTrackerContainer::GetInstance()->stats_tracker();
Expand All @@ -53,9 +44,9 @@ void RecordFileWriteStat(int write_file_result) {
}

// Make sure our Whence mappings match the system headers.
static_assert(File::FROM_BEGIN == static_cast<int>(kSbFileFromBegin) &&
File::FROM_CURRENT == static_cast<int>(kSbFileFromCurrent) &&
File::FROM_END == static_cast<int>(kSbFileFromEnd),
static_assert(File::FROM_BEGIN == static_cast<int>(SEEK_SET) &&
File::FROM_CURRENT == static_cast<int>(SEEK_CUR) &&
File::FROM_END == static_cast<int>(SEEK_END),
"Whence enums from base must match those of Starboard.");

void File::Info::FromStat(const stat_wrapper_t& stat_info) {
Expand Down
2 changes: 1 addition & 1 deletion base/memory/platform_shared_memory_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct BASE_EXPORT ScopedFDPair {

// Platform-specific shared memory type used by the shared memory system.
#if defined(STARBOARD)
using PlatformSharedMemoryHandle = SbFile;
using PlatformSharedMemoryHandle = int;
using ScopedPlatformSharedMemoryHandle = ScopedFD;
#elif BUILDFLAG(IS_APPLE)
using PlatformSharedMemoryHandle = mach_port_t;
Expand Down
1 change: 0 additions & 1 deletion net/cert/internal/trust_store_in_memory_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ std::shared_ptr<const ParsedCertificate> TrustStoreInMemoryStarboard::TryLoadCer
return nullptr;
}

SbFileError out_error;
char cert_buffer[kCertBufferSize];
base::FilePath cert_path = GetCertificateDirPath().Append(cert_file_name);
base::File cert_file(cert_path, base::File::Flags::FLAG_OPEN | base::File::Flags::FLAG_READ);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ TEST(PosixDirectoryOpenTest, FailsInvalidPath) {
struct stat info;
ASSERT_FALSE(stat(path.c_str(), &info) == 0);

SbFileError error = kSbFileErrorMax;
DIR* directory = opendir(path.c_str());
EXPECT_FALSE(directory != NULL);
if (directory != NULL) {
Expand Down
106 changes: 106 additions & 0 deletions starboard/shared/win32/file_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "starboard/shared/win32/file_internal.h"

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

#include <windows.h>
Expand All @@ -23,6 +25,8 @@
#include "starboard/shared/win32/error_utils.h"
#include "starboard/shared/win32/wchar_utils.h"

#define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)

namespace sbwin32 = starboard::shared::win32;

namespace starboard {
Expand Down Expand Up @@ -81,6 +85,108 @@ std::wstring NormalizeWin32Path(std::wstring str) {
return NormalizePathSeparator(str);
}

HANDLE OpenFileOrDirectory(const char* path, int flags) {
// Note that FILE_SHARE_DELETE allows a file to be deleted while there
// are other handles open for read/write. This is necessary for the
// Async file tests which, due to system timing, will sometimes have
// outstanding handles open and fail to delete, failing the test.
const DWORD share_mode =
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;

DWORD desired_access = 0;
if ((flags & O_ACCMODE) == O_RDONLY) {
desired_access |= GENERIC_READ;
} else if ((flags & O_ACCMODE) == O_WRONLY) {
desired_access |= GENERIC_WRITE;
flags &= ~O_WRONLY;
} else if ((flags & O_ACCMODE) == O_RDWR) {
desired_access |= GENERIC_READ | GENERIC_WRITE;
flags &= ~O_RDWR;
} else {
// Applications shall specify exactly one of the first three file access
// modes.
errno = EINVAL;
return INVALID_HANDLE_VALUE;
}

DWORD creation_disposition = 0;
if (!flags) {
creation_disposition = OPEN_EXISTING;
}

if (flags & O_CREAT && flags & O_EXCL) {
SB_DCHECK(!creation_disposition);
creation_disposition = CREATE_NEW;
flags &= ~(O_CREAT | O_EXCL);
}

if (flags & O_CREAT && flags & O_TRUNC) {
SB_DCHECK(!creation_disposition);
creation_disposition = CREATE_ALWAYS;
flags &= ~(O_CREAT | O_TRUNC);
}

if (flags & O_TRUNC) {
SB_DCHECK(!creation_disposition);
SB_DCHECK((flags & O_WRONLY) || (flags & O_RDWR));
creation_disposition = TRUNCATE_EXISTING;
flags &= ~O_TRUNC;
}

if (flags & O_CREAT) {
SB_DCHECK(!creation_disposition);
creation_disposition = OPEN_ALWAYS;
flags &= ~O_CREAT;
}

// SbFileOpen does not support any other combination of flags.
if (flags || !creation_disposition) {
errno = ENOTSUP;
return INVALID_HANDLE_VALUE;
}

SB_DCHECK(desired_access != 0) << "Invalid permission flag.";

std::wstring path_wstring = NormalizeWin32Path(path);
CREATEFILE2_EXTENDED_PARAMETERS create_ex_params = {0};
// Enabling |FILE_FLAG_BACKUP_SEMANTICS| allows us to figure out if the path
// is a directory.
create_ex_params.dwFileFlags = FILE_FLAG_BACKUP_SEMANTICS;
create_ex_params.dwFileFlags |= FILE_FLAG_POSIX_SEMANTICS;
create_ex_params.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
create_ex_params.dwSecurityQosFlags = SECURITY_ANONYMOUS;
create_ex_params.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);

HANDLE file_handle =
CreateFile2(path_wstring.c_str(), desired_access, share_mode,
creation_disposition, &create_ex_params);

const DWORD last_error = GetLastError();

if (!starboard::shared::win32::IsValidHandle(file_handle)) {
switch (last_error) {
case ERROR_ACCESS_DENIED:
errno = EACCES;
break;
case ERROR_FILE_EXISTS: {
if (creation_disposition == CREATE_NEW) {
errno = EEXIST;
} else {
errno = EPERM;
}
break;
}
case ERROR_FILE_NOT_FOUND:
errno = ENOENT;
break;
default:
errno = EPERM;
}
}

return file_handle;
}

HANDLE OpenFileOrDirectory(const char* path,
int flags,
bool* out_created,
Expand Down
3 changes: 3 additions & 0 deletions starboard/shared/win32/file_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class SbFilePrivate {
SbFilePrivate(const SbFilePrivate&) = delete;
SbFilePrivate& operator=(const SbFilePrivate&) = delete;
};

#pragma warning(pop)

namespace starboard {
Expand Down Expand Up @@ -87,6 +88,8 @@ inline bool PathEndsWith(const std::wstring& path, const wchar_t* filename) {
std::wstring NormalizeWin32Path(std::string str);
std::wstring NormalizeWin32Path(std::wstring str);

HANDLE OpenFileOrDirectory(const char* path, int flags);

HANDLE OpenFileOrDirectory(const char* path,
int flags,
bool* out_created,
Expand Down
5 changes: 2 additions & 3 deletions starboard/shared/win32/log_file_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@ void OpenLogInCacheDirectory(const char* log_file_name, int creation_flags) {
}

void OpenLogFile(const char* path, const int creation_flags) {
SB_DCHECK((creation_flags & kSbFileOpenAlways) ||
(creation_flags & kSbFileCreateAlways));
SB_DCHECK(creation_flags & O_CREAT);
SB_DLOG(INFO) << "Logging to [" << path << "]";

int flags = creation_flags | kSbFileWrite;
int flags = creation_flags | O_WRONLY;

pthread_mutex_lock(&log_mutex);
CloseLogFileWithoutLock();
Expand Down
13 changes: 5 additions & 8 deletions starboard/shared/win32/minidump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// clang-format on
#include <crtdbg.h>
#include <dbghelp.h>
#include <fcntl.h>
#include <sstream>
#include <string>

Expand Down Expand Up @@ -72,16 +73,12 @@ class DumpHandler {
SbLogRaw("Could not write minidump because the dump path is missing.");
return;
}
bool out_created = false;
SbFileError out_error = kSbFileOk;

HANDLE file_handle = OpenFileOrDirectory(file_path_.c_str(),
kSbFileCreateAlways | kSbFileWrite,
&out_created, &out_error);
HANDLE file_handle =
OpenFileOrDirectory(file_path_.c_str(), O_CREAT | O_TRUNC | O_WRONLY);

const bool file_ok = out_created && (out_error == kSbFileOk) &&
(file_handle != NULL) &&
(file_handle != INVALID_HANDLE_VALUE);
const bool file_ok =
(file_handle != NULL) && (file_handle != INVALID_HANDLE_VALUE);

if (!file_ok) {
std::stringstream ss;
Expand Down
6 changes: 3 additions & 3 deletions starboard/shared/win32/posix_emu/dirent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <dirent.h>

#include <fcntl.h>
#include <windows.h>
#include <map>

Expand Down Expand Up @@ -163,9 +164,8 @@ DIR* opendir(const char* path) {
return nullptr;
}

SbFileError out_error;
HANDLE directory_handle = starboard::shared::win32::OpenFileOrDirectory(
path, kSbFileOpenOnly | kSbFileRead, nullptr, &out_error);
HANDLE directory_handle =
starboard::shared::win32::OpenFileOrDirectory(path, O_RDONLY);

if (!starboard::shared::win32::IsValidHandle(directory_handle)) {
errno = EBADF;
Expand Down
1 change: 1 addition & 0 deletions third_party/libjpeg_turbo/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ source_set("libjpeg_headers") {
public_deps = [
"//starboard:starboard_headers_only",
"//starboard/common",
"//starboard/common:file_wrapper",
]
}
}
Expand Down
10 changes: 5 additions & 5 deletions third_party/libjpeg_turbo/cdjpeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct cjpeg_source_struct {
void (*finish_input) (j_compress_ptr cinfo, cjpeg_source_ptr sinfo);

#if defined(STARBOARD)
SbFile *input_file;
FileStruct *input_file;
#else
FILE *input_file;
#endif
Expand Down Expand Up @@ -73,7 +73,7 @@ struct djpeg_dest_struct {

/* Target file spec; filled in by djpeg.c after object is created. */
#if defined(STARBOARD)
SbFile *output_file;
FileStruct *output_file;
#else
FILE *output_file;
#endif
Expand Down Expand Up @@ -135,7 +135,7 @@ EXTERN(boolean) set_sample_factors(j_compress_ptr cinfo, char *arg);
/* djpeg support routines (in rdcolmap.c) */

#if defined(STARBOARD)
EXTERN(void) read_color_map(j_decompress_ptr cinfo, SbFile *infile);
EXTERN(void) read_color_map(j_decompress_ptr cinfo, FileStruct *infile);
#else
EXTERN(void) read_color_map(j_decompress_ptr cinfo, FILE *infile);
#endif
Expand All @@ -148,8 +148,8 @@ EXTERN(void) end_progress_monitor(j_common_ptr cinfo);
EXTERN(boolean) keymatch(char *arg, const char *keyword, int minchars);

#if defined(STARBOARD)
EXTERN(SbFile *) read_stdin(void);
EXTERN(SbFile *) write_stdout(void);
EXTERN(FileStruct *) read_stdin(void);
EXTERN(FileStruct *) write_stdout(void);
#else
EXTERN(FILE *) read_stdin(void);
EXTERN(FILE *) write_stdout(void);
Expand Down
2 changes: 1 addition & 1 deletion third_party/libjpeg_turbo/jmemsys.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ typedef struct backing_store_struct {
#else
/* For a typical implementation with temp files, we need: */
#if defined(STARBOARD)
SbFile *temp_file; /* stdio reference to temp file */
FileStruct *temp_file; /* stdio reference to temp file */
#else
FILE *temp_file; /* stdio reference to temp file */
#endif
Expand Down
8 changes: 6 additions & 2 deletions third_party/libjpeg_turbo/jpeglib.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
#include "jmorecfg.h" /* seldom changed options */


#if defined(STARBOARD)
#include "starboard/common/file_wrapper.h"
#endif

#ifdef __cplusplus
#ifndef DONT_USE_EXTERN_C
extern "C" {
Expand Down Expand Up @@ -920,8 +924,8 @@ EXTERN(void) jpeg_destroy_decompress(j_decompress_ptr cinfo);
/* Standard data source and destination managers: stdio streams. */
/* Caller is responsible for opening the file before and closing after. */
#if defined(STARBOARD)
EXTERN(void) jpeg_stdio_dest(j_compress_ptr cinfo, SbFile *outfile);
EXTERN(void) jpeg_stdio_src(j_decompress_ptr cinfo, SbFile *infile);
EXTERN(void) jpeg_stdio_dest(j_compress_ptr cinfo, FileStruct *outfile);
EXTERN(void) jpeg_stdio_src(j_decompress_ptr cinfo, FileStruct *infile);
#else
EXTERN(void) jpeg_stdio_dest(j_compress_ptr cinfo, FILE *outfile);
EXTERN(void) jpeg_stdio_src(j_decompress_ptr cinfo, FILE *infile);
Expand Down

0 comments on commit e019703

Please sign in to comment.