1695b41eeSopenharmony_ci// Copyright 2011 Google Inc. All Rights Reserved.
2695b41eeSopenharmony_ci//
3695b41eeSopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
4695b41eeSopenharmony_ci// you may not use this file except in compliance with the License.
5695b41eeSopenharmony_ci// You may obtain a copy of the License at
6695b41eeSopenharmony_ci//
7695b41eeSopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
8695b41eeSopenharmony_ci//
9695b41eeSopenharmony_ci// Unless required by applicable law or agreed to in writing, software
10695b41eeSopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
11695b41eeSopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12695b41eeSopenharmony_ci// See the License for the specific language governing permissions and
13695b41eeSopenharmony_ci// limitations under the License.
14695b41eeSopenharmony_ci
15695b41eeSopenharmony_ci#ifndef NINJA_DISK_INTERFACE_H_
16695b41eeSopenharmony_ci#define NINJA_DISK_INTERFACE_H_
17695b41eeSopenharmony_ci
18695b41eeSopenharmony_ci#include <map>
19695b41eeSopenharmony_ci#include <string>
20695b41eeSopenharmony_ci
21695b41eeSopenharmony_ci#include "timestamp.h"
22695b41eeSopenharmony_ci
23695b41eeSopenharmony_ci/// Interface for reading files from disk.  See DiskInterface for details.
24695b41eeSopenharmony_ci/// This base offers the minimum interface needed just to read files.
25695b41eeSopenharmony_cistruct FileReader {
26695b41eeSopenharmony_ci    virtual ~FileReader() {}
27695b41eeSopenharmony_ci
28695b41eeSopenharmony_ci    /// Result of ReadFile.
29695b41eeSopenharmony_ci    enum Status {
30695b41eeSopenharmony_ci        Okay,
31695b41eeSopenharmony_ci        NotFound,
32695b41eeSopenharmony_ci        OtherError
33695b41eeSopenharmony_ci    };
34695b41eeSopenharmony_ci
35695b41eeSopenharmony_ci    /// Read and store in given string.  On success, return Okay.
36695b41eeSopenharmony_ci    /// On error, return another Status and fill |err|.
37695b41eeSopenharmony_ci    virtual Status ReadFile(const std::string& path, std::string* contents,
38695b41eeSopenharmony_ci                                                    std::string* err) = 0;
39695b41eeSopenharmony_ci};
40695b41eeSopenharmony_ci
41695b41eeSopenharmony_ci/// Interface for accessing the disk.
42695b41eeSopenharmony_ci///
43695b41eeSopenharmony_ci/// Abstract so it can be mocked out for tests.  The real implementation
44695b41eeSopenharmony_ci/// is RealDiskInterface.
45695b41eeSopenharmony_cistruct DiskInterface: public FileReader {
46695b41eeSopenharmony_ci    /// stat() a file, returning the mtime, or 0 if missing and -1 on
47695b41eeSopenharmony_ci    /// other errors.
48695b41eeSopenharmony_ci    virtual TimeStamp Stat(const std::string& path, std::string* err) const = 0;
49695b41eeSopenharmony_ci
50695b41eeSopenharmony_ci    /// Create a directory, returning false on failure.
51695b41eeSopenharmony_ci    virtual bool MakeDir(const std::string& path) = 0;
52695b41eeSopenharmony_ci
53695b41eeSopenharmony_ci    /// Create a file, with the specified name and contents
54695b41eeSopenharmony_ci    /// Returns true on success, false on failure
55695b41eeSopenharmony_ci    virtual bool WriteFile(const std::string& path,
56695b41eeSopenharmony_ci                                                  const std::string& contents) = 0;
57695b41eeSopenharmony_ci
58695b41eeSopenharmony_ci    /// Remove the file named @a path. It behaves like 'rm -f path' so no errors
59695b41eeSopenharmony_ci    /// are reported if it does not exists.
60695b41eeSopenharmony_ci    /// @returns 0 if the file has been removed,
61695b41eeSopenharmony_ci    ///          1 if the file does not exist, and
62695b41eeSopenharmony_ci    ///          -1 if an error occurs.
63695b41eeSopenharmony_ci    virtual int RemoveFile(const std::string& path) = 0;
64695b41eeSopenharmony_ci
65695b41eeSopenharmony_ci    /// Create all the parent directories for path; like mkdir -p
66695b41eeSopenharmony_ci    /// `basename path`.
67695b41eeSopenharmony_ci    bool MakeDirs(const std::string& path);
68695b41eeSopenharmony_ci};
69695b41eeSopenharmony_ci
70695b41eeSopenharmony_ci/// Implementation of DiskInterface that actually hits the disk.
71695b41eeSopenharmony_cistruct RealDiskInterface : public DiskInterface {
72695b41eeSopenharmony_ci    RealDiskInterface();
73695b41eeSopenharmony_ci    virtual ~RealDiskInterface() {}
74695b41eeSopenharmony_ci    virtual TimeStamp Stat(const std::string& path, std::string* err) const;
75695b41eeSopenharmony_ci    virtual bool MakeDir(const std::string& path);
76695b41eeSopenharmony_ci    virtual bool WriteFile(const std::string& path, const std::string& contents);
77695b41eeSopenharmony_ci    virtual Status ReadFile(const std::string& path, std::string* contents,
78695b41eeSopenharmony_ci                                                    std::string* err);
79695b41eeSopenharmony_ci    virtual int RemoveFile(const std::string& path);
80695b41eeSopenharmony_ci
81695b41eeSopenharmony_ci    /// Whether stat information can be cached.  Only has an effect on Windows.
82695b41eeSopenharmony_ci    void AllowStatCache(bool allow);
83695b41eeSopenharmony_ci
84695b41eeSopenharmony_ci#ifdef _WIN32
85695b41eeSopenharmony_ci    /// Whether long paths are enabled.  Only has an effect on Windows.
86695b41eeSopenharmony_ci    bool AreLongPathsEnabled() const;
87695b41eeSopenharmony_ci#endif
88695b41eeSopenharmony_ci
89695b41eeSopenharmony_ci  private:
90695b41eeSopenharmony_ci#ifdef _WIN32
91695b41eeSopenharmony_ci    /// Whether stat information can be cached.
92695b41eeSopenharmony_ci    bool use_cache_;
93695b41eeSopenharmony_ci
94695b41eeSopenharmony_ci    /// Whether long paths are enabled.
95695b41eeSopenharmony_ci    bool long_paths_enabled_;
96695b41eeSopenharmony_ci
97695b41eeSopenharmony_ci    typedef std::map<std::string, TimeStamp> DirCache;
98695b41eeSopenharmony_ci    // TODO: Neither a map nor a hashmap seems ideal here.  If the statcache
99695b41eeSopenharmony_ci    // works out, come up with a better data structure.
100695b41eeSopenharmony_ci    typedef std::map<std::string, DirCache> Cache;
101695b41eeSopenharmony_ci    mutable Cache cache_;
102695b41eeSopenharmony_ci#endif
103695b41eeSopenharmony_ci};
104695b41eeSopenharmony_ci
105695b41eeSopenharmony_ci#endif  // NINJA_DISK_INTERFACE_H_
106