Commit 0a0ab8d5 by olly Committed by Oliver Woodman

Avoid infinite recursion if cache file modified underneath cache

This generalizes our "does file still exist" check to also check
that the file is the expected length. If it's not, we don't trust
it. This avoids infinite recursion in CacheDataSource if a cache
file is truncated underneath the cache.

Issue: #6165
PiperOrigin-RevId: 265707928
parent aa6ead3d
...@@ -697,9 +697,9 @@ public final class SimpleCache implements Cache { ...@@ -697,9 +697,9 @@ public final class SimpleCache implements Cache {
} }
while (true) { while (true) {
SimpleCacheSpan span = cachedContent.getSpan(position); SimpleCacheSpan span = cachedContent.getSpan(position);
if (span.isCached && !span.file.exists()) { if (span.isCached && span.file.length() != span.length) {
// The file has been deleted from under us. It's likely that other files will have been // The file has been modified or deleted underneath us. It's likely that other files will
// deleted too, so scan the whole in-memory representation. // have been modified too, so scan the whole in-memory representation.
removeStaleSpans(); removeStaleSpans();
continue; continue;
} }
...@@ -739,14 +739,14 @@ public final class SimpleCache implements Cache { ...@@ -739,14 +739,14 @@ public final class SimpleCache implements Cache {
} }
/** /**
* Scans all of the cached spans in the in-memory representation, removing any for which files no * Scans all of the cached spans in the in-memory representation, removing any for which the
* longer exist. * underlying file lengths no longer match.
*/ */
private void removeStaleSpans() { private void removeStaleSpans() {
ArrayList<CacheSpan> spansToBeRemoved = new ArrayList<>(); ArrayList<CacheSpan> spansToBeRemoved = new ArrayList<>();
for (CachedContent cachedContent : contentIndex.getAll()) { for (CachedContent cachedContent : contentIndex.getAll()) {
for (CacheSpan span : cachedContent.getSpans()) { for (CacheSpan span : cachedContent.getSpans()) {
if (!span.file.exists()) { if (span.file.length() != span.length) {
spansToBeRemoved.add(span); spansToBeRemoved.add(span);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment