Remove unknwon files that are in the downloads dir

The files are supposed to be downloaded in a privileged location
that the user can't access. If the user wipes the data of the
application, the updates downloaded are not removed. Perform a
one-time cleanup so that our downloads dir doesn't grow without
control.
This commit is contained in:
Gabriele M
2017-07-08 01:09:22 +02:00
parent 6534932b20
commit efa829358d
4 changed files with 51 additions and 0 deletions

View File

@@ -18,7 +18,10 @@
<string name="app_name">Updater</string> <string name="app_name">Updater</string>
<string name="display_name">Updater</string> <string name="display_name">Updater</string>
<!-- Directory where the update files will be downloaded and stored.
WARNING: The application can and will delete any unknown file. -->
<string name="download_path" translatable="false">/data/ota_package/</string> <string name="download_path" translatable="false">/data/ota_package/</string>
<string name="conf_update_server_url_def" translatable="false">https://download.lineageos.org/api</string> <string name="conf_update_server_url_def" translatable="false">https://download.lineageos.org/api</string>
<string name="verification_failed_notification">Verification failed</string> <string name="verification_failed_notification">Verification failed</string>

View File

@@ -46,6 +46,8 @@ public class UpdatesCheckReceiver extends BroadcastReceiver {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
// Set a repeating alarm on boot to check for new updates once per day // Set a repeating alarm on boot to check for new updates once per day
scheduleRepeatingUpdatesCheck(context); scheduleRepeatingUpdatesCheck(context);
Utils.cleanupDownloadsDir(context);
} }
final SharedPreferences preferences = final SharedPreferences preferences =

View File

@@ -79,6 +79,8 @@ public class UpdaterController implements UpdaterControllerInt {
mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Updater"); mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Updater");
mWakeLock.setReferenceCounted(false); mWakeLock.setReferenceCounted(false);
Utils.cleanupDownloadsDir(context);
for (UpdateDownload update : mUpdatesDbHelper.getUpdates()) { for (UpdateDownload update : mUpdatesDbHelper.getUpdates()) {
addUpdate(update, false); addUpdate(update, false);
} }

View File

@@ -16,9 +16,11 @@
package org.lineageos.updater.misc; package org.lineageos.updater.misc;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.os.SystemProperties; import android.os.SystemProperties;
import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import org.json.JSONArray; import org.json.JSONArray;
@@ -28,6 +30,7 @@ import org.lineageos.updater.R;
import org.lineageos.updater.Update; import org.lineageos.updater.Update;
import org.lineageos.updater.UpdateDownload; import org.lineageos.updater.UpdateDownload;
import org.lineageos.updater.UpdateStatus; import org.lineageos.updater.UpdateStatus;
import org.lineageos.updater.UpdatesDbHelper;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
@@ -201,4 +204,45 @@ public class Utils {
Log.e(TAG, "Entry " + entryPath + " not found"); Log.e(TAG, "Entry " + entryPath + " not found");
throw new IllegalArgumentException("The given entry was not found"); throw new IllegalArgumentException("The given entry was not found");
} }
/**
* Cleanup the download directory, which is assumed to be a privileged location
* the user can't access and that might have stale files. This can happen if
* the data of the application are wiped.
*
* @param context
*/
public static void cleanupDownloadsDir(Context context) {
final String DOWNLOADS_CLEANUP_DONE = "cleanup_done";
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences.getBoolean(DOWNLOADS_CLEANUP_DONE, false)) {
return;
}
File downloadPath = getDownloadPath(context);
Log.d(TAG, "Cleaning " + downloadPath);
if (!downloadPath.isDirectory()) {
return;
}
File[] files = downloadPath.listFiles();
if (files == null) {
return;
}
// Ideally the database is empty when we get here
UpdatesDbHelper dbHelper = new UpdatesDbHelper(context);
List<String> knownPaths = new ArrayList<>();
for (UpdateDownload update : dbHelper.getUpdates()) {
knownPaths.add(update.getFile().getAbsolutePath());
}
for (File file : files) {
if (!knownPaths.contains(file.getAbsolutePath())) {
Log.d(TAG, "Deleting " + file.getAbsolutePath());
file.delete();
}
}
preferences.edit().putBoolean(DOWNLOADS_CLEANUP_DONE, true).apply();
}
} }