diff options
4 files changed, 59 insertions, 0 deletions
diff --git a/apex/blobstore/framework/java/android/app/blob/BlobStoreManager.java b/apex/blobstore/framework/java/android/app/blob/BlobStoreManager.java index 47af7c0a5ed9..00d9efbb1fcd 100644 --- a/apex/blobstore/framework/java/android/app/blob/BlobStoreManager.java +++ b/apex/blobstore/framework/java/android/app/blob/BlobStoreManager.java @@ -323,6 +323,28 @@ public class BlobStoreManager { } /** + * Opens a file descriptor to read the blob content already written into this session. + * + * @return a {@link ParcelFileDescriptor} for reading from the blob file. + * + * @throws IOException when there is an I/O error while opening the file to read. + * @throws SecurityException when the caller is not the owner of the session. + * @throws IllegalStateException when the caller tries to read the file after it is + * abandoned (using {@link #abandon()}) + * or closed (using {@link #close()}). + */ + public @NonNull ParcelFileDescriptor openRead() throws IOException { + try { + return mSession.openRead(); + } catch (ParcelableException e) { + e.maybeRethrow(IOException.class); + throw new RuntimeException(e); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + + /** * Gets the size of the blob file that was written to the session so far. * * @return the size of the blob file so far. diff --git a/apex/blobstore/framework/java/android/app/blob/IBlobStoreSession.aidl b/apex/blobstore/framework/java/android/app/blob/IBlobStoreSession.aidl index 4ae919bfedab..4035b96938d9 100644 --- a/apex/blobstore/framework/java/android/app/blob/IBlobStoreSession.aidl +++ b/apex/blobstore/framework/java/android/app/blob/IBlobStoreSession.aidl @@ -21,6 +21,7 @@ import android.os.ParcelFileDescriptor; /** {@hide} */ interface IBlobStoreSession { ParcelFileDescriptor openWrite(long offsetBytes, long lengthBytes); + ParcelFileDescriptor openRead(); void allowPackageAccess(in String packageName, in byte[] certificate); void allowSameSignatureAccess(); diff --git a/apex/blobstore/service/java/com/android/server/blob/BlobStoreSession.java b/apex/blobstore/service/java/com/android/server/blob/BlobStoreSession.java index 29092b327fc7..612fd89ebbe0 100644 --- a/apex/blobstore/service/java/com/android/server/blob/BlobStoreSession.java +++ b/apex/blobstore/service/java/com/android/server/blob/BlobStoreSession.java @@ -17,6 +17,7 @@ package com.android.server.blob; import static android.app.blob.BlobStoreManager.COMMIT_RESULT_ERROR; import static android.system.OsConstants.O_CREAT; +import static android.system.OsConstants.O_RDONLY; import static android.system.OsConstants.O_RDWR; import static android.system.OsConstants.SEEK_SET; @@ -187,6 +188,40 @@ public class BlobStoreSession extends IBlobStoreSession.Stub { } @Override + @NonNull + public ParcelFileDescriptor openRead() { + assertCallerIsOwner(); + synchronized (mSessionLock) { + if (mState != STATE_OPENED) { + throw new IllegalStateException("Not allowed to read in state: " + + stateToString(mState)); + } + + try { + return openReadLocked(); + } catch (IOException e) { + throw ExceptionUtils.wrap(e); + } + } + } + + @GuardedBy("mSessionLock") + @NonNull + private ParcelFileDescriptor openReadLocked() throws IOException { + FileDescriptor fd = null; + try { + final File sessionFile = getSessionFile(); + if (sessionFile == null) { + throw new IllegalStateException("Couldn't get the file for this session"); + } + fd = Os.open(sessionFile.getPath(), O_RDONLY, 0); + } catch (ErrnoException e) { + e.rethrowAsIOException(); + } + return createRevocableFdLocked(fd); + } + + @Override @BytesLong public long getSize() { return 0; diff --git a/api/current.txt b/api/current.txt index 63cf1c6a4465..c05034744ac8 100644 --- a/api/current.txt +++ b/api/current.txt @@ -7547,6 +7547,7 @@ package android.app.blob { method public boolean isPackageAccessAllowed(@NonNull String, @NonNull byte[]) throws java.io.IOException; method public boolean isPublicAccessAllowed() throws java.io.IOException; method public boolean isSameSignatureAccessAllowed() throws java.io.IOException; + method @NonNull public android.os.ParcelFileDescriptor openRead() throws java.io.IOException; method @NonNull public android.os.ParcelFileDescriptor openWrite(long, long) throws java.io.IOException; } |