OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/disk_cache/simple/simple_synchronous_entry.h" | 5 #include "net/disk_cache/simple/simple_synchronous_entry.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstring> | 8 #include <cstring> |
9 #include <functional> | 9 #include <functional> |
10 #include <limits> | 10 #include <limits> |
(...skipping 701 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
712 if (have_open_files_) | 712 if (have_open_files_) |
713 CloseFiles(); | 713 CloseFiles(); |
714 } | 714 } |
715 | 715 |
716 bool SimpleSynchronousEntry::MaybeOpenFile( | 716 bool SimpleSynchronousEntry::MaybeOpenFile( |
717 int file_index, | 717 int file_index, |
718 File::Error* out_error) { | 718 File::Error* out_error) { |
719 DCHECK(out_error); | 719 DCHECK(out_error); |
720 | 720 |
721 FilePath filename = GetFilenameFromFileIndex(file_index); | 721 FilePath filename = GetFilenameFromFileIndex(file_index); |
722 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE; | 722 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE | |
| 723 File::FLAG_SHARE_DELETE; |
723 files_[file_index].Initialize(filename, flags); | 724 files_[file_index].Initialize(filename, flags); |
724 *out_error = files_[file_index].error_details(); | 725 *out_error = files_[file_index].error_details(); |
725 | 726 |
726 if (CanOmitEmptyFile(file_index) && !files_[file_index].IsValid() && | 727 if (CanOmitEmptyFile(file_index) && !files_[file_index].IsValid() && |
727 *out_error == File::FILE_ERROR_NOT_FOUND) { | 728 *out_error == File::FILE_ERROR_NOT_FOUND) { |
728 empty_file_omitted_[file_index] = true; | 729 empty_file_omitted_[file_index] = true; |
729 return true; | 730 return true; |
730 } | 731 } |
731 | 732 |
732 return files_[file_index].IsValid(); | 733 return files_[file_index].IsValid(); |
733 } | 734 } |
734 | 735 |
735 bool SimpleSynchronousEntry::MaybeCreateFile( | 736 bool SimpleSynchronousEntry::MaybeCreateFile( |
736 int file_index, | 737 int file_index, |
737 FileRequired file_required, | 738 FileRequired file_required, |
738 File::Error* out_error) { | 739 File::Error* out_error) { |
739 DCHECK(out_error); | 740 DCHECK(out_error); |
740 | 741 |
741 if (CanOmitEmptyFile(file_index) && file_required == FILE_NOT_REQUIRED) { | 742 if (CanOmitEmptyFile(file_index) && file_required == FILE_NOT_REQUIRED) { |
742 empty_file_omitted_[file_index] = true; | 743 empty_file_omitted_[file_index] = true; |
743 return true; | 744 return true; |
744 } | 745 } |
745 | 746 |
746 FilePath filename = GetFilenameFromFileIndex(file_index); | 747 FilePath filename = GetFilenameFromFileIndex(file_index); |
747 int flags = File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE; | 748 int flags = File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE | |
| 749 File::FLAG_SHARE_DELETE; |
748 files_[file_index].Initialize(filename, flags); | 750 files_[file_index].Initialize(filename, flags); |
749 *out_error = files_[file_index].error_details(); | 751 *out_error = files_[file_index].error_details(); |
750 | 752 |
751 empty_file_omitted_[file_index] = false; | 753 empty_file_omitted_[file_index] = false; |
752 | 754 |
753 return files_[file_index].IsValid(); | 755 return files_[file_index].IsValid(); |
754 } | 756 } |
755 | 757 |
756 bool SimpleSynchronousEntry::OpenFiles( | 758 bool SimpleSynchronousEntry::OpenFiles( |
757 bool had_index, | 759 bool had_index, |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1129 DeleteFilesForEntryHash(path_, entry_hash_); | 1131 DeleteFilesForEntryHash(path_, entry_hash_); |
1130 } | 1132 } |
1131 | 1133 |
1132 // static | 1134 // static |
1133 bool SimpleSynchronousEntry::DeleteFileForEntryHash( | 1135 bool SimpleSynchronousEntry::DeleteFileForEntryHash( |
1134 const FilePath& path, | 1136 const FilePath& path, |
1135 const uint64 entry_hash, | 1137 const uint64 entry_hash, |
1136 const int file_index) { | 1138 const int file_index) { |
1137 FilePath to_delete = path.AppendASCII( | 1139 FilePath to_delete = path.AppendASCII( |
1138 GetFilenameFromEntryHashAndFileIndex(entry_hash, file_index)); | 1140 GetFilenameFromEntryHashAndFileIndex(entry_hash, file_index)); |
1139 return base::DeleteFile(to_delete, false); | 1141 return simple_util::SimpleCacheDeleteFile(to_delete); |
1140 } | 1142 } |
1141 | 1143 |
1142 // static | 1144 // static |
1143 bool SimpleSynchronousEntry::DeleteFilesForEntryHash( | 1145 bool SimpleSynchronousEntry::DeleteFilesForEntryHash( |
1144 const FilePath& path, | 1146 const FilePath& path, |
1145 const uint64 entry_hash) { | 1147 const uint64 entry_hash) { |
1146 bool result = true; | 1148 bool result = true; |
1147 for (int i = 0; i < kSimpleEntryFileCount; ++i) { | 1149 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
1148 if (!DeleteFileForEntryHash(path, entry_hash, i) && !CanOmitEmptyFile(i)) | 1150 if (!DeleteFileForEntryHash(path, entry_hash, i) && !CanOmitEmptyFile(i)) |
1149 result = false; | 1151 result = false; |
1150 } | 1152 } |
1151 FilePath to_delete = path.AppendASCII( | 1153 FilePath to_delete = path.AppendASCII( |
1152 GetSparseFilenameFromEntryHash(entry_hash)); | 1154 GetSparseFilenameFromEntryHash(entry_hash)); |
1153 base::DeleteFile(to_delete, false); | 1155 simple_util::SimpleCacheDeleteFile(to_delete); |
1154 return result; | 1156 return result; |
1155 } | 1157 } |
1156 | 1158 |
1157 void SimpleSynchronousEntry::RecordSyncCreateResult(CreateEntryResult result, | 1159 void SimpleSynchronousEntry::RecordSyncCreateResult(CreateEntryResult result, |
1158 bool had_index) { | 1160 bool had_index) { |
1159 DCHECK_LT(result, CREATE_ENTRY_MAX); | 1161 DCHECK_LT(result, CREATE_ENTRY_MAX); |
1160 SIMPLE_CACHE_UMA(ENUMERATION, | 1162 SIMPLE_CACHE_UMA(ENUMERATION, |
1161 "SyncCreateResult", cache_type_, result, CREATE_ENTRY_MAX); | 1163 "SyncCreateResult", cache_type_, result, CREATE_ENTRY_MAX); |
1162 if (had_index) { | 1164 if (had_index) { |
1163 SIMPLE_CACHE_UMA(ENUMERATION, | 1165 SIMPLE_CACHE_UMA(ENUMERATION, |
(...skipping 10 matching lines...) Expand all Loading... |
1174 return path_.AppendASCII( | 1176 return path_.AppendASCII( |
1175 GetFilenameFromEntryHashAndFileIndex(entry_hash_, file_index)); | 1177 GetFilenameFromEntryHashAndFileIndex(entry_hash_, file_index)); |
1176 } | 1178 } |
1177 | 1179 |
1178 bool SimpleSynchronousEntry::OpenSparseFileIfExists( | 1180 bool SimpleSynchronousEntry::OpenSparseFileIfExists( |
1179 int32* out_sparse_data_size) { | 1181 int32* out_sparse_data_size) { |
1180 DCHECK(!sparse_file_open()); | 1182 DCHECK(!sparse_file_open()); |
1181 | 1183 |
1182 FilePath filename = path_.AppendASCII( | 1184 FilePath filename = path_.AppendASCII( |
1183 GetSparseFilenameFromEntryHash(entry_hash_)); | 1185 GetSparseFilenameFromEntryHash(entry_hash_)); |
1184 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE; | 1186 int flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE | |
| 1187 File::FLAG_SHARE_DELETE; |
1185 sparse_file_.Initialize(filename, flags); | 1188 sparse_file_.Initialize(filename, flags); |
1186 if (sparse_file_.IsValid()) | 1189 if (sparse_file_.IsValid()) |
1187 return ScanSparseFile(out_sparse_data_size); | 1190 return ScanSparseFile(out_sparse_data_size); |
1188 | 1191 |
1189 return sparse_file_.error_details() == File::FILE_ERROR_NOT_FOUND; | 1192 return sparse_file_.error_details() == File::FILE_ERROR_NOT_FOUND; |
1190 } | 1193 } |
1191 | 1194 |
1192 bool SimpleSynchronousEntry::CreateSparseFile() { | 1195 bool SimpleSynchronousEntry::CreateSparseFile() { |
1193 DCHECK(!sparse_file_open()); | 1196 DCHECK(!sparse_file_open()); |
1194 | 1197 |
1195 FilePath filename = path_.AppendASCII( | 1198 FilePath filename = path_.AppendASCII( |
1196 GetSparseFilenameFromEntryHash(entry_hash_)); | 1199 GetSparseFilenameFromEntryHash(entry_hash_)); |
1197 int flags = File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE; | 1200 int flags = File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE | |
| 1201 File::FLAG_SHARE_DELETE; |
1198 sparse_file_.Initialize(filename, flags); | 1202 sparse_file_.Initialize(filename, flags); |
1199 if (!sparse_file_.IsValid()) | 1203 if (!sparse_file_.IsValid()) |
1200 return false; | 1204 return false; |
1201 | 1205 |
1202 return InitializeSparseFile(); | 1206 return InitializeSparseFile(); |
1203 } | 1207 } |
1204 | 1208 |
1205 void SimpleSynchronousEntry::CloseSparseFile() { | 1209 void SimpleSynchronousEntry::CloseSparseFile() { |
1206 DCHECK(sparse_file_open()); | 1210 DCHECK(sparse_file_open()); |
1207 sparse_file_.Close(); | 1211 sparse_file_.Close(); |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1422 range.offset = offset; | 1426 range.offset = offset; |
1423 range.length = len; | 1427 range.length = len; |
1424 range.data_crc32 = data_crc32; | 1428 range.data_crc32 = data_crc32; |
1425 range.file_offset = data_file_offset; | 1429 range.file_offset = data_file_offset; |
1426 sparse_ranges_.insert(std::make_pair(offset, range)); | 1430 sparse_ranges_.insert(std::make_pair(offset, range)); |
1427 | 1431 |
1428 return true; | 1432 return true; |
1429 } | 1433 } |
1430 | 1434 |
1431 } // namespace disk_cache | 1435 } // namespace disk_cache |
OLD | NEW |