7 #include "packager/file/local_file.h" 12 #endif // defined(OS_WIN) 13 #include "packager/base/files/file_util.h" 14 #include "packager/base/logging.h" 19 const char kAdditionalFileMode[] =
"b";
22 :
File(file_name), file_mode_(mode), internal_file_(NULL) {
23 if (file_mode_.find(kAdditionalFileMode) == std::string::npos)
24 file_mode_ += kAdditionalFileMode;
30 result = base::CloseFile(internal_file_);
31 internal_file_ = NULL;
38 DCHECK(buffer != NULL);
39 DCHECK(internal_file_ != NULL);
40 size_t bytes_read = fread(buffer,
sizeof(
char), length, internal_file_);
41 VLOG(2) <<
"Read " << length <<
" return " << bytes_read <<
" error " 42 << ferror(internal_file_);
43 if (bytes_read == 0 && ferror(internal_file_) != 0) {
50 DCHECK(buffer != NULL);
51 DCHECK(internal_file_ != NULL);
52 size_t bytes_written = fwrite(buffer,
sizeof(
char), length, internal_file_);
53 VLOG(2) <<
"Write " << length <<
" return " << bytes_written <<
" error " 54 << ferror(internal_file_);
55 if (bytes_written == 0 && ferror(internal_file_) != 0) {
62 DCHECK(internal_file_ != NULL);
66 LOG(ERROR) <<
"Cannot flush file.";
71 if (!base::GetFileSize(base::FilePath::FromUTF8Unsafe(
file_name()),
73 LOG(ERROR) <<
"Cannot get file size.";
80 DCHECK(internal_file_ != NULL);
81 return ((fflush(internal_file_) == 0) && !ferror(internal_file_));
86 return _fseeki64(internal_file_, static_cast<__int64>(position), SEEK_SET) ==
89 return fseeko(internal_file_, position, SEEK_SET) >= 0;
90 #endif // !defined(OS_WIN) 95 __int64 offset = _ftelli64(internal_file_);
97 off_t offset = ftello(internal_file_);
98 #endif // !defined(OS_WIN) 101 *position =
static_cast<uint64_t
>(offset);
105 LocalFile::~LocalFile() {}
108 base::FilePath file_path(base::FilePath::FromUTF8Unsafe(
file_name()));
111 if (file_mode_.find(
"w") != std::string::npos) {
112 base::File::Error error;
114 if (!base::CreateDirectoryAndGetError(file_path.DirName(), &error)) {
115 LOG(ERROR) <<
"Failed to create directories for file '" 116 << file_path.AsUTF8Unsafe()
117 <<
"'. Error: " << base::File::ErrorToString(error);
122 internal_file_ = base::OpenFile(file_path, file_mode_.c_str());
123 return (internal_file_ != NULL);
127 return base::DeleteFile(base::FilePath::FromUTF8Unsafe(file_name),
false);
bool Seek(uint64_t position) override
Define an abstract file interface.
const std::string & file_name() const
All the methods that are virtual are virtual for mocking.
static bool Delete(const char *file_name)
int64_t Read(void *buffer, uint64_t length) override
bool Open() override
Internal open. Should not be used directly.
int64_t Write(const void *buffer, uint64_t length) override
bool Tell(uint64_t *position) override
LocalFile(const char *file_name, const char *mode)