2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2016 Maxim Sobolev <sobomax@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #include <sys/endian.h>
34 #include <sys/param.h>
35 #include <sys/sysctl.h>
38 #include <netinet/in.h>
52 #include "mkuz_cloop.h"
53 #include "mkuz_blockcache.h"
54 #include "mkuz_zlib.h"
55 #include "mkuz_lzma.h"
58 #include "mkuz_conveyor.h"
59 #include "mkuz_format.h"
60 #include "mkuz_fqueue.h"
61 #include "mkuz_time.h"
62 #include "mkuz_insize.h"
64 #define DEFAULT_CLSTSIZE 16384
66 static struct mkuz_format uzip_fmt = {
67 .magic = CLOOP_MAGIC_ZLIB,
68 .default_sufx = DEFAULT_SUFX_ZLIB,
69 .f_init = &mkuz_zlib_init,
70 .f_compress = &mkuz_zlib_compress
73 static struct mkuz_format ulzma_fmt = {
74 .magic = CLOOP_MAGIC_LZMA,
75 .default_sufx = DEFAULT_SUFX_LZMA,
76 .f_init = &mkuz_lzma_init,
77 .f_compress = &mkuz_lzma_compress
80 static struct mkuz_blk *readblock(int, u_int32_t);
81 static void usage(void);
82 static void cleanup(void);
84 static char *cleanfile = NULL;
87 cmp_blkno(const struct mkuz_blk *bp, void *p)
93 return (bp->info.blkno == *ap);
96 int main(int argc, char **argv)
107 uint64_t offset, last_offset;
108 struct cloop_header hdr;
109 struct mkuz_conveyor *cvp;
111 struct mkuz_blk_info *chit;
112 size_t ncpusz, ncpu, magiclen;
117 ncpusz = sizeof(size_t);
118 if (sysctlbyname("hw.ncpu", &ncpu, &ncpusz, NULL, 0) < 0) {
120 } else if (ncpu > MAX_WORKERS_AUTO) {
121 ncpu = MAX_WORKERS_AUTO;
124 memset(&hdr, 0, sizeof(hdr));
125 cfs.blksz = DEFAULT_CLSTSIZE;
132 cfs.handler = &uzip_fmt;
134 struct mkuz_blk *iblk, *oblk;
136 while((opt = getopt(argc, argv, "o:s:vZdLSj:")) != -1) {
145 errx(1, "invalid cluster size specified: %s",
165 cfs.handler = &ulzma_fmt;
176 errx(1, "invalid number of compression threads"
177 " specified: %s", optarg);
196 magiclen = strlcpy(hdr.magic, cfs.handler->magic, sizeof(hdr.magic));
197 assert(magiclen < sizeof(hdr.magic));
199 if (cfs.en_dedup != 0) {
200 hdr.magic[CLOOP_OFS_VERSN] = CLOOP_MAJVER_3;
201 hdr.magic[CLOOP_OFS_COMPR] =
202 tolower(hdr.magic[CLOOP_OFS_COMPR]);
205 c_ctx = cfs.handler->f_init(cfs.blksz);
209 asprintf(&oname, "%s%s", cfs.iname, cfs.handler->default_sufx);
211 err(1, "can't allocate memory");
216 signal(SIGHUP, exit);
217 signal(SIGINT, exit);
218 signal(SIGTERM, exit);
219 signal(SIGXCPU, exit);
220 signal(SIGXFSZ, exit);
223 cfs.fdr = open(cfs.iname, O_RDONLY);
225 err(1, "open(%s)", cfs.iname);
228 cfs.isize = mkuz_get_insize(&cfs);
230 errx(1, "can't determine input image size");
233 hdr.nblocks = cfs.isize / cfs.blksz;
234 if ((cfs.isize % cfs.blksz) != 0) {
235 if (cfs.verbose != 0)
236 fprintf(stderr, "file size is not multiple "
237 "of %d, padding data\n", cfs.blksz);
240 toc = mkuz_safe_malloc((hdr.nblocks + 1) * sizeof(*toc));
242 cfs.fdw = open(oname, (cfs.en_dedup ? O_RDWR : O_WRONLY) | O_TRUNC | O_CREAT,
243 S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
245 err(1, "open(%s)", oname);
250 /* Prepare header that we will write later when we have index ready. */
251 iov[0].iov_base = (char *)&hdr;
252 iov[0].iov_len = sizeof(hdr);
253 iov[1].iov_base = (char *)toc;
254 iov[1].iov_len = (hdr.nblocks + 1) * sizeof(*toc);
255 offset = iov[0].iov_len + iov[1].iov_len;
257 /* Reserve space for header */
258 lseek(cfs.fdw, offset, SEEK_SET);
260 if (cfs.verbose != 0) {
261 fprintf(stderr, "data size %ju bytes, number of clusters "
262 "%u, index length %zu bytes\n", cfs.isize,
263 hdr.nblocks, iov[1].iov_len);
266 cvp = mkuz_conveyor_ctor(&cfs);
270 for(i = io = 0; iblk != MKUZ_BLK_EOF; i++) {
271 iblk = readblock(cfs.fdr, cfs.blksz);
272 mkuz_fqueue_enq(cvp->wrk_queue, iblk);
273 if (iblk != MKUZ_BLK_EOF &&
274 (i < (cfs.nworkers * ITEMS_PER_WORKER))) {
278 oblk = mkuz_fqueue_deq_when(cvp->results, cmp_blkno, &io);
279 assert(oblk->info.blkno == (unsigned)io);
280 oblk->info.offset = offset;
282 if (cfs.en_dedup != 0 && oblk->info.len > 0) {
283 chit = mkuz_blkcache_regblock(cfs.fdw, oblk);
285 * There should be at least one non-empty block
286 * between us and the backref'ed offset, otherwise
287 * we won't be able to parse that sequence correctly
288 * as it would be indistinguishible from another
291 if (chit != NULL && chit->offset == last_offset) {
296 toc[io] = htobe64(chit->offset);
299 if (oblk->info.len > 0 && write(cfs.fdw, oblk->data,
300 oblk->info.len) < 0) {
301 err(1, "write(%s)", oname);
304 toc[io] = htobe64(offset);
305 last_offset = offset;
306 offset += oblk->info.len;
308 if (cfs.verbose != 0) {
309 fprintf(stderr, "cluster #%d, in %u bytes, "
310 "out len=%lu offset=%lu", io, cfs.blksz,
311 (u_long)oblk->info.len, (u_long)be64toh(toc[io]));
313 fprintf(stderr, " (backref'ed to #%d)",
316 fprintf(stderr, "\n");
320 if (iblk == MKUZ_BLK_EOF) {
323 /* Last block, see if we need to add some padding */
324 if ((offset % DEV_BSIZE) == 0)
326 oblk = mkuz_blk_ctor(DEV_BSIZE - (offset % DEV_BSIZE));
327 oblk->info.blkno = io;
328 oblk->info.len = oblk->alen;
329 if (cfs.verbose != 0) {
330 fprintf(stderr, "padding data with %lu bytes "
331 "so that file size is multiple of %d\n",
332 (u_long)oblk->alen, DEV_BSIZE);
334 mkuz_fqueue_enq(cvp->results, oblk);
341 if (cfs.verbose != 0 || summary.en != 0) {
343 fprintf(summary.f, "compressed data to %ju bytes, saved %lld "
344 "bytes, %.2f%% decrease, %.2f bytes/sec.\n", offset,
345 (long long)(cfs.isize - offset),
346 100.0 * (long long)(cfs.isize - offset) /
347 (float)cfs.isize, (float)cfs.isize / (et - st));
350 /* Convert to big endian */
351 hdr.blksz = htonl(cfs.blksz);
352 hdr.nblocks = htonl(hdr.nblocks);
353 /* Write headers into pre-allocated space */
354 lseek(cfs.fdw, 0, SEEK_SET);
355 if (writev(cfs.fdw, iov, 2) < 0) {
356 err(1, "writev(%s)", oname);
365 static struct mkuz_blk *
366 readblock(int fd, u_int32_t clstsize)
369 struct mkuz_blk *rval;
373 rval = mkuz_blk_ctor(clstsize);
375 rval->info.blkno = blockcnt;
377 cpos = lseek(fd, 0, SEEK_CUR);
379 err(1, "readblock: lseek() failed");
382 rval->info.offset = cpos;
384 numread = read(fd, rval->data, clstsize);
386 err(1, "readblock: read() failed");
393 rval->info.len = numread;
401 fprintf(stderr, "usage: mkuzip [-vZdLS] [-o outfile] [-s cluster_size] "
402 "[-j ncompr] infile\n");
407 mkuz_safe_malloc(size_t size)
411 retval = malloc(size);
412 if (retval == NULL) {
413 err(1, "can't allocate memory");
420 mkuz_safe_zmalloc(size_t size)
424 retval = mkuz_safe_malloc(size);
433 if (cleanfile != NULL)
438 mkuz_memvcmp(const void *memory, unsigned char val, size_t size)
442 mm = (const u_char *)memory;
443 return (*mm == val) && memcmp(mm, mm + 1, size - 1) == 0;