Securily delete files

This commit is contained in:
M66B 2023-11-16 11:10:34 +01:00
parent 7eb345cdd6
commit 6bdab773bb
14 changed files with 53 additions and 46 deletions

View File

@ -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;
} finally {
file.delete();
Helper.secureDelete(file);
}
}

View File

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

View File

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

View File

@ -403,7 +403,7 @@ public class EntityAttachment {
DB db = DB.getInstance(context);
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) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -151,6 +151,8 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
@ -2834,6 +2836,16 @@ public class Helper {
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() {
StatFs stats = new StatFs(Environment.getDataDirectory().getAbsolutePath());
return stats.getAvailableBlocksLong() * stats.getBlockSizeLong();

View File

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

View File

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