Fossil SCM

More robust implementation of fossil_temp_filename that does not require an open connection to a real database.

drh 2021-08-28 15:46 trunk
Commit 939696667aacc235615c3e08aaf0e5a4f669bf421b957a77513bc9e894b19296
1 file changed +32 -6
+32 -6
--- src/util.c
+++ src/util.c
@@ -31,10 +31,12 @@
3131
#ifdef _WIN32
3232
# include <windows.h>
3333
#else
3434
# include <sys/time.h>
3535
# include <sys/resource.h>
36
+# include <sys/types.h>
37
+# include <sys/stat.h>
3638
# include <unistd.h>
3739
# include <fcntl.h>
3840
# include <errno.h>
3941
#endif
4042
@@ -646,19 +648,43 @@
646648
** The returned string is obtained from sqlite3_malloc() and must be
647649
** freed by the caller.
648650
*/
649651
char *fossil_temp_filename(void){
650652
char *zTFile = 0;
651
- sqlite3 *db;
653
+ const char *zDir;
654
+ u64 r[2];
655
+ int i;
656
+#ifdef _WIN32
657
+ char zTempDir[1000];
658
+#else
659
+ static const char *azTmp[] = {"/var/tmp","/usr/tmp","/tmp"};
660
+#endif
652661
if( g.db ){
653
- db = g.db;
662
+ sqlite3_file_control(g.db, 0, SQLITE_FCNTL_TEMPFILENAME, (void*)&zTFile);
663
+ if( zTFile ) return zTFile;
664
+ }
665
+ sqlite3_randomness(sizeof(r), &r);
666
+#if _WIN32
667
+ zTempDir[0] = 0;
668
+ GetTempPathA(sizeof(zTempDir), zTempDir);
669
+ if( zTempDir[0] ){
670
+ zDir = zTempDir;
654671
}else{
655
- sqlite3_open("",&db);
672
+ zDir = fossil_getenv("LOCALAPPDATA");
673
+ if( zDir==0 ) zDir = ".";
674
+ }
675
+#else
676
+ for(i=0; i<sizeof(azTmp)/sizeof(azTmp[0]); i++){
677
+ struct stat buf;
678
+ zDir = azTmp[i];
679
+ if( stat(zDir,&buf)==0 && S_ISDIR(buf.st_mode) && access(zDir,03)==0 ){
680
+ break;
681
+ }
656682
}
657
- sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, (void*)&zTFile);
658
- if( g.db==0 ) sqlite3_close(db);
659
- return zTFile;
683
+ if( i>=sizeof(azTmp)/sizeof(azTmp[0]) ) zDir = ".";
684
+#endif
685
+ return sqlite3_mprintf("%s/fossil%016llx%016llx", zDir, r[0], r[1]);
660686
}
661687
662688
/*
663689
** Turn memory limits for stack and heap on and off. The argument
664690
** is true to turn memory limits on and false to turn them off.
665691
--- src/util.c
+++ src/util.c
@@ -31,10 +31,12 @@
31 #ifdef _WIN32
32 # include <windows.h>
33 #else
34 # include <sys/time.h>
35 # include <sys/resource.h>
 
 
36 # include <unistd.h>
37 # include <fcntl.h>
38 # include <errno.h>
39 #endif
40
@@ -646,19 +648,43 @@
646 ** The returned string is obtained from sqlite3_malloc() and must be
647 ** freed by the caller.
648 */
649 char *fossil_temp_filename(void){
650 char *zTFile = 0;
651 sqlite3 *db;
 
 
 
 
 
 
 
652 if( g.db ){
653 db = g.db;
 
 
 
 
 
 
 
 
654 }else{
655 sqlite3_open("",&db);
 
 
 
 
 
 
 
 
 
656 }
657 sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, (void*)&zTFile);
658 if( g.db==0 ) sqlite3_close(db);
659 return zTFile;
660 }
661
662 /*
663 ** Turn memory limits for stack and heap on and off. The argument
664 ** is true to turn memory limits on and false to turn them off.
665
--- src/util.c
+++ src/util.c
@@ -31,10 +31,12 @@
31 #ifdef _WIN32
32 # include <windows.h>
33 #else
34 # include <sys/time.h>
35 # include <sys/resource.h>
36 # include <sys/types.h>
37 # include <sys/stat.h>
38 # include <unistd.h>
39 # include <fcntl.h>
40 # include <errno.h>
41 #endif
42
@@ -646,19 +648,43 @@
648 ** The returned string is obtained from sqlite3_malloc() and must be
649 ** freed by the caller.
650 */
651 char *fossil_temp_filename(void){
652 char *zTFile = 0;
653 const char *zDir;
654 u64 r[2];
655 int i;
656 #ifdef _WIN32
657 char zTempDir[1000];
658 #else
659 static const char *azTmp[] = {"/var/tmp","/usr/tmp","/tmp"};
660 #endif
661 if( g.db ){
662 sqlite3_file_control(g.db, 0, SQLITE_FCNTL_TEMPFILENAME, (void*)&zTFile);
663 if( zTFile ) return zTFile;
664 }
665 sqlite3_randomness(sizeof(r), &r);
666 #if _WIN32
667 zTempDir[0] = 0;
668 GetTempPathA(sizeof(zTempDir), zTempDir);
669 if( zTempDir[0] ){
670 zDir = zTempDir;
671 }else{
672 zDir = fossil_getenv("LOCALAPPDATA");
673 if( zDir==0 ) zDir = ".";
674 }
675 #else
676 for(i=0; i<sizeof(azTmp)/sizeof(azTmp[0]); i++){
677 struct stat buf;
678 zDir = azTmp[i];
679 if( stat(zDir,&buf)==0 && S_ISDIR(buf.st_mode) && access(zDir,03)==0 ){
680 break;
681 }
682 }
683 if( i>=sizeof(azTmp)/sizeof(azTmp[0]) ) zDir = ".";
684 #endif
685 return sqlite3_mprintf("%s/fossil%016llx%016llx", zDir, r[0], r[1]);
686 }
687
688 /*
689 ** Turn memory limits for stack and heap on and off. The argument
690 ** is true to turn memory limits on and false to turn them off.
691

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button