Skip to content

Commit 18a3a48

Browse files
author
drh
committed
Experimental changes to put sqlite3_temp_directory behind a mutex.
FossilOrigin-Name: 5ee3515fbb88bf1ae5f8b507844f82dcc429380b6ebeab9b09b52b25ee60a60d
1 parent 4413224 commit 18a3a48

File tree

6 files changed

+75
-25
lines changed

6 files changed

+75
-25
lines changed

manifest

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
C Defer\sdeleting\sa\stransient\sSELECT\sstatement\sassociated\swith\sa\sflattening\nof\sone\sarm\sof\sa\scompound\sSELECT\suntil\safter\sthe\sparse\shas\scompleted.
2-
D 2022-09-01T13:51:09.528
1+
C Experimental\schanges\sto\sput\ssqlite3_temp_directory\sbehind\sa\smutex.
2+
D 2022-09-02T00:36:16.247
33
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
44
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
55
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -572,16 +572,16 @@ F src/os.c 0eb831ba3575af5277e47f4edd14fdfc90025c67eb25ce5cda634518d308d4e9
572572
F src/os.h 1ff5ae51d339d0e30d8a9d814f4b8f8e448169304d83a7ed9db66a65732f3e63
573573
F src/os_common.h b2f4707a603e36811d9b1a13278bffd757857b85
574574
F src/os_setup.h 0dbaea40a7d36bf311613d31342e0b99e2536586
575-
F src/os_unix.c 81e24bcad127faf32da162a4bb06e7a4604bea76fe7b1ed5b01742e9eb320554
576-
F src/os_win.c a8ea80037e81127ca01959daa87387cc135f325c88dc745376c4f760de852a10
575+
F src/os_unix.c 102f7e5c5b59c18ea3dbc929dc3be8acb3afc0e0b6ad572e032335c9c27f44f1
576+
F src/os_win.c d9ebd8bfce17d8fe49a08a87804dfa644c8fb20e28bca817ab62350c6ee2d4a5
577577
F src/os_win.h 7b073010f1451abe501be30d12f6bc599824944a
578578
F src/pager.c c60031c483960660853dfecf14c8e830503baab1f638ac997f0144f1bd3e1781
579579
F src/pager.h f82e9844166e1585f5786837ddc7709966138ced17f568c16af7ccf946c2baa3
580580
F src/parse.y 8e67d820030d2655b9942ffe61c1e7e6b96cea2f2f72183533299393907d0564
581581
F src/pcache.c 5a64e084260560910d9a61bc0e760394fa88aaa22201477ab3e49e278db92edb
582582
F src/pcache.h 4f87acd914cef5016fae3030343540d75f5b85a1877eed1a2a19b9f284248586
583583
F src/pcache1.c 5cde56d6bb057b0d7420f89bebd3bb9103f4ed9acfc17cef765dca8c33cd3a1f
584-
F src/pragma.c b57a859a366472131194a9ad35cd76d5920577226b04c884b1b9085605faa280
584+
F src/pragma.c 9bf7d8a2a9ad3bc36df3ec0d61817a44c38a1da527d59c26c203047f906e334a
585585
F src/pragma.h e690a356c18e98414d2e870ea791c1be1545a714ba623719deb63f7f226d8bb7
586586
F src/prepare.c 971d5819a4bda88038c2283d71fc0a2974ffc3dd480f9bd941341017abacfd1b
587587
F src/printf.c e99ee9741e79ae3873458146f59644276657340385ade4e76a5f5d1c25793764
@@ -593,7 +593,7 @@ F src/shell.c.in e7e7c2c69ae86c5ee9e8ad66227203d46ff6dce8700a1b1dababff01c71d33d
593593
F src/sqlite.h.in b9b7fd73239d94db20332bb6e504688001e5564b655e1318a4427a1caef4b99e
594594
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
595595
F src/sqlite3ext.h a988810c9b21c0dc36dc7a62735012339dc76fc7ab448fb0792721d30eacb69d
596-
F src/sqliteInt.h ebf18764e404a2cef39ae5bfc8dd4a83bf0d70be1c444a4fbd8539eb35ef6ffd
596+
F src/sqliteInt.h 9c46510e108e6a525918c8d2c618a4c4527c53bc0c78c866edf5ee3315646726
597597
F src/sqliteLimit.h d7323ffea5208c6af2734574bae933ca8ed2ab728083caa117c9738581a31657
598598
F src/status.c 160c445d7d28c984a0eae38c144f6419311ed3eace59b44ac6dafc20db4af749
599599
F src/table.c 0f141b58a16de7e2fbe81c308379e7279f4c6b50eb08efeec5892794a0ba30d1
@@ -1999,8 +1999,11 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
19991999
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
20002000
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
20012001
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
2002-
P 609fbb94b8f01d6792e5941ab23ce041313d359f6788c4dde6b1ca749ab49137
2003-
R 487a3bf9c59a00f1f5680d4b9fe8f377
2002+
P 1c4157c71cd1e062a9c2c79787d17e34e340f28ce1e40573851dfe174f5da7d7
2003+
R 76adaad53bd26cbe572b84e819c193a2
2004+
T *branch * tempdir-mutex
2005+
T *sym-tempdir-mutex *
2006+
T -sym-trunk *
20042007
U drh
2005-
Z 09e47f999161ac29a61ba026e8089efe
2008+
Z 159fcca8739ffc7d9b735c4f860ae553
20062009
# Remove this line to create a well-formed Fossil manifest.

