Securily delete files

pull/214/head
M66B 1 year ago
parent 7eb345cdd6
commit 6bdab773bb

@ -1514,7 +1514,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
return Log.getDebugInfo(context, "crash", R.string.title_crash_info_remark, null, sb.toString(), null).id; return Log.getDebugInfo(context, "crash", R.string.title_crash_info_remark, null, sb.toString(), null).id;
} finally { } finally {
file.delete(); Helper.secureDelete(file);
} }
} }

@ -183,8 +183,7 @@ public class ContactInfo {
for (File file : favicons) for (File file : favicons)
if (file.lastModified() + CACHE_FAVICON_DURATION < now) { if (file.lastModified() + CACHE_FAVICON_DURATION < now) {
Log.i("Deleting " + file); Log.i("Deleting " + file);
if (!file.delete()) Helper.secureDelete(file);
Log.w("Error deleting " + file);
} }
} }
} }
@ -210,7 +209,7 @@ public class ContactInfo {
File[] favicons = dir.listFiles(); File[] favicons = dir.listFiles();
if (favicons != null) if (favicons != null)
for (File favicon : favicons) for (File favicon : favicons)
favicon.delete(); Helper.secureDelete(favicon);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
} }

@ -167,7 +167,7 @@ public abstract class DB extends RoomDatabase {
} }
} catch (SQLiteDatabaseCorruptException ex) { } catch (SQLiteDatabaseCorruptException ex) {
Log.e(ex); Log.e(ex);
dbfile.delete(); Helper.secureDelete(dbfile);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
/* /*
@ -325,7 +325,7 @@ public abstract class DB extends RoomDatabase {
Log.e(ex); Log.e(ex);
} }
} else } else
emergency.delete(); Helper.secureDelete(emergency);
} }
private static void checkEmergencyBackup(Context context) { private static void checkEmergencyBackup(Context context) {
@ -1625,7 +1625,7 @@ public abstract class DB extends RoomDatabase {
File[] raws = new File(context.getFilesDir(), "raw").listFiles(); File[] raws = new File(context.getFilesDir(), "raw").listFiles();
if (raws != null) if (raws != null)
for (File file : raws) for (File file : raws)
file.delete(); Helper.secureDelete(file);
} }
}) })
.addMigrations(new Migration(122, 123) { .addMigrations(new Migration(122, 123) {

@ -403,7 +403,7 @@ public class EntityAttachment {
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
db.attachment().setName(id, name + ".zip", "application/zip", zip.length()); db.attachment().setName(id, name + ".zip", "application/zip", zip.length());
file.delete(); Helper.secureDelete(file);
} }
public static boolean equals(List<EntityAttachment> a1, List<EntityAttachment> a2) { public static boolean equals(List<EntityAttachment> a1, List<EntityAttachment> a2) {

@ -4143,7 +4143,7 @@ public class FragmentCompose extends FragmentBase {
return null; return null;
} else if (OpenPgpApi.ACTION_ENCRYPT.equals(data.getAction()) || } else if (OpenPgpApi.ACTION_ENCRYPT.equals(data.getAction()) ||
OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction())) { OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction())) {
input.delete(); Helper.secureDelete(input);
// send message // send message
args.putInt("action", largs.getInt("action")); args.putInt("action", largs.getInt("action"));
@ -4157,7 +4157,7 @@ public class FragmentCompose extends FragmentBase {
return result.getParcelableExtra(OpenPgpApi.RESULT_INTENT); return result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
case OpenPgpApi.RESULT_CODE_ERROR: case OpenPgpApi.RESULT_CODE_ERROR:
input.delete(); Helper.secureDelete(input);
db.identity().setIdentitySignKey(identity.id, null); db.identity().setIdentitySignKey(identity.id, null);
OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR); OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
if (error != null && if (error != null &&
@ -4172,7 +4172,7 @@ public class FragmentCompose extends FragmentBase {
throw new IllegalStateException("OpenPgp unknown result code=" + resultCode); throw new IllegalStateException("OpenPgp unknown result code=" + resultCode);
} }
} finally { } finally {
output.delete(); Helper.secureDelete(output);
} }
} }
@ -4403,7 +4403,7 @@ public class FragmentCompose extends FragmentBase {
CMSSignedData cmsSignedData = cmsGenerator.generate(cmsData); CMSSignedData cmsSignedData = cmsGenerator.generate(cmsData);
byte[] signedMessage = cmsSignedData.getEncoded(); byte[] signedMessage = cmsSignedData.getEncoded();
sinput.delete(); Helper.secureDelete(sinput);
// Build signature // Build signature
if (EntityMessage.SMIME_SIGNONLY.equals(type)) { if (EntityMessage.SMIME_SIGNONLY.equals(type)) {
@ -4555,7 +4555,7 @@ public class FragmentCompose extends FragmentBase {
cmsEnvelopedData.toASN1Structure().encodeTo(os); cmsEnvelopedData.toASN1Structure().encodeTo(os);
} }
einput.delete(); Helper.secureDelete(einput);
db.attachment().setDownloaded(attachment.id, encrypted.length()); db.attachment().setDownloaded(attachment.id, encrypted.length());
@ -5132,8 +5132,7 @@ public class FragmentCompose extends FragmentBase {
// content://eu.faircode.email/photo/nnn.jpg // content://eu.faircode.email/photo/nnn.jpg
File tmp = new File(context.getFilesDir(), uri.getPath()); File tmp = new File(context.getFilesDir(), uri.getPath());
Log.i("Deleting " + tmp); Log.i("Deleting " + tmp);
if (!tmp.delete()) Helper.secureDelete(tmp);
Log.w("Error deleting " + tmp);
} else } else
Log.i("Authority=" + uri.getAuthority()); Log.i("Authority=" + uri.getAuthority());
@ -5262,13 +5261,13 @@ public class FragmentCompose extends FragmentBase {
throw new IOException("compress"); throw new IOException("compress");
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
tmp.delete(); Helper.secureDelete(tmp);
} finally { } finally {
resized.recycle(); resized.recycle();
} }
if (tmp.exists() && tmp.length() > 0) { if (tmp.exists() && tmp.length() > 0) {
file.delete(); Helper.secureDelete(file);
tmp.renameTo(file); tmp.renameTo(file);
} }
@ -6027,7 +6026,7 @@ public class FragmentCompose extends FragmentBase {
File file = attachment.getFile(context); File file = attachment.getFile(context);
Helper.copy(ics, file); Helper.copy(ics, file);
ics.delete(); Helper.secureDelete(ics);
ICalendar icalendar = CalendarHelper.parse(context, file); ICalendar icalendar = CalendarHelper.parse(context, file);
VEvent event = icalendar.getEvents().get(0); VEvent event = icalendar.getEvents().get(0);
@ -6169,7 +6168,7 @@ public class FragmentCompose extends FragmentBase {
File refFile = data.draft.getRefFile(context); File refFile = data.draft.getRefFile(context);
if (refFile.exists()) { if (refFile.exists()) {
ref.html(Helper.readText(refFile)); ref.html(Helper.readText(refFile));
refFile.delete(); Helper.secureDelete(refFile);
} }
// Possibly external draft // Possibly external draft

@ -212,7 +212,7 @@ public class FragmentDialogPrint extends FragmentDialogBase {
if (out.exists() && out.length() > 0) if (out.exists() && out.length() > 0)
continue; continue;
} else { } else {
out.delete(); Helper.secureDelete(out);
continue; continue;
} }

@ -9061,7 +9061,7 @@ public class FragmentMessages extends FragmentBase
throw new IllegalStateException("OpenPgp unknown result code=" + resultCode); throw new IllegalStateException("OpenPgp unknown result code=" + resultCode);
} }
} finally { } finally {
plain.delete(); Helper.secureDelete(plain);
} }
return null; return null;

@ -1662,8 +1662,10 @@ public class FragmentOptionsBackup extends FragmentBase implements SharedPrefere
File dir = Helper.ensureExists(new File(context.getFilesDir(), "syncdata")); File dir = Helper.ensureExists(new File(context.getFilesDir(), "syncdata"));
File[] files = dir.listFiles(); File[] files = dir.listFiles();
if (files != null) if (files != null)
for (File file : files) for (File file : files) {
Log.i("Cloud delete " + file + "=" + file.delete()); Log.i("Cloud delete " + file);
Helper.secureDelete(file);
}
} }
} }

@ -1121,7 +1121,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
File dest = new File(target, attachment.getName()); File dest = new File(target, attachment.getName());
Log.i("Move " + attachment + " to " + dest); Log.i("Move " + attachment + " to " + dest);
Helper.copy(attachment, dest); Helper.copy(attachment, dest);
attachment.delete(); Helper.secureDelete(attachment);
} }
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
@ -1141,9 +1141,9 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
File dest = new File(t, message.getName()); File dest = new File(t, message.getName());
Log.i("Move " + message + " to " + dest); Log.i("Move " + message + " to " + dest);
Helper.copy(message, dest); Helper.copy(message, dest);
message.delete(); Helper.secureDelete(message);
} }
dir.delete(); Helper.secureDelete(dir);
} }
} }

@ -260,7 +260,7 @@ public class Fts4DbHelper extends SQLiteOpenHelper {
for (File file : db.getParentFile().listFiles()) for (File file : db.getParentFile().listFiles())
if (file.getName().startsWith(DATABASE_NAME)) { if (file.getName().startsWith(DATABASE_NAME)) {
Log.i("FTS delete=" + file); Log.i("FTS delete=" + file);
file.delete(); Helper.secureDelete(file);
} }
} }
} }

@ -257,7 +257,7 @@ public class Fts5DbHelper extends SQLiteOpenHelper {
for (File file : db.getParentFile().listFiles()) for (File file : db.getParentFile().listFiles())
if (file.getName().startsWith(DATABASE_NAME)) { if (file.getName().startsWith(DATABASE_NAME)) {
Log.i("FTS delete=" + file); Log.i("FTS delete=" + file);
file.delete(); Helper.secureDelete(file);
} }
} }
} }

@ -151,6 +151,8 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.text.DateFormat; import java.text.DateFormat;
@ -2834,6 +2836,16 @@ public class Helper {
return files; return files;
} }
static void secureDelete(File file) {
if (file.exists()) {
try {
Files.delete(Paths.get(file.getAbsolutePath()));
} catch (IOException ex) {
Log.e(ex);
}
}
}
static long getAvailableStorageSpace() { static long getAvailableStorageSpace() {
StatFs stats = new StatFs(Environment.getDataDirectory().getAbsolutePath()); StatFs stats = new StatFs(Environment.getDataDirectory().getAbsolutePath());
return stats.getAvailableBlocksLong() * stats.getBlockSizeLong(); return stats.getAvailableBlocksLong() * stats.getBlockSizeLong();

@ -454,7 +454,7 @@ public class MessageClassifier {
File file = getFile(context, false); File file = getFile(context, false);
File backup = getFile(context, true); File backup = getFile(context, true);
backup.delete(); Helper.secureDelete(backup);
if (file.exists()) if (file.exists())
file.renameTo(backup); file.renameTo(backup);
@ -532,7 +532,7 @@ public class MessageClassifier {
writer.endObject(); writer.endObject();
} }
backup.delete(); Helper.secureDelete(backup);
dirty = false; dirty = false;
@ -553,7 +553,7 @@ public class MessageClassifier {
_load(file); _load(file);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
file.delete(); Helper.secureDelete(file);
clear(context); clear(context);
} }
} }

@ -199,7 +199,7 @@ public class WorkerCleanup extends Worker {
for (File file : files) for (File file : files)
if (file.getName().endsWith(".pma")) { if (file.getName().endsWith(".pma")) {
Log.i("Deleting " + file); Log.i("Deleting " + file);
file.delete(); Helper.secureDelete(file);
} }
} }
} }
@ -240,12 +240,11 @@ public class WorkerCleanup extends Worker {
EntityMessage message = db.message().getMessage(id); EntityMessage message = db.message().getMessage(id);
if (message == null || !message.content) { if (message == null || !message.content) {
Log.i("Deleting " + file); Log.i("Deleting " + file);
if (!file.delete()) Helper.secureDelete(file);
Log.w("Error deleting " + file);
} }
} catch (NumberFormatException ex) { } catch (NumberFormatException ex) {
Log.e(file.getAbsolutePath(), ex); Log.e(file.getAbsolutePath(), ex);
file.delete(); Helper.secureDelete(file);
} }
// Cleanup raw message files // Cleanup raw message files
@ -267,12 +266,11 @@ public class WorkerCleanup extends Worker {
} }
if (message == null || message.raw == null || !message.raw) { if (message == null || message.raw == null || !message.raw) {
Log.i("Deleting " + file); Log.i("Deleting " + file);
if (!file.delete()) Helper.secureDelete(file);
Log.w("Error deleting " + file);
} }
} catch (NumberFormatException ex) { } catch (NumberFormatException ex) {
Log.e(file.getAbsolutePath(), ex); Log.e(file.getAbsolutePath(), ex);
file.delete(); Helper.secureDelete(file);
} }
} }
@ -287,12 +285,11 @@ public class WorkerCleanup extends Worker {
EntityAttachment attachment = db.attachment().getAttachment(id); EntityAttachment attachment = db.attachment().getAttachment(id);
if (attachment == null || !attachment.available) { if (attachment == null || !attachment.available) {
Log.i("Deleting " + file); Log.i("Deleting " + file);
if (!file.delete()) Helper.secureDelete(file);
Log.w("Error deleting " + file);
} }
} catch (NumberFormatException ex) { } catch (NumberFormatException ex) {
Log.e(file.getAbsolutePath(), ex); Log.e(file.getAbsolutePath(), ex);
file.delete(); Helper.secureDelete(file);
} }
// Cleanup cached images // Cleanup cached images
@ -307,12 +304,11 @@ public class WorkerCleanup extends Worker {
if (manual || message == null || if (manual || message == null ||
file.lastModified() + KEEP_IMAGES_DURATION < now) { file.lastModified() + KEEP_IMAGES_DURATION < now) {
Log.i("Deleting " + file); Log.i("Deleting " + file);
if (!file.delete()) Helper.secureDelete(file);
Log.w("Error deleting " + file);
} }
} catch (NumberFormatException ex) { } catch (NumberFormatException ex) {
Log.e(file.getAbsolutePath(), ex); Log.e(file.getAbsolutePath(), ex);
file.delete(); Helper.secureDelete(file);
} }
// Cleanup shared files // Cleanup shared files
@ -321,8 +317,7 @@ public class WorkerCleanup extends Worker {
for (File file : shared) for (File file : shared)
if (manual || file.lastModified() + KEEP_FILES_DURATION < now) { if (manual || file.lastModified() + KEEP_FILES_DURATION < now) {
Log.i("Deleting " + file); Log.i("Deleting " + file);
if (!file.delete()) Helper.secureDelete(file);
Log.w("Error deleting " + file);
} }
// Cleanup contact info // Cleanup contact info

Loading…
Cancel
Save