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

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

Issue 1710923003: Implements cachetool to manipulate simple typed cache. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@i02
Patch Set: Addresses Gavin's comment 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
« net/net.gyp ('K') | « 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 "net/base/io_buffer.h"
15 #include "net/base/test_completion_callback.h"
16 #include "net/disk_cache/disk_cache.h"
17 #include "net/disk_cache/simple/simple_backend_impl.h"
18 #include "net/http/http_cache.h"
19 #include "net/http/http_response_headers.h"
20 #include "net/http/http_util.h"
21
22 using disk_cache::Backend;
23 using disk_cache::Entry;
24 using disk_cache::SimpleBackendImpl;
25
26 namespace {
27
28 base::MessageLoopForIO* g_message_loop = nullptr;
29
30 scoped_ptr<SimpleBackendImpl> CreateSimpleBackend(
31 const base::FilePath& cache_path) {
32 scoped_ptr<SimpleBackendImpl> cache_backend(new SimpleBackendImpl(
33 cache_path, 0, net::DISK_CACHE, g_message_loop->task_runner(), NULL));
34
35 net::TestCompletionCallback cb;
36 int rv = cache_backend->Init(cb.callback());
37 if (cb.GetResult(rv) != net::OK)
38 return nullptr;
39 return cache_backend;
40 }
41
42 // Print call of a cache's keys to stdout.
gavinp 2016/02/22 15:44:57 Typo: call --> all
gabadie 2016/02/22 17:10:43 Done.
43 bool ListKeys(Backend* cache_backend) {
44 scoped_ptr<Backend::Iterator> entry_iterator =
45 cache_backend->CreateIterator();
46 Entry* entry = nullptr;
47 net::TestCompletionCallback cb;
48 int rv = entry_iterator->OpenNextEntry(&entry, cb.callback());
49 while (cb.GetResult(rv) == net::OK) {
50 std::string url = entry->GetKey();
51 std::cout << url << std::endl;
52 entry->Close();
53 entry = nullptr;
54 rv = entry_iterator->OpenNextEntry(&entry, cb.callback());
55 }
56 return true;
57 }
58
59 // Print a key's stream to stdout.
60 bool GetKeyStream(Backend* cache_backend, const std::string& key, int index) {
61 // Expected values of index are:
62 // 0 (HTTP response header)
63 // 1 (transport encoded content)
64 // 2 (compiled content)
65 if (index < 0 || index > 2) {
66 std::cerr << "Invalid stream index." << std::endl;
67 return false;
68 }
69
70 Entry* cache_entry;
71 net::TestCompletionCallback cb;
72 int rv = cache_backend->OpenEntry(key, &cache_entry, cb.callback());
73 if (cb.GetResult(rv) != net::OK) {
74 std::cerr << "Couldn't find key's entry." << std::endl;
75 return false;
76 }
77
78 const int kInitBufferSize = 8192;
79 scoped_refptr<net::GrowableIOBuffer> buffer(new net::GrowableIOBuffer());
80 buffer->SetCapacity(kInitBufferSize);
81 for (;;) {
82 rv = cache_entry->ReadData(index, buffer->offset(), buffer.get(),
83 buffer->capacity() - buffer->offset(),
84 cb.callback());
85 rv = cb.GetResult(rv);
86 if (rv < 0) {
87 cache_entry->Close();
88 std::cerr << "Stream read error." << std::endl;
89 return false;
90 }
91 buffer->set_offset(buffer->offset() + rv);
92 if (rv == 0) {
93 break;
94 }
95 buffer->SetCapacity(buffer->offset() * 2);
96 }
97 cache_entry->Close();
98 if (index == 0) {
99 net::HttpResponseInfo response_info;
100 bool truncated_response_info = false;
101 net::HttpCache::ParseResponseInfo(buffer->StartOfBuffer(), buffer->offset(),
102 &response_info, &truncated_response_info);
103 if (truncated_response_info) {
104 std::cerr << "Truncated HTTP response." << std::endl;
105 return false;
106 }
107 std::cout << net::HttpUtil::ConvertHeadersBackToHTTPResponse(
108 response_info.headers->raw_headers());
109 } else {
110 std::cout.write(buffer->StartOfBuffer(), buffer->offset());
111 }
112 return true;
113 }
114
115 // Delete a specified key from the cache.
116 bool DeleteKey(Backend* cache_backend, const std::string& key) {
117 net::TestCompletionCallback cb;
118 int rv = cache_backend->DoomEntry(key, cb.callback());
119 if (cb.GetResult(rv) != net::OK) {
120 std::cerr << "Couldn't delete key." << std::endl;
121 return false;
122 }
123 return true;
124 }
125
126 // Print the command line help.
127 int PrintHelp() {
128 std::cout << "cachetool <cache_path> <cache_backend_type> <subcommand> ..."
129 << std::endl
130 << std::endl;
131 std::cout << "Available cache back-end types: simple";
132 std::cout << "Available subcommands:" << std::endl;
133 std::cout << " validate: Verify that the cache can be opened and return, "
134 << "confirming the cache exists and is of the right type."
135 << std::endl;
136 std::cout << " list_keys: List all keys in cache." << std::endl;
137 std::cout << " get_stream <key> <index>: Print a particular stream for a"
gavinp 2016/02/22 15:44:57 How about we document the various crazy values of
gabadie 2016/02/22 17:10:43 Done.
138 << " given key." << std::endl;
139 std::cout << " delete_key <key>: Delete key from cache." << std::endl;
140 return 1;
141 }
142
143 } // namespace
144
145 int main(int argc, char* argv[]) {
146 base::AtExitManager at_exit_manager;
147 base::MessageLoopForIO message_loop;
148 base::CommandLine::Init(argc, argv);
149 const base::CommandLine& command_line =
150 *base::CommandLine::ForCurrentProcess();
151
152 base::CommandLine::StringVector args = command_line.GetArgs();
153 if (args.size() < 3U) {
154 return PrintHelp();
155 }
156
157 base::FilePath cache_path(args[0]);
158 std::string cache_backend_type(args[1]);
159 std::string subcommand(args[2]);
160
161 g_message_loop = &message_loop;
162 scoped_ptr<Backend> cache_backend;
163 if (cache_backend_type == "simple") {
164 cache_backend = CreateSimpleBackend(cache_path);
165 } else {
166 std::cerr << "Unknown cache type." << std::endl;
167 return PrintHelp();
168 }
169
170 if (!cache_backend) {
171 std::cerr << "Invalid cache." << std::endl;
172 return PrintHelp();
173 }
174
175 bool successful_command = false;
176 if (subcommand == "validate") {
177 if (args.size() != 3) {
gavinp 2016/02/22 15:44:57 Don't need braces on a single line if. There's a b
gabadie 2016/02/22 17:10:43 Done.
178 return PrintHelp();
179 }
180 successful_command = true;
181 } else if (subcommand == "list_keys") {
182 if (args.size() != 3) {
183 return PrintHelp();
184 }
185 successful_command = ListKeys(cache_backend.get());
186 } else if (subcommand == "get_stream") {
187 if (args.size() != 5) {
188 return PrintHelp();
189 }
190 std::string key(args[3]);
191 int index = 0;
192 if (!base::StringToInt(args[4], &index)) {
193 std::cerr << "<index> must be an integer." << std::endl;
194 return PrintHelp();
195 }
196 successful_command = GetKeyStream(cache_backend.get(), key, index);
197 } else if (subcommand == "delete_key") {
198 if (args.size() != 4) {
199 return PrintHelp();
200 }
201 std::string key(args[3]);
202 successful_command = DeleteKey(cache_backend.get(), key);
203 } else {
204 std::cerr << "Unknown subcommand." << std::endl;
205 PrintHelp();
206 }
207 return successful_command ? 0 : 1;
208 }
OLDNEW
« net/net.gyp ('K') | « net/net.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698