Use IO delegates to create path in compile_aidl_to_java

Change-Id: I654e97d4149b0eb247fb683f9fefc2a7121f3507
Test: Unit tests pass
Signed-off-by: Casey Dahlin <sadmac@google.com>
diff --git a/aidl.cpp b/aidl.cpp
index 4fef675..3cff7e0 100644
--- a/aidl.cpp
+++ b/aidl.cpp
@@ -312,22 +312,6 @@
     return result;
 }
 
-void check_outputFilePath(const string& path) {
-    size_t len = path.length();
-    for (size_t i=0; i<len ; i++) {
-        if (path[i] == OS_PATH_SEPARATOR) {
-            string p = path.substr(0, i);
-            if (access(path.data(), F_OK) != 0) {
-#ifdef _WIN32
-                _mkdir(p.data());
-#else
-                mkdir(p.data(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP);
-#endif
-            }
-        }
-    }
-}
-
 
 int parse_preprocessed_file(const string& filename, TypeNamespace* types) {
     FILE* f = fopen(filename.c_str(), "rb");
@@ -618,17 +602,17 @@
     output_file_name = generate_outputFileName(options, *interface);
   }
 
+  // make sure the folders of the output file all exists
+  if (!io_delegate.CreatePathForFile(output_file_name)) {
+    return 1;
+  }
+
   // if we were asked to, generate a make dependency file
   // unless it's a parcelable *and* it's supposed to fail on parcelable
   if (options.auto_dep_file_ || options.dep_file_name_ != "") {
-    // make sure the folders of the output file all exists
-    check_outputFilePath(output_file_name);
     generate_dep_file(options, imports, io_delegate);
   }
 
-  // make sure the folders of the output file all exists
-  check_outputFilePath(output_file_name);
-
   err = generate_java(output_file_name, options.input_file_name_.c_str(),
                       interface.get(), types.get(), io_delegate);
 
diff --git a/io_delegate.cpp b/io_delegate.cpp
index ecaaae4..6e5130a 100644
--- a/io_delegate.cpp
+++ b/io_delegate.cpp
@@ -26,6 +26,8 @@
 #include <sys/stat.h>
 #endif
 
+#include <base/strings.h>
+
 #include "logging.h"
 #include "os.h"
 
@@ -33,6 +35,8 @@
 using std::unique_ptr;
 using std::vector;
 
+using android::base::Split;
+
 namespace android {
 namespace aidl {
 
@@ -95,6 +99,22 @@
   return true;
 }
 
+bool IoDelegate::CreatePathForFile(const string& path) const {
+  if (path.empty()) {
+    return true;
+  }
+
+  string base = ".";
+  if (path[0] == OS_PATH_SEPARATOR) {
+    base = "/";
+  }
+
+  auto split = Split(path, string{1u, OS_PATH_SEPARATOR});
+  split.pop_back();
+
+  return CreatedNestedDirs(base, split);
+}
+
 unique_ptr<CodeWriter> IoDelegate::GetCodeWriter(
     const string& file_path) const {
   return GetFileWriter(file_path);
diff --git a/io_delegate.h b/io_delegate.h
index 6f51013..b40abc4 100644
--- a/io_delegate.h
+++ b/io_delegate.h
@@ -45,6 +45,8 @@
       const std::string& base_dir,
       const std::vector<std::string>& nested_subdirs) const;
 
+  bool CreatePathForFile(const std::string& path) const;
+
   virtual std::unique_ptr<CodeWriter> GetCodeWriter(
       const std::string& file_path) const;