1
0
Fork 0
mirror of https://github.com/M66B/NetGuard.git synced 2025-02-24 23:23:08 +00:00

Auto purge log records older than three days

Fixes #348
This commit is contained in:
M66B 2016-02-29 10:20:17 +01:00
parent 7135e241d2
commit 5dc1e66fbf
2 changed files with 24 additions and 1 deletions

View file

@ -406,6 +406,27 @@ public class DatabaseHelper extends SQLiteOpenHelper {
notifyLogChanged();
}
public void cleanupLog(long time) {
mLock.writeLock().lock();
try {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransactionNonExclusive();
try {
// There an index on time
int rows = db.delete("log", "time < ?", new String[]{Long.toString(time)});
Log.i(TAG, "Cleanup log" +
" before=" + SimpleDateFormat.getDateTimeInstance().format(new Date(time)) +
" rows=" + rows);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
} finally {
mLock.writeLock().unlock();
}
}
public Cursor getLog(boolean udp, boolean tcp, boolean other, boolean allowed, boolean blocked) {
mLock.readLock().lock();
try {
@ -748,7 +769,6 @@ public class DatabaseHelper extends SQLiteOpenHelper {
}
public void cleanupDns(long time) {
// There is no index on time for write performance
mLock.writeLock().lock();
try {
SQLiteDatabase db = this.getWritableDatabase();

View file

@ -520,6 +520,9 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
private void householding(Intent intent) {
// Keep log records for three days
DatabaseHelper.getInstance(SinkholeService.this).cleanupLog(new Date().getTime() - 3 * 24 * 3600 * 1000L);
// Keep DNS records for a week
DatabaseHelper.getInstance(SinkholeService.this).cleanupDns(new Date().getTime() - 7 * 24 * 3600 * 1000L);