OLD | NEW |
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 // This command-line program dumps the contents of a set of cache files, either | 5 // This command-line program dumps the contents of a set of cache files, either |
6 // to stdout or to another set of cache files. | 6 // to stdout or to another set of cache files. |
7 | 7 |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/at_exit.h" | 11 #include "base/at_exit.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
16 #include "net/disk_cache/blockfile/disk_format.h" | 16 #include "net/disk_cache/blockfile/disk_format.h" |
17 #include "net/tools/dump_cache/dump_files.h" | 17 #include "net/tools/dump_cache/dump_files.h" |
18 #include "net/tools/dump_cache/simple_cache_dumper.h" | 18 #include "net/tools/dump_cache/simple_cache_dumper.h" |
19 | 19 |
20 #if defined(OS_WIN) | |
21 #include "base/process/launch.h" | |
22 #include "base/win/scoped_handle.h" | |
23 #include "net/tools/dump_cache/upgrade_win.h" | |
24 #endif | |
25 | |
26 enum Errors { | 20 enum Errors { |
27 GENERIC = -1, | 21 GENERIC = -1, |
28 ALL_GOOD = 0, | 22 ALL_GOOD = 0, |
29 INVALID_ARGUMENT = 1, | 23 INVALID_ARGUMENT = 1, |
30 FILE_ACCESS_ERROR, | 24 FILE_ACCESS_ERROR, |
31 UNKNOWN_VERSION, | 25 UNKNOWN_VERSION, |
32 TOOL_NOT_FOUND, | 26 TOOL_NOT_FOUND, |
33 }; | 27 }; |
34 | 28 |
35 #if defined(OS_WIN) | |
36 const char kUpgradeHelp[] = | |
37 "\nIn order to use the upgrade function, a version of this tool that\n" | |
38 "understands the file format of the files to upgrade is needed. For\n" | |
39 "instance, to upgrade files saved with file format 3.4 to version 5.2,\n" | |
40 "a version of this program that was compiled with version 3.4 has to be\n" | |
41 "located beside this executable, and named dump_cache_3.exe, and this\n" | |
42 "executable should be compiled with version 5.2 being the current one."; | |
43 #endif // defined(OS_WIN) | |
44 | |
45 // Folders to read and write cache files. | 29 // Folders to read and write cache files. |
46 const char kInputPath[] = "input"; | 30 const char kInputPath[] = "input"; |
47 const char kOutputPath[] = "output"; | 31 const char kOutputPath[] = "output"; |
48 | 32 |
49 // Dumps the file headers to stdout. | 33 // Dumps the file headers to stdout. |
50 const char kDumpHeaders[] = "dump-headers"; | 34 const char kDumpHeaders[] = "dump-headers"; |
51 | 35 |
52 // Dumps all entries to stdout. | 36 // Dumps all entries to stdout. |
53 const char kDumpContents[] = "dump-contents"; | 37 const char kDumpContents[] = "dump-contents"; |
54 | 38 |
55 // Convert the cache to files. | 39 // Convert the cache to files. |
56 const char kDumpToFiles[] = "dump-to-files"; | 40 const char kDumpToFiles[] = "dump-to-files"; |
57 | 41 |
58 // Upgrade an old version to the current one. | |
59 const char kUpgrade[] = "upgrade"; | |
60 | |
61 // Internal use: | |
62 const char kSlave[] = "slave"; | |
63 #if defined(OS_WIN) | |
64 const char kPipe[] = "pipe"; | |
65 #endif // defined(OS_WIN) | |
66 | |
67 int Help() { | 42 int Help() { |
68 printf("warning: input files are modified by this tool\n"); | 43 printf("warning: input files are modified by this tool\n"); |
69 printf("dump_cache --input=path1 [--output=path2]\n"); | 44 printf("dump_cache --input=path1 [--output=path2]\n"); |
70 printf("--dump-headers: display file headers\n"); | 45 printf("--dump-headers: display file headers\n"); |
71 printf("--dump-contents: display all entries\n"); | 46 printf("--dump-contents: display all entries\n"); |
72 printf("--upgrade: copy contents to the output path\n"); | |
73 printf("--dump-to-files: write the contents of the cache to files\n"); | 47 printf("--dump-to-files: write the contents of the cache to files\n"); |
74 return INVALID_ARGUMENT; | 48 return INVALID_ARGUMENT; |
75 } | 49 } |
76 | 50 |
77 #if defined(OS_WIN) | |
78 | |
79 // Starts a new process, to generate the files. | |
80 int LaunchSlave(CommandLine command_line, | |
81 const base::string16& pipe_number, | |
82 int version) { | |
83 bool do_upgrade = command_line.HasSwitch(kUpgrade); | |
84 bool do_convert_to_text = command_line.HasSwitch(kDumpToFiles); | |
85 | |
86 if (do_upgrade) { | |
87 base::FilePath program( | |
88 base::StringPrintf(L"%ls%d", L"dump_cache", version)); | |
89 command_line.SetProgram(program); | |
90 } | |
91 | |
92 if (do_upgrade || do_convert_to_text) | |
93 command_line.AppendSwitch(kSlave); | |
94 | |
95 command_line.AppendSwitchNative(kPipe, pipe_number); | |
96 if (!base::LaunchProcess(command_line, base::LaunchOptions(), NULL)) { | |
97 printf("Unable to launch the needed version of this tool: %ls\n", | |
98 command_line.GetProgram().value().c_str()); | |
99 printf("%s", kUpgradeHelp); | |
100 return TOOL_NOT_FOUND; | |
101 } | |
102 return ALL_GOOD; | |
103 } | |
104 | |
105 #endif | |
106 | |
107 // ----------------------------------------------------------------------- | 51 // ----------------------------------------------------------------------- |
108 | 52 |
109 int main(int argc, const char* argv[]) { | 53 int main(int argc, const char* argv[]) { |
110 // Setup an AtExitManager so Singleton objects will be destroyed. | 54 // Setup an AtExitManager so Singleton objects will be destroyed. |
111 base::AtExitManager at_exit_manager; | 55 base::AtExitManager at_exit_manager; |
112 | 56 |
113 base::CommandLine::Init(argc, argv); | 57 base::CommandLine::Init(argc, argv); |
114 | 58 |
115 const base::CommandLine& command_line = | 59 const base::CommandLine& command_line = |
116 *base::CommandLine::ForCurrentProcess(); | 60 *base::CommandLine::ForCurrentProcess(); |
117 base::FilePath input_path = command_line.GetSwitchValuePath(kInputPath); | 61 base::FilePath input_path = command_line.GetSwitchValuePath(kInputPath); |
118 if (input_path.empty()) | 62 if (input_path.empty()) |
119 return Help(); | 63 return Help(); |
120 | 64 |
121 bool dump_to_files = command_line.HasSwitch(kDumpToFiles); | 65 bool dump_to_files = command_line.HasSwitch(kDumpToFiles); |
122 bool upgrade = command_line.HasSwitch(kUpgrade); | |
123 | 66 |
124 base::FilePath output_path = command_line.GetSwitchValuePath(kOutputPath); | 67 base::FilePath output_path = command_line.GetSwitchValuePath(kOutputPath); |
125 if ((dump_to_files || upgrade) && output_path.empty()) | 68 if (dump_to_files && output_path.empty()) |
126 return Help(); | 69 return Help(); |
127 | 70 |
128 int version = GetMajorVersion(input_path); | 71 int version = GetMajorVersion(input_path); |
129 if (!version) | 72 if (!version) |
130 return FILE_ACCESS_ERROR; | 73 return FILE_ACCESS_ERROR; |
131 | 74 |
132 bool slave_required = upgrade; | |
133 if (version != disk_cache::kCurrentVersion >> 16) { | |
134 if (command_line.HasSwitch(kSlave)) { | |
135 printf("Unknown version\n"); | |
136 return UNKNOWN_VERSION; | |
137 } | |
138 slave_required = true; | |
139 } | |
140 | |
141 #if defined(OS_WIN) | |
142 base::string16 pipe_number = command_line.GetSwitchValueNative(kPipe); | |
143 if (command_line.HasSwitch(kSlave) && slave_required) | |
144 return RunSlave(input_path, pipe_number); | |
145 | |
146 base::win::ScopedHandle server; | |
147 if (slave_required) { | |
148 server.Set(CreateServer(&pipe_number)); | |
149 if (!server.IsValid()) { | |
150 printf("Unable to create the server pipe\n"); | |
151 return GENERIC; | |
152 } | |
153 | |
154 int ret = LaunchSlave(command_line, pipe_number, version); | |
155 if (ret) | |
156 return ret; | |
157 } | |
158 | |
159 if (upgrade) | |
160 return UpgradeCache(output_path, server); | |
161 | |
162 if (slave_required) { | |
163 // Wait until the slave starts dumping data before we quit. Lazy "fix" for a | |
164 // console quirk. | |
165 Sleep(500); | |
166 return ALL_GOOD; | |
167 } | |
168 #else // defined(OS_WIN) | |
169 if (slave_required) { | |
170 printf("Unsupported operation\n"); | |
171 return INVALID_ARGUMENT; | |
172 } | |
173 #endif | |
174 | |
175 if (dump_to_files) { | 75 if (dump_to_files) { |
176 net::SimpleCacheDumper dumper(input_path, output_path); | 76 net::SimpleCacheDumper dumper(input_path, output_path); |
177 dumper.Run(); | 77 dumper.Run(); |
178 return ALL_GOOD; | 78 return ALL_GOOD; |
179 } | 79 } |
180 | 80 |
181 if (command_line.HasSwitch(kDumpContents)) | 81 if (command_line.HasSwitch(kDumpContents)) |
182 return DumpContents(input_path); | 82 return DumpContents(input_path); |
183 | 83 |
184 if (command_line.HasSwitch(kDumpHeaders)) | 84 if (command_line.HasSwitch(kDumpHeaders)) |
185 return DumpHeaders(input_path); | 85 return DumpHeaders(input_path); |
186 | 86 |
187 return Help(); | 87 return Help(); |
188 } | 88 } |
OLD | NEW |