From feb733a7c09dcdd5e305603120948cddbd1573b7 Mon Sep 17 00:00:00 2001 From: Tommy <79723996+Tommygithubaccount123@users.noreply.github.com> Date: Mon, 31 Jul 2023 11:21:42 -0700 Subject: [PATCH] GC avoidance and tmpDeterminstic (#570) * GC avoidance and tmpDeterminstic * change tmpDeterministic to use hashing - temporaryDeterministic() now takes in a key and hashes it - creates directory with the hash * Update Sources/tart/VMStorageOCI.swift Co-authored-by: Fedor Korotkov --------- Co-authored-by: Fedor Korotkov --- Sources/tart/Root.swift | 10 ++++++---- Sources/tart/VMDirectory.swift | 12 ++++++++++++ Sources/tart/VMStorageOCI.swift | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Sources/tart/Root.swift b/Sources/tart/Root.swift index 52d15bd..1eeacfb 100644 --- a/Sources/tart/Root.swift +++ b/Sources/tart/Root.swift @@ -81,10 +81,12 @@ struct Root: AsyncParsableCommand { var command = try parseAsRoot() // Run garbage-collection before each command (shouldn't take too long) - do { - try Config().gc() - } catch { - fputs("Failed to perform garbage collection!\n\(error)\n", stderr) + if type(of: command) != type(of: Pull()) && type(of: command) != type(of: Clone()){ + do { + try Config().gc() + } catch { + fputs("Failed to perform garbage collection!\n\(error)\n", stderr) + } } if var asyncCommand = command as? AsyncParsableCommand { diff --git a/Sources/tart/VMDirectory.swift b/Sources/tart/VMDirectory.swift index abf9e34..ec755f6 100644 --- a/Sources/tart/VMDirectory.swift +++ b/Sources/tart/VMDirectory.swift @@ -1,5 +1,6 @@ import Foundation import Virtualization +import CryptoKit struct VMDirectory: Prunable { var baseURL: URL @@ -59,6 +60,17 @@ struct VMDirectory: Prunable { return VMDirectory(baseURL: tmpDir) } + //Create tmp directory with hashing + static func temporaryDeterministic(key: String) throws -> VMDirectory { + let keyData = Data(key.utf8) + let hash = Insecure.MD5.hash(data: keyData) + // Convert hash to string + let hashString = hash.compactMap { String(format: "%02x", $0) }.joined() + let tmpDir = try Config().tartTmpDir.appendingPathComponent(hashString) + try FileManager.default.createDirectory(at: tmpDir, withIntermediateDirectories: true) + return VMDirectory(baseURL: tmpDir) + } + var initialized: Bool { FileManager.default.fileExists(atPath: configURL.path) && FileManager.default.fileExists(atPath: diskURL.path) && diff --git a/Sources/tart/VMStorageOCI.swift b/Sources/tart/VMStorageOCI.swift index 8660048..d76ece7 100644 --- a/Sources/tart/VMStorageOCI.swift +++ b/Sources/tart/VMStorageOCI.swift @@ -170,7 +170,7 @@ class VMStorageOCI: PrunableStorage { if !exists(digestName) { let transaction = SentrySDK.startTransaction(name: name.description, operation: "pull", bindToScope: true) - let tmpVMDir = try VMDirectory.temporary() + let tmpVMDir = try VMDirectory.temporaryDeterministic(key: name.description) // Lock the temporary VM directory to prevent it's garbage collection let tmpVMDirLock = try FileLock(lockURL: tmpVMDir.baseURL)