From 8800dbd05e96b663b98c5b65244c6e8cf24c9291 Mon Sep 17 00:00:00 2001 From: Roger Chen Date: Fri, 3 Jan 2014 14:37:26 +0800 Subject: Creates parent directories before creates file Change-Id: I124f7ce2b731b100e9572d7c5d95206e243ee316 Signed-off-by: Roger Chen (cherry picked from commit 70e94740daa83c80bc76a0c57d4d8b86cf329c1e) --- src/com/android/providers/downloads/Helpers.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/com/android/providers/downloads/Helpers.java b/src/com/android/providers/downloads/Helpers.java index 33205557..007e663a 100644 --- a/src/com/android/providers/downloads/Helpers.java +++ b/src/com/android/providers/downloads/Helpers.java @@ -139,7 +139,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); -- cgit v1.2.3