Skip to content

Commit 6f83eff

Browse files
committed
Fix for apps that use multiple different commands not being remebered properly
1 parent 052be9b commit 6f83eff

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

src/com/noshufou/android/su/provider/PermissionsProvider.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,7 @@ public Uri insert(Uri uri, ContentValues values) {
326326

327327
Util.writeStoreFile(mContext,
328328
values.getAsInteger(Apps.UID),
329-
values.getAsInteger(Apps.EXEC_UID),
330-
values.getAsString(Apps.EXEC_CMD),
331-
values.getAsInteger(Apps.ALLOW));
329+
values.getAsInteger(Apps.EXEC_UID));
332330
}
333331
returnUri = ContentUris.withAppendedId(Apps.CONTENT_URI, rowId);
334332

@@ -382,9 +380,7 @@ public int update(Uri uri, ContentValues values, String selection,
382380
if (c.moveToFirst()) {
383381
Util.writeStoreFile(mContext,
384382
c.getInt(c.getColumnIndex(Apps.UID)),
385-
c.getInt(c.getColumnIndex(Apps.EXEC_UID)),
386-
c.getString(c.getColumnIndex(Apps.EXEC_CMD)),
387-
c.getInt(c.getColumnIndex(Apps.ALLOW)));
383+
c.getInt(c.getColumnIndex(Apps.EXEC_UID)));
388384
}
389385
c.close();
390386
break;
@@ -404,6 +400,7 @@ public int delete(Uri uri, String selection, String[] selectionArgs) {
404400

405401
switch (sUriMatcher.match(uri)) {
406402
case APP_ID:
403+
int uid = 0, execUid = 0;
407404
Cursor c = mDb.query(Apps.TABLE_NAME,
408405
new String[] { Apps.UID, Apps.EXEC_UID },
409406
Apps._ID + "=" + uri.getPathSegments().get(1) +
@@ -412,17 +409,16 @@ public int delete(Uri uri, String selection, String[] selectionArgs) {
412409
selectionArgs,
413410
null, null, null);
414411
if (c.moveToFirst()) {
415-
File file = new File(mContext.getFilesDir().getAbsolutePath() + "/stored/" +
416-
c.getInt(c.getColumnIndex(Apps.UID)) + "-" +
417-
c.getInt(c.getColumnIndex(Apps.EXEC_UID)));
418-
file.delete();
412+
uid = c.getInt(c.getColumnIndex(Apps.UID));
413+
execUid = c.getInt(c.getColumnIndex(Apps.EXEC_UID));
419414
}
420415
c.close();
421416
count = mDb.delete(Apps.TABLE_NAME,
422417
Apps._ID + "=" + uri.getPathSegments().get(1) +
423418
(!TextUtils.isEmpty(selection)? " AND (" +
424419
selection + ")":""),
425420
selectionArgs);
421+
Util.writeStoreFile(mContext, uid, execUid);
426422
// No break here so we can fall through and delete associated logs
427423
case APP_ID_LOGS:
428424
case LOGS_APP_ID:
@@ -518,9 +514,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
518514
while (c.moveToNext()) {
519515
Util.writeStoreFile(mContext,
520516
c.getInt(c.getColumnIndex(Apps.UID)),
521-
c.getInt(c.getColumnIndex(Apps.EXEC_UID)),
522-
c.getString(c.getColumnIndex(Apps.EXEC_CMD)),
523-
c.getInt(c.getColumnIndex(Apps.ALLOW)));
517+
c.getInt(c.getColumnIndex(Apps.EXEC_UID)));
524518
}
525519
c.close();
526520
mContext.deleteDatabase("permissions.sqlite");

src/com/noshufou/android/su/util/Util.java

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,17 @@
3636
import android.app.NotificationManager;
3737
import android.app.PendingIntent;
3838
import android.content.ComponentName;
39+
import android.content.ContentResolver;
3940
import android.content.Context;
4041
import android.content.Intent;
4142
import android.content.SharedPreferences;
4243
import android.content.pm.ApplicationInfo;
4344
import android.content.pm.PackageInfo;
4445
import android.content.pm.PackageManager;
4546
import android.content.pm.PackageManager.NameNotFoundException;
47+
import android.database.Cursor;
4648
import android.graphics.drawable.Drawable;
49+
import android.net.Uri;
4750
import android.os.Build;
4851
import android.preference.PreferenceManager;
4952
import android.support.v4.app.NotificationCompat;
@@ -57,6 +60,7 @@
5760
import com.noshufou.android.su.preferences.Preferences;
5861
import com.noshufou.android.su.preferences.PreferencesActivity;
5962
import com.noshufou.android.su.preferences.PreferencesActivityHC;
63+
import com.noshufou.android.su.provider.PermissionsProvider.Apps;
6064
import com.noshufou.android.su.provider.PermissionsProvider.Apps.AllowType;
6165
import com.noshufou.android.su.service.UpdaterService;
6266

@@ -784,29 +788,42 @@ public static ArrayList<String> run(String shell, String[] commands) {
784788
return output;
785789
}
786790

787-
public static boolean writeStoreFile(Context context, int uid, int execUid, String cmd, int allow) {
791+
public static boolean writeStoreFile(Context context, int uid, int execUid) {
788792
File storedDir = new File(context.getFilesDir().getAbsolutePath() + File.separator + "stored");
789793
storedDir.mkdirs();
790-
if (cmd == null) {
791-
Log.d(TAG, "App stored for logging purposes, file not required");
792-
return false;
793-
}
794794
String fileName = uid + "-" + execUid;
795+
ContentResolver cr = context.getContentResolver();
796+
Cursor c = cr.query(Uri.withAppendedPath(Apps.CONTENT_URI, "uid/" + uid),
797+
new String[] { Apps.EXEC_CMD, Apps.ALLOW },
798+
Apps.EXEC_UID + "=?",
799+
new String[] { String.valueOf(execUid) },
800+
null);
795801
try {
796802
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(
797803
new File(storedDir.getAbsolutePath() + File.separator + fileName)));
798-
switch (allow) {
799-
case AllowType.ALLOW:
800-
out.write("allow\n");
801-
break;
802-
case AllowType.DENY:
803-
out.write("deny\n");
804-
break;
805-
default:
806-
out.write("prompt\n");
804+
if (c.getCount() != 0) {
805+
while (c.moveToNext()) {
806+
String cmd = c.getString(c.getColumnIndex(Apps.EXEC_CMD));
807+
if (cmd == null) break;
808+
switch (c.getInt(c.getColumnIndex(Apps.ALLOW))) {
809+
case AllowType.ALLOW:
810+
out.write("allow\n");
811+
break;
812+
case AllowType.DENY:
813+
out.write("deny\n");
814+
break;
815+
default:
816+
out.write("prompt\n");
817+
}
818+
cmd = cmd.replaceAll("\\n", "; ");
819+
out.write(c.getString(c.getColumnIndex(Apps.EXEC_CMD)));
820+
out.write('\n');
821+
}
822+
} else {
823+
File file = new File(context.getFilesDir().getAbsolutePath() + "/stored/" +
824+
uid + "-" + execUid);
825+
file.delete();
807826
}
808-
out.write(cmd);
809-
out.write('\n');
810827
out.flush();
811828
out.close();
812829
} catch (FileNotFoundException e) {
@@ -815,6 +832,8 @@ public static boolean writeStoreFile(Context context, int uid, int execUid, Stri
815832
} catch (IOException e) {
816833
Log.w(TAG, "Store file not written", e);
817834
return false;
835+
} finally {
836+
c.close();
818837
}
819838
return true;
820839
}

0 commit comments

Comments
 (0)