59e2bbf2d3a99e5f18436a756e1e5c8b64308e33 braney Tue Sep 8 09:21:02 2026 -0700 Copy a track collection's hub file when the program that writes it asks for a copy, instead of on every session load. cartCopyLocalHubs ran on every session load, in every CGI and in four more places in hgSession. For each customComposite- cart variable it copied the collection's hub file to a fresh trash name and registered the copy in hgcentral.hubStatus to get a hub id. Nothing removed the old row. 81% of the 3.1 million rows in hubStatus on the RR are these dead registrations, and each load also took the central_hubStatus advisory lock and wrote to a MyISAM table, which serializes every concurrent load of a shared session that carries a collection. The copy itself is needed. A saved session's hub file lives under sessionDataDir and every load of that session names it, so hgCollection must not write it in place. It was just being made for every load, and almost no load is followed by an edit. hgCollection is the only program that writes one of these files, so it now asks for its own copy: main() calls cartRequestLocalHubCopy() before it opens the cart, and cartNew() makes the copy for it. That has to be a property of the program rather than of the request, and the copy has to be made at cart open, above hubConnectLoadHubs. Copying the file gives the hub a new id, the hubs are loaded during cart open, and trackList carries that id in every track name, so a copy made any later leaves printTrackDbListToHub looking for the collection under an id trackList does not have and the hub file comes out with a header and no tracks. A condition that instead tries to work out whether this particular request will write cannot be correct either: "cmd" is not in hgTracks' excludeVars, so it persists in the cart and hgCollection dispatches on the cart rather than on the CGI variable, a settings file can carry it, and a command-line run has no SCRIPT_NAME to test. Three other parts. copyLocalHubs skips a hub the cart already owns, so repeated edits reuse one file and one hub id rather than renumbering the hub on every drag; the test requires a plain file, because saveTrackFile leaves the trash name behind as a symbolic link to the durable copy and writing through that link would rewrite the saved session's own file. copyLocalHubs also screens the path with isServerUserFilePath before opening it, the way getHubName does, and selects on customComposite- with the dash so that the names it acts on are the ones fileNameCartVarPrefixes screens. saveTrackFile copies when the source is a local hub outside trash that is not under this session's own directory, so loading one session and saving it under a new name gives the new session its own file instead of a reference into the first one's directory. pathIsUnderDir is no longer static in trashDir.c. All of it is behind the hg.conf gate collectionHubCopyOnWrite, default off, which reproduces the old behavior exactly. Retiring the gate is not uniform: the body of cartCopyLocalHubsOnSessionLoad is the old behavior and that function and its five callers go away with the gate, while the other three tests lose only the gate term. Measured on hgcentraltest with one binary. With the gate off, five loads of a session carrying a collection add five hubStatus rows and three later edits add none; with it on, the five loads add none and the first edit adds one. Saving a loaded session costs two rows off and one on. The resulting hub file is byte identical either way apart from the hub id, and the file the saved session names is unchanged by md5 and mtime, including after an edit made straight after a save, and including when the cart names a trash symbolic link into session storage. refs #38273 diff --git src/hg/lib/trashDir.c src/hg/lib/trashDir.c index 817fa0749ec..9a95956fbbb 100644 --- src/hg/lib/trashDir.c +++ src/hg/lib/trashDir.c @@ -1,195 +1,195 @@ /* trashDir.c - temporary file creation and directory creation in /trash */ /* Copyright (C) 2014 The Regents of the University of California * See kent/LICENSE or http://genome.ucsc.edu/license/ for licensing information. */ #include "common.h" #include "hash.h" #include "hgConfig.h" #include "portable.h" #include "trashDir.h" static boolean hasDotDotComponent(char *path) /* Return TRUE if any '/'-separated component of path is exactly "..", which is the only * way a path can climb back above a directory it appears to be inside of. */ { char *s = path; while (s != NULL && s[0] != '\0') { if (s[0] == '.' && s[1] == '.' && (s[2] == '/' || s[2] == '\0')) return TRUE; s = strchr(s, '/'); if (s != NULL) s += 1; } return FALSE; } -static boolean pathIsUnderDir(char *dir, char *path) +boolean pathIsUnderDir(char *dir, char *path) /* Return TRUE if path names something underneath dir. A '/' is required at the directory * boundary, so a sibling directory whose name merely starts the same way (trashBackup next * to trash) does not match. ".." below the boundary is refused. */ { if (isEmpty(dir) || isEmpty(path)) return FALSE; int dirLen = strlen(dir); while (dirLen > 0 && dir[dirLen-1] == '/') dirLen -= 1; if (dirLen == 0) return FALSE; if (strncmp(path, dir, dirLen) != 0 || path[dirLen] != '/' || path[dirLen+1] == '\0') return FALSE; return !hasDotDotComponent(path + dirLen + 1); } boolean isTrashPath(char *path) /* Return TRUE if path names a file inside the trash directory. */ { return pathIsUnderDir(trashDir(), path); } boolean isTrashOrSessionDataPath(char *path) /* Return TRUE if path is under the trash directory, or under one of the durable session-data * directories that trash files are moved to when a session is saved. */ { return isTrashPath(path) || pathIsUnderDir(cfgOption("sessionDataDir"), path) || pathIsUnderDir(cfgOption("sessionDataDirOld"), path); } boolean isServerUserFilePath(char *path) /* Return TRUE if path is under one of the directories where the server keeps files it made * for a user: the trash directory, the session-data directories, or a per-feature data * directory such as myVariantsDataDir. */ { return isTrashOrSessionDataPath(path) || pathIsUnderDir(cfgOption("myVariantsDataDir"), path); } boolean isRemoteUrl(char *path) /* Return TRUE if path is a URL fetched over the network rather than a file name. Only the * three protocols the tree actually fetches count; hasProtocol() in net.c is a test for * "://" anywhere in the string, which is too loose to decide anything on. */ { return startsWith("http://", path) || startsWith("https://", path) || startsWith("ftp://", path); } boolean isServerUserFileOrUrl(char *path) /* Return TRUE if path is either a remote URL or a file the server made for a user. * * A few cart variables legitimately hold either one: the user gives hgTracks a URL for the * multi-region BED or pastes the BED itself, and hgSession loads settings from a URL. The * code then decides which it has by looking for a protocol, and treats anything else as a * local file name, so "no protocol" has to mean "one of ours" or the local-file branch reads * whatever the cart says. */ { return isRemoteUrl(path) || isServerUserFilePath(path); } static void trashDirFileExt(struct tempName *tn, char *dirName, char *base, char *suffix, boolean addDate) /* obtain a trash file name trash/dirName/base*.suffix */ { static struct hash *dirHash = NULL; char prefix[128]; char buffer[4096]; if (! dirHash) dirHash = newHash(0); /* already created this directory ? */ if (! hashLookup(dirHash,dirName)) { hashAddInt(dirHash, dirName, 1); /* remember, been here, done that */ mkdirTrashDirectory(dirName); } if (addDate) { safef(buffer, sizeof buffer, "%s/%03d", dirName, dayOfYear()); dirName = buffer; if (! hashLookup(dirHash,dirName)) { hashAddInt(dirHash, dirName, 1); /* remember, been here, done that */ mkdirTrashDirectory(dirName); } } /* no need to duplicate the _ at the end of base, makeTempName is going * to add _ to the given base, some CGIs pass "base_" */ if (endsWith(base,"_")) { char *t = cloneString(base); int len = strlen(t); t[len-1] = '\0'; /* remove ending _ */ safef(prefix, sizeof(prefix), "%s/%s", dirName,t); freeMem(t); } else safef(prefix, sizeof(prefix), "%s/%s", dirName,base); makeTempName(tn, prefix, suffix); } void trashDirFile(struct tempName *tn, char *dirName, char *base, char *suffix) /* obtain a trash file name trash/dirName/base*.suffix */ { trashDirFileExt(tn, dirName, base, suffix, FALSE); } void trashDirDateFile(struct tempName *tn, char *dirName, char *base, char *suffix) /* obtain a trash file name trash/dirName.dayOfYear/base*.suffix */ { trashDirFileExt(tn, dirName, base, suffix, TRUE); } boolean trashDirReusableFile(struct tempName *tn, char *dirName, char *base, char *suffix) /* obtain a resusable trash file name as trash/dirName/base.suffix * returns TRUE if already exists. */ { trashDirFile(tn,dirName,base,suffix); // Don't really want the randomized name. char *cgiName = rStringIn("/",tn->forCgi ); char *htmlName = rStringIn("/",tn->forHtml); if (cgiName == NULL) cgiName = rStringIn("\\",tn->forCgi); assert(cgiName != NULL && htmlName != NULL); cgiName += 1; htmlName += 1; boolean addDot = (*suffix != '.'); safef(cgiName, strlen(cgiName), "%s%s%s", base, (addDot?".":""),suffix);// There is room, since safef(htmlName,strlen(htmlName),"%s%s%s", base, (addDot?".":""),suffix);// tempName: base_*.suffix // exists? return fileExists(tn->forCgi); } void copyFileToTrash(char **pFileName, char *dirName, char *base, char *suffix) /* If *pFileName is not NULL and exists, then create a new file in the * given dirName of trash/ with the given base and suffix, copy *pFileName's * contents to it, and set *pFileName to the new filename. */ { if (pFileName != NULL && *pFileName != NULL) { if (fileExists(*pFileName)) { FILE *fIn = mustOpen(*pFileName, "r"); struct tempName tn; trashDirFile(&tn, dirName, base, suffix); char *newFileName = tn.forCgi; FILE *fOut = mustOpen(newFileName, "w"); unsigned char buf[16 * 1024]; size_t sz; while ((sz = fread(buf, sizeof(buf[0]), ArraySize(buf), fIn)) > 0) fwrite(buf, sizeof(buf[0]), sz, fOut); fclose(fOut); fclose(fIn); *pFileName = cloneString(newFileName); } } }