1515 ******************************************************************************/
1616package com .noshufou .android .su .provider ;
1717
18+ import java .io .File ;
19+ import java .io .FileNotFoundException ;
20+ import java .io .FileOutputStream ;
21+ import java .io .IOException ;
22+ import java .io .OutputStreamWriter ;
1823import java .util .HashMap ;
1924
2025import android .content .ContentProvider ;
@@ -63,10 +68,8 @@ public static class Apps {
6368 public static final String LAST_ACCESS_TYPE = Logs .TYPE ;
6469 public static final String NOTIFICATIONS = "notifications" ;
6570 public static final String LOGGING = "logging" ;
66- public static final String DIRTY = "dirty" ;
6771
6872 public static final class AllowType {
69- public static final int TO_DELETE = -2 ;
7073 public static final int ASK = -1 ;
7174 public static final int DENY = 0 ;
7275 public static final int ALLOW = 1 ;
@@ -154,7 +157,6 @@ public static final class LogType {
154157 sAppsProjectionMap .put (Apps .LAST_ACCESS_TYPE , Logs .TABLE_NAME + "." + Logs .TYPE );
155158 sAppsProjectionMap .put (Apps .NOTIFICATIONS , Apps .TABLE_NAME + "." + Apps .NOTIFICATIONS );
156159 sAppsProjectionMap .put (Apps .LOGGING , Apps .TABLE_NAME + "." + Apps .LOGGING );
157- sAppsProjectionMap .put (Apps .DIRTY , Apps .TABLE_NAME + "." + Apps .DIRTY );
158160 }
159161
160162 private static final HashMap <String , String > sLogsProjectionMap ;
@@ -294,7 +296,6 @@ public Uri insert(Uri uri, ContentValues values) {
294296 switch (sUriMatcher .match (uri )) {
295297 case APPS :
296298 // TODO: Check validity of incoming data before inserting it
297- values .put (Apps .DIRTY , "1" );
298299 try {
299300 rowId = mDb .insertOrThrow (Apps .TABLE_NAME , null , values );
300301 } catch (SQLException e ) {
@@ -322,9 +323,15 @@ public Uri insert(Uri uri, ContentValues values) {
322323 logValues .put (Logs .DATE , System .currentTimeMillis ());
323324 logValues .put (Logs .TYPE , Logs .LogType .CREATE );
324325 mDb .insert (Logs .TABLE_NAME , null , logValues );
326+
327+ Util .writeStoreFile (mContext ,
328+ values .getAsInteger (Apps .UID ),
329+ values .getAsInteger (Apps .EXEC_UID ),
330+ values .getAsString (Apps .EXEC_CMD ),
331+ values .getAsInteger (Apps .ALLOW ));
325332 }
326- Util .updatePermissionsDb (mContext );
327333 returnUri = ContentUris .withAppendedId (Apps .CONTENT_URI , rowId );
334+
328335 break ;
329336 case APP_ID_LOGS :
330337 case LOGS_APP_ID :
@@ -358,38 +365,33 @@ public int update(Uri uri, ContentValues values, String selection,
358365
359366 int count = 0 ;
360367
361- boolean updatePermissionsDb = false ;
362- if (!values .containsKey (Apps .DIRTY )) {
363- Log .d (TAG , "Row dirty, update permissions.sqlite" );
364- values .put (Apps .DIRTY , "1" );
365- updatePermissionsDb = true ;
366- } else {
367- Log .d (TAG , "Row not dirty, don't update permissions.sqlite" );
368- }
369368 switch (sUriMatcher .match (uri )) {
370- case APPS :
371- count = mDb .update (Apps .TABLE_NAME , values , selection , selectionArgs );
372- break ;
373369 case APP_ID :
374370 count = mDb .update (Apps .TABLE_NAME , values ,
375371 Apps ._ID + "=" + uri .getPathSegments ().get (1 ) +
376372 (!TextUtils .isEmpty (selection )? " AND (" +
377373 selection + ")" :"" ),
378374 selectionArgs );
379- break ;
380- case APP_UID :
381- count = mDb .update (Apps .TABLE_NAME , values ,
382- Apps .UID + "=" + uri .getPathSegments ().get (2 ) +
375+ Cursor c = mDb .query (Apps .TABLE_NAME ,
376+ null ,
377+ Apps ._ID + "=" + uri .getPathSegments ().get (1 ) +
383378 (!TextUtils .isEmpty (selection )? " AND (" +
384379 selection + ")" :"" ),
385- selectionArgs );
380+ selectionArgs ,
381+ null , null , null );
382+ if (c .moveToFirst ()) {
383+ Util .writeStoreFile (mContext ,
384+ 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 )));
388+ }
389+ c .close ();
386390 break ;
387391 default :
388392 throw new IllegalArgumentException ("Unsupported URI: " + uri );
389393 }
390- if (updatePermissionsDb ) {
391- Util .updatePermissionsDb (mContext );
392- }
394+
393395 getContext ().getContentResolver ().notifyChange (uri , null );
394396 return count ;
395397 }
@@ -400,25 +402,27 @@ public int delete(Uri uri, String selection, String[] selectionArgs) {
400402
401403 int count = 0 ;
402404
403- ContentValues deleteAppValues = new ContentValues ();
404- deleteAppValues .put (Apps .ALLOW , Apps .AllowType .TO_DELETE );
405- deleteAppValues .put (Apps .DIRTY , 1 );
406-
407405 switch (sUriMatcher .match (uri )) {
408- case APPS :
409- // Don't delete the app here, set it's allow column to -1 and
410- // mark it as dirty. The PermissionsDbService will delete it
411- count = mDb .update (Apps .TABLE_NAME , deleteAppValues , selection , selectionArgs );
412- // Delete from the other DB too
413- Util .updatePermissionsDb (mContext );
414- break ;
415406 case APP_ID :
416- count = mDb .update (Apps .TABLE_NAME , deleteAppValues ,
407+ Cursor c = mDb .query (Apps .TABLE_NAME ,
408+ new String [] { Apps .UID , Apps .EXEC_UID },
409+ Apps ._ID + "=" + uri .getPathSegments ().get (1 ) +
410+ (!TextUtils .isEmpty (selection )? " AND (" +
411+ selection + ")" :"" ),
412+ selectionArgs ,
413+ null , null , null );
414+ 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 ();
419+ }
420+ c .close ();
421+ count = mDb .delete (Apps .TABLE_NAME ,
417422 Apps ._ID + "=" + uri .getPathSegments ().get (1 ) +
418423 (!TextUtils .isEmpty (selection )? " AND (" +
419424 selection + ")" :"" ),
420425 selectionArgs );
421- Util .updatePermissionsDb (mContext );
422426 // No break here so we can fall through and delete associated logs
423427 case APP_ID_LOGS :
424428 case LOGS_APP_ID :
@@ -428,15 +432,6 @@ public int delete(Uri uri, String selection, String[] selectionArgs) {
428432 selection + ")" :"" ),
429433 selectionArgs );
430434 break ;
431- case APP_UID :
432- // May remove this, I don't think I'm going to want to use it
433- count = mDb .update (Apps .TABLE_NAME , deleteAppValues ,
434- Apps .UID + "=" + uri .getPathSegments ().get (2 ) +
435- (!TextUtils .isEmpty (selection )? " AND (" +
436- selection + ")" :"" ),
437- selectionArgs );
438- Util .updatePermissionsDb (mContext );
439- break ;
440435 case APP_CLEAN :
441436 count = mDb .delete (Apps .TABLE_NAME , selection , selectionArgs );
442437 break ;
@@ -461,7 +456,7 @@ private boolean ensureDb() {
461456
462457 private class SuDbOpenHelper extends SQLiteOpenHelper {
463458 private static final String DATABASE_NAME = "su.db" ;
464- private static final int DATABASE_VERSION = 4 ;
459+ private static final int DATABASE_VERSION = 5 ;
465460
466461 SuDbOpenHelper (Context context ) {
467462 super (context , DATABASE_NAME , null , DATABASE_VERSION );
@@ -498,10 +493,6 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
498493 Log .e (TAG , "dirty column already exists... wut?" , e );
499494 }
500495 // Set everything to dirty
501- ContentValues values = new ContentValues ();
502- values .put (Apps .DIRTY , "1" );
503- db .update (Apps .TABLE_NAME , values , null , null );
504- Util .updatePermissionsDb (mContext );
505496 upgradeVersion = 3 ;
506497 }
507498
@@ -521,6 +512,26 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
521512 c .close ();
522513 upgradeVersion = 4 ;
523514 }
515+
516+ if (upgradeVersion == 4 ) {
517+ Cursor c = db .query (Apps .TABLE_NAME , null , null , null , null , null , null );
518+ while (c .moveToNext ()) {
519+ Util .writeStoreFile (mContext ,
520+ 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 )));
524+ }
525+ c .close ();
526+ mContext .deleteDatabase ("permissions.sqlite" );
527+ upgradeVersion = 5 ;
528+ }
529+
530+ }
531+
532+ @ Override
533+ public void onDowngrade (SQLiteDatabase db , int oldVersion , int newVersion ) {
534+ // TODO Auto-generated method stub
524535 }
525536 }
526537
0 commit comments