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