From 4571ada2035c6485f7dff9b365a11793275f954f Mon Sep 17 00:00:00 2001 From: Makoto Onuki Date: Mon, 27 Sep 2021 12:26:37 -0700 Subject: [PATCH] Make robust the open failure reason detection logic Fix: 201320458 Test: atest cts/tests/tests/database/src/android/database/sqlite/cts/SQLiteDatabaseTest.java Change-Id: I812eb1b05ef2737f118e81cadf70ad5fc0ce2bf3 --- .../database/sqlite/SQLiteConnection.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/core/java/android/database/sqlite/SQLiteConnection.java b/core/java/android/database/sqlite/SQLiteConnection.java index 01e237da2fa03..7e61b48b25470 100644 --- a/core/java/android/database/sqlite/SQLiteConnection.java +++ b/core/java/android/database/sqlite/SQLiteConnection.java @@ -228,7 +228,9 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen mConfiguration.lookasideSlotSize, mConfiguration.lookasideSlotCount); } catch (SQLiteCantOpenDatabaseException e) { final StringBuilder message = new StringBuilder("Cannot open database '") - .append(file).append('\''); + .append(file).append('\'') + .append(" with flags 0x") + .append(Integer.toHexString(mConfiguration.openFlags)); try { // Try to diagnose for common reasons. If something fails in here, that's fine; @@ -236,21 +238,27 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen final Path path = FileSystems.getDefault().getPath(file); final Path dir = path.getParent(); - - if (!Files.isDirectory(dir)) { + if (dir == null) { + message.append(": Directory not specified in the file path"); + } else if (!Files.isDirectory(dir)) { message.append(": Directory ").append(dir).append(" doesn't exist"); } else if (!Files.exists(path)) { - message.append(": File ").append(path).append(" doesn't exist"); + message.append(": File ").append(path).append( + " doesn't exist"); + if ((mConfiguration.openFlags & SQLiteDatabase.CREATE_IF_NECESSARY) != 0) { + message.append( + " and CREATE_IF_NECESSARY is set, check directory permissions"); + } } else if (!Files.isReadable(path)) { message.append(": File ").append(path).append(" is not readable"); } else if (Files.isDirectory(path)) { message.append(": Path ").append(path).append(" is a directory"); } else { - message.append(": Unknown reason"); + message.append(": Unable to deduct failure reason"); } } catch (Throwable th) { - message.append(": Unknown reason; cannot examine filesystem: ") - .append(th.getMessage()); + message.append(": Unable to deduct failure reason" + + " because filesystem couldn't be examined: ").append(th.getMessage()); } throw new SQLiteCantOpenDatabaseException(message.toString(), e); } finally {