15
15
******************************************************************************/
16
16
package com .noshufou .android .su .provider ;
17
17
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 ;
18
23
import java .util .HashMap ;
19
24
20
25
import android .content .ContentProvider ;
@@ -63,10 +68,8 @@ public static class Apps {
63
68
public static final String LAST_ACCESS_TYPE = Logs .TYPE ;
64
69
public static final String NOTIFICATIONS = "notifications" ;
65
70
public static final String LOGGING = "logging" ;
66
- public static final String DIRTY = "dirty" ;
67
71
68
72
public static final class AllowType {
69
- public static final int TO_DELETE = -2 ;
70
73
public static final int ASK = -1 ;
71
74
public static final int DENY = 0 ;
72
75
public static final int ALLOW = 1 ;
@@ -154,7 +157,6 @@ public static final class LogType {
154
157
sAppsProjectionMap .put (Apps .LAST_ACCESS_TYPE , Logs .TABLE_NAME + "." + Logs .TYPE );
155
158
sAppsProjectionMap .put (Apps .NOTIFICATIONS , Apps .TABLE_NAME + "." + Apps .NOTIFICATIONS );
156
159
sAppsProjectionMap .put (Apps .LOGGING , Apps .TABLE_NAME + "." + Apps .LOGGING );
157
- sAppsProjectionMap .put (Apps .DIRTY , Apps .TABLE_NAME + "." + Apps .DIRTY );
158
160
}
159
161
160
162
private static final HashMap <String , String > sLogsProjectionMap ;
@@ -294,7 +296,6 @@ public Uri insert(Uri uri, ContentValues values) {
294
296
switch (sUriMatcher .match (uri )) {
295
297
case APPS :
296
298
// TODO: Check validity of incoming data before inserting it
297
- values .put (Apps .DIRTY , "1" );
298
299
try {
299
300
rowId = mDb .insertOrThrow (Apps .TABLE_NAME , null , values );
300
301
} catch (SQLException e ) {
@@ -322,9 +323,15 @@ public Uri insert(Uri uri, ContentValues values) {
322
323
logValues .put (Logs .DATE , System .currentTimeMillis ());
323
324
logValues .put (Logs .TYPE , Logs .LogType .CREATE );
324
325
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 ));
325
332
}
326
- Util .updatePermissionsDb (mContext );
327
333
returnUri = ContentUris .withAppendedId (Apps .CONTENT_URI , rowId );
334
+
328
335
break ;
329
336
case APP_ID_LOGS :
330
337
case LOGS_APP_ID :
@@ -358,38 +365,33 @@ public int update(Uri uri, ContentValues values, String selection,
358
365
359
366
int count = 0 ;
360
367
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
- }
369
368
switch (sUriMatcher .match (uri )) {
370
- case APPS :
371
- count = mDb .update (Apps .TABLE_NAME , values , selection , selectionArgs );
372
- break ;
373
369
case APP_ID :
374
370
count = mDb .update (Apps .TABLE_NAME , values ,
375
371
Apps ._ID + "=" + uri .getPathSegments ().get (1 ) +
376
372
(!TextUtils .isEmpty (selection )? " AND (" +
377
373
selection + ")" :"" ),
378
374
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 ) +
383
378
(!TextUtils .isEmpty (selection )? " AND (" +
384
379
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 ();
386
390
break ;
387
391
default :
388
392
throw new IllegalArgumentException ("Unsupported URI: " + uri );
389
393
}
390
- if (updatePermissionsDb ) {
391
- Util .updatePermissionsDb (mContext );
392
- }
394
+
393
395
getContext ().getContentResolver ().notifyChange (uri , null );
394
396
return count ;
395
397
}
@@ -400,25 +402,27 @@ public int delete(Uri uri, String selection, String[] selectionArgs) {
400
402
401
403
int count = 0 ;
402
404
403
- ContentValues deleteAppValues = new ContentValues ();
404
- deleteAppValues .put (Apps .ALLOW , Apps .AllowType .TO_DELETE );
405
- deleteAppValues .put (Apps .DIRTY , 1 );
406
-
407
405
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 ;
415
406
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 ,
417
422
Apps ._ID + "=" + uri .getPathSegments ().get (1 ) +
418
423
(!TextUtils .isEmpty (selection )? " AND (" +
419
424
selection + ")" :"" ),
420
425
selectionArgs );
421
- Util .updatePermissionsDb (mContext );
422
426
// No break here so we can fall through and delete associated logs
423
427
case APP_ID_LOGS :
424
428
case LOGS_APP_ID :
@@ -428,15 +432,6 @@ public int delete(Uri uri, String selection, String[] selectionArgs) {
428
432
selection + ")" :"" ),
429
433
selectionArgs );
430
434
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 ;
440
435
case APP_CLEAN :
441
436
count = mDb .delete (Apps .TABLE_NAME , selection , selectionArgs );
442
437
break ;
@@ -461,7 +456,7 @@ private boolean ensureDb() {
461
456
462
457
private class SuDbOpenHelper extends SQLiteOpenHelper {
463
458
private static final String DATABASE_NAME = "su.db" ;
464
- private static final int DATABASE_VERSION = 4 ;
459
+ private static final int DATABASE_VERSION = 5 ;
465
460
466
461
SuDbOpenHelper (Context context ) {
467
462
super (context , DATABASE_NAME , null , DATABASE_VERSION );
@@ -498,10 +493,6 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
498
493
Log .e (TAG , "dirty column already exists... wut?" , e );
499
494
}
500
495
// 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 );
505
496
upgradeVersion = 3 ;
506
497
}
507
498
@@ -521,6 +512,26 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
521
512
c .close ();
522
513
upgradeVersion = 4 ;
523
514
}
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
524
535
}
525
536
}
526
537
0 commit comments