Enables compositing support for webview.
[chromium-blink-merge.git] / net / tools / dump_cache / dump_cache.cc
blobd68469b3f070acfcaa98285099cea5f2c396bfb5
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/process_util.h"
14 #include "base/string16.h"
15 #include "base/string_util.h"
16 #include "base/stringprintf.h"
17 #include "net/disk_cache/disk_format.h"
18 #include "net/tools/dump_cache/dump_files.h"
19 #include "net/tools/dump_cache/simple_cache_dumper.h"
21 #if defined(OS_WIN)
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 const char kUpgradeHelp[] =
36 "\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"
38 "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"
40 "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.";
43 // Folders to read and write cache files.
44 const char kInputPath[] = "input";
45 const char kOutputPath[] = "output";
47 // Dumps the file headers to stdout.
48 const char kDumpHeaders[] = "dump-headers";
50 // Dumps all entries to stdout.
51 const char kDumpContents[] = "dump-contents";
53 // Convert the cache to files.
54 const char kDumpToFiles[] = "dump-to-files";
56 // Upgrade an old version to the current one.
57 const char kUpgrade[] = "upgrade";
59 // Internal use:
60 const char kSlave[] = "slave";
61 const char kPipe[] = "pipe";
63 int Help() {
64 printf("warning: input files are modified by this tool\n");
65 printf("dump_cache --input=path1 [--output=path2]\n");
66 printf("--dump-headers: display file headers\n");
67 printf("--dump-contents: display all entries\n");
68 printf("--upgrade: copy contents to the output path\n");
69 printf("--dump-to-files: write the contents of the cache to files\n");
70 return INVALID_ARGUMENT;
73 #if defined(OS_WIN)
75 // Starts a new process, to generate the files.
76 int LaunchSlave(CommandLine command_line,
77 const string16& pipe_number,
78 int version) {
79 bool do_upgrade = command_line.HasSwitch(kUpgrade);
80 bool do_convert_to_text = command_line.HasSwitch(kDumpToFiles);
82 if (do_upgrade) {
83 FilePath program(base::StringPrintf(L"%ls%d", L"dump_cache", version));
84 command_line.SetProgram(program);
87 if (do_upgrade || do_convert_to_text)
88 command_line.AppendSwitch(kSlave);
90 command_line.AppendSwitchNative(kPipe, pipe_number);
91 if (!base::LaunchProcess(command_line, base::LaunchOptions(), NULL)) {
92 printf("Unable to launch the needed version of this tool: %ls\n",
93 command_line.GetProgram().value().c_str());
94 printf(kUpgradeHelp);
95 return TOOL_NOT_FOUND;
97 return ALL_GOOD;
100 #endif
102 // -----------------------------------------------------------------------
104 int main(int argc, const char* argv[]) {
105 // Setup an AtExitManager so Singleton objects will be destroyed.
106 base::AtExitManager at_exit_manager;
108 CommandLine::Init(argc, argv);
110 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
111 FilePath input_path = command_line.GetSwitchValuePath(kInputPath);
112 if (input_path.empty())
113 return Help();
115 bool dump_to_files = command_line.HasSwitch(kDumpToFiles);
116 bool upgrade = command_line.HasSwitch(kUpgrade);
118 FilePath output_path = command_line.GetSwitchValuePath(kOutputPath);
119 if ((dump_to_files || upgrade) && output_path.empty())
120 return Help();
122 int version = GetMajorVersion(input_path);
123 if (!version)
124 return FILE_ACCESS_ERROR;
126 bool slave_required = upgrade;
127 if (version != disk_cache::kCurrentVersion >> 16) {
128 if (command_line.HasSwitch(kSlave)) {
129 printf("Unknown version\n");
130 return UNKNOWN_VERSION;
132 slave_required = true;
135 #if defined(OS_WIN)
136 string16 pipe_number = command_line.GetSwitchValueNative(kPipe);
137 if (command_line.HasSwitch(kSlave) && slave_required)
138 return RunSlave(input_path, pipe_number);
140 base::win::ScopedHandle server;
141 if (slave_required) {
142 server.Set(CreateServer(&pipe_number));
143 if (!server.IsValid()) {
144 printf("Unable to create the server pipe\n");
145 return GENERIC;
148 int ret = LaunchSlave(command_line, pipe_number, version);
149 if (ret)
150 return ret;
153 if (upgrade)
154 return UpgradeCache(output_path, server);
156 if (slave_required) {
157 // Wait until the slave starts dumping data before we quit. Lazy "fix" for a
158 // console quirk.
159 Sleep(500);
160 return ALL_GOOD;
162 #else // defined(OS_WIN)
163 if (slave_required) {
164 printf("Unsupported operation\n");
165 return INVALID_ARGUMENT;
167 #endif
169 if (dump_to_files) {
170 net::SimpleCacheDumper dumper(input_path, output_path);
171 dumper.Run();
172 return ALL_GOOD;
175 if (command_line.HasSwitch(kDumpContents))
176 return DumpContents(input_path);
178 if (command_line.HasSwitch(kDumpHeaders))
179 return DumpHeaders(input_path);
181 return Help();