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

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: 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 "net/base/io_buffer.h"
14 #include "net/base/test_completion_callback.h"
15 #include "net/disk_cache/disk_cache.h"
16 #include "net/disk_cache/simple/simple_backend_impl.h"
17 #include "net/http/http_cache.h"
18 #include "net/http/http_response_headers.h"
19 #include "net/http/http_util.h"
20
21 using namespace disk_cache;
gavinp 2016/02/19 16:21:11 See https://google.github.io/styleguide/cppguide.h
gabadie 2016/02/22 14:35:46 Done.
22
23 namespace {
24
25 base::MessageLoopForIO* g_message_loop = nullptr;
26
27 // Create a simple typed cache back-end.
gavinp 2016/02/19 16:21:11 grammar: "Create a SimpleCache backend." But at t
gabadie 2016/02/22 14:35:45 Done.
28 Backend* CreateSimpleBackend(const base::FilePath& cache_path) {
gavinp 2016/02/19 16:21:11 This should return scoped_ptr<Backend> or scoped_p
gabadie 2016/02/22 14:35:46 Egor not here yet. We can universalize this tool l
29 scoped_ptr<SimpleBackendImpl> cache_backend(new SimpleBackendImpl(
30 cache_path, 0, net::DISK_CACHE, g_message_loop->task_runner(), NULL));
31
32 net::TestCompletionCallback cb;
33 int rv = cache_backend->Init(cb.callback());
34 DCHECK_EQ(net::OK, cb.GetResult(rv));
gavinp 2016/02/19 16:21:11 This somewhat defeats the purpose of your later ch
gabadie 2016/02/22 14:35:46 Oups... Done.
35
36 return cache_backend.release();
37 }
38
39 // Prints all cache's keys to the stdout.
gavinp 2016/02/19 16:21:12 "Print call of a cache's keys to stdout." Explana
gabadie 2016/02/22 14:35:46 Ahah ^^ Thanks for that outstanding example! =) Do
40 int ListKeys(Backend* cache_backend) {
gavinp 2016/02/19 16:21:11 I think "bool" is a better return code for ListKey
gabadie 2016/02/22 14:35:45 Done.
41 scoped_ptr<Backend::Iterator> entry_iterator =
42 cache_backend->CreateIterator();
43 Entry* entry = nullptr;
44 net::TestCompletionCallback cb;
45 int rv = entry_iterator->OpenNextEntry(&entry, cb.callback());
46 while (cb.GetResult(rv) == net::OK) {
47 std::string url = entry->GetKey();
48 std::cout << url << std::endl;
49 entry->Close();
50 entry = nullptr;
51 rv = entry_iterator->OpenNextEntry(&entry, cb.callback());
52 }
53 return 0;
54 }
55
56 // Prints a key's stream to the stdout.
gavinp 2016/02/19 16:21:11 "Print a key's stream to stdout."
gabadie 2016/02/22 14:35:46 Done.
57 int GetKeyStream(Backend* cache_backend, const std::string& key, int index) {
gavinp 2016/02/19 16:21:11 I'm not a huge fan of this API; the magic index va
gabadie 2016/02/22 14:35:45 I totally agree this issue makes the code not pret
gavinp 2016/02/22 15:44:56 Fair point. And you're placing the blame where it
58 if (index < 0 || index > 2) {
gavinp 2016/02/19 16:21:11 "2" is really problematic here. Why is that the ma
gabadie 2016/02/22 14:35:46 Stuffing with comment.
59 std::cerr << "Invalid stream index." << std::endl;
60 return 1;
61 }
62
63 Entry* cache_entry;
64 net::TestCompletionCallback cb;
65 int rv = cache_backend->OpenEntry(key, &cache_entry, cb.callback());
66 if (cb.GetResult(rv) != net::OK) {
67 std::cerr << "Couldn't find key." << std::endl;
gavinp 2016/02/19 16:21:12 Isn't it the entry that we couldn't find?
gabadie 2016/02/22 14:35:46 Done.
68 return 1;
69 }
70
71 const int kInitBufferSize = 8192;
72 scoped_refptr<net::GrowableIOBuffer> buffer(new net::GrowableIOBuffer());
73 buffer->SetCapacity(kInitBufferSize);
74 for (;;) {
75 rv = cache_entry->ReadData(index, buffer->offset(), buffer.get(),
76 buffer->capacity() - buffer->offset(),
77 cb.callback());
78 rv = cb.GetResult(rv);
79 if (rv < 0) {
80 cache_entry->Close();
81 std::cerr << "Stream read error." << std::endl;
82 return 1;
83 }
84 buffer->set_offset(buffer->offset() + rv);
85 if (rv == 0) {
86 break;
87 }
88 buffer->SetCapacity(buffer->offset() * 2);
89 }
90 cache_entry->Close();
91 if (index == 0) {
92 net::HttpResponseInfo response_info;
93 bool truncated_response_info = false;
94 net::HttpCache::ParseResponseInfo(buffer->StartOfBuffer(), buffer->offset(),
95 &response_info, &truncated_response_info);
96 if (truncated_response_info) {
97 std::cerr << "Truncated HTTP response." << std::endl;
98 return 1;
99 }
100 std::cout << net::HttpUtil::ConvertHeadersBackToHTTPResponse(
101 response_info.headers->raw_headers());
102 } else {
103 fwrite(buffer->StartOfBuffer(), 1, buffer->offset(), stdout);
gavinp 2016/02/19 16:21:11 Intermixing fwrite and std::cout is dangerous. I b
gabadie 2016/02/22 14:35:46 My bad, have always been used to use C entry point
104 }
105 return 0;
106 }
107
108 // Delete a specified key from the cache.
109 int DeleteKey(Backend* cache_backend, const std::string& key) {
110 net::TestCompletionCallback cb;
111 int rv = cache_backend->DoomEntry(key, cb.callback());
112 if (cb.GetResult(rv) != net::OK) {
113 std::cerr << "Couldn't delete key." << std::endl;
114 return 1;
115 }
116 return 0;
117 }
118
119 // Prints the command line's help.
gavinp 2016/02/19 16:21:11 "Print the command line help."
gabadie 2016/02/22 14:35:46 Done.
120 int PrintHelp() {
gavinp 2016/02/19 16:21:11 This shouldn't return a value.
gabadie 2016/02/22 14:35:46 It returns 1 to have handy oneliners return PrintH
gavinp 2016/02/22 15:44:56 That's too tricky, too ugly. I wouldn't approve "
121 std::cout << "cachetool <cache_path> <cache_backend_type> <subcommand> ..."
gavinp 2016/02/19 16:21:11 Document available cache backend types.
gabadie 2016/02/22 14:35:46 Done.
122 << std::endl
123 << std::endl;
124 std::cout << "Available subcommands:" << std::endl;
125 std::cout << " validate: Open the cache and return." << std::endl;
gavinp 2016/02/19 16:21:11 "Open a cache and return, confirming the cache exi
gabadie 2016/02/22 14:35:46 Done.
126 std::cout << " list_keys: List all cache's keys." << std::endl;
gavinp 2016/02/19 16:21:11 "List all keys in a cache."
gabadie 2016/02/22 14:35:46 Done.
127 std::cout << " get_stream <key> <index>: Get key's stream." << std::endl;
gavinp 2016/02/19 16:21:11 "Print a particular stream for a given key" Also,
gabadie 2016/02/22 14:35:46 Done.
128 std::cout << " delete_key <key>: Delete key from cache." << std::endl;
129 return 1;
130 }
131
132 } // namespace
133
134 int main(int argc, char* argv[]) {
135 base::AtExitManager at_exit_manager;
136 base::MessageLoopForIO message_loop;
137 base::CommandLine::Init(argc, argv);
138 const base::CommandLine& command_line =
139 *base::CommandLine::ForCurrentProcess();
140
141 base::CommandLine::StringVector args = command_line.GetArgs();
142 if (args.size() < 3U) {
143 return PrintHelp();
144 }
145
146 base::FilePath cache_path(args[0]);
147 std::string cache_backend_type(args[1]);
148 std::string subcommand(args[2]);
149
150 g_message_loop = &message_loop;
151 scoped_ptr<Backend> cache_backend;
152 if (cache_backend_type == "simple") {
153 cache_backend.reset(CreateSimpleBackend(cache_path));
154 } else {
155 std::cerr << "Unknown cache type." << std::endl;
156 return PrintHelp();
157 }
158
159 if (!cache_backend) {
160 std::cerr << "Invalid cache." << std::endl;
161 return PrintHelp();
162 }
163
164 int return_code = 1;
165 if (subcommand == "validate") {
gavinp 2016/02/19 16:21:11 Why don't we check that there's only exactly three
gabadie 2016/02/22 14:35:45 Oups... Done.
166 return_code = 0;
167 } else if (subcommand == "list_keys") {
168 return_code = ListKeys(cache_backend.get());
169 } else if (subcommand == "get_stream") {
170 if (args.size() != 5) {
171 return PrintHelp();
172 }
173 std::string key(args[3]);
174 int index = std::stoi(args[4]);
gavinp 2016/02/19 16:21:11 Earlier I mentioned that I'd like these to be symb
gabadie 2016/02/22 14:35:46 Ah sweet, I was looking into a better function in
175 return_code = GetKeyStream(cache_backend.get(), key, index);
176 } else if (subcommand == "delete_key") {
177 if (args.size() != 4) {
178 return PrintHelp();
179 }
180 std::string key(args[3]);
181 return_code = DeleteKey(cache_backend.get(), key);
182 } else {
183 std::cerr << "Unknown subcommand." << std::endl;
184 PrintHelp();
185 }
186 return return_code;
187 }
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