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 <string>
19 using namespace std;
20 
21 #include "timestamp.h"
22 
23 /// Interface for accessing the disk.
24 ///
25 /// Abstract so it can be mocked out for tests. The real implementation
26 /// is RealDiskInterface.
27 struct DiskInterface {
28  virtual ~DiskInterface() {}
29 
30  /// stat() a file, returning the mtime, or 0 if missing and -1 on
31  /// other errors.
32  virtual TimeStamp Stat(const string& path) = 0;
33 
34  /// Create a directory, returning false on failure.
35  virtual bool MakeDir(const string& path) = 0;
36 
37  /// Create a file, with the specified name and contents
38  /// Returns true on success, false on failure
39  virtual bool WriteFile(const string& path, const string& contents) = 0;
40 
41  /// Read a file to a string. Fill in |err| on error.
42  virtual string ReadFile(const string& path, string* err) = 0;
43 
44  /// Remove the file named @a path. It behaves like 'rm -f path' so no errors
45  /// are reported if it does not exists.
46  /// @returns 0 if the file has been removed,
47  /// 1 if the file does not exist, and
48  /// -1 if an error occurs.
49  virtual int RemoveFile(const string& path) = 0;
50 
51  /// Create all the parent directories for path; like mkdir -p
52  /// `basename path`.
53  bool MakeDirs(const string& path);
54 };
55 
56 /// Implementation of DiskInterface that actually hits the disk.
58  RealDiskInterface() : quiet_(false) {}
59  virtual ~RealDiskInterface() {}
60  virtual TimeStamp Stat(const string& path);
61  virtual bool MakeDir(const string& path);
62  virtual bool WriteFile(const string& path, const string& contents);
63  virtual string ReadFile(const string& path, string* err);
64  virtual int RemoveFile(const string& path);
65 
66  /// Whether to print on errors. Used to make a test quieter.
67  bool quiet_;
68 };
69 
70 #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:178
virtual ~RealDiskInterface()
bool quiet_
Whether to print on errors. Used to make a test quieter.
virtual ~DiskInterface()