Skip to content

Commit

Permalink
Cleanup ScopedFile()
Browse files Browse the repository at this point in the history
Change-Id: I015dcededca634e322a5f1cad7eefa8d084d7997
  • Loading branch information
yjzhang111 committed May 12, 2024
1 parent 5d63c7e commit 3a087e9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 40 deletions.
6 changes: 2 additions & 4 deletions starboard/common/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,9 @@ bool SbFileDeleteRecursive(const char* path, bool preserve_root) {

// The |path| points to a file. Remove it and return.
if (err != kSbFileOk) {
return SbFileDelete(path);
return (unlink(path) == 0);
}

SbFileInfo info;

std::vector<char> entry(kSbFileMaxName);

while (SbDirectoryGetNext(dir, entry.data(), kSbFileMaxName)) {
Expand All @@ -110,7 +108,7 @@ bool SbFileDeleteRecursive(const char* path, bool preserve_root) {

// Don't forget to close and remove the directory before returning!
if (DirectoryCloseLogFailure(path, dir)) {
return preserve_root ? true : SbFileDelete(path);
return preserve_root ? true : (rmdir(path) == 0);
}
return false;
}
Expand Down
59 changes: 24 additions & 35 deletions starboard/common/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,71 +47,60 @@ bool SbFileDeleteRecursive(const char* path, bool preserve_root);
// functions call the corresponding SbFile function.
class ScopedFile {
public:
ScopedFile(const char* path,
int flags,
bool* out_created,
SbFileError* out_error)
: file_(kSbFileInvalid) {
file_ = SbFileOpen(path, flags, out_created, out_error);
ScopedFile(const char* path, int flags, int mode) : file_(-1) {
file_ = open(path, flags, mode);
}

ScopedFile(const char* path, int flags, bool* out_created)
: file_(kSbFileInvalid) {
file_ = SbFileOpen(path, flags, out_created, NULL);
ScopedFile(const char* path, int flags) : file_(-1) {
file_ = SbFileOpen(path, flags);
}

ScopedFile(const char* path, int flags) : file_(kSbFileInvalid) {
file_ = SbFileOpen(path, flags, NULL, NULL);
}

~ScopedFile() { SbFileClose(file_); }
~ScopedFile() { close(file_); }

SbFile file() const { return file_; }
int file() const { return file_; }

bool IsValid() const { return SbFileIsValid(file_); }
bool IsValid() const { return (file_ >= 0); }

int64_t Seek(SbFileWhence whence, int64_t offset) const {
return SbFileSeek(file_, whence, offset);
off_t Seek(int whence, off_t offset) const {
return lseek(file_, offset, whence);
}

int Read(char* data, int size) const { return SbFileRead(file_, data, size); }

int ReadAll(char* data, int size) const {
return SbFileReadAll(file_, data, size);
ssize_t Read(void* data, size_t size) const {
return read(file_, data, size);
}

int Write(const char* data, int size) const {
int result = SbFileWrite(file_, data, size);
int ReadAll(char* data, int size) const { return ReadAll(file_, data, size); }

ssize_t Write(const void* data, size_t size) const {
ssize_t result = write(file_, data, size);
RecordFileWriteStat(result);
return result;
}

int WriteAll(const char* data, int size) const {
int result = SbFileWriteAll(file_, data, size);
ssize_t WriteAll(const void* data, size_t size) const {
ssize_t result = WriteAll(file_, data, size);
RecordFileWriteStat(result);
return result;
}

bool Truncate(int64_t length) const { return SbFileTruncate(file_, length); }
int Truncate(off_t length) const { return ftruncate(file_, length); }

bool Flush() const { return SbFileFlush(file_); }
int Flush() const { return fsync(file_); }

bool GetInfo(SbFileInfo* out_info) const {
return SbFileGetInfo(file_, out_info);
}
int GetInfo(struct stat* out_info) const { return fstat(file_, out_info); }

int64_t GetSize() const {
SbFileInfo file_info;
bool success = GetInfo(&file_info);
return (success ? file_info.size : -1);
struct stat file_info;
int success = GetInfo(&file_info);
return (success == 0 ? file_info.st_size : -1);
}

// disallow copy and move operations
ScopedFile(const ScopedFile&) = delete;
ScopedFile& operator=(const ScopedFile&) = delete;

private:
SbFile file_;
int file_;
};

} // namespace starboard
Expand Down
2 changes: 1 addition & 1 deletion starboard/loader_app/drain_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ bool TryDrain(const char* dir, const char* app_key) {
path.append(filename);

SbFileError error = kSbFileOk;
int file = open(path.c_str(), O_CREAT | O_WRONLY);
int file = open(path.c_str(), O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);

SB_DCHECK(file >= 0);
SB_DCHECK(close(file));
Expand Down

0 comments on commit 3a087e9

Please sign in to comment.