X-Git-Url: https://git.saurik.com/apple/hfs.git/blobdiff_plain/a56bdb9dd68c843a17a0658b5250e0cc67c22fe8..ec99dd3007e7873c4585ff2dde91ee4d9b7e9da9:/CopyHFSMeta/DeviceWrapper.c diff --git a/CopyHFSMeta/DeviceWrapper.c b/CopyHFSMeta/DeviceWrapper.c index 27c9116..74dd4b6 100644 --- a/CopyHFSMeta/DeviceWrapper.c +++ b/CopyHFSMeta/DeviceWrapper.c @@ -46,24 +46,37 @@ writeExtent(struct IOWrapper *context, DeviceInfo_t *devp, off_t start, off_t le { const size_t bufSize = 1024 * 1024; struct DeviceWrapperContext *ctx = (struct DeviceWrapperContext*)context->context; - uint8_t buffer[bufSize]; + uint8_t *buffer = NULL; + ssize_t retval = 0; off_t total = 0; if (debug) printf("Writing extent <%lld, %lld> to device %s", start, len, ctx->pathname); + buffer = malloc(bufSize); + if (buffer == NULL) { + warn("%s(%s): Could not allocate %zu bytes for buffer", __FILE__, __FUNCTION__, bufSize); + retval = -1; + goto done; + } + while (total < len) { ssize_t nread; size_t amt = MIN(bufSize, len - total); + // XXX - currently, DeviceWrapepr isn't used, but it needs to deal wit unaligned I/O when it is. nread = pread(devp->fd, buffer, amt, start + total); if (nread == -1) { warn("Cannot read from device at offset %lld", start + total); - return -1; + retval = -1; + goto done; } - (void)pwrite(ctx->fd, (char*)buffer + total, amt, start + total); - bp(amt); - total += amt; + (void)pwrite(ctx->fd, (char*)buffer, nread, start + total); + bp(nread); + total += nread; } - return 0; +done: + if (buffer) + free(buffer); + return retval; } /* @@ -83,7 +96,7 @@ SetProgress(struct IOWrapper *context, off_t progr) struct IOWrapper * InitDeviceWrapper(const char *path, DeviceInfo_t *devp) { - struct DeviceWrapperContext ctx = { 0 }; + struct DeviceWrapperContext ctx = { .fd = -1 }; struct DeviceWrapperContext *retctx = NULL; IOWrapper_t *retval = NULL; struct stat sb; @@ -106,8 +119,6 @@ InitDeviceWrapper(const char *path, DeviceInfo_t *devp) goto done; } - ctx.pathname = strdup(rawname); - ctx.fd = open(rawname, O_RDWR); if (ctx.fd == -1) { warn("Cannot open device %s for reading and writing", rawname); @@ -148,5 +159,12 @@ InitDeviceWrapper(const char *path, DeviceInfo_t *devp) retval->cleanup = &noClean; done: + if (!retval) { + free(ctx.pathname); + free(retctx); + if (ctx.fd >= 0) + close(ctx.fd); + } + return retval; }