Merge pull request #8154 from samoylenkodmitry:handle_out_of_space

PiperOrigin-RevId: 345428731
This commit is contained in:
Oliver Woodman 2020-12-03 17:12:15 +00:00 committed by Ian Baker
parent 2516e94eb9
commit 8c935e366c

View File

@ -409,18 +409,20 @@ public final class SimpleCache implements Cache {
Assertions.checkNotNull(cachedContent); Assertions.checkNotNull(cachedContent);
Assertions.checkState(cachedContent.isFullyLocked(position, length)); Assertions.checkState(cachedContent.isFullyLocked(position, length));
if (!cacheDir.exists()) { if (!cacheDir.exists()) {
// For some reason the cache directory doesn't exist. Make a best effort to create it. // The cache directory has been deleted from underneath us. Recreate it, and remove in-memory
cacheDir.mkdirs(); // spans corresponding to cache files that no longer exist.
createCacheDirectories(cacheDir);
removeStaleSpans(); removeStaleSpans();
} }
evictor.onStartFile(this, key, position, length); evictor.onStartFile(this, key, position, length);
// Randomly distribute files into subdirectories with a uniform distribution. // Randomly distribute files into subdirectories with a uniform distribution.
File fileDir = new File(cacheDir, Integer.toString(random.nextInt(SUBDIRECTORY_COUNT))); File cacheSubDir = new File(cacheDir, Integer.toString(random.nextInt(SUBDIRECTORY_COUNT)));
if (!fileDir.exists()) { if (!cacheSubDir.exists()) {
fileDir.mkdir(); createCacheDirectories(cacheSubDir);
} }
long lastTouchTimestamp = System.currentTimeMillis(); long lastTouchTimestamp = System.currentTimeMillis();
return SimpleCacheSpan.getCacheFile(fileDir, cachedContent.id, position, lastTouchTimestamp); return SimpleCacheSpan.getCacheFile(
cacheSubDir, cachedContent.id, position, lastTouchTimestamp);
} }
@Override @Override
@ -548,10 +550,10 @@ public final class SimpleCache implements Cache {
/** Ensures that the cache's in-memory representation has been initialized. */ /** Ensures that the cache's in-memory representation has been initialized. */
private void initialize() { private void initialize() {
if (!cacheDir.exists()) { if (!cacheDir.exists()) {
if (!cacheDir.mkdirs()) { try {
String message = "Failed to create cache directory: " + cacheDir; createCacheDirectories(cacheDir);
Log.e(TAG, message); } catch (CacheException e) {
initializationException = new CacheException(message); initializationException = e;
return; return;
} }
} }
@ -834,6 +836,14 @@ public final class SimpleCache implements Cache {
return Long.parseLong(fileName.substring(0, fileName.indexOf('.')), /* radix= */ 16); return Long.parseLong(fileName.substring(0, fileName.indexOf('.')), /* radix= */ 16);
} }
private static void createCacheDirectories(File cacheDir) throws CacheException {
if (!cacheDir.mkdirs()) {
String message = "Failed to create cache directory: " + cacheDir;
Log.e(TAG, message);
throw new CacheException(message);
}
}
private static synchronized boolean lockFolder(File cacheDir) { private static synchronized boolean lockFolder(File cacheDir) {
return lockedCacheDirs.add(cacheDir.getAbsoluteFile()); return lockedCacheDirs.add(cacheDir.getAbsoluteFile());
} }