6ee1cfdfc702cd47e66ff9ebd6ef28e59304cf85
galt
  Sat Aug 16 02:19:41 2014 -0700
for hgsql and related utils, try to remove old temp .hgsql.cnf-* files that have not been accessed for 30 days.
diff --git src/hg/lib/sqlProg.c src/hg/lib/sqlProg.c
index a328153..e974c37 100644
--- src/hg/lib/sqlProg.c
+++ src/hg/lib/sqlProg.c
@@ -1,24 +1,25 @@
 /* sqlProg - functions for building command line programs to deal with
  * sql databases.*/
 
 /* Copyright (C) 2014 The Regents of the University of California 
  * See README in this or parent directory for licensing information. */
 #include "common.h"
 #include "sqlProg.h"
 #include "hgConfig.h"
 #include "obscure.h"
+#include "portable.h"
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <signal.h>
 
 char *tempFileNameToRemove = NULL;
 int killChildPid = 0;
 
 /* trap signals to remove the temporary file and terminate the child process */
 
 static void sqlProgCatchSignal(int sigNum)
 /* handler for various terminal signals for removing the temp file */
 {
 
 if (tempFileNameToRemove)
     unlink(tempFileNameToRemove);
@@ -162,50 +163,74 @@
 sqlExecProgProfile("db", prog, progArgs, userArgc, userArgv);
 }
 
 
 void sqlExecProgLocal(char *prog, char **progArgs, int userArgc, char *userArgv[])
 /* 
  * Exec one of the sql programs using user and password defined in localDb.XXX variables from ~/.hg.conf 
  * progArgs is NULL-terminate array of program-specific arguments to add,
  * which maybe NULL. userArgv are arguments passed in from the command line.
  * The program is execvp-ed, this function does not return. 
  */
 {
 sqlExecProgProfile("localDb", prog, progArgs, userArgc, userArgv);
 }
 
+
+void nukeOldCnfs(char *homeDir)
+/* Remove .hgsql.cnf-* files older than a month */
+{
+struct fileInfo *file, *fileList = listDirX(homeDir, ".hgsql.cnf-*", FALSE);
+time_t now = time(0);
+for (file = fileList; file != NULL; file = file->next)
+    {
+    if (difftime(now, file->lastAccess) >  30 * 24 * 60 * 60)  // 30 days in seconds.
+	{
+	char homePath[256];
+	safef(homePath, sizeof homePath, "%s/%s", homeDir, file->name);
+	remove(homePath);
+	}
+    }
+
+}
+
 void sqlExecProgProfile(char *profile, char *prog, char **progArgs, int userArgc, char *userArgv[])
 /* 
  * Exec one of the sql programs using user and password defined in localDb.XXX variables from ~/.hg.conf 
  * progArgs is NULL-terminate array of program-specific arguments to add,
  * which maybe NULL. userArgv are arguments passed in from the command line.
  * The program is execvp-ed, this function does not return. 
  */
 {
 int i, j = 0, nargc=cntArgv(progArgs)+userArgc+6, defaultFileNo, returnStatus;
 pid_t child_id;
 char **nargv, defaultFileName[256], defaultFileArg[256], *homeDir;
 
 // install cleanup signal handlers
 sqlProgInitSigHandlers();
 
 /* Assemble defaults file */
 if ((homeDir = getenv("HOME")) == NULL)
     errAbort("sqlExecProgProfile: HOME is not defined in environment; cannot create temporary password file");
+
+nukeOldCnfs(homeDir);
+
 safef(defaultFileName, sizeof(defaultFileName), "%s/.hgsql.cnf-XXXXXX", homeDir);
 defaultFileNo=sqlMakeDefaultsFile(defaultFileName, profile, "client");
+
+swapForOldCnf(homeDir,  );
+
 safef(defaultFileArg, sizeof(defaultFileArg), "--defaults-file=%s", defaultFileName);
 
 AllocArray(nargv, nargc);
 
 nargv[j++] = prog;
 nargv[j++] = defaultFileArg;   /* --defaults-file must come before other options */
 if (progArgs != NULL)
     {
     for (i = 0; progArgs[i] != NULL; i++)
         nargv[j++] = progArgs[i];
     }
 for (i = 0; i < userArgc; i++)
     nargv[j++] = userArgv[i];
 nargv[j++] = NULL;