summaryrefslogtreecommitdiffstats
path: root/usr.bin/mkuzip
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin/mkuzip')
-rw-r--r--usr.bin/mkuzip/mkuz_blockcache.c49
-rw-r--r--usr.bin/mkuzip/mkuzip.83
-rw-r--r--usr.bin/mkuzip/mkuzip.c21
3 files changed, 66 insertions, 7 deletions
diff --git a/usr.bin/mkuzip/mkuz_blockcache.c b/usr.bin/mkuzip/mkuz_blockcache.c
index a3ac564..204fce2 100644
--- a/usr.bin/mkuzip/mkuz_blockcache.c
+++ b/usr.bin/mkuzip/mkuz_blockcache.c
@@ -29,7 +29,9 @@
__FBSDID("$FreeBSD$");
#include <sys/types.h>
+#include <err.h>
#include <md5.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
@@ -49,6 +51,35 @@ struct mkuz_blkcache {
static struct mkuz_blkcache blkcache;
+static int
+verify_match(int fd, off_t data_offset, void *data, ssize_t len,
+ struct mkuz_blkcache *bcep)
+{
+ void *vbuf;
+ ssize_t rlen;
+ int rval;
+
+ rval = -1;
+ vbuf = malloc(len);
+ if (vbuf == NULL) {
+ goto e0;
+ }
+ if (lseek(fd, bcep->data_offset, SEEK_SET) < 0) {
+ goto e1;
+ }
+ rlen = read(fd, vbuf, len);
+ if (rlen != len) {
+ goto e2;
+ }
+ rval = (memcmp(data, vbuf, len) == 0) ? 1 : 0;
+e2:
+ lseek(fd, data_offset, SEEK_SET);
+e1:
+ free(vbuf);
+e0:
+ return (rval);
+}
+
struct mkuz_blkcache_hit *
mkuz_blkcache_regblock(int fd, uint32_t blkno, off_t offset, ssize_t len,
void *data)
@@ -57,6 +88,7 @@ mkuz_blkcache_regblock(int fd, uint32_t blkno, off_t offset, ssize_t len,
MD5_CTX mcontext;
off_t data_offset;
unsigned char mdigest[16];
+ int rval;
data_offset = lseek(fd, 0, SEEK_CUR);
if (data_offset < 0) {
@@ -76,10 +108,23 @@ mkuz_blkcache_regblock(int fd, uint32_t blkno, off_t offset, ssize_t len,
}
}
if (bcep != NULL) {
+ rval = verify_match(fd, data_offset, data, len, bcep);
+ if (rval == 1) {
+#if defined(MKUZ_DEBUG)
+ fprintf(stderr, "cache hit %d, %d, %d\n",
+ (int)bcep->hit.offset, (int)data_offset, (int)len);
+#endif
+ return (&bcep->hit);
+ }
+ if (rval == 0) {
#if defined(MKUZ_DEBUG)
- printf("cache hit %d, %d, %d\n", (int)bcep->hit.offset, (int)data_offset, (int)len);
+ fprintf(stderr, "block MD5 collision, you should try lottery, "
+ "man!\n");
#endif
- return (&bcep->hit);
+ return (NULL);
+ }
+ warn("verify_match");
+ return (NULL);
}
bcep = malloc(sizeof(struct mkuz_blkcache));
if (bcep == NULL)
diff --git a/usr.bin/mkuzip/mkuzip.8 b/usr.bin/mkuzip/mkuzip.8
index c839551..cb05098 100644
--- a/usr.bin/mkuzip/mkuzip.8
+++ b/usr.bin/mkuzip/mkuzip.8
@@ -118,6 +118,9 @@ detects identical blocks in the input and replaces each subsequent occurence
of such block with pointer to the very first one in the output.
Setting this option results is moderate decrease of compressed image size,
typically around 3-5% of a final size of the compressed image.
+.It Fl S
+Print summary about the compression ratio as well as output
+file size after file has been processed.
.El
.Sh NOTES
The compression ratio largely depends on the cluster size used.
diff --git a/usr.bin/mkuzip/mkuzip.c b/usr.bin/mkuzip/mkuzip.c
index 1e3a204..c436075 100644
--- a/usr.bin/mkuzip/mkuzip.c
+++ b/usr.bin/mkuzip/mkuzip.c
@@ -90,6 +90,10 @@ int main(int argc, char **argv)
char *iname, *oname, *obuf, *ibuf;
uint64_t *toc;
int fdr, fdw, i, opt, verbose, no_zcomp, tmp, en_dedup;
+ struct {
+ int en;
+ FILE *f;
+ } summary;
struct iovec iov[2];
struct stat sb;
uint32_t destlen;
@@ -104,9 +108,11 @@ int main(int argc, char **argv)
verbose = 0;
no_zcomp = 0;
en_dedup = 0;
+ summary.en = 0;
+ summary.f = stderr;
handler = &uzip_fmt;
- while((opt = getopt(argc, argv, "o:s:vZdL")) != -1) {
+ while((opt = getopt(argc, argv, "o:s:vZdLS")) != -1) {
switch(opt) {
case 'o':
oname = optarg;
@@ -138,6 +144,11 @@ int main(int argc, char **argv)
handler = &ulzma_fmt;
break;
+ case 'S':
+ summary.en = 1;
+ summary.f = stdout;
+ break;
+
default:
usage();
/* Not reached */
@@ -210,7 +221,7 @@ int main(int argc, char **argv)
}
toc = mkuz_safe_malloc((hdr.nblocks + 1) * sizeof(*toc));
- fdw = open(oname, O_WRONLY | O_TRUNC | O_CREAT,
+ fdw = open(oname, (en_dedup ? O_RDWR : O_WRONLY) | O_TRUNC | O_CREAT,
S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
if (fdw < 0) {
err(1, "open(%s)", oname);
@@ -294,8 +305,8 @@ int main(int argc, char **argv)
}
close(fdr);
- if (verbose != 0)
- fprintf(stderr, "compressed data to %ju bytes, saved %lld "
+ if (verbose != 0 || summary.en != 0)
+ fprintf(summary.f, "compressed data to %ju bytes, saved %lld "
"bytes, %.2f%% decrease.\n", offset,
(long long)(sb.st_size - offset),
100.0 * (long long)(sb.st_size - offset) /
@@ -337,7 +348,7 @@ static void
usage(void)
{
- fprintf(stderr, "usage: mkuzip [-vZdL] [-o outfile] [-s cluster_size] "
+ fprintf(stderr, "usage: mkuzip [-vZdLS] [-o outfile] [-s cluster_size] "
"infile\n");
exit(1);
}
OpenPOWER on IntegriCloud