| 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_path.h" | 11 #include "base/files/file_path.h" |
| 11 #include "base/files/memory_mapped_file.h" | 12 #include "base/files/memory_mapped_file.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/pickle.h" | 14 #include "base/pickle.h" |
| 14 #include "base/platform_file.h" | |
| 15 #include "net/disk_cache/simple/simple_backend_version.h" | 15 #include "net/disk_cache/simple/simple_backend_version.h" |
| 16 #include "net/disk_cache/simple/simple_entry_format_history.h" | 16 #include "net/disk_cache/simple/simple_entry_format_history.h" |
| 17 #include "third_party/zlib/zlib.h" | 17 #include "third_party/zlib/zlib.h" |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 // It is not possible to upgrade cache structures on disk that are of version | 21 // It is not possible to upgrade cache structures on disk that are of version |
| 22 // below this, the entire cache should be dropped for them. | 22 // below this, the entire cache should be dropped for them. |
| 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::PlatformFileError error; | 33 base::File file(file_name, base::File::FLAG_CREATE | base::File::FLAG_WRITE); |
| 34 base::PlatformFile file = base::CreatePlatformFile( | 34 if (!file.IsValid()) |
| 35 file_name, | 35 return false; |
| 36 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE, | 36 |
| 37 NULL, | |
| 38 &error); | |
| 39 disk_cache::FakeIndexData file_contents; | 37 disk_cache::FakeIndexData file_contents; |
| 40 file_contents.initial_magic_number = | 38 file_contents.initial_magic_number = |
| 41 disk_cache::simplecache_v5::kSimpleInitialMagicNumber; | 39 disk_cache::simplecache_v5::kSimpleInitialMagicNumber; |
| 42 file_contents.version = disk_cache::kSimpleVersion; | 40 file_contents.version = disk_cache::kSimpleVersion; |
| 43 int bytes_written = base::WritePlatformFile( | 41 int bytes_written = file.Write(0, reinterpret_cast<char*>(&file_contents), |
| 44 file, 0, reinterpret_cast<char*>(&file_contents), sizeof(file_contents)); | 42 sizeof(file_contents)); |
| 45 if (!base::ClosePlatformFile(file) || | 43 if (bytes_written != sizeof(file_contents)) { |
| 46 bytes_written != sizeof(file_contents)) { | |
| 47 LOG(ERROR) << "Failed to write fake index file: " | 44 LOG(ERROR) << "Failed to write fake index file: " |
| 48 << file_name.LossyDisplayName(); | 45 << file_name.LossyDisplayName(); |
| 49 return false; | 46 return false; |
| 50 } | 47 } |
| 51 return true; | 48 return true; |
| 52 } | 49 } |
| 53 | 50 |
| 54 } // namespace | 51 } // namespace |
| 55 | 52 |
| 56 namespace disk_cache { | 53 namespace disk_cache { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 // There is a convention among disk cache backends: looking at the magic in | 126 // There is a convention among disk cache backends: looking at the magic in |
| 130 // the file "index" it should be sufficient to determine if the cache belongs | 127 // the file "index" it should be sufficient to determine if the cache belongs |
| 131 // to the currently running backend. The Simple Backend stores its index in | 128 // to the currently running backend. The Simple Backend stores its index in |
| 132 // the file "the-real-index" (see simple_index_file.cc) and the file "index" | 129 // the file "the-real-index" (see simple_index_file.cc) and the file "index" |
| 133 // only signifies presence of the implementation's magic and version. There | 130 // only signifies presence of the implementation's magic and version. There |
| 134 // are two reasons for that: | 131 // are two reasons for that: |
| 135 // 1. Absence of the index is itself not a fatal error in the Simple Backend | 132 // 1. Absence of the index is itself not a fatal error in the Simple Backend |
| 136 // 2. The Simple Backend has pickled file format for the index making it hacky | 133 // 2. The Simple Backend has pickled file format for the index making it hacky |
| 137 // to have the magic in the right place. | 134 // to have the magic in the right place. |
| 138 const base::FilePath fake_index = path.AppendASCII(kFakeIndexFileName); | 135 const base::FilePath fake_index = path.AppendASCII(kFakeIndexFileName); |
| 139 base::PlatformFileError error; | 136 base::File fake_index_file(fake_index, |
| 140 base::PlatformFile fake_index_file = base::CreatePlatformFile( | 137 base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 141 fake_index, | 138 |
| 142 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, | 139 if (!fake_index_file.IsValid()) { |
| 143 NULL, | 140 if (fake_index_file.error() == base::File::FILE_ERROR_NOT_FOUND) { |
| 144 &error); | 141 return WriteFakeIndexFile(fake_index); |
| 145 if (error == base::PLATFORM_FILE_ERROR_NOT_FOUND) { | 142 } |
| 146 return WriteFakeIndexFile(fake_index); | |
| 147 } else if (error != base::PLATFORM_FILE_OK) { | |
| 148 return false; | 143 return false; |
| 149 } | 144 } |
| 145 |
| 150 FakeIndexData file_header; | 146 FakeIndexData file_header; |
| 151 int bytes_read = base::ReadPlatformFile(fake_index_file, | 147 int bytes_read = fake_index_file.Read(0, |
| 152 0, | 148 reinterpret_cast<char*>(&file_header), |
| 153 reinterpret_cast<char*>(&file_header), | 149 sizeof(file_header)); |
| 154 sizeof(file_header)); | 150 if (bytes_read != sizeof(file_header) || |
| 155 if (!base::ClosePlatformFile(fake_index_file) || | |
| 156 bytes_read != sizeof(file_header) || | |
| 157 file_header.initial_magic_number != | 151 file_header.initial_magic_number != |
| 158 disk_cache::simplecache_v5::kSimpleInitialMagicNumber) { | 152 disk_cache::simplecache_v5::kSimpleInitialMagicNumber) { |
| 159 LOG(ERROR) << "File structure does not match the disk cache backend."; | 153 LOG(ERROR) << "File structure does not match the disk cache backend."; |
| 160 return false; | 154 return false; |
| 161 } | 155 } |
| 156 fake_index_file.Close(); |
| 162 | 157 |
| 163 uint32 version_from = file_header.version; | 158 uint32 version_from = file_header.version; |
| 164 if (version_from < kMinVersionAbleToUpgrade || | 159 if (version_from < kMinVersionAbleToUpgrade || |
| 165 version_from > kSimpleVersion) { | 160 version_from > kSimpleVersion) { |
| 166 LOG(ERROR) << "Inconsistent cache version."; | 161 LOG(ERROR) << "Inconsistent cache version."; |
| 167 return false; | 162 return false; |
| 168 } | 163 } |
| 169 bool upgrade_needed = (version_from != kSimpleVersion); | 164 bool upgrade_needed = (version_from != kSimpleVersion); |
| 170 if (version_from == kMinVersionAbleToUpgrade) { | 165 if (version_from == kMinVersionAbleToUpgrade) { |
| 171 // Upgrade only the index for V4 -> V5 move. | 166 // Upgrade only the index for V4 -> V5 move. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 195 } | 190 } |
| 196 } | 191 } |
| 197 // Verify during the test stage that the upgraders are implemented for all | 192 // Verify during the test stage that the upgraders are implemented for all |
| 198 // versions. The release build would cause backend initialization failure | 193 // versions. The release build would cause backend initialization failure |
| 199 // which would then later lead to removing all files known to the backend. | 194 // which would then later lead to removing all files known to the backend. |
| 200 DCHECK_EQ(kSimpleVersion, version_from); | 195 DCHECK_EQ(kSimpleVersion, version_from); |
| 201 return false; | 196 return false; |
| 202 } | 197 } |
| 203 | 198 |
| 204 } // namespace disk_cache | 199 } // namespace disk_cache |
| OLD | NEW |