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_UTIL_H_ 6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_UTIL_H_ 7 8 #include <stdint.h> 9 10 #include <string> 11 12 #include "base/strings/string_piece.h" 13 #include "net/base/net_export.h" 14 #include "net/disk_cache/simple/simple_file_tracker.h" 15 16 namespace base { 17 class FilePath; 18 } 19 20 namespace disk_cache::simple_util { 21 22 NET_EXPORT_PRIVATE std::string ConvertEntryHashKeyToHexString( 23 uint64_t hash_key); 24 25 // |key| is the regular cache key, such as an URL. 26 // Returns the Hex ascii representation of the uint64_t hash_key. 27 NET_EXPORT_PRIVATE std::string GetEntryHashKeyAsHexString( 28 const std::string& key); 29 30 // |key| is the regular HTTP Cache key, which is a URL. 31 // Returns the hash of the key as uint64_t. 32 NET_EXPORT_PRIVATE uint64_t GetEntryHashKey(const std::string& key); 33 34 // Parses the |hash_key| string into a uint64_t buffer. 35 // |hash_key| string must be of the form: FFFFFFFFFFFFFFFF . 36 NET_EXPORT_PRIVATE bool GetEntryHashKeyFromHexString(base::StringPiece hash_key, 37 uint64_t* hash_key_out); 38 39 // Given a |key| for a (potential) entry in the simple backend and the |index| 40 // of a stream on that entry, returns the filename in which that stream would be 41 // stored. 42 NET_EXPORT_PRIVATE std::string GetFilenameFromKeyAndFileIndex( 43 const std::string& key, 44 int file_index); 45 46 // Same as |GetFilenameFromKeyAndIndex| above, but using a numeric key. 47 NET_EXPORT_PRIVATE std::string GetFilenameFromEntryFileKeyAndFileIndex( 48 const SimpleFileTracker::EntryFileKey& key, 49 int file_index); 50 51 // Given a |key| for an entry, returns the name of the sparse data file. 52 NET_EXPORT_PRIVATE std::string GetSparseFilenameFromEntryFileKey( 53 const SimpleFileTracker::EntryFileKey& key); 54 55 // Given the size of a key, the size in bytes of the header at the beginning 56 // of a simple cache file. 57 size_t GetHeaderSize(size_t key_length); 58 59 // Given the size of a file holding a stream in the simple backend and the key 60 // to an entry, returns the number of bytes in the stream. 61 NET_EXPORT_PRIVATE int32_t GetDataSizeFromFileSize(size_t key_length, 62 int64_t file_size); 63 64 // Given the size of a stream in the simple backend and the key to an entry, 65 // returns the number of bytes in the file. 66 NET_EXPORT_PRIVATE int64_t GetFileSizeFromDataSize(size_t key_length, 67 int32_t data_size); 68 69 // Given the stream index, returns the number of the file the stream is stored 70 // in. 71 NET_EXPORT_PRIVATE int GetFileIndexFromStreamIndex(int stream_index); 72 73 // Deletes a file, insuring POSIX semantics. Provided that all open handles to 74 // this file were opened with File::FLAG_WIN_SHARE_DELETE, it is possible to 75 // delete an open file and continue to use that file. After deleting an open 76 // file, it is possible to immediately create a new file with the same name. 77 NET_EXPORT_PRIVATE bool SimpleCacheDeleteFile(const base::FilePath& path); 78 79 uint32_t Crc32(const char* data, int length); 80 81 uint32_t IncrementalCrc32(uint32_t previous_crc, const char* data, int length); 82 83 } // namespace disk_cache::simple_util 84 85 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_UTIL_H_ 86