]> git.saurik.com Git - apple/hfs.git/blob - tests/disk-image.m
hfs-407.50.6.tar.gz
[apple/hfs.git] / tests / disk-image.m
1 //
2 // disk-image.m
3 // hfs
4 //
5 // Created by Chris Suter on 8/12/15.
6 //
7 //
8
9 #include <unistd.h>
10 #include <spawn.h>
11 #include <sys/stat.h>
12 #include <sys/param.h>
13 #include <sys/mount.h>
14 #include <zlib.h>
15 #include <stdlib.h>
16 #include <fcntl.h>
17 #include <stdbool.h>
18
19 #include <Foundation/Foundation.h>
20 #include <TargetConditionals.h>
21
22 #include "disk-image.h"
23 #include "test-utils.h"
24 #include "systemx.h"
25
26 #if TARGET_OS_EMBEDDED
27
28 #include "dmg.dat"
29
30 bool disk_image_cleanup(disk_image_t *di)
31 {
32 pid_t pid;
33 bool result = false;
34
35 // We need to be root
36 assert(seteuid(0) == 0);
37
38 char *umount_args[]
39 = { "umount", "-f", (char *)di->mount_point, NULL };
40
41 assert_no_err(posix_spawn(&pid, "/sbin/umount", NULL, NULL, umount_args, NULL));
42
43 int status;
44 waitpid(pid, &status, 0);
45
46 char *detach_args[]
47 = { "hdik", "-e", (char *)di->disk, NULL };
48
49 posix_spawn_file_actions_t facts;
50 posix_spawn_file_actions_init(&facts);
51 posix_spawn_file_actions_addopen(&facts, STDOUT_FILENO, "/dev/null", O_APPEND, 0);
52 posix_spawn_file_actions_addopen(&facts, STDERR_FILENO, "/dev/null", O_APPEND, 0);
53
54 assert_no_err(posix_spawn(&pid, "/usr/sbin/hdik", &facts, NULL, detach_args, NULL));
55
56 posix_spawn_file_actions_destroy(&facts);
57
58 waitpid(pid, &status, 0);
59
60 struct stat sb;
61
62 if (WIFEXITED(status) && !WEXITSTATUS(status)
63 && stat(di->disk, &sb) == -1 && errno == ENOENT) {
64 unlink(di->path);
65 result = true;
66 // We are the last user of di, so free it.
67 free(di->mount_point);
68 free(di->disk);
69 free(di->path);
70 free(di);
71 }
72
73 return result;
74 }
75
76 void *zalloc(__unused void *opaque, uInt items, uInt size)
77 {
78 return malloc(items * size);
79 }
80
81 void zfree(__unused void *opaque, void *ptr)
82 {
83 free(ptr);
84 }
85
86 disk_image_t *disk_image_create(const char *path, disk_image_opts_t *opts)
87 {
88 disk_image_t *di;
89
90 di = calloc(1, sizeof(disk_image_t));
91 assert(di);
92
93 // We need to be root
94 uid_t uid_old;
95 if ((uid_old = geteuid()) != 0) {
96 assert_no_err(seteuid(0));
97 }
98
99 // Extract the image
100 int fd = open(path, O_RDWR | O_TRUNC | O_CREAT, 0666);
101
102 z_stream zs = {
103 .zalloc = zalloc,
104 .zfree = zfree,
105 };
106
107 inflateInit(&zs);
108
109 size_t buf_size = 1024 * 1024;
110 void *out_buf = malloc(buf_size);
111 assert(out_buf);
112
113 zs.next_in = data;
114 zs.avail_in = sizeof(data);
115
116 int ret;
117
118 do {
119 zs.next_out = out_buf;
120 zs.avail_out = buf_size;
121
122 ret = inflate(&zs, 0);
123
124 size_t todo = buf_size - zs.avail_out;
125
126 assert(write(fd, out_buf, todo) == (ssize_t)todo);
127 } while (ret == Z_OK);
128
129 assert(ret == Z_STREAM_END);
130
131 di->path = strdup(path);
132
133 // Attach it
134 pid_t pid;
135 char *attach_args[4] = { "hdik", "-nomount", (char *)di->path, NULL };
136 int fds[2];
137
138 assert_no_err(pipe(fds));
139
140 posix_spawn_file_actions_t actions;
141 posix_spawn_file_actions_init(&actions);
142 posix_spawn_file_actions_adddup2(&actions, fds[1], STDOUT_FILENO);
143
144 assert_no_err(posix_spawn(&pid, "/usr/sbin/hdik", &actions, NULL, attach_args, NULL));
145
146 posix_spawn_file_actions_destroy(&actions);
147
148 close(fds[1]);
149
150 char *line, *slice = NULL;
151 size_t lnsz = 64;
152 FILE *fp = fdopen(fds[0], "r");
153
154 line = malloc(lnsz);
155 assert(line);
156
157 while (getline(&line, &lnsz, fp) != -1) {
158 char *first, *second;
159
160 first = strtok(line, " ");
161 assert(first);
162
163 second = strtok(NULL, " ");
164 assert(second);
165
166 if (strstr(second, "GUID"))
167 di->disk = strdup(first);
168
169 // The output of hdik gets truncated, so just search for the leading part of the UUID
170 else if (strstr(second, "48465300-0000-11AA"))
171 slice = strdup(first);
172 }
173
174 int status;
175 assert_with_errno(ignore_eintr(waitpid(pid, &status, 0), -1) == pid);
176 assert(WIFEXITED(status) && !WEXITSTATUS(status));
177
178 assert(di->disk && slice);
179 free(line);
180 fclose(fp);
181
182 // Mount it
183 char *mkdir_args[4] = { "mkdir", "-p", (char *)opts->mount_point, NULL };
184 assert_no_err(posix_spawn(&pid, "/bin/mkdir", NULL, NULL, mkdir_args, NULL));
185
186 assert_with_errno(ignore_eintr(waitpid(pid, &status, 0), -1) == pid);
187 assert(WIFEXITED(status) && !WEXITSTATUS(status));
188
189 posix_spawn_file_actions_t facts;
190 posix_spawn_file_actions_init(&facts);
191 posix_spawn_file_actions_addopen(&facts, STDOUT_FILENO, "/dev/null", O_APPEND, 0);
192 posix_spawn_file_actions_addopen(&facts, STDERR_FILENO, "/dev/null", O_APPEND, 0);
193
194 char *mount_args[4] = { "mount", slice, (char *)opts->mount_point, NULL };
195 assert_no_err(posix_spawn(&pid, "/sbin/mount_hfs", &facts, NULL, mount_args, NULL));
196
197 posix_spawn_file_actions_destroy(&facts);
198 free(slice);
199
200 assert_with_errno(ignore_eintr(waitpid(pid, &status, 0), -1) == pid);
201 assert(WIFEXITED(status) && !WEXITSTATUS(status));
202
203 di->mount_point = strdup(opts->mount_point);
204
205 if (strcmp(path, SHARED_PATH)) { // Don't register a cleanup for the shared image
206 test_cleanup(^ bool {
207 return disk_image_cleanup(di);
208 });
209 }
210
211 assert_no_err(seteuid(uid_old));
212
213 return di;
214 }
215
216 disk_image_t *disk_image_get(void)
217 {
218 disk_image_t *di;
219 struct statfs sfs;
220
221 if (statfs(SHARED_MOUNT, &sfs) == 0) {
222 di = calloc(1, sizeof(*di));
223 di->mount_point = SHARED_MOUNT;
224 di->disk = strdup(sfs.f_mntfromname);
225 di->path = SHARED_PATH;
226
227 // Make sure the di struct is freed when tests are complete.
228 test_cleanup(^ bool {
229 free(di->disk);
230 free(di);
231 return true;
232 });
233 } else {
234 disk_image_opts_t opts = {
235 .mount_point = SHARED_MOUNT
236 };
237 di = disk_image_create(SHARED_PATH, &opts);
238 // Per the contract of disk_image_create(),
239 // di will be freed when disk_image_cleanup() is called,
240 // so don't free it here.
241 }
242
243 return di;
244 }
245
246 #else // !TARGET_OS_EMBEDDED
247
248 bool disk_image_cleanup(disk_image_t *di)
249 {
250 char *detach_args[]
251 = { "hdiutil", "detach", (char *)di->disk, "-force", NULL };
252
253 pid_t pid;
254 bool result = false;
255
256 posix_spawn_file_actions_t facts;
257 posix_spawn_file_actions_init(&facts);
258 posix_spawn_file_actions_addopen(&facts, STDOUT_FILENO, "/dev/null", O_APPEND, 0);
259 posix_spawn_file_actions_addopen(&facts, STDERR_FILENO, "/dev/null", O_APPEND, 0);
260
261 assert_no_err(posix_spawn(&pid, "/usr/bin/hdiutil", &facts, NULL, detach_args, NULL));
262
263 posix_spawn_file_actions_destroy(&facts);
264
265 int status;
266 waitpid(pid, &status, 0);
267
268 struct stat sb;
269
270 if (WIFEXITED(status) && !WEXITSTATUS(status)
271 && stat(di->disk, &sb) == -1 && errno == ENOENT) {
272 if (unlink(di->path) && errno == EACCES && !seteuid(0))
273 unlink(di->path);
274 result = true;
275
276 // We are the last user of di, so free it.
277 free(di->mount_point);
278 free(di->disk);
279 free(di->path);
280 free(di);
281 }
282
283 return result;
284 }
285
286 disk_image_t *disk_image_create(const char *path, disk_image_opts_t *opts)
287 {
288 pid_t pid;
289 char sz[32];
290 sprintf(sz, "%llu", opts->size);
291
292 if (opts->mount_point) {
293 assert(!systemx("/bin/mkdir", SYSTEMX_QUIET, "-p", opts->mount_point, NULL));
294 }
295
296 // Start with the basic args
297 char *args[64] = { "hdiutil", "create", (char *)path, "-size", sz, "-ov" };
298
299 if (opts && opts->partition_type) {
300 args[6] = "-partitionType";
301 args[7] = (char *)opts->partition_type;
302 args[8] = NULL;
303
304 posix_spawn_file_actions_t facts;
305 posix_spawn_file_actions_init(&facts);
306 posix_spawn_file_actions_addopen(&facts, STDOUT_FILENO, "/dev/null", O_APPEND, 0);
307
308 assert_no_err(posix_spawn(&pid, "/usr/bin/hdiutil", &facts, NULL,
309 args, NULL));
310
311 posix_spawn_file_actions_destroy(&facts);
312
313 int status;
314 assert_with_errno(ignore_eintr(waitpid(pid, &status, 0), -1));
315
316 assert(WIFEXITED(status) && !WEXITSTATUS(status));
317
318 args[1] = "attach";
319 // args[2] == path
320 args[3] = "-nomount";
321 args[4] = "-plist";
322 args[5] = NULL;
323 } else if (opts && opts->enable_owners) {
324 args[6] = "-fs";
325 args[7] = "HFS+J";
326 args[8] = NULL;
327
328 posix_spawn_file_actions_t facts;
329 posix_spawn_file_actions_init(&facts);
330 posix_spawn_file_actions_addopen(&facts, STDOUT_FILENO, "/dev/null", O_APPEND, 0);
331
332 assert_no_err(posix_spawn(&pid, "/usr/bin/hdiutil", &facts, NULL,
333 args, NULL));
334
335 posix_spawn_file_actions_destroy(&facts);
336
337 int status;
338 assert_with_errno(ignore_eintr(waitpid(pid, &status, 0), -1));
339
340 assert(WIFEXITED(status) && !WEXITSTATUS(status));
341
342 args[1] = "attach";
343 // args[2] == path
344 args[3] = "-plist";
345 args[4] = "-owners";
346 args[5] = "on";
347 if (opts->mount_point) {
348 args[6] = "-mountpoint";
349 args[7] = (char *)opts->mount_point;
350 args[8] = NULL;
351 }
352 else
353 args[6] = NULL;
354 } else {
355 args[6] = "-fs";
356 args[7] = "HFS+J";
357 args[8] = NULL;
358
359 posix_spawn_file_actions_t facts;
360 posix_spawn_file_actions_init(&facts);
361 posix_spawn_file_actions_addopen(&facts, STDOUT_FILENO, "/dev/null", O_APPEND, 0);
362
363 assert_no_err(posix_spawn(&pid, "/usr/bin/hdiutil", &facts, NULL,
364 args, NULL));
365
366 posix_spawn_file_actions_destroy(&facts);
367
368 int status;
369 assert_with_errno(ignore_eintr(waitpid(pid, &status, 0), -1));
370
371 assert(WIFEXITED(status) && !WEXITSTATUS(status));
372
373 args[1] = "attach";
374 // args[2] == path
375 args[3] = "-plist";
376 if (opts->mount_point) {
377 args[4] = "-mountpoint";
378 args[5] = (char *)opts->mount_point;
379 args[6] = NULL;
380 }
381 else
382 args[4] = NULL;
383 }
384
385 int fds[2];
386 assert_no_err(pipe(fds));
387
388 posix_spawn_file_actions_t actions;
389 posix_spawn_file_actions_init(&actions);
390 posix_spawn_file_actions_adddup2(&actions, fds[1], STDOUT_FILENO);
391
392 assert_no_err(posix_spawn(&pid, "/usr/bin/hdiutil", &actions, NULL, args, NULL));
393
394 posix_spawn_file_actions_destroy(&actions);
395
396 close(fds[1]);
397
398 char buffer[4096];
399 size_t amt = 0;
400
401 for (;;) {
402 ssize_t res = read(fds[0], buffer + amt, 4096 - amt);
403
404 if (!res)
405 break;
406
407 if (res == -1 && errno == EINTR)
408 continue;
409
410 assert_with_errno(res > 0);
411
412 amt += res;
413
414 assert(amt < 4096);
415 }
416
417 disk_image_t *di = calloc(1, sizeof(*di));
418
419 di->path = strdup(path);
420
421 @autoreleasepool {
422 NSDictionary *results
423 = [NSPropertyListSerialization propertyListWithData:
424 [NSData dataWithBytesNoCopy:buffer
425 length:amt
426 freeWhenDone:NO]
427 options:0
428 format:NULL
429 error:NULL];
430
431 for (NSDictionary *entity in results[@"system-entities"]) {
432 if (opts && opts->partition_type) {
433 if (!strcmp([entity[@"unmapped-content-hint"] UTF8String],
434 opts->partition_type)
435 || !strcmp([entity[@"content-hint"] UTF8String],
436 opts->partition_type)) {
437 di->disk = strdup([entity[@"dev-entry"] fileSystemRepresentation]);
438 break;
439 }
440 } else if ([entity[@"content-hint"] isEqualToString:@"Apple_HFS"]) {
441 di->mount_point = strdup([entity[@"mount-point"] fileSystemRepresentation]);
442 di->disk = strdup([entity[@"dev-entry"] fileSystemRepresentation]);
443 break;
444 }
445 }
446 }
447
448 int status;
449 assert_with_errno(ignore_eintr(waitpid(pid, &status, 0), -1) == pid);
450 assert(WIFEXITED(status) && !WEXITSTATUS(status));
451
452 assert(di->disk);
453
454 if (strcmp(path, SHARED_PATH)) { // Don't register a cleanup for the shared image
455 test_cleanup(^ bool {
456 return disk_image_cleanup(di);
457 });
458 }
459
460 return di;
461 }
462
463 disk_image_t *disk_image_get(void)
464 {
465 disk_image_t *di;
466 struct statfs sfs;
467
468 if (statfs(SHARED_MOUNT, &sfs) == 0) {
469 di = calloc(1, sizeof(*di));
470
471 di->mount_point = SHARED_MOUNT;
472 di->disk = strdup(sfs.f_mntfromname);
473 di->path = SHARED_PATH;
474
475 // Make sure the di struct is freed when tests are complete.
476 test_cleanup(^ bool {
477 free(di->disk);
478 free(di);
479 return true;
480 });
481 } else {
482 disk_image_opts_t opts = {
483 .size = 4 GB,
484 .mount_point = SHARED_MOUNT
485 };
486 di = disk_image_create(SHARED_PATH, &opts);
487 // Per the contract of disk_image_create(),
488 // di will be freed when disk_image_cleanup() is called,
489 // so don't free it here.
490 }
491
492 return di;
493 }
494
495 #endif // TARGET_OS_EMBEDDED