Add the ability to specify a alternative temporary file directory using the
"sqlite_temp_directory" global variable. (CVS 1885)

FossilOrigin-Name: fce56ba6a3c53843fabdfad4f545e35a83a01aa9
diff --git a/src/os_unix.c b/src/os_unix.c
index 4d60391..11e7182 100644
--- a/src/os_unix.c
+++ b/src/os_unix.c
@@ -569,11 +569,19 @@
 }
 
 /*
+** If the following global variable points to a string which is the
+** name of a directory, then that directory will be used to store
+** temporary files.
+*/
+const char *sqlite_temp_directory = 0;
+
+/*
 ** Create a temporary file name in zBuf.  zBuf must be big enough to
 ** hold at least SQLITE_TEMPNAME_SIZE characters.
 */
 int sqlite3OsTempFileName(char *zBuf){
   static const char *azDirs[] = {
+     0,
      "/var/tmp",
      "/usr/tmp",
      "/tmp",
@@ -586,7 +594,9 @@
   int i, j;
   struct stat buf;
   const char *zDir = ".";
+  azDirs[0] = sqlite_temp_directory;
   for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){
+    if( azDirs[i]==0 ) continue;
     if( stat(azDirs[i], &buf) ) continue;
     if( !S_ISDIR(buf.st_mode) ) continue;
     if( access(azDirs[i], 07) ) continue;
diff --git a/src/sqlite.h.in b/src/sqlite.h.in
index f4bf1ed..bb7bc9d 100644
--- a/src/sqlite.h.in
+++ b/src/sqlite.h.in
@@ -12,7 +12,7 @@
 ** This header file defines the interface that the SQLite library
 ** presents to client programs.
 **
-** @(#) $Id: sqlite.h.in,v 1.112 2004/08/01 00:10:45 drh Exp $
+** @(#) $Id: sqlite.h.in,v 1.113 2004/08/14 17:10:12 drh Exp $
 */
 #ifndef _SQLITE_H_
 #define _SQLITE_H_
@@ -1115,6 +1115,19 @@
   const void *pKey, int nKey     /* The new key */
 );
 
+/*
+** If the following global variable is made to point to a constant
+** string which is the name of a directory, then all temporary files
+** created by SQLite will be placed in that directory.  If this variable
+** is NULL pointer, then SQLite does a search for an appropriate temporary
+** file directory.
+**
+** This variable should only be changed when there are no open databases.
+** Once sqlite3_open() has been called, this variable should not be changed
+** until all database connections are closed.
+*/
+extern const char *sqlite_temp_directory;
+
 #ifdef __cplusplus
 }  /* End of the 'extern "C"' block */
 #endif
diff --git a/src/test1.c b/src/test1.c
index 5fc284c..3df370c 100644
--- a/src/test1.c
+++ b/src/test1.c
@@ -13,7 +13,7 @@
 ** is not included in the SQLite library.  It is used for automated
 ** testing of the SQLite library.
 **
-** $Id: test1.c,v 1.96 2004/07/26 12:24:23 drh Exp $
+** $Id: test1.c,v 1.97 2004/08/14 17:10:12 drh Exp $
 */
 #include "sqliteInt.h"
 #include "tcl.h"
@@ -2339,6 +2339,26 @@
   return TCL_OK;
 }
 
+/*
+** Usage:  sqlite3OsTempFileName
+*/
+static int test_sqlite3OsTempFileName(
+  void * clientData,
+  Tcl_Interp *interp,
+  int objc,
+  Tcl_Obj *CONST objv[]
+){
+  char zFile[SQLITE_TEMPNAME_SIZE];
+  int rc;
+
+  rc = sqlite3OsTempFileName(zFile);
+  if( rc!=SQLITE_OK ){
+    Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
+    return TCL_ERROR;
+  }
+  Tcl_AppendResult(interp, zFile, 0);
+  return TCL_OK;
+}
 
 /*
 ** Register commands with the TCL interpreter.
@@ -2424,6 +2444,7 @@
      { "sqlite3OsOpenReadWrite",test_sqlite3OsOpenReadWrite, 0 },
      { "sqlite3OsClose",        test_sqlite3OsClose, 0 },
      { "sqlite3OsLock",         test_sqlite3OsLock, 0 },
+     { "sqlite3OsTempFileName", test_sqlite3OsTempFileName, 0 },
    
      /* Custom test interfaces */
      { "sqlite3OsUnlock",         test_sqlite3OsUnlock, 0    },
@@ -2456,5 +2477,7 @@
       (char*)&sqlite3_os_trace, TCL_LINK_INT);
   Tcl_LinkVar(interp, "sqlite_static_bind_value",
       (char*)&sqlite_static_bind_value, TCL_LINK_STRING);
+  Tcl_LinkVar(interp, "sqlite_temp_directory",
+      (char*)&sqlite_temp_directory, TCL_LINK_STRING);
   return TCL_OK;
 }