108f3ee927dd0fdec11c55c27df80e970e7926d0 galt Mon Sep 14 10:20:37 2015 -0700 optimize code to check the old value and see if there is any change, before just writing the new redir value. Also moved the code into a new subroutine diff --git src/lib/udc.c src/lib/udc.c index 8147316..2f6810d 100644 --- src/lib/udc.c +++ src/lib/udc.c @@ -903,30 +903,69 @@ /* Look up the file size from the local cache bitmap file, or -1 if there * is no cache for url. If retTime is non-null, store the remote update time in it. */ { long long int ret = -1; struct udcBitmap *bits = udcBitmapOpen(bitmapFileName); if (bits != NULL) { ret = bits->fileSize; if (retTime) *retTime = bits->remoteUpdate; } udcBitmapClose(&bits); return ret; } +static void udcTestAndSetRedirect(struct udcFile *file, char *protocol, boolean useCacheInfo) +/* update redirect info */ +{ +if (startsWith("http", protocol)) + { + char *newUrl = NULL; + // read redir from cache if it exists + if (fileExists(file->redirFileName)) + { + readInGulp(file->redirFileName, &newUrl, NULL); + } + if (useCacheInfo) + { + file->connInfo.redirUrl = cloneString(newUrl); + } + else + { + if (file->connInfo.redirUrl) + { + if (!sameOk(file->connInfo.redirUrl, newUrl)) + { + // write redir to cache + char *temp = addSuffix(file->redirFileName, ".temp"); + writeGulp(temp, file->connInfo.redirUrl, strlen(file->connInfo.redirUrl)); + rename(temp, file->redirFileName); + freeMem(temp); + } + } + else + { + // delete redir from cache (if it exists) + if (newUrl) + remove(file->redirFileName); + } + } + freeMem(newUrl); + } +} + struct udcFile *udcFileMayOpen(char *url, char *cacheDir) /* Open up a cached file. cacheDir may be null in which case udcDefaultDir() will be * used. Return NULL if file doesn't exist. * Caching is inactive if defaultDir is NULL or the protocol is "transparent". * */ { if (cacheDir == NULL) cacheDir = udcDefaultDir(); verbose(4, "udcfileOpen(%s, %s)\n", url, cacheDir); /* Parse out protocol. Make it "transparent" if none specified. */ char *protocol = NULL, *afterProtocol = NULL, *colon; boolean isTransparent = FALSE; udcParseUrl(url, &protocol, &afterProtocol, &colon); if (!colon) { @@ -989,58 +1028,31 @@ (void)maybeTouchFile(file->bitmapFileName); } if (udcCacheEnabled()) { /* Make directory. */ makeDirsOnPath(file->cacheDir); /* Figure out a little bit about the extent of the good cached data if any. Open bits bitmap. */ setInitialCachedDataBounds(file, useCacheInfo); file->fdSparse = mustOpenFd(file->sparseFileName, O_RDWR); // update redir with latest redirect status - if (startsWith("http", protocol)) - { - if (useCacheInfo) - { - // read redir from cache - if (fileExists(file->redirFileName)) - { - readInGulp(file->redirFileName, &file->connInfo.redirUrl, NULL); - } - } - else - { - if (info.ci.redirUrl) - { - // write redir to cache - char *temp = addSuffix(file->redirFileName, ".temp"); - writeGulp(temp, file->connInfo.redirUrl, strlen(file->connInfo.redirUrl)); - rename(temp, file->redirFileName); - freeMem(temp); - } - else - { - // delete redir from cache (if it exists) - if (fileExists(file->redirFileName)) - remove(file->redirFileName); - } - } - } + udcTestAndSetRedirect(file, protocol, useCacheInfo); } } freeMem(afterProtocol); return file; } struct udcFile *udcFileOpen(char *url, char *cacheDir) /* Open up a cached file. cacheDir may be null in which case udcDefaultDir() will be * used. Abort if file doesn't exist. */ { struct udcFile *udcFile = udcFileMayOpen(url, cacheDir); if (udcFile == NULL) errAbort("Couldn't open %s", url);