8a3a7de94e1769a0c30a7b49ec650793c760df79 galt Thu Jun 24 21:19:51 2010 -0700 using tempname equal to output name with ".paraFetch" extension until download is complete, then rename the tempfile to the real name when all done diff --git src/lib/net.c src/lib/net.c index cff8122..d6c2be9 100644 --- src/lib/net.c +++ src/lib/net.c @@ -1276,6 +1276,10 @@ boolean parallelFetch(char *url, int numConnections, char *outPath) /* Open multiple parallel connections to URL to speed downloading */ { +char *origPath = outPath; +char outTemp[1024]; +safef(outTemp, sizeof(outTemp), "%s.paraFetch", outPath); +outPath = outTemp; /* get the size of the file to be downloaded */ off_t fileSize = 0; if (startsWith("http://",url) || startsWith("https://",url)) @@ -1291,10 +1295,14 @@ if (sizeString == NULL) { hashFree(&hash); - warn("No Content-Length: returned in header for %s, can't proceed, sorry", url); - return FALSE; + warn("No Content-Length: returned in header for %s, must limit to a single connection, will not know if data is complete", url); + numConnections = 1; + fileSize = -1; } + else + { fileSize = atoll(sizeString); + } hashFree(&hash); } else @@ -1322,6 +1330,8 @@ /* what is the size of each part */ off_t partSize = (fileSize + numConnections -1) / numConnections; +if (fileSize == -1) + partSize = -1; off_t base = 0; int c; @@ -1342,7 +1352,7 @@ pc->rangeStart = base; base += partSize; pc->partSize = partSize; - if (pc->rangeStart+pc->partSize >= fileSize) + if (fileSize != -1 && pc->rangeStart+pc->partSize >= fileSize) pc->partSize = fileSize - pc->rangeStart; pc->received = 0; char urlExt[1024]; @@ -1353,6 +1363,9 @@ verbose(2,"debug opening url %s\n", urlExt); //debug + if (fileSize == -1) + pc->sd = netUrlOpen(url); + else pc->sd = netUrlOpen(urlExt); if (pc->sd < 0) { @@ -1372,7 +1385,9 @@ { /* Update sd with newSd, replace it with newUrl, etc. */ pc->sd = newSd; + warn("Redirects not supported at this time: %s re-directed to: %s", url, newUrl); freeMem(newUrl); /* redirects with byterange do not work anyway */ + return FALSE; } } if (pc->sd > n) @@ -1453,7 +1468,7 @@ verbose(2,"debug closing descriptor: %d\n", pc->sd); //debug pc->sd = -1; - if (pc->received != pc->partSize) + 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 @@ -1506,6 +1521,9 @@ close(out); +/* rename the successful download to the original name */ +rename(outTemp, origPath); + return TRUE; }