Skip to content

Commit

Permalink
Do not require write to read from single backup uri.
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-signal authored and cody-signal committed Nov 11, 2020
1 parent d307db8 commit 6bf300a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import android.graphics.Paint;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.provider.DocumentsContract;
import android.text.Editable;
import android.text.Spanned;
import android.text.TextWatcher;
Expand All @@ -28,7 +26,6 @@
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.documentfile.provider.DocumentFile;
import androidx.navigation.Navigation;

import com.dd.CircularProgressButton;
Expand All @@ -52,7 +49,6 @@
import org.thoughtcrime.securesms.service.LocalBackupListener;
import org.thoughtcrime.securesms.util.BackupUtil;
import org.thoughtcrime.securesms.util.DateUtils;
import org.thoughtcrime.securesms.util.StorageUtil;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Util;
import org.thoughtcrime.securesms.util.concurrent.SimpleTask;
Expand Down Expand Up @@ -206,7 +202,7 @@ static void getFromUri(@NonNull Context context,
@NonNull Uri backupUri,
@NonNull OnBackupSearchResultListener listener)
{
SimpleTask.run(() -> BackupUtil.getBackupInfoForUri(context, backupUri),
SimpleTask.run(() -> BackupUtil.getBackupInfoFromSingleUri(context, backupUri),
listener::run);
}

Expand Down
29 changes: 16 additions & 13 deletions app/src/main/java/org/thoughtcrime/securesms/util/BackupUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -166,15 +167,13 @@ private static List<BackupInfo> getAllBackupsNewestFirstApi29() {
}

@RequiresApi(29)
public static @Nullable BackupInfo getBackupInfoForUri(@NonNull Context context, @NonNull Uri uri) {
DocumentFile documentFile = DocumentFile.fromSingleUri(context, uri);

if (documentFile != null && documentFile.exists() && documentFile.canRead() && documentFile.canWrite() && documentFile.getName().endsWith(".backup")) {
long backupTimestamp = getBackupTimestamp(documentFile.getName());
public static @Nullable BackupInfo getBackupInfoFromSingleUri(@NonNull Context context, @NonNull Uri singleUri) {
DocumentFile documentFile = DocumentFile.fromSingleUri(context, singleUri);

if (isBackupFileReadable(documentFile)) {
long backupTimestamp = getBackupTimestamp(Objects.requireNonNull(documentFile.getName()));
return new BackupInfo(backupTimestamp, documentFile.length(), documentFile.getUri());
} else {
logIssueWithDocumentFile(documentFile);
Log.w(TAG, "Could not load backup info.");
return null;
}
Expand Down Expand Up @@ -260,17 +259,21 @@ private static long getBackupTimestamp(@NonNull String backupName) {
return -1;
}

private static void logIssueWithDocumentFile(@Nullable DocumentFile documentFile) {
private static boolean isBackupFileReadable(@Nullable DocumentFile documentFile) {
if (documentFile == null) {
throw new AssertionError("We do not support platforms prior to KitKat.");
} else if (!documentFile.exists()) {
Log.w(TAG, "The document at the specified Uri cannot be found.");
Log.w(TAG, "isBackupFileReadable: The document at the specified Uri cannot be found.");
return false;
} else if (!documentFile.canRead()) {
Log.w(TAG, "The document at the specified Uri cannot be read.");
} else if (!documentFile.canWrite()) {
Log.w(TAG, "The document at the specified Uri cannot be written to.");
} else if (!documentFile.getName().endsWith(".backup")) {
Log.w(TAG, "The document at the specified Uri has an unsupported file extension.");
Log.w(TAG, "isBackupFileReadable: The document at the specified Uri cannot be read.");
return false;
} else if (TextUtils.isEmpty(documentFile.getName()) || !documentFile.getName().endsWith(".backup")) {
Log.w(TAG, "isBackupFileReadable: The document at the specified Uri has an unsupported file extension.");
return false;
} else {
Log.i(TAG, "isBackupFileReadable: The document at the specified Uri looks like a readable backup");
return true;
}
}

Expand Down

0 comments on commit 6bf300a

Please sign in to comment.