7e6ba710dd2889aa522a4e6fd1f8a3ceb94e1055 braney Wed Sep 9 10:22:54 2020 -0700 tweak udc cache code a little to use the correct path name to get the maximum file length diff --git src/lib/udc.c src/lib/udc.c index 746879d..6322bbd 100644 --- src/lib/udc.c +++ src/lib/udc.c @@ -1006,76 +1006,79 @@ *retAuth = NULL; afterProtocol = qEncode(afterProtocol); *retProtocol = protocol; *retAfterProtocol = afterProtocol; *retColon = colon; } void udcParseUrl(char *url, char **retProtocol, char **retAfterProtocol, char **retColon) /* Parse the URL into components that udc treats separately. * *retAfterProtocol is Q-encoded to keep special chars out of filenames. * Free *retProtocol and *retAfterProtocol but not *retColon when done. */ { udcParseUrlFull(url, retProtocol, retAfterProtocol, retColon, NULL); } -static void addElementToDy(struct dyString *dy, char *name) +static void addElementToDy(struct dyString *dy, int maxLen, char *name) /* add one element of a path to a dyString, hashing it if it's longer * than NAME_MAX */ { -if (strlen(name) > pathconf(name, _PC_NAME_MAX)) +if (strlen(name) > maxLen) { unsigned char hash[SHA_DIGEST_LENGTH]; char newName[(SHA_DIGEST_LENGTH + 1) * 2]; SHA1((const unsigned char *)name, strlen(name), hash); hexBinaryString(hash, SHA_DIGEST_LENGTH, newName, (SHA_DIGEST_LENGTH + 1) * 2); dyStringAppend(dy, newName); } else dyStringAppend(dy, name); } -static char *longDirHash(char *name) +static char *longDirHash(char *cacheDir, char *name) /* take a path and hash the elements that are longer than NAME_MAX */ { +int maxLen = pathconf(cacheDir, _PC_NAME_MAX); +if (maxLen < 0) // if we can't get the real system max, assume it's 255 + maxLen = 255; struct dyString *dy = newDyString(strlen(name)); char *ptr = strchr(name, '/'); while(ptr) { *ptr = 0; - addElementToDy(dy, name); + addElementToDy(dy, maxLen, name); dyStringAppend(dy, "/"); name = ptr + 1; ptr = strchr(name, '/'); } -addElementToDy(dy, name); +addElementToDy(dy, maxLen, name); return dyStringCannibalize(&dy); } void udcPathAndFileNames(struct udcFile *file, char *cacheDir, char *protocol, char *afterProtocol) /* Initialize udcFile path and names */ { if (cacheDir==NULL) return; -char *hashedAfterProtocol = longDirHash(afterProtocol); +char *hashedAfterProtocol = longDirHash(cacheDir, afterProtocol); int len = strlen(cacheDir) + 1 + strlen(protocol) + 1 + strlen(hashedAfterProtocol) + 1; file->cacheDir = needMem(len); safef(file->cacheDir, len, "%s/%s/%s", cacheDir, protocol, hashedAfterProtocol); /* Create file names for bitmap and data portions. */ file->bitmapFileName = fileNameInCacheDir(file, bitmapName); file->sparseFileName = fileNameInCacheDir(file, sparseDataName); file->redirFileName = fileNameInCacheDir(file, redirName); } static long long int udcSizeAndModTimeFromBitmap(char *bitmapFileName, time_t *retTime) /* 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;