| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/tools/dump_cache/cache_dumper.h" | 5 #include "net/tools/dump_cache/cache_dumper.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 11 #include "net/disk_cache/blockfile/entry_impl.h" | 11 #include "net/disk_cache/blockfile/entry_impl.h" |
| 12 #include "net/http/http_cache.h" | 12 #include "net/http/http_cache.h" |
| 13 #include "net/http/http_response_headers.h" | 13 #include "net/http/http_response_headers.h" |
| 14 #include "net/http/http_response_info.h" | 14 #include "net/http/http_response_info.h" |
| 15 #include "net/tools/dump_cache/url_to_filename_encoder.h" | 15 #include "net/tools/dump_cache/url_to_filename_encoder.h" |
| 16 | 16 |
| 17 CacheDumper::CacheDumper(disk_cache::Backend* cache) | 17 CacheDumper::CacheDumper(disk_cache::Backend* cache) : cache_(cache) { |
| 18 : cache_(cache) { | |
| 19 } | 18 } |
| 20 | 19 |
| 21 int CacheDumper::CreateEntry(const std::string& key, | 20 int CacheDumper::CreateEntry(const std::string& key, |
| 22 disk_cache::Entry** entry, | 21 disk_cache::Entry** entry, |
| 23 const net::CompletionCallback& callback) { | 22 const net::CompletionCallback& callback) { |
| 24 return cache_->CreateEntry(key, entry, callback); | 23 return cache_->CreateEntry(key, entry, callback); |
| 25 } | 24 } |
| 26 | 25 |
| 27 int CacheDumper::WriteEntry(disk_cache::Entry* entry, int index, int offset, | 26 int CacheDumper::WriteEntry(disk_cache::Entry* entry, |
| 28 net::IOBuffer* buf, int buf_len, | 27 int index, |
| 28 int offset, |
| 29 net::IOBuffer* buf, |
| 30 int buf_len, |
| 29 const net::CompletionCallback& callback) { | 31 const net::CompletionCallback& callback) { |
| 30 return entry->WriteData(index, offset, buf, buf_len, callback, false); | 32 return entry->WriteData(index, offset, buf, buf_len, callback, false); |
| 31 } | 33 } |
| 32 | 34 |
| 33 void CacheDumper::CloseEntry(disk_cache::Entry* entry, base::Time last_used, | 35 void CacheDumper::CloseEntry(disk_cache::Entry* entry, |
| 36 base::Time last_used, |
| 34 base::Time last_modified) { | 37 base::Time last_modified) { |
| 35 if (entry) { | 38 if (entry) { |
| 36 static_cast<disk_cache::EntryImpl*>(entry)->SetTimes(last_used, | 39 static_cast<disk_cache::EntryImpl*>(entry) |
| 37 last_modified); | 40 ->SetTimes(last_used, last_modified); |
| 38 entry->Close(); | 41 entry->Close(); |
| 39 } | 42 } |
| 40 } | 43 } |
| 41 | 44 |
| 42 // A version of CreateDirectory which supports lengthy filenames. | 45 // A version of CreateDirectory which supports lengthy filenames. |
| 43 // Returns true on success, false on failure. | 46 // Returns true on success, false on failure. |
| 44 bool SafeCreateDirectory(const base::FilePath& path) { | 47 bool SafeCreateDirectory(const base::FilePath& path) { |
| 45 #ifdef WIN32_LARGE_FILENAME_SUPPORT | 48 #ifdef WIN32_LARGE_FILENAME_SUPPORT |
| 46 // Due to large paths on windows, it can't simply do a | 49 // Due to large paths on windows, it can't simply do a |
| 47 // CreateDirectory("a/b/c"). Instead, create each subdirectory manually. | 50 // CreateDirectory("a/b/c"). Instead, create each subdirectory manually. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 62 // we keep going even if directory creation failed. | 65 // we keep going even if directory creation failed. |
| 63 pos++; | 66 pos++; |
| 64 } | 67 } |
| 65 // Now create the full path | 68 // Now create the full path |
| 66 return CreateDirectoryW(path.value().c_str(), NULL) == TRUE; | 69 return CreateDirectoryW(path.value().c_str(), NULL) == TRUE; |
| 67 #else | 70 #else |
| 68 return base::CreateDirectory(path); | 71 return base::CreateDirectory(path); |
| 69 #endif | 72 #endif |
| 70 } | 73 } |
| 71 | 74 |
| 72 DiskDumper::DiskDumper(const base::FilePath& path) | 75 DiskDumper::DiskDumper(const base::FilePath& path) : path_(path), entry_(NULL) { |
| 73 : path_(path), entry_(NULL) { | |
| 74 base::CreateDirectory(path); | 76 base::CreateDirectory(path); |
| 75 } | 77 } |
| 76 | 78 |
| 77 int DiskDumper::CreateEntry(const std::string& key, | 79 int DiskDumper::CreateEntry(const std::string& key, |
| 78 disk_cache::Entry** entry, | 80 disk_cache::Entry** entry, |
| 79 const net::CompletionCallback& callback) { | 81 const net::CompletionCallback& callback) { |
| 80 // The URL may not start with a valid protocol; search for it. | 82 // The URL may not start with a valid protocol; search for it. |
| 81 int urlpos = key.find("http"); | 83 int urlpos = key.find("http"); |
| 82 std::string url = urlpos > 0 ? key.substr(urlpos) : key; | 84 std::string url = urlpos > 0 ? key.substr(urlpos) : key; |
| 83 std::string base_path = path_.MaybeAsASCII(); | 85 std::string base_path = path_.MaybeAsASCII(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 95 name.append(entry_path_.value()); | 97 name.append(entry_path_.value()); |
| 96 entry_path_ = base::FilePath(name); | 98 entry_path_ = base::FilePath(name); |
| 97 #endif | 99 #endif |
| 98 | 100 |
| 99 entry_url_ = key; | 101 entry_url_ = key; |
| 100 | 102 |
| 101 SafeCreateDirectory(entry_path_.DirName()); | 103 SafeCreateDirectory(entry_path_.DirName()); |
| 102 | 104 |
| 103 base::FilePath::StringType file = entry_path_.value(); | 105 base::FilePath::StringType file = entry_path_.value(); |
| 104 #ifdef WIN32_LARGE_FILENAME_SUPPORT | 106 #ifdef WIN32_LARGE_FILENAME_SUPPORT |
| 105 entry_ = CreateFileW(file.c_str(), GENERIC_WRITE|GENERIC_READ, 0, 0, | 107 entry_ = CreateFileW(file.c_str(), |
| 106 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); | 108 GENERIC_WRITE | GENERIC_READ, |
| 109 0, |
| 110 0, |
| 111 CREATE_ALWAYS, |
| 112 FILE_ATTRIBUTE_NORMAL, |
| 113 0); |
| 107 if (entry_ == INVALID_HANDLE_VALUE) | 114 if (entry_ == INVALID_HANDLE_VALUE) |
| 108 wprintf(L"CreateFileW (%s) failed: %d\n", file.c_str(), GetLastError()); | 115 wprintf(L"CreateFileW (%s) failed: %d\n", file.c_str(), GetLastError()); |
| 109 return (entry_ != INVALID_HANDLE_VALUE) ? net::OK : net::ERR_FAILED; | 116 return (entry_ != INVALID_HANDLE_VALUE) ? net::OK : net::ERR_FAILED; |
| 110 #else | 117 #else |
| 111 entry_ = base::OpenFile(entry_path_, "w+"); | 118 entry_ = base::OpenFile(entry_path_, "w+"); |
| 112 return (entry_ != NULL) ? net::OK : net::ERR_FAILED; | 119 return (entry_ != NULL) ? net::OK : net::ERR_FAILED; |
| 113 #endif | 120 #endif |
| 114 } | 121 } |
| 115 | 122 |
| 116 // Utility Function to create a normalized header string from a | 123 // Utility Function to create a normalized header string from a |
| (...skipping 29 matching lines...) Expand all Loading... |
| 146 output->append(name); | 153 output->append(name); |
| 147 output->append(": "); | 154 output->append(": "); |
| 148 output->append(value); | 155 output->append(value); |
| 149 output->append("\r\n"); | 156 output->append("\r\n"); |
| 150 } | 157 } |
| 151 | 158 |
| 152 // Mark the end of headers | 159 // Mark the end of headers |
| 153 output->append("\r\n"); | 160 output->append("\r\n"); |
| 154 } | 161 } |
| 155 | 162 |
| 156 int DiskDumper::WriteEntry(disk_cache::Entry* entry, int index, int offset, | 163 int DiskDumper::WriteEntry(disk_cache::Entry* entry, |
| 157 net::IOBuffer* buf, int buf_len, | 164 int index, |
| 165 int offset, |
| 166 net::IOBuffer* buf, |
| 167 int buf_len, |
| 158 const net::CompletionCallback& callback) { | 168 const net::CompletionCallback& callback) { |
| 159 if (!entry_) | 169 if (!entry_) |
| 160 return 0; | 170 return 0; |
| 161 | 171 |
| 162 std::string headers; | 172 std::string headers; |
| 163 const char *data; | 173 const char* data; |
| 164 size_t len; | 174 size_t len; |
| 165 if (index == 0) { // Stream 0 is the headers. | 175 if (index == 0) { // Stream 0 is the headers. |
| 166 net::HttpResponseInfo response_info; | 176 net::HttpResponseInfo response_info; |
| 167 bool truncated; | 177 bool truncated; |
| 168 if (!net::HttpCache::ParseResponseInfo(buf->data(), buf_len, | 178 if (!net::HttpCache::ParseResponseInfo( |
| 169 &response_info, &truncated)) | 179 buf->data(), buf_len, &response_info, &truncated)) |
| 170 return 0; | 180 return 0; |
| 171 | 181 |
| 172 // Skip this entry if it was truncated (results in an empty file). | 182 // Skip this entry if it was truncated (results in an empty file). |
| 173 if (truncated) | 183 if (truncated) |
| 174 return buf_len; | 184 return buf_len; |
| 175 | 185 |
| 176 // Remove the size headers. | 186 // Remove the size headers. |
| 177 response_info.headers->RemoveHeader("transfer-encoding"); | 187 response_info.headers->RemoveHeader("transfer-encoding"); |
| 178 response_info.headers->RemoveHeader("content-length"); | 188 response_info.headers->RemoveHeader("content-length"); |
| 179 response_info.headers->RemoveHeader("x-original-url"); | 189 response_info.headers->RemoveHeader("x-original-url"); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 206 DWORD bytes; | 216 DWORD bytes; |
| 207 if (!WriteFile(entry_, data, len, &bytes, 0)) | 217 if (!WriteFile(entry_, data, len, &bytes, 0)) |
| 208 return 0; | 218 return 0; |
| 209 | 219 |
| 210 return bytes; | 220 return bytes; |
| 211 #else | 221 #else |
| 212 return fwrite(data, 1, len, entry_); | 222 return fwrite(data, 1, len, entry_); |
| 213 #endif | 223 #endif |
| 214 } | 224 } |
| 215 | 225 |
| 216 void DiskDumper::CloseEntry(disk_cache::Entry* entry, base::Time last_used, | 226 void DiskDumper::CloseEntry(disk_cache::Entry* entry, |
| 217 base::Time last_modified) { | 227 base::Time last_used, |
| 228 base::Time last_modified) { |
| 218 #ifdef WIN32_LARGE_FILENAME_SUPPORT | 229 #ifdef WIN32_LARGE_FILENAME_SUPPORT |
| 219 CloseHandle(entry_); | 230 CloseHandle(entry_); |
| 220 #else | 231 #else |
| 221 base::CloseFile(entry_); | 232 base::CloseFile(entry_); |
| 222 #endif | 233 #endif |
| 223 } | 234 } |
| OLD | NEW |