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"
26 #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
30 bool disk_image_cleanup(disk_image_t *di)
36 assert(seteuid(0) == 0);
39 = { "umount", "-f", (char *)di->mount_point, NULL };
41 assert_no_err(posix_spawn(&pid, "/sbin/umount", NULL, NULL, umount_args, NULL));
44 waitpid(pid, &status, 0);
47 = { "hdik", "-e", (char *)di->disk, NULL };
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);
54 assert_no_err(posix_spawn(&pid, "/usr/sbin/hdik", &facts, NULL, detach_args, NULL));
56 posix_spawn_file_actions_destroy(&facts);
58 waitpid(pid, &status, 0);
62 if (WIFEXITED(status) && !WEXITSTATUS(status)
63 && stat(di->disk, &sb) == -1 && errno == ENOENT) {
66 // We are the last user of di, so free it.
67 free(di->mount_point);
76 void *zalloc(__unused void *opaque, uInt items, uInt size)
78 return malloc(items * size);
81 void zfree(__unused void *opaque, void *ptr)
86 disk_image_t *disk_image_create(const char *path, disk_image_opts_t *opts)
90 di = calloc(1, sizeof(disk_image_t));
95 if ((uid_old = geteuid()) != 0) {
96 assert_no_err(seteuid(0));
100 int fd = open(path, O_RDWR | O_TRUNC | O_CREAT, 0666);
109 size_t buf_size = 1024 * 1024;
110 void *out_buf = malloc(buf_size);
114 zs.avail_in = sizeof(data);
119 zs.next_out = out_buf;
120 zs.avail_out = buf_size;
122 ret = inflate(&zs, 0);
124 size_t todo = buf_size - zs.avail_out;
126 assert(write(fd, out_buf, todo) == (ssize_t)todo);
127 } while (ret == Z_OK);
129 assert(ret == Z_STREAM_END);
131 di->path = strdup(path);
135 char *attach_args[4] = { "hdik", "-nomount", (char *)di->path, NULL };
138 assert_no_err(pipe(fds));
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);
144 assert_no_err(posix_spawn(&pid, "/usr/sbin/hdik", &actions, NULL, attach_args, NULL));
146 posix_spawn_file_actions_destroy(&actions);
150 char *line, *slice = NULL;
152 FILE *fp = fdopen(fds[0], "r");
157 while (getline(&line, &lnsz, fp) != -1) {
158 char *first, *second;
160 first = strtok(line, " ");
163 second = strtok(NULL, " ");
166 if (strstr(second, "GUID"))
167 di->disk = strdup(first);
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);
175 assert_with_errno(ignore_eintr(waitpid(pid, &status, 0), -1) == pid);
176 assert(WIFEXITED(status) && !WEXITSTATUS(status));
178 assert(di->disk && slice);
182 // Ensure we have a mount point
183 assert(opts->mount_point);
186 char *mkdir_args[4] = { "mkdir", "-p", (char *)opts->mount_point, NULL };
187 assert_no_err(posix_spawn(&pid, "/bin/mkdir", NULL, NULL, mkdir_args, NULL));
189 assert_with_errno(ignore_eintr(waitpid(pid, &status, 0), -1) == pid);
190 assert(WIFEXITED(status) && !WEXITSTATUS(status));
192 posix_spawn_file_actions_t facts;
193 posix_spawn_file_actions_init(&facts);
194 posix_spawn_file_actions_addopen(&facts, STDOUT_FILENO, "/dev/null", O_APPEND, 0);
195 posix_spawn_file_actions_addopen(&facts, STDERR_FILENO, "/dev/null", O_APPEND, 0);
197 char *mount_args[4] = { "mount", slice, (char *)opts->mount_point, NULL };
198 assert_no_err(posix_spawn(&pid, "/sbin/mount_hfs", &facts, NULL, mount_args, NULL));
200 posix_spawn_file_actions_destroy(&facts);
203 assert_with_errno(ignore_eintr(waitpid(pid, &status, 0), -1) == pid);
204 assert(WIFEXITED(status) && !WEXITSTATUS(status));
206 di->mount_point = strdup(opts->mount_point);
208 if (strcmp(path, SHARED_PATH)) { // Don't register a cleanup for the shared image
209 test_cleanup(^ bool {
210 return disk_image_cleanup(di);
214 assert_no_err(seteuid(uid_old));
219 disk_image_t *disk_image_get(void)
224 if (statfs(SHARED_MOUNT, &sfs) == 0) {
225 di = calloc(1, sizeof(*di));
226 di->mount_point = SHARED_MOUNT;
227 di->disk = strdup(sfs.f_mntfromname);
228 di->path = SHARED_PATH;
230 // Make sure the di struct is freed when tests are complete.
231 test_cleanup(^ bool {
237 disk_image_opts_t opts = {
238 .mount_point = SHARED_MOUNT
240 di = disk_image_create(SHARED_PATH, &opts);
241 // Per the contract of disk_image_create(),
242 // di will be freed when disk_image_cleanup() is called,
243 // so don't free it here.
249 #else // !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
251 bool disk_image_cleanup(disk_image_t *di)
254 = { "hdiutil", "detach", (char *)di->disk, "-force", NULL };
259 posix_spawn_file_actions_t facts;
260 posix_spawn_file_actions_init(&facts);
261 posix_spawn_file_actions_addopen(&facts, STDOUT_FILENO, "/dev/null", O_APPEND, 0);
262 posix_spawn_file_actions_addopen(&facts, STDERR_FILENO, "/dev/null", O_APPEND, 0);
264 assert_no_err(posix_spawn(&pid, "/usr/bin/hdiutil", &facts, NULL, detach_args, NULL));
266 posix_spawn_file_actions_destroy(&facts);
269 waitpid(pid, &status, 0);
273 if (WIFEXITED(status) && !WEXITSTATUS(status)
274 && stat(di->disk, &sb) == -1 && errno == ENOENT) {
275 if (unlink(di->path) && errno == EACCES && !seteuid(0))
279 // We are the last user of di, so free it.
280 free(di->mount_point);
289 disk_image_t *disk_image_create(const char *path, disk_image_opts_t *opts)
293 sprintf(sz, "%llu", opts->size);
295 if (opts->mount_point) {
296 assert(!systemx("/bin/mkdir", SYSTEMX_QUIET, "-p", opts->mount_point, NULL));
299 // Start with the basic args
300 char *args[64] = { "hdiutil", "create", (char *)path, "-size", sz, "-ov" };
302 if (opts && opts->partition_type) {
303 args[6] = "-partitionType";
304 args[7] = (char *)opts->partition_type;
307 posix_spawn_file_actions_t facts;
308 posix_spawn_file_actions_init(&facts);
309 posix_spawn_file_actions_addopen(&facts, STDOUT_FILENO, "/dev/null", O_APPEND, 0);
311 assert_no_err(posix_spawn(&pid, "/usr/bin/hdiutil", &facts, NULL,
314 posix_spawn_file_actions_destroy(&facts);
317 assert_with_errno(ignore_eintr(waitpid(pid, &status, 0), -1));
319 assert(WIFEXITED(status) && !WEXITSTATUS(status));
323 args[3] = "-nomount";
326 } else if (opts && opts->enable_owners) {
331 posix_spawn_file_actions_t facts;
332 posix_spawn_file_actions_init(&facts);
333 posix_spawn_file_actions_addopen(&facts, STDOUT_FILENO, "/dev/null", O_APPEND, 0);
335 assert_no_err(posix_spawn(&pid, "/usr/bin/hdiutil", &facts, NULL,
338 posix_spawn_file_actions_destroy(&facts);
341 assert_with_errno(ignore_eintr(waitpid(pid, &status, 0), -1));
343 assert(WIFEXITED(status) && !WEXITSTATUS(status));
350 if (opts->mount_point) {
351 args[6] = "-mountpoint";
352 args[7] = (char *)opts->mount_point;
362 posix_spawn_file_actions_t facts;
363 posix_spawn_file_actions_init(&facts);
364 posix_spawn_file_actions_addopen(&facts, STDOUT_FILENO, "/dev/null", O_APPEND, 0);
366 assert_no_err(posix_spawn(&pid, "/usr/bin/hdiutil", &facts, NULL,
369 posix_spawn_file_actions_destroy(&facts);
372 assert_with_errno(ignore_eintr(waitpid(pid, &status, 0), -1));
374 assert(WIFEXITED(status) && !WEXITSTATUS(status));
379 if (opts->mount_point) {
380 args[4] = "-mountpoint";
381 args[5] = (char *)opts->mount_point;
389 assert_no_err(pipe(fds));
391 posix_spawn_file_actions_t actions;
392 posix_spawn_file_actions_init(&actions);
393 posix_spawn_file_actions_adddup2(&actions, fds[1], STDOUT_FILENO);
395 assert_no_err(posix_spawn(&pid, "/usr/bin/hdiutil", &actions, NULL, args, NULL));
397 posix_spawn_file_actions_destroy(&actions);
405 ssize_t res = read(fds[0], buffer + amt, 4096 - amt);
410 if (res == -1 && errno == EINTR)
413 assert_with_errno(res > 0);
420 disk_image_t *di = calloc(1, sizeof(*di));
422 di->path = strdup(path);
425 NSDictionary *results
426 = [NSPropertyListSerialization propertyListWithData:
427 [NSData dataWithBytesNoCopy:buffer
434 for (NSDictionary *entity in results[@"system-entities"]) {
435 if (opts && opts->partition_type) {
436 if (!strcmp([entity[@"unmapped-content-hint"] UTF8String],
437 opts->partition_type)
438 || !strcmp([entity[@"content-hint"] UTF8String],
439 opts->partition_type)) {
440 di->disk = strdup([entity[@"dev-entry"] fileSystemRepresentation]);
443 } else if ([entity[@"content-hint"] isEqualToString:@"Apple_HFS"]) {
444 di->mount_point = strdup([entity[@"mount-point"] fileSystemRepresentation]);
445 di->disk = strdup([entity[@"dev-entry"] fileSystemRepresentation]);
452 assert_with_errno(ignore_eintr(waitpid(pid, &status, 0), -1) == pid);
453 assert(WIFEXITED(status) && !WEXITSTATUS(status));
457 if (strcmp(path, SHARED_PATH)) { // Don't register a cleanup for the shared image
458 test_cleanup(^ bool {
459 return disk_image_cleanup(di);
466 disk_image_t *disk_image_get(void)
471 if (statfs(SHARED_MOUNT, &sfs) == 0) {
472 di = calloc(1, sizeof(*di));
474 di->mount_point = SHARED_MOUNT;
475 di->disk = strdup(sfs.f_mntfromname);
476 di->path = SHARED_PATH;
478 // Make sure the di struct is freed when tests are complete.
479 test_cleanup(^ bool {
485 disk_image_opts_t opts = {
487 .mount_point = SHARED_MOUNT
489 di = disk_image_create(SHARED_PATH, &opts);
490 // Per the contract of disk_image_create(),
491 // di will be freed when disk_image_cleanup() is called,
492 // so don't free it here.
498 #endif // (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)