]> git.saurik.com Git - apple/hfs.git/blame - CopyHFSMeta/DeviceWrapper.c
hfs-556.100.11.tar.gz
[apple/hfs.git] / CopyHFSMeta / DeviceWrapper.c
CommitLineData
a56bdb9d
A
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <unistd.h>
5#include <fcntl.h>
6#include <err.h>
7#include <errno.h>
8#include <sys/stat.h>
9#include <sys/fcntl.h>
10#include <sys/disk.h>
11
12#include "hfsmeta.h"
13
14/*
15 * Functions to wrap around a device.
16 */
17#define MIN(a, b) \
18 ({ __typeof(a) __a = (a); __typeof(b) __b = (b); \
19 __a < __b ? __a : __b; })
20
21struct DeviceWrapperContext {
22 char *pathname;
23 size_t blockSize;
24 off_t devSize;
25 int fd;
26};
27
28static int
29noClean(struct IOWrapper *ctx)
30{
31 // Conceivably, we could erase the entire device
32 return 0;
33}
34
35static ssize_t
36doRead(struct IOWrapper *ctx, off_t start, void *buffer, off_t len)
37{
38 // For now, just do a pread
39 struct DeviceWrapperContext *dctx = (struct DeviceWrapperContext*)ctx->context;
40
41 return pread(dctx->fd, buffer, (size_t)len, start);
42}
43
44static ssize_t
45writeExtent(struct IOWrapper *context, DeviceInfo_t *devp, off_t start, off_t len, void (^bp)(off_t))
46{
47 const size_t bufSize = 1024 * 1024;
48 struct DeviceWrapperContext *ctx = (struct DeviceWrapperContext*)context->context;
51e135ce
A
49 uint8_t *buffer = NULL;
50 ssize_t retval = 0;
a56bdb9d
A
51 off_t total = 0;
52
53 if (debug) printf("Writing extent <%lld, %lld> to device %s", start, len, ctx->pathname);
54
51e135ce
A
55 buffer = malloc(bufSize);
56 if (buffer == NULL) {
57 warn("%s(%s): Could not allocate %zu bytes for buffer", __FILE__, __FUNCTION__, bufSize);
58 retval = -1;
59 goto done;
60 }
61
a56bdb9d
A
62 while (total < len) {
63 ssize_t nread;
64 size_t amt = MIN(bufSize, len - total);
51e135ce 65 // XXX - currently, DeviceWrapepr isn't used, but it needs to deal wit unaligned I/O when it is.
a56bdb9d
A
66 nread = pread(devp->fd, buffer, amt, start + total);
67 if (nread == -1) {
68 warn("Cannot read from device at offset %lld", start + total);
51e135ce
A
69 retval = -1;
70 goto done;
a56bdb9d 71 }
04a16b11
A
72 (void)pwrite(ctx->fd, (char*)buffer, nread, start + total);
73 bp(nread);
74 total += nread;
a56bdb9d 75 }
51e135ce
A
76done:
77 if (buffer)
78 free(buffer);
79 return retval;
a56bdb9d
A
80}
81
82/*
83 * Device files can't have progress information stored, so we don't do anything.
84 */
85static off_t
86GetProgress(struct IOWrapper *context)
87{
88 return 0;
89}
90static void
91SetProgress(struct IOWrapper *context, off_t progr)
92{
93 return;
94}
95
96struct IOWrapper *
97InitDeviceWrapper(const char *path, DeviceInfo_t *devp)
98{
558d2836 99 struct DeviceWrapperContext ctx = { .fd = -1 };
a56bdb9d
A
100 struct DeviceWrapperContext *retctx = NULL;
101 IOWrapper_t *retval = NULL;
102 struct stat sb;
103 uint64_t blockCount;
104 char rawname[strlen(path) + 2]; // /dev/disk5 -> /dev/rdisk5
105
106 if (strncmp(path, "/dev/disk", 9) == 0) {
107 // Need to make it into a raw device name
108 sprintf(rawname, "/dev/rdisk%s", path + 9);
109 } else {
110 strcpy(rawname, path);
111 }
112
113 if (lstat(rawname, &sb) == -1) {
114 warn("cannot examine raw device %s", rawname);
115 goto done;
116 }
117 if ((sb.st_mode & S_IFMT) != S_IFCHR) {
118 warnx("device %s is not a raw device", rawname);
119 goto done;
120 }
121
a56bdb9d
A
122 ctx.fd = open(rawname, O_RDWR);
123 if (ctx.fd == -1) {
124 warn("Cannot open device %s for reading and writing", rawname);
125 goto done;
126 }
127
128 if (ioctl(ctx.fd, DKIOCGETBLOCKSIZE, &ctx.blockSize) == -1) {
129 ctx.blockSize = 512; // A reasonable default
130 }
131 if (ioctl(ctx.fd, DKIOCGETBLOCKCOUNT, &blockCount) == -1) {
132 warn("Cannot block count for device %s", rawname);
133 goto done;
134 }
135 ctx.devSize = ctx.blockSize * blockCount;
136
137 if (ctx.devSize != devp->size) {
138 warnx("Device %s is not the same size (%lld) as source device (%lld)", rawname, ctx.devSize, devp->size);
139 goto done;
140 }
141
142 ctx.pathname = strdup(rawname);
927b7b56
A
143 if (ctx.pathname == NULL) {
144 warn("Cannot strdup the pathname");
145 goto done;
146 }
a56bdb9d
A
147 retctx = malloc(sizeof(ctx));
148 if (retctx == NULL) {
149 warn("Cannot allocate space for device context");
150 goto done;
151 }
152 *retctx = ctx;
153 retval = malloc(sizeof(*retval));
154 if (retval == NULL) {
155 warn("Cannot allocate space for device wrapper");
156 goto done;
157 }
158 retval->context = retctx;
159 retval->reader = &doRead;
160 retval->writer = &writeExtent;
161 retval->getprog = &GetProgress;
162 retval->setprog = &SetProgress;
163 retval->cleanup = &noClean;
164
165done:
558d2836
A
166 if (!retval) {
167 free(ctx.pathname);
168 free(retctx);
169 if (ctx.fd >= 0)
170 close(ctx.fd);
171 }
172
a56bdb9d
A
173 return retval;
174}