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

Side by Side Diff: net/tools/cachetool/cachetool.cc

Issue 1730373002: Reland II of "Implements cachetool to manipulate simple typed cache." (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@i02
Patch Set: Created 4 years, 10 months 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
« no previous file with comments | « net/net.gyp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <iostream>
6
7 #include "base/at_exit.h"
8 #include "base/command_line.h"
9 #include "base/files/file_path.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "net/base/io_buffer.h"
16 #include "net/base/test_completion_callback.h"
17 #include "net/disk_cache/disk_cache.h"
18 #include "net/disk_cache/simple/simple_backend_impl.h"
19 #include "net/http/http_cache.h"
20 #include "net/http/http_response_headers.h"
21 #include "net/http/http_util.h"
22
23 using disk_cache::Backend;
24 using disk_cache::Entry;
25 using disk_cache::SimpleBackendImpl;
26
27 namespace {
28
29 base::MessageLoopForIO* g_message_loop = nullptr;
30
31 scoped_ptr<SimpleBackendImpl> CreateSimpleBackend(
32 const base::FilePath& cache_path) {
33 scoped_ptr<SimpleBackendImpl> cache_backend(new SimpleBackendImpl(
34 cache_path, 0, net::DISK_CACHE, g_message_loop->task_runner(), NULL));
35
36 net::TestCompletionCallback cb;
37 int rv = cache_backend->Init(cb.callback());
38 if (cb.GetResult(rv) != net::OK)
39 return nullptr;
40 return cache_backend;
41 }
42
43 // Print all of a cache's keys to stdout.
44 bool ListKeys(Backend* cache_backend) {
45 scoped_ptr<Backend::Iterator> entry_iterator =
46 cache_backend->CreateIterator();
47 Entry* entry = nullptr;
48 net::TestCompletionCallback cb;
49 int rv = entry_iterator->OpenNextEntry(&entry, cb.callback());
50 while (cb.GetResult(rv) == net::OK) {
51 std::string url = entry->GetKey();
52 std::cout << url << std::endl;
53 entry->Close();
54 entry = nullptr;
55 rv = entry_iterator->OpenNextEntry(&entry, cb.callback());
56 }
57 return true;
58 }
59
60 // Print a key's stream to stdout.
61 bool GetKeyStream(Backend* cache_backend, const std::string& key, int index) {
62 if (index < 0 || index > 2) {
63 std::cerr << "Invalid stream index." << std::endl;
64 return false;
65 }
66
67 Entry* cache_entry;
68 net::TestCompletionCallback cb;
69 int rv = cache_backend->OpenEntry(key, &cache_entry, cb.callback());
70 if (cb.GetResult(rv) != net::OK) {
71 std::cerr << "Couldn't find key's entry." << std::endl;
72 return false;
73 }
74
75 const int kInitBufferSize = 8192;
76 scoped_refptr<net::GrowableIOBuffer> buffer(new net::GrowableIOBuffer());
77 buffer->SetCapacity(kInitBufferSize);
78 while (true) {
79 rv = cache_entry->ReadData(index, buffer->offset(), buffer.get(),
80 buffer->capacity() - buffer->offset(),
81 cb.callback());
82 rv = cb.GetResult(rv);
83 if (rv < 0) {
84 cache_entry->Close();
85 std::cerr << "Stream read error." << std::endl;
86 return false;
87 }
88 buffer->set_offset(buffer->offset() + rv);
89 if (rv == 0)
90 break;
91 buffer->SetCapacity(buffer->offset() * 2);
92 }
93 cache_entry->Close();
94 if (index == 0) {
95 net::HttpResponseInfo response_info;
96 bool truncated_response_info = false;
97 net::HttpCache::ParseResponseInfo(buffer->StartOfBuffer(), buffer->offset(),
98 &response_info, &truncated_response_info);
99 if (truncated_response_info) {
100 std::cerr << "Truncated HTTP response." << std::endl;
101 return false;
102 }
103 std::cout << net::HttpUtil::ConvertHeadersBackToHTTPResponse(
104 response_info.headers->raw_headers());
105 } else {
106 std::cout.write(buffer->StartOfBuffer(), buffer->offset());
107 }
108 return true;
109 }
110
111 // Delete a specified key from the cache.
112 bool DeleteKey(Backend* cache_backend, const std::string& key) {
113 net::TestCompletionCallback cb;
114 int rv = cache_backend->DoomEntry(key, cb.callback());
115 if (cb.GetResult(rv) != net::OK) {
116 std::cerr << "Couldn't delete key." << std::endl;
117 return false;
118 }
119 return true;
120 }
121
122 // Print the command line help.
123 void PrintHelp() {
124 std::cout << "cachetool <cache_path> <cache_backend_type> <subcommand> ..."
125 << std::endl
126 << std::endl;
127 std::cout << "Available cache back-end types: simple";
128 std::cout << "Available subcommands:" << std::endl;
129 std::cout << " validate: Verify that the cache can be opened and return, "
130 << "confirming the cache exists and is of the right type."
131 << std::endl;
132 std::cout << " list_keys: List all keys in the cache." << std::endl;
133 std::cout << " get_stream <key> <index>: Print a particular stream for a"
134 << " given key." << std::endl;
135 std::cout << " delete_key <key>: Delete key from cache." << std::endl;
136 std::cout << "Expected values of <index> are:" << std::endl;
137 std::cout << " 0 (HTTP response headers)" << std::endl;
138 std::cout << " 1 (transport encoded content)" << std::endl;
139 std::cout << " 2 (compiled content)" << std::endl;
140 }
141
142 } // namespace
143
144 int main(int argc, char* argv[]) {
145 base::AtExitManager at_exit_manager;
146 base::MessageLoopForIO message_loop;
147 base::CommandLine::Init(argc, argv);
148 const base::CommandLine& command_line =
149 *base::CommandLine::ForCurrentProcess();
150
151 #if defined(OS_WIN)
152 std::vector<std::string> args;
153 base::CommandLine::StringVector wide_args = command_line.GetArgs();
154 for (const auto& arg : wide_args) {
155 args.push_back(base::WideToUTF8(arg));
156 }
157 #else
158 base::CommandLine::StringVector args = command_line.GetArgs();
159 #endif
160 if (args.size() < 3U) {
161 PrintHelp();
162 return 1;
163 }
164
165 #if defined(OS_WIN)
166 base::FilePath cache_path(wide_args[0]);
167 #else
168 base::FilePath cache_path(args[0]);
169 #endif
170 std::string cache_backend_type(args[1]);
171 std::string subcommand(args[2]);
172
173 g_message_loop = &message_loop;
174 scoped_ptr<Backend> cache_backend;
175 if (cache_backend_type == "simple") {
176 cache_backend = CreateSimpleBackend(cache_path);
177 } else {
178 std::cerr << "Unknown cache type." << std::endl;
179 PrintHelp();
180 return 1;
181 }
182
183 if (!cache_backend) {
184 std::cerr << "Invalid cache." << std::endl;
185 PrintHelp();
186 return 1;
187 }
188
189 bool successful_command = false;
190 if (subcommand == "validate") {
191 if (args.size() != 3) {
192 PrintHelp();
193 return 1;
194 }
195 successful_command = true;
196 } else if (subcommand == "list_keys") {
197 if (args.size() != 3) {
198 PrintHelp();
199 return 1;
200 }
201 successful_command = ListKeys(cache_backend.get());
202 } else if (subcommand == "get_stream") {
203 if (args.size() != 5) {
204 PrintHelp();
205 return 1;
206 }
207 std::string key(args[3]);
208 int index = 0;
209 if (!base::StringToInt(args[4], &index)) {
210 std::cerr << "<index> must be an integer." << std::endl;
211 PrintHelp();
212 return 1;
213 }
214 successful_command = GetKeyStream(cache_backend.get(), key, index);
215 } else if (subcommand == "delete_key") {
216 if (args.size() != 4) {
217 PrintHelp();
218 return 1;
219 }
220 std::string key(args[3]);
221 successful_command = DeleteKey(cache_backend.get(), key);
222 } else {
223 std::cerr << "Unknown subcommand." << std::endl;
224 PrintHelp();
225 }
226 return !successful_command;
227 }
OLDNEW
« no previous file with comments | « net/net.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698