summaryrefslogtreecommitdiffstats
path: root/src/com/android/providers/downloads/DownloadThread.java
diff options
context:
space:
mode:
authorJeff Sharkey <jsharkey@android.com>2017-07-15 12:23:05 -0600
committerJeff Sharkey <jsharkey@android.com>2017-07-15 15:04:30 -0600
commitd635ac295850ba23d528c02b1e5c6eb44b64b22b (patch)
tree4c371e93cf162adffcb891752bdeb3f902252d18 /src/com/android/providers/downloads/DownloadThread.java
parentc442fbd95ad5b3de9e58a11e431f419edb4fe978 (diff)
downloadandroid_packages_providers_DownloadProvider-d635ac295850ba23d528c02b1e5c6eb44b64b22b.tar.gz
android_packages_providers_DownloadProvider-d635ac295850ba23d528c02b1e5c6eb44b64b22b.tar.bz2
android_packages_providers_DownloadProvider-d635ac295850ba23d528c02b1e5c6eb44b64b22b.zip
Allocate space using new StorageManager API.
Instead of reaching directly into PackageManager, use the new StorageManager API to allocate disk space for downloads. This wraps both clearing cached files and fallocate() into a single method. Remove support for storing downloads on the /cache partition, which doesn't exist on many devices. Bug: 63057877 Test: bit DownloadProviderTests:* Exempt-From-Owner-Approval: Bug 63673347 Change-Id: I5749f7a2f7ded9157fea763dc652bf4da88d86ff
Diffstat (limited to 'src/com/android/providers/downloads/DownloadThread.java')
-rw-r--r--src/com/android/providers/downloads/DownloadThread.java47
1 files changed, 15 insertions, 32 deletions
diff --git a/src/com/android/providers/downloads/DownloadThread.java b/src/com/android/providers/downloads/DownloadThread.java
index b9849569..9c920053 100644
--- a/src/com/android/providers/downloads/DownloadThread.java
+++ b/src/com/android/providers/downloads/DownloadThread.java
@@ -25,6 +25,7 @@ import static android.provider.Downloads.Impl.STATUS_CANCELED;
import static android.provider.Downloads.Impl.STATUS_CANNOT_RESUME;
import static android.provider.Downloads.Impl.STATUS_FILE_ERROR;
import static android.provider.Downloads.Impl.STATUS_HTTP_DATA_ERROR;
+import static android.provider.Downloads.Impl.STATUS_INSUFFICIENT_SPACE_ERROR;
import static android.provider.Downloads.Impl.STATUS_PAUSED_BY_APP;
import static android.provider.Downloads.Impl.STATUS_QUEUED_FOR_WIFI;
import static android.provider.Downloads.Impl.STATUS_RUNNING;
@@ -63,6 +64,7 @@ import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.os.Process;
import android.os.SystemClock;
+import android.os.storage.StorageManager;
import android.provider.Downloads;
import android.system.ErrnoException;
import android.system.Os;
@@ -116,6 +118,7 @@ public class DownloadThread extends Thread {
private final SystemFacade mSystemFacade;
private final DownloadNotifier mNotifier;
private final NetworkPolicyManager mNetworkPolicy;
+ private final StorageManager mStorage;
private final DownloadJobService mJobService;
private final JobParameters mParams;
@@ -244,6 +247,7 @@ public class DownloadThread extends Thread {
mSystemFacade = Helpers.getSystemFacade(mContext);
mNotifier = Helpers.getDownloadNotifier(mContext);
mNetworkPolicy = mContext.getSystemService(NetworkPolicyManager.class);
+ mStorage = mContext.getSystemService(StorageManager.class);
mJobService = service;
mParams = params;
@@ -563,35 +567,23 @@ public class DownloadThread extends Thread {
out = new ParcelFileDescriptor.AutoCloseOutputStream(outPfd);
}
- // Pre-flight disk space requirements, when known
- if (mInfoDelta.mTotalBytes > 0) {
- final long curSize = Os.fstat(outFd).st_size;
- final long newBytes = mInfoDelta.mTotalBytes - curSize;
-
- StorageUtils.ensureAvailableSpace(mContext, outFd, newBytes);
-
- try {
- // We found enough space, so claim it for ourselves
- Os.posix_fallocate(outFd, 0, mInfoDelta.mTotalBytes);
- } catch (ErrnoException e) {
- if (e.errno == OsConstants.ENOSYS || e.errno == OsConstants.ENOTSUP) {
- Log.w(TAG, "fallocate() not supported; falling back to ftruncate()");
- Os.ftruncate(outFd, mInfoDelta.mTotalBytes);
- } else {
- throw e;
- }
- }
- }
-
// Move into place to begin writing
Os.lseek(outFd, mInfoDelta.mCurrentBytes, OsConstants.SEEK_SET);
-
} catch (ErrnoException e) {
throw new StopRequestException(STATUS_FILE_ERROR, e);
} catch (IOException e) {
throw new StopRequestException(STATUS_FILE_ERROR, e);
}
+ try {
+ // Pre-flight disk space requirements, when known
+ if (mInfoDelta.mTotalBytes > 0 && mStorage.isAllocationSupported(outFd)) {
+ mStorage.allocateBytes(outFd, mInfoDelta.mTotalBytes);
+ }
+ } catch (IOException e) {
+ throw new StopRequestException(STATUS_INSUFFICIENT_SPACE_ERROR, e);
+ }
+
// Start streaming data, periodically watch for pause/cancel
// commands and checking disk space as needed.
transferData(in, out, outFd);
@@ -649,14 +641,6 @@ public class DownloadThread extends Thread {
}
try {
- // When streaming, ensure space before each write
- if (mInfoDelta.mTotalBytes == -1) {
- final long curSize = Os.fstat(outFd).st_size;
- final long newBytes = (mInfoDelta.mCurrentBytes + len) - curSize;
-
- StorageUtils.ensureAvailableSpace(mContext, outFd, newBytes);
- }
-
out.write(buffer, 0, len);
mMadeProgress = true;
@@ -664,8 +648,6 @@ public class DownloadThread extends Thread {
updateProgress(outFd);
- } catch (ErrnoException e) {
- throw new StopRequestException(STATUS_FILE_ERROR, e);
} catch (IOException e) {
throw new StopRequestException(STATUS_FILE_ERROR, e);
}
@@ -673,7 +655,8 @@ public class DownloadThread extends Thread {
// Finished without error; verify length if known
if (mInfoDelta.mTotalBytes != -1 && mInfoDelta.mCurrentBytes != mInfoDelta.mTotalBytes) {
- throw new StopRequestException(STATUS_HTTP_DATA_ERROR, "Content length mismatch");
+ throw new StopRequestException(STATUS_HTTP_DATA_ERROR, "Content length mismatch; found "
+ + mInfoDelta.mCurrentBytes + " instead of " + mInfoDelta.mTotalBytes);
}
}