Skip to content

Commit 4879a46

Browse files
committed
data: prevent duplicate VMs from appearing
Thanks @DevSecTim for triaging. Fixes #7533
1 parent 4784026 commit 4879a46

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

Platform/UTMData.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ enum AlertItem: Identifiable {
158158
}
159159
// now look for and add new VMs in default storage
160160
do {
161-
let files = try fileManager.contentsOfDirectory(at: UTMData.defaultStorageUrl, includingPropertiesForKeys: [.isDirectoryKey], options: .skipsHiddenFiles)
161+
let files = try fileManager.contentsOfDirectory(at: UTMData.defaultStorageUrl, includingPropertiesForKeys: [.isDirectoryKey, .fileResourceIdentifierKey], options: .skipsHiddenFiles)
162162
let newFiles = files.filter { newFile in
163163
!list.contains { existingVM in
164-
existingVM.pathUrl.standardizedFileURL == newFile.standardizedFileURL
164+
isSameFile(existingVM.pathUrl, as: newFile)
165165
}
166166
}
167167
for file in newFiles {
@@ -635,6 +635,19 @@ enum AlertItem: Identifiable {
635635
}
636636
}
637637

638+
/// Compare two files and sees if they are the same
639+
/// - Parameters:
640+
/// - url1: first file
641+
/// - url2: second file
642+
/// - Returns: If they are the same file
643+
func isSameFile(_ url1: URL, as url2: URL) -> Bool {
644+
if let id1 = try? url1.resourceValues(forKeys: [.fileResourceIdentifierKey]).fileResourceIdentifier,
645+
let id2 = try? url2.resourceValues(forKeys: [.fileResourceIdentifierKey]).fileResourceIdentifier {
646+
return id1.isEqual(id2)
647+
}
648+
return url1.standardizedFileURL == url2.standardizedFileURL
649+
}
650+
638651
/// Handles UTM file URLs
639652
///
640653
/// If .utm is already in the list, select it
@@ -660,7 +673,7 @@ enum AlertItem: Identifiable {
660673
let fileName = url.lastPathComponent
661674
let dest = documentsURL.appendingPathComponent(fileName, isDirectory: true)
662675
if let vm = virtualMachines.first(where: { vm -> Bool in
663-
return vm.pathUrl.standardizedFileURL == url.standardizedFileURL
676+
return isSameFile(vm.pathUrl, as: url)
664677
}) {
665678
logger.info("found existing vm!")
666679
if !vm.isLoaded {

0 commit comments

Comments
 (0)