Commit 306677bb authored by rvargas@chromium.org's avatar rvargas@chromium.org

Convert most of base and net to use base::File

BUG=322664
R=thakis@chromium.org

Review URL: https://codereview.chromium.org/125643002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@243852 0039d316-1c4b-4281-b951-d872f2087c98
parent b9e37058
...@@ -5,8 +5,8 @@ ...@@ -5,8 +5,8 @@
#include <string> #include <string>
#include "base/file_util.h" #include "base/file_util.h"
#include "base/files/file.h"
#include "base/files/scoped_temp_dir.h" #include "base/files/scoped_temp_dir.h"
#include "base/platform_file.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace base { namespace base {
...@@ -98,17 +98,13 @@ TEST(ScopedTempDir, MultipleInvocations) { ...@@ -98,17 +98,13 @@ TEST(ScopedTempDir, MultipleInvocations) {
TEST(ScopedTempDir, LockedTempDir) { TEST(ScopedTempDir, LockedTempDir) {
ScopedTempDir dir; ScopedTempDir dir;
EXPECT_TRUE(dir.CreateUniqueTempDir()); EXPECT_TRUE(dir.CreateUniqueTempDir());
int file_flags = base::PLATFORM_FILE_CREATE_ALWAYS | base::File file(dir.path().Append(FILE_PATH_LITERAL("temp")),
base::PLATFORM_FILE_WRITE; base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
base::PlatformFileError error_code = base::PLATFORM_FILE_OK; EXPECT_TRUE(file.IsValid());
FilePath file_path(dir.path().Append(FILE_PATH_LITERAL("temp"))); EXPECT_EQ(base::File::FILE_OK, file.error_details());
base::PlatformFile file = base::CreatePlatformFile(file_path, file_flags,
NULL, &error_code);
EXPECT_NE(base::kInvalidPlatformFileValue, file);
EXPECT_EQ(base::PLATFORM_FILE_OK, error_code);
EXPECT_FALSE(dir.Delete()); // We should not be able to delete. EXPECT_FALSE(dir.Delete()); // We should not be able to delete.
EXPECT_FALSE(dir.path().empty()); // We should still have a valid path. EXPECT_FALSE(dir.path().empty()); // We should still have a valid path.
EXPECT_TRUE(base::ClosePlatformFile(file)); file.Close();
// Now, we should be able to delete. // Now, we should be able to delete.
EXPECT_TRUE(dir.Delete()); EXPECT_TRUE(dir.Delete());
} }
......
...@@ -6,8 +6,6 @@ ...@@ -6,8 +6,6 @@
#define NET_ANDROID_CERT_VERIFY_RESULT_ANDROID_H_ #define NET_ANDROID_CERT_VERIFY_RESULT_ANDROID_H_
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/platform_file.h"
#include "net/base/net_export.h"
namespace net { namespace net {
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
#include "base/files/scoped_temp_dir.h" #include "base/files/scoped_temp_dir.h"
#include "base/i18n/file_util_icu.h" #include "base/i18n/file_util_icu.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/platform_file.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "net/base/directory_lister.h" #include "net/base/directory_lister.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
#include <fcntl.h> #include <fcntl.h>
#include "base/logging.h" #include "base/logging.h"
#include "base/platform_file.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
#include "net/disk_cache/flash/format.h" #include "net/disk_cache/flash/format.h"
...@@ -22,41 +21,38 @@ Storage::Storage(const base::FilePath& path, ...@@ -22,41 +21,38 @@ Storage::Storage(const base::FilePath& path,
} }
bool Storage::Init() { bool Storage::Init() {
int flags = base::PLATFORM_FILE_READ | int flags = base::File::FLAG_READ |
base::PLATFORM_FILE_WRITE | base::File::FLAG_WRITE |
base::PLATFORM_FILE_OPEN_ALWAYS; base::File::FLAG_OPEN_ALWAYS;
file_ = base::CreatePlatformFile(path_, flags, NULL, NULL); file_.Initialize(path_, flags);
if (file_ == base::kInvalidPlatformFileValue) if (!file_.IsValid())
return false; return false;
// TODO(agayev): if file already exists, do some validation and return either // TODO(agayev): if file already exists, do some validation and return either
// true or false based on the result. // true or false based on the result.
#if defined(OS_LINUX) #if defined(OS_LINUX)
fallocate(file_, 0, 0, size_); fallocate(file_.GetPlatformFile(), 0, 0, size_);
#endif #endif
return true; return true;
} }
Storage::~Storage() { Storage::~Storage() {
base::ClosePlatformFile(file_);
} }
bool Storage::Read(void* buffer, int32 size, int32 offset) { bool Storage::Read(void* buffer, int32 size, int32 offset) {
DCHECK(offset >= 0 && offset + size <= size_); DCHECK(offset >= 0 && offset + size <= size_);
int rv = base::ReadPlatformFile(file_, offset, static_cast<char*>(buffer), int rv = file_.Read(offset, static_cast<char*>(buffer), size);
size);
return rv == size; return rv == size;
} }
bool Storage::Write(const void* buffer, int32 size, int32 offset) { bool Storage::Write(const void* buffer, int32 size, int32 offset) {
DCHECK(offset >= 0 && offset + size <= size_); DCHECK(offset >= 0 && offset + size <= size_);
int rv = base::WritePlatformFile(file_, offset, int rv = file_.Write(offset, static_cast<const char*>(buffer), size);
static_cast<const char*>(buffer), size);
return rv == size; return rv == size;
} }
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
#define NET_DISK_CACHE_FLASH_STORAGE_H_ #define NET_DISK_CACHE_FLASH_STORAGE_H_
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/platform_file.h" #include "base/files/file.h"
#include "net/base/net_export.h" #include "net/base/net_export.h"
namespace disk_cache { namespace disk_cache {
...@@ -25,7 +25,7 @@ class NET_EXPORT_PRIVATE Storage { ...@@ -25,7 +25,7 @@ class NET_EXPORT_PRIVATE Storage {
private: private:
base::FilePath path_; base::FilePath path_;
int32 size_; int32 size_;
base::PlatformFile file_; base::File file_;
DISALLOW_COPY_AND_ASSIGN(Storage); DISALLOW_COPY_AND_ASSIGN(Storage);
}; };
......
...@@ -195,7 +195,6 @@ void SimpleIndexFile::SyncWriteToDisk(net::CacheType cache_type, ...@@ -195,7 +195,6 @@ void SimpleIndexFile::SyncWriteToDisk(net::CacheType cache_type,
// part of a Create operation does not fit into the time budget for the index // part of a Create operation does not fit into the time budget for the index
// flush delay. This simple approach will be reconsidered if it does not allow // flush delay. This simple approach will be reconsidered if it does not allow
// for maintaining freshness. // for maintaining freshness.
base::PlatformFileInfo cache_dir_info;
base::Time cache_dir_mtime; base::Time cache_dir_mtime;
if (!simple_util::GetMTime(cache_directory, &cache_dir_mtime)) { if (!simple_util::GetMTime(cache_directory, &cache_dir_mtime)) {
LOG(ERROR) << "Could obtain information about cache age"; LOG(ERROR) << "Could obtain information about cache age";
......
...@@ -23,24 +23,9 @@ ...@@ -23,24 +23,9 @@
#include "net/disk_cache/simple/simple_util.h" #include "net/disk_cache/simple/simple_util.h"
#include "third_party/zlib/zlib.h" #include "third_party/zlib/zlib.h"
using base::kInvalidPlatformFileValue; using base::File;
using base::ClosePlatformFile;
using base::FilePath; using base::FilePath;
using base::GetPlatformFileInfo;
using base::PlatformFileError;
using base::PlatformFileInfo;
using base::PLATFORM_FILE_CREATE;
using base::PLATFORM_FILE_ERROR_EXISTS;
using base::PLATFORM_FILE_ERROR_NOT_FOUND;
using base::PLATFORM_FILE_OK;
using base::PLATFORM_FILE_OPEN;
using base::PLATFORM_FILE_OPEN_ALWAYS;
using base::PLATFORM_FILE_READ;
using base::PLATFORM_FILE_WRITE;
using base::ReadPlatformFile;
using base::Time; using base::Time;
using base::TruncatePlatformFile;
using base::WritePlatformFile;
namespace { namespace {
...@@ -301,8 +286,9 @@ void SimpleSynchronousEntry::ReadData(const EntryOperationData& in_entry_op, ...@@ -301,8 +286,9 @@ void SimpleSynchronousEntry::ReadData(const EntryOperationData& in_entry_op,
// be handled in the SimpleEntryImpl. // be handled in the SimpleEntryImpl.
DCHECK_LT(0, in_entry_op.buf_len); DCHECK_LT(0, in_entry_op.buf_len);
DCHECK(!empty_file_omitted_[file_index]); DCHECK(!empty_file_omitted_[file_index]);
int bytes_read = ReadPlatformFile( File* file = const_cast<File*>(&files_[file_index]);
files_[file_index], file_offset, out_buf->data(), in_entry_op.buf_len); int bytes_read =
file->Read(file_offset, out_buf->data(), in_entry_op.buf_len);
if (bytes_read > 0) { if (bytes_read > 0) {
entry_stat->set_last_used(Time::Now()); entry_stat->set_last_used(Time::Now());
*out_crc32 = crc32(crc32(0L, Z_NULL, 0), *out_crc32 = crc32(crc32(0L, Z_NULL, 0),
...@@ -343,7 +329,7 @@ void SimpleSynchronousEntry::WriteData(const EntryOperationData& in_entry_op, ...@@ -343,7 +329,7 @@ void SimpleSynchronousEntry::WriteData(const EntryOperationData& in_entry_op,
*out_result = net::ERR_CACHE_WRITE_FAILURE; *out_result = net::ERR_CACHE_WRITE_FAILURE;
return; return;
} }
PlatformFileError error; File::Error error;
if (!MaybeCreateFile(file_index, FILE_REQUIRED, &error)) { if (!MaybeCreateFile(file_index, FILE_REQUIRED, &error)) {
RecordWriteResult(cache_type_, WRITE_RESULT_LAZY_CREATE_FAILURE); RecordWriteResult(cache_type_, WRITE_RESULT_LAZY_CREATE_FAILURE);
Doom(); Doom();
...@@ -364,7 +350,7 @@ void SimpleSynchronousEntry::WriteData(const EntryOperationData& in_entry_op, ...@@ -364,7 +350,7 @@ void SimpleSynchronousEntry::WriteData(const EntryOperationData& in_entry_op,
// The EOF record and the eventual stream afterward need to be zeroed out. // The EOF record and the eventual stream afterward need to be zeroed out.
const int64 file_eof_offset = const int64 file_eof_offset =
out_entry_stat->GetEOFOffsetInFile(key_, index); out_entry_stat->GetEOFOffsetInFile(key_, index);
if (!TruncatePlatformFile(files_[file_index], file_eof_offset)) { if (!files_[file_index].SetLength(file_eof_offset)) {
RecordWriteResult(cache_type_, WRITE_RESULT_PRETRUNCATE_FAILURE); RecordWriteResult(cache_type_, WRITE_RESULT_PRETRUNCATE_FAILURE);
Doom(); Doom();
*out_result = net::ERR_CACHE_WRITE_FAILURE; *out_result = net::ERR_CACHE_WRITE_FAILURE;
...@@ -372,8 +358,7 @@ void SimpleSynchronousEntry::WriteData(const EntryOperationData& in_entry_op, ...@@ -372,8 +358,7 @@ void SimpleSynchronousEntry::WriteData(const EntryOperationData& in_entry_op,
} }
} }
if (buf_len > 0) { if (buf_len > 0) {
if (WritePlatformFile( if (files_[file_index].Write(file_offset, in_buf->data(), buf_len) !=
files_[file_index], file_offset, in_buf->data(), buf_len) !=
buf_len) { buf_len) {
RecordWriteResult(cache_type_, WRITE_RESULT_WRITE_FAILURE); RecordWriteResult(cache_type_, WRITE_RESULT_WRITE_FAILURE);
Doom(); Doom();
...@@ -387,7 +372,7 @@ void SimpleSynchronousEntry::WriteData(const EntryOperationData& in_entry_op, ...@@ -387,7 +372,7 @@ void SimpleSynchronousEntry::WriteData(const EntryOperationData& in_entry_op,
} else { } else {
out_entry_stat->set_data_size(index, offset + buf_len); out_entry_stat->set_data_size(index, offset + buf_len);
int file_eof_offset = out_entry_stat->GetLastEOFOffsetInFile(key_, index); int file_eof_offset = out_entry_stat->GetLastEOFOffsetInFile(key_, index);
if (!TruncatePlatformFile(files_[file_index], file_eof_offset)) { if (!files_[file_index].SetLength(file_eof_offset)) {
RecordWriteResult(cache_type_, WRITE_RESULT_TRUNCATE_FAILURE); RecordWriteResult(cache_type_, WRITE_RESULT_TRUNCATE_FAILURE);
Doom(); Doom();
*out_result = net::ERR_CACHE_WRITE_FAILURE; *out_result = net::ERR_CACHE_WRITE_FAILURE;
...@@ -488,8 +473,8 @@ void SimpleSynchronousEntry::WriteSparseData( ...@@ -488,8 +473,8 @@ void SimpleSynchronousEntry::WriteSparseData(
// This is a pessimistic estimate; it assumes the entire buffer is going to // This is a pessimistic estimate; it assumes the entire buffer is going to
// be appended as a new range, not written over existing ranges. // be appended as a new range, not written over existing ranges.
if (sparse_data_size + buf_len > max_sparse_data_size) { if (sparse_data_size + buf_len > max_sparse_data_size) {
DLOG(INFO) << "Truncating sparse data file (" << sparse_data_size << " + " DVLOG(1) << "Truncating sparse data file (" << sparse_data_size << " + "
<< buf_len << " > " << max_sparse_data_size << ")"; << buf_len << " > " << max_sparse_data_size << ")";
TruncateSparseFile(); TruncateSparseFile();
} }
...@@ -624,7 +609,7 @@ void SimpleSynchronousEntry::CheckEOFRecord(int index, ...@@ -624,7 +609,7 @@ void SimpleSynchronousEntry::CheckEOFRecord(int index,
return; return;
} }
if (has_crc32 && crc32 != expected_crc32) { if (has_crc32 && crc32 != expected_crc32) {
DLOG(INFO) << "EOF record had bad crc."; DVLOG(1) << "EOF record had bad crc.";
*out_result = net::ERR_CACHE_CHECKSUM_MISMATCH; *out_result = net::ERR_CACHE_CHECKSUM_MISMATCH;
RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH);
Doom(); Doom();
...@@ -640,12 +625,11 @@ void SimpleSynchronousEntry::Close( ...@@ -640,12 +625,11 @@ void SimpleSynchronousEntry::Close(
DCHECK(stream_0_data); DCHECK(stream_0_data);
// Write stream 0 data. // Write stream 0 data.
int stream_0_offset = entry_stat.GetOffsetInFile(key_, 0, 0); int stream_0_offset = entry_stat.GetOffsetInFile(key_, 0, 0);
if (WritePlatformFile(files_[0], if (files_[0].Write(stream_0_offset, stream_0_data->data(),
stream_0_offset, entry_stat.data_size(0)) !=
stream_0_data->data(), entry_stat.data_size(0)) {
entry_stat.data_size(0)) != entry_stat.data_size(0)) {
RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE); RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE);
DLOG(INFO) << "Could not write stream 0 data."; DVLOG(1) << "Could not write stream 0 data.";
Doom(); Doom();
} }
...@@ -668,18 +652,18 @@ void SimpleSynchronousEntry::Close( ...@@ -668,18 +652,18 @@ void SimpleSynchronousEntry::Close(
// next open will yield wrong stream sizes. On stream 1 and stream 2 proper // next open will yield wrong stream sizes. On stream 1 and stream 2 proper
// resizing of the file is handled in SimpleSynchronousEntry::WriteData(). // resizing of the file is handled in SimpleSynchronousEntry::WriteData().
if (stream_index == 0 && if (stream_index == 0 &&
!TruncatePlatformFile(files_[file_index], eof_offset)) { !files_[file_index].SetLength(eof_offset)) {
RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE); RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE);
DLOG(INFO) << "Could not truncate stream 0 file."; DVLOG(1) << "Could not truncate stream 0 file.";
Doom(); Doom();
break; break;
} }
if (WritePlatformFile(files_[file_index], if (files_[file_index].Write(eof_offset,
eof_offset, reinterpret_cast<const char*>(&eof_record),
reinterpret_cast<const char*>(&eof_record), sizeof(eof_record)) !=
sizeof(eof_record)) != sizeof(eof_record)) { sizeof(eof_record)) {
RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE); RecordCloseResult(cache_type_, CLOSE_RESULT_WRITE_FAILURE);
DLOG(INFO) << "Could not write eof record."; DVLOG(1) << "Could not write eof record.";
Doom(); Doom();
break; break;
} }
...@@ -688,8 +672,7 @@ void SimpleSynchronousEntry::Close( ...@@ -688,8 +672,7 @@ void SimpleSynchronousEntry::Close(
if (empty_file_omitted_[i]) if (empty_file_omitted_[i])
continue; continue;
bool did_close_file = ClosePlatformFile(files_[i]); files_[i].Close();
DCHECK(did_close_file);
const int64 file_size = entry_stat.GetFileSize(key_, i); const int64 file_size = entry_stat.GetFileSize(key_, i);
SIMPLE_CACHE_UMA(CUSTOM_COUNTS, SIMPLE_CACHE_UMA(CUSTOM_COUNTS,
"LastClusterSize", cache_type_, "LastClusterSize", cache_type_,
...@@ -700,10 +683,8 @@ void SimpleSynchronousEntry::Close( ...@@ -700,10 +683,8 @@ void SimpleSynchronousEntry::Close(
cluster_loss * 100 / (cluster_loss + file_size)); cluster_loss * 100 / (cluster_loss + file_size));
} }
if (sparse_file_open()) { if (sparse_file_open())
bool did_close_file = ClosePlatformFile(sparse_file_); sparse_file_.Close();
CHECK(did_close_file);
}
if (files_created_) { if (files_created_) {
const int stream2_file_index = GetFileIndexFromStreamIndex(2); const int stream2_file_index = GetFileIndexFromStreamIndex(2);
...@@ -724,12 +705,9 @@ SimpleSynchronousEntry::SimpleSynchronousEntry(net::CacheType cache_type, ...@@ -724,12 +705,9 @@ SimpleSynchronousEntry::SimpleSynchronousEntry(net::CacheType cache_type,
entry_hash_(entry_hash), entry_hash_(entry_hash),
key_(key), key_(key),
have_open_files_(false), have_open_files_(false),
initialized_(false), initialized_(false) {
sparse_file_(kInvalidPlatformFileValue) { for (int i = 0; i < kSimpleEntryFileCount; ++i)
for (int i = 0; i < kSimpleEntryFileCount; ++i) {
files_[i] = kInvalidPlatformFileValue;
empty_file_omitted_[i] = false; empty_file_omitted_[i] = false;
}
} }
SimpleSynchronousEntry::~SimpleSynchronousEntry() { SimpleSynchronousEntry::~SimpleSynchronousEntry() {
...@@ -740,26 +718,27 @@ SimpleSynchronousEntry::~SimpleSynchronousEntry() { ...@@ -740,26 +718,27 @@ SimpleSynchronousEntry::~SimpleSynchronousEntry() {
bool SimpleSynchronousEntry::MaybeOpenFile( bool SimpleSynchronousEntry::MaybeOpenFile(
int file_index, int file_index,
PlatformFileError* out_error) { File::Error* out_error) {
DCHECK(out_error); DCHECK(out_error);
FilePath filename = GetFilenameFromFileIndex(file_index); FilePath filename = GetFilenameFromFileIndex(file_index);
int flags = PLATFORM_FILE_OPEN | PLATFORM_FILE_READ | PLATFORM_FILE_WRITE; int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE;
files_[file_index] = CreatePlatformFile(filename, flags, NULL, out_error); files_[file_index].Initialize(filename, flags);
*out_error = files_[file_index].error_details();
if (CanOmitEmptyFile(file_index) && if (CanOmitEmptyFile(file_index) && !files_[file_index].IsValid() &&
*out_error == PLATFORM_FILE_ERROR_NOT_FOUND) { *out_error == File::FILE_ERROR_NOT_FOUND) {
empty_file_omitted_[file_index] = true; empty_file_omitted_[file_index] = true;
return true; return true;
} }
return *out_error == PLATFORM_FILE_OK; return files_[file_index].IsValid();
} }
bool SimpleSynchronousEntry::MaybeCreateFile( bool SimpleSynchronousEntry::MaybeCreateFile(
int file_index, int file_index,
FileRequired file_required, FileRequired file_required,
PlatformFileError* out_error) { File::Error* out_error) {
DCHECK(out_error); DCHECK(out_error);
if (CanOmitEmptyFile(file_index) && file_required == FILE_NOT_REQUIRED) { if (CanOmitEmptyFile(file_index) && file_required == FILE_NOT_REQUIRED) {
...@@ -768,19 +747,20 @@ bool SimpleSynchronousEntry::MaybeCreateFile( ...@@ -768,19 +747,20 @@ bool SimpleSynchronousEntry::MaybeCreateFile(
} }
FilePath filename = GetFilenameFromFileIndex(file_index); FilePath filename = GetFilenameFromFileIndex(file_index);
int flags = PLATFORM_FILE_CREATE | PLATFORM_FILE_READ | PLATFORM_FILE_WRITE; int flags = File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE;
files_[file_index] = CreatePlatformFile(filename, flags, NULL, out_error); files_[file_index].Initialize(filename, flags);
*out_error = files_[file_index].error_details();
empty_file_omitted_[file_index] = false; empty_file_omitted_[file_index] = false;
return *out_error == PLATFORM_FILE_OK; return files_[file_index].IsValid();
} }
bool SimpleSynchronousEntry::OpenFiles( bool SimpleSynchronousEntry::OpenFiles(
bool had_index, bool had_index,
SimpleEntryStat* out_entry_stat) { SimpleEntryStat* out_entry_stat) {
for (int i = 0; i < kSimpleEntryFileCount; ++i) { for (int i = 0; i < kSimpleEntryFileCount; ++i) {
PlatformFileError error; File::Error error;
if (!MaybeOpenFile(i, &error)) { if (!MaybeOpenFile(i, &error)) {
// TODO(ttuttle,gavinp): Remove one each of these triplets of histograms. // TODO(ttuttle,gavinp): Remove one each of these triplets of histograms.
// We can calculate the third as the sum or difference of the other two. // We can calculate the third as the sum or difference of the other two.
...@@ -814,8 +794,8 @@ bool SimpleSynchronousEntry::OpenFiles( ...@@ -814,8 +794,8 @@ bool SimpleSynchronousEntry::OpenFiles(
continue; continue;
} }
PlatformFileInfo file_info; File::Info file_info;
bool success = GetPlatformFileInfo(files_[i], &file_info); bool success = files_[i].GetInfo(&file_info);
base::Time file_last_modified; base::Time file_last_modified;
if (!success) { if (!success) {
DLOG(WARNING) << "Could not get platform file info."; DLOG(WARNING) << "Could not get platform file info.";
...@@ -860,7 +840,7 @@ bool SimpleSynchronousEntry::CreateFiles( ...@@ -860,7 +840,7 @@ bool SimpleSynchronousEntry::CreateFiles(
bool had_index, bool had_index,
SimpleEntryStat* out_entry_stat) { SimpleEntryStat* out_entry_stat) {
for (int i = 0; i < kSimpleEntryFileCount; ++i) { for (int i = 0; i < kSimpleEntryFileCount; ++i) {
PlatformFileError error; File::Error error;
if (!MaybeCreateFile(i, FILE_NOT_REQUIRED, &error)) { if (!MaybeCreateFile(i, FILE_NOT_REQUIRED, &error)) {
// TODO(ttuttle,gavinp): Remove one each of these triplets of histograms. // TODO(ttuttle,gavinp): Remove one each of these triplets of histograms.
// We can calculate the third as the sum or difference of the other two. // We can calculate the third as the sum or difference of the other two.
...@@ -901,16 +881,12 @@ void SimpleSynchronousEntry::CloseFile(int index) { ...@@ -901,16 +881,12 @@ void SimpleSynchronousEntry::CloseFile(int index) {
if (empty_file_omitted_[index]) { if (empty_file_omitted_[index]) {
empty_file_omitted_[index] = false; empty_file_omitted_[index] = false;
} else { } else {
DCHECK_NE(kInvalidPlatformFileValue, files_[index]); DCHECK(files_[index].IsValid());
bool did_close = ClosePlatformFile(files_[index]); files_[index].Close();
DCHECK(did_close);
files_[index] = kInvalidPlatformFileValue;
} }
if (sparse_file_open()) { if (sparse_file_open())
bool did_close = CloseSparseFile(); CloseSparseFile();
DCHECK(did_close);
}
} }
void SimpleSynchronousEntry::CloseFiles() { void SimpleSynchronousEntry::CloseFiles() {
...@@ -934,8 +910,7 @@ int SimpleSynchronousEntry::InitializeForOpen( ...@@ -934,8 +910,7 @@ int SimpleSynchronousEntry::InitializeForOpen(
SimpleFileHeader header; SimpleFileHeader header;
int header_read_result = int header_read_result =
ReadPlatformFile(files_[i], 0, reinterpret_cast<char*>(&header), files_[i].Read(0, reinterpret_cast<char*>(&header), sizeof(header));
sizeof(header));
if (header_read_result != sizeof(header)) { if (header_read_result != sizeof(header)) {
DLOG(WARNING) << "Cannot read header from entry."; DLOG(WARNING) << "Cannot read header from entry.";
RecordSyncOpenResult(cache_type_, OPEN_ENTRY_CANT_READ_HEADER, had_index); RecordSyncOpenResult(cache_type_, OPEN_ENTRY_CANT_READ_HEADER, had_index);
...@@ -958,8 +933,8 @@ int SimpleSynchronousEntry::InitializeForOpen( ...@@ -958,8 +933,8 @@ int SimpleSynchronousEntry::InitializeForOpen(
} }
scoped_ptr<char[]> key(new char[header.key_length]); scoped_ptr<char[]> key(new char[header.key_length]);
int key_read_result = ReadPlatformFile(files_[i], sizeof(header), int key_read_result = files_[i].Read(sizeof(header), key.get(),
key.get(), header.key_length); header.key_length);
if (key_read_result != implicit_cast<int>(header.key_length)) { if (key_read_result != implicit_cast<int>(header.key_length)) {
DLOG(WARNING) << "Cannot read key from entry."; DLOG(WARNING) << "Cannot read key from entry.";
RecordSyncOpenResult(cache_type_, OPEN_ENTRY_CANT_READ_KEY, had_index); RecordSyncOpenResult(cache_type_, OPEN_ENTRY_CANT_READ_KEY, had_index);
...@@ -1005,7 +980,7 @@ int SimpleSynchronousEntry::InitializeForOpen( ...@@ -1005,7 +980,7 @@ int SimpleSynchronousEntry::InitializeForOpen(
DCHECK(CanOmitEmptyFile(stream2_file_index)); DCHECK(CanOmitEmptyFile(stream2_file_index));
if (!empty_file_omitted_[stream2_file_index] && if (!empty_file_omitted_[stream2_file_index] &&
out_entry_stat->data_size(2) == 0) { out_entry_stat->data_size(2) == 0) {
DLOG(INFO) << "Removing empty stream 2 file."; DVLOG(1) << "Removing empty stream 2 file.";
CloseFile(stream2_file_index); CloseFile(stream2_file_index);
DeleteFileForEntryHash(path_, entry_hash_, stream2_file_index); DeleteFileForEntryHash(path_, entry_hash_, stream2_file_index);
empty_file_omitted_[stream2_file_index] = true; empty_file_omitted_[stream2_file_index] = true;
...@@ -1030,15 +1005,15 @@ bool SimpleSynchronousEntry::InitializeCreatedFile( ...@@ -1030,15 +1005,15 @@ bool SimpleSynchronousEntry::InitializeCreatedFile(
header.key_length = key_.size(); header.key_length = key_.size();
header.key_hash = base::Hash(key_); header.key_hash = base::Hash(key_);
int bytes_written = WritePlatformFile( int bytes_written = files_[file_index].Write(
files_[file_index], 0, reinterpret_cast<char*>(&header), sizeof(header)); 0, reinterpret_cast<char*>(&header), sizeof(header));
if (bytes_written != sizeof(header)) { if (bytes_written != sizeof(header)) {
*out_result = CREATE_ENTRY_CANT_WRITE_HEADER; *out_result = CREATE_ENTRY_CANT_WRITE_HEADER;
return false; return false;
} }
bytes_written = WritePlatformFile( bytes_written = files_[file_index].Write(sizeof(header), key_.data(),
files_[file_index], sizeof(header), key_.data(), key_.size()); key_.size());
if (bytes_written != implicit_cast<int>(key_.size())) { if (bytes_written != implicit_cast<int>(key_.size())) {
*out_result = CREATE_ENTRY_CANT_WRITE_KEY; *out_result = CREATE_ENTRY_CANT_WRITE_KEY;
return false; return false;
...@@ -1100,8 +1075,9 @@ int SimpleSynchronousEntry::ReadAndValidateStream0( ...@@ -1100,8 +1075,9 @@ int SimpleSynchronousEntry::ReadAndValidateStream0(
*stream_0_data = new net::GrowableIOBuffer(); *stream_0_data = new net::GrowableIOBuffer();
(*stream_0_data)->SetCapacity(stream_0_size); (*stream_0_data)->SetCapacity(stream_0_size);
int file_offset = out_entry_stat->GetOffsetInFile(key_, 0, 0); int file_offset = out_entry_stat->GetOffsetInFile(key_, 0, 0);
int bytes_read = ReadPlatformFile( File* file = const_cast<File*>(&files_[0]);
files_[0], file_offset, (*stream_0_data)->data(), stream_0_size); int bytes_read =
file->Read(file_offset, (*stream_0_data)->data(), stream_0_size);
if (bytes_read != stream_0_size) if (bytes_read != stream_0_size)
return net::ERR_FAILED; return net::ERR_FAILED;
...@@ -1113,7 +1089,7 @@ int SimpleSynchronousEntry::ReadAndValidateStream0( ...@@ -1113,7 +1089,7 @@ int SimpleSynchronousEntry::ReadAndValidateStream0(
reinterpret_cast<const Bytef*>((*stream_0_data)->data()), reinterpret_cast<const Bytef*>((*stream_0_data)->data()),
stream_0_size); stream_0_size);
if (has_crc32 && read_crc32 != expected_crc32) { if (has_crc32 && read_crc32 != expected_crc32) {
DLOG(INFO) << "EOF record had bad crc."; DVLOG(1) << "EOF record had bad crc.";
RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH); RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_CRC_MISMATCH);
return net::ERR_FAILED; return net::ERR_FAILED;
} }
...@@ -1130,17 +1106,17 @@ int SimpleSynchronousEntry::GetEOFRecordData(int index, ...@@ -1130,17 +1106,17 @@ int SimpleSynchronousEntry::GetEOFRecordData(int index,
SimpleFileEOF eof_record; SimpleFileEOF eof_record;
int file_offset = entry_stat.GetEOFOffsetInFile(key_, index); int file_offset = entry_stat.GetEOFOffsetInFile(key_, index);
int file_index = GetFileIndexFromStreamIndex(index); int file_index = GetFileIndexFromStreamIndex(index);
if (ReadPlatformFile(files_[file_index], File* file = const_cast<File*>(&files_[file_index]);
file_offset, if (file->Read(file_offset, reinterpret_cast<char*>(&eof_record),
reinterpret_cast<char*>(&eof_record), sizeof(eof_record)) !=
sizeof(eof_record)) != sizeof(eof_record)) { sizeof(eof_record)) {
RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_READ_FAILURE); RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_READ_FAILURE);
return net::ERR_CACHE_CHECKSUM_READ_FAILURE; return net::ERR_CACHE_CHECKSUM_READ_FAILURE;
} }
if (eof_record.final_magic_number != kSimpleFinalMagicNumber) { if (eof_record.final_magic_number != kSimpleFinalMagicNumber) {
RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_MAGIC_NUMBER_MISMATCH); RecordCheckEOFResult(cache_type_, CHECK_EOF_RESULT_MAGIC_NUMBER_MISMATCH);
DLOG(INFO) << "EOF record had bad magic number."; DVLOG(1) << "EOF record had bad magic number.";
return net::ERR_CACHE_CHECKSUM_READ_FAILURE; return net::ERR_CACHE_CHECKSUM_READ_FAILURE;
} }
...@@ -1208,14 +1184,12 @@ bool SimpleSynchronousEntry::OpenSparseFileIfExists( ...@@ -1208,14 +1184,12 @@ bool SimpleSynchronousEntry::OpenSparseFileIfExists(
FilePath filename = path_.AppendASCII( FilePath filename = path_.AppendASCII(
GetSparseFilenameFromEntryHash(entry_hash_)); GetSparseFilenameFromEntryHash(entry_hash_));
int flags = PLATFORM_FILE_OPEN | PLATFORM_FILE_READ | PLATFORM_FILE_WRITE; int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE;
bool created; sparse_file_.Initialize(filename, flags);
PlatformFileError error; if (sparse_file_.IsValid())
sparse_file_ = CreatePlatformFile(filename, flags, &created, &error); return ScanSparseFile(out_sparse_data_size);
if (error == PLATFORM_FILE_ERROR_NOT_FOUND)
return true;
return ScanSparseFile(out_sparse_data_size); return sparse_file_.error_details() == File::FILE_ERROR_NOT_FOUND;
} }
bool SimpleSynchronousEntry::CreateSparseFile() { bool SimpleSynchronousEntry::CreateSparseFile() {
...@@ -1223,30 +1197,24 @@ bool SimpleSynchronousEntry::CreateSparseFile() { ...@@ -1223,30 +1197,24 @@ bool SimpleSynchronousEntry::CreateSparseFile() {
FilePath filename = path_.AppendASCII( FilePath filename = path_.AppendASCII(
GetSparseFilenameFromEntryHash(entry_hash_)); GetSparseFilenameFromEntryHash(entry_hash_));
int flags = PLATFORM_FILE_CREATE | PLATFORM_FILE_READ | PLATFORM_FILE_WRITE; int flags = File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE;
bool created; sparse_file_.Initialize(filename, flags);
PlatformFileError error; if (!sparse_file_.IsValid())
sparse_file_ = CreatePlatformFile(filename, flags, &created, &error);
if (error != PLATFORM_FILE_OK)
return false; return false;
return InitializeSparseFile(); return InitializeSparseFile();
} }
bool SimpleSynchronousEntry::CloseSparseFile() { void SimpleSynchronousEntry::CloseSparseFile() {
DCHECK(sparse_file_open()); DCHECK(sparse_file_open());
sparse_file_.Close();
bool did_close = ClosePlatformFile(sparse_file_);
if (did_close)
sparse_file_ = kInvalidPlatformFileValue;
return did_close;
} }
bool SimpleSynchronousEntry::TruncateSparseFile() { bool SimpleSynchronousEntry::TruncateSparseFile() {
DCHECK(sparse_file_open()); DCHECK(sparse_file_open());
int64 header_and_key_length = sizeof(SimpleFileHeader) + key_.size(); int64 header_and_key_length = sizeof(SimpleFileHeader) + key_.size();
if (!TruncatePlatformFile(sparse_file_, header_and_key_length)) { if (!sparse_file_.SetLength(header_and_key_length)) {
DLOG(WARNING) << "Could not truncate sparse file"; DLOG(WARNING) << "Could not truncate sparse file";
return false; return false;
} }
...@@ -1266,15 +1234,14 @@ bool SimpleSynchronousEntry::InitializeSparseFile() { ...@@ -1266,15 +1234,14 @@ bool SimpleSynchronousEntry::InitializeSparseFile() {
header.key_hash = base::Hash(key_); header.key_hash = base::Hash(key_);
int header_write_result = int header_write_result =
WritePlatformFile(sparse_file_, 0, reinterpret_cast<char*>(&header), sparse_file_.Write(0, reinterpret_cast<char*>(&header), sizeof(header));
sizeof(header));
if (header_write_result != sizeof(header)) { if (header_write_result != sizeof(header)) {
DLOG(WARNING) << "Could not write sparse file header"; DLOG(WARNING) << "Could not write sparse file header";
return false; return false;
} }
int key_write_result = WritePlatformFile(sparse_file_, sizeof(header), int key_write_result = sparse_file_.Write(sizeof(header), key_.data(),
key_.data(), key_.size()); key_.size());
if (key_write_result != implicit_cast<int>(key_.size())) { if (key_write_result != implicit_cast<int>(key_.size())) {
DLOG(WARNING) << "Could not write sparse file key"; DLOG(WARNING) << "Could not write sparse file key";
return false; return false;
...@@ -1293,8 +1260,7 @@ bool SimpleSynchronousEntry::ScanSparseFile(int32* out_sparse_data_size) { ...@@ -1293,8 +1260,7 @@ bool SimpleSynchronousEntry::ScanSparseFile(int32* out_sparse_data_size) {
SimpleFileHeader header; SimpleFileHeader header;
int header_read_result = int header_read_result =
ReadPlatformFile(sparse_file_, 0, reinterpret_cast<char*>(&header), sparse_file_.Read(0, reinterpret_cast<char*>(&header), sizeof(header));
sizeof(header));
if (header_read_result != sizeof(header)) { if (header_read_result != sizeof(header)) {
DLOG(WARNING) << "Could not read header from sparse file."; DLOG(WARNING) << "Could not read header from sparse file.";
return false; return false;
...@@ -1316,10 +1282,9 @@ bool SimpleSynchronousEntry::ScanSparseFile(int32* out_sparse_data_size) { ...@@ -1316,10 +1282,9 @@ bool SimpleSynchronousEntry::ScanSparseFile(int32* out_sparse_data_size) {
while (1) { while (1) {
SimpleFileSparseRangeHeader range_header; SimpleFileSparseRangeHeader range_header;
int range_header_read_result = int range_header_read_result =
ReadPlatformFile(sparse_file_, sparse_file_.Read(range_header_offset,
range_header_offset, reinterpret_cast<char*>(&range_header),
reinterpret_cast<char*>(&range_header), sizeof(range_header));
sizeof(range_header));
if (range_header_read_result == 0) if (range_header_read_result == 0)
break; break;
if (range_header_read_result != sizeof(range_header)) { if (range_header_read_result != sizeof(range_header)) {
...@@ -1359,9 +1324,7 @@ bool SimpleSynchronousEntry::ReadSparseRange(const SparseRange* range, ...@@ -1359,9 +1324,7 @@ bool SimpleSynchronousEntry::ReadSparseRange(const SparseRange* range,
DCHECK_GE(range->length, offset); DCHECK_GE(range->length, offset);
DCHECK_GE(range->length, offset + len); DCHECK_GE(range->length, offset + len);
int bytes_read = ReadPlatformFile(sparse_file_, int bytes_read = sparse_file_.Read(range->file_offset + offset, buf, len);
range->file_offset + offset,
buf, len);
if (bytes_read < len) { if (bytes_read < len) {
DLOG(WARNING) << "Could not read sparse range."; DLOG(WARNING) << "Could not read sparse range.";
return false; return false;
...@@ -1406,19 +1369,16 @@ bool SimpleSynchronousEntry::WriteSparseRange(SparseRange* range, ...@@ -1406,19 +1369,16 @@ bool SimpleSynchronousEntry::WriteSparseRange(SparseRange* range,
header.length = range->length; header.length = range->length;
header.data_crc32 = range->data_crc32; header.data_crc32 = range->data_crc32;
int bytes_written = WritePlatformFile(sparse_file_, int bytes_written = sparse_file_.Write(range->file_offset - sizeof(header),
range->file_offset - sizeof(header), reinterpret_cast<char*>(&header),
reinterpret_cast<char*>(&header), sizeof(header));
sizeof(header));
if (bytes_written != implicit_cast<int>(sizeof(header))) { if (bytes_written != implicit_cast<int>(sizeof(header))) {
DLOG(WARNING) << "Could not rewrite sparse range header."; DLOG(WARNING) << "Could not rewrite sparse range header.";
return false; return false;
} }
} }
int bytes_written = WritePlatformFile(sparse_file_, int bytes_written = sparse_file_.Write(range->file_offset + offset, buf, len);
range->file_offset + offset,
buf, len);
if (bytes_written < len) { if (bytes_written < len) {
DLOG(WARNING) << "Could not write sparse range."; DLOG(WARNING) << "Could not write sparse range.";
return false; return false;
...@@ -1444,20 +1404,16 @@ bool SimpleSynchronousEntry::AppendSparseRange(int64 offset, ...@@ -1444,20 +1404,16 @@ bool SimpleSynchronousEntry::AppendSparseRange(int64 offset,
header.length = len; header.length = len;
header.data_crc32 = data_crc32; header.data_crc32 = data_crc32;
int bytes_written = WritePlatformFile(sparse_file_, int bytes_written = sparse_file_.Write(sparse_tail_offset_,
sparse_tail_offset_, reinterpret_cast<char*>(&header),
reinterpret_cast<char*>(&header), sizeof(header));
sizeof(header));
if (bytes_written != implicit_cast<int>(sizeof(header))) { if (bytes_written != implicit_cast<int>(sizeof(header))) {
DLOG(WARNING) << "Could not append sparse range header."; DLOG(WARNING) << "Could not append sparse range header.";
return false; return false;
} }
sparse_tail_offset_ += bytes_written; sparse_tail_offset_ += bytes_written;
bytes_written = WritePlatformFile(sparse_file_, bytes_written = sparse_file_.Write(sparse_tail_offset_, buf, len);
sparse_tail_offset_,
buf,
len);
if (bytes_written < len) { if (bytes_written < len) {
DLOG(WARNING) << "Could not append sparse range data."; DLOG(WARNING) << "Could not append sparse range data.";
return false; return false;
......
...@@ -11,10 +11,10 @@ ...@@ -11,10 +11,10 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "base/files/file.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/platform_file.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "net/base/cache_type.h" #include "net/base/cache_type.h"
#include "net/base/net_export.h" #include "net/base/net_export.h"
...@@ -213,13 +213,13 @@ class SimpleSynchronousEntry { ...@@ -213,13 +213,13 @@ class SimpleSynchronousEntry {
// or if the file was not found and is allowed to be omitted if the // or if the file was not found and is allowed to be omitted if the
// corresponding stream is empty. // corresponding stream is empty.
bool MaybeOpenFile(int file_index, bool MaybeOpenFile(int file_index,
base::PlatformFileError* out_error); base::File::Error* out_error);
// Creates one of the cache entry files if necessary. If the file is allowed // Creates one of the cache entry files if necessary. If the file is allowed
// to be omitted if the corresponding stream is empty, and if |file_required| // to be omitted if the corresponding stream is empty, and if |file_required|
// is FILE_NOT_REQUIRED, then the file is not created; otherwise, it is. // is FILE_NOT_REQUIRED, then the file is not created; otherwise, it is.
bool MaybeCreateFile(int file_index, bool MaybeCreateFile(int file_index,
FileRequired file_required, FileRequired file_required,
base::PlatformFileError* out_error); base::File::Error* out_error);
bool OpenFiles(bool had_index, bool OpenFiles(bool had_index,
SimpleEntryStat* out_entry_stat); SimpleEntryStat* out_entry_stat);
bool CreateFiles(bool had_index, bool CreateFiles(bool had_index,
...@@ -268,7 +268,7 @@ class SimpleSynchronousEntry { ...@@ -268,7 +268,7 @@ class SimpleSynchronousEntry {
bool CreateSparseFile(); bool CreateSparseFile();
// Closes the sparse data file. // Closes the sparse data file.
bool CloseSparseFile(); void CloseSparseFile();
// Writes the header to the (newly-created) sparse file. // Writes the header to the (newly-created) sparse file.
bool InitializeSparseFile(); bool InitializeSparseFile();
...@@ -305,7 +305,7 @@ class SimpleSynchronousEntry { ...@@ -305,7 +305,7 @@ class SimpleSynchronousEntry {
base::FilePath GetFilenameFromFileIndex(int file_index); base::FilePath GetFilenameFromFileIndex(int file_index);
bool sparse_file_open() const { bool sparse_file_open() const {
return sparse_file_ != base::kInvalidPlatformFileValue; return sparse_file_.IsValid();
} }
const net::CacheType cache_type_; const net::CacheType cache_type_;
...@@ -316,7 +316,7 @@ class SimpleSynchronousEntry { ...@@ -316,7 +316,7 @@ class SimpleSynchronousEntry {
bool have_open_files_; bool have_open_files_;
bool initialized_; bool initialized_;
base::PlatformFile files_[kSimpleEntryFileCount]; base::File files_[kSimpleEntryFileCount];
// True if the corresponding stream is empty and therefore no on-disk file // True if the corresponding stream is empty and therefore no on-disk file
// was created to store it. // was created to store it.
...@@ -325,7 +325,7 @@ class SimpleSynchronousEntry { ...@@ -325,7 +325,7 @@ class SimpleSynchronousEntry {
typedef std::map<int64, SparseRange> SparseRangeOffsetMap; typedef std::map<int64, SparseRange> SparseRangeOffsetMap;
typedef SparseRangeOffsetMap::iterator SparseRangeIterator; typedef SparseRangeOffsetMap::iterator SparseRangeIterator;
SparseRangeOffsetMap sparse_ranges_; SparseRangeOffsetMap sparse_ranges_;
base::PlatformFile sparse_file_; base::File sparse_file_;
// Offset of the end of the sparse file (where the next sparse range will be // Offset of the end of the sparse file (where the next sparse range will be
// written). // written).
int64 sparse_tail_offset_; int64 sparse_tail_offset_;
......
...@@ -66,7 +66,7 @@ NET_EXPORT_PRIVATE int64 GetFileSizeFromKeyAndDataSize(const std::string& key, ...@@ -66,7 +66,7 @@ NET_EXPORT_PRIVATE int64 GetFileSizeFromKeyAndDataSize(const std::string& key,
NET_EXPORT_PRIVATE int GetFileIndexFromStreamIndex(int stream_index); NET_EXPORT_PRIVATE int GetFileIndexFromStreamIndex(int stream_index);
// Fills |out_time| with the time the file last modified time. Unlike the // Fills |out_time| with the time the file last modified time. Unlike the
// functions in platform_file.h, the time resolution is milliseconds. // functions in file.h, the time resolution is milliseconds.
NET_EXPORT_PRIVATE bool GetMTime(const base::FilePath& path, NET_EXPORT_PRIVATE bool GetMTime(const base::FilePath& path,
base::Time* out_mtime); base::Time* out_mtime);
} // namespace simple_backend } // namespace simple_backend
......
...@@ -7,11 +7,11 @@ ...@@ -7,11 +7,11 @@
#include <cstring> #include <cstring>
#include "base/file_util.h" #include "base/file_util.h"
#include "base/files/file.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/files/memory_mapped_file.h" #include "base/files/memory_mapped_file.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/pickle.h" #include "base/pickle.h"
#include "base/platform_file.h"
#include "net/disk_cache/simple/simple_backend_version.h" #include "net/disk_cache/simple/simple_backend_version.h"
#include "net/disk_cache/simple/simple_entry_format_history.h" #include "net/disk_cache/simple/simple_entry_format_history.h"
#include "third_party/zlib/zlib.h" #include "third_party/zlib/zlib.h"
...@@ -30,20 +30,17 @@ void LogMessageFailedUpgradeFromVersion(int version) { ...@@ -30,20 +30,17 @@ void LogMessageFailedUpgradeFromVersion(int version) {
} }
bool WriteFakeIndexFile(const base::FilePath& file_name) { bool WriteFakeIndexFile(const base::FilePath& file_name) {
base::PlatformFileError error; base::File file(file_name, base::File::FLAG_CREATE | base::File::FLAG_WRITE);
base::PlatformFile file = base::CreatePlatformFile( if (!file.IsValid())
file_name, return false;
base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
NULL,
&error);
disk_cache::FakeIndexData file_contents; disk_cache::FakeIndexData file_contents;
file_contents.initial_magic_number = file_contents.initial_magic_number =
disk_cache::simplecache_v5::kSimpleInitialMagicNumber; disk_cache::simplecache_v5::kSimpleInitialMagicNumber;
file_contents.version = disk_cache::kSimpleVersion; file_contents.version = disk_cache::kSimpleVersion;
int bytes_written = base::WritePlatformFile( int bytes_written = file.Write(0, reinterpret_cast<char*>(&file_contents),
file, 0, reinterpret_cast<char*>(&file_contents), sizeof(file_contents)); sizeof(file_contents));
if (!base::ClosePlatformFile(file) || if (bytes_written != sizeof(file_contents)) {
bytes_written != sizeof(file_contents)) {
LOG(ERROR) << "Failed to write fake index file: " LOG(ERROR) << "Failed to write fake index file: "
<< file_name.LossyDisplayName(); << file_name.LossyDisplayName();
return false; return false;
...@@ -136,29 +133,27 @@ bool UpgradeSimpleCacheOnDisk(const base::FilePath& path) { ...@@ -136,29 +133,27 @@ bool UpgradeSimpleCacheOnDisk(const base::FilePath& path) {
// 2. The Simple Backend has pickled file format for the index making it hacky // 2. The Simple Backend has pickled file format for the index making it hacky
// to have the magic in the right place. // to have the magic in the right place.
const base::FilePath fake_index = path.AppendASCII(kFakeIndexFileName); const base::FilePath fake_index = path.AppendASCII(kFakeIndexFileName);
base::PlatformFileError error; base::File fake_index_file(fake_index,
base::PlatformFile fake_index_file = base::CreatePlatformFile( base::File::FLAG_OPEN | base::File::FLAG_READ);
fake_index,
base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, if (!fake_index_file.IsValid()) {
NULL, if (fake_index_file.error_details() == base::File::FILE_ERROR_NOT_FOUND) {
&error); return WriteFakeIndexFile(fake_index);
if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) { }
return WriteFakeIndexFile(fake_index);
} else if (error != base::PLATFORM_FILE_OK) {
return false; return false;
} }
FakeIndexData file_header; FakeIndexData file_header;
int bytes_read = base::ReadPlatformFile(fake_index_file, int bytes_read = fake_index_file.Read(0,
0, reinterpret_cast<char*>(&file_header),
reinterpret_cast<char*>(&file_header), sizeof(file_header));
sizeof(file_header)); if (bytes_read != sizeof(file_header) ||
if (!base::ClosePlatformFile(fake_index_file) ||
bytes_read != sizeof(file_header) ||
file_header.initial_magic_number != file_header.initial_magic_number !=
disk_cache::simplecache_v5::kSimpleInitialMagicNumber) { disk_cache::simplecache_v5::kSimpleInitialMagicNumber) {
LOG(ERROR) << "File structure does not match the disk cache backend."; LOG(ERROR) << "File structure does not match the disk cache backend.";
return false; return false;
} }
fake_index_file.Close();
uint32 version_from = file_header.version; uint32 version_from = file_header.version;
if (version_from < kMinVersionAbleToUpgrade || if (version_from < kMinVersionAbleToUpgrade ||
......
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/file_util.h" #include "base/file_util.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/platform_file.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/synchronization/lock.h" #include "base/synchronization/lock.h"
#include "base/task_runner.h" #include "base/task_runner.h"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment