More adb cleanup.

This removes adb_dirstart and adb_dirstop. It also fixes a couple of memory
leaks by switching to std::string. This also fixes the bug in the previous
change --- mkdirs is given input like "/system/bin/sh" and only expected to
create "/system/bin". In a later change, we should remove mkdirs and only
expose the intended "unlink && mkdirs && create" functionality.

Change-Id: I30289dc1b3dff575cc1b158d993652178f587552
diff --git a/adb/adb_utils.cpp b/adb/adb_utils.cpp
index 42191c6..3ba971d 100644
--- a/adb/adb_utils.cpp
+++ b/adb/adb_utils.cpp
@@ -72,15 +72,38 @@
   return result;
 }
 
-bool mkdirs(const std::string& path) {
-    for (size_t i = adb_dirstart(path, 1); i != std::string::npos; i = adb_dirstart(path, i + 1)) {
-        if (adb_mkdir(path.substr(0, i), 0775) == -1 && errno != EEXIST) {
+std::string adb_basename(const std::string& path) {
+    size_t base = path.find_last_of(OS_PATH_SEPARATORS);
+    return (base != std::string::npos) ? path.substr(base + 1) : path;
+}
+
+static bool real_mkdirs(const std::string& path) {
+    std::vector<std::string> path_components = android::base::Split(path, OS_PATH_SEPARATOR_STR);
+    // TODO: all the callers do unlink && mkdirs && adb_creat ---
+    // that's probably the operation we should expose.
+    path_components.pop_back();
+    std::string partial_path;
+    for (const auto& path_component : path_components) {
+        if (partial_path.back() != OS_PATH_SEPARATOR) partial_path += OS_PATH_SEPARATOR;
+        partial_path += path_component;
+        if (adb_mkdir(partial_path.c_str(), 0775) == -1 && errno != EEXIST) {
             return false;
         }
     }
     return true;
 }
 
+bool mkdirs(const std::string& path) {
+#if defined(_WIN32)
+    // Replace '/' with '\\' so we can share the code.
+    std::string clean_path = path;
+    std::replace(clean_path.begin(), clean_path.end(), '/', '\\');
+    return real_mkdirs(clean_path);
+#else
+    return real_mkdirs(path);
+#endif
+}
+
 void dump_hex(const void* data, size_t byte_count) {
     byte_count = std::min(byte_count, size_t(16));