Creates parent directories before creates file
Roger Chen [Fri, 3 Jan 2014 06:37:26 +0000 (14:37 +0800)]
Change-Id: I124f7ce2b731b100e9572d7c5d95206e243ee316
Signed-off-by: Roger Chen <cxr514033970@gmail.com>

src/com/android/providers/downloads/Helpers.java

index 013faf2..3562dac 100644 (file)
@@ -141,7 +141,15 @@ public class Helpers {
             // Claim this filename inside lock to prevent other threads from
             // clobbering us. We're not paranoid enough to use O_EXCL.
             try {
-                new File(path).createNewFile();
+                File file = new File(path);
+                File parent = file.getParentFile();
+
+                // Make sure the parent directories exists before generates new file
+                if (parent != null && !parent.exists()) {
+                    parent.mkdirs();
+                }
+
+                file.createNewFile();
             } catch (IOException e) {
                 throw new StopRequestException(Downloads.Impl.STATUS_FILE_ERROR,
                         "Failed to create target file " + path, e);