[Buildsystem] Prevent cache check mangling access time

Checking for text files using `open` changes `atime`, which breaks cache
access sorting, this ensures the cache access time is preserved.
This commit is contained in:
A Thousand Ships 2024-11-21 17:43:00 +01:00 committed by AThousandShips
parent 9e6098432a
commit 31c8aadc47
No known key found for this signature in database
GPG Key ID: DEFC5A5B1306947D

View File

@ -863,16 +863,21 @@ def clean_cache(cache_path: str, cache_limit: int, verbose: bool):
texts = [] texts = []
stats = [] stats = []
for file in files: for file in files:
try:
# Save file stats to rewrite after modifying.
tmp_stat = os.stat(file)
# Failing a utf-8 decode is the easiest way to determine if a file is binary. # Failing a utf-8 decode is the easiest way to determine if a file is binary.
try: try:
with open(file, encoding="utf-8") as out: with open(file, encoding="utf-8") as out:
out.read(1024) out.read(1024)
except UnicodeDecodeError: except UnicodeDecodeError:
stats.append((file, *os.stat(file)[6:8])) stats.append((file, *tmp_stat[6:8]))
except OSError: # Restore file stats after reading.
print_error(f'Failed to access cache file "{file}"; skipping.') os.utime(file, (tmp_stat[7], tmp_stat[8]))
else: else:
texts.append(file) texts.append(file)
except OSError:
print_error(f'Failed to access cache file "{file}"; skipping.')
if texts: if texts:
count = len(texts) count = len(texts)