Home | History | Annotate | Download | only in dump_cache
      1 // Copyright (c) 2011 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 
     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 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 // Starts a new process, to generate the files.
     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 = base::StringPrintf(L"%ls%d", L"dump_cache_", version);
     88   else
     89     new_program = base::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   // TODO(evanm): remove needless usage of wstring from here and elsewhere.
     99   new_command_line.AppendSwitchASCII(kPipe, WideToASCII(pipe_number));
    100   if (!base::LaunchApp(new_command_line, false, false, NULL)) {
    101     printf("Unable to launch the needed version of this tool: %ls\n",
    102            new_program.c_str());
    103     printf(kUpgradeHelp);
    104     return TOOL_NOT_FOUND;
    105   }
    106   return ALL_GOOD;
    107 }
    108 
    109 // -----------------------------------------------------------------------
    110 
    111 int main(int argc, const char* argv[]) {
    112   // Setup an AtExitManager so Singleton objects will be destroyed.
    113   base::AtExitManager at_exit_manager;
    114 
    115   CommandLine::Init(argc, argv);
    116 
    117   const CommandLine& command_line = *CommandLine::ForCurrentProcess();
    118   std::wstring input_path = command_line.GetSwitchValueNative(kInputPath);
    119   if (input_path.empty())
    120     return Help();
    121 
    122   bool upgrade = false;
    123   bool slave_required = false;
    124   bool copy_to_text = false;
    125   // TODO(evanm): port to FilePath.
    126   std::wstring output_path = command_line.GetSwitchValueNative(kOutputPath);
    127   // Make sure that output directory ends with a slash.
    128   if (output_path.size() >= 1 && output_path[output_path.size() - 1] != '\\')
    129     output_path.push_back('\\');
    130 
    131   if (command_line.HasSwitch(kUpgrade))
    132     upgrade = true;
    133   if (command_line.HasSwitch(kDumpToFiles))
    134     copy_to_text = true;
    135 
    136   if (upgrade || copy_to_text) {
    137     if (output_path.empty())
    138       return Help();
    139     slave_required = true;
    140   }
    141 
    142   int version = GetMajorVersion(input_path);
    143   if (!version)
    144     return FILE_ACCESS_ERROR;
    145 
    146   if (version != disk_cache::kCurrentVersion >> 16) {
    147     if (command_line.HasSwitch(kSlave)) {
    148       printf("Unknown version\n");
    149       return UNKNOWN_VERSION;
    150     }
    151     slave_required = true;
    152   }
    153 
    154   std::wstring pipe_number = command_line.GetSwitchValueNative(kPipe);
    155   if (command_line.HasSwitch(kSlave) && slave_required)
    156     return RunSlave(input_path, pipe_number);
    157 
    158   base::win::ScopedHandle server;
    159   if (slave_required) {
    160     server.Set(CreateServer(&pipe_number));
    161     if (!server.IsValid()) {
    162       printf("Unable to create the server pipe\n");
    163       return -1;
    164     }
    165 
    166     int ret = LaunchSlave(command_line, pipe_number, version);
    167     if (ret)
    168       return ret;
    169   }
    170 
    171   if (upgrade || copy_to_text)
    172     return CopyCache(output_path, server, copy_to_text);
    173 
    174   if (slave_required) {
    175     // Wait until the slave starts dumping data before we quit. Lazy "fix" for a
    176     // console quirk.
    177     Sleep(500);
    178     return ALL_GOOD;
    179   }
    180 
    181   if (command_line.HasSwitch(kDumpContents))
    182     return DumpContents(input_path);
    183   if (command_line.HasSwitch(kDumpHeaders))
    184     return DumpHeaders(input_path);
    185   return Help();
    186 }
    187