summaryrefslogtreecommitdiffstats
path: root/src/disk_interface.cc
diff options
context:
space:
mode:
authorThiago Farina <tfarina@chromium.org>2011-08-10 12:47:32 (GMT)
committerThiago Farina <tfarina@chromium.org>2011-08-10 12:47:32 (GMT)
commitf8f63a4a6a88b96b3d7c71c50881c8658d8b4fe4 (patch)
tree18f832ff958d03564ec6faad988804ef70e997ac /src/disk_interface.cc
parent667d16d123e7eb6d9525146c13bdc90f251e7fc9 (diff)
downloadNinja-f8f63a4a6a88b96b3d7c71c50881c8658d8b4fe4.zip
Ninja-f8f63a4a6a88b96b3d7c71c50881c8658d8b4fe4.tar.gz
Ninja-f8f63a4a6a88b96b3d7c71c50881c8658d8b4fe4.tar.bz2
Factor out DiskInterface class into its own source/header files.
This is a TODO in src/ninja_jumble.cc Signed-off-by: Thiago Farina <tfarina@chromium.org>
Diffstat (limited to 'src/disk_interface.cc')
-rw-r--r--src/disk_interface.cc51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/disk_interface.cc b/src/disk_interface.cc
new file mode 100644
index 0000000..70564d7
--- /dev/null
+++ b/src/disk_interface.cc
@@ -0,0 +1,51 @@
+// 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.
+
+#include "disk_interface.h"
+
+namespace {
+
+std::string DirName(const std::string& path) {
+#ifdef WIN32
+ const char kPathSeparator = '\\';
+#else
+ const char kPathSeparator = '/';
+#endif
+
+ std::string::size_type slash_pos = path.rfind(kPathSeparator);
+ if (slash_pos == std::string::npos)
+ return std::string(); // Nothing to do.
+ while (slash_pos > 0 && path[slash_pos - 1] == kPathSeparator)
+ --slash_pos;
+ return path.substr(0, slash_pos);
+}
+
+} // namespace
+
+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);
+ if (mtime < 0)
+ return false; // Error.
+ if (mtime > 0)
+ return true; // Exists already; we're done.
+
+ // Directory doesn't exist. Try creating its parent first.
+ bool success = MakeDirs(dir);
+ if (!success)
+ return false;
+ return MakeDir(dir);
+}