-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_dev_ops.c
71 lines (55 loc) · 1.48 KB
/
file_dev_ops.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
#include "common.h"
#include <sys/stat.h>
#include <stdio.h>
#include <inttypes.h>
#include <sys/ioctl.h>
#include "file_dev_ops.h"
/* Compatibility layer */
#if defined(__APPLE__) && defined(__MACH__)
#include <sys/disk.h>
static enum RET_CODES blkgetsize(int fd, uint64_t *psize)
{
uint32_t blocksize = 0;
uint64_t nblocks;
CHECK_ERROR(ioctl(fd, DKIOCGETBLOCKSIZE, &blocksize), FAIL_IOCTL);
CHECK_ERROR(ioctl(fd, DKIOCGETBLOCKCOUNT, &nblocks), FAIL_IOCTL);
*psize = (uint64_t) nblocks * blocksize;
return FAIL_SUCC;
}
#elif defined(__linux__)
#include <linux/fs.h>
enum RET_CODES blkgetsize(int fd, uint64_t *psize)
{
#ifdef BLKGETSIZE64
CHECK_ERROR(ioctl(fd, BLKGETSIZE64, psize), FAIL_IOCTL);
#elif BLKGETSIZE
unsigned long sectors = 0;
CHECK_ERROR(ioctl(fd, BLKGETSIZE, §ors), FAIL_IOCTL);
*psize = sectors * 512ULL;
#else
# error "Linux configuration error (blkgetsize)"
#endif
return FAIL_SUCC;
}
#else
#error "Unsupported platform."
#endif
enum RET_CODES get_file_or_device_size(int fd, uint64_t *const fd_size)
{
struct stat fstat_buf;
enum RET_CODES rc;
CHECK_ERROR(fstat(fd, &fstat_buf), FAIL_FSTAT);
if (S_ISCHR(fstat_buf.st_mode))
{
fprintf(stderr, "Special character device is not supported. Probably you want to use /dev/rdisk device on MacOS?\n");
return FAIL_CHRNOTSUPP;
}
if (S_ISBLK(fstat_buf.st_mode))
{
rc = blkgetsize(fd, fd_size);
return rc;
} else {
*fd_size = fstat_buf.st_size;
}
return FAIL_SUCC;
}