aboutsummaryrefslogtreecommitdiff
path: root/src/dbbe.c
diff options
context:
space:
mode:
authordrh <drh@noemail.net>2001-01-13 14:34:05 +0000
committerdrh <drh@noemail.net>2001-01-13 14:34:05 +0000
commitae85dc8b0b06ebbf86bcad4e4d7119a5741fbcec (patch)
treedc15f520521376992b889e96c8a79a3c22a976ef /src/dbbe.c
parent993c2a7c8f48ab2ac61a7eb63d8a3b30d6a937c5 (diff)
downloadsqlite-ae85dc8b0b06ebbf86bcad4e4d7119a5741fbcec.tar.gz
sqlite-ae85dc8b0b06ebbf86bcad4e4d7119a5741fbcec.zip
Changes to the DBBE. Moving toward having many more
backend driver choices. (CVS 176) FossilOrigin-Name: c0730217a04323a1a73d125e3e7da32bcc8d58fc
Diffstat (limited to 'src/dbbe.c')
-rw-r--r--src/dbbe.c89
1 files changed, 88 insertions, 1 deletions
diff --git a/src/dbbe.c b/src/dbbe.c
index 43948c1e1..907aa7380 100644
--- a/src/dbbe.c
+++ b/src/dbbe.c
@@ -30,7 +30,7 @@
** relatively simple to convert to a different database such
** as NDBM, SDBM, or BerkeleyDB.
**
-** $Id: dbbe.c,v 1.21 2000/10/19 14:10:09 drh Exp $
+** $Id: dbbe.c,v 1.22 2001/01/13 14:34:06 drh Exp $
*/
#include "sqliteInt.h"
@@ -62,3 +62,90 @@ Dbbe *sqliteDbbeOpen(
}
return sqliteGdbmOpen(zName, writeFlag, createFlag, pzErrMsg);
}
+
+/*
+** Open a temporary file. The file should be deleted when closed.
+**
+** Note that we can't use the old Unix trick of opening the file
+** and then immediately unlinking the file. That works great
+** under Unix, but fails when we try to port to Windows.
+*/
+int sqliteDbbeOpenTempFile(Dbbe *pBe, FILE **ppFile){
+ char *zFile; /* Full name of the temporary file */
+ char zBuf[50]; /* Base name of the temporary file */
+ int i; /* Loop counter */
+ int limit; /* Prevent an infinite loop */
+ int rc = SQLITE_OK; /* Value returned by this function */
+ char *zDir; /* Directory to hold the file */
+
+ for(i=0; i<pBe->nTemp; i++){
+ if( pBe->apTemp[i]==0 ) break;
+ }
+ if( i>=pBe->nTemp ){
+ pBe->nTemp++;
+ pBe->apTemp = sqliteRealloc(pBe->apTemp, pBe->nTemp*sizeof(FILE*) );
+ pBe->azTemp = sqliteRealloc(pBe->azTemp, pBe->nTemp*sizeof(char*) );
+ }
+ if( pBe->apTemp==0 ){
+ *ppFile = 0;
+ return SQLITE_NOMEM;
+ }
+ limit = 4;
+ zFile = 0;
+ zDir = pBe->zDir;
+ if( zDir==0 ){
+ zDir = "./";
+ }
+ do{
+ sqliteRandomName(zBuf, "/_temp_file_");
+ sqliteFree(zFile);
+ zFile = 0;
+ sqliteSetString(&zFile, zDir, zBuf, 0);
+ }while( access(zFile,0)==0 && limit-- >= 0 );
+ *ppFile = pBe->apTemp[i] = fopen(zFile, "w+");
+ if( pBe->apTemp[i]==0 ){
+ rc = SQLITE_ERROR;
+ sqliteFree(zFile);
+ pBe->azTemp[i] = 0;
+ }else{
+ pBe->azTemp[i] = zFile;
+ }
+ return rc;
+}
+
+/*
+** Close a temporary file opened using sqliteGdbmOpenTempFile()
+*/
+void sqliteDbbeCloseTempFile(Dbbe *pBe, FILE *f){
+ int i;
+ for(i=0; i<pBe->nTemp; i++){
+ if( pBe->apTemp[i]==f ){
+ unlink(pBe->azTemp[i]);
+ sqliteFree(pBe->azTemp[i]);
+ pBe->apTemp[i] = 0;
+ pBe->azTemp[i] = 0;
+ break;
+ }
+ }
+ fclose(f);
+}
+
+/*
+** Close all temporary files that happen to still be open.
+** This routine is called when the database is being closed.
+*/
+void sqliteDbbeCloseAllTempFiles(Dbbe *pBe){
+ int i;
+ for(i=0; i<pBe->nTemp; i++){
+ if( pBe->apTemp[i]!=0 ){
+ unlink(pBe->azTemp[i]);
+ fclose(pBe->apTemp[i]);
+ sqliteFree(pBe->azTemp[i]);
+ pBe->apTemp[i] = 0;
+ pBe->azTemp[i] = 0;
+ break;
+ }
+ }
+ sqliteFree(pBe->azTemp);
+ sqliteFree(pBe->apTemp);
+}