• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2010 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 #ifndef NET_TOOLS_DUMP_CACHE_CACHE_DUMPER_H_
6 #define NET_TOOLS_DUMP_CACHE_CACHE_DUMPER_H_
7 #pragma once
8 
9 #include <string>
10 #include "base/file_path.h"
11 #include "base/file_util.h"
12 #include "net/disk_cache/backend_impl.h"
13 
14 #ifdef WIN32
15 // Dumping the cache often creates very large filenames, which are tricky
16 // on windows.  Most API calls don't support large filenames, including
17 // most of the base library functions.  Unfortunately, adding "\\?\" into
18 // the filename support is tricky.  Instead, if WIN32_LARGE_FILENAME_SUPPORT
19 // is set, we use direct WIN32 APIs for manipulating the files.
20 #define WIN32_LARGE_FILENAME_SUPPORT
21 #endif
22 
23 // An abstract class for writing cache dump data.
24 class CacheDumpWriter {
25  public:
~CacheDumpWriter()26   virtual ~CacheDumpWriter() {}
27 
28   // Creates an entry to be written.
29   // On success, populates the |entry|.
30   // Returns a net error code.
31   virtual int CreateEntry(const std::string& key,
32                           disk_cache::Entry** entry,
33                           net::CompletionCallback* callback) = 0;
34 
35   // Write to the current entry.
36   // Returns a net error code.
37   virtual int WriteEntry(disk_cache::Entry* entry, int stream, int offset,
38                          net::IOBuffer* buf, int buf_len,
39                          net::CompletionCallback* callback) = 0;
40 
41   // Close the current entry.
42   virtual void CloseEntry(disk_cache::Entry* entry, base::Time last_used,
43                           base::Time last_modified) = 0;
44 };
45 
46 // Writes data to a cache.
47 class CacheDumper : public CacheDumpWriter {
48  public:
CacheDumper(disk_cache::Backend * cache)49   explicit CacheDumper(disk_cache::Backend* cache) : cache_(cache) {}
50 
51   virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry,
52                           net::CompletionCallback* callback);
53   virtual int WriteEntry(disk_cache::Entry* entry, int stream, int offset,
54                          net::IOBuffer* buf, int buf_len,
55                          net::CompletionCallback* callback);
56   virtual void CloseEntry(disk_cache::Entry* entry, base::Time last_used,
57                           base::Time last_modified);
58 
59  private:
60   disk_cache::Backend* cache_;
61 };
62 
63 // Writes data to a disk.
64 class DiskDumper : public CacheDumpWriter {
65  public:
DiskDumper(const std::wstring & path)66   explicit DiskDumper(const std::wstring& path) : path_(path), entry_(NULL) {
67     file_util::CreateDirectory(FilePath(path));
68   }
69   virtual int CreateEntry(const std::string& key, disk_cache::Entry** entry,
70                           net::CompletionCallback* callback);
71   virtual int WriteEntry(disk_cache::Entry* entry, int stream, int offset,
72                          net::IOBuffer* buf, int buf_len,
73                          net::CompletionCallback* callback);
74   virtual void CloseEntry(disk_cache::Entry* entry, base::Time last_used,
75                           base::Time last_modified);
76 
77  private:
78   std::wstring path_;
79   // This is a bit of a hack.  As we get a CreateEntry, we coin the current
80   // entry_path_ where we write that entry to disk.  Subsequent calls to
81   // WriteEntry() utilize this path for writing to disk.
82   FilePath entry_path_;
83   std::string entry_url_;
84 #ifdef WIN32_LARGE_FILENAME_SUPPORT
85   HANDLE entry_;
86 #else
87   FILE* entry_;
88 #endif
89 };
90 
91 #endif  // NET_TOOLS_DUMP_CACHE_CACHE_DUMPER_H_
92