[Midnightbsd-cvs] src [6668] trunk/usr.bin/fetch: sanity

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Thu Jun 12 22:07:22 EDT 2014


Revision: 6668
          http://svnweb.midnightbsd.org/src/?rev=6668
Author:   laffer1
Date:     2014-06-12 22:07:22 -0400 (Thu, 12 Jun 2014)
Log Message:
-----------
sanity

Modified Paths:
--------------
    trunk/usr.bin/fetch/Makefile
    trunk/usr.bin/fetch/fetch.c

Modified: trunk/usr.bin/fetch/Makefile
===================================================================
--- trunk/usr.bin/fetch/Makefile	2014-06-13 02:05:34 UTC (rev 6667)
+++ trunk/usr.bin/fetch/Makefile	2014-06-13 02:07:22 UTC (rev 6668)
@@ -4,11 +4,12 @@
 
 PROG=		fetch
 CSTD?=		c99
+.if ${MK_OPENSSL} != "no"
+DPADD=		${LIBFETCH} ${LIBSSL} ${LIBCRYPTO}
+LDADD=		-lfetch -lssl -lcrypto
+.else
 DPADD=		${LIBFETCH} ${LIBMD}
 LDADD=		-lfetch -lmd
-.if ${MK_OPENSSL} != "no"
-DPADD+=		${LIBSSL} ${LIBCRYPTO}
-LDADD+=		-lssl -lcrypto
 .endif
 
 .include <bsd.prog.mk>

Modified: trunk/usr.bin/fetch/fetch.c
===================================================================
--- trunk/usr.bin/fetch/fetch.c	2014-06-13 02:05:34 UTC (rev 6667)
+++ trunk/usr.bin/fetch/fetch.c	2014-06-13 02:07:22 UTC (rev 6668)
@@ -115,11 +115,13 @@
 
 struct xferstat {
 	char		 name[64];
-	struct timeval	 start;
-	struct timeval	 last;
-	off_t		 size;
-	off_t		 offset;
-	off_t		 rcvd;
+	struct timeval	 start;		/* start of transfer */
+	struct timeval	 last;		/* time of last update */
+	struct timeval	 last2;		/* time of previous last update */
+	off_t		 size;		/* size of file per HTTP hdr */
+	off_t		 offset;	/* starting offset in file */
+	off_t		 rcvd;		/* bytes already received */
+	off_t		 lastrcvd;	/* bytes received since last update */
 };
 
 /*
@@ -139,9 +141,12 @@
 	if (eta > 3600)
 		snprintf(str, sizeof str, "%02ldh%02ldm",
 		    eta / 3600, (eta % 3600) / 60);
+	else if (eta > 0)
+		snprintf(str, sizeof str, "%02ldm%02lds",
+		    eta / 60, eta % 60);
 	else
 		snprintf(str, sizeof str, "%02ldm%02lds",
-		    eta / 60, eta % 60);
+		    elapsed / 60, elapsed % 60);
 	return (str);
 }
 
@@ -173,11 +178,12 @@
 	double delta, bps;
 
 	delta = (xs->last.tv_sec + (xs->last.tv_usec / 1.e6))
-	    - (xs->start.tv_sec + (xs->start.tv_usec / 1.e6));
+	    - (xs->last2.tv_sec + (xs->last2.tv_usec / 1.e6));
+
 	if (delta == 0.0) {
 		snprintf(str, sizeof str, "?? Bps");
 	} else {
-		bps = (xs->rcvd - xs->offset) / delta;
+		bps = (xs->rcvd - xs->lastrcvd) / delta;
 		snprintf(str, sizeof str, "%sps", stat_bytes((off_t)bps));
 	}
 	return (str);
@@ -200,6 +206,7 @@
 	gettimeofday(&now, NULL);
 	if (!force && now.tv_sec <= xs->last.tv_sec)
 		return;
+	xs->last2 = xs->last;
 	xs->last = now;
 
 	fprintf(stderr, "\r%-46.46s", xs->name);
@@ -214,10 +221,16 @@
 		    (int)((100.0 * xs->rcvd) / xs->size),
 		    stat_bytes(xs->size));
 	}
+	if (force == 2) {
+		xs->lastrcvd = xs->offset;
+		xs->last2 = xs->start;
+	}
 	fprintf(stderr, " %s", stat_bps(xs));
-	if (xs->size > 0 && xs->rcvd > 0 &&
-	    xs->last.tv_sec >= xs->start.tv_sec + 10)
+	if ((xs->size > 0 && xs->rcvd > 0 &&
+	     xs->last.tv_sec >= xs->start.tv_sec + 3) ||
+	    force == 2)
 		fprintf(stderr, " %s", stat_eta(xs));
+	xs->lastrcvd = xs->rcvd;
 }
 
 /*
@@ -232,6 +245,7 @@
 	xs->size = size;
 	xs->offset = offset;
 	xs->rcvd = offset;
+	xs->lastrcvd = offset;
 	if (v_tty && v_level > 0)
 		stat_display(xs, 1);
 	else if (v_level > 0)
@@ -257,7 +271,7 @@
 {
 	gettimeofday(&xs->last, NULL);
 	if (v_tty && v_level > 0) {
-		stat_display(xs, 1);
+		stat_display(xs, 2);
 		putc('\n', stderr);
 	} else if (v_level > 0) {
 		fprintf(stderr, "        %s %s\n",
@@ -604,7 +618,10 @@
 			asprintf(&tmppath, "%.*s.fetch.XXXXXX.%s",
 			    (int)(slash - path), path, slash);
 			if (tmppath != NULL) {
-				mkstemps(tmppath, strlen(slash) + 1);
+				if (mkstemps(tmppath, strlen(slash) + 1) == -1) {
+					warn("%s: mkstemps()", path);
+					goto failure;
+				}
 				of = fopen(tmppath, "w");
 				chown(tmppath, sb.st_uid, sb.st_gid);
 				chmod(tmppath, sb.st_mode & ALLPERMS);
@@ -974,7 +991,8 @@
 	if (v_tty)
 		fetchAuthMethod = query_auth;
 	if (N_filename != NULL)
-		setenv("NETRC", N_filename, 1);
+		if (setenv("NETRC", N_filename, 1) == -1)
+			err(1, "setenv: cannot set NETRC=%s", N_filename);
 
 	while (argc) {
 		if ((p = strrchr(*argv, '/')) == NULL)



More information about the Midnightbsd-cvs mailing list