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