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