1
0
mirror of https://github.com/moparisthebest/k-9 synced 2025-02-17 07:30:16 -05:00

Added some defensive code to prevent crashes caused by the LocalStore getting in a bad state due to issue 1205 (still being worked on). This fixes issue 1214.

This commit is contained in:
Bao-Long Nguyen-Trong 2010-02-11 21:16:37 +00:00
parent 8d8233bcc0
commit 6fe2dad5cc

View File

@ -269,7 +269,7 @@ public class LocalStore extends Store implements Serializable
long attachmentLength = 0; long attachmentLength = 0;
attachmentLength =+ getSize(mInternalAttachmentsDir); attachmentLength =+ getSize(mInternalAttachmentsDir);
if (useExternalAttachmentDir()) if (usesExternalAttachmentDir())
{ {
attachmentLength =+ getSize(mExternalAttachmentsDir); attachmentLength =+ getSize(mExternalAttachmentsDir);
} }
@ -283,11 +283,18 @@ public class LocalStore extends Store implements Serializable
long attachmentLength = 0; long attachmentLength = 0;
File[] files = attachmentsDir.listFiles(); File[] files = attachmentsDir.listFiles();
for (File file : files) if (files==null)
{ {
if (file.exists()) Log.w(K9.LOG_TAG, "Unable to list files: " + attachmentsDir.getAbsolutePath());
}
else
{
for (File file : files)
{ {
attachmentLength += file.length(); if (file.exists())
{
attachmentLength += file.length();
}
} }
} }
@ -422,7 +429,7 @@ public class LocalStore extends Store implements Serializable
} }
delete(mInternalAttachmentsDir); delete(mInternalAttachmentsDir);
if (useExternalAttachmentDir()) if (usesExternalAttachmentDir())
{ {
delete(mExternalAttachmentsDir); delete(mExternalAttachmentsDir);
} }
@ -440,16 +447,23 @@ public class LocalStore extends Store implements Serializable
private void delete(File attachmentsDir) { private void delete(File attachmentsDir) {
try { try {
File[] attachments = attachmentsDir.listFiles(); File[] attachments = attachmentsDir.listFiles();
for (File attachment : attachments) if (attachments==null)
{ {
if (attachment.exists()) Log.w(K9.LOG_TAG, "Unable to list files: " + attachmentsDir.getAbsolutePath());
{
attachment.delete();
}
} }
if (attachmentsDir.exists()) else
{ {
attachmentsDir.delete(); for (File attachment : attachments)
{
if (attachment.exists())
{
attachment.delete();
}
}
if (attachmentsDir.exists())
{
attachmentsDir.delete();
}
} }
} }
catch (Exception e) catch (Exception e)
@ -472,7 +486,7 @@ public class LocalStore extends Store implements Serializable
mDb.update("attachments", cv, null, null); mDb.update("attachments", cv, null, null);
} }
pruneCachedAttachments(force, mInternalAttachmentsDir); pruneCachedAttachments(force, mInternalAttachmentsDir);
if (useExternalAttachmentDir()) if (usesExternalAttachmentDir())
{ {
pruneCachedAttachments(force, mExternalAttachmentsDir); pruneCachedAttachments(force, mExternalAttachmentsDir);
} }
@ -483,64 +497,71 @@ public class LocalStore extends Store implements Serializable
*/ */
private void pruneCachedAttachments(boolean force, File attachmentsDir) throws MessagingException private void pruneCachedAttachments(boolean force, File attachmentsDir) throws MessagingException
{ {
File[] files = attachmentsDir.listFiles(); File[] files = attachmentsDir.listFiles();
for (File file : files) if (files==null)
{ {
if (file.exists()) Log.w(K9.LOG_TAG, "Unable to list files: " + attachmentsDir.getAbsolutePath());
}
else
{
for (File file : files)
{ {
if (!force) if (file.exists())
{ {
Cursor cursor = null; if (!force)
try
{ {
cursor = mDb.query( Cursor cursor = null;
"attachments", try
new String[] { "store_data" },
"id = ?",
new String[] { file.getName() },
null,
null,
null);
if (cursor.moveToNext())
{ {
if (cursor.getString(0) == null) cursor = mDb.query(
"attachments",
new String[] { "store_data" },
"id = ?",
new String[] { file.getName() },
null,
null,
null);
if (cursor.moveToNext())
{ {
Log.d(K9.LOG_TAG, "Attachment " + file.getAbsolutePath() + " has no store data, not deleting"); if (cursor.getString(0) == null)
/* {
* If the attachment has no store data it is not recoverable, so Log.d(K9.LOG_TAG, "Attachment " + file.getAbsolutePath() + " has no store data, not deleting");
* we won't delete it. /*
*/ * If the attachment has no store data it is not recoverable, so
continue; * we won't delete it.
*/
continue;
}
}
}
finally
{
if (cursor != null)
{
cursor.close();
} }
} }
} }
finally if (!force)
{ {
if (cursor != null) try
{ {
cursor.close(); ContentValues cv = new ContentValues();
cv.putNull("content_uri");
mDb.update("attachments", cv, "id = ?", new String[] { file.getName() });
}
catch (Exception e)
{
/*
* If the row has gone away before we got to mark it not-downloaded that's
* okay.
*/
} }
} }
} if (!file.delete())
if (!force)
{
try
{ {
ContentValues cv = new ContentValues(); file.deleteOnExit();
cv.putNull("content_uri");
mDb.update("attachments", cv, "id = ?", new String[] { file.getName() });
} }
catch (Exception e)
{
/*
* If the row has gone away before we got to mark it not-downloaded that's
* okay.
*/
}
}
if (!file.delete())
{
file.deleteOnExit();
} }
} }
} }
@ -2603,8 +2624,12 @@ public class LocalStore extends Store implements Serializable
private File getAttachmentsDir() private File getAttachmentsDir()
{ {
if (useExternalAttachmentDir()) if (usesExternalAttachmentDir())
{ {
if (!mExternalAttachmentsDir.exists())
{
mExternalAttachmentsDir.mkdirs();
}
return mExternalAttachmentsDir; return mExternalAttachmentsDir;
} }
else { else {
@ -2612,7 +2637,7 @@ public class LocalStore extends Store implements Serializable
} }
} }
private boolean useExternalAttachmentDir() private boolean usesExternalAttachmentDir()
{ {
if (mAccount.isStoreAttachmentOnSdCard()) { if (mAccount.isStoreAttachmentOnSdCard()) {
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))