1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This command-line program dumps the contents of a set of cache files, either
// to stdout or to another set of cache files.
#include <stdio.h>
#include <string>
#include "base/at_exit.h"
#include "base/command_line.h"
#include "base/process_util.h"
#include "base/string_util.h"
#include "base/stringprintf.h"
#include "net/disk_cache/disk_format.h"
#include "net/tools/dump_cache/dump_files.h"
#include "net/tools/dump_cache/simple_cache_dumper.h"
#if defined(OS_WIN)
#include "base/win/scoped_handle.h"
#include "net/tools/dump_cache/upgrade_win.h"
#endif
enum Errors {
GENERIC = -1,
ALL_GOOD = 0,
INVALID_ARGUMENT = 1,
FILE_ACCESS_ERROR,
UNKNOWN_VERSION,
TOOL_NOT_FOUND,
};
const char kUpgradeHelp[] =
"\nIn order to use the upgrade function, a version of this tool that\n"
"understands the file format of the files to upgrade is needed. For\n"
"instance, to upgrade files saved with file format 3.4 to version 5.2,\n"
"a version of this program that was compiled with version 3.4 has to be\n"
"located beside this executable, and named dump_cache_3.exe, and this\n"
"executable should be compiled with version 5.2 being the current one.";
// Folders to read and write cache files.
const char kInputPath[] = "input";
const char kOutputPath[] = "output";
// Dumps the file headers to stdout.
const char kDumpHeaders[] = "dump-headers";
// Dumps all entries to stdout.
const char kDumpContents[] = "dump-contents";
// Convert the cache to files.
const char kDumpToFiles[] = "dump-to-files";
// Upgrade an old version to the current one.
const char kUpgrade[] = "upgrade";
// Internal use:
const char kSlave[] = "slave";
const char kPipe[] = "pipe";
int Help() {
printf("warning: input files are modified by this tool\n");
printf("dump_cache --input=path1 [--output=path2]\n");
printf("--dump-headers: display file headers\n");
printf("--dump-contents: display all entries\n");
printf("--upgrade: copy contents to the output path\n");
printf("--dump-to-files: write the contents of the cache to files\n");
return INVALID_ARGUMENT;
}
// -----------------------------------------------------------------------
int main(int argc, const char* argv[]) {
// Setup an AtExitManager so Singleton objects will be destroyed.
base::AtExitManager at_exit_manager;
CommandLine::Init(argc, argv);
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
FilePath input_path = command_line.GetSwitchValuePath(kInputPath);
if (input_path.empty())
return Help();
bool upgrade = false;
bool copy_to_text = false;
FilePath output_path = command_line.GetSwitchValuePath(kOutputPath);
if (command_line.HasSwitch(kUpgrade))
upgrade = true;
if (command_line.HasSwitch(kDumpToFiles))
copy_to_text = true;
if (copy_to_text) {
net::SimpleCacheDumper dumper(input_path, output_path);
dumper.Run();
return 0;
}
#if defined(OS_WIN)
bool slave_required = false;
if (upgrade) {
if (output_path.empty())
return Help();
slave_required = true;
}
int version = GetMajorVersion(input_path);
if (!version)
return FILE_ACCESS_ERROR;
if (version != disk_cache::kCurrentVersion >> 16) {
if (command_line.HasSwitch(kSlave)) {
printf("Unknown version\n");
return UNKNOWN_VERSION;
}
slave_required = true;
}
std::wstring pipe_number = command_line.GetSwitchValueNative(kPipe);
if (command_line.HasSwitch(kSlave) && slave_required)
return RunSlave(input_path, pipe_number);
base::win::ScopedHandle server;
if (slave_required) {
server.Set(CreateServer(&pipe_number));
if (!server.IsValid()) {
printf("Unable to create the server pipe\n");
return -1;
}
int ret = LaunchSlave(command_line, pipe_number, version);
if (ret)
return ret;
}
// TODO(rch): Remove the logic from CopyCache that is redundant with
// SimpleCacheDumper.
if (upgrade)
return CopyCache(output_path, server, false);
if (slave_required) {
// Wait until the slave starts dumping data before we quit. Lazy "fix" for a
// console quirk.
Sleep(500);
return ALL_GOOD;
}
if (command_line.HasSwitch(kDumpContents))
return DumpContents(input_path);
if (command_line.HasSwitch(kDumpHeaders))
return DumpHeaders(input_path);
#endif
return Help();
}
|