Ninja
disk_interface.h
Go to the documentation of this file.
00001 // Copyright 2011 Google Inc. All Rights Reserved.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //     http://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #ifndef NINJA_DISK_INTERFACE_H_
00016 #define NINJA_DISK_INTERFACE_H_
00017 
00018 #include <string>
00019 using namespace std;
00020 
00021 #include "timestamp.h"
00022 
00023 /// Interface for accessing the disk.
00024 ///
00025 /// Abstract so it can be mocked out for tests.  The real implementation
00026 /// is RealDiskInterface.
00027 struct DiskInterface {
00028   virtual ~DiskInterface() {}
00029 
00030   /// stat() a file, returning the mtime, or 0 if missing and -1 on
00031   /// other errors.
00032   virtual TimeStamp Stat(const string& path) = 0;
00033 
00034   /// Create a directory, returning false on failure.
00035   virtual bool MakeDir(const string& path) = 0;
00036 
00037   /// Create a file, with the specified name and contents
00038   /// Returns true on success, false on failure
00039   virtual bool WriteFile(const string& path, const string& contents) = 0;
00040 
00041   /// Read a file to a string.  Fill in |err| on error.
00042   virtual string ReadFile(const string& path, string* err) = 0;
00043 
00044   /// Remove the file named @a path. It behaves like 'rm -f path' so no errors
00045   /// are reported if it does not exists.
00046   /// @returns 0 if the file has been removed,
00047   ///          1 if the file does not exist, and
00048   ///          -1 if an error occurs.
00049   virtual int RemoveFile(const string& path) = 0;
00050 
00051   /// Create all the parent directories for path; like mkdir -p
00052   /// `basename path`.
00053   bool MakeDirs(const string& path);
00054 };
00055 
00056 /// Implementation of DiskInterface that actually hits the disk.
00057 struct RealDiskInterface : public DiskInterface {
00058   RealDiskInterface() : quiet_(false) {}
00059   virtual ~RealDiskInterface() {}
00060   virtual TimeStamp Stat(const string& path);
00061   virtual bool MakeDir(const string& path);
00062   virtual bool WriteFile(const string& path, const string& contents);
00063   virtual string ReadFile(const string& path, string* err);
00064   virtual int RemoveFile(const string& path);
00065 
00066   /// Whether to print on errors.  Used to make a test quieter.
00067   bool quiet_;
00068 };
00069 
00070 #endif  // NINJA_DISK_INTERFACE_H_