consistently use FileHelper.openInputStreamSafe to resolve input streams

This commit is contained in:
Vincent Breitmoser
2024-01-29 10:34:51 +01:00
parent 034eab9df1
commit ba9c93d046
8 changed files with 18 additions and 14 deletions

View File

@@ -141,7 +141,7 @@ public class ImportKeysListLoader
if (ls.mKeyBytes != null) {
inputData = new InputData(new ByteArrayInputStream(ls.mKeyBytes), ls.mKeyBytes.length);
} else if (ls.mDataUri != null) {
InputStream inputStream = mContext.getContentResolver().openInputStream(ls.mDataUri);
InputStream inputStream = FileHelper.openInputStreamSafe(mContext.getContentResolver(), ls.mDataUri);
long length = FileHelper.getFileSize(mContext, ls.mDataUri, -1);
inputData = new InputData(inputStream, length);

View File

@@ -57,6 +57,7 @@ import org.sufficientlysecure.keychain.service.BackupKeyringParcel;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.util.CountingOutputStream;
import org.sufficientlysecure.keychain.util.FileHelper;
import org.sufficientlysecure.keychain.util.InputData;
import org.sufficientlysecure.keychain.util.Numeric9x4PassphraseUtil;
import org.sufficientlysecure.keychain.util.Passphrase;
@@ -175,7 +176,7 @@ public class BackupOperation extends BaseOperation<BackupKeyringParcel> {
}
PgpSignEncryptData pgpSignEncryptData = builder.build();
InputStream inStream = mContext.getContentResolver().openInputStream(plainUri);
InputStream inStream = FileHelper.openInputStreamSafe(mContext.getContentResolver(), plainUri);
String filename;
long[] masterKeyIds = backupInput.getMasterKeyIds();

View File

@@ -55,6 +55,7 @@ import org.sufficientlysecure.keychain.provider.TemporaryFileProvider;
import org.sufficientlysecure.keychain.service.InputDataParcel;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
import org.sufficientlysecure.keychain.util.CharsetVerifier;
import org.sufficientlysecure.keychain.util.FileHelper;
/** This operation deals with input data, trying to determine its type as it goes.
@@ -381,7 +382,7 @@ public class InputDataOperation extends BaseOperation<InputDataParcel> {
try {
// open current uri for input
InputStream in = mContext.getContentResolver().openInputStream(currentInputUri);
InputStream in = FileHelper.openInputStreamSafe(mContext.getContentResolver(), currentInputUri);
parser.parse(in);
if (mSignedDataUri != null) {
@@ -394,7 +395,7 @@ public class InputDataOperation extends BaseOperation<InputDataParcel> {
// the actual content is the signed data now (and will be passed verbatim, if parsing fails)
currentInputUri = mSignedDataUri;
in = mContext.getContentResolver().openInputStream(currentInputUri);
in = FileHelper.openInputStreamSafe(mContext.getContentResolver(), currentInputUri);
// reset signed data result, to indicate to the parser that it is in the inner part
mSignedDataResult = null;
parser.parse(in);

View File

@@ -109,7 +109,7 @@ public class PgpDecryptVerifyOperation extends BaseOperation<PgpDecryptVerifyInp
inputData = new InputData(new ByteArrayInputStream(inputBytes), inputBytes.length);
} else {
try {
InputStream inputStream = mContext.getContentResolver().openInputStream(input.getInputUri());
InputStream inputStream = FileHelper.openInputStreamSafe(mContext.getContentResolver(), input.getInputUri());
long inputSize = FileHelper.getFileSize(mContext, input.getInputUri(), 0);
inputData = new InputData(inputStream, inputSize);
} catch (SecurityException e) {

View File

@@ -41,6 +41,7 @@ import org.sufficientlysecure.keychain.ui.util.recyclerview.ItemTouchHelperAdapt
import org.sufficientlysecure.keychain.ui.util.recyclerview.ItemTouchHelperDragCallback;
import org.sufficientlysecure.keychain.ui.util.recyclerview.ItemTouchHelperViewHolder;
import org.sufficientlysecure.keychain.ui.util.recyclerview.RecyclerItemClickListener;
import org.sufficientlysecure.keychain.util.FileHelper;
import java.io.IOException;
import java.io.InputStream;
@@ -205,7 +206,7 @@ public class SettingsSmartPGPAuthorityFragment extends Fragment implements Recyc
if (uri == null) {
new_cert = old_cert;
} else {
final InputStream fis = getContext().getContentResolver().openInputStream(Uri.parse(uri));
final InputStream fis = FileHelper.openInputStreamSafe(getContext().getContentResolver(), Uri.parse(uri));
final CertificateFactory cf = CertificateFactory.getInstance("X.509");
new_cert = cf.generateCertificate(fis);

View File

@@ -47,6 +47,7 @@ import org.sufficientlysecure.keychain.pgp.UncachedKeyRing.IteratorWithIOThrow;
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
import org.sufficientlysecure.keychain.securitytoken.SecurityTokenInfo;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.util.FileHelper;
import org.sufficientlysecure.keychain.util.ParcelableProxy;
import org.sufficientlysecure.keychain.util.Preferences;
import timber.log.Timber;
@@ -206,7 +207,7 @@ public class PublicKeyRetriever {
log.add(LogType.MSG_RET_CURI_START, 0);
log.add(LogType.MSG_RET_CURI_OPEN, 1, uri.toString());
InputStream is = context.getContentResolver().openInputStream(uri);
InputStream is = FileHelper.openInputStreamSafe(context.getContentResolver(), uri);
if (is == null) {
log.add(LogType.MSG_RET_CURI_ERROR_NOT_FOUND, 1);
return KeyRetrievalResult.createWithError(log);

View File

@@ -49,9 +49,9 @@ import android.os.Build.VERSION_CODES;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.OpenableColumns;
import androidx.fragment.app.Fragment;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import org.sufficientlysecure.keychain.R;
import timber.log.Timber;
@@ -232,7 +232,7 @@ public class FileHelper {
BufferedReader br = null;
try {
InputStream is = context.getContentResolver().openInputStream(uri);
InputStream is = FileHelper.openInputStreamSafe(context.getContentResolver(), uri);
br = new BufferedReader(new InputStreamReader(is));
String header = "-----BEGIN PGP MESSAGE-----";
@@ -264,7 +264,7 @@ public class FileHelper {
byte[] decryptedMessage;
{
InputStream in = context.getContentResolver().openInputStream(outputUri);
InputStream in = FileHelper.openInputStreamSafe(context.getContentResolver(), outputUri);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[256];
int read;
@@ -298,7 +298,7 @@ public class FileHelper {
try {
ContentResolver resolver = context.getContentResolver();
bis = new BufferedInputStream(resolver.openInputStream(fromUri));
bis = new BufferedInputStream(FileHelper.openInputStreamSafe(resolver, fromUri));
bos = new BufferedOutputStream(resolver.openOutputStream(toUri));
byte[] buf = new byte[1024];
int len;

View File

@@ -23,7 +23,6 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import android.annotation.TargetApi;
import android.content.ContentResolver;
import android.content.res.AssetFileDescriptor;
import android.net.Uri;
@@ -33,6 +32,7 @@ import android.system.ErrnoException;
import android.system.Os;
import android.system.StructStat;
import androidx.annotation.RequiresApi;
import timber.log.Timber;
@@ -43,7 +43,7 @@ import timber.log.Timber;
* lollipop-only class. All methods here should only be called by FileHelper,
* and consequently have package visibility.
*/
@TargetApi(VERSION_CODES.LOLLIPOP)
@RequiresApi(VERSION_CODES.LOLLIPOP)
class FileHelperLollipop {
/**
@@ -73,7 +73,7 @@ class FileHelperLollipop {
throw new FileNotFoundException("Unable to create stream");
}
} catch (ErrnoException e) {
Timber.e("fstat() failed: " + e);
Timber.e(e, "fstat() failed");
throw new FileNotFoundException("fstat() failed");
}