Merge branch 'fix-748--verify-error' into 'master'

Move code causing verify error into separate helper class

Fixes #748.

I'm not 100% sure on how the `@TargetApi` and `VerifyError` work
together. However it is something along the lines of:
 * Class loader needs `CleanCacheService`.
 * At this point, it loads the bytecode for that class and verifies
   that it all makes sense.
 * The bytecode within the method targeted at API 21 is not understood
   by earlier APIs, because the entire `Os` class was introduced in 21.
 * By putting it into a different class, that class is only loaded
   at runtime on devices with API of 21 or higher.

Previously, `@TargetApi` + the relevant guard condition to check
the build version at runtime suffices to prevent this. However it seems
that if the entire class does not even exist on earlier APIs, then it
is no longer good enough.

See merge request !379
This commit is contained in:
Daniel Martí 2016-08-20 17:00:15 +00:00
commit 5a6aecad85
2 changed files with 28 additions and 14 deletions

View File

@ -1,6 +1,5 @@
package org.fdroid.fdroid;
import android.annotation.TargetApi;
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
@ -9,9 +8,6 @@ import android.content.Intent;
import android.os.Build;
import android.os.Process;
import android.os.SystemClock;
import android.system.ErrnoException;
import android.system.Os;
import android.system.StructStat;
import org.apache.commons.io.FileUtils;
import org.fdroid.fdroid.installer.ApkCache;
@ -28,7 +24,7 @@ import java.util.concurrent.TimeUnit;
* These files should only be deleted when they are at least an hour-ish old,
* in case they are actively in use while {@code CleanCacheService} is running.
* {@link #clearOldFiles(File, long)} checks the file age using access time from
* {@link StructStat#st_atime} on {@link android.os.Build.VERSION_CODES#LOLLIPOP}
* {@link android.system.StructStat#st_atime} on {@link android.os.Build.VERSION_CODES#LOLLIPOP}
* and newer. On older Android, last modified time from {@link File#lastModified()}
* is used.
*/
@ -152,7 +148,6 @@ public class CleanCacheService extends IntentService {
* @param f The file or directory to clean
* @param millisAgo The number of milliseconds old that marks a file for deletion.
*/
@TargetApi(21)
public static void clearOldFiles(File f, long millisAgo) {
if (f == null) {
return;
@ -172,14 +167,7 @@ public class CleanCacheService extends IntentService {
f.delete();
}
} else {
try {
StructStat stat = Os.lstat(f.getAbsolutePath());
if ((stat.st_atime * 1000L) < olderThan) {
f.delete();
}
} catch (ErrnoException e) {
e.printStackTrace();
}
CleanCacheService21.deleteIfOld(f, millisAgo);
}
}
}

View File

@ -0,0 +1,26 @@
package org.fdroid.fdroid;
import android.annotation.TargetApi;
import android.system.ErrnoException;
import android.system.Os;
import android.system.StructStat;
import java.io.File;
/**
* Helper class to prevent {@link VerifyError}s from occurring in {@link CleanCacheService#clearOldFiles(File, long)}
* due to the fact that {@link Os} was only introduced in API 21.
*/
@TargetApi(21)
class CleanCacheService21 {
static void deleteIfOld(File file, long olderThan) {
try {
StructStat stat = Os.lstat(file.getAbsolutePath());
if ((stat.st_atime * 1000L) < olderThan) {
file.delete();
}
} catch (ErrnoException e) {
e.printStackTrace();
}
}
}