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.
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"
22 #include "base/win/scoped_handle.h"
23 #include "net/tools/dump_cache/upgrade_win.h"
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";
60 const char kSlave
[] = "slave";
61 const char kPipe
[] = "pipe";
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
;
75 // Starts a new process, to generate the files.
76 int LaunchSlave(CommandLine command_line
,
77 const string16
& pipe_number
,
79 bool do_upgrade
= command_line
.HasSwitch(kUpgrade
);
80 bool do_convert_to_text
= command_line
.HasSwitch(kDumpToFiles
);
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());
95 return TOOL_NOT_FOUND
;
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())
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())
122 int version
= GetMajorVersion(input_path
);
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;
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");
148 int ret
= LaunchSlave(command_line
, pipe_number
, version
);
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
162 #else // defined(OS_WIN)
163 if (slave_required
) {
164 printf("Unsupported operation\n");
165 return INVALID_ARGUMENT
;
170 net::SimpleCacheDumper
dumper(input_path
, output_path
);
175 if (command_line
.HasSwitch(kDumpContents
))
176 return DumpContents(input_path
);
178 if (command_line
.HasSwitch(kDumpHeaders
))
179 return DumpHeaders(input_path
);