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

Commit 786cbcac authored by Kenny Root's avatar Kenny Root
Browse files

Use Libcore.os.stat instead of FileUtils

PackageManagerService just needed to know the owner for this file, so
just use stat instead so we can remove the old JNI code.

This is the last user of FileUtils#getPermissions so just remove the
FileUtils method as well.

Change-Id: I953057cd6b9de4410f33b6f22e4bddff02fe2988
parent d2fb6e99
Loading
Loading
Loading
Loading
+0 −9
Original line number Diff line number Diff line
@@ -28,9 +28,6 @@ import java.util.regex.Pattern;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;

import libcore.io.Os;
import libcore.io.StructStat;

/**
 * Tools for managing files.  Not for public consumption.
 * @hide
@@ -96,12 +93,6 @@ public class FileUtils {

    public static native int setPermissions(String file, int mode, int uid, int gid);

    /**
     * @deprecated use {@link Os#stat(String)} instead.
     */
    @Deprecated
    public static native int getPermissions(String file, int[] outPermissions);

    public static native int setUMask(int mask);

    /** returns the FAT file system volume ID for the volume mounted 
+0 −34
Original line number Diff line number Diff line
@@ -68,39 +68,6 @@ jint android_os_FileUtils_setPermissions(JNIEnv* env, jobject clazz,
    return chmod(file8.string(), mode) == 0 ? 0 : errno;
}

jint android_os_FileUtils_getPermissions(JNIEnv* env, jobject clazz,
                                         jstring file, jintArray outArray)
{
    const jchar* str = env->GetStringCritical(file, 0);
    String8 file8;
    if (str) {
        file8 = String8(str, env->GetStringLength(file));
        env->ReleaseStringCritical(file, str);
    }
    if (file8.size() <= 0) {
        return ENOENT;
    }
    struct stat st;
    if (stat(file8.string(), &st) != 0) {
        return errno;
    }
    jint* array = (jint*)env->GetPrimitiveArrayCritical(outArray, 0);
    if (array) {
        int len = env->GetArrayLength(outArray);
        if (len >= 1) {
            array[0] = st.st_mode;
        }
        if (len >= 2) {
            array[1] = st.st_uid;
        }
        if (len >= 3) {
            array[2] = st.st_gid;
        }
    }
    env->ReleasePrimitiveArrayCritical(outArray, array, 0);
    return 0;
}

jint android_os_FileUtils_setUMask(JNIEnv* env, jobject clazz, jint mask)
{
    return umask(mask);
@@ -158,7 +125,6 @@ jboolean android_os_FileUtils_getFileStatus(JNIEnv* env, jobject clazz, jstring

static const JNINativeMethod methods[] = {
    {"setPermissions",  "(Ljava/lang/String;III)I", (void*)android_os_FileUtils_setPermissions},
    {"getPermissions",  "(Ljava/lang/String;[I)I", (void*)android_os_FileUtils_getPermissions},
    {"setUMask",        "(I)I",                    (void*)android_os_FileUtils_setUMask},
    {"getFatVolumeId",  "(Ljava/lang/String;)I", (void*)android_os_FileUtils_getFatVolumeId},
    {"getFileStatusNative", "(Ljava/lang/String;Landroid/os/FileUtils$FileStatus;)Z", (void*)android_os_FileUtils_getFileStatus},
+13 −10
Original line number Diff line number Diff line
@@ -144,6 +144,7 @@ import java.util.Set;
import libcore.io.ErrnoException;
import libcore.io.IoUtils;
import libcore.io.Libcore;
import libcore.io.StructStat;

/**
 * Keep track of all those .apks everywhere.
@@ -294,8 +295,6 @@ public class PackageManagerService extends IPackageManager.Stub {
    File mScanningPath;
    int mLastScanError;

    final int[] mOutPermissions = new int[3];

    // ----------------------------------------------------------------

    // Keys are String (package name), values are Package.  This also serves
@@ -3762,14 +3761,18 @@ public class PackageManagerService extends IPackageManager.Stub {
            boolean uidError = false;

            if (dataPath.exists()) {
                // XXX should really do this check for each user.
                mOutPermissions[1] = 0;
                FileUtils.getPermissions(dataPath.getPath(), mOutPermissions);
                int currentUid = 0;
                try {
                    StructStat stat = Libcore.os.stat(dataPath.getPath());
                    currentUid = stat.st_uid;
                } catch (ErrnoException e) {
                    Slog.e(TAG, "Couldn't stat path " + dataPath.getPath(), e);
                }

                // If we have mismatched owners for the data path, we have a problem.
                if (mOutPermissions[1] != pkg.applicationInfo.uid) {
                if (currentUid != pkg.applicationInfo.uid) {
                    boolean recovered = false;
                    if (mOutPermissions[1] == 0) {
                    if (currentUid == 0) {
                        // The directory somehow became owned by root.  Wow.
                        // This is probably because the system was stopped while
                        // installd was in the middle of messing with its libs
@@ -3798,7 +3801,7 @@ public class PackageManagerService extends IPackageManager.Stub {
                                    ? "System package " : "Third party package ";
                            String msg = prefix + pkg.packageName
                                    + " has changed from uid: "
                                    + mOutPermissions[1] + " to "
                                    + currentUid + " to "
                                    + pkg.applicationInfo.uid + "; old data erased";
                            reportSettingsProblem(Log.WARN, msg);
                            recovered = true;
@@ -3830,11 +3833,11 @@ public class PackageManagerService extends IPackageManager.Stub {
                    if (!recovered) {
                        pkg.applicationInfo.dataDir = "/mismatched_uid/settings_"
                            + pkg.applicationInfo.uid + "/fs_"
                            + mOutPermissions[1];
                            + currentUid;
                        pkg.applicationInfo.nativeLibraryDir = pkg.applicationInfo.dataDir;
                        String msg = "Package " + pkg.packageName
                                + " has mismatched uid: "
                                + mOutPermissions[1] + " on disk, "
                                + currentUid + " on disk, "
                                + pkg.applicationInfo.uid + " in settings";
                        // writer
                        synchronized (mPackages) {