mkuzip.c (209be2056033a52a8df733cbc88eeefc104b16e8) mkuzip.c (bc3b2c55456d06790eb8f2ce26d4dccafce1ffa5)
1/*
2 * Copyright (c) 2004-2016 Maxim Sobolev <sobomax@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 14 unchanged lines hidden (view full) ---

23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
30#include <sys/types.h>
1/*
2 * Copyright (c) 2004-2016 Maxim Sobolev <sobomax@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 14 unchanged lines hidden (view full) ---

23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
30#include <sys/types.h>
31#include <sys/disk.h>
32#include <sys/endian.h>
33#include <sys/param.h>
34#include <sys/sysctl.h>
35#include <sys/stat.h>
36#include <sys/uio.h>
37#include <netinet/in.h>
38#include <assert.h>
39#include <ctype.h>

--- 13 unchanged lines hidden (view full) ---

53#include "mkuz_zlib.h"
54#include "mkuz_lzma.h"
55#include "mkuz_blk.h"
56#include "mkuz_cfg.h"
57#include "mkuz_conveyor.h"
58#include "mkuz_format.h"
59#include "mkuz_fqueue.h"
60#include "mkuz_time.h"
31#include <sys/endian.h>
32#include <sys/param.h>
33#include <sys/sysctl.h>
34#include <sys/stat.h>
35#include <sys/uio.h>
36#include <netinet/in.h>
37#include <assert.h>
38#include <ctype.h>

--- 13 unchanged lines hidden (view full) ---

52#include "mkuz_zlib.h"
53#include "mkuz_lzma.h"
54#include "mkuz_blk.h"
55#include "mkuz_cfg.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"
61
62#define DEFAULT_CLSTSIZE 16384
63
64static 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

--- 20 unchanged lines hidden (view full) ---

89 ap = (uint32_t *)p;
90
91 return (bp->info.blkno == *ap);
92}
93
94int main(int argc, char **argv)
95{
96 struct mkuz_cfg cfs;
61
62#define DEFAULT_CLSTSIZE 16384
63
64static 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

--- 20 unchanged lines hidden (view full) ---

89 ap = (uint32_t *)p;
90
91 return (bp->info.blkno == *ap);
92}
93
94int main(int argc, char **argv)
95{
96 struct mkuz_cfg cfs;
97 char *iname, *oname;
97 char *oname;
98 uint64_t *toc;
99 int i, io, opt, tmp;
100 struct {
101 int en;
102 FILE *f;
103 } summary;
104 struct iovec iov[2];
98 uint64_t *toc;
99 int i, io, opt, tmp;
100 struct {
101 int en;
102 FILE *f;
103 } summary;
104 struct iovec iov[2];
105 struct stat sb;
106 uint64_t offset, last_offset;
107 struct cloop_header hdr;
108 struct mkuz_conveyor *cvp;
109 void *c_ctx;
110 struct mkuz_blk_info *chit;
111 size_t ncpusz, ncpu, magiclen;
112 double st, et;
113

--- 84 unchanged lines hidden (view full) ---

198 if (cfs.en_dedup != 0) {
199 hdr.magic[CLOOP_OFS_VERSN] = CLOOP_MAJVER_3;
200 hdr.magic[CLOOP_OFS_COMPR] =
201 tolower(hdr.magic[CLOOP_OFS_COMPR]);
202 }
203
204 c_ctx = cfs.handler->f_init(cfs.blksz);
205
105 uint64_t offset, last_offset;
106 struct cloop_header hdr;
107 struct mkuz_conveyor *cvp;
108 void *c_ctx;
109 struct mkuz_blk_info *chit;
110 size_t ncpusz, ncpu, magiclen;
111 double st, et;
112

--- 84 unchanged lines hidden (view full) ---

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]);
201 }
202
203 c_ctx = cfs.handler->f_init(cfs.blksz);
204
206 iname = argv[0];
205 cfs.iname = argv[0];
207 if (oname == NULL) {
206 if (oname == NULL) {
208 asprintf(&oname, "%s%s", iname, cfs.handler->default_sufx);
207 asprintf(&oname, "%s%s", cfs.iname, cfs.handler->default_sufx);
209 if (oname == NULL) {
210 err(1, "can't allocate memory");
211 /* Not reached */
212 }
213 }
214
215 signal(SIGHUP, exit);
216 signal(SIGINT, exit);
217 signal(SIGTERM, exit);
218 signal(SIGXCPU, exit);
219 signal(SIGXFSZ, exit);
220 atexit(cleanup);
221
208 if (oname == NULL) {
209 err(1, "can't allocate memory");
210 /* Not reached */
211 }
212 }
213
214 signal(SIGHUP, exit);
215 signal(SIGINT, exit);
216 signal(SIGTERM, exit);
217 signal(SIGXCPU, exit);
218 signal(SIGXFSZ, exit);
219 atexit(cleanup);
220
222 cfs.fdr = open(iname, O_RDONLY);
221 cfs.fdr = open(cfs.iname, O_RDONLY);
223 if (cfs.fdr < 0) {
222 if (cfs.fdr < 0) {
224 err(1, "open(%s)", iname);
223 err(1, "open(%s)", cfs.iname);
225 /* Not reached */
226 }
224 /* Not reached */
225 }
227 if (fstat(cfs.fdr, &sb) != 0) {
228 err(1, "fstat(%s)", iname);
226 cfs.isize = mkuz_get_insize(&cfs);
227 if (cfs.isize < 0) {
228 errx(1, "can't determine input image size");
229 /* Not reached */
230 }
229 /* Not reached */
230 }
231 if (S_ISCHR(sb.st_mode)) {
232 off_t ms;
233
234 if (ioctl(cfs.fdr, DIOCGMEDIASIZE, &ms) < 0) {
235 err(1, "ioctl(DIOCGMEDIASIZE)");
236 /* Not reached */
237 }
238 sb.st_size = ms;
239 } else if (!S_ISREG(sb.st_mode)) {
240 fprintf(stderr, "%s: not a character device or regular file\n",
241 iname);
242 exit(1);
243 }
244 hdr.nblocks = sb.st_size / cfs.blksz;
245 if ((sb.st_size % cfs.blksz) != 0) {
231 hdr.nblocks = cfs.isize / cfs.blksz;
232 if ((cfs.isize % cfs.blksz) != 0) {
246 if (cfs.verbose != 0)
247 fprintf(stderr, "file size is not multiple "
248 "of %d, padding data\n", cfs.blksz);
249 hdr.nblocks++;
250 }
251 toc = mkuz_safe_malloc((hdr.nblocks + 1) * sizeof(*toc));
252
253 cfs.fdw = open(oname, (cfs.en_dedup ? O_RDWR : O_WRONLY) | O_TRUNC | O_CREAT,

--- 11 unchanged lines hidden (view full) ---

265 iov[1].iov_len = (hdr.nblocks + 1) * sizeof(*toc);
266 offset = iov[0].iov_len + iov[1].iov_len;
267
268 /* Reserve space for header */
269 lseek(cfs.fdw, offset, SEEK_SET);
270
271 if (cfs.verbose != 0) {
272 fprintf(stderr, "data size %ju bytes, number of clusters "
233 if (cfs.verbose != 0)
234 fprintf(stderr, "file size is not multiple "
235 "of %d, padding data\n", cfs.blksz);
236 hdr.nblocks++;
237 }
238 toc = mkuz_safe_malloc((hdr.nblocks + 1) * sizeof(*toc));
239
240 cfs.fdw = open(oname, (cfs.en_dedup ? O_RDWR : O_WRONLY) | O_TRUNC | O_CREAT,

--- 11 unchanged lines hidden (view full) ---

252 iov[1].iov_len = (hdr.nblocks + 1) * sizeof(*toc);
253 offset = iov[0].iov_len + iov[1].iov_len;
254
255 /* Reserve space for header */
256 lseek(cfs.fdw, offset, SEEK_SET);
257
258 if (cfs.verbose != 0) {
259 fprintf(stderr, "data size %ju bytes, number of clusters "
273 "%u, index length %zu bytes\n", sb.st_size,
260 "%u, index length %zu bytes\n", cfs.isize,
274 hdr.nblocks, iov[1].iov_len);
275 }
276
277 cvp = mkuz_conveyor_ctor(&cfs);
278
279 last_offset = 0;
280 iblk = oblk = NULL;
281 for(i = io = 0; iblk != MKUZ_BLK_EOF; i++) {

--- 66 unchanged lines hidden (view full) ---

348 }
349
350 close(cfs.fdr);
351
352 if (cfs.verbose != 0 || summary.en != 0) {
353 et = getdtime();
354 fprintf(summary.f, "compressed data to %ju bytes, saved %lld "
355 "bytes, %.2f%% decrease, %.2f bytes/sec.\n", offset,
261 hdr.nblocks, iov[1].iov_len);
262 }
263
264 cvp = mkuz_conveyor_ctor(&cfs);
265
266 last_offset = 0;
267 iblk = oblk = NULL;
268 for(i = io = 0; iblk != MKUZ_BLK_EOF; i++) {

--- 66 unchanged lines hidden (view full) ---

335 }
336
337 close(cfs.fdr);
338
339 if (cfs.verbose != 0 || summary.en != 0) {
340 et = getdtime();
341 fprintf(summary.f, "compressed data to %ju bytes, saved %lld "
342 "bytes, %.2f%% decrease, %.2f bytes/sec.\n", offset,
356 (long long)(sb.st_size - offset),
357 100.0 * (long long)(sb.st_size - offset) /
358 (float)sb.st_size, (float)sb.st_size / (et - st));
343 (long long)(cfs.isize - offset),
344 100.0 * (long long)(cfs.isize - offset) /
345 (float)cfs.isize, (float)cfs.isize / (et - st));
359 }
360
361 /* Convert to big endian */
362 hdr.blksz = htonl(cfs.blksz);
363 hdr.nblocks = htonl(hdr.nblocks);
364 /* Write headers into pre-allocated space */
365 lseek(cfs.fdw, 0, SEEK_SET);
366 if (writev(cfs.fdw, iov, 2) < 0) {

--- 89 unchanged lines hidden ---
346 }
347
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) {

--- 89 unchanged lines hidden ---