src/lib/net.c 1.67

1.67 2009/02/12 01:18:00 galt
adding FTP support for byterange url
Index: src/lib/net.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/lib/net.c,v
retrieving revision 1.66
retrieving revision 1.67
diff -b -B -U 4 -r1.66 -r1.67
--- src/lib/net.c	10 Feb 2009 21:43:14 -0000	1.66
+++ src/lib/net.c	12 Feb 2009 01:18:00 -0000	1.67
@@ -527,8 +525,14 @@
 
 rs = sendFtpCommand(sd, "PASV\r\n", TRUE);
 /* 227 Entering Passive Mode (128,231,210,81,222,250) */
 
+if ((npu.byteRangeStart != -1) && (npu.byteRangeEnd != -1))
+    {
+    safef(cmd,sizeof(cmd),"REST %lld\r\n", (long long) npu.byteRangeStart);
+    sendFtpCommand(sd, cmd, FALSE);
+    }
+
 safef(cmd,sizeof(cmd),"RETR %s\r\n", npu.file);
 sendFtpCommandOnly(sd, cmd);  
 
 sdata = netMustConnect(npu.host, parsePasvPort(rs->string));
@@ -539,9 +543,9 @@
 
 /* see which comes first, an error message on the control conn
  * or data on the data conn */
 
-int secondsWaited=0;
+int secondsWaited = 0;
 while (TRUE)
     {
     if (secondsWaited >= 10)
 	{
@@ -586,17 +590,29 @@
     close(pipefd[0]);  /* close unused half of pipe */
 
     char buf[32768];
     int rd = 0;
+    long long dataPos = 0; 
+    if ((npu.byteRangeStart != -1) && (npu.byteRangeEnd != -1))
+	dataPos = npu.byteRangeStart;
     while((rd = read(sdata, buf, 32768)) > 0) 
 	{
+	if ((npu.byteRangeStart != -1) && (npu.byteRangeEnd != -1))
+	    if ((dataPos + rd) > npu.byteRangeEnd)
+		rd = npu.byteRangeEnd - dataPos + 1;
 	int wt = write(pipefd[1], buf, rd);
 	if (wt == -1)
 	    errnoAbort("error writing ftp data to pipe");
+	dataPos += rd;
+	if ((npu.byteRangeStart != -1) && (npu.byteRangeEnd != -1))
+	    if (dataPos >= npu.byteRangeEnd)
+		break;	    
 	}
     if (rd == -1)
 	errnoAbort("error reading ftp socket");
     close(pipefd[1]);  /* being safe */
+    close(sd);
+    close(sdata);
 
     exit(0);
 
     /* child will never get to here */