Ninja
disk_interface.h
Go to the documentation of this file.
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef NINJA_DISK_INTERFACE_H_
16 #define NINJA_DISK_INTERFACE_H_
17 
18 #include <map>
19 #include <string>
20 using namespace std;
21 
22 #include "timestamp.h"
23 
24 /// Interface for accessing the disk.
25 ///
26 /// Abstract so it can be mocked out for tests. The real implementation
27 /// is RealDiskInterface.
28 struct DiskInterface {
29  virtual ~DiskInterface() {}
30 
31  /// stat() a file, returning the mtime, or 0 if missing and -1 on
32  /// other errors.
33  virtual TimeStamp Stat(const string& path) const = 0;
34 
35  /// Create a directory, returning false on failure.
36  virtual bool MakeDir(const string& path) = 0;
37 
38  /// Create a file, with the specified name and contents
39  /// Returns true on success, false on failure
40  virtual bool WriteFile(const string& path, const string& contents) = 0;
41 
42  /// Read a file to a string. Fill in |err| on error.
43  virtual string ReadFile(const string& path, string* err) = 0;
44 
45  /// Remove the file named @a path. It behaves like 'rm -f path' so no errors
46  /// are reported if it does not exists.
47  /// @returns 0 if the file has been removed,
48  /// 1 if the file does not exist, and
49  /// -1 if an error occurs.
50  virtual int RemoveFile(const string& path) = 0;
51 
52  /// Create all the parent directories for path; like mkdir -p
53  /// `basename path`.
54  bool MakeDirs(const string& path);
55 };
56 
57 /// Implementation of DiskInterface that actually hits the disk.
59  RealDiskInterface() : quiet_(false)
60 #ifdef _WIN32
61  , use_cache_(false)
62 #endif
63  {}
64  virtual ~RealDiskInterface() {}
65  virtual TimeStamp Stat(const string& path) const;
66  virtual bool MakeDir(const string& path);
67  virtual bool WriteFile(const string& path, const string& contents);
68  virtual string ReadFile(const string& path, string* err);
69  virtual int RemoveFile(const string& path);
70 
71  /// Whether to print on errors. Used to make a test quieter.
72  bool quiet_;
73 
74  /// Whether stat information can be cached. Only has an effect on Windows.
75  void AllowStatCache(bool allow);
76 
77  private:
78 #ifdef _WIN32
79  /// Whether stat information can be cached.
80  bool use_cache_;
81 
82  typedef map<string, TimeStamp> DirCache;
83  // TODO: Neither a map nor a hashmap seems ideal here. If the statcache
84  // works out, come up with a better data structure.
85  typedef map<string, DirCache> Cache;
86  mutable Cache cache_;
87 #endif
88 };
89 
90 #endif // NINJA_DISK_INTERFACE_H_
Interface for accessing the disk.
int TimeStamp
Definition: timestamp.h:22
Implementation of DiskInterface that actually hits the disk.
int ReadFile(const string &path, string *contents, string *err)
Read a file to a string (in text mode: with CRLF conversion on Windows).
Definition: util.cc:282
virtual ~RealDiskInterface()
bool quiet_
Whether to print on errors. Used to make a test quieter.
virtual ~DiskInterface()