124
148
#endif /* OS_WINCE */
127
** Convert a UTF-8 string to UTF-32. Space to hold the returned string
128
** is obtained from sqliteMalloc.
151
** Convert a UTF-8 string to microsoft unicode (UTF-16?).
153
** Space to hold the returned string is obtained from malloc.
130
155
static WCHAR *utf8ToUnicode(const char *zFilename){
132
157
WCHAR *zWideFilename;
137
159
nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
138
zWideFilename = sqliteMalloc( nChar*sizeof(zWideFilename[0]) );
160
zWideFilename = malloc( nChar*sizeof(zWideFilename[0]) );
139
161
if( zWideFilename==0 ){
142
164
nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, nChar);
144
sqliteFree(zWideFilename);
145
167
zWideFilename = 0;
147
169
return zWideFilename;
151
** Convert UTF-32 to UTF-8. Space to hold the returned string is
152
** obtained from sqliteMalloc().
173
** Convert microsoft unicode to UTF-8. Space to hold the returned string is
174
** obtained from malloc().
154
176
static char *unicodeToUtf8(const WCHAR *zWideFilename){
158
180
nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
159
zFilename = sqliteMalloc( nByte );
181
zFilename = malloc( nByte );
160
182
if( zFilename==0 ){
163
185
nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
165
187
if( nByte == 0 ){
166
sqliteFree(zFilename);
195
** Convert an ansi string to microsoft unicode, based on the
196
** current codepage settings for file apis.
198
** Space to hold the returned string is obtained
201
static WCHAR *mbcsToUnicode(const char *zFilename){
203
WCHAR *zMbcsFilename;
204
int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
206
nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, NULL,0)*sizeof(WCHAR);
207
zMbcsFilename = malloc( nByte*sizeof(zMbcsFilename[0]) );
208
if( zMbcsFilename==0 ){
211
nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename, nByte);
216
return zMbcsFilename;
220
** Convert microsoft unicode to multibyte character string, based on the
221
** user's Ansi codepage.
223
** Space to hold the returned string is obtained from
226
static char *unicodeToMbcs(const WCHAR *zWideFilename){
229
int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
231
nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
232
zFilename = malloc( nByte );
236
nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename, nByte,
246
** Convert multibyte character string to UTF-8. Space to hold the
247
** returned string is obtained from malloc().
249
static char *mbcsToUtf8(const char *zFilename){
253
zTmpWide = mbcsToUnicode(zFilename);
257
zFilenameUtf8 = unicodeToUtf8(zTmpWide);
259
return zFilenameUtf8;
263
** Convert UTF-8 to multibyte character string. Space to hold the
264
** returned string is obtained from malloc().
266
static char *utf8ToMbcs(const char *zFilename){
270
zTmpWide = utf8ToUnicode(zFilename);
274
zFilenameMbcs = unicodeToMbcs(zTmpWide);
276
return zFilenameMbcs;
475
582
*****************************************************************************/
476
583
#endif /* OS_WINCE */
479
** Delete the named file.
481
** Note that windows does not allow a file to be deleted if some other
482
** process has it open. Sometimes a virus scanner or indexing program
483
** will open a journal file shortly after it is created in order to do
484
** whatever it is it does. While this other process is holding the
485
** file open, we will be unable to delete it. To work around this
486
** problem, we delay 100 milliseconds and try to delete again. Up
487
** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
488
** up and returning an error.
490
#define MX_DELETION_ATTEMPTS 3
491
int sqlite3WinDelete(const char *zFilename){
492
WCHAR *zWide = utf8ToUnicode(zFilename);
497
rc = DeleteFileW(zWide);
498
}while( rc==0 && GetFileAttributesW(zWide)!=0xffffffff
499
&& cnt++ < MX_DELETION_ATTEMPTS && (Sleep(100), 1) );
506
rc = DeleteFileA(zFilename);
507
}while( rc==0 && GetFileAttributesA(zFilename)!=0xffffffff
508
&& cnt++ < MX_DELETION_ATTEMPTS && (Sleep(100), 1) );
511
TRACE2("DELETE \"%s\"\n", zFilename);
512
return rc!=0 ? SQLITE_OK : SQLITE_IOERR;
516
** Return TRUE if the named file exists.
518
int sqlite3WinFileExists(const char *zFilename){
520
WCHAR *zWide = utf8ToUnicode(zFilename);
522
exists = GetFileAttributesW(zWide) != 0xffffffff;
528
exists = GetFileAttributesA(zFilename) != 0xffffffff;
534
/* Forward declaration */
535
static int allocateWinFile(winFile *pInit, OsFile **pId);
538
** Attempt to open a file for both reading and writing. If that
539
** fails, try opening it read-only. If the file does not exist,
542
** On success, a handle for the open file is written to *id
543
** and *pReadonly is set to 0 if the file was opened for reading and
544
** writing or 1 if the file was opened read-only. The function returns
547
** On failure, the function returns SQLITE_CANTOPEN and leaves
548
** *id and *pReadonly unchanged.
550
int sqlite3WinOpenReadWrite(
551
const char *zFilename,
557
WCHAR *zWide = utf8ToUnicode(zFilename);
560
h = CreateFileW(zWide,
561
GENERIC_READ | GENERIC_WRITE,
562
FILE_SHARE_READ | FILE_SHARE_WRITE,
565
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
568
if( h==INVALID_HANDLE_VALUE ){
569
h = CreateFileW(zWide,
571
FILE_SHARE_READ | FILE_SHARE_WRITE,
574
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
577
if( h==INVALID_HANDLE_VALUE ){
579
return SQLITE_CANTOPEN;
586
if (!winceCreateLock(zFilename, &f)){
589
return SQLITE_CANTOPEN;
597
h = CreateFileA(zFilename,
598
GENERIC_READ | GENERIC_WRITE,
599
FILE_SHARE_READ | FILE_SHARE_WRITE,
602
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
605
if( h==INVALID_HANDLE_VALUE ){
606
h = CreateFileA(zFilename,
608
FILE_SHARE_READ | FILE_SHARE_WRITE,
611
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
614
if( h==INVALID_HANDLE_VALUE ){
615
return SQLITE_CANTOPEN;
621
#endif /* OS_WINCE */
625
f.zDeleteOnClose = 0;
627
TRACE3("OPEN R/W %d \"%s\"\n", h, zFilename);
628
return allocateWinFile(&f, pId);
633
** Attempt to open a new file for exclusive access by this process.
634
** The file will be opened for both reading and writing. To avoid
635
** a potential security problem, we do not allow the file to have
636
** previously existed. Nor do we allow the file to be a symbolic
639
** If delFlag is true, then make arrangements to automatically delete
640
** the file when it is closed.
642
** On success, write the file handle into *id and return SQLITE_OK.
644
** On failure, return SQLITE_CANTOPEN.
646
** Sometimes if we have just deleted a prior journal file, windows
647
** will fail to open a new one because there is a "pending delete".
648
** To work around this bug, we pause for 100 milliseconds and attempt
649
** a second open after the first one fails. The whole operation only
650
** fails if both open attempts are unsuccessful.
652
int sqlite3WinOpenExclusive(const char *zFilename, OsFile **pId, int delFlag){
656
WCHAR *zWide = utf8ToUnicode(zFilename);
658
fileflags = FILE_FLAG_RANDOM_ACCESS;
661
fileflags |= FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE;
667
h = CreateFileW(zWide,
668
GENERIC_READ | GENERIC_WRITE,
675
}while( h==INVALID_HANDLE_VALUE && cnt++ < 2 && (Sleep(100), 1) );
683
h = CreateFileA(zFilename,
684
GENERIC_READ | GENERIC_WRITE,
691
}while( h==INVALID_HANDLE_VALUE && cnt++ < 2 && (Sleep(100), 1) );
692
#endif /* OS_WINCE */
694
if( h==INVALID_HANDLE_VALUE ){
695
return SQLITE_CANTOPEN;
699
f.zDeleteOnClose = delFlag ? utf8ToUnicode(zFilename) : 0;
702
TRACE3("OPEN EX %d \"%s\"\n", h, zFilename);
703
return allocateWinFile(&f, pId);
707
** Attempt to open a new file for read-only access.
709
** On success, write the file handle into *id and return SQLITE_OK.
711
** On failure, return SQLITE_CANTOPEN.
713
int sqlite3WinOpenReadOnly(const char *zFilename, OsFile **pId){
716
WCHAR *zWide = utf8ToUnicode(zFilename);
719
h = CreateFileW(zWide,
724
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
732
h = CreateFileA(zFilename,
737
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
742
if( h==INVALID_HANDLE_VALUE ){
743
return SQLITE_CANTOPEN;
747
f.zDeleteOnClose = 0;
750
TRACE3("OPEN RO %d \"%s\"\n", h, zFilename);
751
return allocateWinFile(&f, pId);
755
** Attempt to open a file descriptor for the directory that contains a
756
** file. This file descriptor can be used to fsync() the directory
757
** in order to make sure the creation of a new file is actually written
760
** This routine is only meaningful for Unix. It is a no-op under
761
** windows since windows does not support hard links.
763
** On success, a handle for a previously open file is at *id is
764
** updated with the new directory file descriptor and SQLITE_OK is
767
** On failure, the function returns SQLITE_CANTOPEN and leaves
770
static int winOpenDirectory(
778
** If the following global variable points to a string which is the
779
** name of a directory, then that directory will be used to store
782
char *sqlite3_temp_directory = 0;
785
** Create a temporary file name in zBuf. zBuf must be big enough to
786
** hold at least SQLITE_TEMPNAME_SIZE characters.
788
int sqlite3WinTempFileName(char *zBuf){
789
static char zChars[] =
790
"abcdefghijklmnopqrstuvwxyz"
791
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
794
char zTempPath[SQLITE_TEMPNAME_SIZE];
795
if( sqlite3_temp_directory ){
796
strncpy(zTempPath, sqlite3_temp_directory, SQLITE_TEMPNAME_SIZE-30);
797
zTempPath[SQLITE_TEMPNAME_SIZE-30] = 0;
800
WCHAR zWidePath[SQLITE_TEMPNAME_SIZE];
801
GetTempPathW(SQLITE_TEMPNAME_SIZE-30, zWidePath);
802
zMulti = unicodeToUtf8(zWidePath);
804
strncpy(zTempPath, zMulti, SQLITE_TEMPNAME_SIZE-30);
805
zTempPath[SQLITE_TEMPNAME_SIZE-30] = 0;
809
GetTempPathA(SQLITE_TEMPNAME_SIZE-30, zTempPath);
811
for(i=strlen(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){}
814
sprintf(zBuf, "%s\\"TEMP_FILE_PREFIX, zTempPath);
816
sqlite3Randomness(15, &zBuf[j]);
817
for(i=0; i<15; i++, j++){
818
zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
821
if( !sqlite3OsFileExists(zBuf) ) break;
823
TRACE2("TEMP FILENAME: %s\n", zBuf);
585
/*****************************************************************************
586
** The next group of routines implement the I/O methods specified
587
** by the sqlite3_io_methods object.
588
******************************************************************************/
1246
** Turn a relative pathname into a full pathname. Return a pointer
1247
** to the full pathname stored in space obtained from sqliteMalloc().
1248
** The calling function is responsible for freeing this space once it
1249
** is no longer needed.
1001
** Control and query of the open file handle.
1251
char *sqlite3WinFullPathname(const char *zRelative){
1253
#if defined(__CYGWIN__)
1255
nByte = strlen(zRelative) + MAX_PATH + 1001;
1256
zFull = sqliteMalloc( nByte );
1257
if( zFull==0 ) return 0;
1258
if( cygwin_conv_to_full_win32_path(zRelative, zFull) ) return 0;
1260
/* WinCE has no concept of a relative pathname, or so I am told. */
1261
zFull = sqliteStrDup(zRelative);
1266
zWide = utf8ToUnicode(zRelative);
1268
WCHAR *zTemp, *zNotUsedW;
1269
nByte = GetFullPathNameW(zWide, 0, 0, &zNotUsedW) + 1;
1270
zTemp = sqliteMalloc( nByte*sizeof(zTemp[0]) );
1271
if( zTemp==0 ) return 0;
1272
GetFullPathNameW(zWide, nByte, zTemp, &zNotUsedW);
1274
zFull = unicodeToUtf8(zTemp);
1277
nByte = GetFullPathNameA(zRelative, 0, 0, &zNotUsed) + 1;
1278
zFull = sqliteMalloc( nByte*sizeof(zFull[0]) );
1279
if( zFull==0 ) return 0;
1280
GetFullPathNameA(zRelative, nByte, zFull, &zNotUsed);
1003
static int winFileControl(sqlite3_file *id, int op, void *pArg){
1005
case SQLITE_FCNTL_LOCKSTATE: {
1006
*(int*)pArg = ((winFile*)id)->locktype;
1287
** The fullSync option is meaningless on windows. This is a no-op.
1289
static void winSetFullSync(OsFile *id, int v){
1294
** Return the underlying file handle for an OsFile
1296
static int winFileHandle(OsFile *id){
1297
return (int)((winFile*)id)->h;
1301
** Return an integer that indices the type of lock currently held
1302
** by this handle. (Used for testing and analysis only.)
1304
static int winLockState(OsFile *id){
1305
return ((winFile*)id)->locktype;
1309
** This vector defines all the methods that can operate on an OsFile
1312
static const IoMethod sqlite3WinIoMethod = {
1010
return SQLITE_ERROR;
1014
** Return the sector size in bytes of the underlying block device for
1015
** the specified file. This is almost always 512 bytes, but may be
1016
** larger for some devices.
1018
** SQLite code assumes this function cannot fail. It also assumes that
1019
** if two files are created in the same file-system directory (i.e.
1020
** a database and its journal file) that the sector size will be the
1023
static int winSectorSize(sqlite3_file *id){
1024
return SQLITE_DEFAULT_SECTOR_SIZE;
1028
** Return a vector of device characteristics.
1030
static int winDeviceCharacteristics(sqlite3_file *id){
1035
** This vector defines all the methods that can operate on an
1036
** sqlite3_file for win32.
1038
static const sqlite3_io_methods winIoMethod = {
1326
1048
winCheckReservedLock,
1051
winDeviceCharacteristics
1330
** Allocate memory for an OsFile. Initialize the new OsFile
1331
** to the value given in pInit and return a pointer to the new
1332
** OsFile. If we run out of memory, close the file and return NULL.
1334
static int allocateWinFile(winFile *pInit, OsFile **pId){
1336
pNew = sqliteMalloc( sizeof(*pNew) );
1338
CloseHandle(pInit->h);
1340
sqliteFree(pInit->zDeleteOnClose);
1343
return SQLITE_NOMEM;
1346
pNew->pMethod = &sqlite3WinIoMethod;
1347
pNew->locktype = NO_LOCK;
1348
pNew->sharedLockByte = 0;
1349
*pId = (OsFile*)pNew;
1356
#endif /* SQLITE_OMIT_DISKIO */
1357
1054
/***************************************************************************
1358
** Everything above deals with file I/O. Everything that follows deals
1359
** with other miscellanous aspects of the operating system interface
1055
** Here ends the I/O methods that form the sqlite3_io_methods object.
1057
** The next block of code implements the VFS methods.
1360
1058
****************************************************************************/
1363
** Get information to seed the random number generator. The seed
1364
** is written into the buffer zBuf[256]. The calling function must
1365
** supply a sufficiently large buffer.
1367
int sqlite3WinRandomSeed(char *zBuf){
1368
/* We have to initialize zBuf to prevent valgrind from reporting
1369
** errors. The reports issued by valgrind are incorrect - we would
1370
** prefer that the randomness be increased by making use of the
1371
** uninitialized space in zBuf - but valgrind errors tend to worry
1372
** some users. Rather than argue, it seems easier just to initialize
1373
** the whole array and silence valgrind, even if that means less randomness
1374
** in the random seed.
1376
** When testing, initializing zBuf[] to zero is all we do. That means
1377
** that we always use the same random number sequence.* This makes the
1378
** tests repeatable.
1380
memset(zBuf, 0, 256);
1381
GetSystemTime((LPSYSTEMTIME)zBuf);
1061
** Convert a UTF-8 filename into whatever form the underlying
1062
** operating system wants filenames in. Space to hold the result
1063
** is obtained from malloc and must be freed by the calling
1066
static void *convertUtf8Filename(const char *zFilename){
1067
void *zConverted = 0;
1069
zConverted = utf8ToUnicode(zFilename);
1071
zConverted = utf8ToMbcs(zFilename);
1073
/* caller will handle out of memory */
1081
sqlite3_vfs *pVfs, /* Not used */
1082
const char *zName, /* Name of the file (UTF-8) */
1083
sqlite3_file *id, /* Write the SQLite file handle here */
1084
int flags, /* Open mode flags */
1085
int *pOutFlags /* Status return flags */
1088
DWORD dwDesiredAccess;
1090
DWORD dwCreationDisposition;
1091
DWORD dwFlagsAndAttributes = 0;
1093
winFile *pFile = (winFile*)id;
1094
void *zConverted = convertUtf8Filename(zName);
1095
if( zConverted==0 ){
1096
return SQLITE_NOMEM;
1099
if( flags & SQLITE_OPEN_READWRITE ){
1100
dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
1102
dwDesiredAccess = GENERIC_READ;
1104
if( flags & SQLITE_OPEN_CREATE ){
1105
dwCreationDisposition = OPEN_ALWAYS;
1107
dwCreationDisposition = OPEN_EXISTING;
1109
if( flags & SQLITE_OPEN_MAIN_DB ){
1110
dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
1114
if( flags & SQLITE_OPEN_DELETEONCLOSE ){
1116
dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
1118
dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
1119
| FILE_ATTRIBUTE_HIDDEN
1120
| FILE_FLAG_DELETE_ON_CLOSE;
1124
dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
1127
/* Reports from the internet are that performance is always
1128
** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */
1129
dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
1131
h = CreateFileW((WCHAR*)zConverted,
1135
dwCreationDisposition,
1136
dwFlagsAndAttributes,
1141
return SQLITE_NOMEM;
1143
h = CreateFileA((char*)zConverted,
1147
dwCreationDisposition,
1148
dwFlagsAndAttributes,
1153
if( h==INVALID_HANDLE_VALUE ){
1155
if( flags & SQLITE_OPEN_READWRITE ){
1156
return winOpen(0, zName, id,
1157
((flags|SQLITE_OPEN_READONLY)&~SQLITE_OPEN_READWRITE), pOutFlags);
1159
return SQLITE_CANTOPEN;
1163
if( flags & SQLITE_OPEN_READWRITE ){
1164
*pOutFlags = SQLITE_OPEN_READWRITE;
1166
*pOutFlags = SQLITE_OPEN_READONLY;
1169
memset(pFile, 0, sizeof(*pFile));
1170
pFile->pMethod = &winIoMethod;
1173
if( (flags & (SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB)) ==
1174
(SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB)
1175
&& !winceCreateLock(zName, pFile)
1179
return SQLITE_CANTOPEN;
1182
pFile->zDeleteOnClose = zConverted;
1193
** Delete the named file.
1195
** Note that windows does not allow a file to be deleted if some other
1196
** process has it open. Sometimes a virus scanner or indexing program
1197
** will open a journal file shortly after it is created in order to do
1198
** whatever does. While this other process is holding the
1199
** file open, we will be unable to delete it. To work around this
1200
** problem, we delay 100 milliseconds and try to delete again. Up
1201
** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
1202
** up and returning an error.
1204
#define MX_DELETION_ATTEMPTS 5
1205
static int winDelete(
1206
sqlite3_vfs *pVfs, /* Not used on win32 */
1207
const char *zFilename, /* Name of file to delete */
1208
int syncDir /* Not used on win32 */
1212
void *zConverted = convertUtf8Filename(zFilename);
1213
if( zConverted==0 ){
1214
return SQLITE_NOMEM;
1216
SimulateIOError(return SQLITE_IOERR_DELETE);
1219
DeleteFileW(zConverted);
1220
}while( (rc = GetFileAttributesW(zConverted))!=0xffffffff
1221
&& cnt++ < MX_DELETION_ATTEMPTS && (Sleep(100), 1) );
1224
return SQLITE_NOMEM;
1227
DeleteFileA(zConverted);
1228
}while( (rc = GetFileAttributesA(zConverted))!=0xffffffff
1229
&& cnt++ < MX_DELETION_ATTEMPTS && (Sleep(100), 1) );
1233
OSTRACE2("DELETE \"%s\"\n", zFilename);
1234
return rc==0xffffffff ? SQLITE_OK : SQLITE_IOERR_DELETE;
1238
** Check the existance and status of a file.
1240
static int winAccess(
1241
sqlite3_vfs *pVfs, /* Not used on win32 */
1242
const char *zFilename, /* Name of file to check */
1243
int flags /* Type of test to make on this file */
1247
void *zConverted = convertUtf8Filename(zFilename);
1248
if( zConverted==0 ){
1249
return SQLITE_NOMEM;
1252
attr = GetFileAttributesW((WCHAR*)zConverted);
1255
return SQLITE_NOMEM;
1257
attr = GetFileAttributesA((char*)zConverted);
1262
case SQLITE_ACCESS_READ:
1263
case SQLITE_ACCESS_EXISTS:
1264
rc = attr!=0xffffffff;
1266
case SQLITE_ACCESS_READWRITE:
1267
rc = (attr & FILE_ATTRIBUTE_READONLY)==0;
1270
assert(!"Invalid flags argument");
1277
** Create a temporary file name in zBuf. zBuf must be big enough to
1278
** hold at pVfs->mxPathname characters.
1280
static int winGetTempname(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
1281
static char zChars[] =
1282
"abcdefghijklmnopqrstuvwxyz"
1283
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1286
char zTempPath[MAX_PATH+1];
1287
if( sqlite3_temp_directory ){
1288
sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", sqlite3_temp_directory);
1291
WCHAR zWidePath[MAX_PATH];
1292
GetTempPathW(MAX_PATH-30, zWidePath);
1293
zMulti = unicodeToUtf8(zWidePath);
1295
sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zMulti);
1298
return SQLITE_NOMEM;
1302
char zMbcsPath[MAX_PATH];
1303
GetTempPathA(MAX_PATH-30, zMbcsPath);
1304
zUtf8 = mbcsToUtf8(zMbcsPath);
1306
sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8);
1309
return SQLITE_NOMEM;
1312
for(i=strlen(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){}
1314
sqlite3_snprintf(nBuf-30, zBuf,
1315
"%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
1317
sqlite3_randomness(20, &zBuf[j]);
1318
for(i=0; i<20; i++, j++){
1319
zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
1322
OSTRACE2("TEMP FILENAME: %s\n", zBuf);
1327
** Turn a relative pathname into a full pathname. Write the full
1328
** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
1331
static int winFullPathname(
1332
sqlite3_vfs *pVfs, /* Pointer to vfs object */
1333
const char *zRelative, /* Possibly relative input path */
1334
int nFull, /* Size of output buffer in bytes */
1335
char *zFull /* Output buffer */
1338
#if defined(__CYGWIN__)
1339
cygwin_conv_to_full_win32_path(zRelative, zFull);
1344
/* WinCE has no concept of a relative pathname, or so I am told. */
1345
sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zRelative);
1349
#if !OS_WINCE && !defined(__CYGWIN__)
1353
zConverted = convertUtf8Filename(zRelative);
1356
nByte = GetFullPathNameW((WCHAR*)zConverted, 0, 0, 0) + 3;
1357
zTemp = malloc( nByte*sizeof(zTemp[0]) );
1360
return SQLITE_NOMEM;
1362
GetFullPathNameW((WCHAR*)zConverted, nByte, zTemp, 0);
1364
zOut = unicodeToUtf8(zTemp);
1368
nByte = GetFullPathNameA((char*)zConverted, 0, 0, 0) + 3;
1369
zTemp = malloc( nByte*sizeof(zTemp[0]) );
1372
return SQLITE_NOMEM;
1374
GetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
1376
zOut = mbcsToUtf8(zTemp);
1380
sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zOut);
1384
return SQLITE_NOMEM;
1389
#ifndef SQLITE_OMIT_LOAD_EXTENSION
1391
** Interfaces for opening a shared library, finding entry points
1392
** within the shared library, and closing the shared library.
1395
** Interfaces for opening a shared library, finding entry points
1396
** within the shared library, and closing the shared library.
1398
static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
1400
void *zConverted = convertUtf8Filename(zFilename);
1401
if( zConverted==0 ){
1405
h = LoadLibraryW((WCHAR*)zConverted);
1410
h = LoadLibraryA((char*)zConverted);
1416
static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
1418
int error = GetLastError();
1419
if( error>0x7FFFFFF ){
1420
sqlite3_snprintf(nBuf, zBufOut, "OsError 0x%x", error);
1422
sqlite3_snprintf(nBuf, zBufOut, "OsError %d", error);
1426
FORMAT_MESSAGE_FROM_SYSTEM,
1436
void *winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){
1438
/* The GetProcAddressA() routine is only available on wince. */
1439
return GetProcAddressA((HANDLE)pHandle, zSymbol);
1441
/* All other windows platforms expect GetProcAddress() to take
1442
** an Ansi string regardless of the _UNICODE setting */
1443
return GetProcAddress((HANDLE)pHandle, zSymbol);
1446
void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
1447
FreeLibrary((HANDLE)pHandle);
1449
#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
1451
#define winDlError 0
1453
#define winDlClose 0
1458
** Write up to nBuf bytes of randomness into zBuf.
1460
static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
1462
if( sizeof(SYSTEMTIME)<=nBuf-n ){
1465
memcpy(&zBuf[n], &x, sizeof(x));
1468
if( sizeof(DWORD)<=nBuf-n ){
1469
DWORD pid = GetCurrentProcessId();
1470
memcpy(&zBuf[n], &pid, sizeof(pid));
1473
if( sizeof(DWORD)<=nBuf-n ){
1474
DWORD cnt = GetTickCount();
1475
memcpy(&zBuf[n], &cnt, sizeof(cnt));
1478
if( sizeof(LARGE_INTEGER)<=nBuf-n ){
1480
QueryPerformanceCounter(&i);
1481
memcpy(&zBuf[n], &i, sizeof(i));
1386
1489
** Sleep for a little while. Return the amount of time slept.
1388
int sqlite3WinSleep(int ms){
1394
** Static variables used for thread synchronization
1396
static int inMutex = 0;
1397
#ifdef SQLITE_W32_THREADS
1398
static DWORD mutexOwner;
1399
static CRITICAL_SECTION cs;
1403
** The following pair of routines implement mutual exclusion for
1404
** multi-threaded processes. Only a single thread is allowed to
1405
** executed code that is surrounded by EnterMutex() and LeaveMutex().
1407
** SQLite uses only a single Mutex. There is not much critical
1408
** code and what little there is executes quickly and without blocking.
1410
** Version 3.3.1 and earlier used a simple mutex. Beginning with
1411
** version 3.3.2, a recursive mutex is required.
1413
void sqlite3WinEnterMutex(){
1414
#ifdef SQLITE_W32_THREADS
1415
static int isInit = 0;
1417
static long lock = 0;
1418
if( InterlockedIncrement(&lock)==1 ){
1419
InitializeCriticalSection(&cs);
1425
EnterCriticalSection(&cs);
1426
mutexOwner = GetCurrentThreadId();
1430
void sqlite3WinLeaveMutex(){
1433
#ifdef SQLITE_W32_THREADS
1434
assert( mutexOwner==GetCurrentThreadId() );
1435
LeaveCriticalSection(&cs);
1440
** Return TRUE if the mutex is currently held.
1442
** If the thisThreadOnly parameter is true, return true if and only if the
1443
** calling thread holds the mutex. If the parameter is false, return
1444
** true if any thread holds the mutex.
1446
int sqlite3WinInMutex(int thisThreadOnly){
1447
#ifdef SQLITE_W32_THREADS
1448
return inMutex>0 && (thisThreadOnly==0 || mutexOwner==GetCurrentThreadId());
1491
static int winSleep(sqlite3_vfs *pVfs, int microsec){
1492
Sleep((microsec+999)/1000);
1493
return ((microsec+999)/1000)*1000;
1456
1497
** The following variable, if set to a non-zero value, becomes the result