55de7ef426a462247f89f48afd9aa6a2eb2cdd19 chmalee Thu Jan 16 17:11:17 2025 -0800 Remove a bunch of dead experimental hubspace code, and make sure to cgi-encode hub names, refs #35018 diff --git src/hg/lib/userdata.c src/hg/lib/userdata.c index 5cfe613..d62bd66 100644 --- src/hg/lib/userdata.c +++ src/hg/lib/userdata.c @@ -64,59 +64,60 @@ /* Strips the getDataDir(userName) off of fname */ { char *dataDir = getDataDir(userName); int prefixSize = strlen(dataDir); if (startsWith(dataDir, fname)) { char *ret = fname + prefixSize; return ret; } return NULL; } char *getHubDataDir(char *userName, char *hub) { char *dataDir = getDataDir(userName); -return catTwoStrings(dataDir, hub); +return catTwoStrings(dataDir, cgiEncode(hub)); } char *hubSpaceUrl = NULL; static char *getHubSpaceUrl() { if (!hubSpaceUrl) hubSpaceUrl = cfgOption("hubSpaceUrl"); return hubSpaceUrl; } char *webDataDir(char *userName) /* Return a web accesible path to the userDataDir, this is different from the full path tusd uses */ { char *retUrl = NULL; if (userName) { char *encUserName = cgiEncode(userName); char *userPrefix = md5HexForString(encUserName); userPrefix[2] = '\0'; struct dyString *userDirDy = dyStringNew(0); dyStringPrintf(userDirDy, "%s/%s/%s/", getHubSpaceUrl(), userPrefix, encUserName); retUrl = dyStringCannibalize(&userDirDy); } return retUrl; } char *prefixUserFile(char *userName, char *fname, char *parentDir) -/* Allocate a new string that contains the full per-user path to fname, NULL otherwise. +/* Allocate a new string that contains the full per-user path to fname. return NULL if + * we cannot construct a full path because of a realpath(3) failure. * parentDir is optional and will go in between the per-user dir and the fname */ { char *pathPrefix = getDataDir(userName); char *path = NULL; if (pathPrefix) { if (parentDir) { struct dyString *ret = dyStringCreate("%s%s%s%s", pathPrefix, parentDir, lastChar(parentDir) == '/' ? "" : "/", fname); path = dyStringCannibalize(&ret); } else path = catTwoStrings(pathPrefix, fname); char canonicalPath[PATH_MAX]; realpath(path, canonicalPath); @@ -130,32 +131,30 @@ static boolean checkHubSpaceRowExists(struct hubSpace *row) /* Return TRUE if row already exists */ { struct sqlConnection *conn = hConnectCentral(); struct dyString *queryCheck = sqlDyStringCreate("select count(*) from hubSpace where userName='%s' and fileName='%s' and parentDir='%s'", row->userName, row->fileName, row->parentDir); int ret = sqlQuickNum(conn, dyStringCannibalize(&queryCheck)); hDisconnectCentral(&conn); return ret > 0; } char *hubNameFromPath(char *path) /* Return the last directory component of path. Assume that a '.' char in the last component * means that component is a filename and go back further */ { -fprintf(stderr, "hubNameFromPath('%s')\n", path); -fflush(stderr); char *copy = cloneString(path); if (endsWith(copy, "/")) trimLastChar(copy); char *ptr = strrchr(copy, '/'); // check to see if we're in a file name, like /blah/blah/name/hub.txt if (ptr) { if (strchr(ptr, '.')) { *ptr = 0; ptr = strrchr(copy, '/'); } if (ptr) { ++ptr; @@ -335,114 +334,31 @@ } void removeFileForUser(char *fname, char *userName) /* Remove a file for this user if it exists */ { // The file to remove must be prefixed by the hg.conf userDataDir if (!startsWith(getDataDir(userName), fname)) return; if (fileExists(fname)) { // delete the actual file mustRemove(fname); // delete the table row deleteHubSpaceRow(fname, userName); } -} - -void removeHubForUser(char *path, char *userName) -/* Remove a hub directory for this user (and all files in the directory), if it exists */ -{ -if (!startsWith(getDataDir(userName), path)) - return; -if (isDirectory(path)) - { - struct fileInfo *f, *flist = listDirX(path, NULL, TRUE); - for (f = flist; f != NULL; f = f->next) - mustRemove(f->name); - // now we have deleted all the files in the dir we can safely rmdir - mustRemove(path); - deleteHubSpaceRow(path, userName); - } -} - -static time_t getFileListLatestTime(struct userFiles *userFiles) -/* Return the greatest last access time of the files in userFiles->fileList */ -{ -if (!userFiles->fileList) - errAbort("no files in userFiles->fileList"); -time_t modTime = 0; -struct fileInfo *f; -for (f = userFiles->fileList; f != NULL; f = f->next) - { - if (f->lastAccess > modTime) - { - modTime = f->lastAccess; - } - } -return modTime; -} - -time_t getHubLatestTime(struct userHubs *hub) -/* Return the latest access time of the files in a hub */ -{ -// NOTE: every hub is guaranteed to have at least one file -return getFileListLatestTime(hub->fileList); -} - -char *findParentDirs(char *parentDir, char *userName, char *fname) -/* For a given file with parentDir, go up the tree and find the full path back to - * the rootmost parentDir */ -{ -return NULL; -} - -struct userFiles *listFilesForUserHub(char *userName, char *hubName) -/* Get all the files for a particular hub for a particular user */ -{ -struct userFiles *userListing; -AllocVar(userListing); -char *path = getHubDataDir(userName, hubName); -struct fileInfo *fiList = listDirX(path,NULL,FALSE); -userListing->userName = userName; -userListing->fileList = fiList; -return userListing; -} - -struct userHubs *listHubsForUser(char *userName) -/* Lists the directories for a particular user */ -{ -struct userHubs *userHubs = NULL; -char *path = getDataDir(userName); -struct fileInfo *fi, *fiList = listDirX(path,NULL,FALSE); -for (fi = fiList; fi != NULL; fi = fi->next) - { - if (fi->isDir) - { - struct userHubs *hub; - AllocVar(hub); - hub->hubName = cloneString(fi->name); - hub->userName = cloneString(userName); - char hubPath[PATH_LEN]; - safef(hubPath, sizeof(hubPath), "%s%s", path, fi->name); - struct userFiles *hubFileList = listFilesForUserHub(userName, hub->hubName); - hub->lastModified = getFileListLatestTime(hubFileList); - hub->fileList = hubFileList; - slAddHead(&userHubs, hub); - } - } -return userHubs; +// TODO: we should also modify the hub.txt associated with this file } struct hubSpace *listFilesForUser(char *userName) /* Return the files the user has uploaded */ { struct sqlConnection *conn = hConnectCentral(); struct dyString *query = sqlDyStringCreate("select userName, fileName, fileSize, fileType, creationTime, DATE_FORMAT(lastModified, '%%c/%%d/%%Y, %%l:%%i:%%s %%p') as lastModified, db, location, md5sum, parentDir from hubSpace where userName='%s' order by location,creationTime", userName); struct hubSpace *fileList = hubSpaceLoadByQuery(conn, dyStringCannibalize(&query)); hDisconnectCentral(&conn); return fileList; } #define defaultHubName "defaultHub" char *defaultHubNameForUser(char *userName) /* Return a name to use as a default for a hub, starts with defaultHub, then defaultHub2, ... */ @@ -481,36 +397,15 @@ long long specialQuota = quotaForUserName(userName); return specialQuota == 0 ? HUB_SPACE_DEFAULT_QUOTA : specialQuota; } long long checkUserQuota(char *userName) /* Return the amount of space a user is currently using */ { long long quota = 0; struct hubSpace *hubSpace, *hubSpaceList = listFilesForUser(userName); for (hubSpace = hubSpaceList; hubSpace != NULL; hubSpace = hubSpace->next) { quota += hubSpace->fileSize; } return quota; } - -char *storeUserFile(char *userName, char *newFileName, void *data, size_t dataSize) -/* Give a fileName and a data stream, write the data to: - * userDataDir/hashedUserName/userName/fileName - * where userDataDir comes from hg.conf and - * hashedUserName is based on the md5sum of the userName - * to prevent proliferation of too many directories. - * - * After sucessfully saving the file, return a web accessible url - * to the file. */ -{ -char *userDir = getDataDir(userName); -makeDirsOnPath(userDir); -char *pathToFile = catTwoStrings(userDir, newFileName); -FILE *newFile = mustOpen(pathToFile, "wb"); -// the data will start with a line feed so get rid of that -mustWrite(newFile, data, dataSize); -// missing an EOF? -carefulClose(&newFile); -return pathToFile; -}