| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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_version_upgrade.h" | 5 #include "net/disk_cache/simple/simple_version_upgrade.h" |
| 6 | 6 |
| 7 #include <cstring> | 7 #include <cstring> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 const uint32 kMinVersionAbleToUpgrade = 5; | 23 const uint32 kMinVersionAbleToUpgrade = 5; |
| 24 | 24 |
| 25 const char kFakeIndexFileName[] = "index"; | 25 const char kFakeIndexFileName[] = "index"; |
| 26 const char kIndexFileName[] = "the-real-index"; | 26 const char kIndexFileName[] = "the-real-index"; |
| 27 | 27 |
| 28 void LogMessageFailedUpgradeFromVersion(int version) { | 28 void LogMessageFailedUpgradeFromVersion(int version) { |
| 29 LOG(ERROR) << "Failed to upgrade Simple Cache from version: " << version; | 29 LOG(ERROR) << "Failed to upgrade Simple Cache from version: " << version; |
| 30 } | 30 } |
| 31 | 31 |
| 32 bool WriteFakeIndexFile(const base::FilePath& file_name) { | 32 bool WriteFakeIndexFile(const base::FilePath& file_name) { |
| 33 base::File file(file_name, base::File::FLAG_CREATE | base::File::FLAG_WRITE); | 33 base::File file(file_name, base::File::FLAG_CREATE | base::File::FLAG_WRITE); |
| 34 if (!file.IsValid()) | 34 if (!file.IsValid()) |
| 35 return false; | 35 return false; |
| 36 | 36 |
| 37 disk_cache::FakeIndexData file_contents; | 37 disk_cache::FakeIndexData file_contents; |
| 38 file_contents.initial_magic_number = | 38 file_contents.initial_magic_number = |
| 39 disk_cache::simplecache_v5::kSimpleInitialMagicNumber; | 39 disk_cache::simplecache_v5::kSimpleInitialMagicNumber; |
| 40 file_contents.version = disk_cache::kSimpleVersion; | 40 file_contents.version = disk_cache::kSimpleVersion; |
| 41 int bytes_written = file.Write(0, reinterpret_cast<char*>(&file_contents), | 41 int bytes_written = file.Write( |
| 42 sizeof(file_contents)); | 42 0, reinterpret_cast<char*>(&file_contents), sizeof(file_contents)); |
| 43 if (bytes_written != sizeof(file_contents)) { | 43 if (bytes_written != sizeof(file_contents)) { |
| 44 LOG(ERROR) << "Failed to write fake index file: " | 44 LOG(ERROR) << "Failed to write fake index file: " |
| 45 << file_name.LossyDisplayName(); | 45 << file_name.LossyDisplayName(); |
| 46 return false; | 46 return false; |
| 47 } | 47 } |
| 48 return true; | 48 return true; |
| 49 } | 49 } |
| 50 | 50 |
| 51 } // namespace | 51 } // namespace |
| 52 | 52 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 base::File::FLAG_OPEN | base::File::FLAG_READ); | 137 base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 138 | 138 |
| 139 if (!fake_index_file.IsValid()) { | 139 if (!fake_index_file.IsValid()) { |
| 140 if (fake_index_file.error_details() == base::File::FILE_ERROR_NOT_FOUND) { | 140 if (fake_index_file.error_details() == base::File::FILE_ERROR_NOT_FOUND) { |
| 141 return WriteFakeIndexFile(fake_index); | 141 return WriteFakeIndexFile(fake_index); |
| 142 } | 142 } |
| 143 return false; | 143 return false; |
| 144 } | 144 } |
| 145 | 145 |
| 146 FakeIndexData file_header; | 146 FakeIndexData file_header; |
| 147 int bytes_read = fake_index_file.Read(0, | 147 int bytes_read = fake_index_file.Read( |
| 148 reinterpret_cast<char*>(&file_header), | 148 0, reinterpret_cast<char*>(&file_header), sizeof(file_header)); |
| 149 sizeof(file_header)); | |
| 150 if (bytes_read != sizeof(file_header) || | 149 if (bytes_read != sizeof(file_header) || |
| 151 file_header.initial_magic_number != | 150 file_header.initial_magic_number != |
| 152 disk_cache::simplecache_v5::kSimpleInitialMagicNumber) { | 151 disk_cache::simplecache_v5::kSimpleInitialMagicNumber) { |
| 153 LOG(ERROR) << "File structure does not match the disk cache backend."; | 152 LOG(ERROR) << "File structure does not match the disk cache backend."; |
| 154 return false; | 153 return false; |
| 155 } | 154 } |
| 156 fake_index_file.Close(); | 155 fake_index_file.Close(); |
| 157 | 156 |
| 158 uint32 version_from = file_header.version; | 157 uint32 version_from = file_header.version; |
| 159 if (version_from < kMinVersionAbleToUpgrade || | 158 if (version_from < kMinVersionAbleToUpgrade || |
| (...skipping 30 matching lines...) Expand all Loading... |
| 190 } | 189 } |
| 191 } | 190 } |
| 192 // Verify during the test stage that the upgraders are implemented for all | 191 // Verify during the test stage that the upgraders are implemented for all |
| 193 // versions. The release build would cause backend initialization failure | 192 // versions. The release build would cause backend initialization failure |
| 194 // which would then later lead to removing all files known to the backend. | 193 // which would then later lead to removing all files known to the backend. |
| 195 DCHECK_EQ(kSimpleVersion, version_from); | 194 DCHECK_EQ(kSimpleVersion, version_from); |
| 196 return false; | 195 return false; |
| 197 } | 196 } |
| 198 | 197 |
| 199 } // namespace disk_cache | 198 } // namespace disk_cache |
| OLD | NEW |