Skip to content

Commit 786cbca

Browse files
committed
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
1 parent d2fb6e9 commit 786cbca

File tree

3 files changed

+13
-53
lines changed

3 files changed

+13
-53
lines changed

core/java/android/os/FileUtils.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
import java.util.zip.CRC32;
2929
import java.util.zip.CheckedInputStream;
3030

31-
import libcore.io.Os;
32-
import libcore.io.StructStat;
33-
3431
/**
3532
* Tools for managing files. Not for public consumption.
3633
* @hide
@@ -96,12 +93,6 @@ public static boolean getFileStatus(String path, FileStatus status) {
9693

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

99-
/**
100-
* @deprecated use {@link Os#stat(String)} instead.
101-
*/
102-
@Deprecated
103-
public static native int getPermissions(String file, int[] outPermissions);
104-
10596
public static native int setUMask(int mask);
10697

10798
/** returns the FAT file system volume ID for the volume mounted

core/jni/android_os_FileUtils.cpp

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -68,39 +68,6 @@ jint android_os_FileUtils_setPermissions(JNIEnv* env, jobject clazz,
6868
return chmod(file8.string(), mode) == 0 ? 0 : errno;
6969
}
7070

71-
jint android_os_FileUtils_getPermissions(JNIEnv* env, jobject clazz,
72-
jstring file, jintArray outArray)
73-
{
74-
const jchar* str = env->GetStringCritical(file, 0);
75-
String8 file8;
76-
if (str) {
77-
file8 = String8(str, env->GetStringLength(file));
78-
env->ReleaseStringCritical(file, str);
79-
}
80-
if (file8.size() <= 0) {
81-
return ENOENT;
82-
}
83-
struct stat st;
84-
if (stat(file8.string(), &st) != 0) {
85-
return errno;
86-
}
87-
jint* array = (jint*)env->GetPrimitiveArrayCritical(outArray, 0);
88-
if (array) {
89-
int len = env->GetArrayLength(outArray);
90-
if (len >= 1) {
91-
array[0] = st.st_mode;
92-
}
93-
if (len >= 2) {
94-
array[1] = st.st_uid;
95-
}
96-
if (len >= 3) {
97-
array[2] = st.st_gid;
98-
}
99-
}
100-
env->ReleasePrimitiveArrayCritical(outArray, array, 0);
101-
return 0;
102-
}
103-
10471
jint android_os_FileUtils_setUMask(JNIEnv* env, jobject clazz, jint mask)
10572
{
10673
return umask(mask);
@@ -158,7 +125,6 @@ jboolean android_os_FileUtils_getFileStatus(JNIEnv* env, jobject clazz, jstring
158125

159126
static const JNINativeMethod methods[] = {
160127
{"setPermissions", "(Ljava/lang/String;III)I", (void*)android_os_FileUtils_setPermissions},
161-
{"getPermissions", "(Ljava/lang/String;[I)I", (void*)android_os_FileUtils_getPermissions},
162128
{"setUMask", "(I)I", (void*)android_os_FileUtils_setUMask},
163129
{"getFatVolumeId", "(Ljava/lang/String;)I", (void*)android_os_FileUtils_getFatVolumeId},
164130
{"getFileStatusNative", "(Ljava/lang/String;Landroid/os/FileUtils$FileStatus;)Z", (void*)android_os_FileUtils_getFileStatus},

services/java/com/android/server/pm/PackageManagerService.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
import libcore.io.ErrnoException;
145145
import libcore.io.IoUtils;
146146
import libcore.io.Libcore;
147+
import libcore.io.StructStat;
147148

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

297-
final int[] mOutPermissions = new int[3];
298-
299298
// ----------------------------------------------------------------
300299

301300
// Keys are String (package name), values are Package. This also serves
@@ -3762,14 +3761,18 @@ private PackageParser.Package scanPackageLI(PackageParser.Package pkg,
37623761
boolean uidError = false;
37633762

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

37693772
// If we have mismatched owners for the data path, we have a problem.
3770-
if (mOutPermissions[1] != pkg.applicationInfo.uid) {
3773+
if (currentUid != pkg.applicationInfo.uid) {
37713774
boolean recovered = false;
3772-
if (mOutPermissions[1] == 0) {
3775+
if (currentUid == 0) {
37733776
// The directory somehow became owned by root. Wow.
37743777
// This is probably because the system was stopped while
37753778
// installd was in the middle of messing with its libs
@@ -3798,7 +3801,7 @@ private PackageParser.Package scanPackageLI(PackageParser.Package pkg,
37983801
? "System package " : "Third party package ";
37993802
String msg = prefix + pkg.packageName
38003803
+ " has changed from uid: "
3801-
+ mOutPermissions[1] + " to "
3804+
+ currentUid + " to "
38023805
+ pkg.applicationInfo.uid + "; old data erased";
38033806
reportSettingsProblem(Log.WARN, msg);
38043807
recovered = true;
@@ -3830,11 +3833,11 @@ private PackageParser.Package scanPackageLI(PackageParser.Package pkg,
38303833
if (!recovered) {
38313834
pkg.applicationInfo.dataDir = "/mismatched_uid/settings_"
38323835
+ pkg.applicationInfo.uid + "/fs_"
3833-
+ mOutPermissions[1];
3836+
+ currentUid;
38343837
pkg.applicationInfo.nativeLibraryDir = pkg.applicationInfo.dataDir;
38353838
String msg = "Package " + pkg.packageName
38363839
+ " has mismatched uid: "
3837-
+ mOutPermissions[1] + " on disk, "
3840+
+ currentUid + " on disk, "
38383841
+ pkg.applicationInfo.uid + " in settings";
38393842
// writer
38403843
synchronized (mPackages) {

0 commit comments

Comments
 (0)