From 60f0eac7a8b191ad32187539ae0cdaa324258312 Mon Sep 17 00:00:00 2001 From: Fedor Korotkov Date: Fri, 8 Dec 2023 01:43:43 -0500 Subject: [PATCH] Cache only non-empty archives (#685) Fixes #684. But I'm not sure how it got into this state in the first place. `URLSession.shared.data` should've throw. --- Sources/tart/Commands/Run.swift | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Sources/tart/Commands/Run.swift b/Sources/tart/Commands/Run.swift index c29814e..91fc2ca 100644 --- a/Sources/tart/Commands/Run.swift +++ b/Sources/tart/Commands/Run.swift @@ -659,7 +659,7 @@ struct DirectoryShare { let archiveRequest = URLRequest(url: path, cachePolicy: .returnCacheDataElseLoad) var response: CachedURLResponse? = urlCache.cachedResponse(for: archiveRequest) - if (response == nil) { + if (response == nil || response?.data.isEmpty == true) { print("Downloading \(path)...") // download and unarchive remote directories if needed here // use old school API to prevent deadlocks since we are running via MainActor @@ -667,8 +667,12 @@ struct DirectoryShare { Task { do { let (archiveData, archiveResponse) = try await URLSession.shared.data(for: archiveRequest) - urlCache.storeCachedResponse(CachedURLResponse(response: archiveResponse, data: archiveData, storagePolicy: .allowed), for: archiveRequest) - print("Cached for future invocations!") + if archiveData.isEmpty { + print("Remote archive is empty!") + } else { + urlCache.storeCachedResponse(CachedURLResponse(response: archiveResponse, data: archiveData, storagePolicy: .allowed), for: archiveRequest) + print("Cached for future invocations!") + } } catch { print("Download failed: \(error)") }