-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathFileManager.swift
136 lines (107 loc) · 4.09 KB
/
FileManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//
// FileSystemAdapter.swift
// UTM Snapshot-Manager
//
// Created by Jan Zombik on 04.03.23.
//
import Foundation
import Cocoa
extension FileManager {
static func qcow2FileURLsAt(_ url: URL?) -> [URL] {
guard let urlUnwrapped = url else {
return []
}
let resourceKeys = Set<URLResourceKey>([.nameKey, .isDirectoryKey])
let directoryEnumerator = FileManager.default.enumerator(at: urlUnwrapped, includingPropertiesForKeys: Array(resourceKeys), options: .skipsHiddenFiles)!
var qcow2FileURLs: [URL] = []
for case let fileURL as URL in directoryEnumerator {
guard let resourceValues = try? fileURL.resourceValues(forKeys: resourceKeys),
let isDirectory = resourceValues.isDirectory,
let name = resourceValues.name
else {
continue
}
if isDirectory {
if name == "_extras" {
directoryEnumerator.skipDescendants()
}
continue
}
if fileURL.pathExtension == "qcow2" {
qcow2FileURLs.append(fileURL)
}
}
return qcow2FileURLs
}
static func utmPackageURLsAt(_ url: URL?) -> [URL] {
guard let urlUnwrapped = url else {
return []
}
if self.isValidUTMPackageUrl(urlUnwrapped) {
return [urlUnwrapped]
}
let resourceKeys = Set<URLResourceKey>([.nameKey, .isDirectoryKey])
let directoryEnumerator = FileManager.default.enumerator(at: urlUnwrapped, includingPropertiesForKeys: Array(resourceKeys), options: .skipsHiddenFiles)!
var utmPackageURLs: [URL] = []
for case let fileURL as URL in directoryEnumerator {
guard let resourceValues = try? fileURL.resourceValues(forKeys: resourceKeys),
let isDirectory = resourceValues.isDirectory,
let name = resourceValues.name
else {
continue
}
if isDirectory && name == "_extras" {
directoryEnumerator.skipDescendants()
continue
}
if self.isValidUTMPackageUrl(fileURL) {
utmPackageURLs.append(fileURL)
}
}
return utmPackageURLs
}
static func utmPackageURLsAt(_ urls: [URL]) -> [URL] {
var utmPackageURLs: [URL] = []
for url in urls {
utmPackageURLs.append(contentsOf: self.utmPackageURLsAt(url))
}
return utmPackageURLs
}
static func isValidQcow2ImageUrl(_ url: URL) -> Bool {
if !FileManager.default.fileExists(atPath: url.path(percentEncoded: false)) {
return false
}
if !FileManager.default.isReadableFile(atPath: url.path(percentEncoded: false)) {
return false
}
if !FileManager.default.isWritableFile(atPath: url.path(percentEncoded: false)) {
return false
}
if url.pathExtension != "qcow2" {
return false
}
return true
}
static func isValidUTMPackageUrl(_ url: URL) -> Bool {
var isDirectory = ObjCBool(false)
if !FileManager.default.fileExists(atPath: url.path(percentEncoded: false), isDirectory: &isDirectory) {
return false
}
if !isDirectory.boolValue {
return false;
}
if !FileManager.default.isReadableFile(atPath: url.path(percentEncoded: false)) {
return false
}
if !FileManager.default.isWritableFile(atPath: url.path(percentEncoded: false)) {
return false
}
if url.pathExtension != "utm" {
return false
}
if !NSWorkspace.shared.isFilePackage(atPath: url.path(percentEncoded: false)) {
return false
}
return true
}
}