manifest.uuid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1c4157c71cd1e062a9c2c79787d17e34e340f28ce1e40573851dfe174f5da7d7
1+
5ee3515fbb88bf1ae5f8b507844f82dcc429380b6ebeab9b09b52b25ee60a60d

src/os_unix.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5855,6 +5855,7 @@ static const char *unixTempFileDir(void){
58555855
static int unixGetTempname(int nBuf, char *zBuf){
58565856
const char *zDir;
58575857
int iLimit = 0;
5858+
int rc = SQLITE_OK;
58585859

58595860
/* It's odd to simulate an io-error here, but really this is just
58605861
** using the io-error infrastructure to test that SQLite handles this
@@ -5863,18 +5864,26 @@ static int unixGetTempname(int nBuf, char *zBuf){
58635864
zBuf[0] = 0;
58645865
SimulateIOError( return SQLITE_IOERR );
58655866

5867+
sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
58665868
zDir = unixTempFileDir();
5867-
if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH;
5868-
do{
5869-
u64 r;
5870-
sqlite3_randomness(sizeof(r), &r);
5871-
assert( nBuf>2 );
5872-
zBuf[nBuf-2] = 0;
5873-
sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c",
5874-
zDir, r, 0);
5875-
if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR;
5876-
}while( osAccess(zBuf,0)==0 );
5877-
return SQLITE_OK;
5869+
if( zDir==0 ){
5870+
rc = SQLITE_IOERR_GETTEMPPATH;
5871+
}else{
5872+
do{
5873+
u64 r;
5874+
sqlite3_randomness(sizeof(r), &r);
5875+
assert( nBuf>2 );
5876+
zBuf[nBuf-2] = 0;
5877+
sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c",
5878+
zDir, r, 0);
5879+
if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ){
5880+
rc = SQLITE_ERROR;
5881+
break;
5882+
}
5883+
}while( osAccess(zBuf,0)==0 );
5884+
}
5885+
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
5886+
return rc;
58785887
}
58795888

58805889
#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)

src/os_win.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,6 +1922,7 @@ int sqlite3_win32_set_directory8(
19221922
int rc = sqlite3_initialize();
19231923
if( rc ) return rc;
19241924
#endif
1925+
sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
19251926
if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
19261927
ppDirectory = &sqlite3_data_directory;
19271928
}else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
@@ -1936,14 +1937,19 @@ int sqlite3_win32_set_directory8(
19361937
if( zValue && zValue[0] ){
19371938
zCopy = sqlite3_mprintf("%s", zValue);
19381939
if ( zCopy==0 ){
1939-
return SQLITE_NOMEM_BKPT;
1940+
rc = SQLITE_NOMEM_BKPT;
1941+
goto set_directory8_done;
19401942
}
19411943
}
19421944
sqlite3_free(*ppDirectory);
19431945
*ppDirectory = zCopy;
1944-
return SQLITE_OK;
1946+
rc = SQLITE_OK;
1947+
}else{
1948+
rc = SQLITE_ERROR;
19451949
}
1946-
return SQLITE_ERROR;
1950+
set_directory8_done:
1951+
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
1952+
return rc;
19471953
}
19481954

19491955
/*
@@ -4717,6 +4723,18 @@ static int winMakeEndInDirSep(int nBuf, char *zBuf){
47174723
return 0;
47184724
}
47194725

4726+
/*
4727+
** If sqlite3_temp_directory is not, take the mutex and return true.
4728+
**
4729+
** If sqlite3_temp_directory is NULL, omit the mutex and return false.
4730+
*/
4731+
static int winTempDirDefined(void){
4732+
sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
4733+
if( sqlite3_temp_directory!=0 ) return 1;
4734+
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
4735+
return 0;
4736+
}
4737+
47204738
/*
47214739
** Create a temporary file name and store the resulting pointer into pzBuf.
47224740
** The pointer returned in pzBuf must be freed via sqlite3_free().
@@ -4753,20 +4771,23 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
47534771
*/
47544772
nDir = nMax - (nPre + 15);
47554773
assert( nDir>0 );
4756-
if( sqlite3_temp_directory ){
4774+
if( winTempDirDefined() ){
47574775
int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
47584776
if( nDirLen>0 ){
47594777
if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
47604778
nDirLen++;
47614779
}
47624780
if( nDirLen>nDir ){
4781+
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
47634782
sqlite3_free(zBuf);
47644783
OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
47654784
return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0);
47664785
}
47674786
sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
47684787
}
4788+
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
47694789
}
4790+
47704791
#if defined(__CYGWIN__)
47714792
else{
47724793
static const char *azDirs[] = {
@@ -5579,6 +5600,7 @@ static int winFullPathname(
55795600
SimulateIOError( return SQLITE_ERROR );
55805601
UNUSED_PARAMETER(nFull);
55815602
assert( nFull>=pVfs->mxPathname );
5603+
sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
55825604
if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
55835605
/*
55845606
** NOTE: We are dealing with a relative path name and the data
@@ -5629,13 +5651,15 @@ static int winFullPathname(
56295651
sqlite3_free(zOut);
56305652
}
56315653
}
5654+
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
56325655
return SQLITE_OK;
56335656
#endif
56345657

56355658
#if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
56365659
SimulateIOError( return SQLITE_ERROR );
56375660
/* WinCE has no concept of a relative pathname, or so I am told. */
56385661
/* WinRT has no way to convert a relative path to an absolute one. */
5662+
sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
56395663
if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
56405664
/*
56415665
** NOTE: We are dealing with a relative path name and the data
@@ -5648,6 +5672,7 @@ static int winFullPathname(
56485672
}else{
56495673
sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
56505674
}
5675+
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
56515676
return SQLITE_OK;
56525677
#endif
56535678

@@ -5658,6 +5683,7 @@ static int winFullPathname(
56585683
** current working directory has been unlinked.
56595684
*/
56605685
SimulateIOError( return SQLITE_ERROR );
5686+
sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
56615687
if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
56625688
/*
56635689
** NOTE: We are dealing with a relative path name and the data
@@ -5667,6 +5693,7 @@ static int winFullPathname(
56675693
*/
56685694
sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
56695695
sqlite3_data_directory, winGetDirSep(), zRelative);
5696+
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
56705697
return SQLITE_OK;
56715698
}
56725699
zConverted = winConvertFromUtf8Filename(zRelative);

src/pragma.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,7 @@ void sqlite3Pragma(
965965
**
966966
*/
967967
case PragTyp_TEMP_STORE_DIRECTORY: {
968+
sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
968969
if( !zRight ){
969970
returnSingleText(v, sqlite3_temp_directory);
970971
}else{
@@ -974,6 +975,7 @@ void sqlite3Pragma(
974975
rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
975976
if( rc!=SQLITE_OK || res==0 ){
976977
sqlite3ErrorMsg(pParse, "not a writable directory");
978+
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
977979
goto pragma_out;
978980
}
979981
}
@@ -991,6 +993,7 @@ void sqlite3Pragma(
991993
}
992994
#endif /* SQLITE_OMIT_WSD */
993995
}
996+
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
994997
break;
995998
}
996999

@@ -1009,6 +1012,7 @@ void sqlite3Pragma(
10091012
**
10101013
*/
10111014
case PragTyp_DATA_STORE_DIRECTORY: {
1015+
sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
10121016
if( !zRight ){
10131017
returnSingleText(v, sqlite3_data_directory);
10141018
}else{
@@ -1018,6 +1022,7 @@ void sqlite3Pragma(
10181022
rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
10191023
if( rc!=SQLITE_OK || res==0 ){
10201024
sqlite3ErrorMsg(pParse, "not a writable directory");
1025+
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
10211026
goto pragma_out;
10221027
}
10231028
}
@@ -1029,6 +1034,7 @@ void sqlite3Pragma(
10291034
}
10301035
#endif /* SQLITE_OMIT_WSD */
10311036
}
1037+
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_TEMPDIR));
10321038
break;
10331039
}
10341040
#endif

src/sqliteInt.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@
198198
*/
199199
#include "sqlite3.h"
200200

201+
/*
202+
** Reuse the STATIC_LRU for mutex access to sqlite3_temp_directory.
203+
*/
204+
#define SQLITE_MUTEX_STATIC_TEMPDIR SQLITE_MUTEX_STATIC_LRU
205+
201206
/*
202207
** Include the configuration header output by 'configure' if we're using the
203208
** autoconf-based build

0 commit comments

Comments
 (0)