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
21#include "timestamp.h"
22
23/// Interface for reading files from disk. See DiskInterface for details.
24/// This base offers the minimum interface needed just to read files.
25struct FileReader {
26 virtual ~FileReader() {}
27
28 /// Result of ReadFile.
34
35 /// Read and store in given string. On success, return Okay.
36 /// On error, return another Status and fill |err|.
37 virtual Status ReadFile(const std::string& path, std::string* contents,
38 std::string* err) = 0;
39};
40
41/// Interface for accessing the disk.
42///
43/// Abstract so it can be mocked out for tests. The real implementation
44/// is RealDiskInterface.
45struct DiskInterface: public FileReader {
46 /// stat() a file, returning the mtime, or 0 if missing and -1 on
47 /// other errors.
48 virtual TimeStamp Stat(const std::string& path, std::string* err) const = 0;
49
50 /// Create a directory, returning false on failure.
51 virtual bool MakeDir(const std::string& path) = 0;
52
53 /// Create a file, with the specified name and contents
54 /// If \a crlf_on_windows is true, \n will be converted to \r\n (only on
55 /// Windows builds of Ninja).
56 /// Returns true on success, false on failure
57 virtual bool WriteFile(const std::string& path, const std::string& contents,
58 bool crlf_on_windows) = 0;
59
60 /// Remove the file named @a path. It behaves like 'rm -f path' so no errors
61 /// are reported if it does not exists.
62 /// @returns 0 if the file has been removed,
63 /// 1 if the file does not exist, and
64 /// -1 if an error occurs.
65 virtual int RemoveFile(const std::string& path) = 0;
66
67 /// Create all the parent directories for path; like mkdir -p
68 /// `basename path`.
69 bool MakeDirs(const std::string& path);
70};
71
72/// Implementation of DiskInterface that actually hits the disk.
75 virtual ~RealDiskInterface() {}
76 TimeStamp Stat(const std::string& path, std::string* err) const override;
77 bool MakeDir(const std::string& path) override;
78 bool WriteFile(const std::string& path, const std::string& contents,
79 bool crlf_on_windows) override;
80 Status ReadFile(const std::string& path, std::string* contents,
81 std::string* err) override;
82 int RemoveFile(const std::string& path) override;
83
84 /// Whether stat information can be cached. Only has an effect on Windows.
85 void AllowStatCache(bool allow);
86
87#ifdef _WIN32
88 /// Whether long paths are enabled. Only has an effect on Windows.
89 bool AreLongPathsEnabled() const;
90#endif
91
92 private:
93#ifdef _WIN32
94 /// Whether stat information can be cached.
95 bool use_cache_;
96
97 /// Whether long paths are enabled.
98 bool long_paths_enabled_;
99
100 typedef std::map<std::string, TimeStamp> DirCache;
101 // TODO: Neither a map nor a hashmap seems ideal here. If the statcache
102 // works out, come up with a better data structure.
103 typedef std::map<std::string, DirCache> Cache;
104 mutable Cache cache_;
105#endif
106};
107
108#endif // NINJA_DISK_INTERFACE_H_
Interface for accessing the disk.
virtual bool WriteFile(const std::string &path, const std::string &contents, bool crlf_on_windows)=0
Create a file, with the specified name and contents If crlf_on_windows is true, will be converted t...
virtual bool MakeDir(const std::string &path)=0
Create a directory, returning false on failure.
bool MakeDirs(const std::string &path)
Create all the parent directories for path; like mkdir -p basename path.
virtual int RemoveFile(const std::string &path)=0
Remove the file named path.
virtual TimeStamp Stat(const std::string &path, std::string *err) const =0
stat() a file, returning the mtime, or 0 if missing and -1 on other errors.
Interface for reading files from disk.
virtual ~FileReader()
Status
Result of ReadFile.
virtual Status ReadFile(const std::string &path, std::string *contents, std::string *err)=0
Read and store in given string.
Status ReadFile(const std::string &path, std::string *contents, std::string *err) override
Read and store in given string.
void AllowStatCache(bool allow)
Whether stat information can be cached. Only has an effect on Windows.
bool WriteFile(const std::string &path, const std::string &contents, bool crlf_on_windows) override
Create a file, with the specified name and contents If crlf_on_windows is true, will be converted t...
virtual ~RealDiskInterface()
int RemoveFile(const std::string &path) override
Remove the file named path.
TimeStamp Stat(const std::string &path, std::string *err) const override
stat() a file, returning the mtime, or 0 if missing and -1 on other errors.
bool MakeDir(const std::string &path) override
Create a directory, returning false on failure.
Abstract interface to object that tracks the status of a build: completion fraction,...
Definition status.h:27
int64_t TimeStamp
Definition timestamp.h:31