Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c4bab984 authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Only create custom database paths.

If the getDatabasePath() caller is using the default getDatabasesDir()
path, that method already handles possible path creation, so no
need for us to retry.

Also fix two small bugs that would have NPE'ed when listing files in
a directory that wasn't created.

Bug: 26895777
Change-Id: Iced16b05cfe95062717da8d0414c48daefcb5648
parent e6cb0974
Loading
Loading
Loading
Loading
+8 −12
Original line number Diff line number Diff line
@@ -182,8 +182,6 @@ class ContextImpl extends Context {
    @GuardedBy("mSync")
    private File[] mExternalMediaDirs;

    private static final String[] EMPTY_STRING_ARRAY = {};

    // The system service cache for the system services that are cached per-ContextImpl.
    final Object[] mServiceCache = SystemServiceRegistry.createServiceCache();

@@ -627,8 +625,7 @@ class ContextImpl extends Context {

    @Override
    public String[] fileList() {
        final String[] list = getFilesDir().list();
        return (list != null) ? list : EMPTY_STRING_ARRAY;
        return FileUtils.listOrEmpty(getFilesDir());
    }

    @Override
@@ -682,24 +679,23 @@ class ContextImpl extends Context {
            dir = new File(dirPath);
            name = name.substring(name.lastIndexOf(File.separatorChar));
            f = new File(dir, name);
        } else {
            dir = getDatabasesDir();
            f = makeFilename(dir, name);
        }

            if (!dir.isDirectory() && dir.mkdir()) {
                FileUtils.setPermissions(dir.getPath(),
                    FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
                    -1, -1);
            }
        } else {
            dir = getDatabasesDir();
            f = makeFilename(dir, name);
        }

        return f;
    }

    @Override
    public String[] databaseList() {
        final String[] list = getDatabasesDir().list();
        return (list != null) ? list : EMPTY_STRING_ARRAY;
        return FileUtils.listOrEmpty(getDatabasesDir());
    }

    private File getDatabasesDir() {
+18 −4
Original line number Diff line number Diff line
@@ -29,6 +29,8 @@ import android.webkit.MimeTypeMap;

import com.android.internal.annotations.VisibleForTesting;

import libcore.util.EmptyArray;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
@@ -658,8 +660,19 @@ public class FileUtils {
        }
    }

    public static @NonNull File[] listFilesOrEmpty(File dir) {
        File[] res = dir.listFiles();
    public static @NonNull String[] listOrEmpty(@Nullable File dir) {
        if (dir == null) return EmptyArray.STRING;
        final String[] res = dir.list();
        if (res != null) {
            return res;
        } else {
            return EmptyArray.STRING;
        }
    }

    public static @NonNull File[] listFilesOrEmpty(@Nullable File dir) {
        if (dir == null) return EMPTY;
        final File[] res = dir.listFiles();
        if (res != null) {
            return res;
        } else {
@@ -667,8 +680,9 @@ public class FileUtils {
        }
    }

    public static @NonNull File[] listFilesOrEmpty(File dir, FilenameFilter filter) {
        File[] res = dir.listFiles(filter);
    public static @NonNull File[] listFilesOrEmpty(@Nullable File dir, FilenameFilter filter) {
        if (dir == null) return EMPTY;
        final File[] res = dir.listFiles(filter);
        if (res != null) {
            return res;
        } else {