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/strings/string16.h" 14 #include "base/strings/string_util.h" 15 #include "base/strings/stringprintf.h" 16 #include "net/disk_cache/blockfile/disk_format.h" 17 #include "net/tools/dump_cache/dump_files.h" 18 #include "net/tools/dump_cache/simple_cache_dumper.h" 19 20 enum Errors { 21 GENERIC = -1, 22 ALL_GOOD = 0, 23 INVALID_ARGUMENT = 1, 24 FILE_ACCESS_ERROR, 25 UNKNOWN_VERSION, 26 TOOL_NOT_FOUND, 27 }; 28 29 // Folders to read and write cache files. 30 const char kInputPath[] = "input"; 31 const char kOutputPath[] = "output"; 32 33 // Dumps the file headers to stdout. 34 const char kDumpHeaders[] = "dump-headers"; 35 36 // Dumps all entries to stdout. 37 const char kDumpContents[] = "dump-contents"; 38 39 // Convert the cache to files. 40 const char kDumpToFiles[] = "dump-to-files"; 41 42 int Help() { 43 printf("warning: input files are modified by this tool\n"); 44 printf("dump_cache --input=path1 [--output=path2]\n"); 45 printf("--dump-headers: display file headers\n"); 46 printf("--dump-contents: display all entries\n"); 47 printf("--dump-to-files: write the contents of the cache to files\n"); 48 return INVALID_ARGUMENT; 49 } 50 51 // ----------------------------------------------------------------------- 52 53 int main(int argc, const char* argv[]) { 54 // Setup an AtExitManager so Singleton objects will be destroyed. 55 base::AtExitManager at_exit_manager; 56 57 base::CommandLine::Init(argc, argv); 58 59 const base::CommandLine& command_line = 60 *base::CommandLine::ForCurrentProcess(); 61 base::FilePath input_path = command_line.GetSwitchValuePath(kInputPath); 62 if (input_path.empty()) 63 return Help(); 64 65 bool dump_to_files = command_line.HasSwitch(kDumpToFiles); 66 67 base::FilePath output_path = command_line.GetSwitchValuePath(kOutputPath); 68 if (dump_to_files && output_path.empty()) 69 return Help(); 70 71 int version = GetMajorVersion(input_path); 72 if (!version) 73 return FILE_ACCESS_ERROR; 74 75 if (dump_to_files) { 76 net::SimpleCacheDumper dumper(input_path, output_path); 77 dumper.Run(); 78 return ALL_GOOD; 79 } 80 81 if (command_line.HasSwitch(kDumpContents)) 82 return DumpContents(input_path); 83 84 if (command_line.HasSwitch(kDumpHeaders)) 85 return DumpHeaders(input_path); 86 87 return Help(); 88 } 89