2 * Copyright (c) 2004-2016 Maxim Sobolev <sobomax@FreeBSD.org>
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>
31 #include <sys/endian.h>
32 #include <sys/param.h>
33 #include <sys/sysctl.h>
36 #include <netinet/in.h>
50 #include "mkuz_cloop.h"
51 #include "mkuz_blockcache.h"
52 #include "mkuz_zlib.h"
53 #include "mkuz_lzma.h"
56 #include "mkuz_conveyor.h"
57 #include "mkuz_format.h"
58 #include "mkuz_fqueue.h"
59 #include "mkuz_time.h"
60 #include "mkuz_insize.h"
62 #define DEFAULT_CLSTSIZE 16384
64 static struct mkuz_format uzip_fmt = {
65 .magic = CLOOP_MAGIC_ZLIB,
66 .default_sufx = DEFAULT_SUFX_ZLIB,
67 .f_init = &mkuz_zlib_init,
68 .f_compress = &mkuz_zlib_compress
71 static struct mkuz_format ulzma_fmt = {
72 .magic = CLOOP_MAGIC_LZMA,
73 .default_sufx = DEFAULT_SUFX_LZMA,
74 .f_init = &mkuz_lzma_init,
75 .f_compress = &mkuz_lzma_compress
78 static struct mkuz_blk *readblock(int, u_int32_t);
79 static void usage(void);
80 static void cleanup(void);
82 static char *cleanfile = NULL;
85 cmp_blkno(const struct mkuz_blk *bp, void *p)
91 return (bp->info.blkno == *ap);
94 int main(int argc, char **argv)
105 uint64_t offset, last_offset;
106 struct cloop_header hdr;
107 struct mkuz_conveyor *cvp;
109 struct mkuz_blk_info *chit;
110 size_t ncpusz, ncpu, magiclen;
115 ncpusz = sizeof(size_t);
116 if (sysctlbyname("hw.ncpu", &ncpu, &ncpusz, NULL, 0) < 0) {
118 } else if (ncpu > MAX_WORKERS_AUTO) {
119 ncpu = MAX_WORKERS_AUTO;
122 memset(&hdr, 0, sizeof(hdr));
123 cfs.blksz = DEFAULT_CLSTSIZE;
130 cfs.handler = &uzip_fmt;
132 struct mkuz_blk *iblk, *oblk;
134 while((opt = getopt(argc, argv, "o:s:vZdLSj:")) != -1) {
143 errx(1, "invalid cluster size specified: %s",
163 cfs.handler = &ulzma_fmt;
174 errx(1, "invalid number of compression threads"
175 " specified: %s", optarg);
194 magiclen = strlcpy(hdr.magic, cfs.handler->magic, sizeof(hdr.magic));
195 assert(magiclen < sizeof(hdr.magic));
197 if (cfs.en_dedup != 0) {
198 hdr.magic[CLOOP_OFS_VERSN] = CLOOP_MAJVER_3;
199 hdr.magic[CLOOP_OFS_COMPR] =
200 tolower(hdr.magic[CLOOP_OFS_COMPR]);
203 c_ctx = cfs.handler->f_init(cfs.blksz);
207 asprintf(&oname, "%s%s", cfs.iname, cfs.handler->default_sufx);
209 err(1, "can't allocate memory");
214 signal(SIGHUP, exit);
215 signal(SIGINT, exit);
216 signal(SIGTERM, exit);
217 signal(SIGXCPU, exit);
218 signal(SIGXFSZ, exit);
221 cfs.fdr = open(cfs.iname, O_RDONLY);
223 err(1, "open(%s)", cfs.iname);
226 cfs.isize = mkuz_get_insize(&cfs);
228 errx(1, "can't determine input image size");
231 hdr.nblocks = cfs.isize / cfs.blksz;
232 if ((cfs.isize % cfs.blksz) != 0) {
233 if (cfs.verbose != 0)
234 fprintf(stderr, "file size is not multiple "
235 "of %d, padding data\n", cfs.blksz);
238 toc = mkuz_safe_malloc((hdr.nblocks + 1) * sizeof(*toc));
240 cfs.fdw = open(oname, (cfs.en_dedup ? O_RDWR : O_WRONLY) | O_TRUNC | O_CREAT,
241 S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
243 err(1, "open(%s)", oname);
248 /* Prepare header that we will write later when we have index ready. */
249 iov[0].iov_base = (char *)&hdr;
250 iov[0].iov_len = sizeof(hdr);
251 iov[1].iov_base = (char *)toc;
252 iov[1].iov_len = (hdr.nblocks + 1) * sizeof(*toc);
253 offset = iov[0].iov_len + iov[1].iov_len;
255 /* Reserve space for header */
256 lseek(cfs.fdw, offset, SEEK_SET);
258 if (cfs.verbose != 0) {
259 fprintf(stderr, "data size %ju bytes, number of clusters "
260 "%u, index length %zu bytes\n", cfs.isize,
261 hdr.nblocks, iov[1].iov_len);
264 cvp = mkuz_conveyor_ctor(&cfs);
268 for(i = io = 0; iblk != MKUZ_BLK_EOF; i++) {
269 iblk = readblock(cfs.fdr, cfs.blksz);
270 mkuz_fqueue_enq(cvp->wrk_queue, iblk);
271 if (iblk != MKUZ_BLK_EOF &&
272 (i < (cfs.nworkers * ITEMS_PER_WORKER))) {
276 oblk = mkuz_fqueue_deq_when(cvp->results, cmp_blkno, &io);
277 assert(oblk->info.blkno == (unsigned)io);
278 oblk->info.offset = offset;
280 if (cfs.en_dedup != 0 && oblk->info.len > 0) {
281 chit = mkuz_blkcache_regblock(cfs.fdw, oblk);
283 * There should be at least one non-empty block
284 * between us and the backref'ed offset, otherwise
285 * we won't be able to parse that sequence correctly
286 * as it would be indistinguishible from another
289 if (chit != NULL && chit->offset == last_offset) {
294 toc[io] = htobe64(chit->offset);
297 if (oblk->info.len > 0 && write(cfs.fdw, oblk->data,
298 oblk->info.len) < 0) {
299 err(1, "write(%s)", oname);
302 toc[io] = htobe64(offset);
303 last_offset = offset;
304 offset += oblk->info.len;
306 if (cfs.verbose != 0) {
307 fprintf(stderr, "cluster #%d, in %u bytes, "
308 "out len=%lu offset=%lu", io, cfs.blksz,
309 (u_long)oblk->info.len, (u_long)be64toh(toc[io]));
311 fprintf(stderr, " (backref'ed to #%d)",
314 fprintf(stderr, "\n");
318 if (iblk == MKUZ_BLK_EOF) {
321 /* Last block, see if we need to add some padding */
322 if ((offset % DEV_BSIZE) == 0)
324 oblk = mkuz_blk_ctor(DEV_BSIZE - (offset % DEV_BSIZE));
325 oblk->info.blkno = io;
326 oblk->info.len = oblk->alen;
327 if (cfs.verbose != 0) {
328 fprintf(stderr, "padding data with %lu bytes "
329 "so that file size is multiple of %d\n",
330 (u_long)oblk->alen, DEV_BSIZE);
332 mkuz_fqueue_enq(cvp->results, oblk);
339 if (cfs.verbose != 0 || summary.en != 0) {
341 fprintf(summary.f, "compressed data to %ju bytes, saved %lld "
342 "bytes, %.2f%% decrease, %.2f bytes/sec.\n", offset,
343 (long long)(cfs.isize - offset),
344 100.0 * (long long)(cfs.isize - offset) /
345 (float)cfs.isize, (float)cfs.isize / (et - st));
348 /* Convert to big endian */
349 hdr.blksz = htonl(cfs.blksz);
350 hdr.nblocks = htonl(hdr.nblocks);
351 /* Write headers into pre-allocated space */
352 lseek(cfs.fdw, 0, SEEK_SET);
353 if (writev(cfs.fdw, iov, 2) < 0) {
354 err(1, "writev(%s)", oname);
363 static struct mkuz_blk *
364 readblock(int fd, u_int32_t clstsize)
367 struct mkuz_blk *rval;
371 rval = mkuz_blk_ctor(clstsize);
373 rval->info.blkno = blockcnt;
375 cpos = lseek(fd, 0, SEEK_CUR);
377 err(1, "readblock: lseek() failed");
380 rval->info.offset = cpos;
382 numread = read(fd, rval->data, clstsize);
384 err(1, "readblock: read() failed");
391 rval->info.len = numread;
399 fprintf(stderr, "usage: mkuzip [-vZdLS] [-o outfile] [-s cluster_size] "
400 "[-j ncompr] infile\n");
405 mkuz_safe_malloc(size_t size)
409 retval = malloc(size);
410 if (retval == NULL) {
411 err(1, "can't allocate memory");
418 mkuz_safe_zmalloc(size_t size)
422 retval = mkuz_safe_malloc(size);
431 if (cleanfile != NULL)
436 mkuz_memvcmp(const void *memory, unsigned char val, size_t size)
440 mm = (const u_char *)memory;
441 return (*mm == val) && memcmp(mm, mm + 1, size - 1) == 0;