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

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

Issue 1728653002: Revert 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 "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 all of a cache's keys to stdout.
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 if (index < 0 || index > 2) {
62 std::cerr << "Invalid stream index." << std::endl;
63 return false;
64 }
65
66 Entry* cache_entry;
67 net::TestCompletionCallback cb;
68 int rv = cache_backend->OpenEntry(key, &cache_entry, cb.callback());
69 if (cb.GetResult(rv) != net::OK) {
70 std::cerr << "Couldn't find key's entry." << std::endl;
71 return false;
72 }
73
74 const int kInitBufferSize = 8192;
75 scoped_refptr<net::GrowableIOBuffer> buffer(new net::GrowableIOBuffer());
76 buffer->SetCapacity(kInitBufferSize);
77 while (true) {
78 rv = cache_entry->ReadData(index, buffer->offset(), buffer.get(),
79 buffer->capacity() - buffer->offset(),
80 cb.callback());
81 rv = cb.GetResult(rv);
82 if (rv < 0) {
83 cache_entry->Close();
84 std::cerr << "Stream read error." << std::endl;
85 return false;
86 }
87 buffer->set_offset(buffer->offset() + rv);
88 if (rv == 0)
89 break;
90 buffer->SetCapacity(buffer->offset() * 2);
91 }
92 cache_entry->Close();
93 if (index == 0) {
94 net::HttpResponseInfo response_info;
95 bool truncated_response_info = false;
96 net::HttpCache::ParseResponseInfo(buffer->StartOfBuffer(), buffer->offset(),
97 &response_info, &truncated_response_info);
98 if (truncated_response_info) {
99 std::cerr << "Truncated HTTP response." << std::endl;
100 return false;
101 }
102 std::cout << net::HttpUtil::ConvertHeadersBackToHTTPResponse(
103 response_info.headers->raw_headers());
104 } else {
105 std::cout.write(buffer->StartOfBuffer(), buffer->offset());
106 }
107 return true;
108 }
109
110 // Delete a specified key from the cache.
111 bool DeleteKey(Backend* cache_backend, const std::string& key) {
112 net::TestCompletionCallback cb;
113 int rv = cache_backend->DoomEntry(key, cb.callback());
114 if (cb.GetResult(rv) != net::OK) {
115 std::cerr << "Couldn't delete key." << std::endl;
116 return false;
117 }
118 return true;
119 }
120
121 // Print the command line help.
122 void PrintHelp() {
123 std::cout << "cachetool <cache_path> <cache_backend_type> <subcommand> ..."
124 << std::endl
125 << std::endl;
126 std::cout << "Available cache back-end types: simple";
127 std::cout << "Available subcommands:" << std::endl;
128 std::cout << " validate: Verify that the cache can be opened and return, "
129 << "confirming the cache exists and is of the right type."
130 << std::endl;
131 std::cout << " list_keys: List all keys in the cache." << std::endl;
132 std::cout << " get_stream <key> <index>: Print a particular stream for a"
133 << " given key." << std::endl;
134 std::cout << " delete_key <key>: Delete key from cache." << std::endl;
135 std::cout << "Expected values of <index> are:" << std::endl;
136 std::cout << " 0 (HTTP response headers)" << std::endl;
137 std::cout << " 1 (transport encoded content)" << std::endl;
138 std::cout << " 2 (compiled content)" << std::endl;
139 }
140
141 } // namespace
142
143 int main(int argc, char* argv[]) {
144 base::AtExitManager at_exit_manager;
145 base::MessageLoopForIO message_loop;
146 base::CommandLine::Init(argc, argv);
147 const base::CommandLine& command_line =
148 *base::CommandLine::ForCurrentProcess();
149
150 base::CommandLine::StringVector args = command_line.GetArgs();
151 if (args.size() < 3U) {
152 PrintHelp();
153 return 1;
154 }
155
156 base::FilePath cache_path(args[0]);
157 std::string cache_backend_type(args[1]);
158 std::string subcommand(args[2]);
159
160 g_message_loop = &message_loop;
161 scoped_ptr<Backend> cache_backend;
162 if (cache_backend_type == "simple") {
163 cache_backend = CreateSimpleBackend(cache_path);
164 } else {
165 std::cerr << "Unknown cache type." << std::endl;
166 PrintHelp();
167 return 1;
168 }
169
170 if (!cache_backend) {
171 std::cerr << "Invalid cache." << std::endl;
172 PrintHelp();
173 return 1;
174 }
175
176 bool successful_command = false;
177 if (subcommand == "validate") {
178 if (args.size() != 3) {
179 PrintHelp();
180 return 1;
181 }
182 successful_command = true;
183 } else if (subcommand == "list_keys") {
184 if (args.size() != 3) {
185 PrintHelp();
186 return 1;
187 }
188 successful_command = ListKeys(cache_backend.get());
189 } else if (subcommand == "get_stream") {
190 if (args.size() != 5) {
191 PrintHelp();
192 return 1;
193 }
194 std::string key(args[3]);
195 int index = 0;
196 if (!base::StringToInt(args[4], &index)) {
197 std::cerr << "<index> must be an integer." << std::endl;
198 PrintHelp();
199 return 1;
200 }
201 successful_command = GetKeyStream(cache_backend.get(), key, index);
202 } else if (subcommand == "delete_key") {
203 if (args.size() != 4) {
204 PrintHelp();
205 return 1;
206 }
207 std::string key(args[3]);
208 successful_command = DeleteKey(cache_backend.get(), key);
209 } else {
210 std::cerr << "Unknown subcommand." << std::endl;
211 PrintHelp();
212 }
213 return !successful_command;
214 }
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