dump_cache.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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. 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/string_util.h" 15#include "base/stringprintf.h" 16#include "base/win/scoped_handle.h" 17#include "net/disk_cache/disk_format.h" 18 19enum 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 28int GetMajorVersion(const FilePath& input_path); 29int DumpContents(const FilePath& input_path); 30int DumpHeaders(const FilePath& input_path); 31int RunSlave(const FilePath& input_path, const std::wstring& pipe_number); 32int CopyCache(const FilePath& output_path, HANDLE pipe, bool copy_to_text); 33HANDLE CreateServer(std::wstring* pipe_number); 34 35const 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. 44const char kInputPath[] = "input"; 45const char kOutputPath[] = "output"; 46 47// Dumps the file headers to stdout. 48const char kDumpHeaders[] = "dump-headers"; 49 50// Dumps all entries to stdout. 51const char kDumpContents[] = "dump-contents"; 52 53// Convert the cache to files. 54const char kDumpToFiles[] = "dump-to-files"; 55 56// Upgrade an old version to the current one. 57const char kUpgrade[] = "upgrade"; 58 59// Internal use: 60const char kSlave[] = "slave"; 61const char kPipe[] = "pipe"; 62 63int 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. 74int LaunchSlave(CommandLine command_line, 75 const std::wstring& pipe_number, 76 int version) { 77 bool do_upgrade = command_line.HasSwitch(kUpgrade); 78 bool do_convert_to_text = command_line.HasSwitch(kDumpToFiles); 79 80 if (do_upgrade) { 81 FilePath program(base::StringPrintf(L"%ls%d", L"dump_cache", version)); 82 command_line.SetProgram(program); 83 } 84 85 if (do_upgrade || do_convert_to_text) 86 command_line.AppendSwitch(kSlave); 87 88 command_line.AppendSwitchNative(kPipe, pipe_number); 89 if (!base::LaunchProcess(command_line, base::LaunchOptions(), NULL)) { 90 printf("Unable to launch the needed version of this tool: %ls\n", 91 command_line.GetProgram().value().c_str()); 92 printf(kUpgradeHelp); 93 return TOOL_NOT_FOUND; 94 } 95 return ALL_GOOD; 96} 97 98// ----------------------------------------------------------------------- 99 100int main(int argc, const char* argv[]) { 101 // Setup an AtExitManager so Singleton objects will be destroyed. 102 base::AtExitManager at_exit_manager; 103 104 CommandLine::Init(argc, argv); 105 106 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 107 FilePath input_path = command_line.GetSwitchValuePath(kInputPath); 108 if (input_path.empty()) 109 return Help(); 110 111 bool upgrade = false; 112 bool slave_required = false; 113 bool copy_to_text = false; 114 FilePath output_path = command_line.GetSwitchValuePath(kOutputPath); 115 116 if (command_line.HasSwitch(kUpgrade)) 117 upgrade = true; 118 if (command_line.HasSwitch(kDumpToFiles)) 119 copy_to_text = true; 120 121 if (upgrade || copy_to_text) { 122 if (output_path.empty()) 123 return Help(); 124 slave_required = true; 125 } 126 127 int version = GetMajorVersion(input_path); 128 if (!version) 129 return FILE_ACCESS_ERROR; 130 131 if (version != disk_cache::kCurrentVersion >> 16) { 132 if (command_line.HasSwitch(kSlave)) { 133 printf("Unknown version\n"); 134 return UNKNOWN_VERSION; 135 } 136 slave_required = true; 137 } 138 139 std::wstring pipe_number = command_line.GetSwitchValueNative(kPipe); 140 if (command_line.HasSwitch(kSlave) && slave_required) 141 return RunSlave(input_path, pipe_number); 142 143 base::win::ScopedHandle server; 144 if (slave_required) { 145 server.Set(CreateServer(&pipe_number)); 146 if (!server.IsValid()) { 147 printf("Unable to create the server pipe\n"); 148 return -1; 149 } 150 151 int ret = LaunchSlave(command_line, pipe_number, version); 152 if (ret) 153 return ret; 154 } 155 156 if (upgrade || copy_to_text) 157 return CopyCache(output_path, server, copy_to_text); 158 159 if (slave_required) { 160 // Wait until the slave starts dumping data before we quit. Lazy "fix" for a 161 // console quirk. 162 Sleep(500); 163 return ALL_GOOD; 164 } 165 166 if (command_line.HasSwitch(kDumpContents)) 167 return DumpContents(input_path); 168 if (command_line.HasSwitch(kDumpHeaders)) 169 return DumpHeaders(input_path); 170 return Help(); 171} 172