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