From 93a1b70ecb74dc3b35fd50905f5bad2f0f0b827b Mon Sep 17 00:00:00 2001 From: Fedor Korotkov Date: Thu, 13 Jul 2023 05:59:23 -0400 Subject: [PATCH] Don't delete an initiator of pruning (#556) * Don't delete an initiator of pruning Sometimes people have an image that is greater than half of the disk itself. In that case such image will be pulled and prunned right away. This change makes sure that an image that is being cloned from is not pruned right away. * Resolve symbolic links --- Sources/tart/Commands/Clone.swift | 2 +- Sources/tart/Commands/Prune.swift | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Sources/tart/Commands/Clone.swift b/Sources/tart/Commands/Clone.swift index 8b4016c..799c57b 100644 --- a/Sources/tart/Commands/Clone.swift +++ b/Sources/tart/Commands/Clone.swift @@ -53,7 +53,7 @@ struct Clone: AsyncParsableCommand { // APFS is doing copy-on-write so the above cloning operation (just copying files on disk) // is not actually claiming new space until the VM is started and it writes something to disk. // So once we clone the VM let's try to claim a little bit of space for the VM to run. - try Prune.reclaimIfNeeded(UInt64(sourceVM.sizeBytes())) + try Prune.reclaimIfNeeded(UInt64(sourceVM.sizeBytes()), sourceVM) }, onCancel: { try? FileManager.default.removeItem(at: tmpVMDir.baseURL) }) diff --git a/Sources/tart/Commands/Prune.swift b/Sources/tart/Commands/Prune.swift index ca9dadc..0521c29 100644 --- a/Sources/tart/Commands/Prune.swift +++ b/Sources/tart/Commands/Prune.swift @@ -77,7 +77,7 @@ struct Prune: AsyncParsableCommand { try prunablesToDelete.forEach { try $0.delete() } } - static func reclaimIfNeeded(_ requiredBytes: UInt64) throws { + static func reclaimIfNeeded(_ requiredBytes: UInt64, _ initiator: Prunable? = nil) throws { SentrySDK.configureScope { scope in scope.setContext(value: ["requiredBytes": requiredBytes], key: "Prune") } @@ -114,10 +114,10 @@ struct Prune: AsyncParsableCommand { return } - try Prune.reclaimIfPossible(requiredBytes - volumeAvailableCapacityCalculated) + try Prune.reclaimIfPossible(requiredBytes - volumeAvailableCapacityCalculated, initiator) } - private static func reclaimIfPossible(_ reclaimBytes: UInt64) throws { + private static func reclaimIfPossible(_ reclaimBytes: UInt64, _ initiator: Prunable? = nil) throws { let transaction = SentrySDK.startTransaction(name: "Pruning cache", operation: "prune", bindToScope: true) defer { transaction.finish() } @@ -141,6 +141,11 @@ struct Prune: AsyncParsableCommand { break } + if prunable.url == initiator?.url.resolvingSymlinksInPath() { + // do not prune the initiator + continue + } + try SentrySDK.span?.setData(value: prunable.sizeBytes(), key: prunable.url.path) cacheReclaimedBytes += try prunable.sizeBytes()