1 // Copyright 2013 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_VERSION_UPGRADE_H_ 6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_VERSION_UPGRADE_H_ 7 8 // Defines functionality to upgrade the file structure of the Simple Cache 9 // Backend on disk. Assumes no backend operations are running simultaneously. 10 // Hence must be run at cache initialization step. 11 12 #include <stdint.h> 13 14 #include "net/base/cache_type.h" 15 #include "net/base/net_export.h" 16 17 namespace base { 18 class FilePath; 19 } 20 21 namespace disk_cache { 22 23 class BackendFileOperations; 24 25 // These values are persisted to logs. Entries should not be renumbered and 26 // numeric values should never be reused. 27 enum class SimpleCacheConsistencyResult { 28 kOK = 0, 29 kCreateDirectoryFailed = 1, 30 kBadFakeIndexFile = 2, 31 kBadInitialMagicNumber = 3, 32 kVersionTooOld = 4, 33 kVersionFromTheFuture = 5, 34 kBadZeroCheck = 6, 35 kUpgradeIndexV5V6Failed = 7, 36 kWriteFakeIndexFileFailed = 8, 37 kReplaceFileFailed = 9, 38 kBadFakeIndexReadSize = 10, 39 kMaxValue = kBadFakeIndexReadSize, 40 }; 41 42 // Performs all necessary disk IO to upgrade the cache structure if it is 43 // needed. 44 // 45 // Returns true iff no errors were found during consistency checks and all 46 // necessary transitions succeeded. If this function fails, there is nothing 47 // left to do other than dropping the whole cache directory. 48 NET_EXPORT_PRIVATE SimpleCacheConsistencyResult 49 UpgradeSimpleCacheOnDisk(BackendFileOperations* file_operations, 50 const base::FilePath& path); 51 52 // Check if the cache structure at the given path is empty except for index 53 // files. If so, then delete the index files. Returns true if any files 54 // were deleted. 55 NET_EXPORT_PRIVATE bool DeleteIndexFilesIfCacheIsEmpty( 56 const base::FilePath& path); 57 58 struct NET_EXPORT_PRIVATE FakeIndexData { 59 FakeIndexData(); 60 61 // Must be equal to simplecache_v4::kSimpleInitialMagicNumber. 62 uint64_t initial_magic_number; 63 64 // Must be equal kSimpleVersion when the cache backend is instantiated. 65 uint32_t version; 66 67 // These must be zero. The first was used for experiment type (With a max 68 // valid value of 2), and the second was used for an experiment parameter. 69 uint32_t zero; 70 uint32_t zero2; 71 }; 72 73 // Exposed for testing. 74 NET_EXPORT_PRIVATE bool UpgradeIndexV5V6(BackendFileOperations* file_operations, 75 const base::FilePath& cache_directory); 76 77 } // namespace disk_cache 78 79 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_VERSION_UPGRADE_H_ 80