Creating Compressed Loop FS from stdin

Peter Pentchev roam at ringlet.net
Thu Dec 30 06:55:47 PST 2004


On Thu, Dec 30, 2004 at 04:20:16PM +0200, Maxim Sobolev wrote:
> You don't check return code of the second lseek - I bet it fails. This 
> probably leads to creation of seemingly valid loop fs (i.e. with valid 
> header), but filled with zeroes or some random junk.

I said I'd tested it before posting it the first time.  It works.
It creates a valid loop fs, containing exactly the files that are in
the input ISO image.

However, your point is valid; here's another patch which returns to the
start only if the original lseek() did not move away (or failed), and
checks the return value, too.  This version of mkuzip works on today's
RELENG_5, as can be seen from the attached mkuzip.script sequence of
commands.

G'luck,
Peter

-- 
Peter Pentchev	roam at ringlet.net    roam at cnsys.bg    roam at FreeBSD.org
PGP key:	http://people.FreeBSD.org/~roam/roam.key.asc
Key fingerprint	FDBA FD79 C26F 3C51 C95E  DF9E ED18 B68D 1619 4553
Nostalgia ain't what it used to be.
-------------- next part --------------
Script started on Thu Dec 30 16:50:57 2004
Starting interactive C shell
[roam at straylight ~/fbsd/r/src/usr.bin/mkuzip]> `make -V .OBJDIR`/`make -V PROG` -v -s 65536 -o dns-stdin.uz /dev/stdin < ~/dns.iso
data size 5505024 bytes, number of clusters 84, index lengh 680 bytes
cluster #0, in 65536 bytes, out 2263 bytes
cluster #1, in 65536 bytes, out 11581 bytes
[snip more of the same]
cluster #82, in 65536 bytes, out 11684 bytes
cluster #83, in 65536 bytes, out 752 bytes
padding data with 161 bytes so that file size is multiple of 512
compressed data to 1361920 bytes, saved 4143104 bytes, 75.26% decrease.
[roam at straylight ~/fbsd/r/src/usr.bin/mkuzip]> sudo mdconfig -a -f dns-stdin.uz
otp-md5 455 st1434 ext
Password: 
md0
[roam at straylight ~/fbsd/r/src/usr.bin/mkuzip]> sudo mount_cd9660 /dev/md0.uzip /mnt
[roam at straylight ~/fbsd/r/src/usr.bin/mkuzip]> sudo find /mnt | wc -l
     604
[roam at straylight ~/fbsd/r/src/usr.bin/mkuzip]> sudo md5 /mnt/djbdns-1.05/tinydns
MD5 (/mnt/djbdns-1.05/tinydns) = d16c2fe610c19148ff005fb35242561c
[roam at straylight ~/fbsd/r/src/usr.bin/mkuzip]> sudo md5 /home/roam/fbsd/r/ports/dns/djbdns/work/djbdns-1.05/tinydns
MD5 (/home/roam/fbsd/r/ports/dns/djbdns/work/djbdns-1.05/tinydns) = d16c2fe610c19148ff005fb35242561c
[roam at straylight ~/fbsd/r/src/usr.bin/mkuzip]> sudo umount /mnt
[roam at straylight ~/fbsd/r/src/usr.bin/mkuzip]> sudo mdconfig -d -u 0
[roam at straylight ~/fbsd/r/src/usr.bin/mkuzip]> exit
exit

Script done on Thu Dec 30 16:52:53 2004
-------------- next part --------------
Index: src/usr.bin/mkuzip/mkuzip.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/mkuzip/mkuzip.c,v
retrieving revision 1.2
diff -u -r1.2 mkuzip.c
--- src/usr.bin/mkuzip/mkuzip.c	10 Sep 2004 23:16:05 -0000	1.2
+++ src/usr.bin/mkuzip/mkuzip.c	30 Dec 2004 14:47:35 -0000
@@ -122,10 +122,28 @@
 	signal(SIGXFSZ, exit);
 	atexit(cleanup);
 
-	if (stat(iname, &sb) != 0) {
+	fdr = open(iname, O_RDONLY);
+	if (fdr < 0) {
+		err(1, "%s", iname);
+		/* Not reached */
+	}
+
+	/* Try seeking to the end; if that fails, fall back to fstat() */
+	memset(&sb, 0, sizeof(sb));
+	sb.st_size = lseek(fdr, 0, SEEK_END);
+	if (sb.st_size < 1) {
+		if (fstat(fdr, &sb) != 0) {
+			err(1, "%s", iname);
+			/* Not reached */
+		}
+	} else if (lseek(fdr, 0, SEEK_SET) != 0) {
 		err(1, "%s", iname);
 		/* Not reached */
 	}
+	if (sb.st_size < 1) {
+		errx(1, "Could not determine the size of %s", iname);
+		/* Not reached */
+	}
 	hdr.nblocks = sb.st_size / hdr.blksz;
 	if ((sb.st_size % hdr.blksz) != 0) {
 		if (verbose != 0)
@@ -135,11 +153,6 @@
 	}
 	toc = safe_malloc((hdr.nblocks + 1) * sizeof(*toc));
 
-	fdr = open(iname, O_RDONLY);
-	if (fdr < 0) {
-		err(1, "%s", iname);
-		/* Not reached */
-	}
 	fdw = open(oname, O_WRONLY | O_TRUNC | O_CREAT,
 		   S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
 	if (fdw < 0) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20041230/d6d89e64/attachment.bin


More information about the freebsd-hackers mailing list