2 * Copyright (c) 2014 Juniper Networks, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
42 #define BUFFER_SIZE (1024*1024)
44 static char image_tmpfile[PATH_MAX];
45 static int image_fd = -1;
46 static lba_t image_size;
54 unlink(image_tmpfile);
58 image_copyin(lba_t blk, int fd, uint64_t *sizep)
65 assert(BUFFER_SIZE % secsz == 0);
67 buffer = malloc(BUFFER_SIZE);
73 rdsz = read(fd, buffer, BUFFER_SIZE);
75 error = (rdsz < 0) ? errno : 0;
81 bcnt = (rdsz + secsz - 1) / secsz;
82 error = image_write(blk, buffer, bcnt);
86 partial = ((ssize_t)(bcnt * secsz) != rdsz) ? 1 : 0;
99 error = image_copyout_region(fd, 0, image_size);
101 error = image_copyout_done(fd);
106 image_copyout_done(int fd)
111 ofs = lseek(fd, 0L, SEEK_CUR);
114 error = (ftruncate(fd, ofs) == -1) ? errno : 0;
119 image_copyout_region(int fd, lba_t blk, lba_t size)
127 ofs = lseek(fd, 0L, SEEK_CUR);
130 if (lseek(image_fd, blk, SEEK_SET) != blk)
132 buffer = malloc(BUFFER_SIZE);
138 sz = (BUFFER_SIZE < size) ? BUFFER_SIZE : size;
139 rdsz = read(image_fd, buffer, sz);
141 error = (rdsz < 0) ? errno : 0;
145 write(fd, buffer, rdsz) :
146 sparse_write(fd, buffer, rdsz);
151 assert(wrsz == rdsz);
159 image_data(lba_t blk, lba_t size)
164 if (lseek(image_fd, blk, SEEK_SET) != blk)
168 buffer = malloc(size);
172 if (read(image_fd, buffer, size) != (ssize_t)size) {
178 while (size > 0 && *p == '\0')
182 return ((size == 0) ? 0 : 1);
193 image_set_size(lba_t blk)
197 if (ftruncate(image_fd, blk * secsz) == -1)
203 image_write(lba_t blk, void *buf, ssize_t len)
207 if (lseek(image_fd, blk, SEEK_SET) != blk)
210 if (sparse_write(image_fd, buf, len) != len)
220 if (atexit(cleanup) == -1)
222 if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
224 snprintf(image_tmpfile, sizeof(image_tmpfile), "%s/mkimg-XXXXXX",
226 image_fd = mkstemp(image_tmpfile);