c1f214b4ddd63132bbbc2e12e55d37b2fe20a3a0
chmalee
  Thu Apr 17 16:04:24 2025 -0700
When a user hits an error during upload, we need to remove the temp file created by tusd to allow the user to re-try the upload again, refs Max zoom

diff --git src/hg/hgHubConnect/hooks/pre-finish.c src/hg/hgHubConnect/hooks/pre-finish.c
index b988ad22454..aaadfca0812 100644
--- src/hg/hgHubConnect/hooks/pre-finish.c
+++ src/hg/hgHubConnect/hooks/pre-finish.c
@@ -43,30 +43,31 @@
  * errors that the user needs to see, they need to be in the JSON response */
 {
 // TODO: create response object and do all error catching through that
 char *reqId = getenv("TUS_ID");
 // always return an exit status to the daemon and print to stdout, as
 // stdout gets sent by the daemon back to the client
 int exitStatus = 0;
 struct jsonElement *response = makeDefaultResponse();
 if (!(reqId))
     {
     rejectUpload(response, "not a TUS request");
     exitStatus = 1;
     }
 else
     {
+    char *tusFile = NULL, *tusInfo = NULL;
     struct errCatch *errCatch = errCatchNew(0);
     if (errCatchStart(errCatch))
         {
         // the variables for the row entry for this file, some can be NULL
         char *userName = NULL;
         char *dataDir = NULL, *userDataDir = NULL;
         char *fileName = NULL;
         long long fileSize = 0;
         char *fileType = NULL;
         char *db = NULL;
         char *location = NULL;
         char *reqLm = NULL;
         time_t lastModified = 0;
         boolean isHubToolsUpload = FALSE;
         char *parentDir = NULL, *encodedParentDir = NULL;
@@ -99,42 +100,42 @@
         db = jsonQueryString(req, "", "Event.Upload.MetaData.genome", NULL);
         reqLm = jsonQueryString(req, "", "Event.Upload.MetaData.lastModified", NULL);
         lastModified = sqlLongLong(reqLm) / 1000; // yes Javascript dates are in millis
         char *hubtoolsStr = jsonQueryString(req, "", "Event.Upload.MetaData.hubtools", NULL);
         if (hubtoolsStr)
             isHubToolsUpload = sameString(hubtoolsStr, "TRUE") || sameString(hubtoolsStr, "true");
         parentDir = jsonQueryString(req, "", "Event.Upload.MetaData.parentDir", NULL);
         fprintf(stderr, "parentDir = '%s'\n", parentDir);
         fflush(stderr);
         // strip out plain leading '.' and '/' components
         // middle '.' components are dealt with later
         if (startsWith("./", parentDir) || startsWith("/", parentDir))
             parentDir = skipBeyondDelimit(parentDir, '/');
         fprintf(stderr, "parentDir = '%s'\n", parentDir);
         fflush(stderr);
-        char *tusFile = jsonQueryString(req, "", "Event.Upload.Storage.Path", NULL);
+        tusFile = jsonQueryString(req, "", "Event.Upload.Storage.Path", NULL);
         if (fileName == NULL)
             {
             errAbort("No Event.Upload.fileName setting");
             }
         else if (tusFile == NULL)
             {
             errAbort("No Event.Path setting");
             }
         else
             {
-            char *tusInfo = catTwoStrings(tusFile, ".info");
+            tusInfo = catTwoStrings(tusFile, ".info");
             userDataDir = dataDir = getDataDir(userName);
             struct dyString *newFile = dyStringNew(0);
             // if parentDir provided we are throwing the files in there
             if (parentDir)
                 {
                 encodedParentDir = encodePath(parentDir);
                 if (!endsWith(encodedParentDir, "/"))
                     encodedParentDir = catTwoStrings(encodedParentDir, "/");
                 dataDir = catTwoStrings(dataDir, encodedParentDir);
                 }
             dyStringPrintf(newFile, "%s%s", dataDir, fileName);
 
             fprintf(stderr, "moving %s to %s\n", tusFile, dyStringContents(newFile));
             // TODO: check if file exists or not and let user choose to overwrite
             // and re-call this hook, for now just exit if the file exists
@@ -190,30 +191,40 @@
                 createNewTempHubForUpload(reqId, row, userDataDir, encodedParentDir);
                 fprintf(stderr, "added hub.txt and hubSpace row for hub for file: '%s'\n", fileName);
                 fflush(stderr);
                 }
             // first make the parentDir rows
             makeParentDirRows(row->userName, sqlDateToUnixTime(row->lastModified), row->db, row->parentDir, userDataDir);
             row->parentDir = hubNameFromPath(encodedParentDir);
             addHubSpaceRowForFile(row);
             fprintf(stderr, "added hubSpace row for file '%s'\n", fileName);
             fflush(stderr);
             }
         }
     if (errCatch->gotError)
         {
         rejectUpload(response, errCatch->message->string);
+        // must remove the tusd temp files so if the users tries again after a temp error
+        // the upload will work
+        if (tusFile)
+            {
+            mustRemove(tusFile);
+            mustRemove(tusInfo);
+            }
+        // TODO: if the first mysql request in createNewTempHubForUpload() works but then
+        // either of makeParentDirRows() or addHubSpaceRowForFile() fails, we need to also
+        // drop any rows we may have added because the upload didn't full go through
         exitStatus = 1;
         }
     errCatchEnd(errCatch);
     }
 // always print a response no matter what
 jsonPrintToFile(response, NULL, stdout, 0);
 return exitStatus;
 }
 
 int main(int argc, char *argv[])
 /* Process command line. */
 {
 optionInit(&argc, argv, options);
 if (argc != 1)
     usage();