Fossil SCM

Further improvements to fossil_temp_filename() to help ensure that the correct directory separators are used on both unix and windows.

drh 2021-08-28 16:36 trunk
Commit 5434d2e32ce5f19cbca8980dc6d5fb012074f7f6a21904f2a92b59291d9cf471
1 file changed +9 -1
+9 -1
--- src/util.c
+++ src/util.c
@@ -649,10 +649,13 @@
649649
** freed by the caller.
650650
*/
651651
char *fossil_temp_filename(void){
652652
char *zTFile = 0;
653653
const char *zDir;
654
+ char cDirSep;
655
+ char zSep[2];
656
+ size_t nDir;
654657
u64 r[2];
655658
int i;
656659
#ifdef _WIN32
657660
char zTempDir[1000];
658661
#else
@@ -663,10 +666,11 @@
663666
if( zTFile ) return zTFile;
664667
}
665668
sqlite3_randomness(sizeof(r), &r);
666669
#if _WIN32
667670
zTempDir[0] = 0;
671
+ cDirSep = '\\';
668672
GetTempPathA(sizeof(zTempDir), zTempDir);
669673
if( zTempDir[0] ){
670674
zDir = zTempDir;
671675
}else{
672676
zDir = fossil_getenv("LOCALAPPDATA");
@@ -679,12 +683,16 @@
679683
if( stat(zDir,&buf)==0 && S_ISDIR(buf.st_mode) && access(zDir,03)==0 ){
680684
break;
681685
}
682686
}
683687
if( i>=sizeof(azTmp)/sizeof(azTmp[0]) ) zDir = ".";
688
+ cDirSep = '/';
684689
#endif
685
- return sqlite3_mprintf("%s/fossil%016llx%016llx", zDir, r[0], r[1]);
690
+ nDir = strlen(zDir);
691
+ zSep[1] = 0;
692
+ zSep[0] = (nDir && zDir[nDir-1]==cDirSep) ? 0 : cDirSep;
693
+ return sqlite3_mprintf("%s%sfossil%016llx%016llx", zDir,zSep,r[0],r[1]);
686694
}
687695
688696
/*
689697
** Turn memory limits for stack and heap on and off. The argument
690698
** is true to turn memory limits on and false to turn them off.
691699
--- src/util.c
+++ src/util.c
@@ -649,10 +649,13 @@
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
@@ -663,10 +666,11 @@
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");
@@ -679,12 +683,16 @@
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
--- src/util.c
+++ src/util.c
@@ -649,10 +649,13 @@
649 ** freed by the caller.
650 */
651 char *fossil_temp_filename(void){
652 char *zTFile = 0;
653 const char *zDir;
654 char cDirSep;
655 char zSep[2];
656 size_t nDir;
657 u64 r[2];
658 int i;
659 #ifdef _WIN32
660 char zTempDir[1000];
661 #else
@@ -663,10 +666,11 @@
666 if( zTFile ) return zTFile;
667 }
668 sqlite3_randomness(sizeof(r), &r);
669 #if _WIN32
670 zTempDir[0] = 0;
671 cDirSep = '\\';
672 GetTempPathA(sizeof(zTempDir), zTempDir);
673 if( zTempDir[0] ){
674 zDir = zTempDir;
675 }else{
676 zDir = fossil_getenv("LOCALAPPDATA");
@@ -679,12 +683,16 @@
683 if( stat(zDir,&buf)==0 && S_ISDIR(buf.st_mode) && access(zDir,03)==0 ){
684 break;
685 }
686 }
687 if( i>=sizeof(azTmp)/sizeof(azTmp[0]) ) zDir = ".";
688 cDirSep = '/';
689 #endif
690 nDir = strlen(zDir);
691 zSep[1] = 0;
692 zSep[0] = (nDir && zDir[nDir-1]==cDirSep) ? 0 : cDirSep;
693 return sqlite3_mprintf("%s%sfossil%016llx%016llx", zDir,zSep,r[0],r[1]);
694 }
695
696 /*
697 ** Turn memory limits for stack and heap on and off. The argument
698 ** is true to turn memory limits on and false to turn them off.
699

Keyboard Shortcuts

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