OLD | NEW |
(Empty) | |
| 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 Upgrade(const std::wstring output_path, HANDLE pipe); |
| 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 // Upgrade an old version to the current one. |
| 54 const wchar_t kUpgrade[] = L"upgrade"; |
| 55 |
| 56 // Internal use: |
| 57 const wchar_t kSlave[] = L"slave"; |
| 58 const wchar_t kPipe[] = L"pipe"; |
| 59 |
| 60 int Help() { |
| 61 printf("warning: input files are modified by this tool\n"); |
| 62 printf("dump_cache --input=path1 [--output=path2]\n"); |
| 63 printf("--dump-headers: display file headers\n"); |
| 64 printf("--dump-contents: display all entries\n"); |
| 65 printf("--upgrade: copy contents to the output path\n"); |
| 66 return INVALID_ARGUMENT; |
| 67 } |
| 68 |
| 69 // Starts a new process, to generate the files. |
| 70 int LaunchSlave(CommandLine &command_line, const std::wstring pipe_number, |
| 71 int version) { |
| 72 std::wstring new_command_line = command_line.command_line_string(); |
| 73 const std::wstring old_exe(L"dump_cache.exe"); |
| 74 size_t to_remove = new_command_line.find(old_exe); |
| 75 new_command_line.erase(to_remove, old_exe.size()); |
| 76 |
| 77 std::wstring new_program = StringPrintf(L"%ls%d.exe", L"dump_cache_", |
| 78 version); |
| 79 new_command_line.insert(to_remove, new_program); |
| 80 if (command_line.HasSwitch(kUpgrade)) |
| 81 CommandLine::AppendSwitch(&new_command_line, kSlave); |
| 82 |
| 83 CommandLine::AppendSwitchWithValue(&new_command_line, kPipe, pipe_number); |
| 84 if (!base::LaunchApp(new_command_line, false, false, NULL)) { |
| 85 printf("Unable to launch the needed version of this tool: %ls\n", |
| 86 new_program.c_str()); |
| 87 printf(kUpgradeHelp); |
| 88 return TOOL_NOT_FOUND; |
| 89 } |
| 90 return ALL_GOOD; |
| 91 } |
| 92 |
| 93 // ----------------------------------------------------------------------- |
| 94 |
| 95 int main(int argc, const char* argv[]) { |
| 96 // Setup an AtExitManager so Singleton objects will be destroyed. |
| 97 base::AtExitManager at_exit_manager; |
| 98 |
| 99 CommandLine command_line; |
| 100 std::wstring input_path = command_line.GetSwitchValue(kInputPath); |
| 101 if (input_path.empty()) |
| 102 return Help(); |
| 103 |
| 104 bool upgrade = false; |
| 105 bool slave_required = false; |
| 106 std::wstring output_path; |
| 107 if (command_line.HasSwitch(kUpgrade)) { |
| 108 output_path = command_line.GetSwitchValue(kOutputPath); |
| 109 if (output_path.empty()) |
| 110 return Help(); |
| 111 slave_required = true; |
| 112 upgrade = true; |
| 113 } |
| 114 |
| 115 int version = GetMajorVersion(input_path); |
| 116 if (!version) |
| 117 return FILE_ACCESS_ERROR; |
| 118 |
| 119 if (version != disk_cache::kCurrentVersion >> 16) { |
| 120 if (command_line.HasSwitch(kSlave)) { |
| 121 printf("Unknown version\n"); |
| 122 return UNKNOWN_VERSION; |
| 123 } |
| 124 slave_required = true; |
| 125 } |
| 126 |
| 127 std::wstring pipe_number = command_line.GetSwitchValue(kPipe); |
| 128 if (command_line.HasSwitch(kSlave) && slave_required) |
| 129 return RunSlave(input_path, pipe_number); |
| 130 |
| 131 ScopedHandle server; |
| 132 if (slave_required) { |
| 133 server.Set(CreateServer(&pipe_number)); |
| 134 if (!server.IsValid()) { |
| 135 printf("Unable to create the server pipe\n"); |
| 136 return -1; |
| 137 } |
| 138 |
| 139 int ret = LaunchSlave(command_line, pipe_number, version); |
| 140 if (ret) |
| 141 return ret; |
| 142 } |
| 143 |
| 144 if (upgrade) |
| 145 return Upgrade(output_path, server); |
| 146 |
| 147 if (slave_required) { |
| 148 // Wait until the slave starts dumping data before we quit. Lazy "fix" for a |
| 149 // console quirk. |
| 150 Sleep(500); |
| 151 return ALL_GOOD; |
| 152 } |
| 153 |
| 154 if (command_line.HasSwitch(kDumpContents)) |
| 155 return DumpContents(input_path); |
| 156 if (command_line.HasSwitch(kDumpHeaders)) |
| 157 return DumpHeaders(input_path); |
| 158 return Help(); |
| 159 } |
| 160 |
OLD | NEW |