-
Notifications
You must be signed in to change notification settings - Fork 16
/
snapUtil.c
245 lines (211 loc) · 5.44 KB
/
snapUtil.c
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* Copyright 2017 Adam H. Leventhal. All Rights Reserved.
*/
#include <unistd.h>
#include <fcntl.h>
#include <sys/syscall.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <errno.h>
#include <sys/attr.h>
#include <sys/snapshot.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
const char *g_pname;
void
usage(void)
{
(void) fprintf(stderr, "Usage:\n");
(void) fprintf(stderr, "\t%s -l <vol> (List all snapshots)\n", g_pname);
(void) fprintf(stderr, "\t%s -c <snap> <vol> (Create snapshot)\n", g_pname);
(void) fprintf(stderr, "\t%s -n <snap> <newname> <vol> (Rename snapshot)\n", g_pname);
(void) fprintf(stderr, "\t%s -d <snap> <vol> (Delete snapshot)\n", g_pname);
(void) fprintf(stderr, "\t%s -r <snap> <vol> (Revert to snapshot)\n", g_pname);
(void) fprintf(stderr, "\t%s -s <snap> <vol> <mntpnt> (Mount snapshot)\n", g_pname);
(void) fprintf(stderr, "\t%s -o (Print original snapshot name)\n", g_pname);
exit(2);
}
int
do_create(const char *vol, const char *snap)
{
int dirfd = open(vol, O_RDONLY, 0);
if (dirfd < 0) {
perror("open");
exit(1);
}
int ret = fs_snapshot_create(dirfd, snap, 0);
if (ret != 0)
perror("fs_snapshot_create");
return (ret);
}
int
do_delete(const char *vol, const char *snap)
{
int dirfd = open(vol, O_RDONLY, 0);
if (dirfd < 0) {
perror("open");
exit(1);
}
int ret = fs_snapshot_delete(dirfd, snap, 0);
if (ret != 0)
perror("fs_snapshot_delete");
return (ret);
}
int
do_revert(const char *vol, const char *snap)
{
int dirfd = open(vol, O_RDONLY, 0);
if (dirfd < 0) {
perror("open");
exit(1);
}
int ret = fs_snapshot_revert(dirfd, snap, 0);
if (ret != 0)
perror("fs_snapshot_revert");
return (ret);
}
int
do_rename(const char *vol, const char *snap, const char *nw)
{
int dirfd = open(vol, O_RDONLY, 0);
if (dirfd < 0) {
perror("open");
exit(1);
}
int ret = fs_snapshot_rename(dirfd, snap, nw, 0);
if (ret != 0)
perror("fs_snapshot_rename");
return (ret);
}
int
do_mount(const char *vol, const char *snap, const char *mntpnt)
{
int dirfd = open(vol, O_RDONLY, 0);
if (dirfd < 0) {
perror("open");
exit(1);
}
int ret = fs_snapshot_mount(dirfd, mntpnt, snap, 0);
if (ret != 0) {
perror("fs_snapshot_mount");
} else {
printf("mount_apfs: snapshot implicitly mounted readonly\n");
}
return (ret);
}
int
do_list(const char *vol)
{
int dirfd = open(vol, O_RDONLY, 0);
if (dirfd < 0) {
perror("open");
exit(1);
}
struct attrlist alist = { 0 };
char abuf[2048];
alist.commonattr = ATTR_BULK_REQUIRED;
for (;;) {
int count = fs_snapshot_list(dirfd, &alist, &abuf[0], sizeof (abuf), 0);
if (count < 0) {
perror("fs_snapshot_list");
exit(1);
} else if (count == 0) {
break;
} else {
char *p = &abuf[0];
for (int i = 0; i < count; i++) {
char *field = p;
uint32_t len = *(uint32_t *)field;
field += sizeof (uint32_t);
attribute_set_t attrs = *(attribute_set_t *)field;
field += sizeof (attribute_set_t);
if (attrs.commonattr & ATTR_CMN_NAME) {
attrreference_t ar = *(attrreference_t *)field;
char *name = field + ar.attr_dataoffset;
field += sizeof (attrreference_t);
(void) printf("%s\n", name);
}
p += len;
}
}
}
return (0);
}
int
do_origName(void) {
const UInt8 *bytes;
CFIndex length;
CFDataRef manifestHash, rootSnapshotName;
io_registry_entry_t chosen = IORegistryEntryFromPath(0, "IODeviceTree:/chosen");
rootSnapshotName = IORegistryEntryCreateCFProperty(chosen, CFSTR("root-snapshot-name"), kCFAllocatorDefault, 0);
if (rootSnapshotName != NULL && CFGetTypeID(rootSnapshotName) == CFDataGetTypeID()) {
CFStringRef snapshotString = CFStringCreateFromExternalRepresentation(kCFAllocatorDefault, rootSnapshotName, kCFStringEncodingUTF8);
CFRelease(rootSnapshotName);
char buffer[100];
const char *ptr = CFStringGetCStringPtr(snapshotString, kCFStringEncodingUTF8);
if (ptr == NULL) {
if (CFStringGetCString(snapshotString, buffer, 100, kCFStringEncodingUTF8))
ptr = buffer;
}
printf("%s\n", ptr);
} else {
manifestHash = (CFDataRef)IORegistryEntryCreateCFProperty(chosen, CFSTR("boot-manifest-hash"), kCFAllocatorDefault, 0);
IOObjectRelease(chosen);
if (manifestHash == NULL || CFGetTypeID(manifestHash) != CFDataGetTypeID()) {
fprintf(stderr, "Unable to read boot-manifest-hash or root-snapshot-name\n");
return 1;
}
length = CFDataGetLength(manifestHash);
bytes = CFDataGetBytePtr(manifestHash);
CFRelease(manifestHash);
printf("com.apple.os.update-");
for (int i = 0; i < length; i++)
printf("%02X", bytes[i]);
printf("\n");
}
return 0;
}
int
main(int argc, char **argv)
{
g_pname = argv[0];
if (argc < 2 || argv[1][0] != '-' ||
argv[1][1] == '\0' || argv[1][2] != '\0') {
usage();
}
switch (argv[1][1]) {
case 'l':
if (argc != 3)
usage();
return (do_list(argv[2]));
case 'c':
if (argc != 4)
usage();
return (do_create(argv[3], argv[2]));
case 'd':
if (argc != 4)
usage();
return (do_delete(argv[3], argv[2]));
case 'n':
if (argc != 5)
usage();
return (do_rename(argv[4], argv[2], argv[3]));
case 's':
if (argc != 5)
usage();
return (do_mount(argv[3], argv[2], argv[4]));
case 'r':
if (argc != 4)
usage();
return (do_revert(argv[3], argv[2]));
case 'o':
if (argc != 2)
usage();
return (do_origName());
default:
usage();
}
return (0);
}