Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / tools / dump_cache / dump_cache.cc
blobb3970e6053f0f7f64a560acc807bbf3fabea90c1
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
3 // found in the LICENSE file.
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.
8 #include <stdio.h>
9 #include <string>
11 #include "base/at_exit.h"
12 #include "base/command_line.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "net/disk_cache/blockfile/disk_format.h"
17 #include "net/tools/dump_cache/dump_files.h"
18 #include "net/tools/dump_cache/simple_cache_dumper.h"
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
26 enum Errors {
27 GENERIC = -1,
28 ALL_GOOD = 0,
29 INVALID_ARGUMENT = 1,
30 FILE_ACCESS_ERROR,
31 UNKNOWN_VERSION,
32 TOOL_NOT_FOUND,
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)
45 // Folders to read and write cache files.
46 const char kInputPath[] = "input";
47 const char kOutputPath[] = "output";
49 // Dumps the file headers to stdout.
50 const char kDumpHeaders[] = "dump-headers";
52 // Dumps all entries to stdout.
53 const char kDumpContents[] = "dump-contents";
55 // Convert the cache to files.
56 const char kDumpToFiles[] = "dump-to-files";
58 // Upgrade an old version to the current one.
59 const char kUpgrade[] = "upgrade";
61 // Internal use:
62 const char kSlave[] = "slave";
63 #if defined(OS_WIN)
64 const char kPipe[] = "pipe";
65 #endif // defined(OS_WIN)
67 int Help() {
68 printf("warning: input files are modified by this tool\n");
69 printf("dump_cache --input=path1 [--output=path2]\n");
70 printf("--dump-headers: display file headers\n");
71 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");
74 return INVALID_ARGUMENT;
77 #if defined(OS_WIN)
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);
86 if (do_upgrade) {
87 base::FilePath program(
88 base::StringPrintf(L"%ls%d", L"dump_cache", version));
89 command_line.SetProgram(program);
92 if (do_upgrade || do_convert_to_text)
93 command_line.AppendSwitch(kSlave);
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;
102 return ALL_GOOD;
105 #endif
107 // -----------------------------------------------------------------------
109 int main(int argc, const char* argv[]) {
110 // Setup an AtExitManager so Singleton objects will be destroyed.
111 base::AtExitManager at_exit_manager;
113 base::CommandLine::Init(argc, argv);
115 const base::CommandLine& command_line =
116 *base::CommandLine::ForCurrentProcess();
117 base::FilePath input_path = command_line.GetSwitchValuePath(kInputPath);
118 if (input_path.empty())
119 return Help();
121 bool dump_to_files = command_line.HasSwitch(kDumpToFiles);
122 bool upgrade = command_line.HasSwitch(kUpgrade);
124 base::FilePath output_path = command_line.GetSwitchValuePath(kOutputPath);
125 if ((dump_to_files || upgrade) && output_path.empty())
126 return Help();
128 int version = GetMajorVersion(input_path);
129 if (!version)
130 return FILE_ACCESS_ERROR;
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;
138 slave_required = true;
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);
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;
154 int ret = LaunchSlave(command_line, pipe_number, version);
155 if (ret)
156 return ret;
159 if (upgrade)
160 return UpgradeCache(output_path, server);
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;
168 #else // defined(OS_WIN)
169 if (slave_required) {
170 printf("Unsupported operation\n");
171 return INVALID_ARGUMENT;
173 #endif
175 if (dump_to_files) {
176 net::SimpleCacheDumper dumper(input_path, output_path);
177 dumper.Run();
178 return ALL_GOOD;
181 if (command_line.HasSwitch(kDumpContents))
182 return DumpContents(input_path);
184 if (command_line.HasSwitch(kDumpHeaders))
185 return DumpHeaders(input_path);
187 return Help();