5 // Created by Chris Suter on 8/12/15.
12 #include <sys/param.h>
13 #include <sys/mount.h>
19 #include <Foundation/Foundation.h>
20 #include <TargetConditionals.h>
22 #include "disk-image.h"
23 #include "test-utils.h"
28 #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
32 bool disk_image_cleanup(disk_image_t *di)
40 assert(seteuid(0) == 0);
43 = { "umount", "-f", (char *)di->mount_point, NULL };
45 assert_no_err(posix_spawn(&pid, "/sbin/umount", NULL, NULL, umount_args, NULL));
47 waitpid(pid, &status, 0);
50 = { "hdik", "-e", (char *)di->disk, NULL };
52 posix_spawn_file_actions_t facts;
53 posix_spawn_file_actions_init(&facts);
54 posix_spawn_file_actions_addopen(&facts, STDOUT_FILENO, "/dev/null", O_APPEND, 0);
55 posix_spawn_file_actions_addopen(&facts, STDERR_FILENO, "/dev/null", O_APPEND, 0);
57 for (eject_retry = 0; eject_retry < RETRY_MAX; ++eject_retry) {
58 if (!posix_spawn(&pid, "/usr/sbin/hdik", &facts, NULL, detach_args, NULL)) {
59 waitpid(pid, &status, 0);
60 if (WIFEXITED(status) && !WEXITSTATUS(status))
66 posix_spawn_file_actions_destroy(&facts);
68 assert(eject_retry != RETRY_MAX);
72 if (stat(di->disk, &sb) == -1 && errno == ENOENT) {
75 // We are the last user of di, so free it.
76 free(di->mount_point);
85 void *zalloc(__unused void *opaque, uInt items, uInt size)
87 return malloc(items * size);
90 void zfree(__unused void *opaque, void *ptr)
95 disk_image_t *disk_image_create(const char *path, disk_image_opts_t *opts)
99 di = calloc(1, sizeof(disk_image_t));
102 // We need to be root
104 if ((uid_old = geteuid()) != 0) {
105 assert_no_err(seteuid(0));
109 int fd = open(path, O_RDWR | O_TRUNC | O_CREAT, 0666);
118 size_t buf_size = 1024 * 1024;
119 void *out_buf = malloc(buf_size);
123 zs.avail_in = sizeof(data);
128 zs.next_out = out_buf;
129 zs.avail_out = (uInt)buf_size;
131 ret = inflate(&zs, 0);
133 size_t todo = buf_size - zs.avail_out;
135 assert(write(fd, out_buf, todo) == (ssize_t)todo);
136 } while (ret == Z_OK);
138 assert(ret == Z_STREAM_END);
140 di->path = strdup(path);
144 char *attach_args[4] = { "hdik", "-nomount", (char *)di->path, NULL };
147 assert_no_err(pipe(fds));
149 posix_spawn_file_actions_t actions;
150 posix_spawn_file_actions_init(&actions);
151 posix_spawn_file_actions_adddup2(&actions, fds[1], STDOUT_FILENO);
153 assert_no_err(posix_spawn(&pid, "/usr/sbin/hdik", &actions, NULL, attach_args, NULL));
155 posix_spawn_file_actions_destroy(&actions);
159 char *line, *slice = NULL;
161 FILE *fp = fdopen(fds[0], "r");
166 while (getline(&line, &lnsz, fp) != -1) {
167 char *first, *second;
169 first = strtok(line, " ");
172 second = strtok(NULL, " ");
175 if (strstr(second, "GUID"))
176 di->disk = strdup(first);
178 // The output of hdik gets truncated, so just search for the leading part of the UUID
179 else if (strstr(second, "48465300-0000-11AA"))
180 slice = strdup(first);
184 assert_with_errno(ignore_eintr(waitpid(pid, &status, 0), -1) == pid);
185 assert(WIFEXITED(status) && !WEXITSTATUS(status));
187 assert(di->disk && slice);
191 // Ensure we have a mount point
192 assert(opts->mount_point);
195 char *mkdir_args[4] = { "mkdir", "-p", (char *)opts->mount_point, NULL };
196 assert_no_err(posix_spawn(&pid, "/bin/mkdir", NULL, NULL, mkdir_args, NULL));
198 assert_with_errno(ignore_eintr(waitpid(pid, &status, 0), -1) == pid);
199 assert(WIFEXITED(status) && !WEXITSTATUS(status));
201 posix_spawn_file_actions_t facts;
202 posix_spawn_file_actions_init(&facts);
203 posix_spawn_file_actions_addopen(&facts, STDOUT_FILENO, "/dev/null", O_APPEND, 0);
204 posix_spawn_file_actions_addopen(&facts, STDERR_FILENO, "/dev/null", O_APPEND, 0);
206 char *mount_args[4] = { "mount", slice, (char *)opts->mount_point, NULL };
207 assert_no_err(posix_spawn(&pid, "/sbin/mount_hfs", &facts, NULL, mount_args, NULL));
209 posix_spawn_file_actions_destroy(&facts);
212 assert_with_errno(ignore_eintr(waitpid(pid, &status, 0), -1) == pid);
213 assert(WIFEXITED(status) && !WEXITSTATUS(status));
215 di->mount_point = strdup(opts->mount_point);
217 if (strcmp(path, SHARED_PATH)) { // Don't register a cleanup for the shared image
218 test_cleanup(^ bool {
219 return disk_image_cleanup(di);
223 assert_no_err(seteuid(uid_old));
228 disk_image_t *disk_image_get(void)
233 if (statfs(SHARED_MOUNT, &sfs) == 0) {
234 di = calloc(1, sizeof(*di));
235 di->mount_point = SHARED_MOUNT;
236 di->disk = strdup(sfs.f_mntfromname);
237 di->path = SHARED_PATH;
239 // Make sure the di struct is freed when tests are complete.
240 test_cleanup(^ bool {
246 disk_image_opts_t opts = {
247 .mount_point = SHARED_MOUNT
249 di = disk_image_create(SHARED_PATH, &opts);
250 // Per the contract of disk_image_create(),
251 // di will be freed when disk_image_cleanup() is called,
252 // so don't free it here.
258 #else // !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
260 bool disk_image_cleanup(disk_image_t *di)
263 = { "hdiutil", "detach", (char *)di->disk, "-force", NULL };
270 posix_spawn_file_actions_t facts;
271 posix_spawn_file_actions_init(&facts);
272 posix_spawn_file_actions_addopen(&facts, STDOUT_FILENO, "/dev/null", O_APPEND, 0);
273 posix_spawn_file_actions_addopen(&facts, STDERR_FILENO, "/dev/null", O_APPEND, 0);
275 for (eject_retry = 0; eject_retry < RETRY_MAX; ++eject_retry) {
276 if (!posix_spawn(&pid, "/usr/bin/hdiutil", &facts, NULL, detach_args, NULL)) {
277 waitpid(pid, &status, 0);
278 if (WIFEXITED(status) && !WEXITSTATUS(status)) {
285 posix_spawn_file_actions_destroy(&facts);
287 assert(eject_retry != RETRY_MAX);
291 if (stat(di->disk, &sb) == -1 && errno == ENOENT) {
292 if (unlink(di->path) && errno == EACCES && !seteuid(0))
296 // We are the last user of di, so free it.
297 free(di->mount_point);
306 disk_image_t *disk_image_create(const char *path, disk_image_opts_t *opts)
310 sprintf(sz, "%llu", opts->size);
312 if (opts->mount_point) {
313 assert(!systemx("/bin/mkdir", SYSTEMX_QUIET, "-p", opts->mount_point, NULL));
316 // Start with the basic args
317 char *args[64] = { "hdiutil", "create", (char *)path, "-size", sz, "-ov" };
319 if (opts && opts->partition_type) {
320 args[6] = "-partitionType";
321 args[7] = (char *)opts->partition_type;
324 posix_spawn_file_actions_t facts;
325 posix_spawn_file_actions_init(&facts);
326 posix_spawn_file_actions_addopen(&facts, STDOUT_FILENO, "/dev/null", O_APPEND, 0);
328 assert_no_err(posix_spawn(&pid, "/usr/bin/hdiutil", &facts, NULL,
331 posix_spawn_file_actions_destroy(&facts);
334 assert_with_errno(ignore_eintr(waitpid(pid, &status, 0), -1));
336 assert(WIFEXITED(status) && !WEXITSTATUS(status));
340 args[3] = "-nomount";
343 } else if (opts && opts->enable_owners) {
348 posix_spawn_file_actions_t facts;
349 posix_spawn_file_actions_init(&facts);
350 posix_spawn_file_actions_addopen(&facts, STDOUT_FILENO, "/dev/null", O_APPEND, 0);
352 assert_no_err(posix_spawn(&pid, "/usr/bin/hdiutil", &facts, NULL,
355 posix_spawn_file_actions_destroy(&facts);
358 assert_with_errno(ignore_eintr(waitpid(pid, &status, 0), -1));
360 assert(WIFEXITED(status) && !WEXITSTATUS(status));
367 if (opts->mount_point) {
368 args[6] = "-mountpoint";
369 args[7] = (char *)opts->mount_point;
379 posix_spawn_file_actions_t facts;
380 posix_spawn_file_actions_init(&facts);
381 posix_spawn_file_actions_addopen(&facts, STDOUT_FILENO, "/dev/null", O_APPEND, 0);
383 assert_no_err(posix_spawn(&pid, "/usr/bin/hdiutil", &facts, NULL,
386 posix_spawn_file_actions_destroy(&facts);
389 assert_with_errno(ignore_eintr(waitpid(pid, &status, 0), -1));
391 assert(WIFEXITED(status) && !WEXITSTATUS(status));
396 if (opts->mount_point) {
397 args[4] = "-mountpoint";
398 args[5] = (char *)opts->mount_point;
406 assert_no_err(pipe(fds));
408 posix_spawn_file_actions_t actions;
409 posix_spawn_file_actions_init(&actions);
410 posix_spawn_file_actions_adddup2(&actions, fds[1], STDOUT_FILENO);
412 assert_no_err(posix_spawn(&pid, "/usr/bin/hdiutil", &actions, NULL, args, NULL));
414 posix_spawn_file_actions_destroy(&actions);
422 ssize_t res = read(fds[0], buffer + amt, 4096 - amt);
427 if (res == -1 && errno == EINTR)
430 assert_with_errno(res > 0);
437 disk_image_t *di = calloc(1, sizeof(*di));
439 di->path = strdup(path);
442 NSDictionary *results
443 = [NSPropertyListSerialization propertyListWithData:
444 [NSData dataWithBytesNoCopy:buffer
451 for (NSDictionary *entity in results[@"system-entities"]) {
452 if (opts && opts->partition_type) {
453 if (!strcmp([entity[@"unmapped-content-hint"] UTF8String],
454 opts->partition_type)
455 || !strcmp([entity[@"content-hint"] UTF8String],
456 opts->partition_type)) {
457 di->disk = strdup([entity[@"dev-entry"] fileSystemRepresentation]);
460 } else if ([entity[@"content-hint"] isEqualToString:@"Apple_HFS"]) {
461 di->mount_point = strdup([entity[@"mount-point"] fileSystemRepresentation]);
462 di->disk = strdup([entity[@"dev-entry"] fileSystemRepresentation]);
469 assert_with_errno(ignore_eintr(waitpid(pid, &status, 0), -1) == pid);
470 assert(WIFEXITED(status) && !WEXITSTATUS(status));
474 if (strcmp(path, SHARED_PATH)) { // Don't register a cleanup for the shared image
475 test_cleanup(^ bool {
476 return disk_image_cleanup(di);
483 disk_image_t *disk_image_get(void)
488 if (statfs(SHARED_MOUNT, &sfs) == 0) {
489 di = calloc(1, sizeof(*di));
491 di->mount_point = SHARED_MOUNT;
492 di->disk = strdup(sfs.f_mntfromname);
493 di->path = SHARED_PATH;
495 // Make sure the di struct is freed when tests are complete.
496 test_cleanup(^ bool {
502 disk_image_opts_t opts = {
504 .mount_point = SHARED_MOUNT
506 di = disk_image_create(SHARED_PATH, &opts);
507 // Per the contract of disk_image_create(),
508 // di will be freed when disk_image_cleanup() is called,
509 // so don't free it here.
515 #endif // (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)