diff options
author | rch@chromium.org <rch@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-28 19:48:49 +0000 |
---|---|---|
committer | rch@chromium.org <rch@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-28 19:48:49 +0000 |
commit | 753a99e15e7d714883aabefc4ac195b3be521834 (patch) | |
tree | 7ded0478ce3e49d47438cf2653a3ea24a685a9d5 /net/tools | |
parent | 17374045338851677a6bb86405bc5cb69ea5c1d0 (diff) | |
download | chromium_src-753a99e15e7d714883aabefc4ac195b3be521834.zip chromium_src-753a99e15e7d714883aabefc4ac195b3be521834.tar.gz chromium_src-753a99e15e7d714883aabefc4ac195b3be521834.tar.bz2 |
Adding a simple class for dumping the contents of a cache. Use this new class in dump_cache.
Reverted: 169769
Initially landed: 169704
Review URL: https://codereview.chromium.org/11316204
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@170024 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/tools')
-rw-r--r-- | net/tools/dump_cache/cache_dumper.cc | 22 | ||||
-rw-r--r-- | net/tools/dump_cache/cache_dumper.h | 19 | ||||
-rw-r--r-- | net/tools/dump_cache/dump_cache.cc | 59 | ||||
-rw-r--r-- | net/tools/dump_cache/dump_files.cc | 25 | ||||
-rw-r--r-- | net/tools/dump_cache/dump_files.h | 23 | ||||
-rw-r--r-- | net/tools/dump_cache/simple_cache_dumper.cc | 268 | ||||
-rw-r--r-- | net/tools/dump_cache/simple_cache_dumper.h | 94 | ||||
-rw-r--r-- | net/tools/dump_cache/upgrade_win.cc (renamed from net/tools/dump_cache/upgrade.cc) | 28 | ||||
-rw-r--r-- | net/tools/dump_cache/upgrade_win.h | 22 |
9 files changed, 500 insertions, 60 deletions
diff --git a/net/tools/dump_cache/cache_dumper.cc b/net/tools/dump_cache/cache_dumper.cc index dbe8273..7f2292b 100644 --- a/net/tools/dump_cache/cache_dumper.cc +++ b/net/tools/dump_cache/cache_dumper.cc @@ -13,6 +13,10 @@ #include "net/http/http_response_info.h" #include "net/tools/dump_cache/url_to_filename_encoder.h" +CacheDumper::CacheDumper(disk_cache::Backend* cache) + : cache_(cache) { +} + int CacheDumper::CreateEntry(const std::string& key, disk_cache::Entry** entry, const net::CompletionCallback& callback) { @@ -52,7 +56,7 @@ bool SafeCreateDirectory(const FilePath& path) { // Create the subdirectories individually while ((pos = path.value().find(backslash, pos)) != std::wstring::npos) { - std::wstring subdir = path.value().substr(0, pos); + FilePath::StringType subdir = path.value().substr(0, pos); CreateDirectoryW(subdir.c_str(), NULL); // we keep going even if directory creation failed. pos++; @@ -64,16 +68,21 @@ bool SafeCreateDirectory(const FilePath& path) { #endif } +DiskDumper::DiskDumper(const FilePath& path) + : path_(path), entry_(NULL) { + file_util::CreateDirectory(path); +} + int DiskDumper::CreateEntry(const std::string& key, disk_cache::Entry** entry, const net::CompletionCallback& callback) { // The URL may not start with a valid protocol; search for it. int urlpos = key.find("http"); std::string url = urlpos > 0 ? key.substr(urlpos) : key; - std::string base_path = WideToASCII(path_.value()); + std::string base_path = path_.MaybeAsASCII(); std::string new_path = net::UrlToFilenameEncoder::Encode(url, base_path, false); - entry_path_ = FilePath(ASCIIToWide(new_path)); + entry_path_ = FilePath::FromUTF8Unsafe(new_path); #ifdef WIN32_LARGE_FILENAME_SUPPORT // In order for long filenames to work, we'll need to prepend @@ -90,7 +99,7 @@ int DiskDumper::CreateEntry(const std::string& key, SafeCreateDirectory(entry_path_.DirName()); - std::wstring file = entry_path_.value(); + FilePath::StringType file = entry_path_.value(); #ifdef WIN32_LARGE_FILENAME_SUPPORT entry_ = CreateFileW(file.c_str(), GENERIC_WRITE|GENERIC_READ, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); @@ -185,10 +194,13 @@ int DiskDumper::WriteEntry(disk_cache::Entry* entry, int index, int offset, data = headers.c_str(); len = headers.size(); - } else if (index == 1) { // Stream 1 is the data. + } else if (index == 1) { data = buf->data(); len = buf_len; + } else { + return 0; } + #ifdef WIN32_LARGE_FILENAME_SUPPORT DWORD bytes; if (!WriteFile(entry_, data, len, &bytes, 0)) diff --git a/net/tools/dump_cache/cache_dumper.h b/net/tools/dump_cache/cache_dumper.h index 1c0c7c2..daca8fe 100644 --- a/net/tools/dump_cache/cache_dumper.h +++ b/net/tools/dump_cache/cache_dumper.h @@ -46,15 +46,15 @@ class CacheDumpWriter { // Writes data to a cache. class CacheDumper : public CacheDumpWriter { public: - explicit CacheDumper(disk_cache::Backend* cache) : cache_(cache) {} + explicit CacheDumper(disk_cache::Backend* cache); virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry, - const net::CompletionCallback& callback); + const net::CompletionCallback& callback) OVERRIDE; virtual int WriteEntry(disk_cache::Entry* entry, int stream, int offset, net::IOBuffer* buf, int buf_len, - const net::CompletionCallback& callback); + const net::CompletionCallback& callback) OVERRIDE; virtual void CloseEntry(disk_cache::Entry* entry, base::Time last_used, - base::Time last_modified); + base::Time last_modified) OVERRIDE; private: disk_cache::Backend* cache_; @@ -63,16 +63,15 @@ class CacheDumper : public CacheDumpWriter { // Writes data to a disk. class DiskDumper : public CacheDumpWriter { public: - explicit DiskDumper(const FilePath& path) : path_(path), entry_(NULL) { - file_util::CreateDirectory(path); - } + explicit DiskDumper(const FilePath& path); + virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry, - const net::CompletionCallback& callback); + const net::CompletionCallback& callback) OVERRIDE; virtual int WriteEntry(disk_cache::Entry* entry, int stream, int offset, net::IOBuffer* buf, int buf_len, - const net::CompletionCallback& callback); + const net::CompletionCallback& callback) OVERRIDE; virtual void CloseEntry(disk_cache::Entry* entry, base::Time last_used, - base::Time last_modified); + base::Time last_modified) OVERRIDE; private: FilePath path_; diff --git a/net/tools/dump_cache/dump_cache.cc b/net/tools/dump_cache/dump_cache.cc index 1b65118..adff056 100644 --- a/net/tools/dump_cache/dump_cache.cc +++ b/net/tools/dump_cache/dump_cache.cc @@ -13,8 +13,14 @@ #include "base/process_util.h" #include "base/string_util.h" #include "base/stringprintf.h" -#include "base/win/scoped_handle.h" #include "net/disk_cache/disk_format.h" +#include "net/tools/dump_cache/dump_files.h" +#include "net/tools/dump_cache/simple_cache_dumper.h" + +#if defined(OS_WIN) +#include "base/win/scoped_handle.h" +#include "net/tools/dump_cache/upgrade_win.h" +#endif enum Errors { GENERIC = -1, @@ -25,13 +31,6 @@ enum Errors { TOOL_NOT_FOUND, }; -int GetMajorVersion(const FilePath& input_path); -int DumpContents(const FilePath& input_path); -int DumpHeaders(const FilePath& input_path); -int RunSlave(const FilePath& input_path, const std::wstring& pipe_number); -int CopyCache(const FilePath& output_path, HANDLE pipe, bool copy_to_text); -HANDLE CreateServer(std::wstring* pipe_number); - const char kUpgradeHelp[] = "\nIn order to use the upgrade function, a version of this tool that\n" "understands the file format of the files to upgrade is needed. For\n" @@ -70,31 +69,6 @@ int Help() { return INVALID_ARGUMENT; } -// Starts a new process, to generate the files. -int LaunchSlave(CommandLine command_line, - const std::wstring& pipe_number, - int version) { - bool do_upgrade = command_line.HasSwitch(kUpgrade); - bool do_convert_to_text = command_line.HasSwitch(kDumpToFiles); - - if (do_upgrade) { - FilePath program(base::StringPrintf(L"%ls%d", L"dump_cache", version)); - command_line.SetProgram(program); - } - - if (do_upgrade || do_convert_to_text) - command_line.AppendSwitch(kSlave); - - command_line.AppendSwitchNative(kPipe, pipe_number); - if (!base::LaunchProcess(command_line, base::LaunchOptions(), NULL)) { - printf("Unable to launch the needed version of this tool: %ls\n", - command_line.GetProgram().value().c_str()); - printf(kUpgradeHelp); - return TOOL_NOT_FOUND; - } - return ALL_GOOD; -} - // ----------------------------------------------------------------------- int main(int argc, const char* argv[]) { @@ -109,16 +83,24 @@ int main(int argc, const char* argv[]) { return Help(); bool upgrade = false; - bool slave_required = false; bool copy_to_text = false; FilePath output_path = command_line.GetSwitchValuePath(kOutputPath); if (command_line.HasSwitch(kUpgrade)) upgrade = true; + if (command_line.HasSwitch(kDumpToFiles)) copy_to_text = true; - if (upgrade || copy_to_text) { + if (copy_to_text) { + net::SimpleCacheDumper dumper(input_path, output_path); + dumper.Run(); + return 0; + } + +#if defined(OS_WIN) + bool slave_required = false; + if (upgrade) { if (output_path.empty()) return Help(); slave_required = true; @@ -153,8 +135,10 @@ int main(int argc, const char* argv[]) { return ret; } - if (upgrade || copy_to_text) - return CopyCache(output_path, server, copy_to_text); + // TODO(rch): Remove the logic from CopyCache that is redundant with + // SimpleCacheDumper. + if (upgrade) + return CopyCache(output_path, server, false); if (slave_required) { // Wait until the slave starts dumping data before we quit. Lazy "fix" for a @@ -167,5 +151,6 @@ int main(int argc, const char* argv[]) { return DumpContents(input_path); if (command_line.HasSwitch(kDumpHeaders)) return DumpHeaders(input_path); +#endif return Help(); } diff --git a/net/tools/dump_cache/dump_files.cc b/net/tools/dump_cache/dump_files.cc index 26a11c3..3ce890a 100644 --- a/net/tools/dump_cache/dump_files.cc +++ b/net/tools/dump_cache/dump_files.cc @@ -6,6 +6,8 @@ // to the actual files (they still may change if an error is detected on the // files). +#include "net/tools/dump_cache/dump_files.h" + #include <stdio.h> #include <set> @@ -22,20 +24,20 @@ namespace { -const wchar_t kIndexName[] = L"index"; +const FilePath::CharType kIndexName[] = FILE_PATH_LITERAL("index"); // Reads the |header_size| bytes from the beginning of file |name|. bool ReadHeader(const FilePath& name, char* header, int header_size) { net::FileStream file(NULL); file.OpenSync(name, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ); if (!file.IsOpen()) { - printf("Unable to open file %ls\n", name.value().c_str()); + printf("Unable to open file %s\n", name.MaybeAsASCII().c_str()); return false; } int read = file.ReadSync(header, header_size); if (read != header_size) { - printf("Unable to read file %ls\n", name.value().c_str()); + printf("Unable to read file %s\n", name.MaybeAsASCII().c_str()); return false; } return true; @@ -82,7 +84,7 @@ void DumpBlockHeader(const FilePath& name) { if (!ReadHeader(name, reinterpret_cast<char*>(&header), sizeof(header))) return; - printf("Block file: %ls\n", name.BaseName().value().c_str()); + printf("Block file: %s\n", name.BaseName().MaybeAsASCII().c_str()); printf("magic: %x\n", header.magic); printf("version: %d.%d\n", header.version >> 16, header.version & 0xffff); printf("file id: %d\n", header.this_file); @@ -268,11 +270,19 @@ int GetMajorVersion(const FilePath& input_path) { if (!version) return 0; - FilePath data_name(input_path.Append(L"data_0")); + FilePath data_name(input_path.Append(FILE_PATH_LITERAL("data_0"))); + if (version != GetMajorVersionFromFile(data_name)) + return 0; + + data_name = input_path.Append(FILE_PATH_LITERAL("data_1")); + if (version != GetMajorVersionFromFile(data_name)) + return 0; + + data_name = input_path.Append(FILE_PATH_LITERAL("data_2")); if (version != GetMajorVersionFromFile(data_name)) return 0; - data_name = input_path.Append(L"data_1"); + data_name = input_path.Append(FILE_PATH_LITERAL("data_3")); if (version != GetMajorVersionFromFile(data_name)) return 0; @@ -285,7 +295,8 @@ int DumpHeaders(const FilePath& input_path) { DumpIndexHeader(index_name); file_util::FileEnumerator iter(input_path, false, - file_util::FileEnumerator::FILES, L"data_*"); + file_util::FileEnumerator::FILES, + FILE_PATH_LITERAL("data_*")); for (FilePath file = iter.Next(); !file.empty(); file = iter.Next()) DumpBlockHeader(file); return 0; diff --git a/net/tools/dump_cache/dump_files.h b/net/tools/dump_cache/dump_files.h new file mode 100644 index 0000000..abc8b5b --- /dev/null +++ b/net/tools/dump_cache/dump_files.h @@ -0,0 +1,23 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef NET_TOOLS_DUMP_CACHE_DUMP_FILES_H_ +#define NET_TOOLS_DUMP_CACHE_DUMP_FILE_H_ + +// Performs basic inspection of the disk cache files with minimal disruption +// to the actual files (they still may change if an error is detected on the +// files). + +#include "base/file_path.h" + +// Returns the major version of the specified cache. +int GetMajorVersion(const FilePath& input_path); + +// Dumps all entries from the cache. +int DumpContents(const FilePath& input_path); + +// Dumps the headers of all files. +int DumpHeaders(const FilePath& input_path); + +#endif // NET_TOOLS_DUMP_CACHE_DUMP_FILES_H_ diff --git a/net/tools/dump_cache/simple_cache_dumper.cc b/net/tools/dump_cache/simple_cache_dumper.cc new file mode 100644 index 0000000..73c3cd0 --- /dev/null +++ b/net/tools/dump_cache/simple_cache_dumper.cc @@ -0,0 +1,268 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/tools/dump_cache/simple_cache_dumper.h" + +#include "base/at_exit.h" +#include "base/command_line.h" +#include "base/logging.h" +#include "base/memory/scoped_ptr.h" +#include "base/message_loop.h" +#include "base/message_loop_proxy.h" +#include "base/threading/thread.h" +#include "net/base/io_buffer.h" +#include "net/base/net_errors.h" +#include "net/disk_cache/disk_cache.h" +#include "net/tools/dump_cache/cache_dumper.h" + +namespace net { + +SimpleCacheDumper::SimpleCacheDumper(FilePath input_path, FilePath output_path) + : state_(STATE_NONE), + input_path_(input_path), + output_path_(output_path), + cache_(NULL), + writer_(new DiskDumper(output_path)), + cache_thread_(new base::Thread("CacheThead")), + iter_(NULL), + src_entry_(NULL), + dst_entry_(NULL), + io_callback_(base::Bind(&SimpleCacheDumper::OnIOComplete, + base::Unretained(this))), + rv_(0) { +} + +SimpleCacheDumper::~SimpleCacheDumper() { + delete(cache_); +} + +int SimpleCacheDumper::Run() { + MessageLoopForIO main_message_loop; + + LOG(INFO) << "Reading cache from: " << input_path_.value(); + LOG(INFO) << "Writing cache to: " << output_path_.value(); + + if (!cache_thread_->StartWithOptions( + base::Thread::Options(MessageLoop::TYPE_IO, 0))) { + LOG(ERROR) << "Unable to start thread"; + return ERR_UNEXPECTED; + } + state_ = STATE_CREATE_CACHE; + int rv = DoLoop(OK); + if (rv == ERR_IO_PENDING) { + main_message_loop.Run(); + return rv_; + } + return rv; +} + +int SimpleCacheDumper::DoLoop(int rv) { + do { + State state = state_; + state_ = STATE_NONE; + switch (state) { + case STATE_CREATE_CACHE: + CHECK_EQ(OK, rv); + rv = DoCreateCache(); + break; + case STATE_CREATE_CACHE_COMPLETE: + rv = DoCreateCacheComplete(rv); + break; + case STATE_OPEN_ENTRY: + CHECK_EQ(OK, rv); + rv = DoOpenEntry(); + break; + case STATE_OPEN_ENTRY_COMPLETE: + rv = DoOpenEntryComplete(rv); + break; + case STATE_CREATE_ENTRY: + CHECK_EQ(OK, rv); + rv = DoCreateEntry(); + break; + case STATE_CREATE_ENTRY_COMPLETE: + rv = DoCreateEntryComplete(rv); + break; + case STATE_READ_HEADERS: + CHECK_EQ(OK, rv); + rv = DoReadHeaders(); + break; + case STATE_READ_HEADERS_COMPLETE: + rv = DoReadHeadersComplete(rv); + break; + case STATE_WRITE_HEADERS: + CHECK_EQ(OK, rv); + rv = DoWriteHeaders(); + break; + case STATE_WRITE_HEADERS_COMPLETE: + rv = DoWriteHeadersComplete(rv); + break; + case STATE_READ_BODY: + CHECK_EQ(OK, rv); + rv = DoReadBody(); + break; + case STATE_READ_BODY_COMPLETE: + rv = DoReadBodyComplete(rv); + break; + case STATE_WRITE_BODY: + CHECK_EQ(OK, rv); + rv = DoWriteBody(); + break; + case STATE_WRITE_BODY_COMPLETE: + rv = DoWriteBodyComplete(rv); + break; + default: + NOTREACHED() << "state_: " << state_; + break; + } + } while (state_ != STATE_NONE && rv != ERR_IO_PENDING); + return rv; +} + +int SimpleCacheDumper::DoCreateCache() { + DCHECK(!cache_); + state_ = STATE_CREATE_CACHE_COMPLETE; + return disk_cache::CreateCacheBackend( + DISK_CACHE, input_path_, 0, false, + cache_thread_->message_loop_proxy(), + NULL, &cache_, io_callback_); +} + +int SimpleCacheDumper::DoCreateCacheComplete(int rv) { + if (rv < 0) + return rv; + + reinterpret_cast<disk_cache::BackendImpl*>(cache_)->SetUpgradeMode(); + reinterpret_cast<disk_cache::BackendImpl*>(cache_)->SetFlags( + disk_cache::kNoRandom); + + state_ = STATE_OPEN_ENTRY; + return OK; +} + +int SimpleCacheDumper::DoOpenEntry() { + DCHECK(!dst_entry_); + DCHECK(!src_entry_); + state_ = STATE_OPEN_ENTRY_COMPLETE; + return cache_->OpenNextEntry(&iter_, &src_entry_, io_callback_); +} + +int SimpleCacheDumper::DoOpenEntryComplete(int rv) { + // ERR_FAILED indicates iteration finished. + if (rv == ERR_FAILED) { + cache_->EndEnumeration(&iter_); + return OK; + } + + if (rv < 0) + return rv; + + state_ = STATE_CREATE_ENTRY; + return OK; +} + +int SimpleCacheDumper::DoCreateEntry() { + DCHECK(!dst_entry_); + state_ = STATE_CREATE_ENTRY_COMPLETE; + + return writer_->CreateEntry(src_entry_->GetKey(), &dst_entry_, + io_callback_); +} + +int SimpleCacheDumper::DoCreateEntryComplete(int rv) { + if (rv < 0) + return rv; + + state_ = STATE_READ_HEADERS; + return OK; +} + +int SimpleCacheDumper::DoReadHeaders() { + state_ = STATE_READ_HEADERS_COMPLETE; + int32 size = src_entry_->GetDataSize(0); + buf_ = new IOBufferWithSize(size); + return src_entry_->ReadData(0, 0, buf_, size, io_callback_); +} + +int SimpleCacheDumper::DoReadHeadersComplete(int rv) { + if (rv < 0) + return rv; + + state_ = STATE_WRITE_HEADERS; + return OK; +} + +int SimpleCacheDumper::DoWriteHeaders() { + int rv = writer_->WriteEntry(dst_entry_, 0, 0, buf_, buf_->size(), + io_callback_); + if (rv == 0) + return ERR_FAILED; + + state_ = STATE_WRITE_HEADERS_COMPLETE; + return OK; +} + +int SimpleCacheDumper::DoWriteHeadersComplete(int rv) { + if (rv < 0) + return rv; + + state_ = STATE_READ_BODY; + return OK; +} + +int SimpleCacheDumper::DoReadBody() { + state_ = STATE_READ_BODY_COMPLETE; + int32 size = src_entry_->GetDataSize(1); + // If the body is empty, we can neither read nor write it, so + // just move to the next. + if (size <= 0) { + state_ = STATE_WRITE_BODY_COMPLETE; + return OK; + } + buf_ = new IOBufferWithSize(size); + return src_entry_->ReadData(1, 0, buf_, size, io_callback_); +} + +int SimpleCacheDumper::DoReadBodyComplete(int rv) { + if (rv < 0) + return rv; + + state_ = STATE_WRITE_BODY; + return OK; +} + +int SimpleCacheDumper::DoWriteBody() { + int rv = writer_->WriteEntry(dst_entry_, 1, 0, buf_, buf_->size(), + io_callback_); + if (rv == 0) + return ERR_FAILED; + + state_ = STATE_WRITE_BODY_COMPLETE; + return OK; +} + +int SimpleCacheDumper::DoWriteBodyComplete(int rv) { + if (rv < 0) + return rv; + + src_entry_->Close(); + writer_->CloseEntry(dst_entry_, base::Time::Now(), base::Time::Now()); + src_entry_ = NULL; + dst_entry_ = NULL; + + state_ = STATE_OPEN_ENTRY; + return OK; +} + +void SimpleCacheDumper::OnIOComplete(int rv) { + rv = DoLoop(rv); + + if (rv != ERR_IO_PENDING) { + rv_ = rv; + delete cache_; + cache_ = NULL; + MessageLoop::current()->Quit(); + } +} + +} // namespace net diff --git a/net/tools/dump_cache/simple_cache_dumper.h b/net/tools/dump_cache/simple_cache_dumper.h new file mode 100644 index 0000000..4a76529 --- /dev/null +++ b/net/tools/dump_cache/simple_cache_dumper.h @@ -0,0 +1,94 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef NET_TOOLS_DUMP_CACHE_SIMPLE_CACHE_DUMPER_H_ +#define NET_TOOLS_DUMP_CACHE_SIMPLE_CACHE_DUMPER_H_ + +#include "base/file_path.h" +#include "base/memory/scoped_ptr.h" +#include "base/threading/thread.h" +#include "net/base/completion_callback.h" + +class DiskDumper; + +namespace disk_cache { +class Backend; +class Entry; +} // namespace disk_cache + +namespace net { + +class IOBufferWithSize; + +// A class for dumping the contents of a disk cache to a series of text +// files. The files will contain the response headers, followed by the +// response body, as if the HTTP response were written directly to disk. +class SimpleCacheDumper { + public: + SimpleCacheDumper(FilePath input_path, FilePath output_path); + ~SimpleCacheDumper(); + + // Dumps the cache to disk. Returns OK if the operation was successful, + // and a net error code otherwise. + int Run(); + + private: + enum State { + STATE_NONE, + STATE_CREATE_CACHE, + STATE_CREATE_CACHE_COMPLETE, + STATE_OPEN_ENTRY, + STATE_OPEN_ENTRY_COMPLETE, + STATE_CREATE_ENTRY, + STATE_CREATE_ENTRY_COMPLETE, + STATE_READ_HEADERS, + STATE_READ_HEADERS_COMPLETE, + STATE_WRITE_HEADERS, + STATE_WRITE_HEADERS_COMPLETE, + STATE_READ_BODY, + STATE_READ_BODY_COMPLETE, + STATE_WRITE_BODY, + STATE_WRITE_BODY_COMPLETE, + STATE_DONE, + }; + + int DoLoop(int rv); + + int DoCreateCache(); + int DoCreateCacheComplete(int rv); + int DoOpenEntry(); + int DoOpenEntryComplete(int rv); + int DoCreateEntry(); + int DoCreateEntryComplete(int rv); + int DoReadHeaders(); + int DoReadHeadersComplete(int rv); + int DoWriteHeaders(); + int DoWriteHeadersComplete(int rv); + int DoReadBody(); + int DoReadBodyComplete(int rv); + int DoWriteBody(); + int DoWriteBodyComplete(int rv); + int DoDone(); + + void OnIOComplete(int rv); + + State state_; + FilePath input_path_; + FilePath output_path_; + disk_cache::Backend* cache_; + scoped_ptr<DiskDumper> writer_; + base::Thread* cache_thread_; + void* iter_; + disk_cache::Entry* src_entry_; + disk_cache::Entry* dst_entry_; + CompletionCallback io_callback_; + scoped_refptr<IOBufferWithSize> buf_; + int rv_; + + DISALLOW_COPY_AND_ASSIGN(SimpleCacheDumper); +}; + +} // namespace net + +#endif // NET_TOOLS_DUMP_CACHE_SIMPLE_CACHE_DUMPER_H_ diff --git a/net/tools/dump_cache/upgrade.cc b/net/tools/dump_cache/upgrade_win.cc index 34bc174..df5107c 100644 --- a/net/tools/dump_cache/upgrade.cc +++ b/net/tools/dump_cache/upgrade_win.cc @@ -2,9 +2,10 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "net/tools/dump_cache/upgrade_win.h" + #include "base/bind.h" #include "base/bind_helpers.h" -#include "base/file_path.h" #include "base/logging.h" #include "base/memory/scoped_ptr.h" #include "base/message_loop.h" @@ -920,3 +921,28 @@ int RunSlave(const FilePath& input_path, const std::wstring& pipe_number) { loop.Run(); return 0; } + +// Starts a new process, to generate the files. +int LaunchSlave(CommandLine command_line, + const std::wstring& pipe_number, + int version) { + bool do_upgrade = command_line.HasSwitch(kUpgrade); + bool do_convert_to_text = command_line.HasSwitch(kDumpToFiles); + + if (do_upgrade) { + FilePath program(base::StringPrintf(L"%ls%d", L"dump_cache", version)); + command_line.SetProgram(program); + } + + if (do_upgrade || do_convert_to_text) + command_line.AppendSwitch(kSlave); + + command_line.AppendSwitchNative(kPipe, pipe_number); + if (!base::LaunchProcess(command_line, base::LaunchOptions(), NULL)) { + printf("Unable to launch the needed version of this tool: %ls\n", + command_line.GetProgram().value().c_str()); + printf(kUpgradeHelp); + return TOOL_NOT_FOUND; + } + return ALL_GOOD; +} diff --git a/net/tools/dump_cache/upgrade_win.h b/net/tools/dump_cache/upgrade_win.h new file mode 100644 index 0000000..3384707 --- /dev/null +++ b/net/tools/dump_cache/upgrade_win.h @@ -0,0 +1,22 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <windows.h> + +#include "base/command_line.h" +#include "base/file_path.h" + +// Creates a new server, and returns a new named pipe to communicate with it. +HANDLE CreateServer(std::wstring* pipe_number); + +// This is the controller process for an upgrade operation. +int CopyCache(const FilePath& output_path, HANDLE pipe, bool copy_to_text); + +// This process will only execute commands from the controller. +int RunSlave(const FilePath& input_path, const std::wstring& pipe_number); + +// Starts a new process, to generate the files. +int LaunchSlave(CommandLine command_line, + const std::wstring& pipe_number, + int version); |