-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmount.c
84 lines (63 loc) · 2.3 KB
/
mount.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
#include "mount.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <inttypes.h>
#if defined(__APPLE__) && defined(__MACH__)
/* Just call hdiutil which handles heavy lifting. */
static enum RET_CODES mount_slot_os_x(int fd, const unsigned int slot, const char *const image_name)
{
struct stat fstat_buf;
int ret;
char buffer[1024] = { '\0' };
const uint64_t offset = slot * MAGIC_OFFSET;
const uint64_t start_block = offset / 512;
const uint64_t end_block = start_block + (IMAGE_SIZE / 512);
CHECK_ERROR(fstat(fd, &fstat_buf), FAIL_FSTAT);
if (!S_ISREG(fstat_buf.st_mode)) {
fprintf(stderr, "Only image files are supported on macOS/Mac OS X.\n");
return FAIL_NOTSUPP;
}
/* Don't allow fd leak. If execl fails, then descriptor will be closed. */
close(fd);
snprintf(buffer, sizeof(buffer), "%" PRIu64 ",%" PRIu64, start_block, end_block);
ret = execl("/usr/bin/hdiutil", "/usr/bin/hdiutil", "attach", "-section", buffer, image_name,(char*) NULL);
if (ret == -1) {
fprintf(stderr, "Unable to execute hdiutil.\n");
return FAIL_FAIL;
}
return FAIL_FAIL;
}
#elif defined(__linux__)
/* Just call mount with options - this saves tons of code cause loopback devices
are tricky on Linux sometimes (no loop-control provided etc). */
static enum RET_CODES mount_slot_linux(int fd, const unsigned int slot, const char *const image_name)
{
const uint64_t offset = slot * MAGIC_OFFSET;
int ret;
char buffer[64] = { '\0' };
close(fd);
snprintf(buffer, sizeof(buffer), "loop,offset=%" PRIu64 , offset);
/* it's stupid and only /media dir is supported for now */
ret = execlp("mount", "mount", "-o", buffer, image_name, "/media", (char *) NULL);
if (ret == -1) {
fprintf(stderr, "Unable to execute hdiutil.\n");
return FAIL_FAIL;
}
return FAIL_FAIL;
}
#endif
enum RET_CODES mount_slot(int fd, const unsigned int slot, const char *const image_name)
{
enum RET_CODES rc;
#if defined(__APPLE__) && defined(__MACH__)
rc = mount_slot_os_x(fd, slot, image_name);
return rc;
#elif defined(__linux__)
rc = mount_slot_linux(fd, slot, image_name);
return rc;
#endif
fprintf(stderr, "Not supported on your OS.\n");
return FAIL_NOTSUPP;
}