forked from caldwell/gdisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device-macosx.c
52 lines (46 loc) · 1.09 KB
/
device-macosx.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
// Copyright (c) 2008 David Caldwell, All Rights Reserved.
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/disk.h>
#include <err.h>
#include "xmem.h"
#include "device.h"
static unsigned int sector_size(int fd)
{
uint32_t size;
if (ioctl(fd, DKIOCGETBLOCKSIZE, &size) == -1)
return 0;
return size;
}
static unsigned long long sector_count(int fd)
{
uint64_t count;
if (ioctl(fd, DKIOCGETBLOCKCOUNT, &count) == -1)
return 0;
return count;
}
struct device *open_disk_device(char *name)
{
int fd = open(name, O_RDWR | O_EXLOCK);
if (fd < 0) {
if (errno == ENOENT) return NULL;
if (errno == EBUSY)
fd = open(name, O_RDWR | O_SHLOCK);
}
if (fd < 0)
return NULL;
struct device dev = {
.fd = fd,
.name = strdup(name),
.sector_size = sector_size(fd),
.sector_count = sector_count(fd),
};
return xmemdup(&dev, sizeof(dev));
}
char *device_help()
{
return " <device> is /dev/disk* style device (full path)\n";
}