src/lib/udc.c 1.13
1.13 2009/02/18 00:33:25 galt
Adding FTP support for udc cache lib
Index: src/lib/udc.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/lib/udc.c,v
retrieving revision 1.12
retrieving revision 1.13
diff -b -B -U 4 -r1.12 -r1.13
--- src/lib/udc.c 13 Feb 2009 02:39:05 -0000 1.12
+++ src/lib/udc.c 18 Feb 2009 00:33:25 -0000 1.13
@@ -254,9 +254,9 @@
return total;
}
boolean udcInfoViaHttp(char *url, struct udcRemoteFileInfo *retInfo)
-/* Sets size and last modified time of URL
+/* Gets size and last modified time of URL
* and returns status of HEAD GET. */
{
verbose(2, "checking http remote info on %s\n", url);
struct hash *hash = newHash(0);
@@ -309,8 +309,57 @@
return status;
}
+/********* Section for ftp protocol **********/
+
+int udcDataViaFtp(char *url, bits64 offset, int size, void *buffer)
+/* Fetch a block of data of given size into buffer using the ftp: protocol.
+ * Returns number of bytes actually read. Does an errAbort on
+ * error. Typically will be called with size in the 8k - 64k range. */
+{
+verbose(2, "reading ftp data - %d bytes at %lld - on %s\n", size, offset, url);
+char rangeUrl[1024];
+if (!startsWith("ftp://",url))
+ errAbort("Invalid protocol in url [%s] in udcDataViaFtp, only ftp supported", url);
+safef(rangeUrl, sizeof(rangeUrl), "%s;byterange=%lld-%lld"
+ , url
+ , (long long) offset
+ , (long long) offset + size - 1);
+int sd = netUrlOpen(rangeUrl);
+if (sd < 0)
+ errAbort("Couldn't open %s", url); // do we really want errAbort here?
+
+int rd = 0, total = 0, remaining = size;
+char *buf = (char *)buffer;
+while ((remaining > 0) && ((rd = read(sd, buf, remaining)) > 0))
+ {
+ total += rd;
+ buf += rd;
+ remaining -= rd;
+ }
+if (rd == -1)
+ errnoAbort("error reading socket");
+close(sd);
+
+return total;
+}
+
+boolean udcInfoViaFtp(char *url, struct udcRemoteFileInfo *retInfo)
+/* Gets size and last modified time of FTP URL */
+{
+verbose(2, "checking ftp remote info on %s\n", url);
+long long size = 0;
+time_t t;
+boolean ok = netGetFtpInfo(url, &size, &t);
+if (!ok)
+ return FALSE;
+retInfo->size = size;
+retInfo->updateTime = t;
+return TRUE;
+}
+
+
/********* Non-protocol-specific bits **********/
static char *fileNameInCacheDir(struct udcFile *file, char *fileName)
@@ -441,8 +490,13 @@
{
prot->fetchData = udcDataViaHttp;
prot->fetchInfo = udcInfoViaHttp;
}
+else if (sameString(upToColon, "ftp"))
+ {
+ prot->fetchData = udcDataViaFtp;
+ prot->fetchInfo = udcInfoViaFtp;
+ }
else if (sameString(upToColon, "transparent"))
{
prot->fetchData = udcDataViaTransparent;
prot->fetchInfo = udcInfoViaTransparent;