convert all time_t to a new TimeStamp type
authorEvan Martin <martine@danga.com>
Thu, 5 Jan 2012 17:24:30 +0000 (09:24 -0800)
committerEvan Martin <martine@danga.com>
Thu, 5 Jan 2012 17:24:30 +0000 (09:24 -0800)
src/build.cc
src/build_log.cc
src/build_log.h
src/disk_interface.cc
src/disk_interface.h
src/disk_interface_test.cc
src/graph.cc
src/graph.h
src/test.cc
src/test.h
src/timestamp.h [new file with mode: 0644]

index 4b54561dc0350c4ecb9a5a514bc61dfe43972b88..61439ccd6093e4f52f796cd911bae9ed8531db3f 100644 (file)
@@ -321,7 +321,7 @@ void Plan::CleanNode(BuildLog* build_log, Node* node) {
                             end = (*ei)->inputs_.end() - (*ei)->order_only_deps_;
     if (find_if(begin, end, mem_fun(&Node::dirty)) == end) {
       // Recompute most_recent_input and command.
-      time_t most_recent_input = 1;
+      TimeStamp most_recent_input = 1;
       for (vector<Node*>::iterator ni = begin; ni != end; ++ni)
         if ((*ni)->mtime() > most_recent_input)
           most_recent_input = (*ni)->mtime();
@@ -569,7 +569,7 @@ bool Builder::StartEdge(Edge* edge, string* err) {
 }
 
 void Builder::FinishEdge(Edge* edge, bool success, const string& output) {
-  time_t restat_mtime = 0;
+  TimeStamp restat_mtime = 0;
 
   if (success) {
     if (edge->rule().restat()) {
@@ -578,7 +578,7 @@ void Builder::FinishEdge(Edge* edge, bool success, const string& output) {
       for (vector<Node*>::iterator i = edge->outputs_.begin();
            i != edge->outputs_.end(); ++i) {
         if ((*i)->exists()) {
-          time_t new_mtime = disk_interface_->Stat((*i)->path());
+          TimeStamp new_mtime = disk_interface_->Stat((*i)->path());
           if ((*i)->mtime() == new_mtime) {
             // The rule command did not change the output.  Propagate the clean
             // state through the build graph.
@@ -593,7 +593,7 @@ void Builder::FinishEdge(Edge* edge, bool success, const string& output) {
         // (existing) non-order-only input or the depfile.
         for (vector<Node*>::iterator i = edge->inputs_.begin();
              i != edge->inputs_.end() - edge->order_only_deps_; ++i) {
-          time_t input_mtime = disk_interface_->Stat((*i)->path());
+          TimeStamp input_mtime = disk_interface_->Stat((*i)->path());
           if (input_mtime == 0) {
             restat_mtime = 0;
             break;
@@ -603,7 +603,7 @@ void Builder::FinishEdge(Edge* edge, bool success, const string& output) {
         }
 
         if (restat_mtime != 0 && !edge->rule().depfile().empty()) {
-          time_t depfile_mtime = disk_interface_->Stat(edge->EvaluateDepFile());
+          TimeStamp depfile_mtime = disk_interface_->Stat(edge->EvaluateDepFile());
           if (depfile_mtime == 0)
             restat_mtime = 0;
           else if (depfile_mtime > restat_mtime)
index 501b15b011b30a8d43faa4fd89daa3533d7a9871..b5641372445eee4f73942ecfc2d29edd077013e6 100644 (file)
@@ -73,7 +73,7 @@ bool BuildLog::OpenForWrite(const string& path, string* err) {
 }
 
 void BuildLog::RecordCommand(Edge* edge, int start_time, int end_time,
-                             time_t restat_mtime) {
+                             TimeStamp restat_mtime) {
   const string command = edge->EvaluateCommand();
   for (vector<Node*>::iterator out = edge->outputs_.begin();
        out != edge->outputs_.end(); ++out) {
@@ -133,7 +133,7 @@ bool BuildLog::Load(const string& path, string* err) {
     *end = 0;
 
     int start_time = 0, end_time = 0;
-    time_t restat_mtime = 0;
+    TimeStamp restat_mtime = 0;
 
     start_time = atoi(start);
     start = end + 1;
index 51cbe5ed753d73bffb4c0663c37d0579e79d6da9..3e27eee32c3b3b69c49841f23176088a6b5042d4 100644 (file)
@@ -20,6 +20,7 @@
 using namespace std;
 
 #include "hash_map.h"
+#include "timestamp.h"
 
 struct BuildConfig;
 struct Edge;
@@ -39,7 +40,7 @@ struct BuildLog {
   void SetConfig(BuildConfig* config) { config_ = config; }
   bool OpenForWrite(const string& path, string* err);
   void RecordCommand(Edge* edge, int start_time, int end_time,
-                     time_t restat_mtime = 0);
+                     TimeStamp restat_mtime = 0);
   void Close();
 
   /// Load the on-disk log.
@@ -50,7 +51,7 @@ struct BuildLog {
     string command;
     int start_time;
     int end_time;
-    time_t restat_mtime;
+    TimeStamp restat_mtime;
 
     // Used by tests.
     bool operator==(const LogEntry& o) {
index dec3836c30fc08fe85ccfd5267e048140267cf32..09446435508397125e3e7e136a38a7562d3cd51f 100644 (file)
@@ -50,7 +50,7 @@ bool DiskInterface::MakeDirs(const std::string& path) {
   std::string dir = DirName(path);
   if (dir.empty())
     return true;  // Reached root; assume it's there.
-  int mtime = Stat(dir);
+  TimeStamp mtime = Stat(dir);
   if (mtime < 0)
     return false;  // Error.
   if (mtime > 0)
@@ -65,7 +65,7 @@ bool DiskInterface::MakeDirs(const std::string& path) {
 
 // RealDiskInterface -----------------------------------------------------------
 
-int RealDiskInterface::Stat(const std::string& path) {
+TimeStamp RealDiskInterface::Stat(const std::string& path) {
 #ifdef WIN32
   WIN32_FILE_ATTRIBUTE_DATA attrs;
   if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &attrs)) {
@@ -84,7 +84,7 @@ int RealDiskInterface::Stat(const std::string& path) {
     ((uint64_t)filetime.dwLowDateTime);
   mtime /= 1000000000LL / 100; // 100ns -> s.
   mtime -= 12622770400LL;  // 1600 epoch -> 2000 epoch (subtract 400 years).
-  return (int)mtime;
+  return (TimeStamp)mtime;
 #else
   struct stat st;
   if (stat(path.c_str(), &st) < 0) {
index 9efcfce60380264ce2cf0bb10526f3d9bb9c9de2..b0fed3db0172f785d1726ccf31a586c06737f9bb 100644 (file)
@@ -16,6 +16,9 @@
 #define NINJA_DISK_INTERFACE_H_
 
 #include <string>
+using namespace std;
+
+#include "timestamp.h"
 
 /// Interface for accessing the disk.
 ///
@@ -26,33 +29,33 @@ struct DiskInterface {
 
   /// stat() a file, returning the mtime, or 0 if missing and -1 on
   /// other errors.
-  virtual int Stat(const std::string& path) = 0;
+  virtual TimeStamp Stat(const string& path) = 0;
 
   /// Create a directory, returning false on failure.
-  virtual bool MakeDir(const std::string& path) = 0;
+  virtual bool MakeDir(const string& path) = 0;
 
   /// Read a file to a string.  Fill in |err| on error.
-  virtual std::string ReadFile(const std::string& path, std::string* err) = 0;
+  virtual string ReadFile(const string& path, string* err) = 0;
 
   /// Remove the file named @a path. It behaves like 'rm -f path' so no errors
   /// are reported if it does not exists.
   /// @returns 0 if the file has been removed,
   ///          1 if the file does not exist, and
   ///          -1 if an error occurs.
-  virtual int RemoveFile(const std::string& path) = 0;
+  virtual int RemoveFile(const string& path) = 0;
 
   /// Create all the parent directories for path; like mkdir -p
   /// `basename path`.
-  bool MakeDirs(const std::string& path);
+  bool MakeDirs(const string& path);
 };
 
 /// Implementation of DiskInterface that actually hits the disk.
 struct RealDiskInterface : public DiskInterface {
   virtual ~RealDiskInterface() {}
-  virtual int Stat(const std::string& path);
-  virtual bool MakeDir(const std::string& path);
-  virtual std::string ReadFile(const std::string& path, std::string* err);
-  virtual int RemoveFile(const std::string& path);
+  virtual TimeStamp Stat(const string& path);
+  virtual bool MakeDir(const string& path);
+  virtual string ReadFile(const string& path, string* err);
+  virtual int RemoveFile(const string& path);
 };
 
 #endif  // NINJA_DISK_INTERFACE_H_
index 3bb8067928c55ede9af701cbb989609f2c516950..c431e75c1e1505fb4e1d52ed5f15b5e614d0c62a 100644 (file)
@@ -168,7 +168,7 @@ TEST_F(DiskInterfaceTest, RemoveFile) {
 struct StatTest : public StateTestWithBuiltinRules,
                   public DiskInterface {
   // DiskInterface implementation.
-  virtual int Stat(const string& path);
+  virtual TimeStamp Stat(const string& path);
   virtual bool MakeDir(const string& path) {
     assert(false);
     return false;
@@ -182,13 +182,13 @@ struct StatTest : public StateTestWithBuiltinRules,
     return 0;
   }
 
-  map<string, time_t> mtimes_;
+  map<string, TimeStamp> mtimes_;
   vector<string> stats_;
 };
 
-int StatTest::Stat(const string& path) {
+TimeStamp StatTest::Stat(const string& path) {
   stats_.push_back(path);
-  map<string, time_t>::iterator i = mtimes_.find(path);
+  map<string, TimeStamp>::iterator i = mtimes_.find(path);
   if (i == mtimes_.end())
     return 0;  // File not found.
   return i->second;
index ed7097ba7acabb8ad554587ba7b1ad5e5744a04c..eb107f21707cc593f6df103b4072a86e17186288 100644 (file)
@@ -40,7 +40,7 @@ bool Edge::RecomputeDirty(State* state, DiskInterface* disk_interface,
   }
 
   // Visit all inputs; we're dirty if any of the inputs are dirty.
-  time_t most_recent_input = 1;
+  TimeStamp most_recent_input = 1;
   for (vector<Node*>::iterator i = inputs_.begin(); i != inputs_.end(); ++i) {
     if ((*i)->StatIfNecessary(disk_interface)) {
       if (Edge* edge = (*i)->in_edge()) {
@@ -103,11 +103,12 @@ bool Edge::RecomputeDirty(State* state, DiskInterface* disk_interface,
   return true;
 }
 
-bool Edge::RecomputeOutputDirty(BuildLog* build_log, time_t most_recent_input,
+bool Edge::RecomputeOutputDirty(BuildLog* build_log,
+                                TimeStamp most_recent_input,
                                 const string& command, Node* output) {
   if (is_phony()) {
-    // Phony edges don't write any output.
-    // Outputs are only dirty if there are no inputs and we're missing the output.
+    // Phony edges don't write any output.  Outputs are only dirty if
+    // there are no inputs and we're missing the output.
     return inputs_.empty() && !output->exists();
   }
 
index 0514c0c8ef1d6b904c9c8308a06ce15c8852f459..6ba82b9faa35fa9cb2cdc0f347b5b6b96a55db2e 100644 (file)
@@ -20,9 +20,9 @@
 using namespace std;
 
 #include "eval_env.h"
+#include "timestamp.h"
 
 struct DiskInterface;
-
 struct Edge;
 
 /// Information about a node in the dependency graph: the file, whether
@@ -62,7 +62,7 @@ struct Node {
   }
 
   const string& path() const { return path_; }
-  time_t mtime() const { return mtime_; }
+  TimeStamp mtime() const { return mtime_; }
 
   bool dirty() const { return dirty_; }
   void set_dirty(bool dirty) { dirty_ = dirty; }
@@ -80,7 +80,7 @@ private:
   ///   -1: file hasn't been examined
   ///   0:  we looked, and file doesn't exist
   ///   >0: actual file's mtime
-  time_t mtime_;
+  TimeStamp mtime_;
 
   /// Dirty is true when the underlying file is out-of-date.
   /// But note that Edge::outputs_ready_ is also used in judging which
@@ -141,7 +141,7 @@ struct Edge {
 
   /// Recompute whether a given single output should be marked dirty.
   /// Returns true if so.
-  bool RecomputeOutputDirty(BuildLog* build_log, time_t most_recent_input,
+  bool RecomputeOutputDirty(BuildLog* build_log, TimeStamp most_recent_input,
                             const string& command, Node* output);
 
   /// Return true if all inputs' in-edges are ready.
index e8ea621386861bc9a566517b08c0581a107bd37a..925a6cb631bf72a97d5ff4d0b471045f91ebd30f 100644 (file)
@@ -41,7 +41,7 @@ void VirtualFileSystem::Create(const string& path, int time,
   files_[path].contents = contents;
 }
 
-int VirtualFileSystem::Stat(const string& path) {
+TimeStamp VirtualFileSystem::Stat(const string& path) {
   FileMap::iterator i = files_.find(path);
   if (i != files_.end())
     return i->second.mtime;
index c27e051a547c954c2f1fcb63bca6a0a29bc003f4..c8c275879b82c236e7154e190db3229c3c003f73 100644 (file)
@@ -43,7 +43,7 @@ struct VirtualFileSystem : public DiskInterface {
   void Create(const string& path, int time, const string& contents);
 
   // DiskInterface
-  virtual int Stat(const string& path);
+  virtual TimeStamp Stat(const string& path);
   virtual bool MakeDir(const string& path);
   virtual string ReadFile(const string& path, string* err);
   virtual int RemoveFile(const string& path);
diff --git a/src/timestamp.h b/src/timestamp.h
new file mode 100644 (file)
index 0000000..cee7ba8
--- /dev/null
@@ -0,0 +1,24 @@
+// Copyright 2011 Google Inc. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef NINJA_TIMESTAMP_H_
+#define NINJA_TIMESTAMP_H_
+
+// When considering file modification times we only care to compare
+// them against one another -- we never convert them to an absolute
+// real time.  On POSIX we use time_t (seconds since epoch) and on
+// Windows we use a different value.  Both fit in an int.
+typedef int TimeStamp;
+
+#endif  // NINJA_TIMESTAMP_H_