This repository has been archived by the owner on May 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.swift
107 lines (91 loc) · 2.97 KB
/
main.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
import Foundation
import os
let imageMounts = [
"Virtual Machines": "/Users/Michael/Library/VM/VM.sparsebundle"
].filter {
// only include existing images
FileManager.default.fileExists(atPath: $0.value)
}
switch CommandLine.argc {
case 1:
print(imageMounts.keys.joined(separator: "\n"))
case 2:
guard let image = imageMounts[CommandLine.arguments[1]] else {
exit(EX_USAGE)
}
// drop privileges to the owner of the disk image
let attributes = try? FileManager.default.attributesOfItem(atPath: image)
if let group = (attributes?[.groupOwnerAccountID] as? NSNumber)?.uint32Value {
setgid(group)
}
if let user = (attributes?[.ownerAccountID] as? NSNumber)?.uint32Value {
setuid(user)
}
// compact the disk image for about 10% of mount attempts
let hdiutil = URL(fileURLWithPath: "/usr/bin/hdiutil")
if Float.random(in: 0...1) < 0.1 {
guard let process = try? Process.run(hdiutil, arguments: ["compact", image, "-quiet"]) else {
exit(EX_OSERR)
}
process.waitUntilExit()
if process.terminationStatus == EX_OK {
os_log("compacted disk image ‘%{public}s’", image)
}
// compaction expectedly fails when the image is already attached
}
// attach the image without mounting it
let result: AttachInfo
do {
let process = Process()
let pipe = Pipe()
process.executableURL = hdiutil
process.arguments = ["attach", image, "-plist", "-nomount", "-noverify", "-noautofsck"]
process.standardOutput = pipe
try process.run()
process.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let status = process.terminationStatus
if !(data.count > 0 && status == EX_OK) {
os_log("attaching the disk image ‘%{public}s’ failed with error code %d", image, status)
exit(EX_UNAVAILABLE)
}
result = try AttachInfo(from: data)
}
catch {
exit(EX_OSERR)
}
// now that we know the details, definitely print output when finished
defer {
print("-fstype=\(result.type),nobrowse,nodev,nosuid :\(result.device)")
}
// early exit if the image has been mounted by previous automount invocations
if result.mounted { break }
// run filesystem check if the image is not mounted
do {
let process = Process()
process.executableURL = URL(fileURLWithPath: "/sbin/fsck_\(result.type)")
process.arguments = ["-q", result.device]
process.standardOutput = FileHandle.nullDevice
try process.run()
process.waitUntilExit()
let status = process.terminationStatus
if status != EX_OK {
os_log("the file system in disk image ‘%{public}s’ needs repair, error code: %d", image, status)
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/sbin/diskutil")
process.arguments = ["repairVolume", result.device]
process.standardOutput = FileHandle.nullDevice
try process.run()
process.waitUntilExit()
let status = process.terminationStatus
if status != EX_OK {
os_log("the file system could not be repaired, error code: %d", status)
}
}
}
catch {
exit(EX_OSERR)
}
default:
exit(EX_USAGE)
}