forked from utmapp/UTM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUTMConfigurationDrive.swift
124 lines (108 loc) · 4.5 KB
/
UTMConfigurationDrive.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
//
// Copyright © 2022 osy. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
/// Settings for single disk device
protocol UTMConfigurationDrive: Codable, Hashable, Identifiable {
/// If not removable, this is the name of the file in the bundle.
var imageName: String? { get set }
/// Size of the image when creating a new image (in MiB).
var sizeMib: Int { get }
/// If true, the drive image will be mounted as read-only.
var isReadOnly: Bool { get }
/// If true, a bookmark is stored in the package.
var isExternal: Bool { get }
/// If true, the created image will be raw format and not QCOW2. Not saved.
var isRawImage: Bool { get }
/// If valid, will point to the actual location of the drive image. Not saved.
var imageURL: URL? { get set }
/// Unique identifier for this drive
var id: String { get }
/// Create a new copy with a unique ID
/// - Returns: Copy
func clone() -> Self
}
extension UTMConfigurationDrive {
static func == (lhs: Self, rhs: Self) -> Bool {
lhs.hashValue == rhs.hashValue
}
}
// MARK: - Saving data
extension UTMConfigurationDrive {
private var bytesInMib: UInt64 { 1048576 }
@MainActor mutating func saveData(to dataURL: URL) async throws -> [URL] {
guard !isExternal else {
return [] // nothing to save
}
let fileManager = FileManager.default
if let imageURL = imageURL {
#if os(macOS)
let newURL = try await UTMQemuConfiguration.copyItemIfChanged(from: imageURL, to: dataURL, customCopy: isRawImage ? nil : convertQcow2Image)
#else
let newURL = try await UTMQemuConfiguration.copyItemIfChanged(from: imageURL, to: dataURL)
#endif
self.imageName = newURL.lastPathComponent
self.imageURL = newURL
return [newURL]
} else if imageName == nil {
let newName = "\(id).\(isRawImage ? "img" : "qcow2")"
let newURL = dataURL.appendingPathComponent(newName)
guard !fileManager.fileExists(atPath: newURL.path) else {
throw UTMConfigurationError.driveAlreadyExists(newURL)
}
if isRawImage {
try await createRawImage(at: newURL, size: sizeMib)
} else {
try await createQcow2Image(at: newURL, size: sizeMib)
}
self.imageName = newName
self.imageURL = newURL
return [newURL]
} else {
let existingURL = dataURL.appendingPathComponent(imageName!)
return [existingURL]
}
}
private func createRawImage(at newURL: URL, size sizeMib: Int) async throws {
let fileManager = FileManager.default
let size = UInt64(sizeMib) * bytesInMib
try await Task.detached {
guard fileManager.createFile(atPath: newURL.path, contents: nil, attributes: nil) else {
throw UTMConfigurationError.cannotCreateDiskImage
}
let handle = try FileHandle(forWritingTo: newURL)
try handle.truncate(atOffset: size)
try handle.close()
}.value
}
private func createQcow2Image(at newURL: URL, size sizeMib: Int) async throws {
try await Task.detached {
if !GenerateDefaultQcow2File(newURL as CFURL, sizeMib) {
throw UTMConfigurationError.cannotCreateDiskImage
}
}.value
}
#if os(macOS)
private func convertQcow2Image(at sourceURL: URL, to destURL: URL) async throws -> URL {
let fileManager = FileManager.default
let destQcow2 = destURL.deletingPathExtension().appendingPathExtension("qcow2")
guard !fileManager.fileExists(atPath: destQcow2.path) else {
throw UTMConfigurationError.driveAlreadyExists(destQcow2)
}
try await UTMQemuImage.convert(from: sourceURL, toQcow2: destQcow2)
return destQcow2
}
#endif
}