am b9af2ba4: am 047585f9: Ensure CTS result directories are unique.

* commit 'b9af2ba4889d809bb2f22013a1730256f112a26a':
  Ensure CTS result directories are unique.
diff --git a/tools/tradefed-host/src/com/android/cts/tradefed/result/CtsXmlResultReporter.java b/tools/tradefed-host/src/com/android/cts/tradefed/result/CtsXmlResultReporter.java
index 9517d8e..71c093b 100644
--- a/tools/tradefed-host/src/com/android/cts/tradefed/result/CtsXmlResultReporter.java
+++ b/tools/tradefed-host/src/com/android/cts/tradefed/result/CtsXmlResultReporter.java
@@ -134,10 +134,8 @@
             if (mReportDir == null) {
                 mReportDir = ctsBuildHelper.getResultsDir();
             }
-            // create a unique directory for saving results, using old cts host convention
-            // TODO: in future, consider using LogFileSaver to create build-specific directories
-            mReportDir = new File(mReportDir, TimeUtil.getResultTimestamp());
-            mReportDir.mkdirs();
+            mReportDir = createUniqueReportDir(mReportDir);
+
             mStartTime = getTimestamp();
             logResult("Created result dir %s", mReportDir.getName());
         }
@@ -152,6 +150,39 @@
     }
 
     /**
+     * Create a unique directory for saving results.
+     * <p/>
+     * Currently using legacy CTS host convention of timestamp directory names. In case of
+     * collisions, will use {@link FileUtil} to generate unique file name.
+     * <p/>
+     * TODO: in future, consider using LogFileSaver to create build-specific directories
+     *
+     * @param parentDir the parent folder to create dir in
+     * @return the created directory
+     */
+    private static synchronized File createUniqueReportDir(File parentDir) {
+        // TODO: in future, consider using LogFileSaver to create build-specific directories
+
+        File reportDir = new File(parentDir, TimeUtil.getResultTimestamp());
+        if (reportDir.exists()) {
+            // directory with this timestamp exists already! Choose a unique, although uglier, name
+            try {
+                reportDir = FileUtil.createTempDir(TimeUtil.getResultTimestamp() + "_", parentDir);
+            } catch (IOException e) {
+                CLog.e(e);
+                CLog.e("Failed to create result directory %s", reportDir.getAbsolutePath());
+            }
+        } else {
+            if (!reportDir.mkdirs()) {
+                // TODO: consider throwing an exception
+                CLog.e("mkdirs failed when attempting to create result directory %s",
+                        reportDir.getAbsolutePath());
+            }
+        }
+        return reportDir;
+    }
+
+    /**
      * Helper method to retrieve the {@link CtsBuildHelper}.
      * @param ctsBuild
      */