Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/netbsd-9]: src/usr.bin/ftp Pull up following revision(s) (requested by l...



details:   https://anonhg.NetBSD.org/src/rev/d3d86fa2ab3a
branches:  netbsd-9
changeset: 990022:d3d86fa2ab3a
user:      martin <martin%NetBSD.org@localhost>
date:      Sun Oct 24 10:13:40 2021 +0000

description:
Pull up following revision(s) (requested by lukem in ticket #1365):

        usr.bin/ftp/fetch.c: revision 1.233

Use raw write(2) instead of fwrite(3) to avoid stream corruption because
of the progress bar interrupts. From RVP.

diffstat:

 usr.bin/ftp/fetch.c |  43 ++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 40 insertions(+), 3 deletions(-)

diffs (71 lines):

diff -r 7d690d341d21 -r d3d86fa2ab3a usr.bin/ftp/fetch.c
--- a/usr.bin/ftp/fetch.c       Sat Oct 23 11:23:05 2021 +0000
+++ b/usr.bin/ftp/fetch.c       Sun Oct 24 10:13:40 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fetch.c,v 1.231.2.1 2021/06/14 11:34:20 martin Exp $   */
+/*     $NetBSD: fetch.c,v 1.231.2.2 2021/10/24 10:13:40 martin Exp $   */
 
 /*-
  * Copyright (c) 1997-2015 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: fetch.c,v 1.231.2.1 2021/06/14 11:34:20 martin Exp $");
+__RCSID("$NetBSD: fetch.c,v 1.231.2.2 2021/10/24 10:13:40 martin Exp $");
 #endif /* not lint */
 
 /*
@@ -138,6 +138,43 @@
        ((urltype) == HTTP_URL_T)
 #endif
 
+/**
+ * fwrite(3) replacement that just uses write(2). Many stdio implementations
+ * don't handle interrupts properly and corrupt the output. We are taking
+ * alarm interrupts because of the progress bar.
+ *
+ * Assumes `fp' is pristine with no prior I/O calls on it.
+ */
+static size_t
+maxwrite(const void *buf, size_t size, size_t nmemb, FILE *fp)
+{
+       const char *p = buf;
+       ssize_t nwr = 0;
+       ssize_t n;
+       int fd = fileno(fp);
+
+       size *= nmemb;  /* assume no overflow */
+
+       while (size > 0) {
+               if ((n = write(fd, p, size)) == -1) {
+                       switch (errno) {
+                       case EINTR:
+                       case EAGAIN:
+#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
+                       case EWOULDBLOCK:
+#endif
+                               continue;
+                       default:
+                               return nwr;
+                       }
+               }
+               p += n;
+               nwr += n;
+               size -= n;
+       }
+       return nwr;
+}
+
 /*
  * Determine if token is the next word in buf (case insensitive).
  * If so, advance buf past the token and any trailing LWS, and
@@ -1650,7 +1687,7 @@
                                }
                                bytes += flen;
                                bufrem -= flen;
-                               if (fwrite(xferbuf, sizeof(char), flen, fout)
+                               if (maxwrite(xferbuf, sizeof(char), flen, fout)
                                    != flen) {
                                        warn("Writing `%s'", savefile);
                                        goto cleanup_fetch_url;



Home | Main Index | Thread Index | Old Index