Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(605)

Side by Side Diff: net/tools/dump_cache/cache_dumper.cc

Issue 11299239: Adding a simple class for dumping the contents of a cache. Use this new class in dump_cache. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/tools/dump_cache/cache_dumper.h ('k') | net/tools/dump_cache/dump_cache.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/utf_string_conversions.h" 7 #include "base/utf_string_conversions.h"
8 #include "net/base/io_buffer.h" 8 #include "net/base/io_buffer.h"
9 #include "net/base/net_errors.h" 9 #include "net/base/net_errors.h"
10 #include "net/disk_cache/entry_impl.h" 10 #include "net/disk_cache/entry_impl.h"
11 #include "net/http/http_cache.h" 11 #include "net/http/http_cache.h"
12 #include "net/http/http_response_headers.h" 12 #include "net/http/http_response_headers.h"
13 #include "net/http/http_response_info.h" 13 #include "net/http/http_response_info.h"
14 #include "net/tools/dump_cache/url_to_filename_encoder.h" 14 #include "net/tools/dump_cache/url_to_filename_encoder.h"
15 15
16 CacheDumper::CacheDumper(disk_cache::Backend* cache)
17 : cache_(cache) {
18 }
19
16 int CacheDumper::CreateEntry(const std::string& key, 20 int CacheDumper::CreateEntry(const std::string& key,
17 disk_cache::Entry** entry, 21 disk_cache::Entry** entry,
18 const net::CompletionCallback& callback) { 22 const net::CompletionCallback& callback) {
19 return cache_->CreateEntry(key, entry, callback); 23 return cache_->CreateEntry(key, entry, callback);
20 } 24 }
21 25
22 int CacheDumper::WriteEntry(disk_cache::Entry* entry, int index, int offset, 26 int CacheDumper::WriteEntry(disk_cache::Entry* entry, int index, int offset,
23 net::IOBuffer* buf, int buf_len, 27 net::IOBuffer* buf, int buf_len,
24 const net::CompletionCallback& callback) { 28 const net::CompletionCallback& callback) {
25 return entry->WriteData(index, offset, buf, buf_len, callback, false); 29 return entry->WriteData(index, offset, buf, buf_len, callback, false);
(...skipping 19 matching lines...) Expand all
45 std::wstring backslash(L"\\"); 49 std::wstring backslash(L"\\");
46 50
47 // If the path starts with the long file header, skip over that 51 // If the path starts with the long file header, skip over that
48 const std::wstring kLargeFilenamePrefix(L"\\\\?\\"); 52 const std::wstring kLargeFilenamePrefix(L"\\\\?\\");
49 std::wstring header(kLargeFilenamePrefix); 53 std::wstring header(kLargeFilenamePrefix);
50 if (path.value().find(header) == 0) 54 if (path.value().find(header) == 0)
51 pos = 4; 55 pos = 4;
52 56
53 // Create the subdirectories individually 57 // Create the subdirectories individually
54 while ((pos = path.value().find(backslash, pos)) != std::wstring::npos) { 58 while ((pos = path.value().find(backslash, pos)) != std::wstring::npos) {
55 std::wstring subdir = path.value().substr(0, pos); 59 FilePath::StringType subdir = path.value().substr(0, pos);
56 CreateDirectoryW(subdir.c_str(), NULL); 60 CreateDirectoryW(subdir.c_str(), NULL);
57 // we keep going even if directory creation failed. 61 // we keep going even if directory creation failed.
58 pos++; 62 pos++;
59 } 63 }
60 // Now create the full path 64 // Now create the full path
61 return CreateDirectoryW(path.value().c_str(), NULL) == TRUE; 65 return CreateDirectoryW(path.value().c_str(), NULL) == TRUE;
62 #else 66 #else
63 return file_util::CreateDirectory(path); 67 return file_util::CreateDirectory(path);
64 #endif 68 #endif
65 } 69 }
66 70
71 DiskDumper::DiskDumper(const FilePath& path)
72 : path_(path), entry_(NULL) {
73 file_util::CreateDirectory(path);
74 }
75
67 int DiskDumper::CreateEntry(const std::string& key, 76 int DiskDumper::CreateEntry(const std::string& key,
68 disk_cache::Entry** entry, 77 disk_cache::Entry** entry,
69 const net::CompletionCallback& callback) { 78 const net::CompletionCallback& callback) {
70 // The URL may not start with a valid protocol; search for it. 79 // The URL may not start with a valid protocol; search for it.
71 int urlpos = key.find("http"); 80 int urlpos = key.find("http");
72 std::string url = urlpos > 0 ? key.substr(urlpos) : key; 81 std::string url = urlpos > 0 ? key.substr(urlpos) : key;
73 std::string base_path = WideToASCII(path_.value()); 82 std::string base_path = path_.MaybeAsASCII();
74 std::string new_path = 83 std::string new_path =
75 net::UrlToFilenameEncoder::Encode(url, base_path, false); 84 net::UrlToFilenameEncoder::Encode(url, base_path, false);
76 entry_path_ = FilePath(ASCIIToWide(new_path)); 85 entry_path_ = FilePath::FromUTF8Unsafe(new_path);
77 86
78 #ifdef WIN32_LARGE_FILENAME_SUPPORT 87 #ifdef WIN32_LARGE_FILENAME_SUPPORT
79 // In order for long filenames to work, we'll need to prepend 88 // In order for long filenames to work, we'll need to prepend
80 // the windows magic token. 89 // the windows magic token.
81 const std::wstring kLongFilenamePrefix(L"\\\\?\\"); 90 const std::wstring kLongFilenamePrefix(L"\\\\?\\");
82 // There is no way to prepend to a filename. We simply *have* 91 // There is no way to prepend to a filename. We simply *have*
83 // to convert to a wstring to do this. 92 // to convert to a wstring to do this.
84 std::wstring name = kLongFilenamePrefix; 93 std::wstring name = kLongFilenamePrefix;
85 name.append(entry_path_.value()); 94 name.append(entry_path_.value());
86 entry_path_ = FilePath(name); 95 entry_path_ = FilePath(name);
87 #endif 96 #endif
88 97
89 entry_url_ = key; 98 entry_url_ = key;
90 99
91 SafeCreateDirectory(entry_path_.DirName()); 100 SafeCreateDirectory(entry_path_.DirName());
92 101
93 std::wstring file = entry_path_.value(); 102 FilePath::StringType file = entry_path_.value();
94 #ifdef WIN32_LARGE_FILENAME_SUPPORT 103 #ifdef WIN32_LARGE_FILENAME_SUPPORT
95 entry_ = CreateFileW(file.c_str(), GENERIC_WRITE|GENERIC_READ, 0, 0, 104 entry_ = CreateFileW(file.c_str(), GENERIC_WRITE|GENERIC_READ, 0, 0,
96 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); 105 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
97 if (entry_ == INVALID_HANDLE_VALUE) 106 if (entry_ == INVALID_HANDLE_VALUE)
98 wprintf(L"CreateFileW (%s) failed: %d\n", file.c_str(), GetLastError()); 107 wprintf(L"CreateFileW (%s) failed: %d\n", file.c_str(), GetLastError());
99 return (entry_ != INVALID_HANDLE_VALUE) ? net::OK : net::ERR_FAILED; 108 return (entry_ != INVALID_HANDLE_VALUE) ? net::OK : net::ERR_FAILED;
100 #else 109 #else
101 entry_ = file_util::OpenFile(entry_path_, "w+"); 110 entry_ = file_util::OpenFile(entry_path_, "w+");
102 return (entry_ != NULL) ? net::OK : net::ERR_FAILED; 111 return (entry_ != NULL) ? net::OK : net::ERR_FAILED;
103 #endif 112 #endif
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 if ((pos = url.find("http")) != 0) { 187 if ((pos = url.find("http")) != 0) {
179 if (pos != std::string::npos) 188 if (pos != std::string::npos)
180 url = url.substr(pos); 189 url = url.substr(pos);
181 } 190 }
182 std::string x_original_url = "X-Original-Url: " + url + "\r\n"; 191 std::string x_original_url = "X-Original-Url: " + url + "\r\n";
183 // we know that the last two bytes are CRLF. 192 // we know that the last two bytes are CRLF.
184 headers.replace(headers.length() - 2, 0, x_original_url); 193 headers.replace(headers.length() - 2, 0, x_original_url);
185 194
186 data = headers.c_str(); 195 data = headers.c_str();
187 len = headers.size(); 196 len = headers.size();
188 } else if (index == 1) { // Stream 1 is the data. 197 } else if (index == 1) {
189 data = buf->data(); 198 data = buf->data();
190 len = buf_len; 199 len = buf_len;
200 } else {
201 return 0;
191 } 202 }
203
192 #ifdef WIN32_LARGE_FILENAME_SUPPORT 204 #ifdef WIN32_LARGE_FILENAME_SUPPORT
193 DWORD bytes; 205 DWORD bytes;
194 if (!WriteFile(entry_, data, len, &bytes, 0)) 206 if (!WriteFile(entry_, data, len, &bytes, 0))
195 return 0; 207 return 0;
196 208
197 return bytes; 209 return bytes;
198 #else 210 #else
199 return fwrite(data, 1, len, entry_); 211 return fwrite(data, 1, len, entry_);
200 #endif 212 #endif
201 } 213 }
202 214
203 void DiskDumper::CloseEntry(disk_cache::Entry* entry, base::Time last_used, 215 void DiskDumper::CloseEntry(disk_cache::Entry* entry, base::Time last_used,
204 base::Time last_modified) { 216 base::Time last_modified) {
205 #ifdef WIN32_LARGE_FILENAME_SUPPORT 217 #ifdef WIN32_LARGE_FILENAME_SUPPORT
206 CloseHandle(entry_); 218 CloseHandle(entry_);
207 #else 219 #else
208 file_util::CloseFile(entry_); 220 file_util::CloseFile(entry_);
209 #endif 221 #endif
210 } 222 }
OLDNEW
« no previous file with comments | « net/tools/dump_cache/cache_dumper.h ('k') | net/tools/dump_cache/dump_cache.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698