• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2008 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.
4 
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.
7 
8 #include <stdio.h>
9 #include <string>
10 
11 #include "base/at_exit.h"
12 #include "base/command_line.h"
13 #include "base/process_util.h"
14 #include "base/scoped_handle.h"
15 #include "base/string_util.h"
16 
17 #include "net/disk_cache/disk_format.h"
18 
19 enum Errors {
20   GENERIC = -1,
21   ALL_GOOD = 0,
22   INVALID_ARGUMENT = 1,
23   FILE_ACCESS_ERROR,
24   UNKNOWN_VERSION,
25   TOOL_NOT_FOUND,
26 };
27 
28 int GetMajorVersion(const std::wstring& input_path);
29 int DumpContents(const std::wstring& input_path);
30 int DumpHeaders(const std::wstring& input_path);
31 int RunSlave(const std::wstring& input_path, const std::wstring& pipe_number);
32 int CopyCache(const std::wstring& output_path, HANDLE pipe, bool copy_to_text);
33 HANDLE CreateServer(std::wstring* pipe_number);
34 
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.";
42 
43 // Folders to read and write cache files.
44 const wchar_t kInputPath[] = L"input";
45 const wchar_t kOutputPath[] = L"output";
46 
47 // Dumps the file headers to stdout.
48 const wchar_t kDumpHeaders[] = L"dump-headers";
49 
50 // Dumps all entries to stdout.
51 const wchar_t kDumpContents[] = L"dump-contents";
52 
53 // Convert the cache to files.
54 const wchar_t kDumpToFiles[] = L"dump-to-files";
55 
56 // Upgrade an old version to the current one.
57 const wchar_t kUpgrade[] = L"upgrade";
58 
59 // Internal use:
60 const char kSlave[] = "slave";
61 const char kPipe[] = "pipe";
62 
Help()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;
71 }
72 
73 // Starts a new process, to generate the files.
LaunchSlave(const CommandLine & command_line,const std::wstring & pipe_number,int version)74 int LaunchSlave(const CommandLine& command_line,
75                 const std::wstring& pipe_number, int version) {
76   // TODO(port): remove this string-munging hackery.
77   std::wstring hacked_command_line = command_line.command_line_string();
78   const std::wstring old_exe(L"dump_cache");
79   size_t to_remove = hacked_command_line.find(old_exe);
80   hacked_command_line.erase(to_remove, old_exe.size());
81 
82   bool do_upgrade = command_line.HasSwitch(kUpgrade);
83   bool do_convert_to_text = command_line.HasSwitch(kDumpToFiles);
84 
85   std::wstring new_program;
86   if (do_upgrade)
87     new_program = StringPrintf(L"%ls%d", L"dump_cache_", version);
88   else
89     new_program = StringPrintf(L"dump_cache");
90 
91   hacked_command_line.insert(to_remove, new_program);
92 
93   CommandLine new_command_line = CommandLine::FromString(hacked_command_line);
94 
95   if (do_upgrade || do_convert_to_text)
96     new_command_line.AppendSwitch(kSlave);
97 
98   new_command_line.AppendSwitchWithValue(kPipe, pipe_number);
99   if (!base::LaunchApp(new_command_line, false, false, NULL)) {
100     printf("Unable to launch the needed version of this tool: %ls\n",
101            new_program.c_str());
102     printf(kUpgradeHelp);
103     return TOOL_NOT_FOUND;
104   }
105   return ALL_GOOD;
106 }
107 
108 // -----------------------------------------------------------------------
109 
main(int argc,const char * argv[])110 int main(int argc, const char* argv[]) {
111   // Setup an AtExitManager so Singleton objects will be destroyed.
112   base::AtExitManager at_exit_manager;
113 
114   CommandLine::Init(argc, argv);
115 
116   const CommandLine& command_line = *CommandLine::ForCurrentProcess();
117   std::wstring input_path = command_line.GetSwitchValue(kInputPath);
118   if (input_path.empty())
119     return Help();
120 
121   bool upgrade = false;
122   bool slave_required = false;
123   bool copy_to_text = false;
124   std::wstring output_path = command_line.GetSwitchValue(kOutputPath);
125   if (command_line.HasSwitch(kUpgrade))
126     upgrade = true;
127   if (command_line.HasSwitch(kDumpToFiles))
128     copy_to_text = true;
129 
130   if (upgrade || copy_to_text) {
131     if (output_path.empty())
132       return Help();
133     slave_required = true;
134   }
135 
136   int version = GetMajorVersion(input_path);
137   if (!version)
138     return FILE_ACCESS_ERROR;
139 
140   if (version != disk_cache::kCurrentVersion >> 16) {
141     if (command_line.HasSwitch(kSlave)) {
142       printf("Unknown version\n");
143       return UNKNOWN_VERSION;
144     }
145     slave_required = true;
146   }
147 
148   std::wstring pipe_number = command_line.GetSwitchValue(kPipe);
149   if (command_line.HasSwitch(kSlave) && slave_required)
150     return RunSlave(input_path, pipe_number);
151 
152   ScopedHandle server;
153   if (slave_required) {
154     server.Set(CreateServer(&pipe_number));
155     if (!server.IsValid()) {
156       printf("Unable to create the server pipe\n");
157       return -1;
158     }
159 
160     int ret = LaunchSlave(command_line, pipe_number, version);
161     if (ret)
162       return ret;
163   }
164 
165   if (upgrade || copy_to_text)
166     return CopyCache(output_path, server, copy_to_text);
167 
168   if (slave_required) {
169     // Wait until the slave starts dumping data before we quit. Lazy "fix" for a
170     // console quirk.
171     Sleep(500);
172     return ALL_GOOD;
173   }
174 
175   if (command_line.HasSwitch(kDumpContents))
176     return DumpContents(input_path);
177   if (command_line.HasSwitch(kDumpHeaders))
178     return DumpHeaders(input_path);
179   return Help();
180 }
181