@@ -162,7 +162,36 @@ extension Snapshot {
162162
163163 let fileManager = FileManager ( )
164164
165- /// Start by deleting and pruning roots as needed.
165+ /// Start by deleting and pruning roots as needed. We attempt to do this twice, as older versions of the persistence (prior to 0.4) didn't record the datastore ID along with the root id, which would therefor require extra work.
166+ /// First, delete the root entries we know to be removed.
167+ for datastoreRoot in datastoreRootsToPruneAndDelete {
168+ guard let datastoreID = datastoreRoot. datastoreID else { continue }
169+ let datastore = datastores [ datastoreID] ?? DiskPersistence < AccessMode > . Datastore ( id: datastoreID, snapshot: self )
170+ do {
171+ try await datastore. pruneRootObject ( with: datastoreRoot. datastoreRootID, mode: mode, shouldDelete: true )
172+ } catch URLError . fileDoesNotExist , CocoaError. fileReadNoSuchFile , CocoaError. fileNoSuchFile , POSIXError. ENOENT {
173+ /// This datastore root is already gone.
174+ } catch {
175+ print ( " Could not delete datastore root \( datastoreRoot) : \( error) " )
176+ throw error
177+ }
178+ datastoreRootsToPruneAndDelete. remove ( datastoreRoot)
179+ }
180+ /// Prune the root entries that were just added, as they themselves refer to other deleted assets.
181+ for datastoreRoot in datastoreRootsToPrune {
182+ guard let datastoreID = datastoreRoot. datastoreID else { continue }
183+ let datastore = datastores [ datastoreID] ?? DiskPersistence < AccessMode > . Datastore ( id: datastoreID, snapshot: self )
184+ do {
185+ try await datastore. pruneRootObject ( with: datastoreRoot. datastoreRootID, mode: mode, shouldDelete: false )
186+ } catch URLError . fileDoesNotExist , CocoaError. fileReadNoSuchFile , CocoaError. fileNoSuchFile , POSIXError. ENOENT {
187+ /// This datastore root is already gone.
188+ } catch {
189+ print ( " Could not prune datastore root \( datastoreRoot) : \( error) " )
190+ throw error
191+ }
192+ datastoreRootsToPrune. remove ( datastoreRoot)
193+ }
194+ /// If any regerences remain, funnel into this code path for very old persistences.
166195 if !datastoreRootsToPruneAndDelete. isEmpty || !datastoreRootsToPrune. isEmpty {
167196 for (_, datastoreInfo) in iteration. dataStores {
168197 /// Skip any roots for datastores being deleted, since we'll just unlink the whole directory in that case.
@@ -172,23 +201,27 @@ extension Snapshot {
172201
173202 /// Delete the root entries we know to be removed.
174203 for datastoreRoot in datastoreRootsToPruneAndDelete {
175- // TODO: Clean this up by also storing the datastore ID in with the root ID…
176204 do {
177- try await datastore. pruneRootObject ( with: datastoreRoot, mode: mode, shouldDelete: true )
205+ try await datastore. pruneRootObject ( with: datastoreRoot. datastoreRootID , mode: mode, shouldDelete: true )
178206 datastoreRootsToPruneAndDelete. remove ( datastoreRoot)
179- } catch {
207+ } catch URLError . fileDoesNotExist , CocoaError . fileReadNoSuchFile , CocoaError . fileNoSuchFile , POSIXError . ENOENT {
180208 /// This datastore did not contain the specified root, skip it for now.
209+ } catch {
210+ print ( " Could not delete datastore root \( datastoreRoot) : \( error) . " )
211+ throw error
181212 }
182213 }
183214
184215 /// Prune the root entries that were just added, as they themselves refer to other deleted assets.
185216 for datastoreRoot in datastoreRootsToPrune {
186- // TODO: Clean this up by also storing the datastore ID in with the root ID…
187217 do {
188- try await datastore. pruneRootObject ( with: datastoreRoot, mode: mode, shouldDelete: false )
218+ try await datastore. pruneRootObject ( with: datastoreRoot. datastoreRootID , mode: mode, shouldDelete: false )
189219 datastoreRootsToPrune. remove ( datastoreRoot)
190- } catch {
220+ } catch URLError . fileDoesNotExist , CocoaError . fileReadNoSuchFile , CocoaError . fileNoSuchFile , POSIXError . ENOENT {
191221 /// This datastore did not contain the specified root, skip it for now.
222+ } catch {
223+ print ( " Could not prune datastore root \( datastoreRoot) : \( error) . " )
224+ throw error
192225 }
193226 }
194227 }
0 commit comments