Skip to content

Commit

Permalink
Clean up SbFile references.
Browse files Browse the repository at this point in the history
Change-Id: I3871fa9b956c5b936507388b7a2cad76a1d67037
  • Loading branch information
yjzhang111 committed Jul 1, 2024
1 parent 5e67cdb commit 822bf38
Show file tree
Hide file tree
Showing 19 changed files with 135 additions and 110 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
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
37 changes: 19 additions & 18 deletions starboard/shared/posix/storage_write_record.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

#include "starboard/common/storage.h"

#include <fcntl.h>

#include <algorithm>
#include <vector>

#include "starboard/common/file.h"
#include "starboard/common/log.h"
#include "starboard/common/string.h"
#include "starboard/configuration_constants.h"
#include "starboard/file.h"
#include "starboard/shared/starboard/file_storage/storage_internal.h"

const char kTempFileSuffix[] = ".temp";
Expand All @@ -45,49 +47,48 @@ bool SbStorageWriteRecord(SbStorageRecord record,
starboard::strlcat(temp_file_path.data(), kTempFileSuffix, kSbFileMaxPath);

SbFileError error;
SbFile temp_file = SbFileOpen(
temp_file_path.data(), kSbFileCreateAlways | kSbFileWrite | kSbFileRead,
NULL, &error);
if (error != kSbFileOk) {
int temp_file = open(temp_file_path.data(), O_CREAT | O_TRUNC | O_RDWR,
S_IRUSR | S_IWUSR);
if (!starboard::IsValid(temp_file)) {
return false;
}

SbFileTruncate(temp_file, 0);
ftruncate(temp_file, 0);

const char* source = data;
int64_t to_write = data_size;
while (to_write > 0) {
int to_write_max =
static_cast<int>(std::min(to_write, static_cast<int64_t>(kSbInt32Max)));
int bytes_written = SbFileWrite(temp_file, source, to_write_max);
int bytes_written = write(temp_file, source, to_write_max);
if (bytes_written < 0) {
SbFileClose(temp_file);
SbFileDelete(temp_file_path.data());
close(temp_file);
unlink(temp_file_path.data());
return false;
}

source += bytes_written;
to_write -= bytes_written;
}

SbFileFlush(temp_file);
fsync(temp_file);

if (SbFileIsValid(record->file) && !SbFileClose(record->file)) {
SbFileClose(temp_file);
SbFileDelete(temp_file_path.data());
if (starboard::IsValid(record->file) && (close(record->file) < 0)) {
close(temp_file);
unlink(temp_file_path.data());
return false;
}

record->file = kSbFileInvalid;
record->file = -1;

if ((!SbFileDelete(original_file_path.data())) ||
if (unlink(original_file_path.data()) ||
(rename(temp_file_path.data(), original_file_path.data()) != 0)) {
SbFileClose(temp_file);
SbFileDelete(temp_file_path.data());
close(temp_file);
unlink(temp_file_path.data());
return false;
}

SbFileFlush(temp_file);
fsync(temp_file);

record->file = temp_file;

Expand Down
21 changes: 11 additions & 10 deletions starboard/shared/starboard/file_atomic_replace_write_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

#include "starboard/shared/starboard/file_atomic_replace_write_file.h"

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

#include <algorithm>

#include "starboard/common/file.h"
Expand All @@ -27,38 +30,36 @@ namespace starboard {
bool SbFileAtomicReplaceWriteFile(const char* path,
const char* data,
int64_t data_size) {
SbFileError error;
SbFile temp_file = SbFileOpen(
path, kSbFileCreateAlways | kSbFileWrite | kSbFileRead, NULL, &error);
int temp_file = open(path, O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR);

if (error != kSbFileOk) {
if (temp_file < 0) {
return false;
}

SbFileTruncate(temp_file, 0);
ftruncate(temp_file, 0);

const char* source = data;
int64_t to_write = data_size;

while (to_write > 0) {
const int to_write_max =
static_cast<int>(std::min(to_write, static_cast<int64_t>(kSbInt32Max)));
const int bytes_written = SbFileWrite(temp_file, source, to_write_max);
const int bytes_written = write(temp_file, source, to_write_max);
RecordFileWriteStat(bytes_written);

if (bytes_written < 0) {
SbFileClose(temp_file);
SbFileDelete(path);
close(temp_file);
unlink(path);
return false;
}

source += bytes_written;
to_write -= bytes_written;
}

SbFileFlush(temp_file);
fsync(temp_file);

if (!SbFileClose(temp_file)) {
if (close(temp_file)) {
return false;
}
return true;
Expand Down
21 changes: 14 additions & 7 deletions starboard/shared/starboard/file_mode_string_to_flags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <fcntl.h>

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

namespace {
bool IsUpdate(const char* mode) {
Expand Down Expand Up @@ -42,21 +44,26 @@ int SbFileModeStringToFlags(const char* mode) {
switch (mode[0]) {
case 'r':
if (IsUpdate(mode + 1)) {
flags |= kSbFileWrite;
flags |= O_RDWR;
} else {
flags |= O_RDONLY;
}
flags |= kSbFileOpenOnly | kSbFileRead;
break;
case 'w':
if (IsUpdate(mode + 1)) {
flags |= kSbFileRead;
flags |= O_RDWR;
} else {
flags |= O_WRONLY;
}
flags |= kSbFileCreateAlways | kSbFileWrite;
flags |= O_CREAT | O_TRUNC;
break;
case 'a':
if (IsUpdate(mode + 1)) {
flags |= kSbFileRead;
flags |= O_RDONLY;
} else {
flags |= O_RDWR;
}
flags |= kSbFileOpenAlways | kSbFileWrite;
flags |= O_CREAT;
break;
default:
SB_NOTREACHED();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@

#include "starboard/common/storage.h"

#include "starboard/file.h"
#include <unistd.h>

#include "starboard/common/file.h"
#include "starboard/shared/starboard/file_storage/storage_internal.h"

bool SbStorageCloseRecord(SbStorageRecord record) {
if (!SbStorageIsValidRecord(record)) {
return false;
}

if (SbFileIsValid(record->file)) {
SbFileClose(record->file);
if (starboard::IsValid(record->file)) {
close(record->file);
}

delete record;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@

#include "starboard/common/storage.h"

#include <unistd.h>

#include <vector>

#include "starboard/configuration_constants.h"
#include "starboard/file.h"
#include "starboard/shared/starboard/file_storage/storage_internal.h"

#if SB_API_VERSION < 16
Expand All @@ -33,5 +34,5 @@ bool SbStorageDeleteRecord(const char* name) {
return false;
}

return SbFileDelete(path.data());
return !unlink(path.data());
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@

#include "starboard/common/storage.h"

#include "starboard/file.h"
#include "starboard/common/file.h"
#include "starboard/shared/starboard/file_storage/storage_internal.h"

int64_t SbStorageGetRecordSize(SbStorageRecord record) {
if (!SbStorageIsValidRecord(record)) {
return -1;
}

if (!SbFileIsValid(record->file)) {
if (!starboard::IsValid(record->file)) {
return -1;
}

SbFileInfo info;
bool success = SbFileGetInfo(record->file, &info);
if (!success) {
struct stat info;
bool success = !fstat(record->file, &info);
if (success) {
return -1;
}

return info.size;
return info.st_size;
}
Loading

0 comments on commit 822bf38

Please sign in to comment.