Date: Thu, 15 May 2014 14:48:25 +0000 (UTC) From: Marcel Moolenaar <marcel@FreeBSD.org> To: src-committers@freebsd.org, svn-src-user@freebsd.org Subject: svn commit: r266132 - user/marcel/mkimg Message-ID: <201405151448.s4FEmPsk030675@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: marcel Date: Thu May 15 14:48:25 2014 New Revision: 266132 URL: http://svnweb.freebsd.org/changeset/base/266132 Log: Replace unchecked calls to write(2) and lseek(2) with calls to sparse_write() and make sure to check for errors. In particular, lseek(2) may not be possible (e.g. the output file is stdout) and sparse_write(2) will do what is optimal (i.e. use lseek(2) when possible). Modified: user/marcel/mkimg/vmdk.c Modified: user/marcel/mkimg/vmdk.c ============================================================================== --- user/marcel/mkimg/vmdk.c Thu May 15 14:44:16 2014 (r266131) +++ user/marcel/mkimg/vmdk.c Thu May 15 14:48:25 2014 (r266132) @@ -112,7 +112,8 @@ vmdk_write(int fd) { struct vmdk_header hdr; uint32_t *gt, *gd; - char *desc; + char *buf, *desc; + off_t cur, lim; uint64_t imagesz; size_t gdsz, gtsz; uint32_t sec; @@ -181,17 +182,37 @@ vmdk_write(int fd) for (n = 0; n < ngrains; n++) le32enc(gt + n, sec + n * grainsz); - write(fd, &hdr, VMDK_SECTOR_SIZE); - write(fd, desc, desc_len); - write(fd, gd, gdsz); - write(fd, gt, gtsz); - lseek(fd, sec * VMDK_SECTOR_SIZE, SEEK_SET); - error = image_copyout(fd); - + error = 0; + if (!error && sparse_write(fd, &hdr, VMDK_SECTOR_SIZE) < 0) + error = errno; + if (!error && sparse_write(fd, desc, desc_len) < 0) + error = errno; + if (!error && sparse_write(fd, gd, gdsz) < 0) + error = errno; + if (!error && sparse_write(fd, gt, gtsz) < 0) + error = errno; free(gt); free(gd); free(desc); + if (error) + return (error); + cur = VMDK_SECTOR_SIZE + desc_len + gdsz + gtsz; + lim = sec * VMDK_SECTOR_SIZE; + if (cur < lim) { + buf = calloc(VMDK_SECTOR_SIZE, 1); + if (buf == NULL) + error = ENOMEM; + while (!error && cur < lim) { + if (sparse_write(fd, buf, VMDK_SECTOR_SIZE) < 0) + error = errno; + cur += VMDK_SECTOR_SIZE; + } + if (buf != NULL) + free(buf); + } + if (!error) + error = image_copyout(fd); return (error); }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201405151448.s4FEmPsk030675>