1f20d0bf98e50192fcc35ede31890ca7485c4a29
galt
  Fri Jun 25 00:48:21 2010 -0700
re-arranging to support retrying unfinished parts
diff --git src/lib/net.c src/lib/net.c
index d6c2be9..d4ed0d5 100644
--- src/lib/net.c
+++ src/lib/net.c
@@ -1355,6 +1355,43 @@
     if (fileSize != -1 && pc->rangeStart+pc->partSize >= fileSize)
 	pc->partSize = fileSize - pc->rangeStart;
     pc->received = 0;
+    pc->sd = -4;  /* no connection tried yet */
+    slAddHead(&pcList, pc);
+    }
+slReverse(&pcList);
+
+
+    
+
+int out = open(outPath, O_CREAT|O_WRONLY, 0664);
+if (out < 0)
+    {
+    warn("Unable to open %s for write while downloading %s, can't proceed, sorry", url, outPath);
+    return FALSE;
+    }
+
+
+/* descriptors for select() */
+fd_set rfds;
+struct timeval tv;
+int retval;
+
+verbose(2,"debug: connOpen = %d\n", connOpen); //debug
+verbose(2,"debug: n+1 = %d (select)\n", n+1); //debug
+
+ssize_t readCount = 0;
+#define BUFSIZE 65536 * 4
+char buf[BUFSIZE];
+
+#define SELTIMEOUT 5
+while (TRUE)
+    {
+
+    /* See if we need to open any connections */
+    for(pc = pcList; pc; pc = pc->next)
+	{
+	if (pc->sd == -4)  /* never been opened */
+	    {
     char urlExt[1024];
     safef(urlExt, sizeof(urlExt), "%s;byterange=%llu-%llu"
     , url
@@ -1369,9 +1406,10 @@
     	pc->sd = netUrlOpen(urlExt);
     if (pc->sd < 0)
 	{
-	warn("Couldn't open %s", url);
-	return FALSE;
+		pc->sd = -3;  /* failed to open, can retry later */
 	}
+	    else
+		{
     char *newUrl = NULL;
     int newSd = 0;
     if (startsWith("http://",url) || startsWith("https://",url))
@@ -1390,42 +1428,24 @@
 	    return FALSE;
 	    }
 	}
-    if (pc->sd > n)
-	n = pc->sd;
     ++connOpen;
-    slAddHead(&pcList, pc);
     }
-slReverse(&pcList);
+	    }
+	}
 
-int out = open(outPath, O_CREAT|O_WRONLY, 0664);
-if (out < 0)
+
+    if (connOpen == 0)
     {
-    warn("Unable to open %s for write while downloading %s, can't proceed, sorry", url, outPath);
+	warn("Unable to open any connections to download %s, can't proceed, sorry", url);
     return FALSE;
     }
 
 
-/* descriptors for select() */
-fd_set rfds;
-struct timeval tv;
-int retval;
-
-verbose(2,"debug: connOpen = %d\n", connOpen); //debug
-verbose(2,"debug: n+1 = %d (select)\n", n+1); //debug
-
-ssize_t readCount = 0;
-#define BUFSIZE 65536 * 4
-char buf[BUFSIZE];
-
-#define SELTIMEOUT 5
-while (connOpen > 0)
-    {
-
     FD_ZERO(&rfds);
     n = 0;
     for(pc = pcList; pc; pc = pc->next)
 	{
-	if (pc->sd != -1)
+	if (pc->sd >= 0)
 	    {
 	    FD_SET(pc->sd, &rfds);    /* reset descriptor in readfds for select() */
 	    if (pc->sd > n)
@@ -1470,10 +1490,7 @@
 
 		    if (fileSize != -1 && pc->received != pc->partSize)	
 			{
-			warn("error expected to read %llu, actually read %llu bytes for url %s"
-			    , (unsigned long long) pc->partSize
-			    , (unsigned long long) pc->received
-			    , url);
+			pc->sd = -2;  /* conn was closed before all data was sent, can retry later */
 			return FALSE;
 			}
 		    --connOpen;
@@ -1517,6 +1534,16 @@
 	return FALSE;
 	}
 
+    /* are we done? */
+    if (connOpen == 0)
+	{
+	boolean done = TRUE;
+	for(pc = pcList; pc; pc = pc->next)
+	    if (pc->sd != -1)
+		done = FALSE;
+	if (done) break;
+	}
+
     }
 
 close(out);