Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: third_party/sqlite/amalgamation/sqlite3.01.c

Issue 2755803002: NCI: trybot test for sqlite 3.17 import. (Closed)
Patch Set: also clang on Linux i386 Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /************** Begin file date.c ********************************************/
2 /*
3 ** 2003 October 31
4 **
5 ** The author disclaims copyright to this source code. In place of
6 ** a legal notice, here is a blessing:
7 **
8 ** May you do good and not evil.
9 ** May you find forgiveness for yourself and forgive others.
10 ** May you share freely, never taking more than you give.
11 **
12 *************************************************************************
13 ** This file contains the C functions that implement date and time
14 ** functions for SQLite.
15 **
16 ** There is only one exported symbol in this file - the function
17 ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
18 ** All other code has file scope.
19 **
20 ** SQLite processes all times and dates as julian day numbers. The
21 ** dates and times are stored as the number of days since noon
22 ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
23 ** calendar system.
24 **
25 ** 1970-01-01 00:00:00 is JD 2440587.5
26 ** 2000-01-01 00:00:00 is JD 2451544.5
27 **
28 ** This implementation requires years to be expressed as a 4-digit number
29 ** which means that only dates between 0000-01-01 and 9999-12-31 can
30 ** be represented, even though julian day numbers allow a much wider
31 ** range of dates.
32 **
33 ** The Gregorian calendar system is used for all dates and times,
34 ** even those that predate the Gregorian calendar. Historians usually
35 ** use the julian calendar for dates prior to 1582-10-15 and for some
36 ** dates afterwards, depending on locale. Beware of this difference.
37 **
38 ** The conversion algorithms are implemented based on descriptions
39 ** in the following text:
40 **
41 ** Jean Meeus
42 ** Astronomical Algorithms, 2nd Edition, 1998
43 ** ISBM 0-943396-61-1
44 ** Willmann-Bell, Inc
45 ** Richmond, Virginia (USA)
46 */
47 /* #include "sqliteInt.h" */
48 /* #include <stdlib.h> */
49 /* #include <assert.h> */
50 #include <time.h>
51
52 #ifndef SQLITE_OMIT_DATETIME_FUNCS
53
54 /*
55 ** The MSVC CRT on Windows CE may not have a localtime() function.
56 ** So declare a substitute. The substitute function itself is
57 ** defined in "os_win.c".
58 */
59 #if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
60 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
61 struct tm *__cdecl localtime(const time_t *);
62 #endif
63
64 /*
65 ** A structure for holding a single date and time.
66 */
67 typedef struct DateTime DateTime;
68 struct DateTime {
69 sqlite3_int64 iJD; /* The julian day number times 86400000 */
70 int Y, M, D; /* Year, month, and day */
71 int h, m; /* Hour and minutes */
72 int tz; /* Timezone offset in minutes */
73 double s; /* Seconds */
74 char validJD; /* True (1) if iJD is valid */
75 char rawS; /* Raw numeric value stored in s */
76 char validYMD; /* True (1) if Y,M,D are valid */
77 char validHMS; /* True (1) if h,m,s are valid */
78 char validTZ; /* True (1) if tz is valid */
79 char tzSet; /* Timezone was set explicitly */
80 char isError; /* An overflow has occurred */
81 };
82
83
84 /*
85 ** Convert zDate into one or more integers according to the conversion
86 ** specifier zFormat.
87 **
88 ** zFormat[] contains 4 characters for each integer converted, except for
89 ** the last integer which is specified by three characters. The meaning
90 ** of a four-character format specifiers ABCD is:
91 **
92 ** A: number of digits to convert. Always "2" or "4".
93 ** B: minimum value. Always "0" or "1".
94 ** C: maximum value, decoded as:
95 ** a: 12
96 ** b: 14
97 ** c: 24
98 ** d: 31
99 ** e: 59
100 ** f: 9999
101 ** D: the separator character, or \000 to indicate this is the
102 ** last number to convert.
103 **
104 ** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would
105 ** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-".
106 ** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates
107 ** the 2-digit day which is the last integer in the set.
108 **
109 ** The function returns the number of successful conversions.
110 */
111 static int getDigits(const char *zDate, const char *zFormat, ...){
112 /* The aMx[] array translates the 3rd character of each format
113 ** spec into a max size: a b c d e f */
114 static const u16 aMx[] = { 12, 14, 24, 31, 59, 9999 };
115 va_list ap;
116 int cnt = 0;
117 char nextC;
118 va_start(ap, zFormat);
119 do{
120 char N = zFormat[0] - '0';
121 char min = zFormat[1] - '0';
122 int val = 0;
123 u16 max;
124
125 assert( zFormat[2]>='a' && zFormat[2]<='f' );
126 max = aMx[zFormat[2] - 'a'];
127 nextC = zFormat[3];
128 val = 0;
129 while( N-- ){
130 if( !sqlite3Isdigit(*zDate) ){
131 goto end_getDigits;
132 }
133 val = val*10 + *zDate - '0';
134 zDate++;
135 }
136 if( val<(int)min || val>(int)max || (nextC!=0 && nextC!=*zDate) ){
137 goto end_getDigits;
138 }
139 *va_arg(ap,int*) = val;
140 zDate++;
141 cnt++;
142 zFormat += 4;
143 }while( nextC );
144 end_getDigits:
145 va_end(ap);
146 return cnt;
147 }
148
149 /*
150 ** Parse a timezone extension on the end of a date-time.
151 ** The extension is of the form:
152 **
153 ** (+/-)HH:MM
154 **
155 ** Or the "zulu" notation:
156 **
157 ** Z
158 **
159 ** If the parse is successful, write the number of minutes
160 ** of change in p->tz and return 0. If a parser error occurs,
161 ** return non-zero.
162 **
163 ** A missing specifier is not considered an error.
164 */
165 static int parseTimezone(const char *zDate, DateTime *p){
166 int sgn = 0;
167 int nHr, nMn;
168 int c;
169 while( sqlite3Isspace(*zDate) ){ zDate++; }
170 p->tz = 0;
171 c = *zDate;
172 if( c=='-' ){
173 sgn = -1;
174 }else if( c=='+' ){
175 sgn = +1;
176 }else if( c=='Z' || c=='z' ){
177 zDate++;
178 goto zulu_time;
179 }else{
180 return c!=0;
181 }
182 zDate++;
183 if( getDigits(zDate, "20b:20e", &nHr, &nMn)!=2 ){
184 return 1;
185 }
186 zDate += 5;
187 p->tz = sgn*(nMn + nHr*60);
188 zulu_time:
189 while( sqlite3Isspace(*zDate) ){ zDate++; }
190 p->tzSet = 1;
191 return *zDate!=0;
192 }
193
194 /*
195 ** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
196 ** The HH, MM, and SS must each be exactly 2 digits. The
197 ** fractional seconds FFFF can be one or more digits.
198 **
199 ** Return 1 if there is a parsing error and 0 on success.
200 */
201 static int parseHhMmSs(const char *zDate, DateTime *p){
202 int h, m, s;
203 double ms = 0.0;
204 if( getDigits(zDate, "20c:20e", &h, &m)!=2 ){
205 return 1;
206 }
207 zDate += 5;
208 if( *zDate==':' ){
209 zDate++;
210 if( getDigits(zDate, "20e", &s)!=1 ){
211 return 1;
212 }
213 zDate += 2;
214 if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
215 double rScale = 1.0;
216 zDate++;
217 while( sqlite3Isdigit(*zDate) ){
218 ms = ms*10.0 + *zDate - '0';
219 rScale *= 10.0;
220 zDate++;
221 }
222 ms /= rScale;
223 }
224 }else{
225 s = 0;
226 }
227 p->validJD = 0;
228 p->rawS = 0;
229 p->validHMS = 1;
230 p->h = h;
231 p->m = m;
232 p->s = s + ms;
233 if( parseTimezone(zDate, p) ) return 1;
234 p->validTZ = (p->tz!=0)?1:0;
235 return 0;
236 }
237
238 /*
239 ** Put the DateTime object into its error state.
240 */
241 static void datetimeError(DateTime *p){
242 memset(p, 0, sizeof(*p));
243 p->isError = 1;
244 }
245
246 /*
247 ** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
248 ** that the YYYY-MM-DD is according to the Gregorian calendar.
249 **
250 ** Reference: Meeus page 61
251 */
252 static void computeJD(DateTime *p){
253 int Y, M, D, A, B, X1, X2;
254
255 if( p->validJD ) return;
256 if( p->validYMD ){
257 Y = p->Y;
258 M = p->M;
259 D = p->D;
260 }else{
261 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */
262 M = 1;
263 D = 1;
264 }
265 if( Y<-4713 || Y>9999 || p->rawS ){
266 datetimeError(p);
267 return;
268 }
269 if( M<=2 ){
270 Y--;
271 M += 12;
272 }
273 A = Y/100;
274 B = 2 - A + (A/4);
275 X1 = 36525*(Y+4716)/100;
276 X2 = 306001*(M+1)/10000;
277 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
278 p->validJD = 1;
279 if( p->validHMS ){
280 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
281 if( p->validTZ ){
282 p->iJD -= p->tz*60000;
283 p->validYMD = 0;
284 p->validHMS = 0;
285 p->validTZ = 0;
286 }
287 }
288 }
289
290 /*
291 ** Parse dates of the form
292 **
293 ** YYYY-MM-DD HH:MM:SS.FFF
294 ** YYYY-MM-DD HH:MM:SS
295 ** YYYY-MM-DD HH:MM
296 ** YYYY-MM-DD
297 **
298 ** Write the result into the DateTime structure and return 0
299 ** on success and 1 if the input string is not a well-formed
300 ** date.
301 */
302 static int parseYyyyMmDd(const char *zDate, DateTime *p){
303 int Y, M, D, neg;
304
305 if( zDate[0]=='-' ){
306 zDate++;
307 neg = 1;
308 }else{
309 neg = 0;
310 }
311 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){
312 return 1;
313 }
314 zDate += 10;
315 while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
316 if( parseHhMmSs(zDate, p)==0 ){
317 /* We got the time */
318 }else if( *zDate==0 ){
319 p->validHMS = 0;
320 }else{
321 return 1;
322 }
323 p->validJD = 0;
324 p->validYMD = 1;
325 p->Y = neg ? -Y : Y;
326 p->M = M;
327 p->D = D;
328 if( p->validTZ ){
329 computeJD(p);
330 }
331 return 0;
332 }
333
334 /*
335 ** Set the time to the current time reported by the VFS.
336 **
337 ** Return the number of errors.
338 */
339 static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
340 p->iJD = sqlite3StmtCurrentTime(context);
341 if( p->iJD>0 ){
342 p->validJD = 1;
343 return 0;
344 }else{
345 return 1;
346 }
347 }
348
349 /*
350 ** Input "r" is a numeric quantity which might be a julian day number,
351 ** or the number of seconds since 1970. If the value if r is within
352 ** range of a julian day number, install it as such and set validJD.
353 ** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
354 */
355 static void setRawDateNumber(DateTime *p, double r){
356 p->s = r;
357 p->rawS = 1;
358 if( r>=0.0 && r<5373484.5 ){
359 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
360 p->validJD = 1;
361 }
362 }
363
364 /*
365 ** Attempt to parse the given string into a julian day number. Return
366 ** the number of errors.
367 **
368 ** The following are acceptable forms for the input string:
369 **
370 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
371 ** DDDD.DD
372 ** now
373 **
374 ** In the first form, the +/-HH:MM is always optional. The fractional
375 ** seconds extension (the ".FFF") is optional. The seconds portion
376 ** (":SS.FFF") is option. The year and date can be omitted as long
377 ** as there is a time string. The time string can be omitted as long
378 ** as there is a year and date.
379 */
380 static int parseDateOrTime(
381 sqlite3_context *context,
382 const char *zDate,
383 DateTime *p
384 ){
385 double r;
386 if( parseYyyyMmDd(zDate,p)==0 ){
387 return 0;
388 }else if( parseHhMmSs(zDate, p)==0 ){
389 return 0;
390 }else if( sqlite3StrICmp(zDate,"now")==0){
391 return setDateTimeToCurrent(context, p);
392 }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
393 setRawDateNumber(p, r);
394 return 0;
395 }
396 return 1;
397 }
398
399 /* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
400 ** Multiplying this by 86400000 gives 464269060799999 as the maximum value
401 ** for DateTime.iJD.
402 **
403 ** But some older compilers (ex: gcc 4.2.1 on older Macs) cannot deal with
404 ** such a large integer literal, so we have to encode it.
405 */
406 #define INT_464269060799999 ((((i64)0x1a640)<<32)|0x1072fdff)
407
408 /*
409 ** Return TRUE if the given julian day number is within range.
410 **
411 ** The input is the JulianDay times 86400000.
412 */
413 static int validJulianDay(sqlite3_int64 iJD){
414 return iJD>=0 && iJD<=INT_464269060799999;
415 }
416
417 /*
418 ** Compute the Year, Month, and Day from the julian day number.
419 */
420 static void computeYMD(DateTime *p){
421 int Z, A, B, C, D, E, X1;
422 if( p->validYMD ) return;
423 if( !p->validJD ){
424 p->Y = 2000;
425 p->M = 1;
426 p->D = 1;
427 }else{
428 assert( validJulianDay(p->iJD) );
429 Z = (int)((p->iJD + 43200000)/86400000);
430 A = (int)((Z - 1867216.25)/36524.25);
431 A = Z + 1 + A - (A/4);
432 B = A + 1524;
433 C = (int)((B - 122.1)/365.25);
434 D = (36525*(C&32767))/100;
435 E = (int)((B-D)/30.6001);
436 X1 = (int)(30.6001*E);
437 p->D = B - D - X1;
438 p->M = E<14 ? E-1 : E-13;
439 p->Y = p->M>2 ? C - 4716 : C - 4715;
440 }
441 p->validYMD = 1;
442 }
443
444 /*
445 ** Compute the Hour, Minute, and Seconds from the julian day number.
446 */
447 static void computeHMS(DateTime *p){
448 int s;
449 if( p->validHMS ) return;
450 computeJD(p);
451 s = (int)((p->iJD + 43200000) % 86400000);
452 p->s = s/1000.0;
453 s = (int)p->s;
454 p->s -= s;
455 p->h = s/3600;
456 s -= p->h*3600;
457 p->m = s/60;
458 p->s += s - p->m*60;
459 p->rawS = 0;
460 p->validHMS = 1;
461 }
462
463 /*
464 ** Compute both YMD and HMS
465 */
466 static void computeYMD_HMS(DateTime *p){
467 computeYMD(p);
468 computeHMS(p);
469 }
470
471 /*
472 ** Clear the YMD and HMS and the TZ
473 */
474 static void clearYMD_HMS_TZ(DateTime *p){
475 p->validYMD = 0;
476 p->validHMS = 0;
477 p->validTZ = 0;
478 }
479
480 #ifndef SQLITE_OMIT_LOCALTIME
481 /*
482 ** On recent Windows platforms, the localtime_s() function is available
483 ** as part of the "Secure CRT". It is essentially equivalent to
484 ** localtime_r() available under most POSIX platforms, except that the
485 ** order of the parameters is reversed.
486 **
487 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
488 **
489 ** If the user has not indicated to use localtime_r() or localtime_s()
490 ** already, check for an MSVC build environment that provides
491 ** localtime_s().
492 */
493 #if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S \
494 && defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
495 #undef HAVE_LOCALTIME_S
496 #define HAVE_LOCALTIME_S 1
497 #endif
498
499 /*
500 ** The following routine implements the rough equivalent of localtime_r()
501 ** using whatever operating-system specific localtime facility that
502 ** is available. This routine returns 0 on success and
503 ** non-zero on any kind of error.
504 **
505 ** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this
506 ** routine will always fail.
507 **
508 ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
509 ** library function localtime_r() is used to assist in the calculation of
510 ** local time.
511 */
512 static int osLocaltime(time_t *t, struct tm *pTm){
513 int rc;
514 #if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S
515 struct tm *pX;
516 #if SQLITE_THREADSAFE>0
517 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
518 #endif
519 sqlite3_mutex_enter(mutex);
520 pX = localtime(t);
521 #ifndef SQLITE_UNTESTABLE
522 if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0;
523 #endif
524 if( pX ) *pTm = *pX;
525 sqlite3_mutex_leave(mutex);
526 rc = pX==0;
527 #else
528 #ifndef SQLITE_UNTESTABLE
529 if( sqlite3GlobalConfig.bLocaltimeFault ) return 1;
530 #endif
531 #if HAVE_LOCALTIME_R
532 rc = localtime_r(t, pTm)==0;
533 #else
534 rc = localtime_s(pTm, t);
535 #endif /* HAVE_LOCALTIME_R */
536 #endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
537 return rc;
538 }
539 #endif /* SQLITE_OMIT_LOCALTIME */
540
541
542 #ifndef SQLITE_OMIT_LOCALTIME
543 /*
544 ** Compute the difference (in milliseconds) between localtime and UTC
545 ** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs,
546 ** return this value and set *pRc to SQLITE_OK.
547 **
548 ** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value
549 ** is undefined in this case.
550 */
551 static sqlite3_int64 localtimeOffset(
552 DateTime *p, /* Date at which to calculate offset */
553 sqlite3_context *pCtx, /* Write error here if one occurs */
554 int *pRc /* OUT: Error code. SQLITE_OK or ERROR */
555 ){
556 DateTime x, y;
557 time_t t;
558 struct tm sLocal;
559
560 /* Initialize the contents of sLocal to avoid a compiler warning. */
561 memset(&sLocal, 0, sizeof(sLocal));
562
563 x = *p;
564 computeYMD_HMS(&x);
565 if( x.Y<1971 || x.Y>=2038 ){
566 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only
567 ** works for years between 1970 and 2037. For dates outside this range,
568 ** SQLite attempts to map the year into an equivalent year within this
569 ** range, do the calculation, then map the year back.
570 */
571 x.Y = 2000;
572 x.M = 1;
573 x.D = 1;
574 x.h = 0;
575 x.m = 0;
576 x.s = 0.0;
577 } else {
578 int s = (int)(x.s + 0.5);
579 x.s = s;
580 }
581 x.tz = 0;
582 x.validJD = 0;
583 computeJD(&x);
584 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
585 if( osLocaltime(&t, &sLocal) ){
586 sqlite3_result_error(pCtx, "local time unavailable", -1);
587 *pRc = SQLITE_ERROR;
588 return 0;
589 }
590 y.Y = sLocal.tm_year + 1900;
591 y.M = sLocal.tm_mon + 1;
592 y.D = sLocal.tm_mday;
593 y.h = sLocal.tm_hour;
594 y.m = sLocal.tm_min;
595 y.s = sLocal.tm_sec;
596 y.validYMD = 1;
597 y.validHMS = 1;
598 y.validJD = 0;
599 y.rawS = 0;
600 y.validTZ = 0;
601 y.isError = 0;
602 computeJD(&y);
603 *pRc = SQLITE_OK;
604 return y.iJD - x.iJD;
605 }
606 #endif /* SQLITE_OMIT_LOCALTIME */
607
608 /*
609 ** The following table defines various date transformations of the form
610 **
611 ** 'NNN days'
612 **
613 ** Where NNN is an arbitrary floating-point number and "days" can be one
614 ** of several units of time.
615 */
616 static const struct {
617 u8 eType; /* Transformation type code */
618 u8 nName; /* Length of th name */
619 char *zName; /* Name of the transformation */
620 double rLimit; /* Maximum NNN value for this transform */
621 double rXform; /* Constant used for this transform */
622 } aXformType[] = {
623 { 0, 6, "second", 464269060800.0, 86400000.0/(24.0*60.0*60.0) },
624 { 0, 6, "minute", 7737817680.0, 86400000.0/(24.0*60.0) },
625 { 0, 4, "hour", 128963628.0, 86400000.0/24.0 },
626 { 0, 3, "day", 5373485.0, 86400000.0 },
627 { 1, 5, "month", 176546.0, 30.0*86400000.0 },
628 { 2, 4, "year", 14713.0, 365.0*86400000.0 },
629 };
630
631 /*
632 ** Process a modifier to a date-time stamp. The modifiers are
633 ** as follows:
634 **
635 ** NNN days
636 ** NNN hours
637 ** NNN minutes
638 ** NNN.NNNN seconds
639 ** NNN months
640 ** NNN years
641 ** start of month
642 ** start of year
643 ** start of week
644 ** start of day
645 ** weekday N
646 ** unixepoch
647 ** localtime
648 ** utc
649 **
650 ** Return 0 on success and 1 if there is any kind of error. If the error
651 ** is in a system call (i.e. localtime()), then an error message is written
652 ** to context pCtx. If the error is an unrecognized modifier, no error is
653 ** written to pCtx.
654 */
655 static int parseModifier(
656 sqlite3_context *pCtx, /* Function context */
657 const char *z, /* The text of the modifier */
658 int n, /* Length of zMod in bytes */
659 DateTime *p /* The date/time value to be modified */
660 ){
661 int rc = 1;
662 double r;
663 switch(sqlite3UpperToLower[(u8)z[0]] ){
664 #ifndef SQLITE_OMIT_LOCALTIME
665 case 'l': {
666 /* localtime
667 **
668 ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
669 ** show local time.
670 */
671 if( sqlite3_stricmp(z, "localtime")==0 ){
672 computeJD(p);
673 p->iJD += localtimeOffset(p, pCtx, &rc);
674 clearYMD_HMS_TZ(p);
675 }
676 break;
677 }
678 #endif
679 case 'u': {
680 /*
681 ** unixepoch
682 **
683 ** Treat the current value of p->s as the number of
684 ** seconds since 1970. Convert to a real julian day number.
685 */
686 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){
687 r = p->s*1000.0 + 210866760000000.0;
688 if( r>=0.0 && r<464269060800000.0 ){
689 clearYMD_HMS_TZ(p);
690 p->iJD = (sqlite3_int64)r;
691 p->validJD = 1;
692 p->rawS = 0;
693 rc = 0;
694 }
695 }
696 #ifndef SQLITE_OMIT_LOCALTIME
697 else if( sqlite3_stricmp(z, "utc")==0 ){
698 if( p->tzSet==0 ){
699 sqlite3_int64 c1;
700 computeJD(p);
701 c1 = localtimeOffset(p, pCtx, &rc);
702 if( rc==SQLITE_OK ){
703 p->iJD -= c1;
704 clearYMD_HMS_TZ(p);
705 p->iJD += c1 - localtimeOffset(p, pCtx, &rc);
706 }
707 p->tzSet = 1;
708 }else{
709 rc = SQLITE_OK;
710 }
711 }
712 #endif
713 break;
714 }
715 case 'w': {
716 /*
717 ** weekday N
718 **
719 ** Move the date to the same time on the next occurrence of
720 ** weekday N where 0==Sunday, 1==Monday, and so forth. If the
721 ** date is already on the appropriate weekday, this is a no-op.
722 */
723 if( sqlite3_strnicmp(z, "weekday ", 8)==0
724 && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)
725 && (n=(int)r)==r && n>=0 && r<7 ){
726 sqlite3_int64 Z;
727 computeYMD_HMS(p);
728 p->validTZ = 0;
729 p->validJD = 0;
730 computeJD(p);
731 Z = ((p->iJD + 129600000)/86400000) % 7;
732 if( Z>n ) Z -= 7;
733 p->iJD += (n - Z)*86400000;
734 clearYMD_HMS_TZ(p);
735 rc = 0;
736 }
737 break;
738 }
739 case 's': {
740 /*
741 ** start of TTTTT
742 **
743 ** Move the date backwards to the beginning of the current day,
744 ** or month or year.
745 */
746 if( sqlite3_strnicmp(z, "start of ", 9)!=0 ) break;
747 z += 9;
748 computeYMD(p);
749 p->validHMS = 1;
750 p->h = p->m = 0;
751 p->s = 0.0;
752 p->validTZ = 0;
753 p->validJD = 0;
754 if( sqlite3_stricmp(z,"month")==0 ){
755 p->D = 1;
756 rc = 0;
757 }else if( sqlite3_stricmp(z,"year")==0 ){
758 computeYMD(p);
759 p->M = 1;
760 p->D = 1;
761 rc = 0;
762 }else if( sqlite3_stricmp(z,"day")==0 ){
763 rc = 0;
764 }
765 break;
766 }
767 case '+':
768 case '-':
769 case '0':
770 case '1':
771 case '2':
772 case '3':
773 case '4':
774 case '5':
775 case '6':
776 case '7':
777 case '8':
778 case '9': {
779 double rRounder;
780 int i;
781 for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
782 if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){
783 rc = 1;
784 break;
785 }
786 if( z[n]==':' ){
787 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
788 ** specified number of hours, minutes, seconds, and fractional seconds
789 ** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be
790 ** omitted.
791 */
792 const char *z2 = z;
793 DateTime tx;
794 sqlite3_int64 day;
795 if( !sqlite3Isdigit(*z2) ) z2++;
796 memset(&tx, 0, sizeof(tx));
797 if( parseHhMmSs(z2, &tx) ) break;
798 computeJD(&tx);
799 tx.iJD -= 43200000;
800 day = tx.iJD/86400000;
801 tx.iJD -= day*86400000;
802 if( z[0]=='-' ) tx.iJD = -tx.iJD;
803 computeJD(p);
804 clearYMD_HMS_TZ(p);
805 p->iJD += tx.iJD;
806 rc = 0;
807 break;
808 }
809
810 /* If control reaches this point, it means the transformation is
811 ** one of the forms like "+NNN days". */
812 z += n;
813 while( sqlite3Isspace(*z) ) z++;
814 n = sqlite3Strlen30(z);
815 if( n>10 || n<3 ) break;
816 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--;
817 computeJD(p);
818 rc = 1;
819 rRounder = r<0 ? -0.5 : +0.5;
820 for(i=0; i<ArraySize(aXformType); i++){
821 if( aXformType[i].nName==n
822 && sqlite3_strnicmp(aXformType[i].zName, z, n)==0
823 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit
824 ){
825 switch( aXformType[i].eType ){
826 case 1: { /* Special processing to add months */
827 int x;
828 computeYMD_HMS(p);
829 p->M += (int)r;
830 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
831 p->Y += x;
832 p->M -= x*12;
833 p->validJD = 0;
834 r -= (int)r;
835 break;
836 }
837 case 2: { /* Special processing to add years */
838 int y = (int)r;
839 computeYMD_HMS(p);
840 p->Y += y;
841 p->validJD = 0;
842 r -= (int)r;
843 break;
844 }
845 }
846 computeJD(p);
847 p->iJD += (sqlite3_int64)(r*aXformType[i].rXform + rRounder);
848 rc = 0;
849 break;
850 }
851 }
852 clearYMD_HMS_TZ(p);
853 break;
854 }
855 default: {
856 break;
857 }
858 }
859 return rc;
860 }
861
862 /*
863 ** Process time function arguments. argv[0] is a date-time stamp.
864 ** argv[1] and following are modifiers. Parse them all and write
865 ** the resulting time into the DateTime structure p. Return 0
866 ** on success and 1 if there are any errors.
867 **
868 ** If there are zero parameters (if even argv[0] is undefined)
869 ** then assume a default value of "now" for argv[0].
870 */
871 static int isDate(
872 sqlite3_context *context,
873 int argc,
874 sqlite3_value **argv,
875 DateTime *p
876 ){
877 int i, n;
878 const unsigned char *z;
879 int eType;
880 memset(p, 0, sizeof(*p));
881 if( argc==0 ){
882 return setDateTimeToCurrent(context, p);
883 }
884 if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
885 || eType==SQLITE_INTEGER ){
886 setRawDateNumber(p, sqlite3_value_double(argv[0]));
887 }else{
888 z = sqlite3_value_text(argv[0]);
889 if( !z || parseDateOrTime(context, (char*)z, p) ){
890 return 1;
891 }
892 }
893 for(i=1; i<argc; i++){
894 z = sqlite3_value_text(argv[i]);
895 n = sqlite3_value_bytes(argv[i]);
896 if( z==0 || parseModifier(context, (char*)z, n, p) ) return 1;
897 }
898 computeJD(p);
899 if( p->isError || !validJulianDay(p->iJD) ) return 1;
900 return 0;
901 }
902
903
904 /*
905 ** The following routines implement the various date and time functions
906 ** of SQLite.
907 */
908
909 /*
910 ** julianday( TIMESTRING, MOD, MOD, ...)
911 **
912 ** Return the julian day number of the date specified in the arguments
913 */
914 static void juliandayFunc(
915 sqlite3_context *context,
916 int argc,
917 sqlite3_value **argv
918 ){
919 DateTime x;
920 if( isDate(context, argc, argv, &x)==0 ){
921 computeJD(&x);
922 sqlite3_result_double(context, x.iJD/86400000.0);
923 }
924 }
925
926 /*
927 ** datetime( TIMESTRING, MOD, MOD, ...)
928 **
929 ** Return YYYY-MM-DD HH:MM:SS
930 */
931 static void datetimeFunc(
932 sqlite3_context *context,
933 int argc,
934 sqlite3_value **argv
935 ){
936 DateTime x;
937 if( isDate(context, argc, argv, &x)==0 ){
938 char zBuf[100];
939 computeYMD_HMS(&x);
940 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
941 x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
942 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
943 }
944 }
945
946 /*
947 ** time( TIMESTRING, MOD, MOD, ...)
948 **
949 ** Return HH:MM:SS
950 */
951 static void timeFunc(
952 sqlite3_context *context,
953 int argc,
954 sqlite3_value **argv
955 ){
956 DateTime x;
957 if( isDate(context, argc, argv, &x)==0 ){
958 char zBuf[100];
959 computeHMS(&x);
960 sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
961 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
962 }
963 }
964
965 /*
966 ** date( TIMESTRING, MOD, MOD, ...)
967 **
968 ** Return YYYY-MM-DD
969 */
970 static void dateFunc(
971 sqlite3_context *context,
972 int argc,
973 sqlite3_value **argv
974 ){
975 DateTime x;
976 if( isDate(context, argc, argv, &x)==0 ){
977 char zBuf[100];
978 computeYMD(&x);
979 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
980 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
981 }
982 }
983
984 /*
985 ** strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
986 **
987 ** Return a string described by FORMAT. Conversions as follows:
988 **
989 ** %d day of month
990 ** %f ** fractional seconds SS.SSS
991 ** %H hour 00-24
992 ** %j day of year 000-366
993 ** %J ** julian day number
994 ** %m month 01-12
995 ** %M minute 00-59
996 ** %s seconds since 1970-01-01
997 ** %S seconds 00-59
998 ** %w day of week 0-6 sunday==0
999 ** %W week of year 00-53
1000 ** %Y year 0000-9999
1001 ** %% %
1002 */
1003 static void strftimeFunc(
1004 sqlite3_context *context,
1005 int argc,
1006 sqlite3_value **argv
1007 ){
1008 DateTime x;
1009 u64 n;
1010 size_t i,j;
1011 char *z;
1012 sqlite3 *db;
1013 const char *zFmt;
1014 char zBuf[100];
1015 if( argc==0 ) return;
1016 zFmt = (const char*)sqlite3_value_text(argv[0]);
1017 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
1018 db = sqlite3_context_db_handle(context);
1019 for(i=0, n=1; zFmt[i]; i++, n++){
1020 if( zFmt[i]=='%' ){
1021 switch( zFmt[i+1] ){
1022 case 'd':
1023 case 'H':
1024 case 'm':
1025 case 'M':
1026 case 'S':
1027 case 'W':
1028 n++;
1029 /* fall thru */
1030 case 'w':
1031 case '%':
1032 break;
1033 case 'f':
1034 n += 8;
1035 break;
1036 case 'j':
1037 n += 3;
1038 break;
1039 case 'Y':
1040 n += 8;
1041 break;
1042 case 's':
1043 case 'J':
1044 n += 50;
1045 break;
1046 default:
1047 return; /* ERROR. return a NULL */
1048 }
1049 i++;
1050 }
1051 }
1052 testcase( n==sizeof(zBuf)-1 );
1053 testcase( n==sizeof(zBuf) );
1054 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
1055 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
1056 if( n<sizeof(zBuf) ){
1057 z = zBuf;
1058 }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
1059 sqlite3_result_error_toobig(context);
1060 return;
1061 }else{
1062 z = sqlite3DbMallocRawNN(db, (int)n);
1063 if( z==0 ){
1064 sqlite3_result_error_nomem(context);
1065 return;
1066 }
1067 }
1068 computeJD(&x);
1069 computeYMD_HMS(&x);
1070 for(i=j=0; zFmt[i]; i++){
1071 if( zFmt[i]!='%' ){
1072 z[j++] = zFmt[i];
1073 }else{
1074 i++;
1075 switch( zFmt[i] ){
1076 case 'd': sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
1077 case 'f': {
1078 double s = x.s;
1079 if( s>59.999 ) s = 59.999;
1080 sqlite3_snprintf(7, &z[j],"%06.3f", s);
1081 j += sqlite3Strlen30(&z[j]);
1082 break;
1083 }
1084 case 'H': sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
1085 case 'W': /* Fall thru */
1086 case 'j': {
1087 int nDay; /* Number of days since 1st day of year */
1088 DateTime y = x;
1089 y.validJD = 0;
1090 y.M = 1;
1091 y.D = 1;
1092 computeJD(&y);
1093 nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
1094 if( zFmt[i]=='W' ){
1095 int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */
1096 wd = (int)(((x.iJD+43200000)/86400000)%7);
1097 sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
1098 j += 2;
1099 }else{
1100 sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
1101 j += 3;
1102 }
1103 break;
1104 }
1105 case 'J': {
1106 sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
1107 j+=sqlite3Strlen30(&z[j]);
1108 break;
1109 }
1110 case 'm': sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
1111 case 'M': sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
1112 case 's': {
1113 sqlite3_snprintf(30,&z[j],"%lld",
1114 (i64)(x.iJD/1000 - 21086676*(i64)10000));
1115 j += sqlite3Strlen30(&z[j]);
1116 break;
1117 }
1118 case 'S': sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
1119 case 'w': {
1120 z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
1121 break;
1122 }
1123 case 'Y': {
1124 sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
1125 break;
1126 }
1127 default: z[j++] = '%'; break;
1128 }
1129 }
1130 }
1131 z[j] = 0;
1132 sqlite3_result_text(context, z, -1,
1133 z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
1134 }
1135
1136 /*
1137 ** current_time()
1138 **
1139 ** This function returns the same value as time('now').
1140 */
1141 static void ctimeFunc(
1142 sqlite3_context *context,
1143 int NotUsed,
1144 sqlite3_value **NotUsed2
1145 ){
1146 UNUSED_PARAMETER2(NotUsed, NotUsed2);
1147 timeFunc(context, 0, 0);
1148 }
1149
1150 /*
1151 ** current_date()
1152 **
1153 ** This function returns the same value as date('now').
1154 */
1155 static void cdateFunc(
1156 sqlite3_context *context,
1157 int NotUsed,
1158 sqlite3_value **NotUsed2
1159 ){
1160 UNUSED_PARAMETER2(NotUsed, NotUsed2);
1161 dateFunc(context, 0, 0);
1162 }
1163
1164 /*
1165 ** current_timestamp()
1166 **
1167 ** This function returns the same value as datetime('now').
1168 */
1169 static void ctimestampFunc(
1170 sqlite3_context *context,
1171 int NotUsed,
1172 sqlite3_value **NotUsed2
1173 ){
1174 UNUSED_PARAMETER2(NotUsed, NotUsed2);
1175 datetimeFunc(context, 0, 0);
1176 }
1177 #endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
1178
1179 #ifdef SQLITE_OMIT_DATETIME_FUNCS
1180 /*
1181 ** If the library is compiled to omit the full-scale date and time
1182 ** handling (to get a smaller binary), the following minimal version
1183 ** of the functions current_time(), current_date() and current_timestamp()
1184 ** are included instead. This is to support column declarations that
1185 ** include "DEFAULT CURRENT_TIME" etc.
1186 **
1187 ** This function uses the C-library functions time(), gmtime()
1188 ** and strftime(). The format string to pass to strftime() is supplied
1189 ** as the user-data for the function.
1190 */
1191 static void currentTimeFunc(
1192 sqlite3_context *context,
1193 int argc,
1194 sqlite3_value **argv
1195 ){
1196 time_t t;
1197 char *zFormat = (char *)sqlite3_user_data(context);
1198 sqlite3_int64 iT;
1199 struct tm *pTm;
1200 struct tm sNow;
1201 char zBuf[20];
1202
1203 UNUSED_PARAMETER(argc);
1204 UNUSED_PARAMETER(argv);
1205
1206 iT = sqlite3StmtCurrentTime(context);
1207 if( iT<=0 ) return;
1208 t = iT/1000 - 10000*(sqlite3_int64)21086676;
1209 #if HAVE_GMTIME_R
1210 pTm = gmtime_r(&t, &sNow);
1211 #else
1212 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
1213 pTm = gmtime(&t);
1214 if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
1215 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
1216 #endif
1217 if( pTm ){
1218 strftime(zBuf, 20, zFormat, &sNow);
1219 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
1220 }
1221 }
1222 #endif
1223
1224 /*
1225 ** This function registered all of the above C functions as SQL
1226 ** functions. This should be the only routine in this file with
1227 ** external linkage.
1228 */
1229 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
1230 static FuncDef aDateTimeFuncs[] = {
1231 #ifndef SQLITE_OMIT_DATETIME_FUNCS
1232 DFUNCTION(julianday, -1, 0, 0, juliandayFunc ),
1233 DFUNCTION(date, -1, 0, 0, dateFunc ),
1234 DFUNCTION(time, -1, 0, 0, timeFunc ),
1235 DFUNCTION(datetime, -1, 0, 0, datetimeFunc ),
1236 DFUNCTION(strftime, -1, 0, 0, strftimeFunc ),
1237 DFUNCTION(current_time, 0, 0, 0, ctimeFunc ),
1238 DFUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
1239 DFUNCTION(current_date, 0, 0, 0, cdateFunc ),
1240 #else
1241 STR_FUNCTION(current_time, 0, "%H:%M:%S", 0, currentTimeFunc),
1242 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc),
1243 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
1244 #endif
1245 };
1246 sqlite3InsertBuiltinFuncs(aDateTimeFuncs, ArraySize(aDateTimeFuncs));
1247 }
1248
1249 /************** End of date.c ************************************************/
1250 /************** Begin file os.c **********************************************/
1251 /*
1252 ** 2005 November 29
1253 **
1254 ** The author disclaims copyright to this source code. In place of
1255 ** a legal notice, here is a blessing:
1256 **
1257 ** May you do good and not evil.
1258 ** May you find forgiveness for yourself and forgive others.
1259 ** May you share freely, never taking more than you give.
1260 **
1261 ******************************************************************************
1262 **
1263 ** This file contains OS interface code that is common to all
1264 ** architectures.
1265 */
1266 /* #include "sqliteInt.h" */
1267
1268 /*
1269 ** If we compile with the SQLITE_TEST macro set, then the following block
1270 ** of code will give us the ability to simulate a disk I/O error. This
1271 ** is used for testing the I/O recovery logic.
1272 */
1273 #if defined(SQLITE_TEST)
1274 SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Error s */
1275 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign erro rs */
1276 SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O e rror */
1277 SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persis t */
1278 SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
1279 SQLITE_API int sqlite3_diskfull_pending = 0;
1280 SQLITE_API int sqlite3_diskfull = 0;
1281 #endif /* defined(SQLITE_TEST) */
1282
1283 /*
1284 ** When testing, also keep a count of the number of open files.
1285 */
1286 #if defined(SQLITE_TEST)
1287 SQLITE_API int sqlite3_open_file_count = 0;
1288 #endif /* defined(SQLITE_TEST) */
1289
1290 /*
1291 ** The default SQLite sqlite3_vfs implementations do not allocate
1292 ** memory (actually, os_unix.c allocates a small amount of memory
1293 ** from within OsOpen()), but some third-party implementations may.
1294 ** So we test the effects of a malloc() failing and the sqlite3OsXXX()
1295 ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
1296 **
1297 ** The following functions are instrumented for malloc() failure
1298 ** testing:
1299 **
1300 ** sqlite3OsRead()
1301 ** sqlite3OsWrite()
1302 ** sqlite3OsSync()
1303 ** sqlite3OsFileSize()
1304 ** sqlite3OsLock()
1305 ** sqlite3OsCheckReservedLock()
1306 ** sqlite3OsFileControl()
1307 ** sqlite3OsShmMap()
1308 ** sqlite3OsOpen()
1309 ** sqlite3OsDelete()
1310 ** sqlite3OsAccess()
1311 ** sqlite3OsFullPathname()
1312 **
1313 */
1314 #if defined(SQLITE_TEST)
1315 SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
1316 #define DO_OS_MALLOC_TEST(x) \
1317 if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3JournalIsInMemory(x))) { \
1318 void *pTstAlloc = sqlite3Malloc(10); \
1319 if (!pTstAlloc) return SQLITE_IOERR_NOMEM_BKPT; \
1320 sqlite3_free(pTstAlloc); \
1321 }
1322 #else
1323 #define DO_OS_MALLOC_TEST(x)
1324 #endif
1325
1326 /*
1327 ** The following routines are convenience wrappers around methods
1328 ** of the sqlite3_file object. This is mostly just syntactic sugar. All
1329 ** of this would be completely automatic if SQLite were coded using
1330 ** C++ instead of plain old C.
1331 */
1332 SQLITE_PRIVATE void sqlite3OsClose(sqlite3_file *pId){
1333 if( pId->pMethods ){
1334 pId->pMethods->xClose(pId);
1335 pId->pMethods = 0;
1336 }
1337 }
1338 SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offs et){
1339 DO_OS_MALLOC_TEST(id);
1340 return id->pMethods->xRead(id, pBuf, amt, offset);
1341 }
1342 SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i 64 offset){
1343 DO_OS_MALLOC_TEST(id);
1344 return id->pMethods->xWrite(id, pBuf, amt, offset);
1345 }
1346 SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
1347 return id->pMethods->xTruncate(id, size);
1348 }
1349 SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
1350 DO_OS_MALLOC_TEST(id);
1351 return id->pMethods->xSync(id, flags);
1352 }
1353 SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
1354 DO_OS_MALLOC_TEST(id);
1355 return id->pMethods->xFileSize(id, pSize);
1356 }
1357 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
1358 DO_OS_MALLOC_TEST(id);
1359 return id->pMethods->xLock(id, lockType);
1360 }
1361 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
1362 return id->pMethods->xUnlock(id, lockType);
1363 }
1364 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
1365 DO_OS_MALLOC_TEST(id);
1366 return id->pMethods->xCheckReservedLock(id, pResOut);
1367 }
1368
1369 /*
1370 ** Use sqlite3OsFileControl() when we are doing something that might fail
1371 ** and we need to know about the failures. Use sqlite3OsFileControlHint()
1372 ** when simply tossing information over the wall to the VFS and we do not
1373 ** really care if the VFS receives and understands the information since it
1374 ** is only a hint and can be safely ignored. The sqlite3OsFileControlHint()
1375 ** routine has no return value since the return value would be meaningless.
1376 */
1377 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
1378 #ifdef SQLITE_TEST
1379 if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){
1380 /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
1381 ** is using a regular VFS, it is called after the corresponding
1382 ** transaction has been committed. Injecting a fault at this point
1383 ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
1384 ** but the transaction is committed anyway.
1385 **
1386 ** The core must call OsFileControl() though, not OsFileControlHint(),
1387 ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
1388 ** means the commit really has failed and an error should be returned
1389 ** to the user. */
1390 DO_OS_MALLOC_TEST(id);
1391 }
1392 #endif
1393 return id->pMethods->xFileControl(id, op, pArg);
1394 }
1395 SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pAr g){
1396 (void)id->pMethods->xFileControl(id, op, pArg);
1397 }
1398
1399 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
1400 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
1401 return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
1402 }
1403 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
1404 return id->pMethods->xDeviceCharacteristics(id);
1405 }
1406 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int fla gs){
1407 return id->pMethods->xShmLock(id, offset, n, flags);
1408 }
1409 SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
1410 id->pMethods->xShmBarrier(id);
1411 }
1412 SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
1413 return id->pMethods->xShmUnmap(id, deleteFlag);
1414 }
1415 SQLITE_PRIVATE int sqlite3OsShmMap(
1416 sqlite3_file *id, /* Database file handle */
1417 int iPage,
1418 int pgsz,
1419 int bExtend, /* True to extend file if necessary */
1420 void volatile **pp /* OUT: Pointer to mapping */
1421 ){
1422 DO_OS_MALLOC_TEST(id);
1423 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
1424 }
1425
1426 #if SQLITE_MAX_MMAP_SIZE>0
1427 /* The real implementation of xFetch and xUnfetch */
1428 SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **p p){
1429 DO_OS_MALLOC_TEST(id);
1430 return id->pMethods->xFetch(id, iOff, iAmt, pp);
1431 }
1432 SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
1433 return id->pMethods->xUnfetch(id, iOff, p);
1434 }
1435 #else
1436 /* No-op stubs to use when memory-mapped I/O is disabled */
1437 SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **p p){
1438 *pp = 0;
1439 return SQLITE_OK;
1440 }
1441 SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
1442 return SQLITE_OK;
1443 }
1444 #endif
1445
1446 /*
1447 ** The next group of routines are convenience wrappers around the
1448 ** VFS methods.
1449 */
1450 SQLITE_PRIVATE int sqlite3OsOpen(
1451 sqlite3_vfs *pVfs,
1452 const char *zPath,
1453 sqlite3_file *pFile,
1454 int flags,
1455 int *pFlagsOut
1456 ){
1457 int rc;
1458 DO_OS_MALLOC_TEST(0);
1459 /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
1460 ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example,
1461 ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
1462 ** reaching the VFS. */
1463 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut);
1464 assert( rc==SQLITE_OK || pFile->pMethods==0 );
1465 return rc;
1466 }
1467 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dir Sync){
1468 DO_OS_MALLOC_TEST(0);
1469 assert( dirSync==0 || dirSync==1 );
1470 return pVfs->xDelete(pVfs, zPath, dirSync);
1471 }
1472 SQLITE_PRIVATE int sqlite3OsAccess(
1473 sqlite3_vfs *pVfs,
1474 const char *zPath,
1475 int flags,
1476 int *pResOut
1477 ){
1478 DO_OS_MALLOC_TEST(0);
1479 return pVfs->xAccess(pVfs, zPath, flags, pResOut);
1480 }
1481 SQLITE_PRIVATE int sqlite3OsFullPathname(
1482 sqlite3_vfs *pVfs,
1483 const char *zPath,
1484 int nPathOut,
1485 char *zPathOut
1486 ){
1487 DO_OS_MALLOC_TEST(0);
1488 zPathOut[0] = 0;
1489 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
1490 }
1491 #ifndef SQLITE_OMIT_LOAD_EXTENSION
1492 SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
1493 return pVfs->xDlOpen(pVfs, zPath);
1494 }
1495 SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut ){
1496 pVfs->xDlError(pVfs, nByte, zBufOut);
1497 }
1498 SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
1499 return pVfs->xDlSym(pVfs, pHdle, zSym);
1500 }
1501 SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
1502 pVfs->xDlClose(pVfs, pHandle);
1503 }
1504 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
1505 SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufO ut){
1506 return pVfs->xRandomness(pVfs, nByte, zBufOut);
1507 }
1508 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
1509 return pVfs->xSleep(pVfs, nMicro);
1510 }
1511 SQLITE_PRIVATE int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
1512 return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
1513 }
1514 SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p TimeOut){
1515 int rc;
1516 /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
1517 ** method to get the current date and time if that method is available
1518 ** (if iVersion is 2 or greater and the function pointer is not NULL) and
1519 ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
1520 ** unavailable.
1521 */
1522 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
1523 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
1524 }else{
1525 double r;
1526 rc = pVfs->xCurrentTime(pVfs, &r);
1527 *pTimeOut = (sqlite3_int64)(r*86400000.0);
1528 }
1529 return rc;
1530 }
1531
1532 SQLITE_PRIVATE int sqlite3OsOpenMalloc(
1533 sqlite3_vfs *pVfs,
1534 const char *zFile,
1535 sqlite3_file **ppFile,
1536 int flags,
1537 int *pOutFlags
1538 ){
1539 int rc;
1540 sqlite3_file *pFile;
1541 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
1542 if( pFile ){
1543 rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
1544 if( rc!=SQLITE_OK ){
1545 sqlite3_free(pFile);
1546 }else{
1547 *ppFile = pFile;
1548 }
1549 }else{
1550 rc = SQLITE_NOMEM_BKPT;
1551 }
1552 return rc;
1553 }
1554 SQLITE_PRIVATE void sqlite3OsCloseFree(sqlite3_file *pFile){
1555 assert( pFile );
1556 sqlite3OsClose(pFile);
1557 sqlite3_free(pFile);
1558 }
1559
1560 /*
1561 ** This function is a wrapper around the OS specific implementation of
1562 ** sqlite3_os_init(). The purpose of the wrapper is to provide the
1563 ** ability to simulate a malloc failure, so that the handling of an
1564 ** error in sqlite3_os_init() by the upper layers can be tested.
1565 */
1566 SQLITE_PRIVATE int sqlite3OsInit(void){
1567 void *p = sqlite3_malloc(10);
1568 if( p==0 ) return SQLITE_NOMEM_BKPT;
1569 sqlite3_free(p);
1570 return sqlite3_os_init();
1571 }
1572
1573 /*
1574 ** The list of all registered VFS implementations.
1575 */
1576 static sqlite3_vfs * SQLITE_WSD vfsList = 0;
1577 #define vfsList GLOBAL(sqlite3_vfs *, vfsList)
1578
1579 /*
1580 ** Locate a VFS by name. If no name is given, simply return the
1581 ** first VFS on the list.
1582 */
1583 SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
1584 sqlite3_vfs *pVfs = 0;
1585 #if SQLITE_THREADSAFE
1586 sqlite3_mutex *mutex;
1587 #endif
1588 #ifndef SQLITE_OMIT_AUTOINIT
1589 int rc = sqlite3_initialize();
1590 if( rc ) return 0;
1591 #endif
1592 #if SQLITE_THREADSAFE
1593 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
1594 #endif
1595 sqlite3_mutex_enter(mutex);
1596 for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
1597 if( zVfs==0 ) break;
1598 if( strcmp(zVfs, pVfs->zName)==0 ) break;
1599 }
1600 sqlite3_mutex_leave(mutex);
1601 return pVfs;
1602 }
1603
1604 /*
1605 ** Unlink a VFS from the linked list
1606 */
1607 static void vfsUnlink(sqlite3_vfs *pVfs){
1608 assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
1609 if( pVfs==0 ){
1610 /* No-op */
1611 }else if( vfsList==pVfs ){
1612 vfsList = pVfs->pNext;
1613 }else if( vfsList ){
1614 sqlite3_vfs *p = vfsList;
1615 while( p->pNext && p->pNext!=pVfs ){
1616 p = p->pNext;
1617 }
1618 if( p->pNext==pVfs ){
1619 p->pNext = pVfs->pNext;
1620 }
1621 }
1622 }
1623
1624 /*
1625 ** Register a VFS with the system. It is harmless to register the same
1626 ** VFS multiple times. The new VFS becomes the default if makeDflt is
1627 ** true.
1628 */
1629 SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
1630 MUTEX_LOGIC(sqlite3_mutex *mutex;)
1631 #ifndef SQLITE_OMIT_AUTOINIT
1632 int rc = sqlite3_initialize();
1633 if( rc ) return rc;
1634 #endif
1635 #ifdef SQLITE_ENABLE_API_ARMOR
1636 if( pVfs==0 ) return SQLITE_MISUSE_BKPT;
1637 #endif
1638
1639 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
1640 sqlite3_mutex_enter(mutex);
1641 vfsUnlink(pVfs);
1642 if( makeDflt || vfsList==0 ){
1643 pVfs->pNext = vfsList;
1644 vfsList = pVfs;
1645 }else{
1646 pVfs->pNext = vfsList->pNext;
1647 vfsList->pNext = pVfs;
1648 }
1649 assert(vfsList);
1650 sqlite3_mutex_leave(mutex);
1651 return SQLITE_OK;
1652 }
1653
1654 /*
1655 ** Unregister a VFS so that it is no longer accessible.
1656 */
1657 SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
1658 #if SQLITE_THREADSAFE
1659 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
1660 #endif
1661 sqlite3_mutex_enter(mutex);
1662 vfsUnlink(pVfs);
1663 sqlite3_mutex_leave(mutex);
1664 return SQLITE_OK;
1665 }
1666
1667 /************** End of os.c **************************************************/
1668 /************** Begin file fault.c *******************************************/
1669 /*
1670 ** 2008 Jan 22
1671 **
1672 ** The author disclaims copyright to this source code. In place of
1673 ** a legal notice, here is a blessing:
1674 **
1675 ** May you do good and not evil.
1676 ** May you find forgiveness for yourself and forgive others.
1677 ** May you share freely, never taking more than you give.
1678 **
1679 *************************************************************************
1680 **
1681 ** This file contains code to support the concept of "benign"
1682 ** malloc failures (when the xMalloc() or xRealloc() method of the
1683 ** sqlite3_mem_methods structure fails to allocate a block of memory
1684 ** and returns 0).
1685 **
1686 ** Most malloc failures are non-benign. After they occur, SQLite
1687 ** abandons the current operation and returns an error code (usually
1688 ** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
1689 ** fatal. For example, if a malloc fails while resizing a hash table, this
1690 ** is completely recoverable simply by not carrying out the resize. The
1691 ** hash table will continue to function normally. So a malloc failure
1692 ** during a hash table resize is a benign fault.
1693 */
1694
1695 /* #include "sqliteInt.h" */
1696
1697 #ifndef SQLITE_UNTESTABLE
1698
1699 /*
1700 ** Global variables.
1701 */
1702 typedef struct BenignMallocHooks BenignMallocHooks;
1703 static SQLITE_WSD struct BenignMallocHooks {
1704 void (*xBenignBegin)(void);
1705 void (*xBenignEnd)(void);
1706 } sqlite3Hooks = { 0, 0 };
1707
1708 /* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
1709 ** structure. If writable static data is unsupported on the target,
1710 ** we have to locate the state vector at run-time. In the more common
1711 ** case where writable static data is supported, wsdHooks can refer directly
1712 ** to the "sqlite3Hooks" state vector declared above.
1713 */
1714 #ifdef SQLITE_OMIT_WSD
1715 # define wsdHooksInit \
1716 BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
1717 # define wsdHooks x[0]
1718 #else
1719 # define wsdHooksInit
1720 # define wsdHooks sqlite3Hooks
1721 #endif
1722
1723
1724 /*
1725 ** Register hooks to call when sqlite3BeginBenignMalloc() and
1726 ** sqlite3EndBenignMalloc() are called, respectively.
1727 */
1728 SQLITE_PRIVATE void sqlite3BenignMallocHooks(
1729 void (*xBenignBegin)(void),
1730 void (*xBenignEnd)(void)
1731 ){
1732 wsdHooksInit;
1733 wsdHooks.xBenignBegin = xBenignBegin;
1734 wsdHooks.xBenignEnd = xBenignEnd;
1735 }
1736
1737 /*
1738 ** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
1739 ** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
1740 ** indicates that subsequent malloc failures are non-benign.
1741 */
1742 SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void){
1743 wsdHooksInit;
1744 if( wsdHooks.xBenignBegin ){
1745 wsdHooks.xBenignBegin();
1746 }
1747 }
1748 SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
1749 wsdHooksInit;
1750 if( wsdHooks.xBenignEnd ){
1751 wsdHooks.xBenignEnd();
1752 }
1753 }
1754
1755 #endif /* #ifndef SQLITE_UNTESTABLE */
1756
1757 /************** End of fault.c ***********************************************/
1758 /************** Begin file mem0.c ********************************************/
1759 /*
1760 ** 2008 October 28
1761 **
1762 ** The author disclaims copyright to this source code. In place of
1763 ** a legal notice, here is a blessing:
1764 **
1765 ** May you do good and not evil.
1766 ** May you find forgiveness for yourself and forgive others.
1767 ** May you share freely, never taking more than you give.
1768 **
1769 *************************************************************************
1770 **
1771 ** This file contains a no-op memory allocation drivers for use when
1772 ** SQLITE_ZERO_MALLOC is defined. The allocation drivers implemented
1773 ** here always fail. SQLite will not operate with these drivers. These
1774 ** are merely placeholders. Real drivers must be substituted using
1775 ** sqlite3_config() before SQLite will operate.
1776 */
1777 /* #include "sqliteInt.h" */
1778
1779 /*
1780 ** This version of the memory allocator is the default. It is
1781 ** used when no other memory allocator is specified using compile-time
1782 ** macros.
1783 */
1784 #ifdef SQLITE_ZERO_MALLOC
1785
1786 /*
1787 ** No-op versions of all memory allocation routines
1788 */
1789 static void *sqlite3MemMalloc(int nByte){ return 0; }
1790 static void sqlite3MemFree(void *pPrior){ return; }
1791 static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
1792 static int sqlite3MemSize(void *pPrior){ return 0; }
1793 static int sqlite3MemRoundup(int n){ return n; }
1794 static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; }
1795 static void sqlite3MemShutdown(void *NotUsed){ return; }
1796
1797 /*
1798 ** This routine is the only routine in this file with external linkage.
1799 **
1800 ** Populate the low-level memory allocation function pointers in
1801 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
1802 */
1803 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
1804 static const sqlite3_mem_methods defaultMethods = {
1805 sqlite3MemMalloc,
1806 sqlite3MemFree,
1807 sqlite3MemRealloc,
1808 sqlite3MemSize,
1809 sqlite3MemRoundup,
1810 sqlite3MemInit,
1811 sqlite3MemShutdown,
1812 0
1813 };
1814 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
1815 }
1816
1817 #endif /* SQLITE_ZERO_MALLOC */
1818
1819 /************** End of mem0.c ************************************************/
1820 /************** Begin file mem1.c ********************************************/
1821 /*
1822 ** 2007 August 14
1823 **
1824 ** The author disclaims copyright to this source code. In place of
1825 ** a legal notice, here is a blessing:
1826 **
1827 ** May you do good and not evil.
1828 ** May you find forgiveness for yourself and forgive others.
1829 ** May you share freely, never taking more than you give.
1830 **
1831 *************************************************************************
1832 **
1833 ** This file contains low-level memory allocation drivers for when
1834 ** SQLite will use the standard C-library malloc/realloc/free interface
1835 ** to obtain the memory it needs.
1836 **
1837 ** This file contains implementations of the low-level memory allocation
1838 ** routines specified in the sqlite3_mem_methods object. The content of
1839 ** this file is only used if SQLITE_SYSTEM_MALLOC is defined. The
1840 ** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the
1841 ** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined. The
1842 ** default configuration is to use memory allocation routines in this
1843 ** file.
1844 **
1845 ** C-preprocessor macro summary:
1846 **
1847 ** HAVE_MALLOC_USABLE_SIZE The configure script sets this symbol if
1848 ** the malloc_usable_size() interface exists
1849 ** on the target platform. Or, this symbol
1850 ** can be set manually, if desired.
1851 ** If an equivalent interface exists by
1852 ** a different name, using a separate -D
1853 ** option to rename it.
1854 **
1855 ** SQLITE_WITHOUT_ZONEMALLOC Some older macs lack support for the zone
1856 ** memory allocator. Set this symbol to enable
1857 ** building on older macs.
1858 **
1859 ** SQLITE_WITHOUT_MSIZE Set this symbol to disable the use of
1860 ** _msize() on windows systems. This might
1861 ** be necessary when compiling for Delphi,
1862 ** for example.
1863 */
1864 /* #include "sqliteInt.h" */
1865
1866 /*
1867 ** This version of the memory allocator is the default. It is
1868 ** used when no other memory allocator is specified using compile-time
1869 ** macros.
1870 */
1871 #ifdef SQLITE_SYSTEM_MALLOC
1872 #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
1873
1874 /*
1875 ** Use the zone allocator available on apple products unless the
1876 ** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
1877 */
1878 #include <sys/sysctl.h>
1879 #include <malloc/malloc.h>
1880 #include <libkern/OSAtomic.h>
1881 static malloc_zone_t* _sqliteZone_;
1882 #define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
1883 #define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
1884 #define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
1885 #define SQLITE_MALLOCSIZE(x) \
1886 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
1887
1888 #else /* if not __APPLE__ */
1889
1890 /*
1891 ** Use standard C library malloc and free on non-Apple systems.
1892 ** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
1893 */
1894 #define SQLITE_MALLOC(x) malloc(x)
1895 #define SQLITE_FREE(x) free(x)
1896 #define SQLITE_REALLOC(x,y) realloc((x),(y))
1897
1898 /*
1899 ** The malloc.h header file is needed for malloc_usable_size() function
1900 ** on some systems (e.g. Linux).
1901 */
1902 #if HAVE_MALLOC_H && HAVE_MALLOC_USABLE_SIZE
1903 # define SQLITE_USE_MALLOC_H 1
1904 # define SQLITE_USE_MALLOC_USABLE_SIZE 1
1905 /*
1906 ** The MSVCRT has malloc_usable_size(), but it is called _msize(). The
1907 ** use of _msize() is automatic, but can be disabled by compiling with
1908 ** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
1909 ** the malloc.h header file.
1910 */
1911 #elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
1912 # define SQLITE_USE_MALLOC_H
1913 # define SQLITE_USE_MSIZE
1914 #endif
1915
1916 /*
1917 ** Include the malloc.h header file, if necessary. Also set define macro
1918 ** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize()
1919 ** for MSVC and malloc_usable_size() for most other systems (e.g. Linux).
1920 ** The memory size function can always be overridden manually by defining
1921 ** the macro SQLITE_MALLOCSIZE to the desired function name.
1922 */
1923 #if defined(SQLITE_USE_MALLOC_H)
1924 # include <malloc.h>
1925 # if defined(SQLITE_USE_MALLOC_USABLE_SIZE)
1926 # if !defined(SQLITE_MALLOCSIZE)
1927 # define SQLITE_MALLOCSIZE(x) malloc_usable_size(x)
1928 # endif
1929 # elif defined(SQLITE_USE_MSIZE)
1930 # if !defined(SQLITE_MALLOCSIZE)
1931 # define SQLITE_MALLOCSIZE _msize
1932 # endif
1933 # endif
1934 #endif /* defined(SQLITE_USE_MALLOC_H) */
1935
1936 #endif /* __APPLE__ or not __APPLE__ */
1937
1938 /*
1939 ** Like malloc(), but remember the size of the allocation
1940 ** so that we can find it later using sqlite3MemSize().
1941 **
1942 ** For this low-level routine, we are guaranteed that nByte>0 because
1943 ** cases of nByte<=0 will be intercepted and dealt with by higher level
1944 ** routines.
1945 */
1946 static void *sqlite3MemMalloc(int nByte){
1947 #ifdef SQLITE_MALLOCSIZE
1948 void *p;
1949 testcase( ROUND8(nByte)==nByte );
1950 p = SQLITE_MALLOC( nByte );
1951 if( p==0 ){
1952 testcase( sqlite3GlobalConfig.xLog!=0 );
1953 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
1954 }
1955 return p;
1956 #else
1957 sqlite3_int64 *p;
1958 assert( nByte>0 );
1959 testcase( ROUND8(nByte)!=nByte );
1960 p = SQLITE_MALLOC( nByte+8 );
1961 if( p ){
1962 p[0] = nByte;
1963 p++;
1964 }else{
1965 testcase( sqlite3GlobalConfig.xLog!=0 );
1966 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
1967 }
1968 return (void *)p;
1969 #endif
1970 }
1971
1972 /*
1973 ** Like free() but works for allocations obtained from sqlite3MemMalloc()
1974 ** or sqlite3MemRealloc().
1975 **
1976 ** For this low-level routine, we already know that pPrior!=0 since
1977 ** cases where pPrior==0 will have been intecepted and dealt with
1978 ** by higher-level routines.
1979 */
1980 static void sqlite3MemFree(void *pPrior){
1981 #ifdef SQLITE_MALLOCSIZE
1982 SQLITE_FREE(pPrior);
1983 #else
1984 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
1985 assert( pPrior!=0 );
1986 p--;
1987 SQLITE_FREE(p);
1988 #endif
1989 }
1990
1991 /*
1992 ** Report the allocated size of a prior return from xMalloc()
1993 ** or xRealloc().
1994 */
1995 static int sqlite3MemSize(void *pPrior){
1996 #ifdef SQLITE_MALLOCSIZE
1997 assert( pPrior!=0 );
1998 return (int)SQLITE_MALLOCSIZE(pPrior);
1999 #else
2000 sqlite3_int64 *p;
2001 assert( pPrior!=0 );
2002 p = (sqlite3_int64*)pPrior;
2003 p--;
2004 return (int)p[0];
2005 #endif
2006 }
2007
2008 /*
2009 ** Like realloc(). Resize an allocation previously obtained from
2010 ** sqlite3MemMalloc().
2011 **
2012 ** For this low-level interface, we know that pPrior!=0. Cases where
2013 ** pPrior==0 while have been intercepted by higher-level routine and
2014 ** redirected to xMalloc. Similarly, we know that nByte>0 because
2015 ** cases where nByte<=0 will have been intercepted by higher-level
2016 ** routines and redirected to xFree.
2017 */
2018 static void *sqlite3MemRealloc(void *pPrior, int nByte){
2019 #ifdef SQLITE_MALLOCSIZE
2020 void *p = SQLITE_REALLOC(pPrior, nByte);
2021 if( p==0 ){
2022 testcase( sqlite3GlobalConfig.xLog!=0 );
2023 sqlite3_log(SQLITE_NOMEM,
2024 "failed memory resize %u to %u bytes",
2025 SQLITE_MALLOCSIZE(pPrior), nByte);
2026 }
2027 return p;
2028 #else
2029 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
2030 assert( pPrior!=0 && nByte>0 );
2031 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
2032 p--;
2033 p = SQLITE_REALLOC(p, nByte+8 );
2034 if( p ){
2035 p[0] = nByte;
2036 p++;
2037 }else{
2038 testcase( sqlite3GlobalConfig.xLog!=0 );
2039 sqlite3_log(SQLITE_NOMEM,
2040 "failed memory resize %u to %u bytes",
2041 sqlite3MemSize(pPrior), nByte);
2042 }
2043 return (void*)p;
2044 #endif
2045 }
2046
2047 /*
2048 ** Round up a request size to the next valid allocation size.
2049 */
2050 static int sqlite3MemRoundup(int n){
2051 return ROUND8(n);
2052 }
2053
2054 /*
2055 ** Initialize this module.
2056 */
2057 static int sqlite3MemInit(void *NotUsed){
2058 #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
2059 int cpuCount;
2060 size_t len;
2061 if( _sqliteZone_ ){
2062 return SQLITE_OK;
2063 }
2064 len = sizeof(cpuCount);
2065 /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
2066 sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
2067 if( cpuCount>1 ){
2068 /* defer MT decisions to system malloc */
2069 _sqliteZone_ = malloc_default_zone();
2070 }else{
2071 /* only 1 core, use our own zone to contention over global locks,
2072 ** e.g. we have our own dedicated locks */
2073 bool success;
2074 malloc_zone_t* newzone = malloc_create_zone(4096, 0);
2075 malloc_set_zone_name(newzone, "Sqlite_Heap");
2076 do{
2077 success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone,
2078 (void * volatile *)&_sqliteZone_);
2079 }while(!_sqliteZone_);
2080 if( !success ){
2081 /* somebody registered a zone first */
2082 malloc_destroy_zone(newzone);
2083 }
2084 }
2085 #endif
2086 UNUSED_PARAMETER(NotUsed);
2087 return SQLITE_OK;
2088 }
2089
2090 /*
2091 ** Deinitialize this module.
2092 */
2093 static void sqlite3MemShutdown(void *NotUsed){
2094 UNUSED_PARAMETER(NotUsed);
2095 return;
2096 }
2097
2098 /*
2099 ** This routine is the only routine in this file with external linkage.
2100 **
2101 ** Populate the low-level memory allocation function pointers in
2102 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
2103 */
2104 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
2105 static const sqlite3_mem_methods defaultMethods = {
2106 sqlite3MemMalloc,
2107 sqlite3MemFree,
2108 sqlite3MemRealloc,
2109 sqlite3MemSize,
2110 sqlite3MemRoundup,
2111 sqlite3MemInit,
2112 sqlite3MemShutdown,
2113 0
2114 };
2115 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
2116 }
2117
2118 #endif /* SQLITE_SYSTEM_MALLOC */
2119
2120 /************** End of mem1.c ************************************************/
2121 /************** Begin file mem2.c ********************************************/
2122 /*
2123 ** 2007 August 15
2124 **
2125 ** The author disclaims copyright to this source code. In place of
2126 ** a legal notice, here is a blessing:
2127 **
2128 ** May you do good and not evil.
2129 ** May you find forgiveness for yourself and forgive others.
2130 ** May you share freely, never taking more than you give.
2131 **
2132 *************************************************************************
2133 **
2134 ** This file contains low-level memory allocation drivers for when
2135 ** SQLite will use the standard C-library malloc/realloc/free interface
2136 ** to obtain the memory it needs while adding lots of additional debugging
2137 ** information to each allocation in order to help detect and fix memory
2138 ** leaks and memory usage errors.
2139 **
2140 ** This file contains implementations of the low-level memory allocation
2141 ** routines specified in the sqlite3_mem_methods object.
2142 */
2143 /* #include "sqliteInt.h" */
2144
2145 /*
2146 ** This version of the memory allocator is used only if the
2147 ** SQLITE_MEMDEBUG macro is defined
2148 */
2149 #ifdef SQLITE_MEMDEBUG
2150
2151 /*
2152 ** The backtrace functionality is only available with GLIBC
2153 */
2154 #ifdef __GLIBC__
2155 extern int backtrace(void**,int);
2156 extern void backtrace_symbols_fd(void*const*,int,int);
2157 #else
2158 # define backtrace(A,B) 1
2159 # define backtrace_symbols_fd(A,B,C)
2160 #endif
2161 /* #include <stdio.h> */
2162
2163 /*
2164 ** Each memory allocation looks like this:
2165 **
2166 ** ------------------------------------------------------------------------
2167 ** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard |
2168 ** ------------------------------------------------------------------------
2169 **
2170 ** The application code sees only a pointer to the allocation. We have
2171 ** to back up from the allocation pointer to find the MemBlockHdr. The
2172 ** MemBlockHdr tells us the size of the allocation and the number of
2173 ** backtrace pointers. There is also a guard word at the end of the
2174 ** MemBlockHdr.
2175 */
2176 struct MemBlockHdr {
2177 i64 iSize; /* Size of this allocation */
2178 struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */
2179 char nBacktrace; /* Number of backtraces on this alloc */
2180 char nBacktraceSlots; /* Available backtrace slots */
2181 u8 nTitle; /* Bytes of title; includes '\0' */
2182 u8 eType; /* Allocation type code */
2183 int iForeGuard; /* Guard word for sanity */
2184 };
2185
2186 /*
2187 ** Guard words
2188 */
2189 #define FOREGUARD 0x80F5E153
2190 #define REARGUARD 0xE4676B53
2191
2192 /*
2193 ** Number of malloc size increments to track.
2194 */
2195 #define NCSIZE 1000
2196
2197 /*
2198 ** All of the static variables used by this module are collected
2199 ** into a single structure named "mem". This is to keep the
2200 ** static variables organized and to reduce namespace pollution
2201 ** when this module is combined with other in the amalgamation.
2202 */
2203 static struct {
2204
2205 /*
2206 ** Mutex to control access to the memory allocation subsystem.
2207 */
2208 sqlite3_mutex *mutex;
2209
2210 /*
2211 ** Head and tail of a linked list of all outstanding allocations
2212 */
2213 struct MemBlockHdr *pFirst;
2214 struct MemBlockHdr *pLast;
2215
2216 /*
2217 ** The number of levels of backtrace to save in new allocations.
2218 */
2219 int nBacktrace;
2220 void (*xBacktrace)(int, int, void **);
2221
2222 /*
2223 ** Title text to insert in front of each block
2224 */
2225 int nTitle; /* Bytes of zTitle to save. Includes '\0' and padding */
2226 char zTitle[100]; /* The title text */
2227
2228 /*
2229 ** sqlite3MallocDisallow() increments the following counter.
2230 ** sqlite3MallocAllow() decrements it.
2231 */
2232 int disallow; /* Do not allow memory allocation */
2233
2234 /*
2235 ** Gather statistics on the sizes of memory allocations.
2236 ** nAlloc[i] is the number of allocation attempts of i*8
2237 ** bytes. i==NCSIZE is the number of allocation attempts for
2238 ** sizes more than NCSIZE*8 bytes.
2239 */
2240 int nAlloc[NCSIZE]; /* Total number of allocations */
2241 int nCurrent[NCSIZE]; /* Current number of allocations */
2242 int mxCurrent[NCSIZE]; /* Highwater mark for nCurrent */
2243
2244 } mem;
2245
2246
2247 /*
2248 ** Adjust memory usage statistics
2249 */
2250 static void adjustStats(int iSize, int increment){
2251 int i = ROUND8(iSize)/8;
2252 if( i>NCSIZE-1 ){
2253 i = NCSIZE - 1;
2254 }
2255 if( increment>0 ){
2256 mem.nAlloc[i]++;
2257 mem.nCurrent[i]++;
2258 if( mem.nCurrent[i]>mem.mxCurrent[i] ){
2259 mem.mxCurrent[i] = mem.nCurrent[i];
2260 }
2261 }else{
2262 mem.nCurrent[i]--;
2263 assert( mem.nCurrent[i]>=0 );
2264 }
2265 }
2266
2267 /*
2268 ** Given an allocation, find the MemBlockHdr for that allocation.
2269 **
2270 ** This routine checks the guards at either end of the allocation and
2271 ** if they are incorrect it asserts.
2272 */
2273 static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
2274 struct MemBlockHdr *p;
2275 int *pInt;
2276 u8 *pU8;
2277 int nReserve;
2278
2279 p = (struct MemBlockHdr*)pAllocation;
2280 p--;
2281 assert( p->iForeGuard==(int)FOREGUARD );
2282 nReserve = ROUND8(p->iSize);
2283 pInt = (int*)pAllocation;
2284 pU8 = (u8*)pAllocation;
2285 assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
2286 /* This checks any of the "extra" bytes allocated due
2287 ** to rounding up to an 8 byte boundary to ensure
2288 ** they haven't been overwritten.
2289 */
2290 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
2291 return p;
2292 }
2293
2294 /*
2295 ** Return the number of bytes currently allocated at address p.
2296 */
2297 static int sqlite3MemSize(void *p){
2298 struct MemBlockHdr *pHdr;
2299 if( !p ){
2300 return 0;
2301 }
2302 pHdr = sqlite3MemsysGetHeader(p);
2303 return (int)pHdr->iSize;
2304 }
2305
2306 /*
2307 ** Initialize the memory allocation subsystem.
2308 */
2309 static int sqlite3MemInit(void *NotUsed){
2310 UNUSED_PARAMETER(NotUsed);
2311 assert( (sizeof(struct MemBlockHdr)&7) == 0 );
2312 if( !sqlite3GlobalConfig.bMemstat ){
2313 /* If memory status is enabled, then the malloc.c wrapper will already
2314 ** hold the STATIC_MEM mutex when the routines here are invoked. */
2315 mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
2316 }
2317 return SQLITE_OK;
2318 }
2319
2320 /*
2321 ** Deinitialize the memory allocation subsystem.
2322 */
2323 static void sqlite3MemShutdown(void *NotUsed){
2324 UNUSED_PARAMETER(NotUsed);
2325 mem.mutex = 0;
2326 }
2327
2328 /*
2329 ** Round up a request size to the next valid allocation size.
2330 */
2331 static int sqlite3MemRoundup(int n){
2332 return ROUND8(n);
2333 }
2334
2335 /*
2336 ** Fill a buffer with pseudo-random bytes. This is used to preset
2337 ** the content of a new memory allocation to unpredictable values and
2338 ** to clear the content of a freed allocation to unpredictable values.
2339 */
2340 static void randomFill(char *pBuf, int nByte){
2341 unsigned int x, y, r;
2342 x = SQLITE_PTR_TO_INT(pBuf);
2343 y = nByte | 1;
2344 while( nByte >= 4 ){
2345 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
2346 y = y*1103515245 + 12345;
2347 r = x ^ y;
2348 *(int*)pBuf = r;
2349 pBuf += 4;
2350 nByte -= 4;
2351 }
2352 while( nByte-- > 0 ){
2353 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
2354 y = y*1103515245 + 12345;
2355 r = x ^ y;
2356 *(pBuf++) = r & 0xff;
2357 }
2358 }
2359
2360 /*
2361 ** Allocate nByte bytes of memory.
2362 */
2363 static void *sqlite3MemMalloc(int nByte){
2364 struct MemBlockHdr *pHdr;
2365 void **pBt;
2366 char *z;
2367 int *pInt;
2368 void *p = 0;
2369 int totalSize;
2370 int nReserve;
2371 sqlite3_mutex_enter(mem.mutex);
2372 assert( mem.disallow==0 );
2373 nReserve = ROUND8(nByte);
2374 totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
2375 mem.nBacktrace*sizeof(void*) + mem.nTitle;
2376 p = malloc(totalSize);
2377 if( p ){
2378 z = p;
2379 pBt = (void**)&z[mem.nTitle];
2380 pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
2381 pHdr->pNext = 0;
2382 pHdr->pPrev = mem.pLast;
2383 if( mem.pLast ){
2384 mem.pLast->pNext = pHdr;
2385 }else{
2386 mem.pFirst = pHdr;
2387 }
2388 mem.pLast = pHdr;
2389 pHdr->iForeGuard = FOREGUARD;
2390 pHdr->eType = MEMTYPE_HEAP;
2391 pHdr->nBacktraceSlots = mem.nBacktrace;
2392 pHdr->nTitle = mem.nTitle;
2393 if( mem.nBacktrace ){
2394 void *aAddr[40];
2395 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
2396 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
2397 assert(pBt[0]);
2398 if( mem.xBacktrace ){
2399 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
2400 }
2401 }else{
2402 pHdr->nBacktrace = 0;
2403 }
2404 if( mem.nTitle ){
2405 memcpy(z, mem.zTitle, mem.nTitle);
2406 }
2407 pHdr->iSize = nByte;
2408 adjustStats(nByte, +1);
2409 pInt = (int*)&pHdr[1];
2410 pInt[nReserve/sizeof(int)] = REARGUARD;
2411 randomFill((char*)pInt, nByte);
2412 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
2413 p = (void*)pInt;
2414 }
2415 sqlite3_mutex_leave(mem.mutex);
2416 return p;
2417 }
2418
2419 /*
2420 ** Free memory.
2421 */
2422 static void sqlite3MemFree(void *pPrior){
2423 struct MemBlockHdr *pHdr;
2424 void **pBt;
2425 char *z;
2426 assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0
2427 || mem.mutex!=0 );
2428 pHdr = sqlite3MemsysGetHeader(pPrior);
2429 pBt = (void**)pHdr;
2430 pBt -= pHdr->nBacktraceSlots;
2431 sqlite3_mutex_enter(mem.mutex);
2432 if( pHdr->pPrev ){
2433 assert( pHdr->pPrev->pNext==pHdr );
2434 pHdr->pPrev->pNext = pHdr->pNext;
2435 }else{
2436 assert( mem.pFirst==pHdr );
2437 mem.pFirst = pHdr->pNext;
2438 }
2439 if( pHdr->pNext ){
2440 assert( pHdr->pNext->pPrev==pHdr );
2441 pHdr->pNext->pPrev = pHdr->pPrev;
2442 }else{
2443 assert( mem.pLast==pHdr );
2444 mem.pLast = pHdr->pPrev;
2445 }
2446 z = (char*)pBt;
2447 z -= pHdr->nTitle;
2448 adjustStats((int)pHdr->iSize, -1);
2449 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
2450 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
2451 free(z);
2452 sqlite3_mutex_leave(mem.mutex);
2453 }
2454
2455 /*
2456 ** Change the size of an existing memory allocation.
2457 **
2458 ** For this debugging implementation, we *always* make a copy of the
2459 ** allocation into a new place in memory. In this way, if the
2460 ** higher level code is using pointer to the old allocation, it is
2461 ** much more likely to break and we are much more liking to find
2462 ** the error.
2463 */
2464 static void *sqlite3MemRealloc(void *pPrior, int nByte){
2465 struct MemBlockHdr *pOldHdr;
2466 void *pNew;
2467 assert( mem.disallow==0 );
2468 assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */
2469 pOldHdr = sqlite3MemsysGetHeader(pPrior);
2470 pNew = sqlite3MemMalloc(nByte);
2471 if( pNew ){
2472 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize));
2473 if( nByte>pOldHdr->iSize ){
2474 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
2475 }
2476 sqlite3MemFree(pPrior);
2477 }
2478 return pNew;
2479 }
2480
2481 /*
2482 ** Populate the low-level memory allocation function pointers in
2483 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
2484 */
2485 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
2486 static const sqlite3_mem_methods defaultMethods = {
2487 sqlite3MemMalloc,
2488 sqlite3MemFree,
2489 sqlite3MemRealloc,
2490 sqlite3MemSize,
2491 sqlite3MemRoundup,
2492 sqlite3MemInit,
2493 sqlite3MemShutdown,
2494 0
2495 };
2496 sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
2497 }
2498
2499 /*
2500 ** Set the "type" of an allocation.
2501 */
2502 SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){
2503 if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
2504 struct MemBlockHdr *pHdr;
2505 pHdr = sqlite3MemsysGetHeader(p);
2506 assert( pHdr->iForeGuard==FOREGUARD );
2507 pHdr->eType = eType;
2508 }
2509 }
2510
2511 /*
2512 ** Return TRUE if the mask of type in eType matches the type of the
2513 ** allocation p. Also return true if p==NULL.
2514 **
2515 ** This routine is designed for use within an assert() statement, to
2516 ** verify the type of an allocation. For example:
2517 **
2518 ** assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
2519 */
2520 SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){
2521 int rc = 1;
2522 if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
2523 struct MemBlockHdr *pHdr;
2524 pHdr = sqlite3MemsysGetHeader(p);
2525 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
2526 if( (pHdr->eType&eType)==0 ){
2527 rc = 0;
2528 }
2529 }
2530 return rc;
2531 }
2532
2533 /*
2534 ** Return TRUE if the mask of type in eType matches no bits of the type of the
2535 ** allocation p. Also return true if p==NULL.
2536 **
2537 ** This routine is designed for use within an assert() statement, to
2538 ** verify the type of an allocation. For example:
2539 **
2540 ** assert( sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
2541 */
2542 SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){
2543 int rc = 1;
2544 if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
2545 struct MemBlockHdr *pHdr;
2546 pHdr = sqlite3MemsysGetHeader(p);
2547 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
2548 if( (pHdr->eType&eType)!=0 ){
2549 rc = 0;
2550 }
2551 }
2552 return rc;
2553 }
2554
2555 /*
2556 ** Set the number of backtrace levels kept for each allocation.
2557 ** A value of zero turns off backtracing. The number is always rounded
2558 ** up to a multiple of 2.
2559 */
2560 SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){
2561 if( depth<0 ){ depth = 0; }
2562 if( depth>20 ){ depth = 20; }
2563 depth = (depth+1)&0xfe;
2564 mem.nBacktrace = depth;
2565 }
2566
2567 SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int , void **)){
2568 mem.xBacktrace = xBacktrace;
2569 }
2570
2571 /*
2572 ** Set the title string for subsequent allocations.
2573 */
2574 SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){
2575 unsigned int n = sqlite3Strlen30(zTitle) + 1;
2576 sqlite3_mutex_enter(mem.mutex);
2577 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
2578 memcpy(mem.zTitle, zTitle, n);
2579 mem.zTitle[n] = 0;
2580 mem.nTitle = ROUND8(n);
2581 sqlite3_mutex_leave(mem.mutex);
2582 }
2583
2584 SQLITE_PRIVATE void sqlite3MemdebugSync(){
2585 struct MemBlockHdr *pHdr;
2586 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
2587 void **pBt = (void**)pHdr;
2588 pBt -= pHdr->nBacktraceSlots;
2589 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
2590 }
2591 }
2592
2593 /*
2594 ** Open the file indicated and write a log of all unfreed memory
2595 ** allocations into that log.
2596 */
2597 SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){
2598 FILE *out;
2599 struct MemBlockHdr *pHdr;
2600 void **pBt;
2601 int i;
2602 out = fopen(zFilename, "w");
2603 if( out==0 ){
2604 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
2605 zFilename);
2606 return;
2607 }
2608 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
2609 char *z = (char*)pHdr;
2610 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
2611 fprintf(out, "**** %lld bytes at %p from %s ****\n",
2612 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
2613 if( pHdr->nBacktrace ){
2614 fflush(out);
2615 pBt = (void**)pHdr;
2616 pBt -= pHdr->nBacktraceSlots;
2617 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
2618 fprintf(out, "\n");
2619 }
2620 }
2621 fprintf(out, "COUNTS:\n");
2622 for(i=0; i<NCSIZE-1; i++){
2623 if( mem.nAlloc[i] ){
2624 fprintf(out, " %5d: %10d %10d %10d\n",
2625 i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
2626 }
2627 }
2628 if( mem.nAlloc[NCSIZE-1] ){
2629 fprintf(out, " %5d: %10d %10d %10d\n",
2630 NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
2631 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
2632 }
2633 fclose(out);
2634 }
2635
2636 /*
2637 ** Return the number of times sqlite3MemMalloc() has been called.
2638 */
2639 SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){
2640 int i;
2641 int nTotal = 0;
2642 for(i=0; i<NCSIZE; i++){
2643 nTotal += mem.nAlloc[i];
2644 }
2645 return nTotal;
2646 }
2647
2648
2649 #endif /* SQLITE_MEMDEBUG */
2650
2651 /************** End of mem2.c ************************************************/
2652 /************** Begin file mem3.c ********************************************/
2653 /*
2654 ** 2007 October 14
2655 **
2656 ** The author disclaims copyright to this source code. In place of
2657 ** a legal notice, here is a blessing:
2658 **
2659 ** May you do good and not evil.
2660 ** May you find forgiveness for yourself and forgive others.
2661 ** May you share freely, never taking more than you give.
2662 **
2663 *************************************************************************
2664 ** This file contains the C functions that implement a memory
2665 ** allocation subsystem for use by SQLite.
2666 **
2667 ** This version of the memory allocation subsystem omits all
2668 ** use of malloc(). The SQLite user supplies a block of memory
2669 ** before calling sqlite3_initialize() from which allocations
2670 ** are made and returned by the xMalloc() and xRealloc()
2671 ** implementations. Once sqlite3_initialize() has been called,
2672 ** the amount of memory available to SQLite is fixed and cannot
2673 ** be changed.
2674 **
2675 ** This version of the memory allocation subsystem is included
2676 ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
2677 */
2678 /* #include "sqliteInt.h" */
2679
2680 /*
2681 ** This version of the memory allocator is only built into the library
2682 ** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
2683 ** mean that the library will use a memory-pool by default, just that
2684 ** it is available. The mempool allocator is activated by calling
2685 ** sqlite3_config().
2686 */
2687 #ifdef SQLITE_ENABLE_MEMSYS3
2688
2689 /*
2690 ** Maximum size (in Mem3Blocks) of a "small" chunk.
2691 */
2692 #define MX_SMALL 10
2693
2694
2695 /*
2696 ** Number of freelist hash slots
2697 */
2698 #define N_HASH 61
2699
2700 /*
2701 ** A memory allocation (also called a "chunk") consists of two or
2702 ** more blocks where each block is 8 bytes. The first 8 bytes are
2703 ** a header that is not returned to the user.
2704 **
2705 ** A chunk is two or more blocks that is either checked out or
2706 ** free. The first block has format u.hdr. u.hdr.size4x is 4 times the
2707 ** size of the allocation in blocks if the allocation is free.
2708 ** The u.hdr.size4x&1 bit is true if the chunk is checked out and
2709 ** false if the chunk is on the freelist. The u.hdr.size4x&2 bit
2710 ** is true if the previous chunk is checked out and false if the
2711 ** previous chunk is free. The u.hdr.prevSize field is the size of
2712 ** the previous chunk in blocks if the previous chunk is on the
2713 ** freelist. If the previous chunk is checked out, then
2714 ** u.hdr.prevSize can be part of the data for that chunk and should
2715 ** not be read or written.
2716 **
2717 ** We often identify a chunk by its index in mem3.aPool[]. When
2718 ** this is done, the chunk index refers to the second block of
2719 ** the chunk. In this way, the first chunk has an index of 1.
2720 ** A chunk index of 0 means "no such chunk" and is the equivalent
2721 ** of a NULL pointer.
2722 **
2723 ** The second block of free chunks is of the form u.list. The
2724 ** two fields form a double-linked list of chunks of related sizes.
2725 ** Pointers to the head of the list are stored in mem3.aiSmall[]
2726 ** for smaller chunks and mem3.aiHash[] for larger chunks.
2727 **
2728 ** The second block of a chunk is user data if the chunk is checked
2729 ** out. If a chunk is checked out, the user data may extend into
2730 ** the u.hdr.prevSize value of the following chunk.
2731 */
2732 typedef struct Mem3Block Mem3Block;
2733 struct Mem3Block {
2734 union {
2735 struct {
2736 u32 prevSize; /* Size of previous chunk in Mem3Block elements */
2737 u32 size4x; /* 4x the size of current chunk in Mem3Block elements */
2738 } hdr;
2739 struct {
2740 u32 next; /* Index in mem3.aPool[] of next free chunk */
2741 u32 prev; /* Index in mem3.aPool[] of previous free chunk */
2742 } list;
2743 } u;
2744 };
2745
2746 /*
2747 ** All of the static variables used by this module are collected
2748 ** into a single structure named "mem3". This is to keep the
2749 ** static variables organized and to reduce namespace pollution
2750 ** when this module is combined with other in the amalgamation.
2751 */
2752 static SQLITE_WSD struct Mem3Global {
2753 /*
2754 ** Memory available for allocation. nPool is the size of the array
2755 ** (in Mem3Blocks) pointed to by aPool less 2.
2756 */
2757 u32 nPool;
2758 Mem3Block *aPool;
2759
2760 /*
2761 ** True if we are evaluating an out-of-memory callback.
2762 */
2763 int alarmBusy;
2764
2765 /*
2766 ** Mutex to control access to the memory allocation subsystem.
2767 */
2768 sqlite3_mutex *mutex;
2769
2770 /*
2771 ** The minimum amount of free space that we have seen.
2772 */
2773 u32 mnMaster;
2774
2775 /*
2776 ** iMaster is the index of the master chunk. Most new allocations
2777 ** occur off of this chunk. szMaster is the size (in Mem3Blocks)
2778 ** of the current master. iMaster is 0 if there is not master chunk.
2779 ** The master chunk is not in either the aiHash[] or aiSmall[].
2780 */
2781 u32 iMaster;
2782 u32 szMaster;
2783
2784 /*
2785 ** Array of lists of free blocks according to the block size
2786 ** for smaller chunks, or a hash on the block size for larger
2787 ** chunks.
2788 */
2789 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
2790 u32 aiHash[N_HASH]; /* For sizes MX_SMALL+1 and larger */
2791 } mem3 = { 97535575 };
2792
2793 #define mem3 GLOBAL(struct Mem3Global, mem3)
2794
2795 /*
2796 ** Unlink the chunk at mem3.aPool[i] from list it is currently
2797 ** on. *pRoot is the list that i is a member of.
2798 */
2799 static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
2800 u32 next = mem3.aPool[i].u.list.next;
2801 u32 prev = mem3.aPool[i].u.list.prev;
2802 assert( sqlite3_mutex_held(mem3.mutex) );
2803 if( prev==0 ){
2804 *pRoot = next;
2805 }else{
2806 mem3.aPool[prev].u.list.next = next;
2807 }
2808 if( next ){
2809 mem3.aPool[next].u.list.prev = prev;
2810 }
2811 mem3.aPool[i].u.list.next = 0;
2812 mem3.aPool[i].u.list.prev = 0;
2813 }
2814
2815 /*
2816 ** Unlink the chunk at index i from
2817 ** whatever list is currently a member of.
2818 */
2819 static void memsys3Unlink(u32 i){
2820 u32 size, hash;
2821 assert( sqlite3_mutex_held(mem3.mutex) );
2822 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
2823 assert( i>=1 );
2824 size = mem3.aPool[i-1].u.hdr.size4x/4;
2825 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
2826 assert( size>=2 );
2827 if( size <= MX_SMALL ){
2828 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
2829 }else{
2830 hash = size % N_HASH;
2831 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
2832 }
2833 }
2834
2835 /*
2836 ** Link the chunk at mem3.aPool[i] so that is on the list rooted
2837 ** at *pRoot.
2838 */
2839 static void memsys3LinkIntoList(u32 i, u32 *pRoot){
2840 assert( sqlite3_mutex_held(mem3.mutex) );
2841 mem3.aPool[i].u.list.next = *pRoot;
2842 mem3.aPool[i].u.list.prev = 0;
2843 if( *pRoot ){
2844 mem3.aPool[*pRoot].u.list.prev = i;
2845 }
2846 *pRoot = i;
2847 }
2848
2849 /*
2850 ** Link the chunk at index i into either the appropriate
2851 ** small chunk list, or into the large chunk hash table.
2852 */
2853 static void memsys3Link(u32 i){
2854 u32 size, hash;
2855 assert( sqlite3_mutex_held(mem3.mutex) );
2856 assert( i>=1 );
2857 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
2858 size = mem3.aPool[i-1].u.hdr.size4x/4;
2859 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
2860 assert( size>=2 );
2861 if( size <= MX_SMALL ){
2862 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
2863 }else{
2864 hash = size % N_HASH;
2865 memsys3LinkIntoList(i, &mem3.aiHash[hash]);
2866 }
2867 }
2868
2869 /*
2870 ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
2871 ** will already be held (obtained by code in malloc.c) if
2872 ** sqlite3GlobalConfig.bMemStat is true.
2873 */
2874 static void memsys3Enter(void){
2875 if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
2876 mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
2877 }
2878 sqlite3_mutex_enter(mem3.mutex);
2879 }
2880 static void memsys3Leave(void){
2881 sqlite3_mutex_leave(mem3.mutex);
2882 }
2883
2884 /*
2885 ** Called when we are unable to satisfy an allocation of nBytes.
2886 */
2887 static void memsys3OutOfMemory(int nByte){
2888 if( !mem3.alarmBusy ){
2889 mem3.alarmBusy = 1;
2890 assert( sqlite3_mutex_held(mem3.mutex) );
2891 sqlite3_mutex_leave(mem3.mutex);
2892 sqlite3_release_memory(nByte);
2893 sqlite3_mutex_enter(mem3.mutex);
2894 mem3.alarmBusy = 0;
2895 }
2896 }
2897
2898
2899 /*
2900 ** Chunk i is a free chunk that has been unlinked. Adjust its
2901 ** size parameters for check-out and return a pointer to the
2902 ** user portion of the chunk.
2903 */
2904 static void *memsys3Checkout(u32 i, u32 nBlock){
2905 u32 x;
2906 assert( sqlite3_mutex_held(mem3.mutex) );
2907 assert( i>=1 );
2908 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
2909 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
2910 x = mem3.aPool[i-1].u.hdr.size4x;
2911 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
2912 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
2913 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
2914 return &mem3.aPool[i];
2915 }
2916
2917 /*
2918 ** Carve a piece off of the end of the mem3.iMaster free chunk.
2919 ** Return a pointer to the new allocation. Or, if the master chunk
2920 ** is not large enough, return 0.
2921 */
2922 static void *memsys3FromMaster(u32 nBlock){
2923 assert( sqlite3_mutex_held(mem3.mutex) );
2924 assert( mem3.szMaster>=nBlock );
2925 if( nBlock>=mem3.szMaster-1 ){
2926 /* Use the entire master */
2927 void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
2928 mem3.iMaster = 0;
2929 mem3.szMaster = 0;
2930 mem3.mnMaster = 0;
2931 return p;
2932 }else{
2933 /* Split the master block. Return the tail. */
2934 u32 newi, x;
2935 newi = mem3.iMaster + mem3.szMaster - nBlock;
2936 assert( newi > mem3.iMaster+1 );
2937 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
2938 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
2939 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
2940 mem3.szMaster -= nBlock;
2941 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
2942 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
2943 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
2944 if( mem3.szMaster < mem3.mnMaster ){
2945 mem3.mnMaster = mem3.szMaster;
2946 }
2947 return (void*)&mem3.aPool[newi];
2948 }
2949 }
2950
2951 /*
2952 ** *pRoot is the head of a list of free chunks of the same size
2953 ** or same size hash. In other words, *pRoot is an entry in either
2954 ** mem3.aiSmall[] or mem3.aiHash[].
2955 **
2956 ** This routine examines all entries on the given list and tries
2957 ** to coalesce each entries with adjacent free chunks.
2958 **
2959 ** If it sees a chunk that is larger than mem3.iMaster, it replaces
2960 ** the current mem3.iMaster with the new larger chunk. In order for
2961 ** this mem3.iMaster replacement to work, the master chunk must be
2962 ** linked into the hash tables. That is not the normal state of
2963 ** affairs, of course. The calling routine must link the master
2964 ** chunk before invoking this routine, then must unlink the (possibly
2965 ** changed) master chunk once this routine has finished.
2966 */
2967 static void memsys3Merge(u32 *pRoot){
2968 u32 iNext, prev, size, i, x;
2969
2970 assert( sqlite3_mutex_held(mem3.mutex) );
2971 for(i=*pRoot; i>0; i=iNext){
2972 iNext = mem3.aPool[i].u.list.next;
2973 size = mem3.aPool[i-1].u.hdr.size4x;
2974 assert( (size&1)==0 );
2975 if( (size&2)==0 ){
2976 memsys3UnlinkFromList(i, pRoot);
2977 assert( i > mem3.aPool[i-1].u.hdr.prevSize );
2978 prev = i - mem3.aPool[i-1].u.hdr.prevSize;
2979 if( prev==iNext ){
2980 iNext = mem3.aPool[prev].u.list.next;
2981 }
2982 memsys3Unlink(prev);
2983 size = i + size/4 - prev;
2984 x = mem3.aPool[prev-1].u.hdr.size4x & 2;
2985 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
2986 mem3.aPool[prev+size-1].u.hdr.prevSize = size;
2987 memsys3Link(prev);
2988 i = prev;
2989 }else{
2990 size /= 4;
2991 }
2992 if( size>mem3.szMaster ){
2993 mem3.iMaster = i;
2994 mem3.szMaster = size;
2995 }
2996 }
2997 }
2998
2999 /*
3000 ** Return a block of memory of at least nBytes in size.
3001 ** Return NULL if unable.
3002 **
3003 ** This function assumes that the necessary mutexes, if any, are
3004 ** already held by the caller. Hence "Unsafe".
3005 */
3006 static void *memsys3MallocUnsafe(int nByte){
3007 u32 i;
3008 u32 nBlock;
3009 u32 toFree;
3010
3011 assert( sqlite3_mutex_held(mem3.mutex) );
3012 assert( sizeof(Mem3Block)==8 );
3013 if( nByte<=12 ){
3014 nBlock = 2;
3015 }else{
3016 nBlock = (nByte + 11)/8;
3017 }
3018 assert( nBlock>=2 );
3019
3020 /* STEP 1:
3021 ** Look for an entry of the correct size in either the small
3022 ** chunk table or in the large chunk hash table. This is
3023 ** successful most of the time (about 9 times out of 10).
3024 */
3025 if( nBlock <= MX_SMALL ){
3026 i = mem3.aiSmall[nBlock-2];
3027 if( i>0 ){
3028 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
3029 return memsys3Checkout(i, nBlock);
3030 }
3031 }else{
3032 int hash = nBlock % N_HASH;
3033 for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
3034 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
3035 memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
3036 return memsys3Checkout(i, nBlock);
3037 }
3038 }
3039 }
3040
3041 /* STEP 2:
3042 ** Try to satisfy the allocation by carving a piece off of the end
3043 ** of the master chunk. This step usually works if step 1 fails.
3044 */
3045 if( mem3.szMaster>=nBlock ){
3046 return memsys3FromMaster(nBlock);
3047 }
3048
3049
3050 /* STEP 3:
3051 ** Loop through the entire memory pool. Coalesce adjacent free
3052 ** chunks. Recompute the master chunk as the largest free chunk.
3053 ** Then try again to satisfy the allocation by carving a piece off
3054 ** of the end of the master chunk. This step happens very
3055 ** rarely (we hope!)
3056 */
3057 for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
3058 memsys3OutOfMemory(toFree);
3059 if( mem3.iMaster ){
3060 memsys3Link(mem3.iMaster);
3061 mem3.iMaster = 0;
3062 mem3.szMaster = 0;
3063 }
3064 for(i=0; i<N_HASH; i++){
3065 memsys3Merge(&mem3.aiHash[i]);
3066 }
3067 for(i=0; i<MX_SMALL-1; i++){
3068 memsys3Merge(&mem3.aiSmall[i]);
3069 }
3070 if( mem3.szMaster ){
3071 memsys3Unlink(mem3.iMaster);
3072 if( mem3.szMaster>=nBlock ){
3073 return memsys3FromMaster(nBlock);
3074 }
3075 }
3076 }
3077
3078 /* If none of the above worked, then we fail. */
3079 return 0;
3080 }
3081
3082 /*
3083 ** Free an outstanding memory allocation.
3084 **
3085 ** This function assumes that the necessary mutexes, if any, are
3086 ** already held by the caller. Hence "Unsafe".
3087 */
3088 static void memsys3FreeUnsafe(void *pOld){
3089 Mem3Block *p = (Mem3Block*)pOld;
3090 int i;
3091 u32 size, x;
3092 assert( sqlite3_mutex_held(mem3.mutex) );
3093 assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
3094 i = p - mem3.aPool;
3095 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
3096 size = mem3.aPool[i-1].u.hdr.size4x/4;
3097 assert( i+size<=mem3.nPool+1 );
3098 mem3.aPool[i-1].u.hdr.size4x &= ~1;
3099 mem3.aPool[i+size-1].u.hdr.prevSize = size;
3100 mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
3101 memsys3Link(i);
3102
3103 /* Try to expand the master using the newly freed chunk */
3104 if( mem3.iMaster ){
3105 while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
3106 size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
3107 mem3.iMaster -= size;
3108 mem3.szMaster += size;
3109 memsys3Unlink(mem3.iMaster);
3110 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
3111 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
3112 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
3113 }
3114 x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
3115 while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
3116 memsys3Unlink(mem3.iMaster+mem3.szMaster);
3117 mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
3118 mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
3119 mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
3120 }
3121 }
3122 }
3123
3124 /*
3125 ** Return the size of an outstanding allocation, in bytes. The
3126 ** size returned omits the 8-byte header overhead. This only
3127 ** works for chunks that are currently checked out.
3128 */
3129 static int memsys3Size(void *p){
3130 Mem3Block *pBlock;
3131 assert( p!=0 );
3132 pBlock = (Mem3Block*)p;
3133 assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
3134 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
3135 }
3136
3137 /*
3138 ** Round up a request size to the next valid allocation size.
3139 */
3140 static int memsys3Roundup(int n){
3141 if( n<=12 ){
3142 return 12;
3143 }else{
3144 return ((n+11)&~7) - 4;
3145 }
3146 }
3147
3148 /*
3149 ** Allocate nBytes of memory.
3150 */
3151 static void *memsys3Malloc(int nBytes){
3152 sqlite3_int64 *p;
3153 assert( nBytes>0 ); /* malloc.c filters out 0 byte requests */
3154 memsys3Enter();
3155 p = memsys3MallocUnsafe(nBytes);
3156 memsys3Leave();
3157 return (void*)p;
3158 }
3159
3160 /*
3161 ** Free memory.
3162 */
3163 static void memsys3Free(void *pPrior){
3164 assert( pPrior );
3165 memsys3Enter();
3166 memsys3FreeUnsafe(pPrior);
3167 memsys3Leave();
3168 }
3169
3170 /*
3171 ** Change the size of an existing memory allocation
3172 */
3173 static void *memsys3Realloc(void *pPrior, int nBytes){
3174 int nOld;
3175 void *p;
3176 if( pPrior==0 ){
3177 return sqlite3_malloc(nBytes);
3178 }
3179 if( nBytes<=0 ){
3180 sqlite3_free(pPrior);
3181 return 0;
3182 }
3183 nOld = memsys3Size(pPrior);
3184 if( nBytes<=nOld && nBytes>=nOld-128 ){
3185 return pPrior;
3186 }
3187 memsys3Enter();
3188 p = memsys3MallocUnsafe(nBytes);
3189 if( p ){
3190 if( nOld<nBytes ){
3191 memcpy(p, pPrior, nOld);
3192 }else{
3193 memcpy(p, pPrior, nBytes);
3194 }
3195 memsys3FreeUnsafe(pPrior);
3196 }
3197 memsys3Leave();
3198 return p;
3199 }
3200
3201 /*
3202 ** Initialize this module.
3203 */
3204 static int memsys3Init(void *NotUsed){
3205 UNUSED_PARAMETER(NotUsed);
3206 if( !sqlite3GlobalConfig.pHeap ){
3207 return SQLITE_ERROR;
3208 }
3209
3210 /* Store a pointer to the memory block in global structure mem3. */
3211 assert( sizeof(Mem3Block)==8 );
3212 mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
3213 mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
3214
3215 /* Initialize the master block. */
3216 mem3.szMaster = mem3.nPool;
3217 mem3.mnMaster = mem3.szMaster;
3218 mem3.iMaster = 1;
3219 mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
3220 mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
3221 mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
3222
3223 return SQLITE_OK;
3224 }
3225
3226 /*
3227 ** Deinitialize this module.
3228 */
3229 static void memsys3Shutdown(void *NotUsed){
3230 UNUSED_PARAMETER(NotUsed);
3231 mem3.mutex = 0;
3232 return;
3233 }
3234
3235
3236
3237 /*
3238 ** Open the file indicated and write a log of all unfreed memory
3239 ** allocations into that log.
3240 */
3241 SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
3242 #ifdef SQLITE_DEBUG
3243 FILE *out;
3244 u32 i, j;
3245 u32 size;
3246 if( zFilename==0 || zFilename[0]==0 ){
3247 out = stdout;
3248 }else{
3249 out = fopen(zFilename, "w");
3250 if( out==0 ){
3251 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
3252 zFilename);
3253 return;
3254 }
3255 }
3256 memsys3Enter();
3257 fprintf(out, "CHUNKS:\n");
3258 for(i=1; i<=mem3.nPool; i+=size/4){
3259 size = mem3.aPool[i-1].u.hdr.size4x;
3260 if( size/4<=1 ){
3261 fprintf(out, "%p size error\n", &mem3.aPool[i]);
3262 assert( 0 );
3263 break;
3264 }
3265 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
3266 fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
3267 assert( 0 );
3268 break;
3269 }
3270 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
3271 fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
3272 assert( 0 );
3273 break;
3274 }
3275 if( size&1 ){
3276 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
3277 }else{
3278 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
3279 i==mem3.iMaster ? " **master**" : "");
3280 }
3281 }
3282 for(i=0; i<MX_SMALL-1; i++){
3283 if( mem3.aiSmall[i]==0 ) continue;
3284 fprintf(out, "small(%2d):", i);
3285 for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
3286 fprintf(out, " %p(%d)", &mem3.aPool[j],
3287 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
3288 }
3289 fprintf(out, "\n");
3290 }
3291 for(i=0; i<N_HASH; i++){
3292 if( mem3.aiHash[i]==0 ) continue;
3293 fprintf(out, "hash(%2d):", i);
3294 for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
3295 fprintf(out, " %p(%d)", &mem3.aPool[j],
3296 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
3297 }
3298 fprintf(out, "\n");
3299 }
3300 fprintf(out, "master=%d\n", mem3.iMaster);
3301 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
3302 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
3303 sqlite3_mutex_leave(mem3.mutex);
3304 if( out==stdout ){
3305 fflush(stdout);
3306 }else{
3307 fclose(out);
3308 }
3309 #else
3310 UNUSED_PARAMETER(zFilename);
3311 #endif
3312 }
3313
3314 /*
3315 ** This routine is the only routine in this file with external
3316 ** linkage.
3317 **
3318 ** Populate the low-level memory allocation function pointers in
3319 ** sqlite3GlobalConfig.m with pointers to the routines in this file. The
3320 ** arguments specify the block of memory to manage.
3321 **
3322 ** This routine is only called by sqlite3_config(), and therefore
3323 ** is not required to be threadsafe (it is not).
3324 */
3325 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
3326 static const sqlite3_mem_methods mempoolMethods = {
3327 memsys3Malloc,
3328 memsys3Free,
3329 memsys3Realloc,
3330 memsys3Size,
3331 memsys3Roundup,
3332 memsys3Init,
3333 memsys3Shutdown,
3334 0
3335 };
3336 return &mempoolMethods;
3337 }
3338
3339 #endif /* SQLITE_ENABLE_MEMSYS3 */
3340
3341 /************** End of mem3.c ************************************************/
3342 /************** Begin file mem5.c ********************************************/
3343 /*
3344 ** 2007 October 14
3345 **
3346 ** The author disclaims copyright to this source code. In place of
3347 ** a legal notice, here is a blessing:
3348 **
3349 ** May you do good and not evil.
3350 ** May you find forgiveness for yourself and forgive others.
3351 ** May you share freely, never taking more than you give.
3352 **
3353 *************************************************************************
3354 ** This file contains the C functions that implement a memory
3355 ** allocation subsystem for use by SQLite.
3356 **
3357 ** This version of the memory allocation subsystem omits all
3358 ** use of malloc(). The application gives SQLite a block of memory
3359 ** before calling sqlite3_initialize() from which allocations
3360 ** are made and returned by the xMalloc() and xRealloc()
3361 ** implementations. Once sqlite3_initialize() has been called,
3362 ** the amount of memory available to SQLite is fixed and cannot
3363 ** be changed.
3364 **
3365 ** This version of the memory allocation subsystem is included
3366 ** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
3367 **
3368 ** This memory allocator uses the following algorithm:
3369 **
3370 ** 1. All memory allocation sizes are rounded up to a power of 2.
3371 **
3372 ** 2. If two adjacent free blocks are the halves of a larger block,
3373 ** then the two blocks are coalesced into the single larger block.
3374 **
3375 ** 3. New memory is allocated from the first available free block.
3376 **
3377 ** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
3378 ** Concerning Dynamic Storage Allocation". Journal of the Association for
3379 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
3380 **
3381 ** Let n be the size of the largest allocation divided by the minimum
3382 ** allocation size (after rounding all sizes up to a power of 2.) Let M
3383 ** be the maximum amount of memory ever outstanding at one time. Let
3384 ** N be the total amount of memory available for allocation. Robson
3385 ** proved that this memory allocator will never breakdown due to
3386 ** fragmentation as long as the following constraint holds:
3387 **
3388 ** N >= M*(1 + log2(n)/2) - n + 1
3389 **
3390 ** The sqlite3_status() logic tracks the maximum values of n and M so
3391 ** that an application can, at any time, verify this constraint.
3392 */
3393 /* #include "sqliteInt.h" */
3394
3395 /*
3396 ** This version of the memory allocator is used only when
3397 ** SQLITE_ENABLE_MEMSYS5 is defined.
3398 */
3399 #ifdef SQLITE_ENABLE_MEMSYS5
3400
3401 /*
3402 ** A minimum allocation is an instance of the following structure.
3403 ** Larger allocations are an array of these structures where the
3404 ** size of the array is a power of 2.
3405 **
3406 ** The size of this object must be a power of two. That fact is
3407 ** verified in memsys5Init().
3408 */
3409 typedef struct Mem5Link Mem5Link;
3410 struct Mem5Link {
3411 int next; /* Index of next free chunk */
3412 int prev; /* Index of previous free chunk */
3413 };
3414
3415 /*
3416 ** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
3417 ** mem5.szAtom is always at least 8 and 32-bit integers are used,
3418 ** it is not actually possible to reach this limit.
3419 */
3420 #define LOGMAX 30
3421
3422 /*
3423 ** Masks used for mem5.aCtrl[] elements.
3424 */
3425 #define CTRL_LOGSIZE 0x1f /* Log2 Size of this block */
3426 #define CTRL_FREE 0x20 /* True if not checked out */
3427
3428 /*
3429 ** All of the static variables used by this module are collected
3430 ** into a single structure named "mem5". This is to keep the
3431 ** static variables organized and to reduce namespace pollution
3432 ** when this module is combined with other in the amalgamation.
3433 */
3434 static SQLITE_WSD struct Mem5Global {
3435 /*
3436 ** Memory available for allocation
3437 */
3438 int szAtom; /* Smallest possible allocation in bytes */
3439 int nBlock; /* Number of szAtom sized blocks in zPool */
3440 u8 *zPool; /* Memory available to be allocated */
3441
3442 /*
3443 ** Mutex to control access to the memory allocation subsystem.
3444 */
3445 sqlite3_mutex *mutex;
3446
3447 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
3448 /*
3449 ** Performance statistics
3450 */
3451 u64 nAlloc; /* Total number of calls to malloc */
3452 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
3453 u64 totalExcess; /* Total internal fragmentation */
3454 u32 currentOut; /* Current checkout, including internal fragmentation */
3455 u32 currentCount; /* Current number of distinct checkouts */
3456 u32 maxOut; /* Maximum instantaneous currentOut */
3457 u32 maxCount; /* Maximum instantaneous currentCount */
3458 u32 maxRequest; /* Largest allocation (exclusive of internal frag) */
3459 #endif
3460
3461 /*
3462 ** Lists of free blocks. aiFreelist[0] is a list of free blocks of
3463 ** size mem5.szAtom. aiFreelist[1] holds blocks of size szAtom*2.
3464 ** aiFreelist[2] holds free blocks of size szAtom*4. And so forth.
3465 */
3466 int aiFreelist[LOGMAX+1];
3467
3468 /*
3469 ** Space for tracking which blocks are checked out and the size
3470 ** of each block. One byte per block.
3471 */
3472 u8 *aCtrl;
3473
3474 } mem5;
3475
3476 /*
3477 ** Access the static variable through a macro for SQLITE_OMIT_WSD.
3478 */
3479 #define mem5 GLOBAL(struct Mem5Global, mem5)
3480
3481 /*
3482 ** Assuming mem5.zPool is divided up into an array of Mem5Link
3483 ** structures, return a pointer to the idx-th such link.
3484 */
3485 #define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
3486
3487 /*
3488 ** Unlink the chunk at mem5.aPool[i] from list it is currently
3489 ** on. It should be found on mem5.aiFreelist[iLogsize].
3490 */
3491 static void memsys5Unlink(int i, int iLogsize){
3492 int next, prev;
3493 assert( i>=0 && i<mem5.nBlock );
3494 assert( iLogsize>=0 && iLogsize<=LOGMAX );
3495 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
3496
3497 next = MEM5LINK(i)->next;
3498 prev = MEM5LINK(i)->prev;
3499 if( prev<0 ){
3500 mem5.aiFreelist[iLogsize] = next;
3501 }else{
3502 MEM5LINK(prev)->next = next;
3503 }
3504 if( next>=0 ){
3505 MEM5LINK(next)->prev = prev;
3506 }
3507 }
3508
3509 /*
3510 ** Link the chunk at mem5.aPool[i] so that is on the iLogsize
3511 ** free list.
3512 */
3513 static void memsys5Link(int i, int iLogsize){
3514 int x;
3515 assert( sqlite3_mutex_held(mem5.mutex) );
3516 assert( i>=0 && i<mem5.nBlock );
3517 assert( iLogsize>=0 && iLogsize<=LOGMAX );
3518 assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
3519
3520 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
3521 MEM5LINK(i)->prev = -1;
3522 if( x>=0 ){
3523 assert( x<mem5.nBlock );
3524 MEM5LINK(x)->prev = i;
3525 }
3526 mem5.aiFreelist[iLogsize] = i;
3527 }
3528
3529 /*
3530 ** Obtain or release the mutex needed to access global data structures.
3531 */
3532 static void memsys5Enter(void){
3533 sqlite3_mutex_enter(mem5.mutex);
3534 }
3535 static void memsys5Leave(void){
3536 sqlite3_mutex_leave(mem5.mutex);
3537 }
3538
3539 /*
3540 ** Return the size of an outstanding allocation, in bytes.
3541 ** This only works for chunks that are currently checked out.
3542 */
3543 static int memsys5Size(void *p){
3544 int iSize, i;
3545 assert( p!=0 );
3546 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
3547 assert( i>=0 && i<mem5.nBlock );
3548 iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
3549 return iSize;
3550 }
3551
3552 /*
3553 ** Return a block of memory of at least nBytes in size.
3554 ** Return NULL if unable. Return NULL if nBytes==0.
3555 **
3556 ** The caller guarantees that nByte is positive.
3557 **
3558 ** The caller has obtained a mutex prior to invoking this
3559 ** routine so there is never any chance that two or more
3560 ** threads can be in this routine at the same time.
3561 */
3562 static void *memsys5MallocUnsafe(int nByte){
3563 int i; /* Index of a mem5.aPool[] slot */
3564 int iBin; /* Index into mem5.aiFreelist[] */
3565 int iFullSz; /* Size of allocation rounded up to power of 2 */
3566 int iLogsize; /* Log2 of iFullSz/POW2_MIN */
3567
3568 /* nByte must be a positive */
3569 assert( nByte>0 );
3570
3571 /* No more than 1GiB per allocation */
3572 if( nByte > 0x40000000 ) return 0;
3573
3574 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
3575 /* Keep track of the maximum allocation request. Even unfulfilled
3576 ** requests are counted */
3577 if( (u32)nByte>mem5.maxRequest ){
3578 mem5.maxRequest = nByte;
3579 }
3580 #endif
3581
3582
3583 /* Round nByte up to the next valid power of two */
3584 for(iFullSz=mem5.szAtom,iLogsize=0; iFullSz<nByte; iFullSz*=2,iLogsize++){}
3585
3586 /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
3587 ** block. If not, then split a block of the next larger power of
3588 ** two in order to create a new free block of size iLogsize.
3589 */
3590 for(iBin=iLogsize; iBin<=LOGMAX && mem5.aiFreelist[iBin]<0; iBin++){}
3591 if( iBin>LOGMAX ){
3592 testcase( sqlite3GlobalConfig.xLog!=0 );
3593 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
3594 return 0;
3595 }
3596 i = mem5.aiFreelist[iBin];
3597 memsys5Unlink(i, iBin);
3598 while( iBin>iLogsize ){
3599 int newSize;
3600
3601 iBin--;
3602 newSize = 1 << iBin;
3603 mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
3604 memsys5Link(i+newSize, iBin);
3605 }
3606 mem5.aCtrl[i] = iLogsize;
3607
3608 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
3609 /* Update allocator performance statistics. */
3610 mem5.nAlloc++;
3611 mem5.totalAlloc += iFullSz;
3612 mem5.totalExcess += iFullSz - nByte;
3613 mem5.currentCount++;
3614 mem5.currentOut += iFullSz;
3615 if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
3616 if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
3617 #endif
3618
3619 #ifdef SQLITE_DEBUG
3620 /* Make sure the allocated memory does not assume that it is set to zero
3621 ** or retains a value from a previous allocation */
3622 memset(&mem5.zPool[i*mem5.szAtom], 0xAA, iFullSz);
3623 #endif
3624
3625 /* Return a pointer to the allocated memory. */
3626 return (void*)&mem5.zPool[i*mem5.szAtom];
3627 }
3628
3629 /*
3630 ** Free an outstanding memory allocation.
3631 */
3632 static void memsys5FreeUnsafe(void *pOld){
3633 u32 size, iLogsize;
3634 int iBlock;
3635
3636 /* Set iBlock to the index of the block pointed to by pOld in
3637 ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
3638 */
3639 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom);
3640
3641 /* Check that the pointer pOld points to a valid, non-free block. */
3642 assert( iBlock>=0 && iBlock<mem5.nBlock );
3643 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
3644 assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
3645
3646 iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
3647 size = 1<<iLogsize;
3648 assert( iBlock+size-1<(u32)mem5.nBlock );
3649
3650 mem5.aCtrl[iBlock] |= CTRL_FREE;
3651 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
3652
3653 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
3654 assert( mem5.currentCount>0 );
3655 assert( mem5.currentOut>=(size*mem5.szAtom) );
3656 mem5.currentCount--;
3657 mem5.currentOut -= size*mem5.szAtom;
3658 assert( mem5.currentOut>0 || mem5.currentCount==0 );
3659 assert( mem5.currentCount>0 || mem5.currentOut==0 );
3660 #endif
3661
3662 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
3663 while( ALWAYS(iLogsize<LOGMAX) ){
3664 int iBuddy;
3665 if( (iBlock>>iLogsize) & 1 ){
3666 iBuddy = iBlock - size;
3667 assert( iBuddy>=0 );
3668 }else{
3669 iBuddy = iBlock + size;
3670 if( iBuddy>=mem5.nBlock ) break;
3671 }
3672 if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
3673 memsys5Unlink(iBuddy, iLogsize);
3674 iLogsize++;
3675 if( iBuddy<iBlock ){
3676 mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
3677 mem5.aCtrl[iBlock] = 0;
3678 iBlock = iBuddy;
3679 }else{
3680 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
3681 mem5.aCtrl[iBuddy] = 0;
3682 }
3683 size *= 2;
3684 }
3685
3686 #ifdef SQLITE_DEBUG
3687 /* Overwrite freed memory with the 0x55 bit pattern to verify that it is
3688 ** not used after being freed */
3689 memset(&mem5.zPool[iBlock*mem5.szAtom], 0x55, size);
3690 #endif
3691
3692 memsys5Link(iBlock, iLogsize);
3693 }
3694
3695 /*
3696 ** Allocate nBytes of memory.
3697 */
3698 static void *memsys5Malloc(int nBytes){
3699 sqlite3_int64 *p = 0;
3700 if( nBytes>0 ){
3701 memsys5Enter();
3702 p = memsys5MallocUnsafe(nBytes);
3703 memsys5Leave();
3704 }
3705 return (void*)p;
3706 }
3707
3708 /*
3709 ** Free memory.
3710 **
3711 ** The outer layer memory allocator prevents this routine from
3712 ** being called with pPrior==0.
3713 */
3714 static void memsys5Free(void *pPrior){
3715 assert( pPrior!=0 );
3716 memsys5Enter();
3717 memsys5FreeUnsafe(pPrior);
3718 memsys5Leave();
3719 }
3720
3721 /*
3722 ** Change the size of an existing memory allocation.
3723 **
3724 ** The outer layer memory allocator prevents this routine from
3725 ** being called with pPrior==0.
3726 **
3727 ** nBytes is always a value obtained from a prior call to
3728 ** memsys5Round(). Hence nBytes is always a non-negative power
3729 ** of two. If nBytes==0 that means that an oversize allocation
3730 ** (an allocation larger than 0x40000000) was requested and this
3731 ** routine should return 0 without freeing pPrior.
3732 */
3733 static void *memsys5Realloc(void *pPrior, int nBytes){
3734 int nOld;
3735 void *p;
3736 assert( pPrior!=0 );
3737 assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */
3738 assert( nBytes>=0 );
3739 if( nBytes==0 ){
3740 return 0;
3741 }
3742 nOld = memsys5Size(pPrior);
3743 if( nBytes<=nOld ){
3744 return pPrior;
3745 }
3746 p = memsys5Malloc(nBytes);
3747 if( p ){
3748 memcpy(p, pPrior, nOld);
3749 memsys5Free(pPrior);
3750 }
3751 return p;
3752 }
3753
3754 /*
3755 ** Round up a request size to the next valid allocation size. If
3756 ** the allocation is too large to be handled by this allocation system,
3757 ** return 0.
3758 **
3759 ** All allocations must be a power of two and must be expressed by a
3760 ** 32-bit signed integer. Hence the largest allocation is 0x40000000
3761 ** or 1073741824 bytes.
3762 */
3763 static int memsys5Roundup(int n){
3764 int iFullSz;
3765 if( n > 0x40000000 ) return 0;
3766 for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2);
3767 return iFullSz;
3768 }
3769
3770 /*
3771 ** Return the ceiling of the logarithm base 2 of iValue.
3772 **
3773 ** Examples: memsys5Log(1) -> 0
3774 ** memsys5Log(2) -> 1
3775 ** memsys5Log(4) -> 2
3776 ** memsys5Log(5) -> 3
3777 ** memsys5Log(8) -> 3
3778 ** memsys5Log(9) -> 4
3779 */
3780 static int memsys5Log(int iValue){
3781 int iLog;
3782 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
3783 return iLog;
3784 }
3785
3786 /*
3787 ** Initialize the memory allocator.
3788 **
3789 ** This routine is not threadsafe. The caller must be holding a mutex
3790 ** to prevent multiple threads from entering at the same time.
3791 */
3792 static int memsys5Init(void *NotUsed){
3793 int ii; /* Loop counter */
3794 int nByte; /* Number of bytes of memory available to this allocator */
3795 u8 *zByte; /* Memory usable by this allocator */
3796 int nMinLog; /* Log base 2 of minimum allocation size in bytes */
3797 int iOffset; /* An offset into mem5.aCtrl[] */
3798
3799 UNUSED_PARAMETER(NotUsed);
3800
3801 /* For the purposes of this routine, disable the mutex */
3802 mem5.mutex = 0;
3803
3804 /* The size of a Mem5Link object must be a power of two. Verify that
3805 ** this is case.
3806 */
3807 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
3808
3809 nByte = sqlite3GlobalConfig.nHeap;
3810 zByte = (u8*)sqlite3GlobalConfig.pHeap;
3811 assert( zByte!=0 ); /* sqlite3_config() does not allow otherwise */
3812
3813 /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
3814 nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
3815 mem5.szAtom = (1<<nMinLog);
3816 while( (int)sizeof(Mem5Link)>mem5.szAtom ){
3817 mem5.szAtom = mem5.szAtom << 1;
3818 }
3819
3820 mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
3821 mem5.zPool = zByte;
3822 mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
3823
3824 for(ii=0; ii<=LOGMAX; ii++){
3825 mem5.aiFreelist[ii] = -1;
3826 }
3827
3828 iOffset = 0;
3829 for(ii=LOGMAX; ii>=0; ii--){
3830 int nAlloc = (1<<ii);
3831 if( (iOffset+nAlloc)<=mem5.nBlock ){
3832 mem5.aCtrl[iOffset] = ii | CTRL_FREE;
3833 memsys5Link(iOffset, ii);
3834 iOffset += nAlloc;
3835 }
3836 assert((iOffset+nAlloc)>mem5.nBlock);
3837 }
3838
3839 /* If a mutex is required for normal operation, allocate one */
3840 if( sqlite3GlobalConfig.bMemstat==0 ){
3841 mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
3842 }
3843
3844 return SQLITE_OK;
3845 }
3846
3847 /*
3848 ** Deinitialize this module.
3849 */
3850 static void memsys5Shutdown(void *NotUsed){
3851 UNUSED_PARAMETER(NotUsed);
3852 mem5.mutex = 0;
3853 return;
3854 }
3855
3856 #ifdef SQLITE_TEST
3857 /*
3858 ** Open the file indicated and write a log of all unfreed memory
3859 ** allocations into that log.
3860 */
3861 SQLITE_PRIVATE void sqlite3Memsys5Dump(const char *zFilename){
3862 FILE *out;
3863 int i, j, n;
3864 int nMinLog;
3865
3866 if( zFilename==0 || zFilename[0]==0 ){
3867 out = stdout;
3868 }else{
3869 out = fopen(zFilename, "w");
3870 if( out==0 ){
3871 fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
3872 zFilename);
3873 return;
3874 }
3875 }
3876 memsys5Enter();
3877 nMinLog = memsys5Log(mem5.szAtom);
3878 for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
3879 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
3880 fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
3881 }
3882 fprintf(out, "mem5.nAlloc = %llu\n", mem5.nAlloc);
3883 fprintf(out, "mem5.totalAlloc = %llu\n", mem5.totalAlloc);
3884 fprintf(out, "mem5.totalExcess = %llu\n", mem5.totalExcess);
3885 fprintf(out, "mem5.currentOut = %u\n", mem5.currentOut);
3886 fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
3887 fprintf(out, "mem5.maxOut = %u\n", mem5.maxOut);
3888 fprintf(out, "mem5.maxCount = %u\n", mem5.maxCount);
3889 fprintf(out, "mem5.maxRequest = %u\n", mem5.maxRequest);
3890 memsys5Leave();
3891 if( out==stdout ){
3892 fflush(stdout);
3893 }else{
3894 fclose(out);
3895 }
3896 }
3897 #endif
3898
3899 /*
3900 ** This routine is the only routine in this file with external
3901 ** linkage. It returns a pointer to a static sqlite3_mem_methods
3902 ** struct populated with the memsys5 methods.
3903 */
3904 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
3905 static const sqlite3_mem_methods memsys5Methods = {
3906 memsys5Malloc,
3907 memsys5Free,
3908 memsys5Realloc,
3909 memsys5Size,
3910 memsys5Roundup,
3911 memsys5Init,
3912 memsys5Shutdown,
3913 0
3914 };
3915 return &memsys5Methods;
3916 }
3917
3918 #endif /* SQLITE_ENABLE_MEMSYS5 */
3919
3920 /************** End of mem5.c ************************************************/
3921 /************** Begin file mutex.c *******************************************/
3922 /*
3923 ** 2007 August 14
3924 **
3925 ** The author disclaims copyright to this source code. In place of
3926 ** a legal notice, here is a blessing:
3927 **
3928 ** May you do good and not evil.
3929 ** May you find forgiveness for yourself and forgive others.
3930 ** May you share freely, never taking more than you give.
3931 **
3932 *************************************************************************
3933 ** This file contains the C functions that implement mutexes.
3934 **
3935 ** This file contains code that is common across all mutex implementations.
3936 */
3937 /* #include "sqliteInt.h" */
3938
3939 #if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
3940 /*
3941 ** For debugging purposes, record when the mutex subsystem is initialized
3942 ** and uninitialized so that we can assert() if there is an attempt to
3943 ** allocate a mutex while the system is uninitialized.
3944 */
3945 static SQLITE_WSD int mutexIsInit = 0;
3946 #endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */
3947
3948
3949 #ifndef SQLITE_MUTEX_OMIT
3950 /*
3951 ** Initialize the mutex system.
3952 */
3953 SQLITE_PRIVATE int sqlite3MutexInit(void){
3954 int rc = SQLITE_OK;
3955 if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
3956 /* If the xMutexAlloc method has not been set, then the user did not
3957 ** install a mutex implementation via sqlite3_config() prior to
3958 ** sqlite3_initialize() being called. This block copies pointers to
3959 ** the default implementation into the sqlite3GlobalConfig structure.
3960 */
3961 sqlite3_mutex_methods const *pFrom;
3962 sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
3963
3964 if( sqlite3GlobalConfig.bCoreMutex ){
3965 pFrom = sqlite3DefaultMutex();
3966 }else{
3967 pFrom = sqlite3NoopMutex();
3968 }
3969 pTo->xMutexInit = pFrom->xMutexInit;
3970 pTo->xMutexEnd = pFrom->xMutexEnd;
3971 pTo->xMutexFree = pFrom->xMutexFree;
3972 pTo->xMutexEnter = pFrom->xMutexEnter;
3973 pTo->xMutexTry = pFrom->xMutexTry;
3974 pTo->xMutexLeave = pFrom->xMutexLeave;
3975 pTo->xMutexHeld = pFrom->xMutexHeld;
3976 pTo->xMutexNotheld = pFrom->xMutexNotheld;
3977 sqlite3MemoryBarrier();
3978 pTo->xMutexAlloc = pFrom->xMutexAlloc;
3979 }
3980 assert( sqlite3GlobalConfig.mutex.xMutexInit );
3981 rc = sqlite3GlobalConfig.mutex.xMutexInit();
3982
3983 #ifdef SQLITE_DEBUG
3984 GLOBAL(int, mutexIsInit) = 1;
3985 #endif
3986
3987 return rc;
3988 }
3989
3990 /*
3991 ** Shutdown the mutex system. This call frees resources allocated by
3992 ** sqlite3MutexInit().
3993 */
3994 SQLITE_PRIVATE int sqlite3MutexEnd(void){
3995 int rc = SQLITE_OK;
3996 if( sqlite3GlobalConfig.mutex.xMutexEnd ){
3997 rc = sqlite3GlobalConfig.mutex.xMutexEnd();
3998 }
3999
4000 #ifdef SQLITE_DEBUG
4001 GLOBAL(int, mutexIsInit) = 0;
4002 #endif
4003
4004 return rc;
4005 }
4006
4007 /*
4008 ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
4009 */
4010 SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
4011 #ifndef SQLITE_OMIT_AUTOINIT
4012 if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0;
4013 if( id>SQLITE_MUTEX_RECURSIVE && sqlite3MutexInit() ) return 0;
4014 #endif
4015 assert( sqlite3GlobalConfig.mutex.xMutexAlloc );
4016 return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
4017 }
4018
4019 SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
4020 if( !sqlite3GlobalConfig.bCoreMutex ){
4021 return 0;
4022 }
4023 assert( GLOBAL(int, mutexIsInit) );
4024 assert( sqlite3GlobalConfig.mutex.xMutexAlloc );
4025 return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
4026 }
4027
4028 /*
4029 ** Free a dynamic mutex.
4030 */
4031 SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
4032 if( p ){
4033 assert( sqlite3GlobalConfig.mutex.xMutexFree );
4034 sqlite3GlobalConfig.mutex.xMutexFree(p);
4035 }
4036 }
4037
4038 /*
4039 ** Obtain the mutex p. If some other thread already has the mutex, block
4040 ** until it can be obtained.
4041 */
4042 SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
4043 if( p ){
4044 assert( sqlite3GlobalConfig.mutex.xMutexEnter );
4045 sqlite3GlobalConfig.mutex.xMutexEnter(p);
4046 }
4047 }
4048
4049 /*
4050 ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
4051 ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
4052 */
4053 SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
4054 int rc = SQLITE_OK;
4055 if( p ){
4056 assert( sqlite3GlobalConfig.mutex.xMutexTry );
4057 return sqlite3GlobalConfig.mutex.xMutexTry(p);
4058 }
4059 return rc;
4060 }
4061
4062 /*
4063 ** The sqlite3_mutex_leave() routine exits a mutex that was previously
4064 ** entered by the same thread. The behavior is undefined if the mutex
4065 ** is not currently entered. If a NULL pointer is passed as an argument
4066 ** this function is a no-op.
4067 */
4068 SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
4069 if( p ){
4070 assert( sqlite3GlobalConfig.mutex.xMutexLeave );
4071 sqlite3GlobalConfig.mutex.xMutexLeave(p);
4072 }
4073 }
4074
4075 #ifndef NDEBUG
4076 /*
4077 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
4078 ** intended for use inside assert() statements.
4079 */
4080 SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
4081 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexHeld );
4082 return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
4083 }
4084 SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
4085 assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld );
4086 return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
4087 }
4088 #endif
4089
4090 #endif /* !defined(SQLITE_MUTEX_OMIT) */
4091
4092 /************** End of mutex.c ***********************************************/
4093 /************** Begin file mutex_noop.c **************************************/
4094 /*
4095 ** 2008 October 07
4096 **
4097 ** The author disclaims copyright to this source code. In place of
4098 ** a legal notice, here is a blessing:
4099 **
4100 ** May you do good and not evil.
4101 ** May you find forgiveness for yourself and forgive others.
4102 ** May you share freely, never taking more than you give.
4103 **
4104 *************************************************************************
4105 ** This file contains the C functions that implement mutexes.
4106 **
4107 ** This implementation in this file does not provide any mutual
4108 ** exclusion and is thus suitable for use only in applications
4109 ** that use SQLite in a single thread. The routines defined
4110 ** here are place-holders. Applications can substitute working
4111 ** mutex routines at start-time using the
4112 **
4113 ** sqlite3_config(SQLITE_CONFIG_MUTEX,...)
4114 **
4115 ** interface.
4116 **
4117 ** If compiled with SQLITE_DEBUG, then additional logic is inserted
4118 ** that does error checking on mutexes to make sure they are being
4119 ** called correctly.
4120 */
4121 /* #include "sqliteInt.h" */
4122
4123 #ifndef SQLITE_MUTEX_OMIT
4124
4125 #ifndef SQLITE_DEBUG
4126 /*
4127 ** Stub routines for all mutex methods.
4128 **
4129 ** This routines provide no mutual exclusion or error checking.
4130 */
4131 static int noopMutexInit(void){ return SQLITE_OK; }
4132 static int noopMutexEnd(void){ return SQLITE_OK; }
4133 static sqlite3_mutex *noopMutexAlloc(int id){
4134 UNUSED_PARAMETER(id);
4135 return (sqlite3_mutex*)8;
4136 }
4137 static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
4138 static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
4139 static int noopMutexTry(sqlite3_mutex *p){
4140 UNUSED_PARAMETER(p);
4141 return SQLITE_OK;
4142 }
4143 static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
4144
4145 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
4146 static const sqlite3_mutex_methods sMutex = {
4147 noopMutexInit,
4148 noopMutexEnd,
4149 noopMutexAlloc,
4150 noopMutexFree,
4151 noopMutexEnter,
4152 noopMutexTry,
4153 noopMutexLeave,
4154
4155 0,
4156 0,
4157 };
4158
4159 return &sMutex;
4160 }
4161 #endif /* !SQLITE_DEBUG */
4162
4163 #ifdef SQLITE_DEBUG
4164 /*
4165 ** In this implementation, error checking is provided for testing
4166 ** and debugging purposes. The mutexes still do not provide any
4167 ** mutual exclusion.
4168 */
4169
4170 /*
4171 ** The mutex object
4172 */
4173 typedef struct sqlite3_debug_mutex {
4174 int id; /* The mutex type */
4175 int cnt; /* Number of entries without a matching leave */
4176 } sqlite3_debug_mutex;
4177
4178 /*
4179 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
4180 ** intended for use inside assert() statements.
4181 */
4182 static int debugMutexHeld(sqlite3_mutex *pX){
4183 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
4184 return p==0 || p->cnt>0;
4185 }
4186 static int debugMutexNotheld(sqlite3_mutex *pX){
4187 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
4188 return p==0 || p->cnt==0;
4189 }
4190
4191 /*
4192 ** Initialize and deinitialize the mutex subsystem.
4193 */
4194 static int debugMutexInit(void){ return SQLITE_OK; }
4195 static int debugMutexEnd(void){ return SQLITE_OK; }
4196
4197 /*
4198 ** The sqlite3_mutex_alloc() routine allocates a new
4199 ** mutex and returns a pointer to it. If it returns NULL
4200 ** that means that a mutex could not be allocated.
4201 */
4202 static sqlite3_mutex *debugMutexAlloc(int id){
4203 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1];
4204 sqlite3_debug_mutex *pNew = 0;
4205 switch( id ){
4206 case SQLITE_MUTEX_FAST:
4207 case SQLITE_MUTEX_RECURSIVE: {
4208 pNew = sqlite3Malloc(sizeof(*pNew));
4209 if( pNew ){
4210 pNew->id = id;
4211 pNew->cnt = 0;
4212 }
4213 break;
4214 }
4215 default: {
4216 #ifdef SQLITE_ENABLE_API_ARMOR
4217 if( id-2<0 || id-2>=ArraySize(aStatic) ){
4218 (void)SQLITE_MISUSE_BKPT;
4219 return 0;
4220 }
4221 #endif
4222 pNew = &aStatic[id-2];
4223 pNew->id = id;
4224 break;
4225 }
4226 }
4227 return (sqlite3_mutex*)pNew;
4228 }
4229
4230 /*
4231 ** This routine deallocates a previously allocated mutex.
4232 */
4233 static void debugMutexFree(sqlite3_mutex *pX){
4234 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
4235 assert( p->cnt==0 );
4236 if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){
4237 sqlite3_free(p);
4238 }else{
4239 #ifdef SQLITE_ENABLE_API_ARMOR
4240 (void)SQLITE_MISUSE_BKPT;
4241 #endif
4242 }
4243 }
4244
4245 /*
4246 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
4247 ** to enter a mutex. If another thread is already within the mutex,
4248 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
4249 ** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
4250 ** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
4251 ** be entered multiple times by the same thread. In such cases the,
4252 ** mutex must be exited an equal number of times before another thread
4253 ** can enter. If the same thread tries to enter any other kind of mutex
4254 ** more than once, the behavior is undefined.
4255 */
4256 static void debugMutexEnter(sqlite3_mutex *pX){
4257 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
4258 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
4259 p->cnt++;
4260 }
4261 static int debugMutexTry(sqlite3_mutex *pX){
4262 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
4263 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
4264 p->cnt++;
4265 return SQLITE_OK;
4266 }
4267
4268 /*
4269 ** The sqlite3_mutex_leave() routine exits a mutex that was
4270 ** previously entered by the same thread. The behavior
4271 ** is undefined if the mutex is not currently entered or
4272 ** is not currently allocated. SQLite will never do either.
4273 */
4274 static void debugMutexLeave(sqlite3_mutex *pX){
4275 sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
4276 assert( debugMutexHeld(pX) );
4277 p->cnt--;
4278 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
4279 }
4280
4281 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
4282 static const sqlite3_mutex_methods sMutex = {
4283 debugMutexInit,
4284 debugMutexEnd,
4285 debugMutexAlloc,
4286 debugMutexFree,
4287 debugMutexEnter,
4288 debugMutexTry,
4289 debugMutexLeave,
4290
4291 debugMutexHeld,
4292 debugMutexNotheld
4293 };
4294
4295 return &sMutex;
4296 }
4297 #endif /* SQLITE_DEBUG */
4298
4299 /*
4300 ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
4301 ** is used regardless of the run-time threadsafety setting.
4302 */
4303 #ifdef SQLITE_MUTEX_NOOP
4304 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
4305 return sqlite3NoopMutex();
4306 }
4307 #endif /* defined(SQLITE_MUTEX_NOOP) */
4308 #endif /* !defined(SQLITE_MUTEX_OMIT) */
4309
4310 /************** End of mutex_noop.c ******************************************/
4311 /************** Begin file mutex_unix.c **************************************/
4312 /*
4313 ** 2007 August 28
4314 **
4315 ** The author disclaims copyright to this source code. In place of
4316 ** a legal notice, here is a blessing:
4317 **
4318 ** May you do good and not evil.
4319 ** May you find forgiveness for yourself and forgive others.
4320 ** May you share freely, never taking more than you give.
4321 **
4322 *************************************************************************
4323 ** This file contains the C functions that implement mutexes for pthreads
4324 */
4325 /* #include "sqliteInt.h" */
4326
4327 /*
4328 ** The code in this file is only used if we are compiling threadsafe
4329 ** under unix with pthreads.
4330 **
4331 ** Note that this implementation requires a version of pthreads that
4332 ** supports recursive mutexes.
4333 */
4334 #ifdef SQLITE_MUTEX_PTHREADS
4335
4336 #include <pthread.h>
4337
4338 /*
4339 ** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
4340 ** are necessary under two condidtions: (1) Debug builds and (2) using
4341 ** home-grown mutexes. Encapsulate these conditions into a single #define.
4342 */
4343 #if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
4344 # define SQLITE_MUTEX_NREF 1
4345 #else
4346 # define SQLITE_MUTEX_NREF 0
4347 #endif
4348
4349 /*
4350 ** Each recursive mutex is an instance of the following structure.
4351 */
4352 struct sqlite3_mutex {
4353 pthread_mutex_t mutex; /* Mutex controlling the lock */
4354 #if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
4355 int id; /* Mutex type */
4356 #endif
4357 #if SQLITE_MUTEX_NREF
4358 volatile int nRef; /* Number of entrances */
4359 volatile pthread_t owner; /* Thread that is within this mutex */
4360 int trace; /* True to trace changes */
4361 #endif
4362 };
4363 #if SQLITE_MUTEX_NREF
4364 #define SQLITE3_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER,0,0,(pthread_t)0,0}
4365 #elif defined(SQLITE_ENABLE_API_ARMOR)
4366 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0 }
4367 #else
4368 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
4369 #endif
4370
4371 /*
4372 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
4373 ** intended for use only inside assert() statements. On some platforms,
4374 ** there might be race conditions that can cause these routines to
4375 ** deliver incorrect results. In particular, if pthread_equal() is
4376 ** not an atomic operation, then these routines might delivery
4377 ** incorrect results. On most platforms, pthread_equal() is a
4378 ** comparison of two integers and is therefore atomic. But we are
4379 ** told that HPUX is not such a platform. If so, then these routines
4380 ** will not always work correctly on HPUX.
4381 **
4382 ** On those platforms where pthread_equal() is not atomic, SQLite
4383 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
4384 ** make sure no assert() statements are evaluated and hence these
4385 ** routines are never called.
4386 */
4387 #if !defined(NDEBUG) || defined(SQLITE_DEBUG)
4388 static int pthreadMutexHeld(sqlite3_mutex *p){
4389 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
4390 }
4391 static int pthreadMutexNotheld(sqlite3_mutex *p){
4392 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
4393 }
4394 #endif
4395
4396 /*
4397 ** Try to provide a memory barrier operation, needed for initialization
4398 ** and also for the implementation of xShmBarrier in the VFS in cases
4399 ** where SQLite is compiled without mutexes.
4400 */
4401 SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
4402 #if defined(SQLITE_MEMORY_BARRIER)
4403 SQLITE_MEMORY_BARRIER;
4404 #elif defined(__GNUC__) && GCC_VERSION>=4001000
4405 __sync_synchronize();
4406 #endif
4407 }
4408
4409 /*
4410 ** Initialize and deinitialize the mutex subsystem.
4411 */
4412 static int pthreadMutexInit(void){ return SQLITE_OK; }
4413 static int pthreadMutexEnd(void){ return SQLITE_OK; }
4414
4415 /*
4416 ** The sqlite3_mutex_alloc() routine allocates a new
4417 ** mutex and returns a pointer to it. If it returns NULL
4418 ** that means that a mutex could not be allocated. SQLite
4419 ** will unwind its stack and return an error. The argument
4420 ** to sqlite3_mutex_alloc() is one of these integer constants:
4421 **
4422 ** <ul>
4423 ** <li> SQLITE_MUTEX_FAST
4424 ** <li> SQLITE_MUTEX_RECURSIVE
4425 ** <li> SQLITE_MUTEX_STATIC_MASTER
4426 ** <li> SQLITE_MUTEX_STATIC_MEM
4427 ** <li> SQLITE_MUTEX_STATIC_OPEN
4428 ** <li> SQLITE_MUTEX_STATIC_PRNG
4429 ** <li> SQLITE_MUTEX_STATIC_LRU
4430 ** <li> SQLITE_MUTEX_STATIC_PMEM
4431 ** <li> SQLITE_MUTEX_STATIC_APP1
4432 ** <li> SQLITE_MUTEX_STATIC_APP2
4433 ** <li> SQLITE_MUTEX_STATIC_APP3
4434 ** <li> SQLITE_MUTEX_STATIC_VFS1
4435 ** <li> SQLITE_MUTEX_STATIC_VFS2
4436 ** <li> SQLITE_MUTEX_STATIC_VFS3
4437 ** </ul>
4438 **
4439 ** The first two constants cause sqlite3_mutex_alloc() to create
4440 ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
4441 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
4442 ** The mutex implementation does not need to make a distinction
4443 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
4444 ** not want to. But SQLite will only request a recursive mutex in
4445 ** cases where it really needs one. If a faster non-recursive mutex
4446 ** implementation is available on the host platform, the mutex subsystem
4447 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
4448 **
4449 ** The other allowed parameters to sqlite3_mutex_alloc() each return
4450 ** a pointer to a static preexisting mutex. Six static mutexes are
4451 ** used by the current version of SQLite. Future versions of SQLite
4452 ** may add additional static mutexes. Static mutexes are for internal
4453 ** use by SQLite only. Applications that use SQLite mutexes should
4454 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
4455 ** SQLITE_MUTEX_RECURSIVE.
4456 **
4457 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
4458 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
4459 ** returns a different mutex on every call. But for the static
4460 ** mutex types, the same mutex is returned on every call that has
4461 ** the same type number.
4462 */
4463 static sqlite3_mutex *pthreadMutexAlloc(int iType){
4464 static sqlite3_mutex staticMutexes[] = {
4465 SQLITE3_MUTEX_INITIALIZER,
4466 SQLITE3_MUTEX_INITIALIZER,
4467 SQLITE3_MUTEX_INITIALIZER,
4468 SQLITE3_MUTEX_INITIALIZER,
4469 SQLITE3_MUTEX_INITIALIZER,
4470 SQLITE3_MUTEX_INITIALIZER,
4471 SQLITE3_MUTEX_INITIALIZER,
4472 SQLITE3_MUTEX_INITIALIZER,
4473 SQLITE3_MUTEX_INITIALIZER,
4474 SQLITE3_MUTEX_INITIALIZER,
4475 SQLITE3_MUTEX_INITIALIZER,
4476 SQLITE3_MUTEX_INITIALIZER
4477 };
4478 sqlite3_mutex *p;
4479 switch( iType ){
4480 case SQLITE_MUTEX_RECURSIVE: {
4481 p = sqlite3MallocZero( sizeof(*p) );
4482 if( p ){
4483 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
4484 /* If recursive mutexes are not available, we will have to
4485 ** build our own. See below. */
4486 pthread_mutex_init(&p->mutex, 0);
4487 #else
4488 /* Use a recursive mutex if it is available */
4489 pthread_mutexattr_t recursiveAttr;
4490 pthread_mutexattr_init(&recursiveAttr);
4491 pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
4492 pthread_mutex_init(&p->mutex, &recursiveAttr);
4493 pthread_mutexattr_destroy(&recursiveAttr);
4494 #endif
4495 }
4496 break;
4497 }
4498 case SQLITE_MUTEX_FAST: {
4499 p = sqlite3MallocZero( sizeof(*p) );
4500 if( p ){
4501 pthread_mutex_init(&p->mutex, 0);
4502 }
4503 break;
4504 }
4505 default: {
4506 #ifdef SQLITE_ENABLE_API_ARMOR
4507 if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){
4508 (void)SQLITE_MISUSE_BKPT;
4509 return 0;
4510 }
4511 #endif
4512 p = &staticMutexes[iType-2];
4513 break;
4514 }
4515 }
4516 #if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
4517 if( p ) p->id = iType;
4518 #endif
4519 return p;
4520 }
4521
4522
4523 /*
4524 ** This routine deallocates a previously
4525 ** allocated mutex. SQLite is careful to deallocate every
4526 ** mutex that it allocates.
4527 */
4528 static void pthreadMutexFree(sqlite3_mutex *p){
4529 assert( p->nRef==0 );
4530 #if SQLITE_ENABLE_API_ARMOR
4531 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE )
4532 #endif
4533 {
4534 pthread_mutex_destroy(&p->mutex);
4535 sqlite3_free(p);
4536 }
4537 #ifdef SQLITE_ENABLE_API_ARMOR
4538 else{
4539 (void)SQLITE_MISUSE_BKPT;
4540 }
4541 #endif
4542 }
4543
4544 /*
4545 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
4546 ** to enter a mutex. If another thread is already within the mutex,
4547 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
4548 ** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
4549 ** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
4550 ** be entered multiple times by the same thread. In such cases the,
4551 ** mutex must be exited an equal number of times before another thread
4552 ** can enter. If the same thread tries to enter any other kind of mutex
4553 ** more than once, the behavior is undefined.
4554 */
4555 static void pthreadMutexEnter(sqlite3_mutex *p){
4556 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
4557
4558 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
4559 /* If recursive mutexes are not available, then we have to grow
4560 ** our own. This implementation assumes that pthread_equal()
4561 ** is atomic - that it cannot be deceived into thinking self
4562 ** and p->owner are equal if p->owner changes between two values
4563 ** that are not equal to self while the comparison is taking place.
4564 ** This implementation also assumes a coherent cache - that
4565 ** separate processes cannot read different values from the same
4566 ** address at the same time. If either of these two conditions
4567 ** are not met, then the mutexes will fail and problems will result.
4568 */
4569 {
4570 pthread_t self = pthread_self();
4571 if( p->nRef>0 && pthread_equal(p->owner, self) ){
4572 p->nRef++;
4573 }else{
4574 pthread_mutex_lock(&p->mutex);
4575 assert( p->nRef==0 );
4576 p->owner = self;
4577 p->nRef = 1;
4578 }
4579 }
4580 #else
4581 /* Use the built-in recursive mutexes if they are available.
4582 */
4583 pthread_mutex_lock(&p->mutex);
4584 #if SQLITE_MUTEX_NREF
4585 assert( p->nRef>0 || p->owner==0 );
4586 p->owner = pthread_self();
4587 p->nRef++;
4588 #endif
4589 #endif
4590
4591 #ifdef SQLITE_DEBUG
4592 if( p->trace ){
4593 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
4594 }
4595 #endif
4596 }
4597 static int pthreadMutexTry(sqlite3_mutex *p){
4598 int rc;
4599 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
4600
4601 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
4602 /* If recursive mutexes are not available, then we have to grow
4603 ** our own. This implementation assumes that pthread_equal()
4604 ** is atomic - that it cannot be deceived into thinking self
4605 ** and p->owner are equal if p->owner changes between two values
4606 ** that are not equal to self while the comparison is taking place.
4607 ** This implementation also assumes a coherent cache - that
4608 ** separate processes cannot read different values from the same
4609 ** address at the same time. If either of these two conditions
4610 ** are not met, then the mutexes will fail and problems will result.
4611 */
4612 {
4613 pthread_t self = pthread_self();
4614 if( p->nRef>0 && pthread_equal(p->owner, self) ){
4615 p->nRef++;
4616 rc = SQLITE_OK;
4617 }else if( pthread_mutex_trylock(&p->mutex)==0 ){
4618 assert( p->nRef==0 );
4619 p->owner = self;
4620 p->nRef = 1;
4621 rc = SQLITE_OK;
4622 }else{
4623 rc = SQLITE_BUSY;
4624 }
4625 }
4626 #else
4627 /* Use the built-in recursive mutexes if they are available.
4628 */
4629 if( pthread_mutex_trylock(&p->mutex)==0 ){
4630 #if SQLITE_MUTEX_NREF
4631 p->owner = pthread_self();
4632 p->nRef++;
4633 #endif
4634 rc = SQLITE_OK;
4635 }else{
4636 rc = SQLITE_BUSY;
4637 }
4638 #endif
4639
4640 #ifdef SQLITE_DEBUG
4641 if( rc==SQLITE_OK && p->trace ){
4642 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
4643 }
4644 #endif
4645 return rc;
4646 }
4647
4648 /*
4649 ** The sqlite3_mutex_leave() routine exits a mutex that was
4650 ** previously entered by the same thread. The behavior
4651 ** is undefined if the mutex is not currently entered or
4652 ** is not currently allocated. SQLite will never do either.
4653 */
4654 static void pthreadMutexLeave(sqlite3_mutex *p){
4655 assert( pthreadMutexHeld(p) );
4656 #if SQLITE_MUTEX_NREF
4657 p->nRef--;
4658 if( p->nRef==0 ) p->owner = 0;
4659 #endif
4660 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
4661
4662 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
4663 if( p->nRef==0 ){
4664 pthread_mutex_unlock(&p->mutex);
4665 }
4666 #else
4667 pthread_mutex_unlock(&p->mutex);
4668 #endif
4669
4670 #ifdef SQLITE_DEBUG
4671 if( p->trace ){
4672 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
4673 }
4674 #endif
4675 }
4676
4677 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
4678 static const sqlite3_mutex_methods sMutex = {
4679 pthreadMutexInit,
4680 pthreadMutexEnd,
4681 pthreadMutexAlloc,
4682 pthreadMutexFree,
4683 pthreadMutexEnter,
4684 pthreadMutexTry,
4685 pthreadMutexLeave,
4686 #ifdef SQLITE_DEBUG
4687 pthreadMutexHeld,
4688 pthreadMutexNotheld
4689 #else
4690 0,
4691 0
4692 #endif
4693 };
4694
4695 return &sMutex;
4696 }
4697
4698 #endif /* SQLITE_MUTEX_PTHREADS */
4699
4700 /************** End of mutex_unix.c ******************************************/
4701 /************** Begin file mutex_w32.c ***************************************/
4702 /*
4703 ** 2007 August 14
4704 **
4705 ** The author disclaims copyright to this source code. In place of
4706 ** a legal notice, here is a blessing:
4707 **
4708 ** May you do good and not evil.
4709 ** May you find forgiveness for yourself and forgive others.
4710 ** May you share freely, never taking more than you give.
4711 **
4712 *************************************************************************
4713 ** This file contains the C functions that implement mutexes for Win32.
4714 */
4715 /* #include "sqliteInt.h" */
4716
4717 #if SQLITE_OS_WIN
4718 /*
4719 ** Include code that is common to all os_*.c files
4720 */
4721 /************** Include os_common.h in the middle of mutex_w32.c *************/
4722 /************** Begin file os_common.h ***************************************/
4723 /*
4724 ** 2004 May 22
4725 **
4726 ** The author disclaims copyright to this source code. In place of
4727 ** a legal notice, here is a blessing:
4728 **
4729 ** May you do good and not evil.
4730 ** May you find forgiveness for yourself and forgive others.
4731 ** May you share freely, never taking more than you give.
4732 **
4733 ******************************************************************************
4734 **
4735 ** This file contains macros and a little bit of code that is common to
4736 ** all of the platform-specific files (os_*.c) and is #included into those
4737 ** files.
4738 **
4739 ** This file should be #included by the os_*.c files only. It is not a
4740 ** general purpose header file.
4741 */
4742 #ifndef _OS_COMMON_H_
4743 #define _OS_COMMON_H_
4744
4745 /*
4746 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
4747 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
4748 ** switch. The following code should catch this problem at compile-time.
4749 */
4750 #ifdef MEMORY_DEBUG
4751 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
4752 #endif
4753
4754 /*
4755 ** Macros for performance tracing. Normally turned off. Only works
4756 ** on i486 hardware.
4757 */
4758 #ifdef SQLITE_PERFORMANCE_TRACE
4759
4760 /*
4761 ** hwtime.h contains inline assembler code for implementing
4762 ** high-performance timing routines.
4763 */
4764 /************** Include hwtime.h in the middle of os_common.h ****************/
4765 /************** Begin file hwtime.h ******************************************/
4766 /*
4767 ** 2008 May 27
4768 **
4769 ** The author disclaims copyright to this source code. In place of
4770 ** a legal notice, here is a blessing:
4771 **
4772 ** May you do good and not evil.
4773 ** May you find forgiveness for yourself and forgive others.
4774 ** May you share freely, never taking more than you give.
4775 **
4776 ******************************************************************************
4777 **
4778 ** This file contains inline asm code for retrieving "high-performance"
4779 ** counters for x86 class CPUs.
4780 */
4781 #ifndef SQLITE_HWTIME_H
4782 #define SQLITE_HWTIME_H
4783
4784 /*
4785 ** The following routine only works on pentium-class (or newer) processors.
4786 ** It uses the RDTSC opcode to read the cycle count value out of the
4787 ** processor and returns that value. This can be used for high-res
4788 ** profiling.
4789 */
4790 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
4791 (defined(i386) || defined(__i386__) || defined(_M_IX86))
4792
4793 #if defined(__GNUC__)
4794
4795 __inline__ sqlite_uint64 sqlite3Hwtime(void){
4796 unsigned int lo, hi;
4797 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
4798 return (sqlite_uint64)hi << 32 | lo;
4799 }
4800
4801 #elif defined(_MSC_VER)
4802
4803 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
4804 __asm {
4805 rdtsc
4806 ret ; return value at EDX:EAX
4807 }
4808 }
4809
4810 #endif
4811
4812 #elif (defined(__GNUC__) && defined(__x86_64__))
4813
4814 __inline__ sqlite_uint64 sqlite3Hwtime(void){
4815 unsigned long val;
4816 __asm__ __volatile__ ("rdtsc" : "=A" (val));
4817 return val;
4818 }
4819
4820 #elif (defined(__GNUC__) && defined(__ppc__))
4821
4822 __inline__ sqlite_uint64 sqlite3Hwtime(void){
4823 unsigned long long retval;
4824 unsigned long junk;
4825 __asm__ __volatile__ ("\n\
4826 1: mftbu %1\n\
4827 mftb %L0\n\
4828 mftbu %0\n\
4829 cmpw %0,%1\n\
4830 bne 1b"
4831 : "=r" (retval), "=r" (junk));
4832 return retval;
4833 }
4834
4835 #else
4836
4837 #error Need implementation of sqlite3Hwtime() for your platform.
4838
4839 /*
4840 ** To compile without implementing sqlite3Hwtime() for your platform,
4841 ** you can remove the above #error and use the following
4842 ** stub function. You will lose timing support for many
4843 ** of the debugging and testing utilities, but it should at
4844 ** least compile and run.
4845 */
4846 SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
4847
4848 #endif
4849
4850 #endif /* !defined(SQLITE_HWTIME_H) */
4851
4852 /************** End of hwtime.h **********************************************/
4853 /************** Continuing where we left off in os_common.h ******************/
4854
4855 static sqlite_uint64 g_start;
4856 static sqlite_uint64 g_elapsed;
4857 #define TIMER_START g_start=sqlite3Hwtime()
4858 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
4859 #define TIMER_ELAPSED g_elapsed
4860 #else
4861 #define TIMER_START
4862 #define TIMER_END
4863 #define TIMER_ELAPSED ((sqlite_uint64)0)
4864 #endif
4865
4866 /*
4867 ** If we compile with the SQLITE_TEST macro set, then the following block
4868 ** of code will give us the ability to simulate a disk I/O error. This
4869 ** is used for testing the I/O recovery logic.
4870 */
4871 #if defined(SQLITE_TEST)
4872 SQLITE_API extern int sqlite3_io_error_hit;
4873 SQLITE_API extern int sqlite3_io_error_hardhit;
4874 SQLITE_API extern int sqlite3_io_error_pending;
4875 SQLITE_API extern int sqlite3_io_error_persist;
4876 SQLITE_API extern int sqlite3_io_error_benign;
4877 SQLITE_API extern int sqlite3_diskfull_pending;
4878 SQLITE_API extern int sqlite3_diskfull;
4879 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
4880 #define SimulateIOError(CODE) \
4881 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
4882 || sqlite3_io_error_pending-- == 1 ) \
4883 { local_ioerr(); CODE; }
4884 static void local_ioerr(){
4885 IOTRACE(("IOERR\n"));
4886 sqlite3_io_error_hit++;
4887 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
4888 }
4889 #define SimulateDiskfullError(CODE) \
4890 if( sqlite3_diskfull_pending ){ \
4891 if( sqlite3_diskfull_pending == 1 ){ \
4892 local_ioerr(); \
4893 sqlite3_diskfull = 1; \
4894 sqlite3_io_error_hit = 1; \
4895 CODE; \
4896 }else{ \
4897 sqlite3_diskfull_pending--; \
4898 } \
4899 }
4900 #else
4901 #define SimulateIOErrorBenign(X)
4902 #define SimulateIOError(A)
4903 #define SimulateDiskfullError(A)
4904 #endif /* defined(SQLITE_TEST) */
4905
4906 /*
4907 ** When testing, keep a count of the number of open files.
4908 */
4909 #if defined(SQLITE_TEST)
4910 SQLITE_API extern int sqlite3_open_file_count;
4911 #define OpenCounter(X) sqlite3_open_file_count+=(X)
4912 #else
4913 #define OpenCounter(X)
4914 #endif /* defined(SQLITE_TEST) */
4915
4916 #endif /* !defined(_OS_COMMON_H_) */
4917
4918 /************** End of os_common.h *******************************************/
4919 /************** Continuing where we left off in mutex_w32.c ******************/
4920
4921 /*
4922 ** Include the header file for the Windows VFS.
4923 */
4924 /************** Include os_win.h in the middle of mutex_w32.c ****************/
4925 /************** Begin file os_win.h ******************************************/
4926 /*
4927 ** 2013 November 25
4928 **
4929 ** The author disclaims copyright to this source code. In place of
4930 ** a legal notice, here is a blessing:
4931 **
4932 ** May you do good and not evil.
4933 ** May you find forgiveness for yourself and forgive others.
4934 ** May you share freely, never taking more than you give.
4935 **
4936 ******************************************************************************
4937 **
4938 ** This file contains code that is specific to Windows.
4939 */
4940 #ifndef SQLITE_OS_WIN_H
4941 #define SQLITE_OS_WIN_H
4942
4943 /*
4944 ** Include the primary Windows SDK header file.
4945 */
4946 #include "windows.h"
4947
4948 #ifdef __CYGWIN__
4949 # include <sys/cygwin.h>
4950 # include <errno.h> /* amalgamator: dontcache */
4951 #endif
4952
4953 /*
4954 ** Determine if we are dealing with Windows NT.
4955 **
4956 ** We ought to be able to determine if we are compiling for Windows 9x or
4957 ** Windows NT using the _WIN32_WINNT macro as follows:
4958 **
4959 ** #if defined(_WIN32_WINNT)
4960 ** # define SQLITE_OS_WINNT 1
4961 ** #else
4962 ** # define SQLITE_OS_WINNT 0
4963 ** #endif
4964 **
4965 ** However, Visual Studio 2005 does not set _WIN32_WINNT by default, as
4966 ** it ought to, so the above test does not work. We'll just assume that
4967 ** everything is Windows NT unless the programmer explicitly says otherwise
4968 ** by setting SQLITE_OS_WINNT to 0.
4969 */
4970 #if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
4971 # define SQLITE_OS_WINNT 1
4972 #endif
4973
4974 /*
4975 ** Determine if we are dealing with Windows CE - which has a much reduced
4976 ** API.
4977 */
4978 #if defined(_WIN32_WCE)
4979 # define SQLITE_OS_WINCE 1
4980 #else
4981 # define SQLITE_OS_WINCE 0
4982 #endif
4983
4984 /*
4985 ** Determine if we are dealing with WinRT, which provides only a subset of
4986 ** the full Win32 API.
4987 */
4988 #if !defined(SQLITE_OS_WINRT)
4989 # define SQLITE_OS_WINRT 0
4990 #endif
4991
4992 /*
4993 ** For WinCE, some API function parameters do not appear to be declared as
4994 ** volatile.
4995 */
4996 #if SQLITE_OS_WINCE
4997 # define SQLITE_WIN32_VOLATILE
4998 #else
4999 # define SQLITE_WIN32_VOLATILE volatile
5000 #endif
5001
5002 /*
5003 ** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
5004 ** functions are not available (e.g. those not using MSVC, Cygwin, etc).
5005 */
5006 #if SQLITE_OS_WIN && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
5007 SQLITE_THREADSAFE>0 && !defined(__CYGWIN__)
5008 # define SQLITE_OS_WIN_THREADS 1
5009 #else
5010 # define SQLITE_OS_WIN_THREADS 0
5011 #endif
5012
5013 #endif /* SQLITE_OS_WIN_H */
5014
5015 /************** End of os_win.h **********************************************/
5016 /************** Continuing where we left off in mutex_w32.c ******************/
5017 #endif
5018
5019 /*
5020 ** The code in this file is only used if we are compiling multithreaded
5021 ** on a Win32 system.
5022 */
5023 #ifdef SQLITE_MUTEX_W32
5024
5025 /*
5026 ** Each recursive mutex is an instance of the following structure.
5027 */
5028 struct sqlite3_mutex {
5029 CRITICAL_SECTION mutex; /* Mutex controlling the lock */
5030 int id; /* Mutex type */
5031 #ifdef SQLITE_DEBUG
5032 volatile int nRef; /* Number of enterances */
5033 volatile DWORD owner; /* Thread holding this mutex */
5034 volatile int trace; /* True to trace changes */
5035 #endif
5036 };
5037
5038 /*
5039 ** These are the initializer values used when declaring a "static" mutex
5040 ** on Win32. It should be noted that all mutexes require initialization
5041 ** on the Win32 platform.
5042 */
5043 #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
5044
5045 #ifdef SQLITE_DEBUG
5046 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, \
5047 0L, (DWORD)0, 0 }
5048 #else
5049 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
5050 #endif
5051
5052 #ifdef SQLITE_DEBUG
5053 /*
5054 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
5055 ** intended for use only inside assert() statements.
5056 */
5057 static int winMutexHeld(sqlite3_mutex *p){
5058 return p->nRef!=0 && p->owner==GetCurrentThreadId();
5059 }
5060
5061 static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
5062 return p->nRef==0 || p->owner!=tid;
5063 }
5064
5065 static int winMutexNotheld(sqlite3_mutex *p){
5066 DWORD tid = GetCurrentThreadId();
5067 return winMutexNotheld2(p, tid);
5068 }
5069 #endif
5070
5071 /*
5072 ** Try to provide a memory barrier operation, needed for initialization
5073 ** and also for the xShmBarrier method of the VFS in cases when SQLite is
5074 ** compiled without mutexes (SQLITE_THREADSAFE=0).
5075 */
5076 SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
5077 #if defined(SQLITE_MEMORY_BARRIER)
5078 SQLITE_MEMORY_BARRIER;
5079 #elif defined(__GNUC__)
5080 __sync_synchronize();
5081 #elif MSVC_VERSION>=1300
5082 _ReadWriteBarrier();
5083 #elif defined(MemoryBarrier)
5084 MemoryBarrier();
5085 #endif
5086 }
5087
5088 /*
5089 ** Initialize and deinitialize the mutex subsystem.
5090 */
5091 static sqlite3_mutex winMutex_staticMutexes[] = {
5092 SQLITE3_MUTEX_INITIALIZER,
5093 SQLITE3_MUTEX_INITIALIZER,
5094 SQLITE3_MUTEX_INITIALIZER,
5095 SQLITE3_MUTEX_INITIALIZER,
5096 SQLITE3_MUTEX_INITIALIZER,
5097 SQLITE3_MUTEX_INITIALIZER,
5098 SQLITE3_MUTEX_INITIALIZER,
5099 SQLITE3_MUTEX_INITIALIZER,
5100 SQLITE3_MUTEX_INITIALIZER,
5101 SQLITE3_MUTEX_INITIALIZER,
5102 SQLITE3_MUTEX_INITIALIZER,
5103 SQLITE3_MUTEX_INITIALIZER
5104 };
5105
5106 static int winMutex_isInit = 0;
5107 static int winMutex_isNt = -1; /* <0 means "need to query" */
5108
5109 /* As the winMutexInit() and winMutexEnd() functions are called as part
5110 ** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
5111 ** "interlocked" magic used here is probably not strictly necessary.
5112 */
5113 static LONG SQLITE_WIN32_VOLATILE winMutex_lock = 0;
5114
5115 SQLITE_API int sqlite3_win32_is_nt(void); /* os_win.c */
5116 SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
5117
5118 static int winMutexInit(void){
5119 /* The first to increment to 1 does actual initialization */
5120 if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
5121 int i;
5122 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
5123 #if SQLITE_OS_WINRT
5124 InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0);
5125 #else
5126 InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
5127 #endif
5128 }
5129 winMutex_isInit = 1;
5130 }else{
5131 /* Another thread is (in the process of) initializing the static
5132 ** mutexes */
5133 while( !winMutex_isInit ){
5134 sqlite3_win32_sleep(1);
5135 }
5136 }
5137 return SQLITE_OK;
5138 }
5139
5140 static int winMutexEnd(void){
5141 /* The first to decrement to 0 does actual shutdown
5142 ** (which should be the last to shutdown.) */
5143 if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
5144 if( winMutex_isInit==1 ){
5145 int i;
5146 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
5147 DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
5148 }
5149 winMutex_isInit = 0;
5150 }
5151 }
5152 return SQLITE_OK;
5153 }
5154
5155 /*
5156 ** The sqlite3_mutex_alloc() routine allocates a new
5157 ** mutex and returns a pointer to it. If it returns NULL
5158 ** that means that a mutex could not be allocated. SQLite
5159 ** will unwind its stack and return an error. The argument
5160 ** to sqlite3_mutex_alloc() is one of these integer constants:
5161 **
5162 ** <ul>
5163 ** <li> SQLITE_MUTEX_FAST
5164 ** <li> SQLITE_MUTEX_RECURSIVE
5165 ** <li> SQLITE_MUTEX_STATIC_MASTER
5166 ** <li> SQLITE_MUTEX_STATIC_MEM
5167 ** <li> SQLITE_MUTEX_STATIC_OPEN
5168 ** <li> SQLITE_MUTEX_STATIC_PRNG
5169 ** <li> SQLITE_MUTEX_STATIC_LRU
5170 ** <li> SQLITE_MUTEX_STATIC_PMEM
5171 ** <li> SQLITE_MUTEX_STATIC_APP1
5172 ** <li> SQLITE_MUTEX_STATIC_APP2
5173 ** <li> SQLITE_MUTEX_STATIC_APP3
5174 ** <li> SQLITE_MUTEX_STATIC_VFS1
5175 ** <li> SQLITE_MUTEX_STATIC_VFS2
5176 ** <li> SQLITE_MUTEX_STATIC_VFS3
5177 ** </ul>
5178 **
5179 ** The first two constants cause sqlite3_mutex_alloc() to create
5180 ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
5181 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
5182 ** The mutex implementation does not need to make a distinction
5183 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
5184 ** not want to. But SQLite will only request a recursive mutex in
5185 ** cases where it really needs one. If a faster non-recursive mutex
5186 ** implementation is available on the host platform, the mutex subsystem
5187 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
5188 **
5189 ** The other allowed parameters to sqlite3_mutex_alloc() each return
5190 ** a pointer to a static preexisting mutex. Six static mutexes are
5191 ** used by the current version of SQLite. Future versions of SQLite
5192 ** may add additional static mutexes. Static mutexes are for internal
5193 ** use by SQLite only. Applications that use SQLite mutexes should
5194 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
5195 ** SQLITE_MUTEX_RECURSIVE.
5196 **
5197 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
5198 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
5199 ** returns a different mutex on every call. But for the static
5200 ** mutex types, the same mutex is returned on every call that has
5201 ** the same type number.
5202 */
5203 static sqlite3_mutex *winMutexAlloc(int iType){
5204 sqlite3_mutex *p;
5205
5206 switch( iType ){
5207 case SQLITE_MUTEX_FAST:
5208 case SQLITE_MUTEX_RECURSIVE: {
5209 p = sqlite3MallocZero( sizeof(*p) );
5210 if( p ){
5211 p->id = iType;
5212 #ifdef SQLITE_DEBUG
5213 #ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC
5214 p->trace = 1;
5215 #endif
5216 #endif
5217 #if SQLITE_OS_WINRT
5218 InitializeCriticalSectionEx(&p->mutex, 0, 0);
5219 #else
5220 InitializeCriticalSection(&p->mutex);
5221 #endif
5222 }
5223 break;
5224 }
5225 default: {
5226 #ifdef SQLITE_ENABLE_API_ARMOR
5227 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){
5228 (void)SQLITE_MISUSE_BKPT;
5229 return 0;
5230 }
5231 #endif
5232 p = &winMutex_staticMutexes[iType-2];
5233 p->id = iType;
5234 #ifdef SQLITE_DEBUG
5235 #ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
5236 p->trace = 1;
5237 #endif
5238 #endif
5239 break;
5240 }
5241 }
5242 return p;
5243 }
5244
5245
5246 /*
5247 ** This routine deallocates a previously
5248 ** allocated mutex. SQLite is careful to deallocate every
5249 ** mutex that it allocates.
5250 */
5251 static void winMutexFree(sqlite3_mutex *p){
5252 assert( p );
5253 assert( p->nRef==0 && p->owner==0 );
5254 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){
5255 DeleteCriticalSection(&p->mutex);
5256 sqlite3_free(p);
5257 }else{
5258 #ifdef SQLITE_ENABLE_API_ARMOR
5259 (void)SQLITE_MISUSE_BKPT;
5260 #endif
5261 }
5262 }
5263
5264 /*
5265 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
5266 ** to enter a mutex. If another thread is already within the mutex,
5267 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
5268 ** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK
5269 ** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can
5270 ** be entered multiple times by the same thread. In such cases the,
5271 ** mutex must be exited an equal number of times before another thread
5272 ** can enter. If the same thread tries to enter any other kind of mutex
5273 ** more than once, the behavior is undefined.
5274 */
5275 static void winMutexEnter(sqlite3_mutex *p){
5276 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
5277 DWORD tid = GetCurrentThreadId();
5278 #endif
5279 #ifdef SQLITE_DEBUG
5280 assert( p );
5281 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
5282 #else
5283 assert( p );
5284 #endif
5285 assert( winMutex_isInit==1 );
5286 EnterCriticalSection(&p->mutex);
5287 #ifdef SQLITE_DEBUG
5288 assert( p->nRef>0 || p->owner==0 );
5289 p->owner = tid;
5290 p->nRef++;
5291 if( p->trace ){
5292 OSTRACE(("ENTER-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
5293 tid, p, p->trace, p->nRef));
5294 }
5295 #endif
5296 }
5297
5298 static int winMutexTry(sqlite3_mutex *p){
5299 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
5300 DWORD tid = GetCurrentThreadId();
5301 #endif
5302 int rc = SQLITE_BUSY;
5303 assert( p );
5304 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
5305 /*
5306 ** The sqlite3_mutex_try() routine is very rarely used, and when it
5307 ** is used it is merely an optimization. So it is OK for it to always
5308 ** fail.
5309 **
5310 ** The TryEnterCriticalSection() interface is only available on WinNT.
5311 ** And some windows compilers complain if you try to use it without
5312 ** first doing some #defines that prevent SQLite from building on Win98.
5313 ** For that reason, we will omit this optimization for now. See
5314 ** ticket #2685.
5315 */
5316 #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
5317 assert( winMutex_isInit==1 );
5318 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 );
5319 if( winMutex_isNt<0 ){
5320 winMutex_isNt = sqlite3_win32_is_nt();
5321 }
5322 assert( winMutex_isNt==0 || winMutex_isNt==1 );
5323 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
5324 #ifdef SQLITE_DEBUG
5325 p->owner = tid;
5326 p->nRef++;
5327 #endif
5328 rc = SQLITE_OK;
5329 }
5330 #else
5331 UNUSED_PARAMETER(p);
5332 #endif
5333 #ifdef SQLITE_DEBUG
5334 if( p->trace ){
5335 OSTRACE(("TRY-MUTEX tid=%lu, mutex=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
5336 tid, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
5337 }
5338 #endif
5339 return rc;
5340 }
5341
5342 /*
5343 ** The sqlite3_mutex_leave() routine exits a mutex that was
5344 ** previously entered by the same thread. The behavior
5345 ** is undefined if the mutex is not currently entered or
5346 ** is not currently allocated. SQLite will never do either.
5347 */
5348 static void winMutexLeave(sqlite3_mutex *p){
5349 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
5350 DWORD tid = GetCurrentThreadId();
5351 #endif
5352 assert( p );
5353 #ifdef SQLITE_DEBUG
5354 assert( p->nRef>0 );
5355 assert( p->owner==tid );
5356 p->nRef--;
5357 if( p->nRef==0 ) p->owner = 0;
5358 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
5359 #endif
5360 assert( winMutex_isInit==1 );
5361 LeaveCriticalSection(&p->mutex);
5362 #ifdef SQLITE_DEBUG
5363 if( p->trace ){
5364 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
5365 tid, p, p->trace, p->nRef));
5366 }
5367 #endif
5368 }
5369
5370 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
5371 static const sqlite3_mutex_methods sMutex = {
5372 winMutexInit,
5373 winMutexEnd,
5374 winMutexAlloc,
5375 winMutexFree,
5376 winMutexEnter,
5377 winMutexTry,
5378 winMutexLeave,
5379 #ifdef SQLITE_DEBUG
5380 winMutexHeld,
5381 winMutexNotheld
5382 #else
5383 0,
5384 0
5385 #endif
5386 };
5387 return &sMutex;
5388 }
5389
5390 #endif /* SQLITE_MUTEX_W32 */
5391
5392 /************** End of mutex_w32.c *******************************************/
5393 /************** Begin file malloc.c ******************************************/
5394 /*
5395 ** 2001 September 15
5396 **
5397 ** The author disclaims copyright to this source code. In place of
5398 ** a legal notice, here is a blessing:
5399 **
5400 ** May you do good and not evil.
5401 ** May you find forgiveness for yourself and forgive others.
5402 ** May you share freely, never taking more than you give.
5403 **
5404 *************************************************************************
5405 **
5406 ** Memory allocation functions used throughout sqlite.
5407 */
5408 /* #include "sqliteInt.h" */
5409 /* #include <stdarg.h> */
5410
5411 /*
5412 ** Attempt to release up to n bytes of non-essential memory currently
5413 ** held by SQLite. An example of non-essential memory is memory used to
5414 ** cache database pages that are not currently in use.
5415 */
5416 SQLITE_API int sqlite3_release_memory(int n){
5417 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
5418 return sqlite3PcacheReleaseMemory(n);
5419 #else
5420 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
5421 ** is a no-op returning zero if SQLite is not compiled with
5422 ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
5423 UNUSED_PARAMETER(n);
5424 return 0;
5425 #endif
5426 }
5427
5428 /*
5429 ** An instance of the following object records the location of
5430 ** each unused scratch buffer.
5431 */
5432 typedef struct ScratchFreeslot {
5433 struct ScratchFreeslot *pNext; /* Next unused scratch buffer */
5434 } ScratchFreeslot;
5435
5436 /*
5437 ** State information local to the memory allocation subsystem.
5438 */
5439 static SQLITE_WSD struct Mem0Global {
5440 sqlite3_mutex *mutex; /* Mutex to serialize access */
5441 sqlite3_int64 alarmThreshold; /* The soft heap limit */
5442
5443 /*
5444 ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
5445 ** (so that a range test can be used to determine if an allocation
5446 ** being freed came from pScratch) and a pointer to the list of
5447 ** unused scratch allocations.
5448 */
5449 void *pScratchEnd;
5450 ScratchFreeslot *pScratchFree;
5451 u32 nScratchFree;
5452
5453 /*
5454 ** True if heap is nearly "full" where "full" is defined by the
5455 ** sqlite3_soft_heap_limit() setting.
5456 */
5457 int nearlyFull;
5458 } mem0 = { 0, 0, 0, 0, 0, 0 };
5459
5460 #define mem0 GLOBAL(struct Mem0Global, mem0)
5461
5462 /*
5463 ** Return the memory allocator mutex. sqlite3_status() needs it.
5464 */
5465 SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void){
5466 return mem0.mutex;
5467 }
5468
5469 #ifndef SQLITE_OMIT_DEPRECATED
5470 /*
5471 ** Deprecated external interface. It used to set an alarm callback
5472 ** that was invoked when memory usage grew too large. Now it is a
5473 ** no-op.
5474 */
5475 SQLITE_API int sqlite3_memory_alarm(
5476 void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
5477 void *pArg,
5478 sqlite3_int64 iThreshold
5479 ){
5480 (void)xCallback;
5481 (void)pArg;
5482 (void)iThreshold;
5483 return SQLITE_OK;
5484 }
5485 #endif
5486
5487 /*
5488 ** Set the soft heap-size limit for the library. Passing a zero or
5489 ** negative value indicates no limit.
5490 */
5491 SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
5492 sqlite3_int64 priorLimit;
5493 sqlite3_int64 excess;
5494 sqlite3_int64 nUsed;
5495 #ifndef SQLITE_OMIT_AUTOINIT
5496 int rc = sqlite3_initialize();
5497 if( rc ) return -1;
5498 #endif
5499 sqlite3_mutex_enter(mem0.mutex);
5500 priorLimit = mem0.alarmThreshold;
5501 if( n<0 ){
5502 sqlite3_mutex_leave(mem0.mutex);
5503 return priorLimit;
5504 }
5505 mem0.alarmThreshold = n;
5506 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
5507 mem0.nearlyFull = (n>0 && n<=nUsed);
5508 sqlite3_mutex_leave(mem0.mutex);
5509 excess = sqlite3_memory_used() - n;
5510 if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
5511 return priorLimit;
5512 }
5513 SQLITE_API void sqlite3_soft_heap_limit(int n){
5514 if( n<0 ) n = 0;
5515 sqlite3_soft_heap_limit64(n);
5516 }
5517
5518 /*
5519 ** Initialize the memory allocation subsystem.
5520 */
5521 SQLITE_PRIVATE int sqlite3MallocInit(void){
5522 int rc;
5523 if( sqlite3GlobalConfig.m.xMalloc==0 ){
5524 sqlite3MemSetDefault();
5525 }
5526 memset(&mem0, 0, sizeof(mem0));
5527 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
5528 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
5529 && sqlite3GlobalConfig.nScratch>0 ){
5530 int i, n, sz;
5531 ScratchFreeslot *pSlot;
5532 sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
5533 sqlite3GlobalConfig.szScratch = sz;
5534 pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
5535 n = sqlite3GlobalConfig.nScratch;
5536 mem0.pScratchFree = pSlot;
5537 mem0.nScratchFree = n;
5538 for(i=0; i<n-1; i++){
5539 pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
5540 pSlot = pSlot->pNext;
5541 }
5542 pSlot->pNext = 0;
5543 mem0.pScratchEnd = (void*)&pSlot[1];
5544 }else{
5545 mem0.pScratchEnd = 0;
5546 sqlite3GlobalConfig.pScratch = 0;
5547 sqlite3GlobalConfig.szScratch = 0;
5548 sqlite3GlobalConfig.nScratch = 0;
5549 }
5550 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
5551 || sqlite3GlobalConfig.nPage<=0 ){
5552 sqlite3GlobalConfig.pPage = 0;
5553 sqlite3GlobalConfig.szPage = 0;
5554 }
5555 rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
5556 if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));
5557 return rc;
5558 }
5559
5560 /*
5561 ** Return true if the heap is currently under memory pressure - in other
5562 ** words if the amount of heap used is close to the limit set by
5563 ** sqlite3_soft_heap_limit().
5564 */
5565 SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){
5566 return mem0.nearlyFull;
5567 }
5568
5569 /*
5570 ** Deinitialize the memory allocation subsystem.
5571 */
5572 SQLITE_PRIVATE void sqlite3MallocEnd(void){
5573 if( sqlite3GlobalConfig.m.xShutdown ){
5574 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
5575 }
5576 memset(&mem0, 0, sizeof(mem0));
5577 }
5578
5579 /*
5580 ** Return the amount of memory currently checked out.
5581 */
5582 SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
5583 sqlite3_int64 res, mx;
5584 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0);
5585 return res;
5586 }
5587
5588 /*
5589 ** Return the maximum amount of memory that has ever been
5590 ** checked out since either the beginning of this process
5591 ** or since the most recent reset.
5592 */
5593 SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
5594 sqlite3_int64 res, mx;
5595 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag);
5596 return mx;
5597 }
5598
5599 /*
5600 ** Trigger the alarm
5601 */
5602 static void sqlite3MallocAlarm(int nByte){
5603 if( mem0.alarmThreshold<=0 ) return;
5604 sqlite3_mutex_leave(mem0.mutex);
5605 sqlite3_release_memory(nByte);
5606 sqlite3_mutex_enter(mem0.mutex);
5607 }
5608
5609 /*
5610 ** Do a memory allocation with statistics and alarms. Assume the
5611 ** lock is already held.
5612 */
5613 static void mallocWithAlarm(int n, void **pp){
5614 void *p;
5615 int nFull;
5616 assert( sqlite3_mutex_held(mem0.mutex) );
5617 assert( n>0 );
5618
5619 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
5620 ** implementation of malloc_good_size(), which must be called in debug
5621 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
5622 ** or else a crash results. Hence, do not attempt to optimize out the
5623 ** following xRoundup() call. */
5624 nFull = sqlite3GlobalConfig.m.xRoundup(n);
5625
5626 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n);
5627 if( mem0.alarmThreshold>0 ){
5628 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
5629 if( nUsed >= mem0.alarmThreshold - nFull ){
5630 mem0.nearlyFull = 1;
5631 sqlite3MallocAlarm(nFull);
5632 }else{
5633 mem0.nearlyFull = 0;
5634 }
5635 }
5636 p = sqlite3GlobalConfig.m.xMalloc(nFull);
5637 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
5638 if( p==0 && mem0.alarmThreshold>0 ){
5639 sqlite3MallocAlarm(nFull);
5640 p = sqlite3GlobalConfig.m.xMalloc(nFull);
5641 }
5642 #endif
5643 if( p ){
5644 nFull = sqlite3MallocSize(p);
5645 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull);
5646 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1);
5647 }
5648 *pp = p;
5649 }
5650
5651 /*
5652 ** Allocate memory. This routine is like sqlite3_malloc() except that it
5653 ** assumes the memory subsystem has already been initialized.
5654 */
5655 SQLITE_PRIVATE void *sqlite3Malloc(u64 n){
5656 void *p;
5657 if( n==0 || n>=0x7fffff00 ){
5658 /* A memory allocation of a number of bytes which is near the maximum
5659 ** signed integer value might cause an integer overflow inside of the
5660 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving
5661 ** 255 bytes of overhead. SQLite itself will never use anything near
5662 ** this amount. The only way to reach the limit is with sqlite3_malloc() */
5663 p = 0;
5664 }else if( sqlite3GlobalConfig.bMemstat ){
5665 sqlite3_mutex_enter(mem0.mutex);
5666 mallocWithAlarm((int)n, &p);
5667 sqlite3_mutex_leave(mem0.mutex);
5668 }else{
5669 p = sqlite3GlobalConfig.m.xMalloc((int)n);
5670 }
5671 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
5672 return p;
5673 }
5674
5675 /*
5676 ** This version of the memory allocation is for use by the application.
5677 ** First make sure the memory subsystem is initialized, then do the
5678 ** allocation.
5679 */
5680 SQLITE_API void *sqlite3_malloc(int n){
5681 #ifndef SQLITE_OMIT_AUTOINIT
5682 if( sqlite3_initialize() ) return 0;
5683 #endif
5684 return n<=0 ? 0 : sqlite3Malloc(n);
5685 }
5686 SQLITE_API void *sqlite3_malloc64(sqlite3_uint64 n){
5687 #ifndef SQLITE_OMIT_AUTOINIT
5688 if( sqlite3_initialize() ) return 0;
5689 #endif
5690 return sqlite3Malloc(n);
5691 }
5692
5693 /*
5694 ** Each thread may only have a single outstanding allocation from
5695 ** xScratchMalloc(). We verify this constraint in the single-threaded
5696 ** case by setting scratchAllocOut to 1 when an allocation
5697 ** is outstanding clearing it when the allocation is freed.
5698 */
5699 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
5700 static int scratchAllocOut = 0;
5701 #endif
5702
5703
5704 /*
5705 ** Allocate memory that is to be used and released right away.
5706 ** This routine is similar to alloca() in that it is not intended
5707 ** for situations where the memory might be held long-term. This
5708 ** routine is intended to get memory to old large transient data
5709 ** structures that would not normally fit on the stack of an
5710 ** embedded processor.
5711 */
5712 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){
5713 void *p;
5714 assert( n>0 );
5715
5716 sqlite3_mutex_enter(mem0.mutex);
5717 sqlite3StatusHighwater(SQLITE_STATUS_SCRATCH_SIZE, n);
5718 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
5719 p = mem0.pScratchFree;
5720 mem0.pScratchFree = mem0.pScratchFree->pNext;
5721 mem0.nScratchFree--;
5722 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1);
5723 sqlite3_mutex_leave(mem0.mutex);
5724 }else{
5725 sqlite3_mutex_leave(mem0.mutex);
5726 p = sqlite3Malloc(n);
5727 if( sqlite3GlobalConfig.bMemstat && p ){
5728 sqlite3_mutex_enter(mem0.mutex);
5729 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p));
5730 sqlite3_mutex_leave(mem0.mutex);
5731 }
5732 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
5733 }
5734 assert( sqlite3_mutex_notheld(mem0.mutex) );
5735
5736
5737 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
5738 /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch
5739 ** buffers per thread.
5740 **
5741 ** This can only be checked in single-threaded mode.
5742 */
5743 assert( scratchAllocOut==0 );
5744 if( p ) scratchAllocOut++;
5745 #endif
5746
5747 return p;
5748 }
5749 SQLITE_PRIVATE void sqlite3ScratchFree(void *p){
5750 if( p ){
5751
5752 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
5753 /* Verify that no more than two scratch allocation per thread
5754 ** is outstanding at one time. (This is only checked in the
5755 ** single-threaded case since checking in the multi-threaded case
5756 ** would be much more complicated.) */
5757 assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
5758 scratchAllocOut--;
5759 #endif
5760
5761 if( SQLITE_WITHIN(p, sqlite3GlobalConfig.pScratch, mem0.pScratchEnd) ){
5762 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
5763 ScratchFreeslot *pSlot;
5764 pSlot = (ScratchFreeslot*)p;
5765 sqlite3_mutex_enter(mem0.mutex);
5766 pSlot->pNext = mem0.pScratchFree;
5767 mem0.pScratchFree = pSlot;
5768 mem0.nScratchFree++;
5769 assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
5770 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1);
5771 sqlite3_mutex_leave(mem0.mutex);
5772 }else{
5773 /* Release memory back to the heap */
5774 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
5775 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) );
5776 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
5777 if( sqlite3GlobalConfig.bMemstat ){
5778 int iSize = sqlite3MallocSize(p);
5779 sqlite3_mutex_enter(mem0.mutex);
5780 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize);
5781 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize);
5782 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
5783 sqlite3GlobalConfig.m.xFree(p);
5784 sqlite3_mutex_leave(mem0.mutex);
5785 }else{
5786 sqlite3GlobalConfig.m.xFree(p);
5787 }
5788 }
5789 }
5790 }
5791
5792 /*
5793 ** TRUE if p is a lookaside memory allocation from db
5794 */
5795 #ifndef SQLITE_OMIT_LOOKASIDE
5796 static int isLookaside(sqlite3 *db, void *p){
5797 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
5798 }
5799 #else
5800 #define isLookaside(A,B) 0
5801 #endif
5802
5803 /*
5804 ** Return the size of a memory allocation previously obtained from
5805 ** sqlite3Malloc() or sqlite3_malloc().
5806 */
5807 SQLITE_PRIVATE int sqlite3MallocSize(void *p){
5808 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
5809 return sqlite3GlobalConfig.m.xSize(p);
5810 }
5811 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
5812 assert( p!=0 );
5813 if( db==0 || !isLookaside(db,p) ){
5814 #if SQLITE_DEBUG
5815 if( db==0 ){
5816 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
5817 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
5818 }else{
5819 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
5820 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
5821 }
5822 #endif
5823 return sqlite3GlobalConfig.m.xSize(p);
5824 }else{
5825 assert( sqlite3_mutex_held(db->mutex) );
5826 return db->lookaside.sz;
5827 }
5828 }
5829 SQLITE_API sqlite3_uint64 sqlite3_msize(void *p){
5830 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
5831 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
5832 return p ? sqlite3GlobalConfig.m.xSize(p) : 0;
5833 }
5834
5835 /*
5836 ** Free memory previously obtained from sqlite3Malloc().
5837 */
5838 SQLITE_API void sqlite3_free(void *p){
5839 if( p==0 ) return; /* IMP: R-49053-54554 */
5840 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
5841 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
5842 if( sqlite3GlobalConfig.bMemstat ){
5843 sqlite3_mutex_enter(mem0.mutex);
5844 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p));
5845 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1);
5846 sqlite3GlobalConfig.m.xFree(p);
5847 sqlite3_mutex_leave(mem0.mutex);
5848 }else{
5849 sqlite3GlobalConfig.m.xFree(p);
5850 }
5851 }
5852
5853 /*
5854 ** Add the size of memory allocation "p" to the count in
5855 ** *db->pnBytesFreed.
5856 */
5857 static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){
5858 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
5859 }
5860
5861 /*
5862 ** Free memory that might be associated with a particular database
5863 ** connection.
5864 */
5865 SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
5866 assert( db==0 || sqlite3_mutex_held(db->mutex) );
5867 if( p==0 ) return;
5868 if( db ){
5869 if( db->pnBytesFreed ){
5870 measureAllocationSize(db, p);
5871 return;
5872 }
5873 if( isLookaside(db, p) ){
5874 LookasideSlot *pBuf = (LookasideSlot*)p;
5875 #if SQLITE_DEBUG
5876 /* Trash all content in the buffer being freed */
5877 memset(p, 0xaa, db->lookaside.sz);
5878 #endif
5879 pBuf->pNext = db->lookaside.pFree;
5880 db->lookaside.pFree = pBuf;
5881 db->lookaside.nOut--;
5882 return;
5883 }
5884 }
5885 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
5886 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
5887 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
5888 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
5889 sqlite3_free(p);
5890 }
5891
5892 /*
5893 ** Change the size of an existing memory allocation
5894 */
5895 SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){
5896 int nOld, nNew, nDiff;
5897 void *pNew;
5898 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
5899 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) );
5900 if( pOld==0 ){
5901 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
5902 }
5903 if( nBytes==0 ){
5904 sqlite3_free(pOld); /* IMP: R-26507-47431 */
5905 return 0;
5906 }
5907 if( nBytes>=0x7fffff00 ){
5908 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
5909 return 0;
5910 }
5911 nOld = sqlite3MallocSize(pOld);
5912 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
5913 ** argument to xRealloc is always a value returned by a prior call to
5914 ** xRoundup. */
5915 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes);
5916 if( nOld==nNew ){
5917 pNew = pOld;
5918 }else if( sqlite3GlobalConfig.bMemstat ){
5919 sqlite3_mutex_enter(mem0.mutex);
5920 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
5921 nDiff = nNew - nOld;
5922 if( nDiff>0 && sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
5923 mem0.alarmThreshold-nDiff ){
5924 sqlite3MallocAlarm(nDiff);
5925 }
5926 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
5927 if( pNew==0 && mem0.alarmThreshold>0 ){
5928 sqlite3MallocAlarm((int)nBytes);
5929 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
5930 }
5931 if( pNew ){
5932 nNew = sqlite3MallocSize(pNew);
5933 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
5934 }
5935 sqlite3_mutex_leave(mem0.mutex);
5936 }else{
5937 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
5938 }
5939 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
5940 return pNew;
5941 }
5942
5943 /*
5944 ** The public interface to sqlite3Realloc. Make sure that the memory
5945 ** subsystem is initialized prior to invoking sqliteRealloc.
5946 */
5947 SQLITE_API void *sqlite3_realloc(void *pOld, int n){
5948 #ifndef SQLITE_OMIT_AUTOINIT
5949 if( sqlite3_initialize() ) return 0;
5950 #endif
5951 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
5952 return sqlite3Realloc(pOld, n);
5953 }
5954 SQLITE_API void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
5955 #ifndef SQLITE_OMIT_AUTOINIT
5956 if( sqlite3_initialize() ) return 0;
5957 #endif
5958 return sqlite3Realloc(pOld, n);
5959 }
5960
5961
5962 /*
5963 ** Allocate and zero memory.
5964 */
5965 SQLITE_PRIVATE void *sqlite3MallocZero(u64 n){
5966 void *p = sqlite3Malloc(n);
5967 if( p ){
5968 memset(p, 0, (size_t)n);
5969 }
5970 return p;
5971 }
5972
5973 /*
5974 ** Allocate and zero memory. If the allocation fails, make
5975 ** the mallocFailed flag in the connection pointer.
5976 */
5977 SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
5978 void *p;
5979 testcase( db==0 );
5980 p = sqlite3DbMallocRaw(db, n);
5981 if( p ) memset(p, 0, (size_t)n);
5982 return p;
5983 }
5984
5985
5986 /* Finish the work of sqlite3DbMallocRawNN for the unusual and
5987 ** slower case when the allocation cannot be fulfilled using lookaside.
5988 */
5989 static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
5990 void *p;
5991 assert( db!=0 );
5992 p = sqlite3Malloc(n);
5993 if( !p ) sqlite3OomFault(db);
5994 sqlite3MemdebugSetType(p,
5995 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
5996 return p;
5997 }
5998
5999 /*
6000 ** Allocate memory, either lookaside (if possible) or heap.
6001 ** If the allocation fails, set the mallocFailed flag in
6002 ** the connection pointer.
6003 **
6004 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
6005 ** failure on the same database connection) then always return 0.
6006 ** Hence for a particular database connection, once malloc starts
6007 ** failing, it fails consistently until mallocFailed is reset.
6008 ** This is an important assumption. There are many places in the
6009 ** code that do things like this:
6010 **
6011 ** int *a = (int*)sqlite3DbMallocRaw(db, 100);
6012 ** int *b = (int*)sqlite3DbMallocRaw(db, 200);
6013 ** if( b ) a[10] = 9;
6014 **
6015 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
6016 ** that all prior mallocs (ex: "a") worked too.
6017 **
6018 ** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
6019 ** not a NULL pointer.
6020 */
6021 SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
6022 void *p;
6023 if( db ) return sqlite3DbMallocRawNN(db, n);
6024 p = sqlite3Malloc(n);
6025 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
6026 return p;
6027 }
6028 SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
6029 #ifndef SQLITE_OMIT_LOOKASIDE
6030 LookasideSlot *pBuf;
6031 assert( db!=0 );
6032 assert( sqlite3_mutex_held(db->mutex) );
6033 assert( db->pnBytesFreed==0 );
6034 if( db->lookaside.bDisable==0 ){
6035 assert( db->mallocFailed==0 );
6036 if( n>db->lookaside.sz ){
6037 db->lookaside.anStat[1]++;
6038 }else if( (pBuf = db->lookaside.pFree)==0 ){
6039 db->lookaside.anStat[2]++;
6040 }else{
6041 db->lookaside.pFree = pBuf->pNext;
6042 db->lookaside.nOut++;
6043 db->lookaside.anStat[0]++;
6044 if( db->lookaside.nOut>db->lookaside.mxOut ){
6045 db->lookaside.mxOut = db->lookaside.nOut;
6046 }
6047 return (void*)pBuf;
6048 }
6049 }else if( db->mallocFailed ){
6050 return 0;
6051 }
6052 #else
6053 assert( db!=0 );
6054 assert( sqlite3_mutex_held(db->mutex) );
6055 assert( db->pnBytesFreed==0 );
6056 if( db->mallocFailed ){
6057 return 0;
6058 }
6059 #endif
6060 return dbMallocRawFinish(db, n);
6061 }
6062
6063 /* Forward declaration */
6064 static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
6065
6066 /*
6067 ** Resize the block of memory pointed to by p to n bytes. If the
6068 ** resize fails, set the mallocFailed flag in the connection object.
6069 */
6070 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
6071 assert( db!=0 );
6072 if( p==0 ) return sqlite3DbMallocRawNN(db, n);
6073 assert( sqlite3_mutex_held(db->mutex) );
6074 if( isLookaside(db,p) && n<=db->lookaside.sz ) return p;
6075 return dbReallocFinish(db, p, n);
6076 }
6077 static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
6078 void *pNew = 0;
6079 assert( db!=0 );
6080 assert( p!=0 );
6081 if( db->mallocFailed==0 ){
6082 if( isLookaside(db, p) ){
6083 pNew = sqlite3DbMallocRawNN(db, n);
6084 if( pNew ){
6085 memcpy(pNew, p, db->lookaside.sz);
6086 sqlite3DbFree(db, p);
6087 }
6088 }else{
6089 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
6090 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
6091 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
6092 pNew = sqlite3_realloc64(p, n);
6093 if( !pNew ){
6094 sqlite3OomFault(db);
6095 }
6096 sqlite3MemdebugSetType(pNew,
6097 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
6098 }
6099 }
6100 return pNew;
6101 }
6102
6103 /*
6104 ** Attempt to reallocate p. If the reallocation fails, then free p
6105 ** and set the mallocFailed flag in the database connection.
6106 */
6107 SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){
6108 void *pNew;
6109 pNew = sqlite3DbRealloc(db, p, n);
6110 if( !pNew ){
6111 sqlite3DbFree(db, p);
6112 }
6113 return pNew;
6114 }
6115
6116 /*
6117 ** Make a copy of a string in memory obtained from sqliteMalloc(). These
6118 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
6119 ** is because when memory debugging is turned on, these two functions are
6120 ** called via macros that record the current file and line number in the
6121 ** ThreadData structure.
6122 */
6123 SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
6124 char *zNew;
6125 size_t n;
6126 if( z==0 ){
6127 return 0;
6128 }
6129 n = strlen(z) + 1;
6130 zNew = sqlite3DbMallocRaw(db, n);
6131 if( zNew ){
6132 memcpy(zNew, z, n);
6133 }
6134 return zNew;
6135 }
6136 SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
6137 char *zNew;
6138 assert( db!=0 );
6139 if( z==0 ){
6140 return 0;
6141 }
6142 assert( (n&0x7fffffff)==n );
6143 zNew = sqlite3DbMallocRawNN(db, n+1);
6144 if( zNew ){
6145 memcpy(zNew, z, (size_t)n);
6146 zNew[n] = 0;
6147 }
6148 return zNew;
6149 }
6150
6151 /*
6152 ** Free any prior content in *pz and replace it with a copy of zNew.
6153 */
6154 SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
6155 sqlite3DbFree(db, *pz);
6156 *pz = sqlite3DbStrDup(db, zNew);
6157 }
6158
6159 /*
6160 ** Call this routine to record the fact that an OOM (out-of-memory) error
6161 ** has happened. This routine will set db->mallocFailed, and also
6162 ** temporarily disable the lookaside memory allocator and interrupt
6163 ** any running VDBEs.
6164 */
6165 SQLITE_PRIVATE void sqlite3OomFault(sqlite3 *db){
6166 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
6167 db->mallocFailed = 1;
6168 if( db->nVdbeExec>0 ){
6169 db->u1.isInterrupted = 1;
6170 }
6171 db->lookaside.bDisable++;
6172 }
6173 }
6174
6175 /*
6176 ** This routine reactivates the memory allocator and clears the
6177 ** db->mallocFailed flag as necessary.
6178 **
6179 ** The memory allocator is not restarted if there are running
6180 ** VDBEs.
6181 */
6182 SQLITE_PRIVATE void sqlite3OomClear(sqlite3 *db){
6183 if( db->mallocFailed && db->nVdbeExec==0 ){
6184 db->mallocFailed = 0;
6185 db->u1.isInterrupted = 0;
6186 assert( db->lookaside.bDisable>0 );
6187 db->lookaside.bDisable--;
6188 }
6189 }
6190
6191 /*
6192 ** Take actions at the end of an API call to indicate an OOM error
6193 */
6194 static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
6195 sqlite3OomClear(db);
6196 sqlite3Error(db, SQLITE_NOMEM);
6197 return SQLITE_NOMEM_BKPT;
6198 }
6199
6200 /*
6201 ** This function must be called before exiting any API function (i.e.
6202 ** returning control to the user) that has called sqlite3_malloc or
6203 ** sqlite3_realloc.
6204 **
6205 ** The returned value is normally a copy of the second argument to this
6206 ** function. However, if a malloc() failure has occurred since the previous
6207 ** invocation SQLITE_NOMEM is returned instead.
6208 **
6209 ** If an OOM as occurred, then the connection error-code (the value
6210 ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM.
6211 */
6212 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
6213 /* If the db handle must hold the connection handle mutex here.
6214 ** Otherwise the read (and possible write) of db->mallocFailed
6215 ** is unsafe, as is the call to sqlite3Error().
6216 */
6217 assert( db!=0 );
6218 assert( sqlite3_mutex_held(db->mutex) );
6219 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
6220 return apiOomError(db);
6221 }
6222 return rc & db->errMask;
6223 }
6224
6225 /************** End of malloc.c **********************************************/
6226 /************** Begin file printf.c ******************************************/
6227 /*
6228 ** The "printf" code that follows dates from the 1980's. It is in
6229 ** the public domain.
6230 **
6231 **************************************************************************
6232 **
6233 ** This file contains code for a set of "printf"-like routines. These
6234 ** routines format strings much like the printf() from the standard C
6235 ** library, though the implementation here has enhancements to support
6236 ** SQLite.
6237 */
6238 /* #include "sqliteInt.h" */
6239
6240 /*
6241 ** Conversion types fall into various categories as defined by the
6242 ** following enumeration.
6243 */
6244 #define etRADIX 0 /* Integer types. %d, %x, %o, and so forth */
6245 #define etFLOAT 1 /* Floating point. %f */
6246 #define etEXP 2 /* Exponentional notation. %e and %E */
6247 #define etGENERIC 3 /* Floating or exponential, depending on exponent. %g */
6248 #define etSIZE 4 /* Return number of characters processed so far. %n */
6249 #define etSTRING 5 /* Strings. %s */
6250 #define etDYNSTRING 6 /* Dynamically allocated strings. %z */
6251 #define etPERCENT 7 /* Percent symbol. %% */
6252 #define etCHARX 8 /* Characters. %c */
6253 /* The rest are extensions, not normally found in printf() */
6254 #define etSQLESCAPE 9 /* Strings with '\'' doubled. %q */
6255 #define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '',
6256 NULL pointers replaced by SQL NULL. %Q */
6257 #define etTOKEN 11 /* a pointer to a Token structure */
6258 #define etSRCLIST 12 /* a pointer to a SrcList */
6259 #define etPOINTER 13 /* The %p conversion */
6260 #define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
6261 #define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
6262
6263 #define etINVALID 16 /* Any unrecognized conversion type */
6264
6265
6266 /*
6267 ** An "etByte" is an 8-bit unsigned value.
6268 */
6269 typedef unsigned char etByte;
6270
6271 /*
6272 ** Each builtin conversion character (ex: the 'd' in "%d") is described
6273 ** by an instance of the following structure
6274 */
6275 typedef struct et_info { /* Information about each format field */
6276 char fmttype; /* The format field code letter */
6277 etByte base; /* The base for radix conversion */
6278 etByte flags; /* One or more of FLAG_ constants below */
6279 etByte type; /* Conversion paradigm */
6280 etByte charset; /* Offset into aDigits[] of the digits string */
6281 etByte prefix; /* Offset into aPrefix[] of the prefix string */
6282 } et_info;
6283
6284 /*
6285 ** Allowed values for et_info.flags
6286 */
6287 #define FLAG_SIGNED 1 /* True if the value to convert is signed */
6288 #define FLAG_STRING 4 /* Allow infinity precision */
6289
6290
6291 /*
6292 ** The following table is searched linearly, so it is good to put the
6293 ** most frequently used conversion types first.
6294 */
6295 static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
6296 static const char aPrefix[] = "-x0\000X0";
6297 static const et_info fmtinfo[] = {
6298 { 'd', 10, 1, etRADIX, 0, 0 },
6299 { 's', 0, 4, etSTRING, 0, 0 },
6300 { 'g', 0, 1, etGENERIC, 30, 0 },
6301 { 'z', 0, 4, etDYNSTRING, 0, 0 },
6302 { 'q', 0, 4, etSQLESCAPE, 0, 0 },
6303 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
6304 { 'w', 0, 4, etSQLESCAPE3, 0, 0 },
6305 { 'c', 0, 0, etCHARX, 0, 0 },
6306 { 'o', 8, 0, etRADIX, 0, 2 },
6307 { 'u', 10, 0, etRADIX, 0, 0 },
6308 { 'x', 16, 0, etRADIX, 16, 1 },
6309 { 'X', 16, 0, etRADIX, 0, 4 },
6310 #ifndef SQLITE_OMIT_FLOATING_POINT
6311 { 'f', 0, 1, etFLOAT, 0, 0 },
6312 { 'e', 0, 1, etEXP, 30, 0 },
6313 { 'E', 0, 1, etEXP, 14, 0 },
6314 { 'G', 0, 1, etGENERIC, 14, 0 },
6315 #endif
6316 { 'i', 10, 1, etRADIX, 0, 0 },
6317 { 'n', 0, 0, etSIZE, 0, 0 },
6318 { '%', 0, 0, etPERCENT, 0, 0 },
6319 { 'p', 16, 0, etPOINTER, 0, 1 },
6320
6321 /* All the rest are undocumented and are for internal use only */
6322 { 'T', 0, 0, etTOKEN, 0, 0 },
6323 { 'S', 0, 0, etSRCLIST, 0, 0 },
6324 { 'r', 10, 1, etORDINAL, 0, 0 },
6325 };
6326
6327 /*
6328 ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
6329 ** conversions will work.
6330 */
6331 #ifndef SQLITE_OMIT_FLOATING_POINT
6332 /*
6333 ** "*val" is a double such that 0.1 <= *val < 10.0
6334 ** Return the ascii code for the leading digit of *val, then
6335 ** multiply "*val" by 10.0 to renormalize.
6336 **
6337 ** Example:
6338 ** input: *val = 3.14159
6339 ** output: *val = 1.4159 function return = '3'
6340 **
6341 ** The counter *cnt is incremented each time. After counter exceeds
6342 ** 16 (the number of significant digits in a 64-bit float) '0' is
6343 ** always returned.
6344 */
6345 static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
6346 int digit;
6347 LONGDOUBLE_TYPE d;
6348 if( (*cnt)<=0 ) return '0';
6349 (*cnt)--;
6350 digit = (int)*val;
6351 d = digit;
6352 digit += '0';
6353 *val = (*val - d)*10.0;
6354 return (char)digit;
6355 }
6356 #endif /* SQLITE_OMIT_FLOATING_POINT */
6357
6358 /*
6359 ** Set the StrAccum object to an error mode.
6360 */
6361 static void setStrAccumError(StrAccum *p, u8 eError){
6362 assert( eError==STRACCUM_NOMEM || eError==STRACCUM_TOOBIG );
6363 p->accError = eError;
6364 p->nAlloc = 0;
6365 }
6366
6367 /*
6368 ** Extra argument values from a PrintfArguments object
6369 */
6370 static sqlite3_int64 getIntArg(PrintfArguments *p){
6371 if( p->nArg<=p->nUsed ) return 0;
6372 return sqlite3_value_int64(p->apArg[p->nUsed++]);
6373 }
6374 static double getDoubleArg(PrintfArguments *p){
6375 if( p->nArg<=p->nUsed ) return 0.0;
6376 return sqlite3_value_double(p->apArg[p->nUsed++]);
6377 }
6378 static char *getTextArg(PrintfArguments *p){
6379 if( p->nArg<=p->nUsed ) return 0;
6380 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
6381 }
6382
6383
6384 /*
6385 ** On machines with a small stack size, you can redefine the
6386 ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
6387 */
6388 #ifndef SQLITE_PRINT_BUF_SIZE
6389 # define SQLITE_PRINT_BUF_SIZE 70
6390 #endif
6391 #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
6392
6393 /*
6394 ** Render a string given by "fmt" into the StrAccum object.
6395 */
6396 SQLITE_PRIVATE void sqlite3VXPrintf(
6397 StrAccum *pAccum, /* Accumulate results here */
6398 const char *fmt, /* Format string */
6399 va_list ap /* arguments */
6400 ){
6401 int c; /* Next character in the format string */
6402 char *bufpt; /* Pointer to the conversion buffer */
6403 int precision; /* Precision of the current field */
6404 int length; /* Length of the field */
6405 int idx; /* A general purpose loop counter */
6406 int width; /* Width of the current field */
6407 etByte flag_leftjustify; /* True if "-" flag is present */
6408 etByte flag_plussign; /* True if "+" flag is present */
6409 etByte flag_blanksign; /* True if " " flag is present */
6410 etByte flag_alternateform; /* True if "#" flag is present */
6411 etByte flag_altform2; /* True if "!" flag is present */
6412 etByte flag_zeropad; /* True if field width constant starts with zero */
6413 etByte flag_long; /* True if "l" flag is present */
6414 etByte flag_longlong; /* True if the "ll" flag is present */
6415 etByte done; /* Loop termination flag */
6416 etByte xtype = etINVALID; /* Conversion paradigm */
6417 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
6418 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
6419 sqlite_uint64 longvalue; /* Value for integer types */
6420 LONGDOUBLE_TYPE realvalue; /* Value for real types */
6421 const et_info *infop; /* Pointer to the appropriate info structure */
6422 char *zOut; /* Rendering buffer */
6423 int nOut; /* Size of the rendering buffer */
6424 char *zExtra = 0; /* Malloced memory used by some conversion */
6425 #ifndef SQLITE_OMIT_FLOATING_POINT
6426 int exp, e2; /* exponent of real numbers */
6427 int nsd; /* Number of significant digits returned */
6428 double rounder; /* Used for rounding floating point values */
6429 etByte flag_dp; /* True if decimal point should be shown */
6430 etByte flag_rtz; /* True if trailing zeros should be removed */
6431 #endif
6432 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
6433 char buf[etBUFSIZE]; /* Conversion buffer */
6434
6435 bufpt = 0;
6436 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
6437 pArgList = va_arg(ap, PrintfArguments*);
6438 bArgList = 1;
6439 }else{
6440 bArgList = 0;
6441 }
6442 for(; (c=(*fmt))!=0; ++fmt){
6443 if( c!='%' ){
6444 bufpt = (char *)fmt;
6445 #if HAVE_STRCHRNUL
6446 fmt = strchrnul(fmt, '%');
6447 #else
6448 do{ fmt++; }while( *fmt && *fmt != '%' );
6449 #endif
6450 sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt));
6451 if( *fmt==0 ) break;
6452 }
6453 if( (c=(*++fmt))==0 ){
6454 sqlite3StrAccumAppend(pAccum, "%", 1);
6455 break;
6456 }
6457 /* Find out what flags are present */
6458 flag_leftjustify = flag_plussign = flag_blanksign =
6459 flag_alternateform = flag_altform2 = flag_zeropad = 0;
6460 done = 0;
6461 do{
6462 switch( c ){
6463 case '-': flag_leftjustify = 1; break;
6464 case '+': flag_plussign = 1; break;
6465 case ' ': flag_blanksign = 1; break;
6466 case '#': flag_alternateform = 1; break;
6467 case '!': flag_altform2 = 1; break;
6468 case '0': flag_zeropad = 1; break;
6469 default: done = 1; break;
6470 }
6471 }while( !done && (c=(*++fmt))!=0 );
6472 /* Get the field width */
6473 if( c=='*' ){
6474 if( bArgList ){
6475 width = (int)getIntArg(pArgList);
6476 }else{
6477 width = va_arg(ap,int);
6478 }
6479 if( width<0 ){
6480 flag_leftjustify = 1;
6481 width = width >= -2147483647 ? -width : 0;
6482 }
6483 c = *++fmt;
6484 }else{
6485 unsigned wx = 0;
6486 while( c>='0' && c<='9' ){
6487 wx = wx*10 + c - '0';
6488 c = *++fmt;
6489 }
6490 testcase( wx>0x7fffffff );
6491 width = wx & 0x7fffffff;
6492 }
6493 assert( width>=0 );
6494 #ifdef SQLITE_PRINTF_PRECISION_LIMIT
6495 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){
6496 width = SQLITE_PRINTF_PRECISION_LIMIT;
6497 }
6498 #endif
6499
6500 /* Get the precision */
6501 if( c=='.' ){
6502 c = *++fmt;
6503 if( c=='*' ){
6504 if( bArgList ){
6505 precision = (int)getIntArg(pArgList);
6506 }else{
6507 precision = va_arg(ap,int);
6508 }
6509 c = *++fmt;
6510 if( precision<0 ){
6511 precision = precision >= -2147483647 ? -precision : -1;
6512 }
6513 }else{
6514 unsigned px = 0;
6515 while( c>='0' && c<='9' ){
6516 px = px*10 + c - '0';
6517 c = *++fmt;
6518 }
6519 testcase( px>0x7fffffff );
6520 precision = px & 0x7fffffff;
6521 }
6522 }else{
6523 precision = -1;
6524 }
6525 assert( precision>=(-1) );
6526 #ifdef SQLITE_PRINTF_PRECISION_LIMIT
6527 if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){
6528 precision = SQLITE_PRINTF_PRECISION_LIMIT;
6529 }
6530 #endif
6531
6532
6533 /* Get the conversion type modifier */
6534 if( c=='l' ){
6535 flag_long = 1;
6536 c = *++fmt;
6537 if( c=='l' ){
6538 flag_longlong = 1;
6539 c = *++fmt;
6540 }else{
6541 flag_longlong = 0;
6542 }
6543 }else{
6544 flag_long = flag_longlong = 0;
6545 }
6546 /* Fetch the info entry for the field */
6547 infop = &fmtinfo[0];
6548 xtype = etINVALID;
6549 for(idx=0; idx<ArraySize(fmtinfo); idx++){
6550 if( c==fmtinfo[idx].fmttype ){
6551 infop = &fmtinfo[idx];
6552 xtype = infop->type;
6553 break;
6554 }
6555 }
6556
6557 /*
6558 ** At this point, variables are initialized as follows:
6559 **
6560 ** flag_alternateform TRUE if a '#' is present.
6561 ** flag_altform2 TRUE if a '!' is present.
6562 ** flag_plussign TRUE if a '+' is present.
6563 ** flag_leftjustify TRUE if a '-' is present or if the
6564 ** field width was negative.
6565 ** flag_zeropad TRUE if the width began with 0.
6566 ** flag_long TRUE if the letter 'l' (ell) prefixed
6567 ** the conversion character.
6568 ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed
6569 ** the conversion character.
6570 ** flag_blanksign TRUE if a ' ' is present.
6571 ** width The specified field width. This is
6572 ** always non-negative. Zero is the default.
6573 ** precision The specified precision. The default
6574 ** is -1.
6575 ** xtype The class of the conversion.
6576 ** infop Pointer to the appropriate info struct.
6577 */
6578 switch( xtype ){
6579 case etPOINTER:
6580 flag_longlong = sizeof(char*)==sizeof(i64);
6581 flag_long = sizeof(char*)==sizeof(long int);
6582 /* Fall through into the next case */
6583 case etORDINAL:
6584 case etRADIX:
6585 if( infop->flags & FLAG_SIGNED ){
6586 i64 v;
6587 if( bArgList ){
6588 v = getIntArg(pArgList);
6589 }else if( flag_longlong ){
6590 v = va_arg(ap,i64);
6591 }else if( flag_long ){
6592 v = va_arg(ap,long int);
6593 }else{
6594 v = va_arg(ap,int);
6595 }
6596 if( v<0 ){
6597 if( v==SMALLEST_INT64 ){
6598 longvalue = ((u64)1)<<63;
6599 }else{
6600 longvalue = -v;
6601 }
6602 prefix = '-';
6603 }else{
6604 longvalue = v;
6605 if( flag_plussign ) prefix = '+';
6606 else if( flag_blanksign ) prefix = ' ';
6607 else prefix = 0;
6608 }
6609 }else{
6610 if( bArgList ){
6611 longvalue = (u64)getIntArg(pArgList);
6612 }else if( flag_longlong ){
6613 longvalue = va_arg(ap,u64);
6614 }else if( flag_long ){
6615 longvalue = va_arg(ap,unsigned long int);
6616 }else{
6617 longvalue = va_arg(ap,unsigned int);
6618 }
6619 prefix = 0;
6620 }
6621 if( longvalue==0 ) flag_alternateform = 0;
6622 if( flag_zeropad && precision<width-(prefix!=0) ){
6623 precision = width-(prefix!=0);
6624 }
6625 if( precision<etBUFSIZE-10 ){
6626 nOut = etBUFSIZE;
6627 zOut = buf;
6628 }else{
6629 nOut = precision + 10;
6630 zOut = zExtra = sqlite3Malloc( nOut );
6631 if( zOut==0 ){
6632 setStrAccumError(pAccum, STRACCUM_NOMEM);
6633 return;
6634 }
6635 }
6636 bufpt = &zOut[nOut-1];
6637 if( xtype==etORDINAL ){
6638 static const char zOrd[] = "thstndrd";
6639 int x = (int)(longvalue % 10);
6640 if( x>=4 || (longvalue/10)%10==1 ){
6641 x = 0;
6642 }
6643 *(--bufpt) = zOrd[x*2+1];
6644 *(--bufpt) = zOrd[x*2];
6645 }
6646 {
6647 const char *cset = &aDigits[infop->charset];
6648 u8 base = infop->base;
6649 do{ /* Convert to ascii */
6650 *(--bufpt) = cset[longvalue%base];
6651 longvalue = longvalue/base;
6652 }while( longvalue>0 );
6653 }
6654 length = (int)(&zOut[nOut-1]-bufpt);
6655 for(idx=precision-length; idx>0; idx--){
6656 *(--bufpt) = '0'; /* Zero pad */
6657 }
6658 if( prefix ) *(--bufpt) = prefix; /* Add sign */
6659 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
6660 const char *pre;
6661 char x;
6662 pre = &aPrefix[infop->prefix];
6663 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
6664 }
6665 length = (int)(&zOut[nOut-1]-bufpt);
6666 break;
6667 case etFLOAT:
6668 case etEXP:
6669 case etGENERIC:
6670 if( bArgList ){
6671 realvalue = getDoubleArg(pArgList);
6672 }else{
6673 realvalue = va_arg(ap,double);
6674 }
6675 #ifdef SQLITE_OMIT_FLOATING_POINT
6676 length = 0;
6677 #else
6678 if( precision<0 ) precision = 6; /* Set default precision */
6679 if( realvalue<0.0 ){
6680 realvalue = -realvalue;
6681 prefix = '-';
6682 }else{
6683 if( flag_plussign ) prefix = '+';
6684 else if( flag_blanksign ) prefix = ' ';
6685 else prefix = 0;
6686 }
6687 if( xtype==etGENERIC && precision>0 ) precision--;
6688 testcase( precision>0xfff );
6689 for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){}
6690 if( xtype==etFLOAT ) realvalue += rounder;
6691 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
6692 exp = 0;
6693 if( sqlite3IsNaN((double)realvalue) ){
6694 bufpt = "NaN";
6695 length = 3;
6696 break;
6697 }
6698 if( realvalue>0.0 ){
6699 LONGDOUBLE_TYPE scale = 1.0;
6700 while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
6701 while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; }
6702 while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
6703 realvalue /= scale;
6704 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
6705 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
6706 if( exp>350 ){
6707 bufpt = buf;
6708 buf[0] = prefix;
6709 memcpy(buf+(prefix!=0),"Inf",4);
6710 length = 3+(prefix!=0);
6711 break;
6712 }
6713 }
6714 bufpt = buf;
6715 /*
6716 ** If the field type is etGENERIC, then convert to either etEXP
6717 ** or etFLOAT, as appropriate.
6718 */
6719 if( xtype!=etFLOAT ){
6720 realvalue += rounder;
6721 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
6722 }
6723 if( xtype==etGENERIC ){
6724 flag_rtz = !flag_alternateform;
6725 if( exp<-4 || exp>precision ){
6726 xtype = etEXP;
6727 }else{
6728 precision = precision - exp;
6729 xtype = etFLOAT;
6730 }
6731 }else{
6732 flag_rtz = flag_altform2;
6733 }
6734 if( xtype==etEXP ){
6735 e2 = 0;
6736 }else{
6737 e2 = exp;
6738 }
6739 if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){
6740 bufpt = zExtra
6741 = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 );
6742 if( bufpt==0 ){
6743 setStrAccumError(pAccum, STRACCUM_NOMEM);
6744 return;
6745 }
6746 }
6747 zOut = bufpt;
6748 nsd = 16 + flag_altform2*10;
6749 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
6750 /* The sign in front of the number */
6751 if( prefix ){
6752 *(bufpt++) = prefix;
6753 }
6754 /* Digits prior to the decimal point */
6755 if( e2<0 ){
6756 *(bufpt++) = '0';
6757 }else{
6758 for(; e2>=0; e2--){
6759 *(bufpt++) = et_getdigit(&realvalue,&nsd);
6760 }
6761 }
6762 /* The decimal point */
6763 if( flag_dp ){
6764 *(bufpt++) = '.';
6765 }
6766 /* "0" digits after the decimal point but before the first
6767 ** significant digit of the number */
6768 for(e2++; e2<0; precision--, e2++){
6769 assert( precision>0 );
6770 *(bufpt++) = '0';
6771 }
6772 /* Significant digits after the decimal point */
6773 while( (precision--)>0 ){
6774 *(bufpt++) = et_getdigit(&realvalue,&nsd);
6775 }
6776 /* Remove trailing zeros and the "." if no digits follow the "." */
6777 if( flag_rtz && flag_dp ){
6778 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
6779 assert( bufpt>zOut );
6780 if( bufpt[-1]=='.' ){
6781 if( flag_altform2 ){
6782 *(bufpt++) = '0';
6783 }else{
6784 *(--bufpt) = 0;
6785 }
6786 }
6787 }
6788 /* Add the "eNNN" suffix */
6789 if( xtype==etEXP ){
6790 *(bufpt++) = aDigits[infop->charset];
6791 if( exp<0 ){
6792 *(bufpt++) = '-'; exp = -exp;
6793 }else{
6794 *(bufpt++) = '+';
6795 }
6796 if( exp>=100 ){
6797 *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */
6798 exp %= 100;
6799 }
6800 *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */
6801 *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */
6802 }
6803 *bufpt = 0;
6804
6805 /* The converted number is in buf[] and zero terminated. Output it.
6806 ** Note that the number is in the usual order, not reversed as with
6807 ** integer conversions. */
6808 length = (int)(bufpt-zOut);
6809 bufpt = zOut;
6810
6811 /* Special case: Add leading zeros if the flag_zeropad flag is
6812 ** set and we are not left justified */
6813 if( flag_zeropad && !flag_leftjustify && length < width){
6814 int i;
6815 int nPad = width - length;
6816 for(i=width; i>=nPad; i--){
6817 bufpt[i] = bufpt[i-nPad];
6818 }
6819 i = prefix!=0;
6820 while( nPad-- ) bufpt[i++] = '0';
6821 length = width;
6822 }
6823 #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
6824 break;
6825 case etSIZE:
6826 if( !bArgList ){
6827 *(va_arg(ap,int*)) = pAccum->nChar;
6828 }
6829 length = width = 0;
6830 break;
6831 case etPERCENT:
6832 buf[0] = '%';
6833 bufpt = buf;
6834 length = 1;
6835 break;
6836 case etCHARX:
6837 if( bArgList ){
6838 bufpt = getTextArg(pArgList);
6839 c = bufpt ? bufpt[0] : 0;
6840 }else{
6841 c = va_arg(ap,int);
6842 }
6843 if( precision>1 ){
6844 width -= precision-1;
6845 if( width>1 && !flag_leftjustify ){
6846 sqlite3AppendChar(pAccum, width-1, ' ');
6847 width = 0;
6848 }
6849 sqlite3AppendChar(pAccum, precision-1, c);
6850 }
6851 length = 1;
6852 buf[0] = c;
6853 bufpt = buf;
6854 break;
6855 case etSTRING:
6856 case etDYNSTRING:
6857 if( bArgList ){
6858 bufpt = getTextArg(pArgList);
6859 xtype = etSTRING;
6860 }else{
6861 bufpt = va_arg(ap,char*);
6862 }
6863 if( bufpt==0 ){
6864 bufpt = "";
6865 }else if( xtype==etDYNSTRING ){
6866 zExtra = bufpt;
6867 }
6868 if( precision>=0 ){
6869 for(length=0; length<precision && bufpt[length]; length++){}
6870 }else{
6871 length = sqlite3Strlen30(bufpt);
6872 }
6873 break;
6874 case etSQLESCAPE: /* Escape ' characters */
6875 case etSQLESCAPE2: /* Escape ' and enclose in '...' */
6876 case etSQLESCAPE3: { /* Escape " characters */
6877 int i, j, k, n, isnull;
6878 int needQuote;
6879 char ch;
6880 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
6881 char *escarg;
6882
6883 if( bArgList ){
6884 escarg = getTextArg(pArgList);
6885 }else{
6886 escarg = va_arg(ap,char*);
6887 }
6888 isnull = escarg==0;
6889 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
6890 k = precision;
6891 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
6892 if( ch==q ) n++;
6893 }
6894 needQuote = !isnull && xtype==etSQLESCAPE2;
6895 n += i + 3;
6896 if( n>etBUFSIZE ){
6897 bufpt = zExtra = sqlite3Malloc( n );
6898 if( bufpt==0 ){
6899 setStrAccumError(pAccum, STRACCUM_NOMEM);
6900 return;
6901 }
6902 }else{
6903 bufpt = buf;
6904 }
6905 j = 0;
6906 if( needQuote ) bufpt[j++] = q;
6907 k = i;
6908 for(i=0; i<k; i++){
6909 bufpt[j++] = ch = escarg[i];
6910 if( ch==q ) bufpt[j++] = ch;
6911 }
6912 if( needQuote ) bufpt[j++] = q;
6913 bufpt[j] = 0;
6914 length = j;
6915 /* The precision in %q and %Q means how many input characters to
6916 ** consume, not the length of the output...
6917 ** if( precision>=0 && precision<length ) length = precision; */
6918 break;
6919 }
6920 case etTOKEN: {
6921 Token *pToken;
6922 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
6923 pToken = va_arg(ap, Token*);
6924 assert( bArgList==0 );
6925 if( pToken && pToken->n ){
6926 sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
6927 }
6928 length = width = 0;
6929 break;
6930 }
6931 case etSRCLIST: {
6932 SrcList *pSrc;
6933 int k;
6934 struct SrcList_item *pItem;
6935 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
6936 pSrc = va_arg(ap, SrcList*);
6937 k = va_arg(ap, int);
6938 pItem = &pSrc->a[k];
6939 assert( bArgList==0 );
6940 assert( k>=0 && k<pSrc->nSrc );
6941 if( pItem->zDatabase ){
6942 sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
6943 sqlite3StrAccumAppend(pAccum, ".", 1);
6944 }
6945 sqlite3StrAccumAppendAll(pAccum, pItem->zName);
6946 length = width = 0;
6947 break;
6948 }
6949 default: {
6950 assert( xtype==etINVALID );
6951 return;
6952 }
6953 }/* End switch over the format type */
6954 /*
6955 ** The text of the conversion is pointed to by "bufpt" and is
6956 ** "length" characters long. The field width is "width". Do
6957 ** the output.
6958 */
6959 width -= length;
6960 if( width>0 ){
6961 if( !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
6962 sqlite3StrAccumAppend(pAccum, bufpt, length);
6963 if( flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
6964 }else{
6965 sqlite3StrAccumAppend(pAccum, bufpt, length);
6966 }
6967
6968 if( zExtra ){
6969 sqlite3DbFree(pAccum->db, zExtra);
6970 zExtra = 0;
6971 }
6972 }/* End for loop over the format string */
6973 } /* End of function */
6974
6975 /*
6976 ** Enlarge the memory allocation on a StrAccum object so that it is
6977 ** able to accept at least N more bytes of text.
6978 **
6979 ** Return the number of bytes of text that StrAccum is able to accept
6980 ** after the attempted enlargement. The value returned might be zero.
6981 */
6982 static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
6983 char *zNew;
6984 assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
6985 if( p->accError ){
6986 testcase(p->accError==STRACCUM_TOOBIG);
6987 testcase(p->accError==STRACCUM_NOMEM);
6988 return 0;
6989 }
6990 if( p->mxAlloc==0 ){
6991 N = p->nAlloc - p->nChar - 1;
6992 setStrAccumError(p, STRACCUM_TOOBIG);
6993 return N;
6994 }else{
6995 char *zOld = isMalloced(p) ? p->zText : 0;
6996 i64 szNew = p->nChar;
6997 assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) );
6998 szNew += N + 1;
6999 if( szNew+p->nChar<=p->mxAlloc ){
7000 /* Force exponential buffer size growth as long as it does not overflow,
7001 ** to avoid having to call this routine too often */
7002 szNew += p->nChar;
7003 }
7004 if( szNew > p->mxAlloc ){
7005 sqlite3StrAccumReset(p);
7006 setStrAccumError(p, STRACCUM_TOOBIG);
7007 return 0;
7008 }else{
7009 p->nAlloc = (int)szNew;
7010 }
7011 if( p->db ){
7012 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
7013 }else{
7014 zNew = sqlite3_realloc64(zOld, p->nAlloc);
7015 }
7016 if( zNew ){
7017 assert( p->zText!=0 || p->nChar==0 );
7018 if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
7019 p->zText = zNew;
7020 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
7021 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
7022 }else{
7023 sqlite3StrAccumReset(p);
7024 setStrAccumError(p, STRACCUM_NOMEM);
7025 return 0;
7026 }
7027 }
7028 return N;
7029 }
7030
7031 /*
7032 ** Append N copies of character c to the given string buffer.
7033 */
7034 SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){
7035 testcase( p->nChar + (i64)N > 0x7fffffff );
7036 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
7037 return;
7038 }
7039 assert( (p->zText==p->zBase)==!isMalloced(p) );
7040 while( (N--)>0 ) p->zText[p->nChar++] = c;
7041 }
7042
7043 /*
7044 ** The StrAccum "p" is not large enough to accept N new bytes of z[].
7045 ** So enlarge if first, then do the append.
7046 **
7047 ** This is a helper routine to sqlite3StrAccumAppend() that does special-case
7048 ** work (enlarging the buffer) using tail recursion, so that the
7049 ** sqlite3StrAccumAppend() routine can use fast calling semantics.
7050 */
7051 static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
7052 N = sqlite3StrAccumEnlarge(p, N);
7053 if( N>0 ){
7054 memcpy(&p->zText[p->nChar], z, N);
7055 p->nChar += N;
7056 }
7057 assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) );
7058 }
7059
7060 /*
7061 ** Append N bytes of text from z to the StrAccum object. Increase the
7062 ** size of the memory allocation for StrAccum if necessary.
7063 */
7064 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
7065 assert( z!=0 || N==0 );
7066 assert( p->zText!=0 || p->nChar==0 || p->accError );
7067 assert( N>=0 );
7068 assert( p->accError==0 || p->nAlloc==0 );
7069 if( p->nChar+N >= p->nAlloc ){
7070 enlargeAndAppend(p,z,N);
7071 }else if( N ){
7072 assert( p->zText );
7073 p->nChar += N;
7074 memcpy(&p->zText[p->nChar-N], z, N);
7075 }
7076 }
7077
7078 /*
7079 ** Append the complete text of zero-terminated string z[] to the p string.
7080 */
7081 SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){
7082 sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z));
7083 }
7084
7085
7086 /*
7087 ** Finish off a string by making sure it is zero-terminated.
7088 ** Return a pointer to the resulting string. Return a NULL
7089 ** pointer if any kind of error was encountered.
7090 */
7091 static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){
7092 assert( p->mxAlloc>0 && !isMalloced(p) );
7093 p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
7094 if( p->zText ){
7095 memcpy(p->zText, p->zBase, p->nChar+1);
7096 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
7097 }else{
7098 setStrAccumError(p, STRACCUM_NOMEM);
7099 }
7100 return p->zText;
7101 }
7102 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
7103 if( p->zText ){
7104 assert( (p->zText==p->zBase)==!isMalloced(p) );
7105 p->zText[p->nChar] = 0;
7106 if( p->mxAlloc>0 && !isMalloced(p) ){
7107 return strAccumFinishRealloc(p);
7108 }
7109 }
7110 return p->zText;
7111 }
7112
7113 /*
7114 ** Reset an StrAccum string. Reclaim all malloced memory.
7115 */
7116 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
7117 assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) );
7118 if( isMalloced(p) ){
7119 sqlite3DbFree(p->db, p->zText);
7120 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
7121 }
7122 p->zText = 0;
7123 }
7124
7125 /*
7126 ** Initialize a string accumulator.
7127 **
7128 ** p: The accumulator to be initialized.
7129 ** db: Pointer to a database connection. May be NULL. Lookaside
7130 ** memory is used if not NULL. db->mallocFailed is set appropriately
7131 ** when not NULL.
7132 ** zBase: An initial buffer. May be NULL in which case the initial buffer
7133 ** is malloced.
7134 ** n: Size of zBase in bytes. If total space requirements never exceed
7135 ** n then no memory allocations ever occur.
7136 ** mx: Maximum number of bytes to accumulate. If mx==0 then no memory
7137 ** allocations will ever occur.
7138 */
7139 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, i nt n, int mx){
7140 p->zText = p->zBase = zBase;
7141 p->db = db;
7142 p->nChar = 0;
7143 p->nAlloc = n;
7144 p->mxAlloc = mx;
7145 p->accError = 0;
7146 p->printfFlags = 0;
7147 }
7148
7149 /*
7150 ** Print into memory obtained from sqliteMalloc(). Use the internal
7151 ** %-conversion extensions.
7152 */
7153 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list a p){
7154 char *z;
7155 char zBase[SQLITE_PRINT_BUF_SIZE];
7156 StrAccum acc;
7157 assert( db!=0 );
7158 sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
7159 db->aLimit[SQLITE_LIMIT_LENGTH]);
7160 acc.printfFlags = SQLITE_PRINTF_INTERNAL;
7161 sqlite3VXPrintf(&acc, zFormat, ap);
7162 z = sqlite3StrAccumFinish(&acc);
7163 if( acc.accError==STRACCUM_NOMEM ){
7164 sqlite3OomFault(db);
7165 }
7166 return z;
7167 }
7168
7169 /*
7170 ** Print into memory obtained from sqliteMalloc(). Use the internal
7171 ** %-conversion extensions.
7172 */
7173 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
7174 va_list ap;
7175 char *z;
7176 va_start(ap, zFormat);
7177 z = sqlite3VMPrintf(db, zFormat, ap);
7178 va_end(ap);
7179 return z;
7180 }
7181
7182 /*
7183 ** Print into memory obtained from sqlite3_malloc(). Omit the internal
7184 ** %-conversion extensions.
7185 */
7186 SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
7187 char *z;
7188 char zBase[SQLITE_PRINT_BUF_SIZE];
7189 StrAccum acc;
7190
7191 #ifdef SQLITE_ENABLE_API_ARMOR
7192 if( zFormat==0 ){
7193 (void)SQLITE_MISUSE_BKPT;
7194 return 0;
7195 }
7196 #endif
7197 #ifndef SQLITE_OMIT_AUTOINIT
7198 if( sqlite3_initialize() ) return 0;
7199 #endif
7200 sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
7201 sqlite3VXPrintf(&acc, zFormat, ap);
7202 z = sqlite3StrAccumFinish(&acc);
7203 return z;
7204 }
7205
7206 /*
7207 ** Print into memory obtained from sqlite3_malloc()(). Omit the internal
7208 ** %-conversion extensions.
7209 */
7210 SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
7211 va_list ap;
7212 char *z;
7213 #ifndef SQLITE_OMIT_AUTOINIT
7214 if( sqlite3_initialize() ) return 0;
7215 #endif
7216 va_start(ap, zFormat);
7217 z = sqlite3_vmprintf(zFormat, ap);
7218 va_end(ap);
7219 return z;
7220 }
7221
7222 /*
7223 ** sqlite3_snprintf() works like snprintf() except that it ignores the
7224 ** current locale settings. This is important for SQLite because we
7225 ** are not able to use a "," as the decimal point in place of "." as
7226 ** specified by some locales.
7227 **
7228 ** Oops: The first two arguments of sqlite3_snprintf() are backwards
7229 ** from the snprintf() standard. Unfortunately, it is too late to change
7230 ** this without breaking compatibility, so we just have to live with the
7231 ** mistake.
7232 **
7233 ** sqlite3_vsnprintf() is the varargs version.
7234 */
7235 SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_li st ap){
7236 StrAccum acc;
7237 if( n<=0 ) return zBuf;
7238 #ifdef SQLITE_ENABLE_API_ARMOR
7239 if( zBuf==0 || zFormat==0 ) {
7240 (void)SQLITE_MISUSE_BKPT;
7241 if( zBuf ) zBuf[0] = 0;
7242 return zBuf;
7243 }
7244 #endif
7245 sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
7246 sqlite3VXPrintf(&acc, zFormat, ap);
7247 zBuf[acc.nChar] = 0;
7248 return zBuf;
7249 }
7250 SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
7251 char *z;
7252 va_list ap;
7253 va_start(ap,zFormat);
7254 z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
7255 va_end(ap);
7256 return z;
7257 }
7258
7259 /*
7260 ** This is the routine that actually formats the sqlite3_log() message.
7261 ** We house it in a separate routine from sqlite3_log() to avoid using
7262 ** stack space on small-stack systems when logging is disabled.
7263 **
7264 ** sqlite3_log() must render into a static buffer. It cannot dynamically
7265 ** allocate memory because it might be called while the memory allocator
7266 ** mutex is held.
7267 **
7268 ** sqlite3VXPrintf() might ask for *temporary* memory allocations for
7269 ** certain format characters (%q) or for very large precisions or widths.
7270 ** Care must be taken that any sqlite3_log() calls that occur while the
7271 ** memory mutex is held do not use these mechanisms.
7272 */
7273 static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
7274 StrAccum acc; /* String accumulator */
7275 char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */
7276
7277 sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
7278 sqlite3VXPrintf(&acc, zFormat, ap);
7279 sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
7280 sqlite3StrAccumFinish(&acc));
7281 }
7282
7283 /*
7284 ** Format and write a message to the log if logging is enabled.
7285 */
7286 SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
7287 va_list ap; /* Vararg list */
7288 if( sqlite3GlobalConfig.xLog ){
7289 va_start(ap, zFormat);
7290 renderLogMsg(iErrCode, zFormat, ap);
7291 va_end(ap);
7292 }
7293 }
7294
7295 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
7296 /*
7297 ** A version of printf() that understands %lld. Used for debugging.
7298 ** The printf() built into some versions of windows does not understand %lld
7299 ** and segfaults if you give it a long long int.
7300 */
7301 SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
7302 va_list ap;
7303 StrAccum acc;
7304 char zBuf[500];
7305 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
7306 va_start(ap,zFormat);
7307 sqlite3VXPrintf(&acc, zFormat, ap);
7308 va_end(ap);
7309 sqlite3StrAccumFinish(&acc);
7310 fprintf(stdout,"%s", zBuf);
7311 fflush(stdout);
7312 }
7313 #endif
7314
7315
7316 /*
7317 ** variable-argument wrapper around sqlite3VXPrintf(). The bFlags argument
7318 ** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
7319 */
7320 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){
7321 va_list ap;
7322 va_start(ap,zFormat);
7323 sqlite3VXPrintf(p, zFormat, ap);
7324 va_end(ap);
7325 }
7326
7327 /************** End of printf.c **********************************************/
7328 /************** Begin file treeview.c ****************************************/
7329 /*
7330 ** 2015-06-08
7331 **
7332 ** The author disclaims copyright to this source code. In place of
7333 ** a legal notice, here is a blessing:
7334 **
7335 ** May you do good and not evil.
7336 ** May you find forgiveness for yourself and forgive others.
7337 ** May you share freely, never taking more than you give.
7338 **
7339 *************************************************************************
7340 **
7341 ** This file contains C code to implement the TreeView debugging routines.
7342 ** These routines print a parse tree to standard output for debugging and
7343 ** analysis.
7344 **
7345 ** The interfaces in this file is only available when compiling
7346 ** with SQLITE_DEBUG.
7347 */
7348 /* #include "sqliteInt.h" */
7349 #ifdef SQLITE_DEBUG
7350
7351 /*
7352 ** Add a new subitem to the tree. The moreToFollow flag indicates that this
7353 ** is not the last item in the tree.
7354 */
7355 static TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
7356 if( p==0 ){
7357 p = sqlite3_malloc64( sizeof(*p) );
7358 if( p==0 ) return 0;
7359 memset(p, 0, sizeof(*p));
7360 }else{
7361 p->iLevel++;
7362 }
7363 assert( moreToFollow==0 || moreToFollow==1 );
7364 if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
7365 return p;
7366 }
7367
7368 /*
7369 ** Finished with one layer of the tree
7370 */
7371 static void sqlite3TreeViewPop(TreeView *p){
7372 if( p==0 ) return;
7373 p->iLevel--;
7374 if( p->iLevel<0 ) sqlite3_free(p);
7375 }
7376
7377 /*
7378 ** Generate a single line of output for the tree, with a prefix that contains
7379 ** all the appropriate tree lines
7380 */
7381 static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
7382 va_list ap;
7383 int i;
7384 StrAccum acc;
7385 char zBuf[500];
7386 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
7387 if( p ){
7388 for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
7389 sqlite3StrAccumAppend(&acc, p->bLine[i] ? "| " : " ", 4);
7390 }
7391 sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
7392 }
7393 va_start(ap, zFormat);
7394 sqlite3VXPrintf(&acc, zFormat, ap);
7395 va_end(ap);
7396 assert( acc.nChar>0 );
7397 if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
7398 sqlite3StrAccumFinish(&acc);
7399 fprintf(stdout,"%s", zBuf);
7400 fflush(stdout);
7401 }
7402
7403 /*
7404 ** Shorthand for starting a new tree item that consists of a single label
7405 */
7406 static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
7407 p = sqlite3TreeViewPush(p, moreFollows);
7408 sqlite3TreeViewLine(p, "%s", zLabel);
7409 }
7410
7411 /*
7412 ** Generate a human-readable description of a WITH clause.
7413 */
7414 SQLITE_PRIVATE void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 m oreToFollow){
7415 int i;
7416 if( pWith==0 ) return;
7417 if( pWith->nCte==0 ) return;
7418 if( pWith->pOuter ){
7419 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
7420 }else{
7421 sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith);
7422 }
7423 if( pWith->nCte>0 ){
7424 pView = sqlite3TreeViewPush(pView, 1);
7425 for(i=0; i<pWith->nCte; i++){
7426 StrAccum x;
7427 char zLine[1000];
7428 const struct Cte *pCte = &pWith->a[i];
7429 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
7430 sqlite3XPrintf(&x, "%s", pCte->zName);
7431 if( pCte->pCols && pCte->pCols->nExpr>0 ){
7432 char cSep = '(';
7433 int j;
7434 for(j=0; j<pCte->pCols->nExpr; j++){
7435 sqlite3XPrintf(&x, "%c%s", cSep, pCte->pCols->a[j].zName);
7436 cSep = ',';
7437 }
7438 sqlite3XPrintf(&x, ")");
7439 }
7440 sqlite3XPrintf(&x, " AS");
7441 sqlite3StrAccumFinish(&x);
7442 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
7443 sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
7444 sqlite3TreeViewPop(pView);
7445 }
7446 sqlite3TreeViewPop(pView);
7447 }
7448 }
7449
7450
7451 /*
7452 ** Generate a human-readable description of a Select object.
7453 */
7454 SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 m oreToFollow){
7455 int n = 0;
7456 int cnt = 0;
7457 pView = sqlite3TreeViewPush(pView, moreToFollow);
7458 if( p->pWith ){
7459 sqlite3TreeViewWith(pView, p->pWith, 1);
7460 cnt = 1;
7461 sqlite3TreeViewPush(pView, 1);
7462 }
7463 do{
7464 sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x nSelectRow=%d",
7465 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
7466 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags,
7467 (int)p->nSelectRow
7468 );
7469 if( cnt++ ) sqlite3TreeViewPop(pView);
7470 if( p->pPrior ){
7471 n = 1000;
7472 }else{
7473 n = 0;
7474 if( p->pSrc && p->pSrc->nSrc ) n++;
7475 if( p->pWhere ) n++;
7476 if( p->pGroupBy ) n++;
7477 if( p->pHaving ) n++;
7478 if( p->pOrderBy ) n++;
7479 if( p->pLimit ) n++;
7480 if( p->pOffset ) n++;
7481 }
7482 sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
7483 if( p->pSrc && p->pSrc->nSrc ){
7484 int i;
7485 pView = sqlite3TreeViewPush(pView, (n--)>0);
7486 sqlite3TreeViewLine(pView, "FROM");
7487 for(i=0; i<p->pSrc->nSrc; i++){
7488 struct SrcList_item *pItem = &p->pSrc->a[i];
7489 StrAccum x;
7490 char zLine[100];
7491 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
7492 sqlite3XPrintf(&x, "{%d,*}", pItem->iCursor);
7493 if( pItem->zDatabase ){
7494 sqlite3XPrintf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
7495 }else if( pItem->zName ){
7496 sqlite3XPrintf(&x, " %s", pItem->zName);
7497 }
7498 if( pItem->pTab ){
7499 sqlite3XPrintf(&x, " tabname=%Q", pItem->pTab->zName);
7500 }
7501 if( pItem->zAlias ){
7502 sqlite3XPrintf(&x, " (AS %s)", pItem->zAlias);
7503 }
7504 if( pItem->fg.jointype & JT_LEFT ){
7505 sqlite3XPrintf(&x, " LEFT-JOIN");
7506 }
7507 sqlite3StrAccumFinish(&x);
7508 sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1);
7509 if( pItem->pSelect ){
7510 sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
7511 }
7512 if( pItem->fg.isTabFunc ){
7513 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
7514 }
7515 sqlite3TreeViewPop(pView);
7516 }
7517 sqlite3TreeViewPop(pView);
7518 }
7519 if( p->pWhere ){
7520 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
7521 sqlite3TreeViewExpr(pView, p->pWhere, 0);
7522 sqlite3TreeViewPop(pView);
7523 }
7524 if( p->pGroupBy ){
7525 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
7526 }
7527 if( p->pHaving ){
7528 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
7529 sqlite3TreeViewExpr(pView, p->pHaving, 0);
7530 sqlite3TreeViewPop(pView);
7531 }
7532 if( p->pOrderBy ){
7533 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
7534 }
7535 if( p->pLimit ){
7536 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
7537 sqlite3TreeViewExpr(pView, p->pLimit, 0);
7538 sqlite3TreeViewPop(pView);
7539 }
7540 if( p->pOffset ){
7541 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
7542 sqlite3TreeViewExpr(pView, p->pOffset, 0);
7543 sqlite3TreeViewPop(pView);
7544 }
7545 if( p->pPrior ){
7546 const char *zOp = "UNION";
7547 switch( p->op ){
7548 case TK_ALL: zOp = "UNION ALL"; break;
7549 case TK_INTERSECT: zOp = "INTERSECT"; break;
7550 case TK_EXCEPT: zOp = "EXCEPT"; break;
7551 }
7552 sqlite3TreeViewItem(pView, zOp, 1);
7553 }
7554 p = p->pPrior;
7555 }while( p!=0 );
7556 sqlite3TreeViewPop(pView);
7557 }
7558
7559 /*
7560 ** Generate a human-readable explanation of an expression tree.
7561 */
7562 SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 m oreToFollow){
7563 const char *zBinOp = 0; /* Binary operator */
7564 const char *zUniOp = 0; /* Unary operator */
7565 char zFlgs[30];
7566 pView = sqlite3TreeViewPush(pView, moreToFollow);
7567 if( pExpr==0 ){
7568 sqlite3TreeViewLine(pView, "nil");
7569 sqlite3TreeViewPop(pView);
7570 return;
7571 }
7572 if( pExpr->flags ){
7573 sqlite3_snprintf(sizeof(zFlgs),zFlgs," flags=0x%x",pExpr->flags);
7574 }else{
7575 zFlgs[0] = 0;
7576 }
7577 switch( pExpr->op ){
7578 case TK_AGG_COLUMN: {
7579 sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
7580 pExpr->iTable, pExpr->iColumn, zFlgs);
7581 break;
7582 }
7583 case TK_COLUMN: {
7584 if( pExpr->iTable<0 ){
7585 /* This only happens when coding check constraints */
7586 sqlite3TreeViewLine(pView, "COLUMN(%d)%s", pExpr->iColumn, zFlgs);
7587 }else{
7588 sqlite3TreeViewLine(pView, "{%d:%d}%s",
7589 pExpr->iTable, pExpr->iColumn, zFlgs);
7590 }
7591 break;
7592 }
7593 case TK_INTEGER: {
7594 if( pExpr->flags & EP_IntValue ){
7595 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
7596 }else{
7597 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
7598 }
7599 break;
7600 }
7601 #ifndef SQLITE_OMIT_FLOATING_POINT
7602 case TK_FLOAT: {
7603 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
7604 break;
7605 }
7606 #endif
7607 case TK_STRING: {
7608 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
7609 break;
7610 }
7611 case TK_NULL: {
7612 sqlite3TreeViewLine(pView,"NULL");
7613 break;
7614 }
7615 #ifndef SQLITE_OMIT_BLOB_LITERAL
7616 case TK_BLOB: {
7617 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
7618 break;
7619 }
7620 #endif
7621 case TK_VARIABLE: {
7622 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
7623 pExpr->u.zToken, pExpr->iColumn);
7624 break;
7625 }
7626 case TK_REGISTER: {
7627 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
7628 break;
7629 }
7630 case TK_ID: {
7631 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
7632 break;
7633 }
7634 #ifndef SQLITE_OMIT_CAST
7635 case TK_CAST: {
7636 /* Expressions of the form: CAST(pLeft AS token) */
7637 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
7638 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
7639 break;
7640 }
7641 #endif /* SQLITE_OMIT_CAST */
7642 case TK_LT: zBinOp = "LT"; break;
7643 case TK_LE: zBinOp = "LE"; break;
7644 case TK_GT: zBinOp = "GT"; break;
7645 case TK_GE: zBinOp = "GE"; break;
7646 case TK_NE: zBinOp = "NE"; break;
7647 case TK_EQ: zBinOp = "EQ"; break;
7648 case TK_IS: zBinOp = "IS"; break;
7649 case TK_ISNOT: zBinOp = "ISNOT"; break;
7650 case TK_AND: zBinOp = "AND"; break;
7651 case TK_OR: zBinOp = "OR"; break;
7652 case TK_PLUS: zBinOp = "ADD"; break;
7653 case TK_STAR: zBinOp = "MUL"; break;
7654 case TK_MINUS: zBinOp = "SUB"; break;
7655 case TK_REM: zBinOp = "REM"; break;
7656 case TK_BITAND: zBinOp = "BITAND"; break;
7657 case TK_BITOR: zBinOp = "BITOR"; break;
7658 case TK_SLASH: zBinOp = "DIV"; break;
7659 case TK_LSHIFT: zBinOp = "LSHIFT"; break;
7660 case TK_RSHIFT: zBinOp = "RSHIFT"; break;
7661 case TK_CONCAT: zBinOp = "CONCAT"; break;
7662 case TK_DOT: zBinOp = "DOT"; break;
7663
7664 case TK_UMINUS: zUniOp = "UMINUS"; break;
7665 case TK_UPLUS: zUniOp = "UPLUS"; break;
7666 case TK_BITNOT: zUniOp = "BITNOT"; break;
7667 case TK_NOT: zUniOp = "NOT"; break;
7668 case TK_ISNULL: zUniOp = "ISNULL"; break;
7669 case TK_NOTNULL: zUniOp = "NOTNULL"; break;
7670
7671 case TK_SPAN: {
7672 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
7673 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
7674 break;
7675 }
7676
7677 case TK_COLLATE: {
7678 sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
7679 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
7680 break;
7681 }
7682
7683 case TK_AGG_FUNCTION:
7684 case TK_FUNCTION: {
7685 ExprList *pFarg; /* List of function arguments */
7686 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
7687 pFarg = 0;
7688 }else{
7689 pFarg = pExpr->x.pList;
7690 }
7691 if( pExpr->op==TK_AGG_FUNCTION ){
7692 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
7693 pExpr->op2, pExpr->u.zToken);
7694 }else{
7695 sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
7696 }
7697 if( pFarg ){
7698 sqlite3TreeViewExprList(pView, pFarg, 0, 0);
7699 }
7700 break;
7701 }
7702 #ifndef SQLITE_OMIT_SUBQUERY
7703 case TK_EXISTS: {
7704 sqlite3TreeViewLine(pView, "EXISTS-expr");
7705 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
7706 break;
7707 }
7708 case TK_SELECT: {
7709 sqlite3TreeViewLine(pView, "SELECT-expr");
7710 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
7711 break;
7712 }
7713 case TK_IN: {
7714 sqlite3TreeViewLine(pView, "IN");
7715 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
7716 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
7717 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
7718 }else{
7719 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
7720 }
7721 break;
7722 }
7723 #endif /* SQLITE_OMIT_SUBQUERY */
7724
7725 /*
7726 ** x BETWEEN y AND z
7727 **
7728 ** This is equivalent to
7729 **
7730 ** x>=y AND x<=z
7731 **
7732 ** X is stored in pExpr->pLeft.
7733 ** Y is stored in pExpr->pList->a[0].pExpr.
7734 ** Z is stored in pExpr->pList->a[1].pExpr.
7735 */
7736 case TK_BETWEEN: {
7737 Expr *pX = pExpr->pLeft;
7738 Expr *pY = pExpr->x.pList->a[0].pExpr;
7739 Expr *pZ = pExpr->x.pList->a[1].pExpr;
7740 sqlite3TreeViewLine(pView, "BETWEEN");
7741 sqlite3TreeViewExpr(pView, pX, 1);
7742 sqlite3TreeViewExpr(pView, pY, 1);
7743 sqlite3TreeViewExpr(pView, pZ, 0);
7744 break;
7745 }
7746 case TK_TRIGGER: {
7747 /* If the opcode is TK_TRIGGER, then the expression is a reference
7748 ** to a column in the new.* or old.* pseudo-tables available to
7749 ** trigger programs. In this case Expr.iTable is set to 1 for the
7750 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
7751 ** is set to the column of the pseudo-table to read, or to -1 to
7752 ** read the rowid field.
7753 */
7754 sqlite3TreeViewLine(pView, "%s(%d)",
7755 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
7756 break;
7757 }
7758 case TK_CASE: {
7759 sqlite3TreeViewLine(pView, "CASE");
7760 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
7761 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
7762 break;
7763 }
7764 #ifndef SQLITE_OMIT_TRIGGER
7765 case TK_RAISE: {
7766 const char *zType = "unk";
7767 switch( pExpr->affinity ){
7768 case OE_Rollback: zType = "rollback"; break;
7769 case OE_Abort: zType = "abort"; break;
7770 case OE_Fail: zType = "fail"; break;
7771 case OE_Ignore: zType = "ignore"; break;
7772 }
7773 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
7774 break;
7775 }
7776 #endif
7777 case TK_MATCH: {
7778 sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
7779 pExpr->iTable, pExpr->iColumn, zFlgs);
7780 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
7781 break;
7782 }
7783 case TK_VECTOR: {
7784 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, "VECTOR");
7785 break;
7786 }
7787 case TK_SELECT_COLUMN: {
7788 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d", pExpr->iColumn);
7789 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
7790 break;
7791 }
7792 default: {
7793 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
7794 break;
7795 }
7796 }
7797 if( zBinOp ){
7798 sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
7799 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
7800 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
7801 }else if( zUniOp ){
7802 sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
7803 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
7804 }
7805 sqlite3TreeViewPop(pView);
7806 }
7807
7808
7809 /*
7810 ** Generate a human-readable explanation of an expression list.
7811 */
7812 SQLITE_PRIVATE void sqlite3TreeViewBareExprList(
7813 TreeView *pView,
7814 const ExprList *pList,
7815 const char *zLabel
7816 ){
7817 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
7818 if( pList==0 ){
7819 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
7820 }else{
7821 int i;
7822 sqlite3TreeViewLine(pView, "%s", zLabel);
7823 for(i=0; i<pList->nExpr; i++){
7824 int j = pList->a[i].u.x.iOrderByCol;
7825 if( j ){
7826 sqlite3TreeViewPush(pView, 0);
7827 sqlite3TreeViewLine(pView, "iOrderByCol=%d", j);
7828 }
7829 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
7830 if( j ) sqlite3TreeViewPop(pView);
7831 }
7832 }
7833 }
7834 SQLITE_PRIVATE void sqlite3TreeViewExprList(
7835 TreeView *pView,
7836 const ExprList *pList,
7837 u8 moreToFollow,
7838 const char *zLabel
7839 ){
7840 pView = sqlite3TreeViewPush(pView, moreToFollow);
7841 sqlite3TreeViewBareExprList(pView, pList, zLabel);
7842 sqlite3TreeViewPop(pView);
7843 }
7844
7845 #endif /* SQLITE_DEBUG */
7846
7847 /************** End of treeview.c ********************************************/
7848 /************** Begin file random.c ******************************************/
7849 /*
7850 ** 2001 September 15
7851 **
7852 ** The author disclaims copyright to this source code. In place of
7853 ** a legal notice, here is a blessing:
7854 **
7855 ** May you do good and not evil.
7856 ** May you find forgiveness for yourself and forgive others.
7857 ** May you share freely, never taking more than you give.
7858 **
7859 *************************************************************************
7860 ** This file contains code to implement a pseudo-random number
7861 ** generator (PRNG) for SQLite.
7862 **
7863 ** Random numbers are used by some of the database backends in order
7864 ** to generate random integer keys for tables or random filenames.
7865 */
7866 /* #include "sqliteInt.h" */
7867
7868
7869 /* All threads share a single random number generator.
7870 ** This structure is the current state of the generator.
7871 */
7872 static SQLITE_WSD struct sqlite3PrngType {
7873 unsigned char isInit; /* True if initialized */
7874 unsigned char i, j; /* State variables */
7875 unsigned char s[256]; /* State variables */
7876 } sqlite3Prng;
7877
7878 /*
7879 ** Return N random bytes.
7880 */
7881 SQLITE_API void sqlite3_randomness(int N, void *pBuf){
7882 unsigned char t;
7883 unsigned char *zBuf = pBuf;
7884
7885 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
7886 ** state vector. If writable static data is unsupported on the target,
7887 ** we have to locate the state vector at run-time. In the more common
7888 ** case where writable static data is supported, wsdPrng can refer directly
7889 ** to the "sqlite3Prng" state vector declared above.
7890 */
7891 #ifdef SQLITE_OMIT_WSD
7892 struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
7893 # define wsdPrng p[0]
7894 #else
7895 # define wsdPrng sqlite3Prng
7896 #endif
7897
7898 #if SQLITE_THREADSAFE
7899 sqlite3_mutex *mutex;
7900 #endif
7901
7902 #ifndef SQLITE_OMIT_AUTOINIT
7903 if( sqlite3_initialize() ) return;
7904 #endif
7905
7906 #if SQLITE_THREADSAFE
7907 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
7908 #endif
7909
7910 sqlite3_mutex_enter(mutex);
7911 if( N<=0 || pBuf==0 ){
7912 wsdPrng.isInit = 0;
7913 sqlite3_mutex_leave(mutex);
7914 return;
7915 }
7916
7917 /* Initialize the state of the random number generator once,
7918 ** the first time this routine is called. The seed value does
7919 ** not need to contain a lot of randomness since we are not
7920 ** trying to do secure encryption or anything like that...
7921 **
7922 ** Nothing in this file or anywhere else in SQLite does any kind of
7923 ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
7924 ** number generator) not as an encryption device.
7925 */
7926 if( !wsdPrng.isInit ){
7927 int i;
7928 char k[256];
7929 wsdPrng.j = 0;
7930 wsdPrng.i = 0;
7931 sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
7932 for(i=0; i<256; i++){
7933 wsdPrng.s[i] = (u8)i;
7934 }
7935 for(i=0; i<256; i++){
7936 wsdPrng.j += wsdPrng.s[i] + k[i];
7937 t = wsdPrng.s[wsdPrng.j];
7938 wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
7939 wsdPrng.s[i] = t;
7940 }
7941 wsdPrng.isInit = 1;
7942 }
7943
7944 assert( N>0 );
7945 do{
7946 wsdPrng.i++;
7947 t = wsdPrng.s[wsdPrng.i];
7948 wsdPrng.j += t;
7949 wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
7950 wsdPrng.s[wsdPrng.j] = t;
7951 t += wsdPrng.s[wsdPrng.i];
7952 *(zBuf++) = wsdPrng.s[t];
7953 }while( --N );
7954 sqlite3_mutex_leave(mutex);
7955 }
7956
7957 #ifndef SQLITE_UNTESTABLE
7958 /*
7959 ** For testing purposes, we sometimes want to preserve the state of
7960 ** PRNG and restore the PRNG to its saved state at a later time, or
7961 ** to reset the PRNG to its initial state. These routines accomplish
7962 ** those tasks.
7963 **
7964 ** The sqlite3_test_control() interface calls these routines to
7965 ** control the PRNG.
7966 */
7967 static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
7968 SQLITE_PRIVATE void sqlite3PrngSaveState(void){
7969 memcpy(
7970 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
7971 &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
7972 sizeof(sqlite3Prng)
7973 );
7974 }
7975 SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
7976 memcpy(
7977 &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
7978 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
7979 sizeof(sqlite3Prng)
7980 );
7981 }
7982 #endif /* SQLITE_UNTESTABLE */
7983
7984 /************** End of random.c **********************************************/
7985 /************** Begin file threads.c *****************************************/
7986 /*
7987 ** 2012 July 21
7988 **
7989 ** The author disclaims copyright to this source code. In place of
7990 ** a legal notice, here is a blessing:
7991 **
7992 ** May you do good and not evil.
7993 ** May you find forgiveness for yourself and forgive others.
7994 ** May you share freely, never taking more than you give.
7995 **
7996 ******************************************************************************
7997 **
7998 ** This file presents a simple cross-platform threading interface for
7999 ** use internally by SQLite.
8000 **
8001 ** A "thread" can be created using sqlite3ThreadCreate(). This thread
8002 ** runs independently of its creator until it is joined using
8003 ** sqlite3ThreadJoin(), at which point it terminates.
8004 **
8005 ** Threads do not have to be real. It could be that the work of the
8006 ** "thread" is done by the main thread at either the sqlite3ThreadCreate()
8007 ** or sqlite3ThreadJoin() call. This is, in fact, what happens in
8008 ** single threaded systems. Nothing in SQLite requires multiple threads.
8009 ** This interface exists so that applications that want to take advantage
8010 ** of multiple cores can do so, while also allowing applications to stay
8011 ** single-threaded if desired.
8012 */
8013 /* #include "sqliteInt.h" */
8014 #if SQLITE_OS_WIN
8015 /* # include "os_win.h" */
8016 #endif
8017
8018 #if SQLITE_MAX_WORKER_THREADS>0
8019
8020 /********************************* Unix Pthreads ****************************/
8021 #if SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) && SQLITE_THREADSAFE>0
8022
8023 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
8024 /* #include <pthread.h> */
8025
8026 /* A running thread */
8027 struct SQLiteThread {
8028 pthread_t tid; /* Thread ID */
8029 int done; /* Set to true when thread finishes */
8030 void *pOut; /* Result returned by the thread */
8031 void *(*xTask)(void*); /* The thread routine */
8032 void *pIn; /* Argument to the thread */
8033 };
8034
8035 /* Create a new thread */
8036 SQLITE_PRIVATE int sqlite3ThreadCreate(
8037 SQLiteThread **ppThread, /* OUT: Write the thread object here */
8038 void *(*xTask)(void*), /* Routine to run in a separate thread */
8039 void *pIn /* Argument passed into xTask() */
8040 ){
8041 SQLiteThread *p;
8042 int rc;
8043
8044 assert( ppThread!=0 );
8045 assert( xTask!=0 );
8046 /* This routine is never used in single-threaded mode */
8047 assert( sqlite3GlobalConfig.bCoreMutex!=0 );
8048
8049 *ppThread = 0;
8050 p = sqlite3Malloc(sizeof(*p));
8051 if( p==0 ) return SQLITE_NOMEM_BKPT;
8052 memset(p, 0, sizeof(*p));
8053 p->xTask = xTask;
8054 p->pIn = pIn;
8055 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
8056 ** function that returns SQLITE_ERROR when passed the argument 200, that
8057 ** forces worker threads to run sequentially and deterministically
8058 ** for testing purposes. */
8059 if( sqlite3FaultSim(200) ){
8060 rc = 1;
8061 }else{
8062 rc = pthread_create(&p->tid, 0, xTask, pIn);
8063 }
8064 if( rc ){
8065 p->done = 1;
8066 p->pOut = xTask(pIn);
8067 }
8068 *ppThread = p;
8069 return SQLITE_OK;
8070 }
8071
8072 /* Get the results of the thread */
8073 SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
8074 int rc;
8075
8076 assert( ppOut!=0 );
8077 if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
8078 if( p->done ){
8079 *ppOut = p->pOut;
8080 rc = SQLITE_OK;
8081 }else{
8082 rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK;
8083 }
8084 sqlite3_free(p);
8085 return rc;
8086 }
8087
8088 #endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */
8089 /******************************** End Unix Pthreads *************************/
8090
8091
8092 /********************************* Win32 Threads ****************************/
8093 #if SQLITE_OS_WIN_THREADS
8094
8095 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
8096 #include <process.h>
8097
8098 /* A running thread */
8099 struct SQLiteThread {
8100 void *tid; /* The thread handle */
8101 unsigned id; /* The thread identifier */
8102 void *(*xTask)(void*); /* The routine to run as a thread */
8103 void *pIn; /* Argument to xTask */
8104 void *pResult; /* Result of xTask */
8105 };
8106
8107 /* Thread procedure Win32 compatibility shim */
8108 static unsigned __stdcall sqlite3ThreadProc(
8109 void *pArg /* IN: Pointer to the SQLiteThread structure */
8110 ){
8111 SQLiteThread *p = (SQLiteThread *)pArg;
8112
8113 assert( p!=0 );
8114 #if 0
8115 /*
8116 ** This assert appears to trigger spuriously on certain
8117 ** versions of Windows, possibly due to _beginthreadex()
8118 ** and/or CreateThread() not fully setting their thread
8119 ** ID parameter before starting the thread.
8120 */
8121 assert( p->id==GetCurrentThreadId() );
8122 #endif
8123 assert( p->xTask!=0 );
8124 p->pResult = p->xTask(p->pIn);
8125
8126 _endthreadex(0);
8127 return 0; /* NOT REACHED */
8128 }
8129
8130 /* Create a new thread */
8131 SQLITE_PRIVATE int sqlite3ThreadCreate(
8132 SQLiteThread **ppThread, /* OUT: Write the thread object here */
8133 void *(*xTask)(void*), /* Routine to run in a separate thread */
8134 void *pIn /* Argument passed into xTask() */
8135 ){
8136 SQLiteThread *p;
8137
8138 assert( ppThread!=0 );
8139 assert( xTask!=0 );
8140 *ppThread = 0;
8141 p = sqlite3Malloc(sizeof(*p));
8142 if( p==0 ) return SQLITE_NOMEM_BKPT;
8143 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a
8144 ** function that returns SQLITE_ERROR when passed the argument 200, that
8145 ** forces worker threads to run sequentially and deterministically
8146 ** (via the sqlite3FaultSim() term of the conditional) for testing
8147 ** purposes. */
8148 if( sqlite3GlobalConfig.bCoreMutex==0 || sqlite3FaultSim(200) ){
8149 memset(p, 0, sizeof(*p));
8150 }else{
8151 p->xTask = xTask;
8152 p->pIn = pIn;
8153 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id);
8154 if( p->tid==0 ){
8155 memset(p, 0, sizeof(*p));
8156 }
8157 }
8158 if( p->xTask==0 ){
8159 p->id = GetCurrentThreadId();
8160 p->pResult = xTask(pIn);
8161 }
8162 *ppThread = p;
8163 return SQLITE_OK;
8164 }
8165
8166 SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject); /* os_win.c */
8167
8168 /* Get the results of the thread */
8169 SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
8170 DWORD rc;
8171 BOOL bRc;
8172
8173 assert( ppOut!=0 );
8174 if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
8175 if( p->xTask==0 ){
8176 /* assert( p->id==GetCurrentThreadId() ); */
8177 rc = WAIT_OBJECT_0;
8178 assert( p->tid==0 );
8179 }else{
8180 assert( p->id!=0 && p->id!=GetCurrentThreadId() );
8181 rc = sqlite3Win32Wait((HANDLE)p->tid);
8182 assert( rc!=WAIT_IO_COMPLETION );
8183 bRc = CloseHandle((HANDLE)p->tid);
8184 assert( bRc );
8185 }
8186 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
8187 sqlite3_free(p);
8188 return (rc==WAIT_OBJECT_0) ? SQLITE_OK : SQLITE_ERROR;
8189 }
8190
8191 #endif /* SQLITE_OS_WIN_THREADS */
8192 /******************************** End Win32 Threads *************************/
8193
8194
8195 /********************************* Single-Threaded **************************/
8196 #ifndef SQLITE_THREADS_IMPLEMENTED
8197 /*
8198 ** This implementation does not actually create a new thread. It does the
8199 ** work of the thread in the main thread, when either the thread is created
8200 ** or when it is joined
8201 */
8202
8203 /* A running thread */
8204 struct SQLiteThread {
8205 void *(*xTask)(void*); /* The routine to run as a thread */
8206 void *pIn; /* Argument to xTask */
8207 void *pResult; /* Result of xTask */
8208 };
8209
8210 /* Create a new thread */
8211 SQLITE_PRIVATE int sqlite3ThreadCreate(
8212 SQLiteThread **ppThread, /* OUT: Write the thread object here */
8213 void *(*xTask)(void*), /* Routine to run in a separate thread */
8214 void *pIn /* Argument passed into xTask() */
8215 ){
8216 SQLiteThread *p;
8217
8218 assert( ppThread!=0 );
8219 assert( xTask!=0 );
8220 *ppThread = 0;
8221 p = sqlite3Malloc(sizeof(*p));
8222 if( p==0 ) return SQLITE_NOMEM_BKPT;
8223 if( (SQLITE_PTR_TO_INT(p)/17)&1 ){
8224 p->xTask = xTask;
8225 p->pIn = pIn;
8226 }else{
8227 p->xTask = 0;
8228 p->pResult = xTask(pIn);
8229 }
8230 *ppThread = p;
8231 return SQLITE_OK;
8232 }
8233
8234 /* Get the results of the thread */
8235 SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){
8236
8237 assert( ppOut!=0 );
8238 if( NEVER(p==0) ) return SQLITE_NOMEM_BKPT;
8239 if( p->xTask ){
8240 *ppOut = p->xTask(p->pIn);
8241 }else{
8242 *ppOut = p->pResult;
8243 }
8244 sqlite3_free(p);
8245
8246 #if defined(SQLITE_TEST)
8247 {
8248 void *pTstAlloc = sqlite3Malloc(10);
8249 if (!pTstAlloc) return SQLITE_NOMEM_BKPT;
8250 sqlite3_free(pTstAlloc);
8251 }
8252 #endif
8253
8254 return SQLITE_OK;
8255 }
8256
8257 #endif /* !defined(SQLITE_THREADS_IMPLEMENTED) */
8258 /****************************** End Single-Threaded *************************/
8259 #endif /* SQLITE_MAX_WORKER_THREADS>0 */
8260
8261 /************** End of threads.c *********************************************/
8262 /************** Begin file utf.c *********************************************/
8263 /*
8264 ** 2004 April 13
8265 **
8266 ** The author disclaims copyright to this source code. In place of
8267 ** a legal notice, here is a blessing:
8268 **
8269 ** May you do good and not evil.
8270 ** May you find forgiveness for yourself and forgive others.
8271 ** May you share freely, never taking more than you give.
8272 **
8273 *************************************************************************
8274 ** This file contains routines used to translate between UTF-8,
8275 ** UTF-16, UTF-16BE, and UTF-16LE.
8276 **
8277 ** Notes on UTF-8:
8278 **
8279 ** Byte-0 Byte-1 Byte-2 Byte-3 Value
8280 ** 0xxxxxxx 00000000 00000000 0xxxxxxx
8281 ** 110yyyyy 10xxxxxx 00000000 00000yyy yyxxxxxx
8282 ** 1110zzzz 10yyyyyy 10xxxxxx 00000000 zzzzyyyy yyxxxxxx
8283 ** 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx 000uuuuu zzzzyyyy yyxxxxxx
8284 **
8285 **
8286 ** Notes on UTF-16: (with wwww+1==uuuuu)
8287 **
8288 ** Word-0 Word-1 Value
8289 ** 110110ww wwzzzzyy 110111yy yyxxxxxx 000uuuuu zzzzyyyy yyxxxxxx
8290 ** zzzzyyyy yyxxxxxx 00000000 zzzzyyyy yyxxxxxx
8291 **
8292 **
8293 ** BOM or Byte Order Mark:
8294 ** 0xff 0xfe little-endian utf-16 follows
8295 ** 0xfe 0xff big-endian utf-16 follows
8296 **
8297 */
8298 /* #include "sqliteInt.h" */
8299 /* #include <assert.h> */
8300 /* #include "vdbeInt.h" */
8301
8302 #if !defined(SQLITE_AMALGAMATION) && SQLITE_BYTEORDER==0
8303 /*
8304 ** The following constant value is used by the SQLITE_BIGENDIAN and
8305 ** SQLITE_LITTLEENDIAN macros.
8306 */
8307 SQLITE_PRIVATE const int sqlite3one = 1;
8308 #endif /* SQLITE_AMALGAMATION && SQLITE_BYTEORDER==0 */
8309
8310 /*
8311 ** This lookup table is used to help decode the first byte of
8312 ** a multi-byte UTF8 character.
8313 */
8314 static const unsigned char sqlite3Utf8Trans1[] = {
8315 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
8316 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
8317 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
8318 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
8319 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
8320 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
8321 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
8322 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
8323 };
8324
8325
8326 #define WRITE_UTF8(zOut, c) { \
8327 if( c<0x00080 ){ \
8328 *zOut++ = (u8)(c&0xFF); \
8329 } \
8330 else if( c<0x00800 ){ \
8331 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \
8332 *zOut++ = 0x80 + (u8)(c & 0x3F); \
8333 } \
8334 else if( c<0x10000 ){ \
8335 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \
8336 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
8337 *zOut++ = 0x80 + (u8)(c & 0x3F); \
8338 }else{ \
8339 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \
8340 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \
8341 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \
8342 *zOut++ = 0x80 + (u8)(c & 0x3F); \
8343 } \
8344 }
8345
8346 #define WRITE_UTF16LE(zOut, c) { \
8347 if( c<=0xFFFF ){ \
8348 *zOut++ = (u8)(c&0x00FF); \
8349 *zOut++ = (u8)((c>>8)&0x00FF); \
8350 }else{ \
8351 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
8352 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
8353 *zOut++ = (u8)(c&0x00FF); \
8354 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
8355 } \
8356 }
8357
8358 #define WRITE_UTF16BE(zOut, c) { \
8359 if( c<=0xFFFF ){ \
8360 *zOut++ = (u8)((c>>8)&0x00FF); \
8361 *zOut++ = (u8)(c&0x00FF); \
8362 }else{ \
8363 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
8364 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
8365 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
8366 *zOut++ = (u8)(c&0x00FF); \
8367 } \
8368 }
8369
8370 #define READ_UTF16LE(zIn, TERM, c){ \
8371 c = (*zIn++); \
8372 c += ((*zIn++)<<8); \
8373 if( c>=0xD800 && c<0xE000 && TERM ){ \
8374 int c2 = (*zIn++); \
8375 c2 += ((*zIn++)<<8); \
8376 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \
8377 } \
8378 }
8379
8380 #define READ_UTF16BE(zIn, TERM, c){ \
8381 c = ((*zIn++)<<8); \
8382 c += (*zIn++); \
8383 if( c>=0xD800 && c<0xE000 && TERM ){ \
8384 int c2 = ((*zIn++)<<8); \
8385 c2 += (*zIn++); \
8386 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \
8387 } \
8388 }
8389
8390 /*
8391 ** Translate a single UTF-8 character. Return the unicode value.
8392 **
8393 ** During translation, assume that the byte that zTerm points
8394 ** is a 0x00.
8395 **
8396 ** Write a pointer to the next unread byte back into *pzNext.
8397 **
8398 ** Notes On Invalid UTF-8:
8399 **
8400 ** * This routine never allows a 7-bit character (0x00 through 0x7f) to
8401 ** be encoded as a multi-byte character. Any multi-byte character that
8402 ** attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
8403 **
8404 ** * This routine never allows a UTF16 surrogate value to be encoded.
8405 ** If a multi-byte character attempts to encode a value between
8406 ** 0xd800 and 0xe000 then it is rendered as 0xfffd.
8407 **
8408 ** * Bytes in the range of 0x80 through 0xbf which occur as the first
8409 ** byte of a character are interpreted as single-byte characters
8410 ** and rendered as themselves even though they are technically
8411 ** invalid characters.
8412 **
8413 ** * This routine accepts over-length UTF8 encodings
8414 ** for unicode values 0x80 and greater. It does not change over-length
8415 ** encodings to 0xfffd as some systems recommend.
8416 */
8417 #define READ_UTF8(zIn, zTerm, c) \
8418 c = *(zIn++); \
8419 if( c>=0xc0 ){ \
8420 c = sqlite3Utf8Trans1[c-0xc0]; \
8421 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \
8422 c = (c<<6) + (0x3f & *(zIn++)); \
8423 } \
8424 if( c<0x80 \
8425 || (c&0xFFFFF800)==0xD800 \
8426 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \
8427 }
8428 SQLITE_PRIVATE u32 sqlite3Utf8Read(
8429 const unsigned char **pz /* Pointer to string from which to read char */
8430 ){
8431 unsigned int c;
8432
8433 /* Same as READ_UTF8() above but without the zTerm parameter.
8434 ** For this routine, we assume the UTF8 string is always zero-terminated.
8435 */
8436 c = *((*pz)++);
8437 if( c>=0xc0 ){
8438 c = sqlite3Utf8Trans1[c-0xc0];
8439 while( (*(*pz) & 0xc0)==0x80 ){
8440 c = (c<<6) + (0x3f & *((*pz)++));
8441 }
8442 if( c<0x80
8443 || (c&0xFFFFF800)==0xD800
8444 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; }
8445 }
8446 return c;
8447 }
8448
8449
8450
8451
8452 /*
8453 ** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
8454 ** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
8455 */
8456 /* #define TRANSLATE_TRACE 1 */
8457
8458 #ifndef SQLITE_OMIT_UTF16
8459 /*
8460 ** This routine transforms the internal text encoding used by pMem to
8461 ** desiredEnc. It is an error if the string is already of the desired
8462 ** encoding, or if *pMem does not contain a string value.
8463 */
8464 SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desired Enc){
8465 int len; /* Maximum length of output string in bytes */
8466 unsigned char *zOut; /* Output buffer */
8467 unsigned char *zIn; /* Input iterator */
8468 unsigned char *zTerm; /* End of input */
8469 unsigned char *z; /* Output iterator */
8470 unsigned int c;
8471
8472 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
8473 assert( pMem->flags&MEM_Str );
8474 assert( pMem->enc!=desiredEnc );
8475 assert( pMem->enc!=0 );
8476 assert( pMem->n>=0 );
8477
8478 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
8479 {
8480 char zBuf[100];
8481 sqlite3VdbeMemPrettyPrint(pMem, zBuf);
8482 fprintf(stderr, "INPUT: %s\n", zBuf);
8483 }
8484 #endif
8485
8486 /* If the translation is between UTF-16 little and big endian, then
8487 ** all that is required is to swap the byte order. This case is handled
8488 ** differently from the others.
8489 */
8490 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
8491 u8 temp;
8492 int rc;
8493 rc = sqlite3VdbeMemMakeWriteable(pMem);
8494 if( rc!=SQLITE_OK ){
8495 assert( rc==SQLITE_NOMEM );
8496 return SQLITE_NOMEM_BKPT;
8497 }
8498 zIn = (u8*)pMem->z;
8499 zTerm = &zIn[pMem->n&~1];
8500 while( zIn<zTerm ){
8501 temp = *zIn;
8502 *zIn = *(zIn+1);
8503 zIn++;
8504 *zIn++ = temp;
8505 }
8506 pMem->enc = desiredEnc;
8507 goto translate_out;
8508 }
8509
8510 /* Set len to the maximum number of bytes required in the output buffer. */
8511 if( desiredEnc==SQLITE_UTF8 ){
8512 /* When converting from UTF-16, the maximum growth results from
8513 ** translating a 2-byte character to a 4-byte UTF-8 character.
8514 ** A single byte is required for the output string
8515 ** nul-terminator.
8516 */
8517 pMem->n &= ~1;
8518 len = pMem->n * 2 + 1;
8519 }else{
8520 /* When converting from UTF-8 to UTF-16 the maximum growth is caused
8521 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
8522 ** character. Two bytes are required in the output buffer for the
8523 ** nul-terminator.
8524 */
8525 len = pMem->n * 2 + 2;
8526 }
8527
8528 /* Set zIn to point at the start of the input buffer and zTerm to point 1
8529 ** byte past the end.
8530 **
8531 ** Variable zOut is set to point at the output buffer, space obtained
8532 ** from sqlite3_malloc().
8533 */
8534 zIn = (u8*)pMem->z;
8535 zTerm = &zIn[pMem->n];
8536 zOut = sqlite3DbMallocRaw(pMem->db, len);
8537 if( !zOut ){
8538 return SQLITE_NOMEM_BKPT;
8539 }
8540 z = zOut;
8541
8542 if( pMem->enc==SQLITE_UTF8 ){
8543 if( desiredEnc==SQLITE_UTF16LE ){
8544 /* UTF-8 -> UTF-16 Little-endian */
8545 while( zIn<zTerm ){
8546 READ_UTF8(zIn, zTerm, c);
8547 WRITE_UTF16LE(z, c);
8548 }
8549 }else{
8550 assert( desiredEnc==SQLITE_UTF16BE );
8551 /* UTF-8 -> UTF-16 Big-endian */
8552 while( zIn<zTerm ){
8553 READ_UTF8(zIn, zTerm, c);
8554 WRITE_UTF16BE(z, c);
8555 }
8556 }
8557 pMem->n = (int)(z - zOut);
8558 *z++ = 0;
8559 }else{
8560 assert( desiredEnc==SQLITE_UTF8 );
8561 if( pMem->enc==SQLITE_UTF16LE ){
8562 /* UTF-16 Little-endian -> UTF-8 */
8563 while( zIn<zTerm ){
8564 READ_UTF16LE(zIn, zIn<zTerm, c);
8565 WRITE_UTF8(z, c);
8566 }
8567 }else{
8568 /* UTF-16 Big-endian -> UTF-8 */
8569 while( zIn<zTerm ){
8570 READ_UTF16BE(zIn, zIn<zTerm, c);
8571 WRITE_UTF8(z, c);
8572 }
8573 }
8574 pMem->n = (int)(z - zOut);
8575 }
8576 *z = 0;
8577 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
8578
8579 c = pMem->flags;
8580 sqlite3VdbeMemRelease(pMem);
8581 pMem->flags = MEM_Str|MEM_Term|(c&(MEM_AffMask|MEM_Subtype));
8582 pMem->enc = desiredEnc;
8583 pMem->z = (char*)zOut;
8584 pMem->zMalloc = pMem->z;
8585 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
8586
8587 translate_out:
8588 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
8589 {
8590 char zBuf[100];
8591 sqlite3VdbeMemPrettyPrint(pMem, zBuf);
8592 fprintf(stderr, "OUTPUT: %s\n", zBuf);
8593 }
8594 #endif
8595 return SQLITE_OK;
8596 }
8597
8598 /*
8599 ** This routine checks for a byte-order mark at the beginning of the
8600 ** UTF-16 string stored in *pMem. If one is present, it is removed and
8601 ** the encoding of the Mem adjusted. This routine does not do any
8602 ** byte-swapping, it just sets Mem.enc appropriately.
8603 **
8604 ** The allocation (static, dynamic etc.) and encoding of the Mem may be
8605 ** changed by this function.
8606 */
8607 SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){
8608 int rc = SQLITE_OK;
8609 u8 bom = 0;
8610
8611 assert( pMem->n>=0 );
8612 if( pMem->n>1 ){
8613 u8 b1 = *(u8 *)pMem->z;
8614 u8 b2 = *(((u8 *)pMem->z) + 1);
8615 if( b1==0xFE && b2==0xFF ){
8616 bom = SQLITE_UTF16BE;
8617 }
8618 if( b1==0xFF && b2==0xFE ){
8619 bom = SQLITE_UTF16LE;
8620 }
8621 }
8622
8623 if( bom ){
8624 rc = sqlite3VdbeMemMakeWriteable(pMem);
8625 if( rc==SQLITE_OK ){
8626 pMem->n -= 2;
8627 memmove(pMem->z, &pMem->z[2], pMem->n);
8628 pMem->z[pMem->n] = '\0';
8629 pMem->z[pMem->n+1] = '\0';
8630 pMem->flags |= MEM_Term;
8631 pMem->enc = bom;
8632 }
8633 }
8634 return rc;
8635 }
8636 #endif /* SQLITE_OMIT_UTF16 */
8637
8638 /*
8639 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
8640 ** return the number of unicode characters in pZ up to (but not including)
8641 ** the first 0x00 byte. If nByte is not less than zero, return the
8642 ** number of unicode characters in the first nByte of pZ (or up to
8643 ** the first 0x00, whichever comes first).
8644 */
8645 SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){
8646 int r = 0;
8647 const u8 *z = (const u8*)zIn;
8648 const u8 *zTerm;
8649 if( nByte>=0 ){
8650 zTerm = &z[nByte];
8651 }else{
8652 zTerm = (const u8*)(-1);
8653 }
8654 assert( z<=zTerm );
8655 while( *z!=0 && z<zTerm ){
8656 SQLITE_SKIP_UTF8(z);
8657 r++;
8658 }
8659 return r;
8660 }
8661
8662 /* This test function is not currently used by the automated test-suite.
8663 ** Hence it is only available in debug builds.
8664 */
8665 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
8666 /*
8667 ** Translate UTF-8 to UTF-8.
8668 **
8669 ** This has the effect of making sure that the string is well-formed
8670 ** UTF-8. Miscoded characters are removed.
8671 **
8672 ** The translation is done in-place and aborted if the output
8673 ** overruns the input.
8674 */
8675 SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
8676 unsigned char *zOut = zIn;
8677 unsigned char *zStart = zIn;
8678 u32 c;
8679
8680 while( zIn[0] && zOut<=zIn ){
8681 c = sqlite3Utf8Read((const u8**)&zIn);
8682 if( c!=0xfffd ){
8683 WRITE_UTF8(zOut, c);
8684 }
8685 }
8686 *zOut = 0;
8687 return (int)(zOut - zStart);
8688 }
8689 #endif
8690
8691 #ifndef SQLITE_OMIT_UTF16
8692 /*
8693 ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
8694 ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
8695 ** be freed by the calling function.
8696 **
8697 ** NULL is returned if there is an allocation error.
8698 */
8699 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 e nc){
8700 Mem m;
8701 memset(&m, 0, sizeof(m));
8702 m.db = db;
8703 sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);
8704 sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
8705 if( db->mallocFailed ){
8706 sqlite3VdbeMemRelease(&m);
8707 m.z = 0;
8708 }
8709 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
8710 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
8711 assert( m.z || db->mallocFailed );
8712 return m.z;
8713 }
8714
8715 /*
8716 ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
8717 ** Return the number of bytes in the first nChar unicode characters
8718 ** in pZ. nChar must be non-negative.
8719 */
8720 SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){
8721 int c;
8722 unsigned char const *z = zIn;
8723 int n = 0;
8724
8725 if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){
8726 while( n<nChar ){
8727 READ_UTF16BE(z, 1, c);
8728 n++;
8729 }
8730 }else{
8731 while( n<nChar ){
8732 READ_UTF16LE(z, 1, c);
8733 n++;
8734 }
8735 }
8736 return (int)(z-(unsigned char const *)zIn);
8737 }
8738
8739 #if defined(SQLITE_TEST)
8740 /*
8741 ** This routine is called from the TCL test function "translate_selftest".
8742 ** It checks that the primitives for serializing and deserializing
8743 ** characters in each encoding are inverses of each other.
8744 */
8745 SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
8746 unsigned int i, t;
8747 unsigned char zBuf[20];
8748 unsigned char *z;
8749 int n;
8750 unsigned int c;
8751
8752 for(i=0; i<0x00110000; i++){
8753 z = zBuf;
8754 WRITE_UTF8(z, i);
8755 n = (int)(z-zBuf);
8756 assert( n>0 && n<=4 );
8757 z[0] = 0;
8758 z = zBuf;
8759 c = sqlite3Utf8Read((const u8**)&z);
8760 t = i;
8761 if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
8762 if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
8763 assert( c==t );
8764 assert( (z-zBuf)==n );
8765 }
8766 for(i=0; i<0x00110000; i++){
8767 if( i>=0xD800 && i<0xE000 ) continue;
8768 z = zBuf;
8769 WRITE_UTF16LE(z, i);
8770 n = (int)(z-zBuf);
8771 assert( n>0 && n<=4 );
8772 z[0] = 0;
8773 z = zBuf;
8774 READ_UTF16LE(z, 1, c);
8775 assert( c==i );
8776 assert( (z-zBuf)==n );
8777 }
8778 for(i=0; i<0x00110000; i++){
8779 if( i>=0xD800 && i<0xE000 ) continue;
8780 z = zBuf;
8781 WRITE_UTF16BE(z, i);
8782 n = (int)(z-zBuf);
8783 assert( n>0 && n<=4 );
8784 z[0] = 0;
8785 z = zBuf;
8786 READ_UTF16BE(z, 1, c);
8787 assert( c==i );
8788 assert( (z-zBuf)==n );
8789 }
8790 }
8791 #endif /* SQLITE_TEST */
8792 #endif /* SQLITE_OMIT_UTF16 */
8793
8794 /************** End of utf.c *************************************************/
8795 /************** Begin file util.c ********************************************/
8796 /*
8797 ** 2001 September 15
8798 **
8799 ** The author disclaims copyright to this source code. In place of
8800 ** a legal notice, here is a blessing:
8801 **
8802 ** May you do good and not evil.
8803 ** May you find forgiveness for yourself and forgive others.
8804 ** May you share freely, never taking more than you give.
8805 **
8806 *************************************************************************
8807 ** Utility functions used throughout sqlite.
8808 **
8809 ** This file contains functions for allocating memory, comparing
8810 ** strings, and stuff like that.
8811 **
8812 */
8813 /* #include "sqliteInt.h" */
8814 /* #include <stdarg.h> */
8815 #if HAVE_ISNAN || SQLITE_HAVE_ISNAN
8816 # include <math.h>
8817 #endif
8818
8819 /*
8820 ** Routine needed to support the testcase() macro.
8821 */
8822 #ifdef SQLITE_COVERAGE_TEST
8823 SQLITE_PRIVATE void sqlite3Coverage(int x){
8824 static unsigned dummy = 0;
8825 dummy += (unsigned)x;
8826 }
8827 #endif
8828
8829 /*
8830 ** Give a callback to the test harness that can be used to simulate faults
8831 ** in places where it is difficult or expensive to do so purely by means
8832 ** of inputs.
8833 **
8834 ** The intent of the integer argument is to let the fault simulator know
8835 ** which of multiple sqlite3FaultSim() calls has been hit.
8836 **
8837 ** Return whatever integer value the test callback returns, or return
8838 ** SQLITE_OK if no test callback is installed.
8839 */
8840 #ifndef SQLITE_UNTESTABLE
8841 SQLITE_PRIVATE int sqlite3FaultSim(int iTest){
8842 int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
8843 return xCallback ? xCallback(iTest) : SQLITE_OK;
8844 }
8845 #endif
8846
8847 #ifndef SQLITE_OMIT_FLOATING_POINT
8848 /*
8849 ** Return true if the floating point value is Not a Number (NaN).
8850 **
8851 ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
8852 ** Otherwise, we have our own implementation that works on most systems.
8853 */
8854 SQLITE_PRIVATE int sqlite3IsNaN(double x){
8855 int rc; /* The value return */
8856 #if !SQLITE_HAVE_ISNAN && !HAVE_ISNAN
8857 /*
8858 ** Systems that support the isnan() library function should probably
8859 ** make use of it by compiling with -DSQLITE_HAVE_ISNAN. But we have
8860 ** found that many systems do not have a working isnan() function so
8861 ** this implementation is provided as an alternative.
8862 **
8863 ** This NaN test sometimes fails if compiled on GCC with -ffast-math.
8864 ** On the other hand, the use of -ffast-math comes with the following
8865 ** warning:
8866 **
8867 ** This option [-ffast-math] should never be turned on by any
8868 ** -O option since it can result in incorrect output for programs
8869 ** which depend on an exact implementation of IEEE or ISO
8870 ** rules/specifications for math functions.
8871 **
8872 ** Under MSVC, this NaN test may fail if compiled with a floating-
8873 ** point precision mode other than /fp:precise. From the MSDN
8874 ** documentation:
8875 **
8876 ** The compiler [with /fp:precise] will properly handle comparisons
8877 ** involving NaN. For example, x != x evaluates to true if x is NaN
8878 ** ...
8879 */
8880 #ifdef __FAST_MATH__
8881 # error SQLite will not work correctly with the -ffast-math option of GCC.
8882 #endif
8883 volatile double y = x;
8884 volatile double z = y;
8885 rc = (y!=z);
8886 #else /* if HAVE_ISNAN */
8887 rc = isnan(x);
8888 #endif /* HAVE_ISNAN */
8889 testcase( rc );
8890 return rc;
8891 }
8892 #endif /* SQLITE_OMIT_FLOATING_POINT */
8893
8894 /*
8895 ** Compute a string length that is limited to what can be stored in
8896 ** lower 30 bits of a 32-bit signed integer.
8897 **
8898 ** The value returned will never be negative. Nor will it ever be greater
8899 ** than the actual length of the string. For very long strings (greater
8900 ** than 1GiB) the value returned might be less than the true string length.
8901 */
8902 SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
8903 if( z==0 ) return 0;
8904 return 0x3fffffff & (int)strlen(z);
8905 }
8906
8907 /*
8908 ** Return the declared type of a column. Or return zDflt if the column
8909 ** has no declared type.
8910 **
8911 ** The column type is an extra string stored after the zero-terminator on
8912 ** the column name if and only if the COLFLAG_HASTYPE flag is set.
8913 */
8914 SQLITE_PRIVATE char *sqlite3ColumnType(Column *pCol, char *zDflt){
8915 if( (pCol->colFlags & COLFLAG_HASTYPE)==0 ) return zDflt;
8916 return pCol->zName + strlen(pCol->zName) + 1;
8917 }
8918
8919 /*
8920 ** Helper function for sqlite3Error() - called rarely. Broken out into
8921 ** a separate routine to avoid unnecessary register saves on entry to
8922 ** sqlite3Error().
8923 */
8924 static SQLITE_NOINLINE void sqlite3ErrorFinish(sqlite3 *db, int err_code){
8925 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
8926 sqlite3SystemError(db, err_code);
8927 }
8928
8929 /*
8930 ** Set the current error code to err_code and clear any prior error message.
8931 ** Also set iSysErrno (by calling sqlite3System) if the err_code indicates
8932 ** that would be appropriate.
8933 */
8934 SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code){
8935 assert( db!=0 );
8936 db->errCode = err_code;
8937 if( err_code || db->pErr ) sqlite3ErrorFinish(db, err_code);
8938 }
8939
8940 /*
8941 ** Load the sqlite3.iSysErrno field if that is an appropriate thing
8942 ** to do based on the SQLite error code in rc.
8943 */
8944 SQLITE_PRIVATE void sqlite3SystemError(sqlite3 *db, int rc){
8945 if( rc==SQLITE_IOERR_NOMEM ) return;
8946 rc &= 0xff;
8947 if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){
8948 db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
8949 }
8950 }
8951
8952 /*
8953 ** Set the most recent error code and error string for the sqlite
8954 ** handle "db". The error code is set to "err_code".
8955 **
8956 ** If it is not NULL, string zFormat specifies the format of the
8957 ** error string in the style of the printf functions: The following
8958 ** format characters are allowed:
8959 **
8960 ** %s Insert a string
8961 ** %z A string that should be freed after use
8962 ** %d Insert an integer
8963 ** %T Insert a token
8964 ** %S Insert the first element of a SrcList
8965 **
8966 ** zFormat and any string tokens that follow it are assumed to be
8967 ** encoded in UTF-8.
8968 **
8969 ** To clear the most recent error for sqlite handle "db", sqlite3Error
8970 ** should be called with err_code set to SQLITE_OK and zFormat set
8971 ** to NULL.
8972 */
8973 SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *z Format, ...){
8974 assert( db!=0 );
8975 db->errCode = err_code;
8976 sqlite3SystemError(db, err_code);
8977 if( zFormat==0 ){
8978 sqlite3Error(db, err_code);
8979 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
8980 char *z;
8981 va_list ap;
8982 va_start(ap, zFormat);
8983 z = sqlite3VMPrintf(db, zFormat, ap);
8984 va_end(ap);
8985 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
8986 }
8987 }
8988
8989 /*
8990 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
8991 ** The following formatting characters are allowed:
8992 **
8993 ** %s Insert a string
8994 ** %z A string that should be freed after use
8995 ** %d Insert an integer
8996 ** %T Insert a token
8997 ** %S Insert the first element of a SrcList
8998 **
8999 ** This function should be used to report any error that occurs while
9000 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
9001 ** last thing the sqlite3_prepare() function does is copy the error
9002 ** stored by this function into the database handle using sqlite3Error().
9003 ** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used
9004 ** during statement execution (sqlite3_step() etc.).
9005 */
9006 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
9007 char *zMsg;
9008 va_list ap;
9009 sqlite3 *db = pParse->db;
9010 va_start(ap, zFormat);
9011 zMsg = sqlite3VMPrintf(db, zFormat, ap);
9012 va_end(ap);
9013 if( db->suppressErr ){
9014 sqlite3DbFree(db, zMsg);
9015 }else{
9016 pParse->nErr++;
9017 sqlite3DbFree(db, pParse->zErrMsg);
9018 pParse->zErrMsg = zMsg;
9019 pParse->rc = SQLITE_ERROR;
9020 }
9021 }
9022
9023 /*
9024 ** Convert an SQL-style quoted string into a normal string by removing
9025 ** the quote characters. The conversion is done in-place. If the
9026 ** input does not begin with a quote character, then this routine
9027 ** is a no-op.
9028 **
9029 ** The input string must be zero-terminated. A new zero-terminator
9030 ** is added to the dequoted string.
9031 **
9032 ** The return value is -1 if no dequoting occurs or the length of the
9033 ** dequoted string, exclusive of the zero terminator, if dequoting does
9034 ** occur.
9035 **
9036 ** 2002-Feb-14: This routine is extended to remove MS-Access style
9037 ** brackets from around identifiers. For example: "[a-b-c]" becomes
9038 ** "a-b-c".
9039 */
9040 SQLITE_PRIVATE void sqlite3Dequote(char *z){
9041 char quote;
9042 int i, j;
9043 if( z==0 ) return;
9044 quote = z[0];
9045 if( !sqlite3Isquote(quote) ) return;
9046 if( quote=='[' ) quote = ']';
9047 for(i=1, j=0;; i++){
9048 assert( z[i] );
9049 if( z[i]==quote ){
9050 if( z[i+1]==quote ){
9051 z[j++] = quote;
9052 i++;
9053 }else{
9054 break;
9055 }
9056 }else{
9057 z[j++] = z[i];
9058 }
9059 }
9060 z[j] = 0;
9061 }
9062
9063 /*
9064 ** Generate a Token object from a string
9065 */
9066 SQLITE_PRIVATE void sqlite3TokenInit(Token *p, char *z){
9067 p->z = z;
9068 p->n = sqlite3Strlen30(z);
9069 }
9070
9071 /* Convenient short-hand */
9072 #define UpperToLower sqlite3UpperToLower
9073
9074 /*
9075 ** Some systems have stricmp(). Others have strcasecmp(). Because
9076 ** there is no consistency, we will define our own.
9077 **
9078 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
9079 ** sqlite3_strnicmp() APIs allow applications and extensions to compare
9080 ** the contents of two buffers containing UTF-8 strings in a
9081 ** case-independent fashion, using the same definition of "case
9082 ** independence" that SQLite uses internally when comparing identifiers.
9083 */
9084 SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){
9085 if( zLeft==0 ){
9086 return zRight ? -1 : 0;
9087 }else if( zRight==0 ){
9088 return 1;
9089 }
9090 return sqlite3StrICmp(zLeft, zRight);
9091 }
9092 SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){
9093 unsigned char *a, *b;
9094 int c;
9095 a = (unsigned char *)zLeft;
9096 b = (unsigned char *)zRight;
9097 for(;;){
9098 c = (int)UpperToLower[*a] - (int)UpperToLower[*b];
9099 if( c || *a==0 ) break;
9100 a++;
9101 b++;
9102 }
9103 return c;
9104 }
9105 SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
9106 register unsigned char *a, *b;
9107 if( zLeft==0 ){
9108 return zRight ? -1 : 0;
9109 }else if( zRight==0 ){
9110 return 1;
9111 }
9112 a = (unsigned char *)zLeft;
9113 b = (unsigned char *)zRight;
9114 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
9115 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
9116 }
9117
9118 /*
9119 ** The string z[] is an text representation of a real number.
9120 ** Convert this string to a double and write it into *pResult.
9121 **
9122 ** The string z[] is length bytes in length (bytes, not characters) and
9123 ** uses the encoding enc. The string is not necessarily zero-terminated.
9124 **
9125 ** Return TRUE if the result is a valid real number (or integer) and FALSE
9126 ** if the string is empty or contains extraneous text. Valid numbers
9127 ** are in one of these formats:
9128 **
9129 ** [+-]digits[E[+-]digits]
9130 ** [+-]digits.[digits][E[+-]digits]
9131 ** [+-].digits[E[+-]digits]
9132 **
9133 ** Leading and trailing whitespace is ignored for the purpose of determining
9134 ** validity.
9135 **
9136 ** If some prefix of the input string is a valid number, this routine
9137 ** returns FALSE but it still converts the prefix and writes the result
9138 ** into *pResult.
9139 */
9140 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 en c){
9141 #ifndef SQLITE_OMIT_FLOATING_POINT
9142 int incr;
9143 const char *zEnd = z + length;
9144 /* sign * significand * (10 ^ (esign * exponent)) */
9145 int sign = 1; /* sign of significand */
9146 i64 s = 0; /* significand */
9147 int d = 0; /* adjust exponent for shifting decimal point */
9148 int esign = 1; /* sign of exponent */
9149 int e = 0; /* exponent */
9150 int eValid = 1; /* True exponent is either not used or is well-formed */
9151 double result;
9152 int nDigits = 0;
9153 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
9154
9155 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
9156 *pResult = 0.0; /* Default return value, in case of an error */
9157
9158 if( enc==SQLITE_UTF8 ){
9159 incr = 1;
9160 }else{
9161 int i;
9162 incr = 2;
9163 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
9164 for(i=3-enc; i<length && z[i]==0; i+=2){}
9165 nonNum = i<length;
9166 zEnd = &z[i^1];
9167 z += (enc&1);
9168 }
9169
9170 /* skip leading spaces */
9171 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
9172 if( z>=zEnd ) return 0;
9173
9174 /* get sign of significand */
9175 if( *z=='-' ){
9176 sign = -1;
9177 z+=incr;
9178 }else if( *z=='+' ){
9179 z+=incr;
9180 }
9181
9182 /* copy max significant digits to significand */
9183 while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
9184 s = s*10 + (*z - '0');
9185 z+=incr, nDigits++;
9186 }
9187
9188 /* skip non-significant significand digits
9189 ** (increase exponent by d to shift decimal left) */
9190 while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++;
9191 if( z>=zEnd ) goto do_atof_calc;
9192
9193 /* if decimal point is present */
9194 if( *z=='.' ){
9195 z+=incr;
9196 /* copy digits from after decimal to significand
9197 ** (decrease exponent by d to shift decimal right) */
9198 while( z<zEnd && sqlite3Isdigit(*z) ){
9199 if( s<((LARGEST_INT64-9)/10) ){
9200 s = s*10 + (*z - '0');
9201 d--;
9202 }
9203 z+=incr, nDigits++;
9204 }
9205 }
9206 if( z>=zEnd ) goto do_atof_calc;
9207
9208 /* if exponent is present */
9209 if( *z=='e' || *z=='E' ){
9210 z+=incr;
9211 eValid = 0;
9212
9213 /* This branch is needed to avoid a (harmless) buffer overread. The
9214 ** special comment alerts the mutation tester that the correct answer
9215 ** is obtained even if the branch is omitted */
9216 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/
9217
9218 /* get sign of exponent */
9219 if( *z=='-' ){
9220 esign = -1;
9221 z+=incr;
9222 }else if( *z=='+' ){
9223 z+=incr;
9224 }
9225 /* copy digits to exponent */
9226 while( z<zEnd && sqlite3Isdigit(*z) ){
9227 e = e<10000 ? (e*10 + (*z - '0')) : 10000;
9228 z+=incr;
9229 eValid = 1;
9230 }
9231 }
9232
9233 /* skip trailing spaces */
9234 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
9235
9236 do_atof_calc:
9237 /* adjust exponent by d, and update sign */
9238 e = (e*esign) + d;
9239 if( e<0 ) {
9240 esign = -1;
9241 e *= -1;
9242 } else {
9243 esign = 1;
9244 }
9245
9246 if( s==0 ) {
9247 /* In the IEEE 754 standard, zero is signed. */
9248 result = sign<0 ? -(double)0 : (double)0;
9249 } else {
9250 /* Attempt to reduce exponent.
9251 **
9252 ** Branches that are not required for the correct answer but which only
9253 ** help to obtain the correct answer faster are marked with special
9254 ** comments, as a hint to the mutation tester.
9255 */
9256 while( e>0 ){ /*OPTIMIZATION-IF-TRUE*/
9257 if( esign>0 ){
9258 if( s>=(LARGEST_INT64/10) ) break; /*OPTIMIZATION-IF-FALSE*/
9259 s *= 10;
9260 }else{
9261 if( s%10!=0 ) break; /*OPTIMIZATION-IF-FALSE*/
9262 s /= 10;
9263 }
9264 e--;
9265 }
9266
9267 /* adjust the sign of significand */
9268 s = sign<0 ? -s : s;
9269
9270 if( e==0 ){ /*OPTIMIZATION-IF-TRUE*/
9271 result = (double)s;
9272 }else{
9273 LONGDOUBLE_TYPE scale = 1.0;
9274 /* attempt to handle extremely small/large numbers better */
9275 if( e>307 ){ /*OPTIMIZATION-IF-TRUE*/
9276 if( e<342 ){ /*OPTIMIZATION-IF-TRUE*/
9277 while( e%308 ) { scale *= 1.0e+1; e -= 1; }
9278 if( esign<0 ){
9279 result = s / scale;
9280 result /= 1.0e+308;
9281 }else{
9282 result = s * scale;
9283 result *= 1.0e+308;
9284 }
9285 }else{ assert( e>=342 );
9286 if( esign<0 ){
9287 result = 0.0*s;
9288 }else{
9289 result = 1e308*1e308*s; /* Infinity */
9290 }
9291 }
9292 }else{
9293 /* 1.0e+22 is the largest power of 10 than can be
9294 ** represented exactly. */
9295 while( e%22 ) { scale *= 1.0e+1; e -= 1; }
9296 while( e>0 ) { scale *= 1.0e+22; e -= 22; }
9297 if( esign<0 ){
9298 result = s / scale;
9299 }else{
9300 result = s * scale;
9301 }
9302 }
9303 }
9304 }
9305
9306 /* store the result */
9307 *pResult = result;
9308
9309 /* return true if number and no extra non-whitespace chracters after */
9310 return z==zEnd && nDigits>0 && eValid && nonNum==0;
9311 #else
9312 return !sqlite3Atoi64(z, pResult, length, enc);
9313 #endif /* SQLITE_OMIT_FLOATING_POINT */
9314 }
9315
9316 /*
9317 ** Compare the 19-character string zNum against the text representation
9318 ** value 2^63: 9223372036854775808. Return negative, zero, or positive
9319 ** if zNum is less than, equal to, or greater than the string.
9320 ** Note that zNum must contain exactly 19 characters.
9321 **
9322 ** Unlike memcmp() this routine is guaranteed to return the difference
9323 ** in the values of the last digit if the only difference is in the
9324 ** last digit. So, for example,
9325 **
9326 ** compare2pow63("9223372036854775800", 1)
9327 **
9328 ** will return -8.
9329 */
9330 static int compare2pow63(const char *zNum, int incr){
9331 int c = 0;
9332 int i;
9333 /* 012345678901234567 */
9334 const char *pow63 = "922337203685477580";
9335 for(i=0; c==0 && i<18; i++){
9336 c = (zNum[i*incr]-pow63[i])*10;
9337 }
9338 if( c==0 ){
9339 c = zNum[18*incr] - '8';
9340 testcase( c==(-1) );
9341 testcase( c==0 );
9342 testcase( c==(+1) );
9343 }
9344 return c;
9345 }
9346
9347 /*
9348 ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
9349 ** routine does *not* accept hexadecimal notation.
9350 **
9351 ** If the zNum value is representable as a 64-bit twos-complement
9352 ** integer, then write that value into *pNum and return 0.
9353 **
9354 ** If zNum is exactly 9223372036854775808, return 2. This special
9355 ** case is broken out because while 9223372036854775808 cannot be a
9356 ** signed 64-bit integer, its negative -9223372036854775808 can be.
9357 **
9358 ** If zNum is too big for a 64-bit integer and is not
9359 ** 9223372036854775808 or if zNum contains any non-numeric text,
9360 ** then return 1.
9361 **
9362 ** length is the number of bytes in the string (bytes, not characters).
9363 ** The string is not necessarily zero-terminated. The encoding is
9364 ** given by enc.
9365 */
9366 SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc ){
9367 int incr;
9368 u64 u = 0;
9369 int neg = 0; /* assume positive */
9370 int i;
9371 int c = 0;
9372 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
9373 const char *zStart;
9374 const char *zEnd = zNum + length;
9375 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
9376 if( enc==SQLITE_UTF8 ){
9377 incr = 1;
9378 }else{
9379 incr = 2;
9380 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
9381 for(i=3-enc; i<length && zNum[i]==0; i+=2){}
9382 nonNum = i<length;
9383 zEnd = &zNum[i^1];
9384 zNum += (enc&1);
9385 }
9386 while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
9387 if( zNum<zEnd ){
9388 if( *zNum=='-' ){
9389 neg = 1;
9390 zNum+=incr;
9391 }else if( *zNum=='+' ){
9392 zNum+=incr;
9393 }
9394 }
9395 zStart = zNum;
9396 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
9397 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
9398 u = u*10 + c - '0';
9399 }
9400 if( u>LARGEST_INT64 ){
9401 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
9402 }else if( neg ){
9403 *pNum = -(i64)u;
9404 }else{
9405 *pNum = (i64)u;
9406 }
9407 testcase( i==18 );
9408 testcase( i==19 );
9409 testcase( i==20 );
9410 if( &zNum[i]<zEnd /* Extra bytes at the end */
9411 || (i==0 && zStart==zNum) /* No digits */
9412 || i>19*incr /* Too many digits */
9413 || nonNum /* UTF16 with high-order bytes non-zero */
9414 ){
9415 /* zNum is empty or contains non-numeric text or is longer
9416 ** than 19 digits (thus guaranteeing that it is too large) */
9417 return 1;
9418 }else if( i<19*incr ){
9419 /* Less than 19 digits, so we know that it fits in 64 bits */
9420 assert( u<=LARGEST_INT64 );
9421 return 0;
9422 }else{
9423 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
9424 c = compare2pow63(zNum, incr);
9425 if( c<0 ){
9426 /* zNum is less than 9223372036854775808 so it fits */
9427 assert( u<=LARGEST_INT64 );
9428 return 0;
9429 }else if( c>0 ){
9430 /* zNum is greater than 9223372036854775808 so it overflows */
9431 return 1;
9432 }else{
9433 /* zNum is exactly 9223372036854775808. Fits if negative. The
9434 ** special case 2 overflow if positive */
9435 assert( u-1==LARGEST_INT64 );
9436 return neg ? 0 : 2;
9437 }
9438 }
9439 }
9440
9441 /*
9442 ** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
9443 ** into a 64-bit signed integer. This routine accepts hexadecimal literals,
9444 ** whereas sqlite3Atoi64() does not.
9445 **
9446 ** Returns:
9447 **
9448 ** 0 Successful transformation. Fits in a 64-bit signed integer.
9449 ** 1 Integer too large for a 64-bit signed integer or is malformed
9450 ** 2 Special case of 9223372036854775808
9451 */
9452 SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
9453 #ifndef SQLITE_OMIT_HEX_INTEGER
9454 if( z[0]=='0'
9455 && (z[1]=='x' || z[1]=='X')
9456 ){
9457 u64 u = 0;
9458 int i, k;
9459 for(i=2; z[i]=='0'; i++){}
9460 for(k=i; sqlite3Isxdigit(z[k]); k++){
9461 u = u*16 + sqlite3HexToInt(z[k]);
9462 }
9463 memcpy(pOut, &u, 8);
9464 return (z[k]==0 && k-i<=16) ? 0 : 1;
9465 }else
9466 #endif /* SQLITE_OMIT_HEX_INTEGER */
9467 {
9468 return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF8);
9469 }
9470 }
9471
9472 /*
9473 ** If zNum represents an integer that will fit in 32-bits, then set
9474 ** *pValue to that integer and return true. Otherwise return false.
9475 **
9476 ** This routine accepts both decimal and hexadecimal notation for integers.
9477 **
9478 ** Any non-numeric characters that following zNum are ignored.
9479 ** This is different from sqlite3Atoi64() which requires the
9480 ** input number to be zero-terminated.
9481 */
9482 SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){
9483 sqlite_int64 v = 0;
9484 int i, c;
9485 int neg = 0;
9486 if( zNum[0]=='-' ){
9487 neg = 1;
9488 zNum++;
9489 }else if( zNum[0]=='+' ){
9490 zNum++;
9491 }
9492 #ifndef SQLITE_OMIT_HEX_INTEGER
9493 else if( zNum[0]=='0'
9494 && (zNum[1]=='x' || zNum[1]=='X')
9495 && sqlite3Isxdigit(zNum[2])
9496 ){
9497 u32 u = 0;
9498 zNum += 2;
9499 while( zNum[0]=='0' ) zNum++;
9500 for(i=0; sqlite3Isxdigit(zNum[i]) && i<8; i++){
9501 u = u*16 + sqlite3HexToInt(zNum[i]);
9502 }
9503 if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){
9504 memcpy(pValue, &u, 4);
9505 return 1;
9506 }else{
9507 return 0;
9508 }
9509 }
9510 #endif
9511 while( zNum[0]=='0' ) zNum++;
9512 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
9513 v = v*10 + c;
9514 }
9515
9516 /* The longest decimal representation of a 32 bit integer is 10 digits:
9517 **
9518 ** 1234567890
9519 ** 2^31 -> 2147483648
9520 */
9521 testcase( i==10 );
9522 if( i>10 ){
9523 return 0;
9524 }
9525 testcase( v-neg==2147483647 );
9526 if( v-neg>2147483647 ){
9527 return 0;
9528 }
9529 if( neg ){
9530 v = -v;
9531 }
9532 *pValue = (int)v;
9533 return 1;
9534 }
9535
9536 /*
9537 ** Return a 32-bit integer value extracted from a string. If the
9538 ** string is not an integer, just return 0.
9539 */
9540 SQLITE_PRIVATE int sqlite3Atoi(const char *z){
9541 int x = 0;
9542 if( z ) sqlite3GetInt32(z, &x);
9543 return x;
9544 }
9545
9546 /*
9547 ** The variable-length integer encoding is as follows:
9548 **
9549 ** KEY:
9550 ** A = 0xxxxxxx 7 bits of data and one flag bit
9551 ** B = 1xxxxxxx 7 bits of data and one flag bit
9552 ** C = xxxxxxxx 8 bits of data
9553 **
9554 ** 7 bits - A
9555 ** 14 bits - BA
9556 ** 21 bits - BBA
9557 ** 28 bits - BBBA
9558 ** 35 bits - BBBBA
9559 ** 42 bits - BBBBBA
9560 ** 49 bits - BBBBBBA
9561 ** 56 bits - BBBBBBBA
9562 ** 64 bits - BBBBBBBBC
9563 */
9564
9565 /*
9566 ** Write a 64-bit variable-length integer to memory starting at p[0].
9567 ** The length of data write will be between 1 and 9 bytes. The number
9568 ** of bytes written is returned.
9569 **
9570 ** A variable-length integer consists of the lower 7 bits of each byte
9571 ** for all bytes that have the 8th bit set and one byte with the 8th
9572 ** bit clear. Except, if we get to the 9th byte, it stores the full
9573 ** 8 bits and is the last byte.
9574 */
9575 static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){
9576 int i, j, n;
9577 u8 buf[10];
9578 if( v & (((u64)0xff000000)<<32) ){
9579 p[8] = (u8)v;
9580 v >>= 8;
9581 for(i=7; i>=0; i--){
9582 p[i] = (u8)((v & 0x7f) | 0x80);
9583 v >>= 7;
9584 }
9585 return 9;
9586 }
9587 n = 0;
9588 do{
9589 buf[n++] = (u8)((v & 0x7f) | 0x80);
9590 v >>= 7;
9591 }while( v!=0 );
9592 buf[0] &= 0x7f;
9593 assert( n<=9 );
9594 for(i=0, j=n-1; j>=0; j--, i++){
9595 p[i] = buf[j];
9596 }
9597 return n;
9598 }
9599 SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){
9600 if( v<=0x7f ){
9601 p[0] = v&0x7f;
9602 return 1;
9603 }
9604 if( v<=0x3fff ){
9605 p[0] = ((v>>7)&0x7f)|0x80;
9606 p[1] = v&0x7f;
9607 return 2;
9608 }
9609 return putVarint64(p,v);
9610 }
9611
9612 /*
9613 ** Bitmasks used by sqlite3GetVarint(). These precomputed constants
9614 ** are defined here rather than simply putting the constant expressions
9615 ** inline in order to work around bugs in the RVT compiler.
9616 **
9617 ** SLOT_2_0 A mask for (0x7f<<14) | 0x7f
9618 **
9619 ** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0
9620 */
9621 #define SLOT_2_0 0x001fc07f
9622 #define SLOT_4_2_0 0xf01fc07f
9623
9624
9625 /*
9626 ** Read a 64-bit variable-length integer from memory starting at p[0].
9627 ** Return the number of bytes read. The value is stored in *v.
9628 */
9629 SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
9630 u32 a,b,s;
9631
9632 a = *p;
9633 /* a: p0 (unmasked) */
9634 if (!(a&0x80))
9635 {
9636 *v = a;
9637 return 1;
9638 }
9639
9640 p++;
9641 b = *p;
9642 /* b: p1 (unmasked) */
9643 if (!(b&0x80))
9644 {
9645 a &= 0x7f;
9646 a = a<<7;
9647 a |= b;
9648 *v = a;
9649 return 2;
9650 }
9651
9652 /* Verify that constants are precomputed correctly */
9653 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
9654 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
9655
9656 p++;
9657 a = a<<14;
9658 a |= *p;
9659 /* a: p0<<14 | p2 (unmasked) */
9660 if (!(a&0x80))
9661 {
9662 a &= SLOT_2_0;
9663 b &= 0x7f;
9664 b = b<<7;
9665 a |= b;
9666 *v = a;
9667 return 3;
9668 }
9669
9670 /* CSE1 from below */
9671 a &= SLOT_2_0;
9672 p++;
9673 b = b<<14;
9674 b |= *p;
9675 /* b: p1<<14 | p3 (unmasked) */
9676 if (!(b&0x80))
9677 {
9678 b &= SLOT_2_0;
9679 /* moved CSE1 up */
9680 /* a &= (0x7f<<14)|(0x7f); */
9681 a = a<<7;
9682 a |= b;
9683 *v = a;
9684 return 4;
9685 }
9686
9687 /* a: p0<<14 | p2 (masked) */
9688 /* b: p1<<14 | p3 (unmasked) */
9689 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
9690 /* moved CSE1 up */
9691 /* a &= (0x7f<<14)|(0x7f); */
9692 b &= SLOT_2_0;
9693 s = a;
9694 /* s: p0<<14 | p2 (masked) */
9695
9696 p++;
9697 a = a<<14;
9698 a |= *p;
9699 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
9700 if (!(a&0x80))
9701 {
9702 /* we can skip these cause they were (effectively) done above
9703 ** while calculating s */
9704 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
9705 /* b &= (0x7f<<14)|(0x7f); */
9706 b = b<<7;
9707 a |= b;
9708 s = s>>18;
9709 *v = ((u64)s)<<32 | a;
9710 return 5;
9711 }
9712
9713 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
9714 s = s<<7;
9715 s |= b;
9716 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
9717
9718 p++;
9719 b = b<<14;
9720 b |= *p;
9721 /* b: p1<<28 | p3<<14 | p5 (unmasked) */
9722 if (!(b&0x80))
9723 {
9724 /* we can skip this cause it was (effectively) done above in calc'ing s */
9725 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
9726 a &= SLOT_2_0;
9727 a = a<<7;
9728 a |= b;
9729 s = s>>18;
9730 *v = ((u64)s)<<32 | a;
9731 return 6;
9732 }
9733
9734 p++;
9735 a = a<<14;
9736 a |= *p;
9737 /* a: p2<<28 | p4<<14 | p6 (unmasked) */
9738 if (!(a&0x80))
9739 {
9740 a &= SLOT_4_2_0;
9741 b &= SLOT_2_0;
9742 b = b<<7;
9743 a |= b;
9744 s = s>>11;
9745 *v = ((u64)s)<<32 | a;
9746 return 7;
9747 }
9748
9749 /* CSE2 from below */
9750 a &= SLOT_2_0;
9751 p++;
9752 b = b<<14;
9753 b |= *p;
9754 /* b: p3<<28 | p5<<14 | p7 (unmasked) */
9755 if (!(b&0x80))
9756 {
9757 b &= SLOT_4_2_0;
9758 /* moved CSE2 up */
9759 /* a &= (0x7f<<14)|(0x7f); */
9760 a = a<<7;
9761 a |= b;
9762 s = s>>4;
9763 *v = ((u64)s)<<32 | a;
9764 return 8;
9765 }
9766
9767 p++;
9768 a = a<<15;
9769 a |= *p;
9770 /* a: p4<<29 | p6<<15 | p8 (unmasked) */
9771
9772 /* moved CSE2 up */
9773 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
9774 b &= SLOT_2_0;
9775 b = b<<8;
9776 a |= b;
9777
9778 s = s<<4;
9779 b = p[-4];
9780 b &= 0x7f;
9781 b = b>>3;
9782 s |= b;
9783
9784 *v = ((u64)s)<<32 | a;
9785
9786 return 9;
9787 }
9788
9789 /*
9790 ** Read a 32-bit variable-length integer from memory starting at p[0].
9791 ** Return the number of bytes read. The value is stored in *v.
9792 **
9793 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
9794 ** integer, then set *v to 0xffffffff.
9795 **
9796 ** A MACRO version, getVarint32, is provided which inlines the
9797 ** single-byte case. All code should use the MACRO version as
9798 ** this function assumes the single-byte case has already been handled.
9799 */
9800 SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
9801 u32 a,b;
9802
9803 /* The 1-byte case. Overwhelmingly the most common. Handled inline
9804 ** by the getVarin32() macro */
9805 a = *p;
9806 /* a: p0 (unmasked) */
9807 #ifndef getVarint32
9808 if (!(a&0x80))
9809 {
9810 /* Values between 0 and 127 */
9811 *v = a;
9812 return 1;
9813 }
9814 #endif
9815
9816 /* The 2-byte case */
9817 p++;
9818 b = *p;
9819 /* b: p1 (unmasked) */
9820 if (!(b&0x80))
9821 {
9822 /* Values between 128 and 16383 */
9823 a &= 0x7f;
9824 a = a<<7;
9825 *v = a | b;
9826 return 2;
9827 }
9828
9829 /* The 3-byte case */
9830 p++;
9831 a = a<<14;
9832 a |= *p;
9833 /* a: p0<<14 | p2 (unmasked) */
9834 if (!(a&0x80))
9835 {
9836 /* Values between 16384 and 2097151 */
9837 a &= (0x7f<<14)|(0x7f);
9838 b &= 0x7f;
9839 b = b<<7;
9840 *v = a | b;
9841 return 3;
9842 }
9843
9844 /* A 32-bit varint is used to store size information in btrees.
9845 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
9846 ** A 3-byte varint is sufficient, for example, to record the size
9847 ** of a 1048569-byte BLOB or string.
9848 **
9849 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
9850 ** rare larger cases can be handled by the slower 64-bit varint
9851 ** routine.
9852 */
9853 #if 1
9854 {
9855 u64 v64;
9856 u8 n;
9857
9858 p -= 2;
9859 n = sqlite3GetVarint(p, &v64);
9860 assert( n>3 && n<=9 );
9861 if( (v64 & SQLITE_MAX_U32)!=v64 ){
9862 *v = 0xffffffff;
9863 }else{
9864 *v = (u32)v64;
9865 }
9866 return n;
9867 }
9868
9869 #else
9870 /* For following code (kept for historical record only) shows an
9871 ** unrolling for the 3- and 4-byte varint cases. This code is
9872 ** slightly faster, but it is also larger and much harder to test.
9873 */
9874 p++;
9875 b = b<<14;
9876 b |= *p;
9877 /* b: p1<<14 | p3 (unmasked) */
9878 if (!(b&0x80))
9879 {
9880 /* Values between 2097152 and 268435455 */
9881 b &= (0x7f<<14)|(0x7f);
9882 a &= (0x7f<<14)|(0x7f);
9883 a = a<<7;
9884 *v = a | b;
9885 return 4;
9886 }
9887
9888 p++;
9889 a = a<<14;
9890 a |= *p;
9891 /* a: p0<<28 | p2<<14 | p4 (unmasked) */
9892 if (!(a&0x80))
9893 {
9894 /* Values between 268435456 and 34359738367 */
9895 a &= SLOT_4_2_0;
9896 b &= SLOT_4_2_0;
9897 b = b<<7;
9898 *v = a | b;
9899 return 5;
9900 }
9901
9902 /* We can only reach this point when reading a corrupt database
9903 ** file. In that case we are not in any hurry. Use the (relatively
9904 ** slow) general-purpose sqlite3GetVarint() routine to extract the
9905 ** value. */
9906 {
9907 u64 v64;
9908 u8 n;
9909
9910 p -= 4;
9911 n = sqlite3GetVarint(p, &v64);
9912 assert( n>5 && n<=9 );
9913 *v = (u32)v64;
9914 return n;
9915 }
9916 #endif
9917 }
9918
9919 /*
9920 ** Return the number of bytes that will be needed to store the given
9921 ** 64-bit integer.
9922 */
9923 SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
9924 int i;
9925 for(i=1; (v >>= 7)!=0; i++){ assert( i<10 ); }
9926 return i;
9927 }
9928
9929
9930 /*
9931 ** Read or write a four-byte big-endian integer value.
9932 */
9933 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
9934 #if SQLITE_BYTEORDER==4321
9935 u32 x;
9936 memcpy(&x,p,4);
9937 return x;
9938 #elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
9939 u32 x;
9940 memcpy(&x,p,4);
9941 return __builtin_bswap32(x);
9942 #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
9943 u32 x;
9944 memcpy(&x,p,4);
9945 return _byteswap_ulong(x);
9946 #else
9947 testcase( p[0]&0x80 );
9948 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
9949 #endif
9950 }
9951 SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
9952 #if SQLITE_BYTEORDER==4321
9953 memcpy(p,&v,4);
9954 #elif SQLITE_BYTEORDER==1234 && (GCC_VERSION>=4003000 || CLANG_VERSION>=3000000)
9955 u32 x = __builtin_bswap32(v);
9956 memcpy(p,&x,4);
9957 #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
9958 u32 x = _byteswap_ulong(v);
9959 memcpy(p,&x,4);
9960 #else
9961 p[0] = (u8)(v>>24);
9962 p[1] = (u8)(v>>16);
9963 p[2] = (u8)(v>>8);
9964 p[3] = (u8)v;
9965 #endif
9966 }
9967
9968
9969
9970 /*
9971 ** Translate a single byte of Hex into an integer.
9972 ** This routine only works if h really is a valid hexadecimal
9973 ** character: 0..9a..fA..F
9974 */
9975 SQLITE_PRIVATE u8 sqlite3HexToInt(int h){
9976 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
9977 #ifdef SQLITE_ASCII
9978 h += 9*(1&(h>>6));
9979 #endif
9980 #ifdef SQLITE_EBCDIC
9981 h += 9*(1&~(h>>4));
9982 #endif
9983 return (u8)(h & 0xf);
9984 }
9985
9986 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
9987 /*
9988 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
9989 ** value. Return a pointer to its binary value. Space to hold the
9990 ** binary value has been obtained from malloc and must be freed by
9991 ** the calling routine.
9992 */
9993 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
9994 char *zBlob;
9995 int i;
9996
9997 zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1);
9998 n--;
9999 if( zBlob ){
10000 for(i=0; i<n; i+=2){
10001 zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
10002 }
10003 zBlob[i/2] = 0;
10004 }
10005 return zBlob;
10006 }
10007 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
10008
10009 /*
10010 ** Log an error that is an API call on a connection pointer that should
10011 ** not have been used. The "type" of connection pointer is given as the
10012 ** argument. The zType is a word like "NULL" or "closed" or "invalid".
10013 */
10014 static void logBadConnection(const char *zType){
10015 sqlite3_log(SQLITE_MISUSE,
10016 "API call with %s database connection pointer",
10017 zType
10018 );
10019 }
10020
10021 /*
10022 ** Check to make sure we have a valid db pointer. This test is not
10023 ** foolproof but it does provide some measure of protection against
10024 ** misuse of the interface such as passing in db pointers that are
10025 ** NULL or which have been previously closed. If this routine returns
10026 ** 1 it means that the db pointer is valid and 0 if it should not be
10027 ** dereferenced for any reason. The calling function should invoke
10028 ** SQLITE_MISUSE immediately.
10029 **
10030 ** sqlite3SafetyCheckOk() requires that the db pointer be valid for
10031 ** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
10032 ** open properly and is not fit for general use but which can be
10033 ** used as an argument to sqlite3_errmsg() or sqlite3_close().
10034 */
10035 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
10036 u32 magic;
10037 if( db==0 ){
10038 logBadConnection("NULL");
10039 return 0;
10040 }
10041 magic = db->magic;
10042 if( magic!=SQLITE_MAGIC_OPEN ){
10043 if( sqlite3SafetyCheckSickOrOk(db) ){
10044 testcase( sqlite3GlobalConfig.xLog!=0 );
10045 logBadConnection("unopened");
10046 }
10047 return 0;
10048 }else{
10049 return 1;
10050 }
10051 }
10052 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
10053 u32 magic;
10054 magic = db->magic;
10055 if( magic!=SQLITE_MAGIC_SICK &&
10056 magic!=SQLITE_MAGIC_OPEN &&
10057 magic!=SQLITE_MAGIC_BUSY ){
10058 testcase( sqlite3GlobalConfig.xLog!=0 );
10059 logBadConnection("invalid");
10060 return 0;
10061 }else{
10062 return 1;
10063 }
10064 }
10065
10066 /*
10067 ** Attempt to add, substract, or multiply the 64-bit signed value iB against
10068 ** the other 64-bit signed integer at *pA and store the result in *pA.
10069 ** Return 0 on success. Or if the operation would have resulted in an
10070 ** overflow, leave *pA unchanged and return 1.
10071 */
10072 SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
10073 #if GCC_VERSION>=5004000 || CLANG_VERSION>=4000000
10074 return __builtin_add_overflow(*pA, iB, pA);
10075 #else
10076 i64 iA = *pA;
10077 testcase( iA==0 ); testcase( iA==1 );
10078 testcase( iB==-1 ); testcase( iB==0 );
10079 if( iB>=0 ){
10080 testcase( iA>0 && LARGEST_INT64 - iA == iB );
10081 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
10082 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
10083 }else{
10084 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
10085 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
10086 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
10087 }
10088 *pA += iB;
10089 return 0;
10090 #endif
10091 }
10092 SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
10093 #if GCC_VERSION>=5004000 || CLANG_VERSION>=4000000
10094 return __builtin_sub_overflow(*pA, iB, pA);
10095 #else
10096 testcase( iB==SMALLEST_INT64+1 );
10097 if( iB==SMALLEST_INT64 ){
10098 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
10099 if( (*pA)>=0 ) return 1;
10100 *pA -= iB;
10101 return 0;
10102 }else{
10103 return sqlite3AddInt64(pA, -iB);
10104 }
10105 #endif
10106 }
10107 SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
10108 /* TODO(shess): Chromium Android clang generates a link error:
10109 ** undefined reference to '__mulodi4'
10110 ** UPDATE(shess): Also, apparently, 32-bit Linux clang.
10111 */
10112 #if GCC_VERSION>=5004000 || \
10113 (CLANG_VERSION>=4000000 && !defined(__ANDROID__) && \
10114 (!defined(__linux__) || !defined(__i386__)))
10115 return __builtin_mul_overflow(*pA, iB, pA);
10116 #else
10117 i64 iA = *pA;
10118 if( iB>0 ){
10119 if( iA>LARGEST_INT64/iB ) return 1;
10120 if( iA<SMALLEST_INT64/iB ) return 1;
10121 }else if( iB<0 ){
10122 if( iA>0 ){
10123 if( iB<SMALLEST_INT64/iA ) return 1;
10124 }else if( iA<0 ){
10125 if( iB==SMALLEST_INT64 ) return 1;
10126 if( iA==SMALLEST_INT64 ) return 1;
10127 if( -iA>LARGEST_INT64/-iB ) return 1;
10128 }
10129 }
10130 *pA = iA*iB;
10131 return 0;
10132 #endif
10133 }
10134
10135 /*
10136 ** Compute the absolute value of a 32-bit signed integer, of possible. Or
10137 ** if the integer has a value of -2147483648, return +2147483647
10138 */
10139 SQLITE_PRIVATE int sqlite3AbsInt32(int x){
10140 if( x>=0 ) return x;
10141 if( x==(int)0x80000000 ) return 0x7fffffff;
10142 return -x;
10143 }
10144
10145 #ifdef SQLITE_ENABLE_8_3_NAMES
10146 /*
10147 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
10148 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
10149 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
10150 ** three characters, then shorten the suffix on z[] to be the last three
10151 ** characters of the original suffix.
10152 **
10153 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
10154 ** do the suffix shortening regardless of URI parameter.
10155 **
10156 ** Examples:
10157 **
10158 ** test.db-journal => test.nal
10159 ** test.db-wal => test.wal
10160 ** test.db-shm => test.shm
10161 ** test.db-mj7f3319fa => test.9fa
10162 */
10163 SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
10164 #if SQLITE_ENABLE_8_3_NAMES<2
10165 if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
10166 #endif
10167 {
10168 int i, sz;
10169 sz = sqlite3Strlen30(z);
10170 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
10171 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
10172 }
10173 }
10174 #endif
10175
10176 /*
10177 ** Find (an approximate) sum of two LogEst values. This computation is
10178 ** not a simple "+" operator because LogEst is stored as a logarithmic
10179 ** value.
10180 **
10181 */
10182 SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
10183 static const unsigned char x[] = {
10184 10, 10, /* 0,1 */
10185 9, 9, /* 2,3 */
10186 8, 8, /* 4,5 */
10187 7, 7, 7, /* 6,7,8 */
10188 6, 6, 6, /* 9,10,11 */
10189 5, 5, 5, /* 12-14 */
10190 4, 4, 4, 4, /* 15-18 */
10191 3, 3, 3, 3, 3, 3, /* 19-24 */
10192 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
10193 };
10194 if( a>=b ){
10195 if( a>b+49 ) return a;
10196 if( a>b+31 ) return a+1;
10197 return a+x[a-b];
10198 }else{
10199 if( b>a+49 ) return b;
10200 if( b>a+31 ) return b+1;
10201 return b+x[b-a];
10202 }
10203 }
10204
10205 /*
10206 ** Convert an integer into a LogEst. In other words, compute an
10207 ** approximation for 10*log2(x).
10208 */
10209 SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
10210 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
10211 LogEst y = 40;
10212 if( x<8 ){
10213 if( x<2 ) return 0;
10214 while( x<8 ){ y -= 10; x <<= 1; }
10215 }else{
10216 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/
10217 while( x>15 ){ y += 10; x >>= 1; }
10218 }
10219 return a[x&7] + y - 10;
10220 }
10221
10222 #ifndef SQLITE_OMIT_VIRTUALTABLE
10223 /*
10224 ** Convert a double into a LogEst
10225 ** In other words, compute an approximation for 10*log2(x).
10226 */
10227 SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double x){
10228 u64 a;
10229 LogEst e;
10230 assert( sizeof(x)==8 && sizeof(a)==8 );
10231 if( x<=1 ) return 0;
10232 if( x<=2000000000 ) return sqlite3LogEst((u64)x);
10233 memcpy(&a, &x, 8);
10234 e = (a>>52) - 1022;
10235 return e*10;
10236 }
10237 #endif /* SQLITE_OMIT_VIRTUALTABLE */
10238
10239 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
10240 defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \
10241 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
10242 /*
10243 ** Convert a LogEst into an integer.
10244 **
10245 ** Note that this routine is only used when one or more of various
10246 ** non-standard compile-time options is enabled.
10247 */
10248 SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst x){
10249 u64 n;
10250 n = x%10;
10251 x /= 10;
10252 if( n>=5 ) n -= 2;
10253 else if( n>=1 ) n -= 1;
10254 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
10255 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
10256 if( x>60 ) return (u64)LARGEST_INT64;
10257 #else
10258 /* If only SQLITE_ENABLE_STAT3_OR_STAT4 is on, then the largest input
10259 ** possible to this routine is 310, resulting in a maximum x of 31 */
10260 assert( x<=60 );
10261 #endif
10262 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
10263 }
10264 #endif /* defined SCANSTAT or STAT4 or ESTIMATED_ROWS */
10265
10266 /*
10267 ** Add a new name/number pair to a VList. This might require that the
10268 ** VList object be reallocated, so return the new VList. If an OOM
10269 ** error occurs, the original VList returned and the
10270 ** db->mallocFailed flag is set.
10271 **
10272 ** A VList is really just an array of integers. To destroy a VList,
10273 ** simply pass it to sqlite3DbFree().
10274 **
10275 ** The first integer is the number of integers allocated for the whole
10276 ** VList. The second integer is the number of integers actually used.
10277 ** Each name/number pair is encoded by subsequent groups of 3 or more
10278 ** integers.
10279 **
10280 ** Each name/number pair starts with two integers which are the numeric
10281 ** value for the pair and the size of the name/number pair, respectively.
10282 ** The text name overlays one or more following integers. The text name
10283 ** is always zero-terminated.
10284 **
10285 ** Conceptually:
10286 **
10287 ** struct VList {
10288 ** int nAlloc; // Number of allocated slots
10289 ** int nUsed; // Number of used slots
10290 ** struct VListEntry {
10291 ** int iValue; // Value for this entry
10292 ** int nSlot; // Slots used by this entry
10293 ** // ... variable name goes here
10294 ** } a[0];
10295 ** }
10296 **
10297 ** During code generation, pointers to the variable names within the
10298 ** VList are taken. When that happens, nAlloc is set to zero as an
10299 ** indication that the VList may never again be enlarged, since the
10300 ** accompanying realloc() would invalidate the pointers.
10301 */
10302 SQLITE_PRIVATE VList *sqlite3VListAdd(
10303 sqlite3 *db, /* The database connection used for malloc() */
10304 VList *pIn, /* The input VList. Might be NULL */
10305 const char *zName, /* Name of symbol to add */
10306 int nName, /* Bytes of text in zName */
10307 int iVal /* Value to associate with zName */
10308 ){
10309 int nInt; /* number of sizeof(int) objects needed for zName */
10310 char *z; /* Pointer to where zName will be stored */
10311 int i; /* Index in pIn[] where zName is stored */
10312
10313 nInt = nName/4 + 3;
10314 assert( pIn==0 || pIn[0]>=3 ); /* Verify ok to add new elements */
10315 if( pIn==0 || pIn[1]+nInt > pIn[0] ){
10316 /* Enlarge the allocation */
10317 int nAlloc = (pIn ? pIn[0]*2 : 10) + nInt;
10318 VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int));
10319 if( pOut==0 ) return pIn;
10320 if( pIn==0 ) pOut[1] = 2;
10321 pIn = pOut;
10322 pIn[0] = nAlloc;
10323 }
10324 i = pIn[1];
10325 pIn[i] = iVal;
10326 pIn[i+1] = nInt;
10327 z = (char*)&pIn[i+2];
10328 pIn[1] = i+nInt;
10329 assert( pIn[1]<=pIn[0] );
10330 memcpy(z, zName, nName);
10331 z[nName] = 0;
10332 return pIn;
10333 }
10334
10335 /*
10336 ** Return a pointer to the name of a variable in the given VList that
10337 ** has the value iVal. Or return a NULL if there is no such variable in
10338 ** the list
10339 */
10340 SQLITE_PRIVATE const char *sqlite3VListNumToName(VList *pIn, int iVal){
10341 int i, mx;
10342 if( pIn==0 ) return 0;
10343 mx = pIn[1];
10344 i = 2;
10345 do{
10346 if( pIn[i]==iVal ) return (char*)&pIn[i+2];
10347 i += pIn[i+1];
10348 }while( i<mx );
10349 return 0;
10350 }
10351
10352 /*
10353 ** Return the number of the variable named zName, if it is in VList.
10354 ** or return 0 if there is no such variable.
10355 */
10356 SQLITE_PRIVATE int sqlite3VListNameToNum(VList *pIn, const char *zName, int nNam e){
10357 int i, mx;
10358 if( pIn==0 ) return 0;
10359 mx = pIn[1];
10360 i = 2;
10361 do{
10362 const char *z = (const char*)&pIn[i+2];
10363 if( strncmp(z,zName,nName)==0 && z[nName]==0 ) return pIn[i];
10364 i += pIn[i+1];
10365 }while( i<mx );
10366 return 0;
10367 }
10368
10369 /************** End of util.c ************************************************/
10370 /************** Begin file hash.c ********************************************/
10371 /*
10372 ** 2001 September 22
10373 **
10374 ** The author disclaims copyright to this source code. In place of
10375 ** a legal notice, here is a blessing:
10376 **
10377 ** May you do good and not evil.
10378 ** May you find forgiveness for yourself and forgive others.
10379 ** May you share freely, never taking more than you give.
10380 **
10381 *************************************************************************
10382 ** This is the implementation of generic hash-tables
10383 ** used in SQLite.
10384 */
10385 /* #include "sqliteInt.h" */
10386 /* #include <assert.h> */
10387
10388 /* Turn bulk memory into a hash table object by initializing the
10389 ** fields of the Hash structure.
10390 **
10391 ** "pNew" is a pointer to the hash table that is to be initialized.
10392 */
10393 SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){
10394 assert( pNew!=0 );
10395 pNew->first = 0;
10396 pNew->count = 0;
10397 pNew->htsize = 0;
10398 pNew->ht = 0;
10399 }
10400
10401 /* Remove all entries from a hash table. Reclaim all memory.
10402 ** Call this routine to delete a hash table or to reset a hash table
10403 ** to the empty state.
10404 */
10405 SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
10406 HashElem *elem; /* For looping over all elements of the table */
10407
10408 assert( pH!=0 );
10409 elem = pH->first;
10410 pH->first = 0;
10411 sqlite3_free(pH->ht);
10412 pH->ht = 0;
10413 pH->htsize = 0;
10414 while( elem ){
10415 HashElem *next_elem = elem->next;
10416 sqlite3_free(elem);
10417 elem = next_elem;
10418 }
10419 pH->count = 0;
10420 }
10421
10422 /*
10423 ** The hashing function.
10424 */
10425 static unsigned int strHash(const char *z){
10426 unsigned int h = 0;
10427 unsigned char c;
10428 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
10429 /* Knuth multiplicative hashing. (Sorting & Searching, p. 510).
10430 ** 0x9e3779b1 is 2654435761 which is the closest prime number to
10431 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
10432 h += sqlite3UpperToLower[c];
10433 h *= 0x9e3779b1;
10434 }
10435 return h;
10436 }
10437
10438
10439 /* Link pNew element into the hash table pH. If pEntry!=0 then also
10440 ** insert pNew into the pEntry hash bucket.
10441 */
10442 static void insertElement(
10443 Hash *pH, /* The complete hash table */
10444 struct _ht *pEntry, /* The entry into which pNew is inserted */
10445 HashElem *pNew /* The element to be inserted */
10446 ){
10447 HashElem *pHead; /* First element already in pEntry */
10448 if( pEntry ){
10449 pHead = pEntry->count ? pEntry->chain : 0;
10450 pEntry->count++;
10451 pEntry->chain = pNew;
10452 }else{
10453 pHead = 0;
10454 }
10455 if( pHead ){
10456 pNew->next = pHead;
10457 pNew->prev = pHead->prev;
10458 if( pHead->prev ){ pHead->prev->next = pNew; }
10459 else { pH->first = pNew; }
10460 pHead->prev = pNew;
10461 }else{
10462 pNew->next = pH->first;
10463 if( pH->first ){ pH->first->prev = pNew; }
10464 pNew->prev = 0;
10465 pH->first = pNew;
10466 }
10467 }
10468
10469
10470 /* Resize the hash table so that it cantains "new_size" buckets.
10471 **
10472 ** The hash table might fail to resize if sqlite3_malloc() fails or
10473 ** if the new size is the same as the prior size.
10474 ** Return TRUE if the resize occurs and false if not.
10475 */
10476 static int rehash(Hash *pH, unsigned int new_size){
10477 struct _ht *new_ht; /* The new hash table */
10478 HashElem *elem, *next_elem; /* For looping over existing elements */
10479
10480 #if SQLITE_MALLOC_SOFT_LIMIT>0
10481 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
10482 new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
10483 }
10484 if( new_size==pH->htsize ) return 0;
10485 #endif
10486
10487 /* The inability to allocates space for a larger hash table is
10488 ** a performance hit but it is not a fatal error. So mark the
10489 ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of
10490 ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
10491 ** only zeroes the requested number of bytes whereas this module will
10492 ** use the actual amount of space allocated for the hash table (which
10493 ** may be larger than the requested amount).
10494 */
10495 sqlite3BeginBenignMalloc();
10496 new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
10497 sqlite3EndBenignMalloc();
10498
10499 if( new_ht==0 ) return 0;
10500 sqlite3_free(pH->ht);
10501 pH->ht = new_ht;
10502 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
10503 memset(new_ht, 0, new_size*sizeof(struct _ht));
10504 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
10505 unsigned int h = strHash(elem->pKey) % new_size;
10506 next_elem = elem->next;
10507 insertElement(pH, &new_ht[h], elem);
10508 }
10509 return 1;
10510 }
10511
10512 /* This function (for internal use only) locates an element in an
10513 ** hash table that matches the given key. The hash for this key is
10514 ** also computed and returned in the *pH parameter.
10515 */
10516 static HashElem *findElementWithHash(
10517 const Hash *pH, /* The pH to be searched */
10518 const char *pKey, /* The key we are searching for */
10519 unsigned int *pHash /* Write the hash value here */
10520 ){
10521 HashElem *elem; /* Used to loop thru the element list */
10522 int count; /* Number of elements left to test */
10523 unsigned int h; /* The computed hash */
10524
10525 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
10526 struct _ht *pEntry;
10527 h = strHash(pKey) % pH->htsize;
10528 pEntry = &pH->ht[h];
10529 elem = pEntry->chain;
10530 count = pEntry->count;
10531 }else{
10532 h = 0;
10533 elem = pH->first;
10534 count = pH->count;
10535 }
10536 *pHash = h;
10537 while( count-- ){
10538 assert( elem!=0 );
10539 if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
10540 return elem;
10541 }
10542 elem = elem->next;
10543 }
10544 return 0;
10545 }
10546
10547 /* Remove a single entry from the hash table given a pointer to that
10548 ** element and a hash on the element's key.
10549 */
10550 static void removeElementGivenHash(
10551 Hash *pH, /* The pH containing "elem" */
10552 HashElem* elem, /* The element to be removed from the pH */
10553 unsigned int h /* Hash value for the element */
10554 ){
10555 struct _ht *pEntry;
10556 if( elem->prev ){
10557 elem->prev->next = elem->next;
10558 }else{
10559 pH->first = elem->next;
10560 }
10561 if( elem->next ){
10562 elem->next->prev = elem->prev;
10563 }
10564 if( pH->ht ){
10565 pEntry = &pH->ht[h];
10566 if( pEntry->chain==elem ){
10567 pEntry->chain = elem->next;
10568 }
10569 pEntry->count--;
10570 assert( pEntry->count>=0 );
10571 }
10572 sqlite3_free( elem );
10573 pH->count--;
10574 if( pH->count==0 ){
10575 assert( pH->first==0 );
10576 assert( pH->count==0 );
10577 sqlite3HashClear(pH);
10578 }
10579 }
10580
10581 /* Attempt to locate an element of the hash table pH with a key
10582 ** that matches pKey. Return the data for this element if it is
10583 ** found, or NULL if there is no match.
10584 */
10585 SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey){
10586 HashElem *elem; /* The element that matches key */
10587 unsigned int h; /* A hash on key */
10588
10589 assert( pH!=0 );
10590 assert( pKey!=0 );
10591 elem = findElementWithHash(pH, pKey, &h);
10592 return elem ? elem->data : 0;
10593 }
10594
10595 /* Insert an element into the hash table pH. The key is pKey
10596 ** and the data is "data".
10597 **
10598 ** If no element exists with a matching key, then a new
10599 ** element is created and NULL is returned.
10600 **
10601 ** If another element already exists with the same key, then the
10602 ** new data replaces the old data and the old data is returned.
10603 ** The key is not copied in this instance. If a malloc fails, then
10604 ** the new data is returned and the hash table is unchanged.
10605 **
10606 ** If the "data" parameter to this function is NULL, then the
10607 ** element corresponding to "key" is removed from the hash table.
10608 */
10609 SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){
10610 unsigned int h; /* the hash of the key modulo hash table size */
10611 HashElem *elem; /* Used to loop thru the element list */
10612 HashElem *new_elem; /* New element added to the pH */
10613
10614 assert( pH!=0 );
10615 assert( pKey!=0 );
10616 elem = findElementWithHash(pH,pKey,&h);
10617 if( elem ){
10618 void *old_data = elem->data;
10619 if( data==0 ){
10620 removeElementGivenHash(pH,elem,h);
10621 }else{
10622 elem->data = data;
10623 elem->pKey = pKey;
10624 }
10625 return old_data;
10626 }
10627 if( data==0 ) return 0;
10628 new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
10629 if( new_elem==0 ) return data;
10630 new_elem->pKey = pKey;
10631 new_elem->data = data;
10632 pH->count++;
10633 if( pH->count>=10 && pH->count > 2*pH->htsize ){
10634 if( rehash(pH, pH->count*2) ){
10635 assert( pH->htsize>0 );
10636 h = strHash(pKey) % pH->htsize;
10637 }
10638 }
10639 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
10640 return 0;
10641 }
10642
10643 /************** End of hash.c ************************************************/
10644 /************** Begin file opcodes.c *****************************************/
10645 /* Automatically generated. Do not edit */
10646 /* See the tool/mkopcodec.tcl script for details. */
10647 #if !defined(SQLITE_OMIT_EXPLAIN) \
10648 || defined(VDBE_PROFILE) \
10649 || defined(SQLITE_DEBUG)
10650 #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG)
10651 # define OpHelp(X) "\0" X
10652 #else
10653 # define OpHelp(X)
10654 #endif
10655 SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
10656 static const char *const azName[] = {
10657 /* 0 */ "Savepoint" OpHelp(""),
10658 /* 1 */ "AutoCommit" OpHelp(""),
10659 /* 2 */ "Transaction" OpHelp(""),
10660 /* 3 */ "SorterNext" OpHelp(""),
10661 /* 4 */ "PrevIfOpen" OpHelp(""),
10662 /* 5 */ "NextIfOpen" OpHelp(""),
10663 /* 6 */ "Prev" OpHelp(""),
10664 /* 7 */ "Next" OpHelp(""),
10665 /* 8 */ "Checkpoint" OpHelp(""),
10666 /* 9 */ "JournalMode" OpHelp(""),
10667 /* 10 */ "Vacuum" OpHelp(""),
10668 /* 11 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
10669 /* 12 */ "VUpdate" OpHelp("data=r[P3@P2]"),
10670 /* 13 */ "Goto" OpHelp(""),
10671 /* 14 */ "Gosub" OpHelp(""),
10672 /* 15 */ "InitCoroutine" OpHelp(""),
10673 /* 16 */ "Yield" OpHelp(""),
10674 /* 17 */ "MustBeInt" OpHelp(""),
10675 /* 18 */ "Jump" OpHelp(""),
10676 /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
10677 /* 20 */ "Once" OpHelp(""),
10678 /* 21 */ "If" OpHelp(""),
10679 /* 22 */ "IfNot" OpHelp(""),
10680 /* 23 */ "SeekLT" OpHelp("key=r[P3@P4]"),
10681 /* 24 */ "SeekLE" OpHelp("key=r[P3@P4]"),
10682 /* 25 */ "SeekGE" OpHelp("key=r[P3@P4]"),
10683 /* 26 */ "SeekGT" OpHelp("key=r[P3@P4]"),
10684 /* 27 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
10685 /* 28 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
10686 /* 29 */ "NoConflict" OpHelp("key=r[P3@P4]"),
10687 /* 30 */ "NotFound" OpHelp("key=r[P3@P4]"),
10688 /* 31 */ "Found" OpHelp("key=r[P3@P4]"),
10689 /* 32 */ "SeekRowid" OpHelp("intkey=r[P3]"),
10690 /* 33 */ "NotExists" OpHelp("intkey=r[P3]"),
10691 /* 34 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
10692 /* 35 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
10693 /* 36 */ "Ne" OpHelp("IF r[P3]!=r[P1]"),
10694 /* 37 */ "Eq" OpHelp("IF r[P3]==r[P1]"),
10695 /* 38 */ "Gt" OpHelp("IF r[P3]>r[P1]"),
10696 /* 39 */ "Le" OpHelp("IF r[P3]<=r[P1]"),
10697 /* 40 */ "Lt" OpHelp("IF r[P3]<r[P1]"),
10698 /* 41 */ "Ge" OpHelp("IF r[P3]>=r[P1]"),
10699 /* 42 */ "ElseNotEq" OpHelp(""),
10700 /* 43 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
10701 /* 44 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
10702 /* 45 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
10703 /* 46 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
10704 /* 47 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
10705 /* 48 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
10706 /* 49 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
10707 /* 50 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
10708 /* 51 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
10709 /* 52 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
10710 /* 53 */ "Last" OpHelp(""),
10711 /* 54 */ "BitNot" OpHelp("r[P1]= ~r[P1]"),
10712 /* 55 */ "SorterSort" OpHelp(""),
10713 /* 56 */ "Sort" OpHelp(""),
10714 /* 57 */ "Rewind" OpHelp(""),
10715 /* 58 */ "IdxLE" OpHelp("key=r[P3@P4]"),
10716 /* 59 */ "IdxGT" OpHelp("key=r[P3@P4]"),
10717 /* 60 */ "IdxLT" OpHelp("key=r[P3@P4]"),
10718 /* 61 */ "IdxGE" OpHelp("key=r[P3@P4]"),
10719 /* 62 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
10720 /* 63 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
10721 /* 64 */ "Program" OpHelp(""),
10722 /* 65 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
10723 /* 66 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
10724 /* 67 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
10725 /* 68 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
10726 /* 69 */ "IncrVacuum" OpHelp(""),
10727 /* 70 */ "VNext" OpHelp(""),
10728 /* 71 */ "Init" OpHelp("Start at P2"),
10729 /* 72 */ "Return" OpHelp(""),
10730 /* 73 */ "EndCoroutine" OpHelp(""),
10731 /* 74 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
10732 /* 75 */ "Halt" OpHelp(""),
10733 /* 76 */ "Integer" OpHelp("r[P2]=P1"),
10734 /* 77 */ "Int64" OpHelp("r[P2]=P4"),
10735 /* 78 */ "String" OpHelp("r[P2]='P4' (len=P1)"),
10736 /* 79 */ "Null" OpHelp("r[P2..P3]=NULL"),
10737 /* 80 */ "SoftNull" OpHelp("r[P1]=NULL"),
10738 /* 81 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"),
10739 /* 82 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"),
10740 /* 83 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"),
10741 /* 84 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
10742 /* 85 */ "SCopy" OpHelp("r[P2]=r[P1]"),
10743 /* 86 */ "IntCopy" OpHelp("r[P2]=r[P1]"),
10744 /* 87 */ "ResultRow" OpHelp("output=r[P1@P2]"),
10745 /* 88 */ "CollSeq" OpHelp(""),
10746 /* 89 */ "Function0" OpHelp("r[P3]=func(r[P2@P5])"),
10747 /* 90 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"),
10748 /* 91 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"),
10749 /* 92 */ "RealAffinity" OpHelp(""),
10750 /* 93 */ "Cast" OpHelp("affinity(r[P1])"),
10751 /* 94 */ "Permutation" OpHelp(""),
10752 /* 95 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
10753 /* 96 */ "Column" OpHelp("r[P3]=PX"),
10754 /* 97 */ "String8" OpHelp("r[P2]='P4'"),
10755 /* 98 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
10756 /* 99 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
10757 /* 100 */ "Count" OpHelp("r[P2]=count()"),
10758 /* 101 */ "ReadCookie" OpHelp(""),
10759 /* 102 */ "SetCookie" OpHelp(""),
10760 /* 103 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
10761 /* 104 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
10762 /* 105 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
10763 /* 106 */ "OpenAutoindex" OpHelp("nColumn=P2"),
10764 /* 107 */ "OpenEphemeral" OpHelp("nColumn=P2"),
10765 /* 108 */ "SorterOpen" OpHelp(""),
10766 /* 109 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
10767 /* 110 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
10768 /* 111 */ "Close" OpHelp(""),
10769 /* 112 */ "ColumnsUsed" OpHelp(""),
10770 /* 113 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
10771 /* 114 */ "NewRowid" OpHelp("r[P2]=rowid"),
10772 /* 115 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
10773 /* 116 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"),
10774 /* 117 */ "Delete" OpHelp(""),
10775 /* 118 */ "ResetCount" OpHelp(""),
10776 /* 119 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
10777 /* 120 */ "SorterData" OpHelp("r[P2]=data"),
10778 /* 121 */ "RowData" OpHelp("r[P2]=data"),
10779 /* 122 */ "Rowid" OpHelp("r[P2]=rowid"),
10780 /* 123 */ "NullRow" OpHelp(""),
10781 /* 124 */ "SorterInsert" OpHelp("key=r[P2]"),
10782 /* 125 */ "IdxInsert" OpHelp("key=r[P2]"),
10783 /* 126 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
10784 /* 127 */ "Seek" OpHelp("Move P3 to P1.rowid"),
10785 /* 128 */ "IdxRowid" OpHelp("r[P2]=rowid"),
10786 /* 129 */ "Destroy" OpHelp(""),
10787 /* 130 */ "Clear" OpHelp(""),
10788 /* 131 */ "ResetSorter" OpHelp(""),
10789 /* 132 */ "Real" OpHelp("r[P2]=P4"),
10790 /* 133 */ "CreateIndex" OpHelp("r[P2]=root iDb=P1"),
10791 /* 134 */ "CreateTable" OpHelp("r[P2]=root iDb=P1"),
10792 /* 135 */ "ParseSchema" OpHelp(""),
10793 /* 136 */ "LoadAnalysis" OpHelp(""),
10794 /* 137 */ "DropTable" OpHelp(""),
10795 /* 138 */ "DropIndex" OpHelp(""),
10796 /* 139 */ "DropTrigger" OpHelp(""),
10797 /* 140 */ "IntegrityCk" OpHelp(""),
10798 /* 141 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
10799 /* 142 */ "Param" OpHelp(""),
10800 /* 143 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
10801 /* 144 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
10802 /* 145 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3] ) else r[P2]=(-1)"),
10803 /* 146 */ "AggStep0" OpHelp("accum=r[P3] step(r[P2@P5])"),
10804 /* 147 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
10805 /* 148 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
10806 /* 149 */ "Expire" OpHelp(""),
10807 /* 150 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
10808 /* 151 */ "VBegin" OpHelp(""),
10809 /* 152 */ "VCreate" OpHelp(""),
10810 /* 153 */ "VDestroy" OpHelp(""),
10811 /* 154 */ "VOpen" OpHelp(""),
10812 /* 155 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
10813 /* 156 */ "VRename" OpHelp(""),
10814 /* 157 */ "Pagecount" OpHelp(""),
10815 /* 158 */ "MaxPgcnt" OpHelp(""),
10816 /* 159 */ "CursorHint" OpHelp(""),
10817 /* 160 */ "Noop" OpHelp(""),
10818 /* 161 */ "Explain" OpHelp(""),
10819 };
10820 return azName[i];
10821 }
10822 #endif
10823
10824 /************** End of opcodes.c *********************************************/
10825 /************** Begin file os_unix.c *****************************************/
10826 /*
10827 ** 2004 May 22
10828 **
10829 ** The author disclaims copyright to this source code. In place of
10830 ** a legal notice, here is a blessing:
10831 **
10832 ** May you do good and not evil.
10833 ** May you find forgiveness for yourself and forgive others.
10834 ** May you share freely, never taking more than you give.
10835 **
10836 ******************************************************************************
10837 **
10838 ** This file contains the VFS implementation for unix-like operating systems
10839 ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
10840 **
10841 ** There are actually several different VFS implementations in this file.
10842 ** The differences are in the way that file locking is done. The default
10843 ** implementation uses Posix Advisory Locks. Alternative implementations
10844 ** use flock(), dot-files, various proprietary locking schemas, or simply
10845 ** skip locking all together.
10846 **
10847 ** This source file is organized into divisions where the logic for various
10848 ** subfunctions is contained within the appropriate division. PLEASE
10849 ** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed
10850 ** in the correct division and should be clearly labeled.
10851 **
10852 ** The layout of divisions is as follows:
10853 **
10854 ** * General-purpose declarations and utility functions.
10855 ** * Unique file ID logic used by VxWorks.
10856 ** * Various locking primitive implementations (all except proxy locking):
10857 ** + for Posix Advisory Locks
10858 ** + for no-op locks
10859 ** + for dot-file locks
10860 ** + for flock() locking
10861 ** + for named semaphore locks (VxWorks only)
10862 ** + for AFP filesystem locks (MacOSX only)
10863 ** * sqlite3_file methods not associated with locking.
10864 ** * Definitions of sqlite3_io_methods objects for all locking
10865 ** methods plus "finder" functions for each locking method.
10866 ** * sqlite3_vfs method implementations.
10867 ** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
10868 ** * Definitions of sqlite3_vfs objects for all locking methods
10869 ** plus implementations of sqlite3_os_init() and sqlite3_os_end().
10870 */
10871 /* #include "sqliteInt.h" */
10872 #if SQLITE_OS_UNIX /* This file is used on unix only */
10873
10874 /*
10875 ** There are various methods for file locking used for concurrency
10876 ** control:
10877 **
10878 ** 1. POSIX locking (the default),
10879 ** 2. No locking,
10880 ** 3. Dot-file locking,
10881 ** 4. flock() locking,
10882 ** 5. AFP locking (OSX only),
10883 ** 6. Named POSIX semaphores (VXWorks only),
10884 ** 7. proxy locking. (OSX only)
10885 **
10886 ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
10887 ** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
10888 ** selection of the appropriate locking style based on the filesystem
10889 ** where the database is located.
10890 */
10891 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
10892 # if defined(__APPLE__)
10893 # define SQLITE_ENABLE_LOCKING_STYLE 1
10894 # else
10895 # define SQLITE_ENABLE_LOCKING_STYLE 0
10896 # endif
10897 #endif
10898
10899 /* Use pread() and pwrite() if they are available */
10900 #if defined(__APPLE__)
10901 # define HAVE_PREAD 1
10902 # define HAVE_PWRITE 1
10903 #endif
10904 #if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64)
10905 # undef USE_PREAD
10906 # define USE_PREAD64 1
10907 #elif defined(HAVE_PREAD) && defined(HAVE_PWRITE)
10908 # undef USE_PREAD64
10909 # define USE_PREAD 1
10910 #endif
10911
10912 /*
10913 ** standard include files.
10914 */
10915 #include <sys/types.h>
10916 #include <sys/stat.h>
10917 #include <fcntl.h>
10918 #include <unistd.h>
10919 /* #include <time.h> */
10920 #include <sys/time.h>
10921 #include <errno.h>
10922 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
10923 # include <sys/mman.h>
10924 #endif
10925
10926 #if SQLITE_ENABLE_LOCKING_STYLE
10927 # include <sys/ioctl.h>
10928 # include <sys/file.h>
10929 # include <sys/param.h>
10930 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
10931
10932 #if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
10933 (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
10934 # if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \
10935 && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0))
10936 # define HAVE_GETHOSTUUID 1
10937 # else
10938 # warning "gethostuuid() is disabled."
10939 # endif
10940 #endif
10941
10942
10943 #if OS_VXWORKS
10944 /* # include <sys/ioctl.h> */
10945 # include <semaphore.h>
10946 # include <limits.h>
10947 #endif /* OS_VXWORKS */
10948
10949 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
10950 # include <sys/mount.h>
10951 #endif
10952
10953 #ifdef HAVE_UTIME
10954 # include <utime.h>
10955 #endif
10956
10957 /*
10958 ** Allowed values of unixFile.fsFlags
10959 */
10960 #define SQLITE_FSFLAGS_IS_MSDOS 0x1
10961
10962 /*
10963 ** If we are to be thread-safe, include the pthreads header and define
10964 ** the SQLITE_UNIX_THREADS macro.
10965 */
10966 #if SQLITE_THREADSAFE
10967 /* # include <pthread.h> */
10968 # define SQLITE_UNIX_THREADS 1
10969 #endif
10970
10971 /*
10972 ** Default permissions when creating a new file
10973 */
10974 #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
10975 # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
10976 #endif
10977
10978 /*
10979 ** Default permissions when creating auto proxy dir
10980 */
10981 #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
10982 # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
10983 #endif
10984
10985 /*
10986 ** Maximum supported path-length.
10987 */
10988 #define MAX_PATHNAME 512
10989
10990 /*
10991 ** Maximum supported symbolic links
10992 */
10993 #define SQLITE_MAX_SYMLINKS 100
10994
10995 /* Always cast the getpid() return type for compatibility with
10996 ** kernel modules in VxWorks. */
10997 #define osGetpid(X) (pid_t)getpid()
10998
10999 /*
11000 ** Only set the lastErrno if the error code is a real error and not
11001 ** a normal expected return code of SQLITE_BUSY or SQLITE_OK
11002 */
11003 #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY))
11004
11005 /* Forward references */
11006 typedef struct unixShm unixShm; /* Connection shared memory */
11007 typedef struct unixShmNode unixShmNode; /* Shared memory instance */
11008 typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
11009 typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */
11010
11011 /*
11012 ** Sometimes, after a file handle is closed by SQLite, the file descriptor
11013 ** cannot be closed immediately. In these cases, instances of the following
11014 ** structure are used to store the file descriptor while waiting for an
11015 ** opportunity to either close or reuse it.
11016 */
11017 struct UnixUnusedFd {
11018 int fd; /* File descriptor to close */
11019 int flags; /* Flags this file descriptor was opened with */
11020 UnixUnusedFd *pNext; /* Next unused file descriptor on same file */
11021 };
11022
11023 /*
11024 ** The unixFile structure is subclass of sqlite3_file specific to the unix
11025 ** VFS implementations.
11026 */
11027 typedef struct unixFile unixFile;
11028 struct unixFile {
11029 sqlite3_io_methods const *pMethod; /* Always the first entry */
11030 sqlite3_vfs *pVfs; /* The VFS that created this unixFile */
11031 unixInodeInfo *pInode; /* Info about locks on this inode */
11032 int h; /* The file descriptor */
11033 unsigned char eFileLock; /* The type of lock held on this fd */
11034 unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
11035 int lastErrno; /* The unix errno from last I/O error */
11036 void *lockingContext; /* Locking style specific state */
11037 UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
11038 const char *zPath; /* Name of the file */
11039 unixShm *pShm; /* Shared memory segment information */
11040 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
11041 #if SQLITE_MAX_MMAP_SIZE>0
11042 int nFetchOut; /* Number of outstanding xFetch refs */
11043 sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */
11044 sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */
11045 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
11046 void *pMapRegion; /* Memory mapped region */
11047 #endif
11048 #ifdef __QNXNTO__
11049 int sectorSize; /* Device sector size */
11050 int deviceCharacteristics; /* Precomputed device characteristics */
11051 #endif
11052 #if SQLITE_ENABLE_LOCKING_STYLE
11053 int openFlags; /* The flags specified at open() */
11054 #endif
11055 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
11056 unsigned fsFlags; /* cached details from statfs() */
11057 #endif
11058 #if OS_VXWORKS
11059 struct vxworksFileId *pId; /* Unique file ID */
11060 #endif
11061 #ifdef SQLITE_DEBUG
11062 /* The next group of variables are used to track whether or not the
11063 ** transaction counter in bytes 24-27 of database files are updated
11064 ** whenever any part of the database changes. An assertion fault will
11065 ** occur if a file is updated without also updating the transaction
11066 ** counter. This test is made to avoid new problems similar to the
11067 ** one described by ticket #3584.
11068 */
11069 unsigned char transCntrChng; /* True if the transaction counter changed */
11070 unsigned char dbUpdate; /* True if any part of database file changed */
11071 unsigned char inNormalWrite; /* True if in a normal write operation */
11072
11073 #endif
11074
11075 #ifdef SQLITE_TEST
11076 /* In test mode, increase the size of this structure a bit so that
11077 ** it is larger than the struct CrashFile defined in test6.c.
11078 */
11079 char aPadding[32];
11080 #endif
11081 };
11082
11083 /* This variable holds the process id (pid) from when the xRandomness()
11084 ** method was called. If xOpen() is called from a different process id,
11085 ** indicating that a fork() has occurred, the PRNG will be reset.
11086 */
11087 static pid_t randomnessPid = 0;
11088
11089 /*
11090 ** Allowed values for the unixFile.ctrlFlags bitmask:
11091 */
11092 #define UNIXFILE_EXCL 0x01 /* Connections from one process only */
11093 #define UNIXFILE_RDONLY 0x02 /* Connection is read only */
11094 #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
11095 #ifndef SQLITE_DISABLE_DIRSYNC
11096 # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
11097 #else
11098 # define UNIXFILE_DIRSYNC 0x00
11099 #endif
11100 #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
11101 #define UNIXFILE_DELETE 0x20 /* Delete on close */
11102 #define UNIXFILE_URI 0x40 /* Filename might have query parameters */
11103 #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */
11104
11105 /*
11106 ** Include code that is common to all os_*.c files
11107 */
11108 /************** Include os_common.h in the middle of os_unix.c ***************/
11109 /************** Begin file os_common.h ***************************************/
11110 /*
11111 ** 2004 May 22
11112 **
11113 ** The author disclaims copyright to this source code. In place of
11114 ** a legal notice, here is a blessing:
11115 **
11116 ** May you do good and not evil.
11117 ** May you find forgiveness for yourself and forgive others.
11118 ** May you share freely, never taking more than you give.
11119 **
11120 ******************************************************************************
11121 **
11122 ** This file contains macros and a little bit of code that is common to
11123 ** all of the platform-specific files (os_*.c) and is #included into those
11124 ** files.
11125 **
11126 ** This file should be #included by the os_*.c files only. It is not a
11127 ** general purpose header file.
11128 */
11129 #ifndef _OS_COMMON_H_
11130 #define _OS_COMMON_H_
11131
11132 /*
11133 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
11134 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
11135 ** switch. The following code should catch this problem at compile-time.
11136 */
11137 #ifdef MEMORY_DEBUG
11138 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
11139 #endif
11140
11141 /*
11142 ** Macros for performance tracing. Normally turned off. Only works
11143 ** on i486 hardware.
11144 */
11145 #ifdef SQLITE_PERFORMANCE_TRACE
11146
11147 /*
11148 ** hwtime.h contains inline assembler code for implementing
11149 ** high-performance timing routines.
11150 */
11151 /************** Include hwtime.h in the middle of os_common.h ****************/
11152 /************** Begin file hwtime.h ******************************************/
11153 /*
11154 ** 2008 May 27
11155 **
11156 ** The author disclaims copyright to this source code. In place of
11157 ** a legal notice, here is a blessing:
11158 **
11159 ** May you do good and not evil.
11160 ** May you find forgiveness for yourself and forgive others.
11161 ** May you share freely, never taking more than you give.
11162 **
11163 ******************************************************************************
11164 **
11165 ** This file contains inline asm code for retrieving "high-performance"
11166 ** counters for x86 class CPUs.
11167 */
11168 #ifndef SQLITE_HWTIME_H
11169 #define SQLITE_HWTIME_H
11170
11171 /*
11172 ** The following routine only works on pentium-class (or newer) processors.
11173 ** It uses the RDTSC opcode to read the cycle count value out of the
11174 ** processor and returns that value. This can be used for high-res
11175 ** profiling.
11176 */
11177 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
11178 (defined(i386) || defined(__i386__) || defined(_M_IX86))
11179
11180 #if defined(__GNUC__)
11181
11182 __inline__ sqlite_uint64 sqlite3Hwtime(void){
11183 unsigned int lo, hi;
11184 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
11185 return (sqlite_uint64)hi << 32 | lo;
11186 }
11187
11188 #elif defined(_MSC_VER)
11189
11190 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
11191 __asm {
11192 rdtsc
11193 ret ; return value at EDX:EAX
11194 }
11195 }
11196
11197 #endif
11198
11199 #elif (defined(__GNUC__) && defined(__x86_64__))
11200
11201 __inline__ sqlite_uint64 sqlite3Hwtime(void){
11202 unsigned long val;
11203 __asm__ __volatile__ ("rdtsc" : "=A" (val));
11204 return val;
11205 }
11206
11207 #elif (defined(__GNUC__) && defined(__ppc__))
11208
11209 __inline__ sqlite_uint64 sqlite3Hwtime(void){
11210 unsigned long long retval;
11211 unsigned long junk;
11212 __asm__ __volatile__ ("\n\
11213 1: mftbu %1\n\
11214 mftb %L0\n\
11215 mftbu %0\n\
11216 cmpw %0,%1\n\
11217 bne 1b"
11218 : "=r" (retval), "=r" (junk));
11219 return retval;
11220 }
11221
11222 #else
11223
11224 #error Need implementation of sqlite3Hwtime() for your platform.
11225
11226 /*
11227 ** To compile without implementing sqlite3Hwtime() for your platform,
11228 ** you can remove the above #error and use the following
11229 ** stub function. You will lose timing support for many
11230 ** of the debugging and testing utilities, but it should at
11231 ** least compile and run.
11232 */
11233 SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
11234
11235 #endif
11236
11237 #endif /* !defined(SQLITE_HWTIME_H) */
11238
11239 /************** End of hwtime.h **********************************************/
11240 /************** Continuing where we left off in os_common.h ******************/
11241
11242 static sqlite_uint64 g_start;
11243 static sqlite_uint64 g_elapsed;
11244 #define TIMER_START g_start=sqlite3Hwtime()
11245 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
11246 #define TIMER_ELAPSED g_elapsed
11247 #else
11248 #define TIMER_START
11249 #define TIMER_END
11250 #define TIMER_ELAPSED ((sqlite_uint64)0)
11251 #endif
11252
11253 /*
11254 ** If we compile with the SQLITE_TEST macro set, then the following block
11255 ** of code will give us the ability to simulate a disk I/O error. This
11256 ** is used for testing the I/O recovery logic.
11257 */
11258 #if defined(SQLITE_TEST)
11259 SQLITE_API extern int sqlite3_io_error_hit;
11260 SQLITE_API extern int sqlite3_io_error_hardhit;
11261 SQLITE_API extern int sqlite3_io_error_pending;
11262 SQLITE_API extern int sqlite3_io_error_persist;
11263 SQLITE_API extern int sqlite3_io_error_benign;
11264 SQLITE_API extern int sqlite3_diskfull_pending;
11265 SQLITE_API extern int sqlite3_diskfull;
11266 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
11267 #define SimulateIOError(CODE) \
11268 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
11269 || sqlite3_io_error_pending-- == 1 ) \
11270 { local_ioerr(); CODE; }
11271 static void local_ioerr(){
11272 IOTRACE(("IOERR\n"));
11273 sqlite3_io_error_hit++;
11274 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
11275 }
11276 #define SimulateDiskfullError(CODE) \
11277 if( sqlite3_diskfull_pending ){ \
11278 if( sqlite3_diskfull_pending == 1 ){ \
11279 local_ioerr(); \
11280 sqlite3_diskfull = 1; \
11281 sqlite3_io_error_hit = 1; \
11282 CODE; \
11283 }else{ \
11284 sqlite3_diskfull_pending--; \
11285 } \
11286 }
11287 #else
11288 #define SimulateIOErrorBenign(X)
11289 #define SimulateIOError(A)
11290 #define SimulateDiskfullError(A)
11291 #endif /* defined(SQLITE_TEST) */
11292
11293 /*
11294 ** When testing, keep a count of the number of open files.
11295 */
11296 #if defined(SQLITE_TEST)
11297 SQLITE_API extern int sqlite3_open_file_count;
11298 #define OpenCounter(X) sqlite3_open_file_count+=(X)
11299 #else
11300 #define OpenCounter(X)
11301 #endif /* defined(SQLITE_TEST) */
11302
11303 #endif /* !defined(_OS_COMMON_H_) */
11304
11305 /************** End of os_common.h *******************************************/
11306 /************** Continuing where we left off in os_unix.c ********************/
11307
11308 /*
11309 ** Define various macros that are missing from some systems.
11310 */
11311 #ifndef O_LARGEFILE
11312 # define O_LARGEFILE 0
11313 #endif
11314 #ifdef SQLITE_DISABLE_LFS
11315 # undef O_LARGEFILE
11316 # define O_LARGEFILE 0
11317 #endif
11318 #ifndef O_NOFOLLOW
11319 # define O_NOFOLLOW 0
11320 #endif
11321 #ifndef O_BINARY
11322 # define O_BINARY 0
11323 #endif
11324
11325 /*
11326 ** The threadid macro resolves to the thread-id or to 0. Used for
11327 ** testing and debugging only.
11328 */
11329 #if SQLITE_THREADSAFE
11330 #define threadid pthread_self()
11331 #else
11332 #define threadid 0
11333 #endif
11334
11335 /*
11336 ** HAVE_MREMAP defaults to true on Linux and false everywhere else.
11337 */
11338 #if !defined(HAVE_MREMAP)
11339 # if defined(__linux__) && defined(_GNU_SOURCE)
11340 # define HAVE_MREMAP 1
11341 # else
11342 # define HAVE_MREMAP 0
11343 # endif
11344 #endif
11345
11346 /*
11347 ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
11348 ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
11349 */
11350 #ifdef __ANDROID__
11351 # define lseek lseek64
11352 #endif
11353
11354 /*
11355 ** Different Unix systems declare open() in different ways. Same use
11356 ** open(const char*,int,mode_t). Others use open(const char*,int,...).
11357 ** The difference is important when using a pointer to the function.
11358 **
11359 ** The safest way to deal with the problem is to always use this wrapper
11360 ** which always has the same well-defined interface.
11361 */
11362 static int posixOpen(const char *zFile, int flags, int mode){
11363 return open(zFile, flags, mode);
11364 }
11365
11366 /* Forward reference */
11367 static int openDirectory(const char*, int*);
11368 static int unixGetpagesize(void);
11369
11370 /*
11371 ** Many system calls are accessed through pointer-to-functions so that
11372 ** they may be overridden at runtime to facilitate fault injection during
11373 ** testing and sandboxing. The following array holds the names and pointers
11374 ** to all overrideable system calls.
11375 */
11376 static struct unix_syscall {
11377 const char *zName; /* Name of the system call */
11378 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
11379 sqlite3_syscall_ptr pDefault; /* Default value */
11380 } aSyscall[] = {
11381 { "open", (sqlite3_syscall_ptr)posixOpen, 0 },
11382 #define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
11383
11384 { "close", (sqlite3_syscall_ptr)close, 0 },
11385 #define osClose ((int(*)(int))aSyscall[1].pCurrent)
11386
11387 { "access", (sqlite3_syscall_ptr)access, 0 },
11388 #define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent)
11389
11390 { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 },
11391 #define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
11392
11393 { "stat", (sqlite3_syscall_ptr)stat, 0 },
11394 #define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
11395
11396 /*
11397 ** The DJGPP compiler environment looks mostly like Unix, but it
11398 ** lacks the fcntl() system call. So redefine fcntl() to be something
11399 ** that always succeeds. This means that locking does not occur under
11400 ** DJGPP. But it is DOS - what did you expect?
11401 */
11402 #ifdef __DJGPP__
11403 { "fstat", 0, 0 },
11404 #define osFstat(a,b,c) 0
11405 #else
11406 { "fstat", (sqlite3_syscall_ptr)fstat, 0 },
11407 #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
11408 #endif
11409
11410 { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 },
11411 #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
11412
11413 { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 },
11414 #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent)
11415
11416 { "read", (sqlite3_syscall_ptr)read, 0 },
11417 #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
11418
11419 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
11420 { "pread", (sqlite3_syscall_ptr)pread, 0 },
11421 #else
11422 { "pread", (sqlite3_syscall_ptr)0, 0 },
11423 #endif
11424 #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
11425
11426 #if defined(USE_PREAD64)
11427 { "pread64", (sqlite3_syscall_ptr)pread64, 0 },
11428 #else
11429 { "pread64", (sqlite3_syscall_ptr)0, 0 },
11430 #endif
11431 #define osPread64 ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent)
11432
11433 { "write", (sqlite3_syscall_ptr)write, 0 },
11434 #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
11435
11436 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
11437 { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 },
11438 #else
11439 { "pwrite", (sqlite3_syscall_ptr)0, 0 },
11440 #endif
11441 #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\
11442 aSyscall[12].pCurrent)
11443
11444 #if defined(USE_PREAD64)
11445 { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 },
11446 #else
11447 { "pwrite64", (sqlite3_syscall_ptr)0, 0 },
11448 #endif
11449 #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off64_t))\
11450 aSyscall[13].pCurrent)
11451
11452 { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 },
11453 #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent)
11454
11455 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
11456 { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 },
11457 #else
11458 { "fallocate", (sqlite3_syscall_ptr)0, 0 },
11459 #endif
11460 #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
11461
11462 { "unlink", (sqlite3_syscall_ptr)unlink, 0 },
11463 #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent)
11464
11465 { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
11466 #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
11467
11468 { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 },
11469 #define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
11470
11471 { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 },
11472 #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
11473
11474 #if defined(HAVE_FCHOWN)
11475 { "fchown", (sqlite3_syscall_ptr)fchown, 0 },
11476 #else
11477 { "fchown", (sqlite3_syscall_ptr)0, 0 },
11478 #endif
11479 #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
11480
11481 { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 },
11482 #define osGeteuid ((uid_t(*)(void))aSyscall[21].pCurrent)
11483
11484 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
11485 { "mmap", (sqlite3_syscall_ptr)mmap, 0 },
11486 #else
11487 { "mmap", (sqlite3_syscall_ptr)0, 0 },
11488 #endif
11489 #define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent)
11490
11491 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
11492 { "munmap", (sqlite3_syscall_ptr)munmap, 0 },
11493 #else
11494 { "munmap", (sqlite3_syscall_ptr)0, 0 },
11495 #endif
11496 #define osMunmap ((void*(*)(void*,size_t))aSyscall[23].pCurrent)
11497
11498 #if HAVE_MREMAP && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
11499 { "mremap", (sqlite3_syscall_ptr)mremap, 0 },
11500 #else
11501 { "mremap", (sqlite3_syscall_ptr)0, 0 },
11502 #endif
11503 #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent)
11504
11505 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
11506 { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 },
11507 #else
11508 { "getpagesize", (sqlite3_syscall_ptr)0, 0 },
11509 #endif
11510 #define osGetpagesize ((int(*)(void))aSyscall[25].pCurrent)
11511
11512 #if defined(HAVE_READLINK)
11513 { "readlink", (sqlite3_syscall_ptr)readlink, 0 },
11514 #else
11515 { "readlink", (sqlite3_syscall_ptr)0, 0 },
11516 #endif
11517 #define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)
11518
11519 #if defined(HAVE_LSTAT)
11520 { "lstat", (sqlite3_syscall_ptr)lstat, 0 },
11521 #else
11522 { "lstat", (sqlite3_syscall_ptr)0, 0 },
11523 #endif
11524 #define osLstat ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
11525
11526 }; /* End of the overrideable system calls */
11527
11528
11529 /*
11530 ** On some systems, calls to fchown() will trigger a message in a security
11531 ** log if they come from non-root processes. So avoid calling fchown() if
11532 ** we are not running as root.
11533 */
11534 static int robustFchown(int fd, uid_t uid, gid_t gid){
11535 #if defined(HAVE_FCHOWN)
11536 return osGeteuid() ? 0 : osFchown(fd,uid,gid);
11537 #else
11538 return 0;
11539 #endif
11540 }
11541
11542 /*
11543 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
11544 ** "unix" VFSes. Return SQLITE_OK opon successfully updating the
11545 ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
11546 ** system call named zName.
11547 */
11548 static int unixSetSystemCall(
11549 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
11550 const char *zName, /* Name of system call to override */
11551 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
11552 ){
11553 unsigned int i;
11554 int rc = SQLITE_NOTFOUND;
11555
11556 UNUSED_PARAMETER(pNotUsed);
11557 if( zName==0 ){
11558 /* If no zName is given, restore all system calls to their default
11559 ** settings and return NULL
11560 */
11561 rc = SQLITE_OK;
11562 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
11563 if( aSyscall[i].pDefault ){
11564 aSyscall[i].pCurrent = aSyscall[i].pDefault;
11565 }
11566 }
11567 }else{
11568 /* If zName is specified, operate on only the one system call
11569 ** specified.
11570 */
11571 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
11572 if( strcmp(zName, aSyscall[i].zName)==0 ){
11573 if( aSyscall[i].pDefault==0 ){
11574 aSyscall[i].pDefault = aSyscall[i].pCurrent;
11575 }
11576 rc = SQLITE_OK;
11577 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
11578 aSyscall[i].pCurrent = pNewFunc;
11579 break;
11580 }
11581 }
11582 }
11583 return rc;
11584 }
11585
11586 /*
11587 ** Return the value of a system call. Return NULL if zName is not a
11588 ** recognized system call name. NULL is also returned if the system call
11589 ** is currently undefined.
11590 */
11591 static sqlite3_syscall_ptr unixGetSystemCall(
11592 sqlite3_vfs *pNotUsed,
11593 const char *zName
11594 ){
11595 unsigned int i;
11596
11597 UNUSED_PARAMETER(pNotUsed);
11598 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
11599 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
11600 }
11601 return 0;
11602 }
11603
11604 /*
11605 ** Return the name of the first system call after zName. If zName==NULL
11606 ** then return the name of the first system call. Return NULL if zName
11607 ** is the last system call or if zName is not the name of a valid
11608 ** system call.
11609 */
11610 static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
11611 int i = -1;
11612
11613 UNUSED_PARAMETER(p);
11614 if( zName ){
11615 for(i=0; i<ArraySize(aSyscall)-1; i++){
11616 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
11617 }
11618 }
11619 for(i++; i<ArraySize(aSyscall); i++){
11620 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
11621 }
11622 return 0;
11623 }
11624
11625 /*
11626 ** Do not accept any file descriptor less than this value, in order to avoid
11627 ** opening database file using file descriptors that are commonly used for
11628 ** standard input, output, and error.
11629 */
11630 #ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR
11631 # define SQLITE_MINIMUM_FILE_DESCRIPTOR 3
11632 #endif
11633
11634 /*
11635 ** Invoke open(). Do so multiple times, until it either succeeds or
11636 ** fails for some reason other than EINTR.
11637 **
11638 ** If the file creation mode "m" is 0 then set it to the default for
11639 ** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
11640 ** 0644) as modified by the system umask. If m is not 0, then
11641 ** make the file creation mode be exactly m ignoring the umask.
11642 **
11643 ** The m parameter will be non-zero only when creating -wal, -journal,
11644 ** and -shm files. We want those files to have *exactly* the same
11645 ** permissions as their original database, unadulterated by the umask.
11646 ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
11647 ** transaction crashes and leaves behind hot journals, then any
11648 ** process that is able to write to the database will also be able to
11649 ** recover the hot journals.
11650 */
11651 static int robust_open(const char *z, int f, mode_t m){
11652 int fd;
11653 mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
11654 while(1){
11655 #if defined(O_CLOEXEC)
11656 fd = osOpen(z,f|O_CLOEXEC,m2);
11657 #else
11658 fd = osOpen(z,f,m2);
11659 #endif
11660 if( fd<0 ){
11661 if( errno==EINTR ) continue;
11662 break;
11663 }
11664 if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break;
11665 osClose(fd);
11666 sqlite3_log(SQLITE_WARNING,
11667 "attempt to open \"%s\" as file descriptor %d", z, fd);
11668 fd = -1;
11669 if( osOpen("/dev/null", f, m)<0 ) break;
11670 }
11671 if( fd>=0 ){
11672 if( m!=0 ){
11673 struct stat statbuf;
11674 if( osFstat(fd, &statbuf)==0
11675 && statbuf.st_size==0
11676 && (statbuf.st_mode&0777)!=m
11677 ){
11678 osFchmod(fd, m);
11679 }
11680 }
11681 #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
11682 osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
11683 #endif
11684 }
11685 return fd;
11686 }
11687
11688 /*
11689 ** Helper functions to obtain and relinquish the global mutex. The
11690 ** global mutex is used to protect the unixInodeInfo and
11691 ** vxworksFileId objects used by this file, all of which may be
11692 ** shared by multiple threads.
11693 **
11694 ** Function unixMutexHeld() is used to assert() that the global mutex
11695 ** is held when required. This function is only used as part of assert()
11696 ** statements. e.g.
11697 **
11698 ** unixEnterMutex()
11699 ** assert( unixMutexHeld() );
11700 ** unixEnterLeave()
11701 */
11702 static void unixEnterMutex(void){
11703 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
11704 }
11705 static void unixLeaveMutex(void){
11706 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
11707 }
11708 #ifdef SQLITE_DEBUG
11709 static int unixMutexHeld(void) {
11710 return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
11711 }
11712 #endif
11713
11714
11715 #ifdef SQLITE_HAVE_OS_TRACE
11716 /*
11717 ** Helper function for printing out trace information from debugging
11718 ** binaries. This returns the string representation of the supplied
11719 ** integer lock-type.
11720 */
11721 static const char *azFileLock(int eFileLock){
11722 switch( eFileLock ){
11723 case NO_LOCK: return "NONE";
11724 case SHARED_LOCK: return "SHARED";
11725 case RESERVED_LOCK: return "RESERVED";
11726 case PENDING_LOCK: return "PENDING";
11727 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
11728 }
11729 return "ERROR";
11730 }
11731 #endif
11732
11733 #ifdef SQLITE_LOCK_TRACE
11734 /*
11735 ** Print out information about all locking operations.
11736 **
11737 ** This routine is used for troubleshooting locks on multithreaded
11738 ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
11739 ** command-line option on the compiler. This code is normally
11740 ** turned off.
11741 */
11742 static int lockTrace(int fd, int op, struct flock *p){
11743 char *zOpName, *zType;
11744 int s;
11745 int savedErrno;
11746 if( op==F_GETLK ){
11747 zOpName = "GETLK";
11748 }else if( op==F_SETLK ){
11749 zOpName = "SETLK";
11750 }else{
11751 s = osFcntl(fd, op, p);
11752 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
11753 return s;
11754 }
11755 if( p->l_type==F_RDLCK ){
11756 zType = "RDLCK";
11757 }else if( p->l_type==F_WRLCK ){
11758 zType = "WRLCK";
11759 }else if( p->l_type==F_UNLCK ){
11760 zType = "UNLCK";
11761 }else{
11762 assert( 0 );
11763 }
11764 assert( p->l_whence==SEEK_SET );
11765 s = osFcntl(fd, op, p);
11766 savedErrno = errno;
11767 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
11768 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
11769 (int)p->l_pid, s);
11770 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
11771 struct flock l2;
11772 l2 = *p;
11773 osFcntl(fd, F_GETLK, &l2);
11774 if( l2.l_type==F_RDLCK ){
11775 zType = "RDLCK";
11776 }else if( l2.l_type==F_WRLCK ){
11777 zType = "WRLCK";
11778 }else if( l2.l_type==F_UNLCK ){
11779 zType = "UNLCK";
11780 }else{
11781 assert( 0 );
11782 }
11783 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
11784 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
11785 }
11786 errno = savedErrno;
11787 return s;
11788 }
11789 #undef osFcntl
11790 #define osFcntl lockTrace
11791 #endif /* SQLITE_LOCK_TRACE */
11792
11793 /*
11794 ** Retry ftruncate() calls that fail due to EINTR
11795 **
11796 ** All calls to ftruncate() within this file should be made through
11797 ** this wrapper. On the Android platform, bypassing the logic below
11798 ** could lead to a corrupt database.
11799 */
11800 static int robust_ftruncate(int h, sqlite3_int64 sz){
11801 int rc;
11802 #ifdef __ANDROID__
11803 /* On Android, ftruncate() always uses 32-bit offsets, even if
11804 ** _FILE_OFFSET_BITS=64 is defined. This means it is unsafe to attempt to
11805 ** truncate a file to any size larger than 2GiB. Silently ignore any
11806 ** such attempts. */
11807 if( sz>(sqlite3_int64)0x7FFFFFFF ){
11808 rc = SQLITE_OK;
11809 }else
11810 #endif
11811 do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
11812 return rc;
11813 }
11814
11815 /*
11816 ** This routine translates a standard POSIX errno code into something
11817 ** useful to the clients of the sqlite3 functions. Specifically, it is
11818 ** intended to translate a variety of "try again" errors into SQLITE_BUSY
11819 ** and a variety of "please close the file descriptor NOW" errors into
11820 ** SQLITE_IOERR
11821 **
11822 ** Errors during initialization of locks, or file system support for locks,
11823 ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
11824 */
11825 static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
11826 assert( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
11827 (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
11828 (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
11829 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) );
11830 switch (posixError) {
11831 case EACCES:
11832 case EAGAIN:
11833 case ETIMEDOUT:
11834 case EBUSY:
11835 case EINTR:
11836 case ENOLCK:
11837 /* random NFS retry error, unless during file system support
11838 * introspection, in which it actually means what it says */
11839 return SQLITE_BUSY;
11840
11841 case EPERM:
11842 return SQLITE_PERM;
11843
11844 default:
11845 return sqliteIOErr;
11846 }
11847 }
11848
11849
11850 /******************************************************************************
11851 ****************** Begin Unique File ID Utility Used By VxWorks ***************
11852 **
11853 ** On most versions of unix, we can get a unique ID for a file by concatenating
11854 ** the device number and the inode number. But this does not work on VxWorks.
11855 ** On VxWorks, a unique file id must be based on the canonical filename.
11856 **
11857 ** A pointer to an instance of the following structure can be used as a
11858 ** unique file ID in VxWorks. Each instance of this structure contains
11859 ** a copy of the canonical filename. There is also a reference count.
11860 ** The structure is reclaimed when the number of pointers to it drops to
11861 ** zero.
11862 **
11863 ** There are never very many files open at one time and lookups are not
11864 ** a performance-critical path, so it is sufficient to put these
11865 ** structures on a linked list.
11866 */
11867 struct vxworksFileId {
11868 struct vxworksFileId *pNext; /* Next in a list of them all */
11869 int nRef; /* Number of references to this one */
11870 int nName; /* Length of the zCanonicalName[] string */
11871 char *zCanonicalName; /* Canonical filename */
11872 };
11873
11874 #if OS_VXWORKS
11875 /*
11876 ** All unique filenames are held on a linked list headed by this
11877 ** variable:
11878 */
11879 static struct vxworksFileId *vxworksFileList = 0;
11880
11881 /*
11882 ** Simplify a filename into its canonical form
11883 ** by making the following changes:
11884 **
11885 ** * removing any trailing and duplicate /
11886 ** * convert /./ into just /
11887 ** * convert /A/../ where A is any simple name into just /
11888 **
11889 ** Changes are made in-place. Return the new name length.
11890 **
11891 ** The original filename is in z[0..n-1]. Return the number of
11892 ** characters in the simplified name.
11893 */
11894 static int vxworksSimplifyName(char *z, int n){
11895 int i, j;
11896 while( n>1 && z[n-1]=='/' ){ n--; }
11897 for(i=j=0; i<n; i++){
11898 if( z[i]=='/' ){
11899 if( z[i+1]=='/' ) continue;
11900 if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
11901 i += 1;
11902 continue;
11903 }
11904 if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
11905 while( j>0 && z[j-1]!='/' ){ j--; }
11906 if( j>0 ){ j--; }
11907 i += 2;
11908 continue;
11909 }
11910 }
11911 z[j++] = z[i];
11912 }
11913 z[j] = 0;
11914 return j;
11915 }
11916
11917 /*
11918 ** Find a unique file ID for the given absolute pathname. Return
11919 ** a pointer to the vxworksFileId object. This pointer is the unique
11920 ** file ID.
11921 **
11922 ** The nRef field of the vxworksFileId object is incremented before
11923 ** the object is returned. A new vxworksFileId object is created
11924 ** and added to the global list if necessary.
11925 **
11926 ** If a memory allocation error occurs, return NULL.
11927 */
11928 static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
11929 struct vxworksFileId *pNew; /* search key and new file ID */
11930 struct vxworksFileId *pCandidate; /* For looping over existing file IDs */
11931 int n; /* Length of zAbsoluteName string */
11932
11933 assert( zAbsoluteName[0]=='/' );
11934 n = (int)strlen(zAbsoluteName);
11935 pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) );
11936 if( pNew==0 ) return 0;
11937 pNew->zCanonicalName = (char*)&pNew[1];
11938 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
11939 n = vxworksSimplifyName(pNew->zCanonicalName, n);
11940
11941 /* Search for an existing entry that matching the canonical name.
11942 ** If found, increment the reference count and return a pointer to
11943 ** the existing file ID.
11944 */
11945 unixEnterMutex();
11946 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
11947 if( pCandidate->nName==n
11948 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
11949 ){
11950 sqlite3_free(pNew);
11951 pCandidate->nRef++;
11952 unixLeaveMutex();
11953 return pCandidate;
11954 }
11955 }
11956
11957 /* No match was found. We will make a new file ID */
11958 pNew->nRef = 1;
11959 pNew->nName = n;
11960 pNew->pNext = vxworksFileList;
11961 vxworksFileList = pNew;
11962 unixLeaveMutex();
11963 return pNew;
11964 }
11965
11966 /*
11967 ** Decrement the reference count on a vxworksFileId object. Free
11968 ** the object when the reference count reaches zero.
11969 */
11970 static void vxworksReleaseFileId(struct vxworksFileId *pId){
11971 unixEnterMutex();
11972 assert( pId->nRef>0 );
11973 pId->nRef--;
11974 if( pId->nRef==0 ){
11975 struct vxworksFileId **pp;
11976 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
11977 assert( *pp==pId );
11978 *pp = pId->pNext;
11979 sqlite3_free(pId);
11980 }
11981 unixLeaveMutex();
11982 }
11983 #endif /* OS_VXWORKS */
11984 /*************** End of Unique File ID Utility Used By VxWorks ****************
11985 ******************************************************************************/
11986
11987
11988 /******************************************************************************
11989 *************************** Posix Advisory Locking ****************************
11990 **
11991 ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996)
11992 ** section 6.5.2.2 lines 483 through 490 specify that when a process
11993 ** sets or clears a lock, that operation overrides any prior locks set
11994 ** by the same process. It does not explicitly say so, but this implies
11995 ** that it overrides locks set by the same process using a different
11996 ** file descriptor. Consider this test case:
11997 **
11998 ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
11999 ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
12000 **
12001 ** Suppose ./file1 and ./file2 are really the same file (because
12002 ** one is a hard or symbolic link to the other) then if you set
12003 ** an exclusive lock on fd1, then try to get an exclusive lock
12004 ** on fd2, it works. I would have expected the second lock to
12005 ** fail since there was already a lock on the file due to fd1.
12006 ** But not so. Since both locks came from the same process, the
12007 ** second overrides the first, even though they were on different
12008 ** file descriptors opened on different file names.
12009 **
12010 ** This means that we cannot use POSIX locks to synchronize file access
12011 ** among competing threads of the same process. POSIX locks will work fine
12012 ** to synchronize access for threads in separate processes, but not
12013 ** threads within the same process.
12014 **
12015 ** To work around the problem, SQLite has to manage file locks internally
12016 ** on its own. Whenever a new database is opened, we have to find the
12017 ** specific inode of the database file (the inode is determined by the
12018 ** st_dev and st_ino fields of the stat structure that fstat() fills in)
12019 ** and check for locks already existing on that inode. When locks are
12020 ** created or removed, we have to look at our own internal record of the
12021 ** locks to see if another thread has previously set a lock on that same
12022 ** inode.
12023 **
12024 ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
12025 ** For VxWorks, we have to use the alternative unique ID system based on
12026 ** canonical filename and implemented in the previous division.)
12027 **
12028 ** The sqlite3_file structure for POSIX is no longer just an integer file
12029 ** descriptor. It is now a structure that holds the integer file
12030 ** descriptor and a pointer to a structure that describes the internal
12031 ** locks on the corresponding inode. There is one locking structure
12032 ** per inode, so if the same inode is opened twice, both unixFile structures
12033 ** point to the same locking structure. The locking structure keeps
12034 ** a reference count (so we will know when to delete it) and a "cnt"
12035 ** field that tells us its internal lock status. cnt==0 means the
12036 ** file is unlocked. cnt==-1 means the file has an exclusive lock.
12037 ** cnt>0 means there are cnt shared locks on the file.
12038 **
12039 ** Any attempt to lock or unlock a file first checks the locking
12040 ** structure. The fcntl() system call is only invoked to set a
12041 ** POSIX lock if the internal lock structure transitions between
12042 ** a locked and an unlocked state.
12043 **
12044 ** But wait: there are yet more problems with POSIX advisory locks.
12045 **
12046 ** If you close a file descriptor that points to a file that has locks,
12047 ** all locks on that file that are owned by the current process are
12048 ** released. To work around this problem, each unixInodeInfo object
12049 ** maintains a count of the number of pending locks on tha inode.
12050 ** When an attempt is made to close an unixFile, if there are
12051 ** other unixFile open on the same inode that are holding locks, the call
12052 ** to close() the file descriptor is deferred until all of the locks clear.
12053 ** The unixInodeInfo structure keeps a list of file descriptors that need to
12054 ** be closed and that list is walked (and cleared) when the last lock
12055 ** clears.
12056 **
12057 ** Yet another problem: LinuxThreads do not play well with posix locks.
12058 **
12059 ** Many older versions of linux use the LinuxThreads library which is
12060 ** not posix compliant. Under LinuxThreads, a lock created by thread
12061 ** A cannot be modified or overridden by a different thread B.
12062 ** Only thread A can modify the lock. Locking behavior is correct
12063 ** if the appliation uses the newer Native Posix Thread Library (NPTL)
12064 ** on linux - with NPTL a lock created by thread A can override locks
12065 ** in thread B. But there is no way to know at compile-time which
12066 ** threading library is being used. So there is no way to know at
12067 ** compile-time whether or not thread A can override locks on thread B.
12068 ** One has to do a run-time check to discover the behavior of the
12069 ** current process.
12070 **
12071 ** SQLite used to support LinuxThreads. But support for LinuxThreads
12072 ** was dropped beginning with version 3.7.0. SQLite will still work with
12073 ** LinuxThreads provided that (1) there is no more than one connection
12074 ** per database file in the same process and (2) database connections
12075 ** do not move across threads.
12076 */
12077
12078 /*
12079 ** An instance of the following structure serves as the key used
12080 ** to locate a particular unixInodeInfo object.
12081 */
12082 struct unixFileId {
12083 dev_t dev; /* Device number */
12084 #if OS_VXWORKS
12085 struct vxworksFileId *pId; /* Unique file ID for vxworks. */
12086 #else
12087 /* We are told that some versions of Android contain a bug that
12088 ** sizes ino_t at only 32-bits instead of 64-bits. (See
12089 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
12090 ** To work around this, always allocate 64-bits for the inode number.
12091 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
12092 ** but that should not be a big deal. */
12093 /* WAS: ino_t ino; */
12094 u64 ino; /* Inode number */
12095 #endif
12096 };
12097
12098 /*
12099 ** An instance of the following structure is allocated for each open
12100 ** inode. Or, on LinuxThreads, there is one of these structures for
12101 ** each inode opened by each thread.
12102 **
12103 ** A single inode can have multiple file descriptors, so each unixFile
12104 ** structure contains a pointer to an instance of this object and this
12105 ** object keeps a count of the number of unixFile pointing to it.
12106 */
12107 struct unixInodeInfo {
12108 struct unixFileId fileId; /* The lookup key */
12109 int nShared; /* Number of SHARED locks held */
12110 unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
12111 unsigned char bProcessLock; /* An exclusive process lock is held */
12112 int nRef; /* Number of pointers to this structure */
12113 unixShmNode *pShmNode; /* Shared memory associated with this inode */
12114 int nLock; /* Number of outstanding file locks */
12115 UnixUnusedFd *pUnused; /* Unused file descriptors to close */
12116 unixInodeInfo *pNext; /* List of all unixInodeInfo objects */
12117 unixInodeInfo *pPrev; /* .... doubly linked */
12118 #if SQLITE_ENABLE_LOCKING_STYLE
12119 unsigned long long sharedByte; /* for AFP simulated shared lock */
12120 #endif
12121 #if OS_VXWORKS
12122 sem_t *pSem; /* Named POSIX semaphore */
12123 char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */
12124 #endif
12125 };
12126
12127 /*
12128 ** A lists of all unixInodeInfo objects.
12129 */
12130 static unixInodeInfo *inodeList = 0;
12131
12132 /*
12133 **
12134 ** This function - unixLogErrorAtLine(), is only ever called via the macro
12135 ** unixLogError().
12136 **
12137 ** It is invoked after an error occurs in an OS function and errno has been
12138 ** set. It logs a message using sqlite3_log() containing the current value of
12139 ** errno and, if possible, the human-readable equivalent from strerror() or
12140 ** strerror_r().
12141 **
12142 ** The first argument passed to the macro should be the error code that
12143 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
12144 ** The two subsequent arguments should be the name of the OS function that
12145 ** failed (e.g. "unlink", "open") and the associated file-system path,
12146 ** if any.
12147 */
12148 #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__)
12149 static int unixLogErrorAtLine(
12150 int errcode, /* SQLite error code */
12151 const char *zFunc, /* Name of OS function that failed */
12152 const char *zPath, /* File path associated with error */
12153 int iLine /* Source line number where error occurred */
12154 ){
12155 char *zErr; /* Message from strerror() or equivalent */
12156 int iErrno = errno; /* Saved syscall error number */
12157
12158 /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
12159 ** the strerror() function to obtain the human-readable error message
12160 ** equivalent to errno. Otherwise, use strerror_r().
12161 */
12162 #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
12163 char aErr[80];
12164 memset(aErr, 0, sizeof(aErr));
12165 zErr = aErr;
12166
12167 /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
12168 ** assume that the system provides the GNU version of strerror_r() that
12169 ** returns a pointer to a buffer containing the error message. That pointer
12170 ** may point to aErr[], or it may point to some static storage somewhere.
12171 ** Otherwise, assume that the system provides the POSIX version of
12172 ** strerror_r(), which always writes an error message into aErr[].
12173 **
12174 ** If the code incorrectly assumes that it is the POSIX version that is
12175 ** available, the error message will often be an empty string. Not a
12176 ** huge problem. Incorrectly concluding that the GNU version is available
12177 ** could lead to a segfault though.
12178 */
12179 #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
12180 zErr =
12181 # endif
12182 strerror_r(iErrno, aErr, sizeof(aErr)-1);
12183
12184 #elif SQLITE_THREADSAFE
12185 /* This is a threadsafe build, but strerror_r() is not available. */
12186 zErr = "";
12187 #else
12188 /* Non-threadsafe build, use strerror(). */
12189 zErr = strerror(iErrno);
12190 #endif
12191
12192 if( zPath==0 ) zPath = "";
12193 sqlite3_log(errcode,
12194 "os_unix.c:%d: (%d) %s(%s) - %s",
12195 iLine, iErrno, zFunc, zPath, zErr
12196 );
12197
12198 return errcode;
12199 }
12200
12201 /*
12202 ** Close a file descriptor.
12203 **
12204 ** We assume that close() almost always works, since it is only in a
12205 ** very sick application or on a very sick platform that it might fail.
12206 ** If it does fail, simply leak the file descriptor, but do log the
12207 ** error.
12208 **
12209 ** Note that it is not safe to retry close() after EINTR since the
12210 ** file descriptor might have already been reused by another thread.
12211 ** So we don't even try to recover from an EINTR. Just log the error
12212 ** and move on.
12213 */
12214 static void robust_close(unixFile *pFile, int h, int lineno){
12215 if( osClose(h) ){
12216 unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
12217 pFile ? pFile->zPath : 0, lineno);
12218 }
12219 }
12220
12221 /*
12222 ** Set the pFile->lastErrno. Do this in a subroutine as that provides
12223 ** a convenient place to set a breakpoint.
12224 */
12225 static void storeLastErrno(unixFile *pFile, int error){
12226 pFile->lastErrno = error;
12227 }
12228
12229 /*
12230 ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
12231 */
12232 static void closePendingFds(unixFile *pFile){
12233 unixInodeInfo *pInode = pFile->pInode;
12234 UnixUnusedFd *p;
12235 UnixUnusedFd *pNext;
12236 for(p=pInode->pUnused; p; p=pNext){
12237 pNext = p->pNext;
12238 robust_close(pFile, p->fd, __LINE__);
12239 sqlite3_free(p);
12240 }
12241 pInode->pUnused = 0;
12242 }
12243
12244 /*
12245 ** Release a unixInodeInfo structure previously allocated by findInodeInfo().
12246 **
12247 ** The mutex entered using the unixEnterMutex() function must be held
12248 ** when this function is called.
12249 */
12250 static void releaseInodeInfo(unixFile *pFile){
12251 unixInodeInfo *pInode = pFile->pInode;
12252 assert( unixMutexHeld() );
12253 if( ALWAYS(pInode) ){
12254 pInode->nRef--;
12255 if( pInode->nRef==0 ){
12256 assert( pInode->pShmNode==0 );
12257 closePendingFds(pFile);
12258 if( pInode->pPrev ){
12259 assert( pInode->pPrev->pNext==pInode );
12260 pInode->pPrev->pNext = pInode->pNext;
12261 }else{
12262 assert( inodeList==pInode );
12263 inodeList = pInode->pNext;
12264 }
12265 if( pInode->pNext ){
12266 assert( pInode->pNext->pPrev==pInode );
12267 pInode->pNext->pPrev = pInode->pPrev;
12268 }
12269 sqlite3_free(pInode);
12270 }
12271 }
12272 }
12273
12274 /*
12275 ** Given a file descriptor, locate the unixInodeInfo object that
12276 ** describes that file descriptor. Create a new one if necessary. The
12277 ** return value might be uninitialized if an error occurs.
12278 **
12279 ** The mutex entered using the unixEnterMutex() function must be held
12280 ** when this function is called.
12281 **
12282 ** Return an appropriate error code.
12283 */
12284 static int findInodeInfo(
12285 unixFile *pFile, /* Unix file with file desc used in the key */
12286 unixInodeInfo **ppInode /* Return the unixInodeInfo object here */
12287 ){
12288 int rc; /* System call return code */
12289 int fd; /* The file descriptor for pFile */
12290 struct unixFileId fileId; /* Lookup key for the unixInodeInfo */
12291 struct stat statbuf; /* Low-level file information */
12292 unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */
12293
12294 assert( unixMutexHeld() );
12295
12296 /* Get low-level information about the file that we can used to
12297 ** create a unique name for the file.
12298 */
12299 fd = pFile->h;
12300 rc = osFstat(fd, &statbuf);
12301 if( rc!=0 ){
12302 storeLastErrno(pFile, errno);
12303 #if defined(EOVERFLOW) && defined(SQLITE_DISABLE_LFS)
12304 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
12305 #endif
12306 return SQLITE_IOERR;
12307 }
12308
12309 #ifdef __APPLE__
12310 /* On OS X on an msdos filesystem, the inode number is reported
12311 ** incorrectly for zero-size files. See ticket #3260. To work
12312 ** around this problem (we consider it a bug in OS X, not SQLite)
12313 ** we always increase the file size to 1 by writing a single byte
12314 ** prior to accessing the inode number. The one byte written is
12315 ** an ASCII 'S' character which also happens to be the first byte
12316 ** in the header of every SQLite database. In this way, if there
12317 ** is a race condition such that another thread has already populated
12318 ** the first page of the database, no damage is done.
12319 */
12320 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
12321 do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
12322 if( rc!=1 ){
12323 storeLastErrno(pFile, errno);
12324 return SQLITE_IOERR;
12325 }
12326 rc = osFstat(fd, &statbuf);
12327 if( rc!=0 ){
12328 storeLastErrno(pFile, errno);
12329 return SQLITE_IOERR;
12330 }
12331 }
12332 #endif
12333
12334 memset(&fileId, 0, sizeof(fileId));
12335 fileId.dev = statbuf.st_dev;
12336 #if OS_VXWORKS
12337 fileId.pId = pFile->pId;
12338 #else
12339 fileId.ino = (u64)statbuf.st_ino;
12340 #endif
12341 pInode = inodeList;
12342 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
12343 pInode = pInode->pNext;
12344 }
12345 if( pInode==0 ){
12346 pInode = sqlite3_malloc64( sizeof(*pInode) );
12347 if( pInode==0 ){
12348 return SQLITE_NOMEM_BKPT;
12349 }
12350 memset(pInode, 0, sizeof(*pInode));
12351 memcpy(&pInode->fileId, &fileId, sizeof(fileId));
12352 pInode->nRef = 1;
12353 pInode->pNext = inodeList;
12354 pInode->pPrev = 0;
12355 if( inodeList ) inodeList->pPrev = pInode;
12356 inodeList = pInode;
12357 }else{
12358 pInode->nRef++;
12359 }
12360 *ppInode = pInode;
12361 return SQLITE_OK;
12362 }
12363
12364 /*
12365 ** Return TRUE if pFile has been renamed or unlinked since it was first opened.
12366 */
12367 static int fileHasMoved(unixFile *pFile){
12368 #if OS_VXWORKS
12369 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
12370 #else
12371 struct stat buf;
12372
12373 /* TODO(shess): This check doesn't work when the Chromium's WebDB code is
12374 ** running in the sandbox.
12375 */
12376 return 0;
12377
12378 return pFile->pInode!=0 &&
12379 (osStat(pFile->zPath, &buf)!=0
12380 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
12381 #endif
12382 }
12383
12384
12385 /*
12386 ** Check a unixFile that is a database. Verify the following:
12387 **
12388 ** (1) There is exactly one hard link on the file
12389 ** (2) The file is not a symbolic link
12390 ** (3) The file has not been renamed or unlinked
12391 **
12392 ** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
12393 */
12394 static void verifyDbFile(unixFile *pFile){
12395 struct stat buf;
12396 int rc;
12397
12398 /* These verifications occurs for the main database only */
12399 if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
12400
12401 rc = osFstat(pFile->h, &buf);
12402 if( rc!=0 ){
12403 sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
12404 return;
12405 }
12406 if( buf.st_nlink==0 ){
12407 sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
12408 return;
12409 }
12410 if( buf.st_nlink>1 ){
12411 sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
12412 return;
12413 }
12414 if( fileHasMoved(pFile) ){
12415 sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
12416 return;
12417 }
12418 }
12419
12420
12421 /*
12422 ** This routine checks if there is a RESERVED lock held on the specified
12423 ** file by this or any other process. If such a lock is held, set *pResOut
12424 ** to a non-zero value otherwise *pResOut is set to zero. The return value
12425 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
12426 */
12427 static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
12428 int rc = SQLITE_OK;
12429 int reserved = 0;
12430 unixFile *pFile = (unixFile*)id;
12431
12432 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
12433
12434 assert( pFile );
12435 assert( pFile->eFileLock<=SHARED_LOCK );
12436 unixEnterMutex(); /* Because pFile->pInode is shared across threads */
12437
12438 /* Check if a thread in this process holds such a lock */
12439 if( pFile->pInode->eFileLock>SHARED_LOCK ){
12440 reserved = 1;
12441 }
12442
12443 /* Otherwise see if some other process holds it.
12444 */
12445 #ifndef __DJGPP__
12446 if( !reserved && !pFile->pInode->bProcessLock ){
12447 struct flock lock;
12448 lock.l_whence = SEEK_SET;
12449 lock.l_start = RESERVED_BYTE;
12450 lock.l_len = 1;
12451 lock.l_type = F_WRLCK;
12452 if( osFcntl(pFile->h, F_GETLK, &lock) ){
12453 rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
12454 storeLastErrno(pFile, errno);
12455 } else if( lock.l_type!=F_UNLCK ){
12456 reserved = 1;
12457 }
12458 }
12459 #endif
12460
12461 unixLeaveMutex();
12462 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
12463
12464 *pResOut = reserved;
12465 return rc;
12466 }
12467
12468 /*
12469 ** Attempt to set a system-lock on the file pFile. The lock is
12470 ** described by pLock.
12471 **
12472 ** If the pFile was opened read/write from unix-excl, then the only lock
12473 ** ever obtained is an exclusive lock, and it is obtained exactly once
12474 ** the first time any lock is attempted. All subsequent system locking
12475 ** operations become no-ops. Locking operations still happen internally,
12476 ** in order to coordinate access between separate database connections
12477 ** within this process, but all of that is handled in memory and the
12478 ** operating system does not participate.
12479 **
12480 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
12481 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
12482 ** and is read-only.
12483 **
12484 ** Zero is returned if the call completes successfully, or -1 if a call
12485 ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
12486 */
12487 static int unixFileLock(unixFile *pFile, struct flock *pLock){
12488 int rc;
12489 unixInodeInfo *pInode = pFile->pInode;
12490 assert( unixMutexHeld() );
12491 assert( pInode!=0 );
12492 if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){
12493 if( pInode->bProcessLock==0 ){
12494 struct flock lock;
12495 assert( pInode->nLock==0 );
12496 lock.l_whence = SEEK_SET;
12497 lock.l_start = SHARED_FIRST;
12498 lock.l_len = SHARED_SIZE;
12499 lock.l_type = F_WRLCK;
12500 rc = osFcntl(pFile->h, F_SETLK, &lock);
12501 if( rc<0 ) return rc;
12502 pInode->bProcessLock = 1;
12503 pInode->nLock++;
12504 }else{
12505 rc = 0;
12506 }
12507 }else{
12508 rc = osFcntl(pFile->h, F_SETLK, pLock);
12509 }
12510 return rc;
12511 }
12512
12513 /*
12514 ** Lock the file with the lock specified by parameter eFileLock - one
12515 ** of the following:
12516 **
12517 ** (1) SHARED_LOCK
12518 ** (2) RESERVED_LOCK
12519 ** (3) PENDING_LOCK
12520 ** (4) EXCLUSIVE_LOCK
12521 **
12522 ** Sometimes when requesting one lock state, additional lock states
12523 ** are inserted in between. The locking might fail on one of the later
12524 ** transitions leaving the lock state different from what it started but
12525 ** still short of its goal. The following chart shows the allowed
12526 ** transitions and the inserted intermediate states:
12527 **
12528 ** UNLOCKED -> SHARED
12529 ** SHARED -> RESERVED
12530 ** SHARED -> (PENDING) -> EXCLUSIVE
12531 ** RESERVED -> (PENDING) -> EXCLUSIVE
12532 ** PENDING -> EXCLUSIVE
12533 **
12534 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
12535 ** routine to lower a locking level.
12536 */
12537 static int unixLock(sqlite3_file *id, int eFileLock){
12538 /* The following describes the implementation of the various locks and
12539 ** lock transitions in terms of the POSIX advisory shared and exclusive
12540 ** lock primitives (called read-locks and write-locks below, to avoid
12541 ** confusion with SQLite lock names). The algorithms are complicated
12542 ** slightly in order to be compatible with Windows95 systems simultaneously
12543 ** accessing the same database file, in case that is ever required.
12544 **
12545 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
12546 ** byte', each single bytes at well known offsets, and the 'shared byte
12547 ** range', a range of 510 bytes at a well known offset.
12548 **
12549 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
12550 ** byte'. If this is successful, 'shared byte range' is read-locked
12551 ** and the lock on the 'pending byte' released. (Legacy note: When
12552 ** SQLite was first developed, Windows95 systems were still very common,
12553 ** and Widnows95 lacks a shared-lock capability. So on Windows95, a
12554 ** single randomly selected by from the 'shared byte range' is locked.
12555 ** Windows95 is now pretty much extinct, but this work-around for the
12556 ** lack of shared-locks on Windows95 lives on, for backwards
12557 ** compatibility.)
12558 **
12559 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
12560 ** A RESERVED lock is implemented by grabbing a write-lock on the
12561 ** 'reserved byte'.
12562 **
12563 ** A process may only obtain a PENDING lock after it has obtained a
12564 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
12565 ** on the 'pending byte'. This ensures that no new SHARED locks can be
12566 ** obtained, but existing SHARED locks are allowed to persist. A process
12567 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
12568 ** This property is used by the algorithm for rolling back a journal file
12569 ** after a crash.
12570 **
12571 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
12572 ** implemented by obtaining a write-lock on the entire 'shared byte
12573 ** range'. Since all other locks require a read-lock on one of the bytes
12574 ** within this range, this ensures that no other locks are held on the
12575 ** database.
12576 */
12577 int rc = SQLITE_OK;
12578 unixFile *pFile = (unixFile*)id;
12579 unixInodeInfo *pInode;
12580 struct flock lock;
12581 int tErrno = 0;
12582
12583 assert( pFile );
12584 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
12585 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
12586 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
12587 osGetpid(0)));
12588
12589 /* If there is already a lock of this type or more restrictive on the
12590 ** unixFile, do nothing. Don't use the end_lock: exit path, as
12591 ** unixEnterMutex() hasn't been called yet.
12592 */
12593 if( pFile->eFileLock>=eFileLock ){
12594 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
12595 azFileLock(eFileLock)));
12596 return SQLITE_OK;
12597 }
12598
12599 /* Make sure the locking sequence is correct.
12600 ** (1) We never move from unlocked to anything higher than shared lock.
12601 ** (2) SQLite never explicitly requests a pendig lock.
12602 ** (3) A shared lock is always held when a reserve lock is requested.
12603 */
12604 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
12605 assert( eFileLock!=PENDING_LOCK );
12606 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
12607
12608 /* This mutex is needed because pFile->pInode is shared across threads
12609 */
12610 unixEnterMutex();
12611 pInode = pFile->pInode;
12612
12613 /* If some thread using this PID has a lock via a different unixFile*
12614 ** handle that precludes the requested lock, return BUSY.
12615 */
12616 if( (pFile->eFileLock!=pInode->eFileLock &&
12617 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
12618 ){
12619 rc = SQLITE_BUSY;
12620 goto end_lock;
12621 }
12622
12623 /* If a SHARED lock is requested, and some thread using this PID already
12624 ** has a SHARED or RESERVED lock, then increment reference counts and
12625 ** return SQLITE_OK.
12626 */
12627 if( eFileLock==SHARED_LOCK &&
12628 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
12629 assert( eFileLock==SHARED_LOCK );
12630 assert( pFile->eFileLock==0 );
12631 assert( pInode->nShared>0 );
12632 pFile->eFileLock = SHARED_LOCK;
12633 pInode->nShared++;
12634 pInode->nLock++;
12635 goto end_lock;
12636 }
12637
12638
12639 /* A PENDING lock is needed before acquiring a SHARED lock and before
12640 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
12641 ** be released.
12642 */
12643 lock.l_len = 1L;
12644 lock.l_whence = SEEK_SET;
12645 if( eFileLock==SHARED_LOCK
12646 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
12647 ){
12648 lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
12649 lock.l_start = PENDING_BYTE;
12650 if( unixFileLock(pFile, &lock) ){
12651 tErrno = errno;
12652 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
12653 if( rc!=SQLITE_BUSY ){
12654 storeLastErrno(pFile, tErrno);
12655 }
12656 goto end_lock;
12657 }
12658 }
12659
12660
12661 /* If control gets to this point, then actually go ahead and make
12662 ** operating system calls for the specified lock.
12663 */
12664 if( eFileLock==SHARED_LOCK ){
12665 assert( pInode->nShared==0 );
12666 assert( pInode->eFileLock==0 );
12667 assert( rc==SQLITE_OK );
12668
12669 /* Now get the read-lock */
12670 lock.l_start = SHARED_FIRST;
12671 lock.l_len = SHARED_SIZE;
12672 if( unixFileLock(pFile, &lock) ){
12673 tErrno = errno;
12674 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
12675 }
12676
12677 /* Drop the temporary PENDING lock */
12678 lock.l_start = PENDING_BYTE;
12679 lock.l_len = 1L;
12680 lock.l_type = F_UNLCK;
12681 if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
12682 /* This could happen with a network mount */
12683 tErrno = errno;
12684 rc = SQLITE_IOERR_UNLOCK;
12685 }
12686
12687 if( rc ){
12688 if( rc!=SQLITE_BUSY ){
12689 storeLastErrno(pFile, tErrno);
12690 }
12691 goto end_lock;
12692 }else{
12693 pFile->eFileLock = SHARED_LOCK;
12694 pInode->nLock++;
12695 pInode->nShared = 1;
12696 }
12697 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
12698 /* We are trying for an exclusive lock but another thread in this
12699 ** same process is still holding a shared lock. */
12700 rc = SQLITE_BUSY;
12701 }else{
12702 /* The request was for a RESERVED or EXCLUSIVE lock. It is
12703 ** assumed that there is a SHARED or greater lock on the file
12704 ** already.
12705 */
12706 assert( 0!=pFile->eFileLock );
12707 lock.l_type = F_WRLCK;
12708
12709 assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
12710 if( eFileLock==RESERVED_LOCK ){
12711 lock.l_start = RESERVED_BYTE;
12712 lock.l_len = 1L;
12713 }else{
12714 lock.l_start = SHARED_FIRST;
12715 lock.l_len = SHARED_SIZE;
12716 }
12717
12718 if( unixFileLock(pFile, &lock) ){
12719 tErrno = errno;
12720 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
12721 if( rc!=SQLITE_BUSY ){
12722 storeLastErrno(pFile, tErrno);
12723 }
12724 }
12725 }
12726
12727
12728 #ifdef SQLITE_DEBUG
12729 /* Set up the transaction-counter change checking flags when
12730 ** transitioning from a SHARED to a RESERVED lock. The change
12731 ** from SHARED to RESERVED marks the beginning of a normal
12732 ** write operation (not a hot journal rollback).
12733 */
12734 if( rc==SQLITE_OK
12735 && pFile->eFileLock<=SHARED_LOCK
12736 && eFileLock==RESERVED_LOCK
12737 ){
12738 pFile->transCntrChng = 0;
12739 pFile->dbUpdate = 0;
12740 pFile->inNormalWrite = 1;
12741 }
12742 #endif
12743
12744
12745 if( rc==SQLITE_OK ){
12746 pFile->eFileLock = eFileLock;
12747 pInode->eFileLock = eFileLock;
12748 }else if( eFileLock==EXCLUSIVE_LOCK ){
12749 pFile->eFileLock = PENDING_LOCK;
12750 pInode->eFileLock = PENDING_LOCK;
12751 }
12752
12753 end_lock:
12754 unixLeaveMutex();
12755 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
12756 rc==SQLITE_OK ? "ok" : "failed"));
12757 return rc;
12758 }
12759
12760 /*
12761 ** Add the file descriptor used by file handle pFile to the corresponding
12762 ** pUnused list.
12763 */
12764 static void setPendingFd(unixFile *pFile){
12765 unixInodeInfo *pInode = pFile->pInode;
12766 UnixUnusedFd *p = pFile->pUnused;
12767 p->pNext = pInode->pUnused;
12768 pInode->pUnused = p;
12769 pFile->h = -1;
12770 pFile->pUnused = 0;
12771 }
12772
12773 /*
12774 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
12775 ** must be either NO_LOCK or SHARED_LOCK.
12776 **
12777 ** If the locking level of the file descriptor is already at or below
12778 ** the requested locking level, this routine is a no-op.
12779 **
12780 ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
12781 ** the byte range is divided into 2 parts and the first part is unlocked then
12782 ** set to a read lock, then the other part is simply unlocked. This works
12783 ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
12784 ** remove the write lock on a region when a read lock is set.
12785 */
12786 static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
12787 unixFile *pFile = (unixFile*)id;
12788 unixInodeInfo *pInode;
12789 struct flock lock;
12790 int rc = SQLITE_OK;
12791
12792 assert( pFile );
12793 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
12794 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
12795 osGetpid(0)));
12796
12797 assert( eFileLock<=SHARED_LOCK );
12798 if( pFile->eFileLock<=eFileLock ){
12799 return SQLITE_OK;
12800 }
12801 unixEnterMutex();
12802 pInode = pFile->pInode;
12803 assert( pInode->nShared!=0 );
12804 if( pFile->eFileLock>SHARED_LOCK ){
12805 assert( pInode->eFileLock==pFile->eFileLock );
12806
12807 #ifdef SQLITE_DEBUG
12808 /* When reducing a lock such that other processes can start
12809 ** reading the database file again, make sure that the
12810 ** transaction counter was updated if any part of the database
12811 ** file changed. If the transaction counter is not updated,
12812 ** other connections to the same file might not realize that
12813 ** the file has changed and hence might not know to flush their
12814 ** cache. The use of a stale cache can lead to database corruption.
12815 */
12816 pFile->inNormalWrite = 0;
12817 #endif
12818
12819 /* downgrading to a shared lock on NFS involves clearing the write lock
12820 ** before establishing the readlock - to avoid a race condition we downgrade
12821 ** the lock in 2 blocks, so that part of the range will be covered by a
12822 ** write lock until the rest is covered by a read lock:
12823 ** 1: [WWWWW]
12824 ** 2: [....W]
12825 ** 3: [RRRRW]
12826 ** 4: [RRRR.]
12827 */
12828 if( eFileLock==SHARED_LOCK ){
12829 #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
12830 (void)handleNFSUnlock;
12831 assert( handleNFSUnlock==0 );
12832 #endif
12833 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
12834 if( handleNFSUnlock ){
12835 int tErrno; /* Error code from system call errors */
12836 off_t divSize = SHARED_SIZE - 1;
12837
12838 lock.l_type = F_UNLCK;
12839 lock.l_whence = SEEK_SET;
12840 lock.l_start = SHARED_FIRST;
12841 lock.l_len = divSize;
12842 if( unixFileLock(pFile, &lock)==(-1) ){
12843 tErrno = errno;
12844 rc = SQLITE_IOERR_UNLOCK;
12845 storeLastErrno(pFile, tErrno);
12846 goto end_unlock;
12847 }
12848 lock.l_type = F_RDLCK;
12849 lock.l_whence = SEEK_SET;
12850 lock.l_start = SHARED_FIRST;
12851 lock.l_len = divSize;
12852 if( unixFileLock(pFile, &lock)==(-1) ){
12853 tErrno = errno;
12854 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
12855 if( IS_LOCK_ERROR(rc) ){
12856 storeLastErrno(pFile, tErrno);
12857 }
12858 goto end_unlock;
12859 }
12860 lock.l_type = F_UNLCK;
12861 lock.l_whence = SEEK_SET;
12862 lock.l_start = SHARED_FIRST+divSize;
12863 lock.l_len = SHARED_SIZE-divSize;
12864 if( unixFileLock(pFile, &lock)==(-1) ){
12865 tErrno = errno;
12866 rc = SQLITE_IOERR_UNLOCK;
12867 storeLastErrno(pFile, tErrno);
12868 goto end_unlock;
12869 }
12870 }else
12871 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
12872 {
12873 lock.l_type = F_RDLCK;
12874 lock.l_whence = SEEK_SET;
12875 lock.l_start = SHARED_FIRST;
12876 lock.l_len = SHARED_SIZE;
12877 if( unixFileLock(pFile, &lock) ){
12878 /* In theory, the call to unixFileLock() cannot fail because another
12879 ** process is holding an incompatible lock. If it does, this
12880 ** indicates that the other process is not following the locking
12881 ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
12882 ** SQLITE_BUSY would confuse the upper layer (in practice it causes
12883 ** an assert to fail). */
12884 rc = SQLITE_IOERR_RDLOCK;
12885 storeLastErrno(pFile, errno);
12886 goto end_unlock;
12887 }
12888 }
12889 }
12890 lock.l_type = F_UNLCK;
12891 lock.l_whence = SEEK_SET;
12892 lock.l_start = PENDING_BYTE;
12893 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
12894 if( unixFileLock(pFile, &lock)==0 ){
12895 pInode->eFileLock = SHARED_LOCK;
12896 }else{
12897 rc = SQLITE_IOERR_UNLOCK;
12898 storeLastErrno(pFile, errno);
12899 goto end_unlock;
12900 }
12901 }
12902 if( eFileLock==NO_LOCK ){
12903 /* Decrement the shared lock counter. Release the lock using an
12904 ** OS call only when all threads in this same process have released
12905 ** the lock.
12906 */
12907 pInode->nShared--;
12908 if( pInode->nShared==0 ){
12909 lock.l_type = F_UNLCK;
12910 lock.l_whence = SEEK_SET;
12911 lock.l_start = lock.l_len = 0L;
12912 if( unixFileLock(pFile, &lock)==0 ){
12913 pInode->eFileLock = NO_LOCK;
12914 }else{
12915 rc = SQLITE_IOERR_UNLOCK;
12916 storeLastErrno(pFile, errno);
12917 pInode->eFileLock = NO_LOCK;
12918 pFile->eFileLock = NO_LOCK;
12919 }
12920 }
12921
12922 /* Decrement the count of locks against this same file. When the
12923 ** count reaches zero, close any other file descriptors whose close
12924 ** was deferred because of outstanding locks.
12925 */
12926 pInode->nLock--;
12927 assert( pInode->nLock>=0 );
12928 if( pInode->nLock==0 ){
12929 closePendingFds(pFile);
12930 }
12931 }
12932
12933 end_unlock:
12934 unixLeaveMutex();
12935 if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
12936 return rc;
12937 }
12938
12939 /*
12940 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
12941 ** must be either NO_LOCK or SHARED_LOCK.
12942 **
12943 ** If the locking level of the file descriptor is already at or below
12944 ** the requested locking level, this routine is a no-op.
12945 */
12946 static int unixUnlock(sqlite3_file *id, int eFileLock){
12947 #if SQLITE_MAX_MMAP_SIZE>0
12948 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
12949 #endif
12950 return posixUnlock(id, eFileLock, 0);
12951 }
12952
12953 #if SQLITE_MAX_MMAP_SIZE>0
12954 static int unixMapfile(unixFile *pFd, i64 nByte);
12955 static void unixUnmapfile(unixFile *pFd);
12956 #endif
12957
12958 /*
12959 ** This function performs the parts of the "close file" operation
12960 ** common to all locking schemes. It closes the directory and file
12961 ** handles, if they are valid, and sets all fields of the unixFile
12962 ** structure to 0.
12963 **
12964 ** It is *not* necessary to hold the mutex when this routine is called,
12965 ** even on VxWorks. A mutex will be acquired on VxWorks by the
12966 ** vxworksReleaseFileId() routine.
12967 */
12968 static int closeUnixFile(sqlite3_file *id){
12969 unixFile *pFile = (unixFile*)id;
12970 #if SQLITE_MAX_MMAP_SIZE>0
12971 unixUnmapfile(pFile);
12972 #endif
12973 if( pFile->h>=0 ){
12974 robust_close(pFile, pFile->h, __LINE__);
12975 pFile->h = -1;
12976 }
12977 #if OS_VXWORKS
12978 if( pFile->pId ){
12979 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
12980 osUnlink(pFile->pId->zCanonicalName);
12981 }
12982 vxworksReleaseFileId(pFile->pId);
12983 pFile->pId = 0;
12984 }
12985 #endif
12986 #ifdef SQLITE_UNLINK_AFTER_CLOSE
12987 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
12988 osUnlink(pFile->zPath);
12989 sqlite3_free(*(char**)&pFile->zPath);
12990 pFile->zPath = 0;
12991 }
12992 #endif
12993 OSTRACE(("CLOSE %-3d\n", pFile->h));
12994 OpenCounter(-1);
12995 sqlite3_free(pFile->pUnused);
12996 memset(pFile, 0, sizeof(unixFile));
12997 return SQLITE_OK;
12998 }
12999
13000 /*
13001 ** Close a file.
13002 */
13003 static int unixClose(sqlite3_file *id){
13004 int rc = SQLITE_OK;
13005 unixFile *pFile = (unixFile *)id;
13006 verifyDbFile(pFile);
13007 unixUnlock(id, NO_LOCK);
13008 unixEnterMutex();
13009
13010 /* unixFile.pInode is always valid here. Otherwise, a different close
13011 ** routine (e.g. nolockClose()) would be called instead.
13012 */
13013 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
13014 if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
13015 /* If there are outstanding locks, do not actually close the file just
13016 ** yet because that would clear those locks. Instead, add the file
13017 ** descriptor to pInode->pUnused list. It will be automatically closed
13018 ** when the last lock is cleared.
13019 */
13020 setPendingFd(pFile);
13021 }
13022 releaseInodeInfo(pFile);
13023 rc = closeUnixFile(id);
13024 unixLeaveMutex();
13025 return rc;
13026 }
13027
13028 /************** End of the posix advisory lock implementation *****************
13029 ******************************************************************************/
13030
13031 /******************************************************************************
13032 ****************************** No-op Locking **********************************
13033 **
13034 ** Of the various locking implementations available, this is by far the
13035 ** simplest: locking is ignored. No attempt is made to lock the database
13036 ** file for reading or writing.
13037 **
13038 ** This locking mode is appropriate for use on read-only databases
13039 ** (ex: databases that are burned into CD-ROM, for example.) It can
13040 ** also be used if the application employs some external mechanism to
13041 ** prevent simultaneous access of the same database by two or more
13042 ** database connections. But there is a serious risk of database
13043 ** corruption if this locking mode is used in situations where multiple
13044 ** database connections are accessing the same database file at the same
13045 ** time and one or more of those connections are writing.
13046 */
13047
13048 static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
13049 UNUSED_PARAMETER(NotUsed);
13050 *pResOut = 0;
13051 return SQLITE_OK;
13052 }
13053 static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
13054 UNUSED_PARAMETER2(NotUsed, NotUsed2);
13055 return SQLITE_OK;
13056 }
13057 static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
13058 UNUSED_PARAMETER2(NotUsed, NotUsed2);
13059 return SQLITE_OK;
13060 }
13061
13062 /*
13063 ** Close the file.
13064 */
13065 static int nolockClose(sqlite3_file *id) {
13066 return closeUnixFile(id);
13067 }
13068
13069 /******************* End of the no-op lock implementation *********************
13070 ******************************************************************************/
13071
13072 /******************************************************************************
13073 ************************* Begin dot-file Locking ******************************
13074 **
13075 ** The dotfile locking implementation uses the existence of separate lock
13076 ** files (really a directory) to control access to the database. This works
13077 ** on just about every filesystem imaginable. But there are serious downsides:
13078 **
13079 ** (1) There is zero concurrency. A single reader blocks all other
13080 ** connections from reading or writing the database.
13081 **
13082 ** (2) An application crash or power loss can leave stale lock files
13083 ** sitting around that need to be cleared manually.
13084 **
13085 ** Nevertheless, a dotlock is an appropriate locking mode for use if no
13086 ** other locking strategy is available.
13087 **
13088 ** Dotfile locking works by creating a subdirectory in the same directory as
13089 ** the database and with the same name but with a ".lock" extension added.
13090 ** The existence of a lock directory implies an EXCLUSIVE lock. All other
13091 ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
13092 */
13093
13094 /*
13095 ** The file suffix added to the data base filename in order to create the
13096 ** lock directory.
13097 */
13098 #define DOTLOCK_SUFFIX ".lock"
13099
13100 /*
13101 ** This routine checks if there is a RESERVED lock held on the specified
13102 ** file by this or any other process. If such a lock is held, set *pResOut
13103 ** to a non-zero value otherwise *pResOut is set to zero. The return value
13104 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
13105 **
13106 ** In dotfile locking, either a lock exists or it does not. So in this
13107 ** variation of CheckReservedLock(), *pResOut is set to true if any lock
13108 ** is held on the file and false if the file is unlocked.
13109 */
13110 static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
13111 int rc = SQLITE_OK;
13112 int reserved = 0;
13113 unixFile *pFile = (unixFile*)id;
13114
13115 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
13116
13117 assert( pFile );
13118 reserved = osAccess((const char*)pFile->lockingContext, 0)==0;
13119 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
13120 *pResOut = reserved;
13121 return rc;
13122 }
13123
13124 /*
13125 ** Lock the file with the lock specified by parameter eFileLock - one
13126 ** of the following:
13127 **
13128 ** (1) SHARED_LOCK
13129 ** (2) RESERVED_LOCK
13130 ** (3) PENDING_LOCK
13131 ** (4) EXCLUSIVE_LOCK
13132 **
13133 ** Sometimes when requesting one lock state, additional lock states
13134 ** are inserted in between. The locking might fail on one of the later
13135 ** transitions leaving the lock state different from what it started but
13136 ** still short of its goal. The following chart shows the allowed
13137 ** transitions and the inserted intermediate states:
13138 **
13139 ** UNLOCKED -> SHARED
13140 ** SHARED -> RESERVED
13141 ** SHARED -> (PENDING) -> EXCLUSIVE
13142 ** RESERVED -> (PENDING) -> EXCLUSIVE
13143 ** PENDING -> EXCLUSIVE
13144 **
13145 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
13146 ** routine to lower a locking level.
13147 **
13148 ** With dotfile locking, we really only support state (4): EXCLUSIVE.
13149 ** But we track the other locking levels internally.
13150 */
13151 static int dotlockLock(sqlite3_file *id, int eFileLock) {
13152 unixFile *pFile = (unixFile*)id;
13153 char *zLockFile = (char *)pFile->lockingContext;
13154 int rc = SQLITE_OK;
13155
13156
13157 /* If we have any lock, then the lock file already exists. All we have
13158 ** to do is adjust our internal record of the lock level.
13159 */
13160 if( pFile->eFileLock > NO_LOCK ){
13161 pFile->eFileLock = eFileLock;
13162 /* Always update the timestamp on the old file */
13163 #ifdef HAVE_UTIME
13164 utime(zLockFile, NULL);
13165 #else
13166 utimes(zLockFile, NULL);
13167 #endif
13168 return SQLITE_OK;
13169 }
13170
13171 /* grab an exclusive lock */
13172 rc = osMkdir(zLockFile, 0777);
13173 if( rc<0 ){
13174 /* failed to open/create the lock directory */
13175 int tErrno = errno;
13176 if( EEXIST == tErrno ){
13177 rc = SQLITE_BUSY;
13178 } else {
13179 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
13180 if( rc!=SQLITE_BUSY ){
13181 storeLastErrno(pFile, tErrno);
13182 }
13183 }
13184 return rc;
13185 }
13186
13187 /* got it, set the type and return ok */
13188 pFile->eFileLock = eFileLock;
13189 return rc;
13190 }
13191
13192 /*
13193 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
13194 ** must be either NO_LOCK or SHARED_LOCK.
13195 **
13196 ** If the locking level of the file descriptor is already at or below
13197 ** the requested locking level, this routine is a no-op.
13198 **
13199 ** When the locking level reaches NO_LOCK, delete the lock file.
13200 */
13201 static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
13202 unixFile *pFile = (unixFile*)id;
13203 char *zLockFile = (char *)pFile->lockingContext;
13204 int rc;
13205
13206 assert( pFile );
13207 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
13208 pFile->eFileLock, osGetpid(0)));
13209 assert( eFileLock<=SHARED_LOCK );
13210
13211 /* no-op if possible */
13212 if( pFile->eFileLock==eFileLock ){
13213 return SQLITE_OK;
13214 }
13215
13216 /* To downgrade to shared, simply update our internal notion of the
13217 ** lock state. No need to mess with the file on disk.
13218 */
13219 if( eFileLock==SHARED_LOCK ){
13220 pFile->eFileLock = SHARED_LOCK;
13221 return SQLITE_OK;
13222 }
13223
13224 /* To fully unlock the database, delete the lock file */
13225 assert( eFileLock==NO_LOCK );
13226 rc = osRmdir(zLockFile);
13227 if( rc<0 ){
13228 int tErrno = errno;
13229 if( tErrno==ENOENT ){
13230 rc = SQLITE_OK;
13231 }else{
13232 rc = SQLITE_IOERR_UNLOCK;
13233 storeLastErrno(pFile, tErrno);
13234 }
13235 return rc;
13236 }
13237 pFile->eFileLock = NO_LOCK;
13238 return SQLITE_OK;
13239 }
13240
13241 /*
13242 ** Close a file. Make sure the lock has been released before closing.
13243 */
13244 static int dotlockClose(sqlite3_file *id) {
13245 unixFile *pFile = (unixFile*)id;
13246 assert( id!=0 );
13247 dotlockUnlock(id, NO_LOCK);
13248 sqlite3_free(pFile->lockingContext);
13249 return closeUnixFile(id);
13250 }
13251 /****************** End of the dot-file lock implementation *******************
13252 ******************************************************************************/
13253
13254 /******************************************************************************
13255 ************************** Begin flock Locking ********************************
13256 **
13257 ** Use the flock() system call to do file locking.
13258 **
13259 ** flock() locking is like dot-file locking in that the various
13260 ** fine-grain locking levels supported by SQLite are collapsed into
13261 ** a single exclusive lock. In other words, SHARED, RESERVED, and
13262 ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite
13263 ** still works when you do this, but concurrency is reduced since
13264 ** only a single process can be reading the database at a time.
13265 **
13266 ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off
13267 */
13268 #if SQLITE_ENABLE_LOCKING_STYLE
13269
13270 /*
13271 ** Retry flock() calls that fail with EINTR
13272 */
13273 #ifdef EINTR
13274 static int robust_flock(int fd, int op){
13275 int rc;
13276 do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
13277 return rc;
13278 }
13279 #else
13280 # define robust_flock(a,b) flock(a,b)
13281 #endif
13282
13283
13284 /*
13285 ** This routine checks if there is a RESERVED lock held on the specified
13286 ** file by this or any other process. If such a lock is held, set *pResOut
13287 ** to a non-zero value otherwise *pResOut is set to zero. The return value
13288 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
13289 */
13290 static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
13291 int rc = SQLITE_OK;
13292 int reserved = 0;
13293 unixFile *pFile = (unixFile*)id;
13294
13295 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
13296
13297 assert( pFile );
13298
13299 /* Check if a thread in this process holds such a lock */
13300 if( pFile->eFileLock>SHARED_LOCK ){
13301 reserved = 1;
13302 }
13303
13304 /* Otherwise see if some other process holds it. */
13305 if( !reserved ){
13306 /* attempt to get the lock */
13307 int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
13308 if( !lrc ){
13309 /* got the lock, unlock it */
13310 lrc = robust_flock(pFile->h, LOCK_UN);
13311 if ( lrc ) {
13312 int tErrno = errno;
13313 /* unlock failed with an error */
13314 lrc = SQLITE_IOERR_UNLOCK;
13315 storeLastErrno(pFile, tErrno);
13316 rc = lrc;
13317 }
13318 } else {
13319 int tErrno = errno;
13320 reserved = 1;
13321 /* someone else might have it reserved */
13322 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
13323 if( IS_LOCK_ERROR(lrc) ){
13324 storeLastErrno(pFile, tErrno);
13325 rc = lrc;
13326 }
13327 }
13328 }
13329 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
13330
13331 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
13332 if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
13333 rc = SQLITE_OK;
13334 reserved=1;
13335 }
13336 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
13337 *pResOut = reserved;
13338 return rc;
13339 }
13340
13341 /*
13342 ** Lock the file with the lock specified by parameter eFileLock - one
13343 ** of the following:
13344 **
13345 ** (1) SHARED_LOCK
13346 ** (2) RESERVED_LOCK
13347 ** (3) PENDING_LOCK
13348 ** (4) EXCLUSIVE_LOCK
13349 **
13350 ** Sometimes when requesting one lock state, additional lock states
13351 ** are inserted in between. The locking might fail on one of the later
13352 ** transitions leaving the lock state different from what it started but
13353 ** still short of its goal. The following chart shows the allowed
13354 ** transitions and the inserted intermediate states:
13355 **
13356 ** UNLOCKED -> SHARED
13357 ** SHARED -> RESERVED
13358 ** SHARED -> (PENDING) -> EXCLUSIVE
13359 ** RESERVED -> (PENDING) -> EXCLUSIVE
13360 ** PENDING -> EXCLUSIVE
13361 **
13362 ** flock() only really support EXCLUSIVE locks. We track intermediate
13363 ** lock states in the sqlite3_file structure, but all locks SHARED or
13364 ** above are really EXCLUSIVE locks and exclude all other processes from
13365 ** access the file.
13366 **
13367 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
13368 ** routine to lower a locking level.
13369 */
13370 static int flockLock(sqlite3_file *id, int eFileLock) {
13371 int rc = SQLITE_OK;
13372 unixFile *pFile = (unixFile*)id;
13373
13374 assert( pFile );
13375
13376 /* if we already have a lock, it is exclusive.
13377 ** Just adjust level and punt on outta here. */
13378 if (pFile->eFileLock > NO_LOCK) {
13379 pFile->eFileLock = eFileLock;
13380 return SQLITE_OK;
13381 }
13382
13383 /* grab an exclusive lock */
13384
13385 if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
13386 int tErrno = errno;
13387 /* didn't get, must be busy */
13388 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
13389 if( IS_LOCK_ERROR(rc) ){
13390 storeLastErrno(pFile, tErrno);
13391 }
13392 } else {
13393 /* got it, set the type and return ok */
13394 pFile->eFileLock = eFileLock;
13395 }
13396 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
13397 rc==SQLITE_OK ? "ok" : "failed"));
13398 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
13399 if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
13400 rc = SQLITE_BUSY;
13401 }
13402 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
13403 return rc;
13404 }
13405
13406
13407 /*
13408 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
13409 ** must be either NO_LOCK or SHARED_LOCK.
13410 **
13411 ** If the locking level of the file descriptor is already at or below
13412 ** the requested locking level, this routine is a no-op.
13413 */
13414 static int flockUnlock(sqlite3_file *id, int eFileLock) {
13415 unixFile *pFile = (unixFile*)id;
13416
13417 assert( pFile );
13418 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
13419 pFile->eFileLock, osGetpid(0)));
13420 assert( eFileLock<=SHARED_LOCK );
13421
13422 /* no-op if possible */
13423 if( pFile->eFileLock==eFileLock ){
13424 return SQLITE_OK;
13425 }
13426
13427 /* shared can just be set because we always have an exclusive */
13428 if (eFileLock==SHARED_LOCK) {
13429 pFile->eFileLock = eFileLock;
13430 return SQLITE_OK;
13431 }
13432
13433 /* no, really, unlock. */
13434 if( robust_flock(pFile->h, LOCK_UN) ){
13435 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
13436 return SQLITE_OK;
13437 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
13438 return SQLITE_IOERR_UNLOCK;
13439 }else{
13440 pFile->eFileLock = NO_LOCK;
13441 return SQLITE_OK;
13442 }
13443 }
13444
13445 /*
13446 ** Close a file.
13447 */
13448 static int flockClose(sqlite3_file *id) {
13449 assert( id!=0 );
13450 flockUnlock(id, NO_LOCK);
13451 return closeUnixFile(id);
13452 }
13453
13454 #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
13455
13456 /******************* End of the flock lock implementation *********************
13457 ******************************************************************************/
13458
13459 /******************************************************************************
13460 ************************ Begin Named Semaphore Locking ************************
13461 **
13462 ** Named semaphore locking is only supported on VxWorks.
13463 **
13464 ** Semaphore locking is like dot-lock and flock in that it really only
13465 ** supports EXCLUSIVE locking. Only a single process can read or write
13466 ** the database file at a time. This reduces potential concurrency, but
13467 ** makes the lock implementation much easier.
13468 */
13469 #if OS_VXWORKS
13470
13471 /*
13472 ** This routine checks if there is a RESERVED lock held on the specified
13473 ** file by this or any other process. If such a lock is held, set *pResOut
13474 ** to a non-zero value otherwise *pResOut is set to zero. The return value
13475 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
13476 */
13477 static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) {
13478 int rc = SQLITE_OK;
13479 int reserved = 0;
13480 unixFile *pFile = (unixFile*)id;
13481
13482 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
13483
13484 assert( pFile );
13485
13486 /* Check if a thread in this process holds such a lock */
13487 if( pFile->eFileLock>SHARED_LOCK ){
13488 reserved = 1;
13489 }
13490
13491 /* Otherwise see if some other process holds it. */
13492 if( !reserved ){
13493 sem_t *pSem = pFile->pInode->pSem;
13494
13495 if( sem_trywait(pSem)==-1 ){
13496 int tErrno = errno;
13497 if( EAGAIN != tErrno ){
13498 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
13499 storeLastErrno(pFile, tErrno);
13500 } else {
13501 /* someone else has the lock when we are in NO_LOCK */
13502 reserved = (pFile->eFileLock < SHARED_LOCK);
13503 }
13504 }else{
13505 /* we could have it if we want it */
13506 sem_post(pSem);
13507 }
13508 }
13509 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
13510
13511 *pResOut = reserved;
13512 return rc;
13513 }
13514
13515 /*
13516 ** Lock the file with the lock specified by parameter eFileLock - one
13517 ** of the following:
13518 **
13519 ** (1) SHARED_LOCK
13520 ** (2) RESERVED_LOCK
13521 ** (3) PENDING_LOCK
13522 ** (4) EXCLUSIVE_LOCK
13523 **
13524 ** Sometimes when requesting one lock state, additional lock states
13525 ** are inserted in between. The locking might fail on one of the later
13526 ** transitions leaving the lock state different from what it started but
13527 ** still short of its goal. The following chart shows the allowed
13528 ** transitions and the inserted intermediate states:
13529 **
13530 ** UNLOCKED -> SHARED
13531 ** SHARED -> RESERVED
13532 ** SHARED -> (PENDING) -> EXCLUSIVE
13533 ** RESERVED -> (PENDING) -> EXCLUSIVE
13534 ** PENDING -> EXCLUSIVE
13535 **
13536 ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate
13537 ** lock states in the sqlite3_file structure, but all locks SHARED or
13538 ** above are really EXCLUSIVE locks and exclude all other processes from
13539 ** access the file.
13540 **
13541 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
13542 ** routine to lower a locking level.
13543 */
13544 static int semXLock(sqlite3_file *id, int eFileLock) {
13545 unixFile *pFile = (unixFile*)id;
13546 sem_t *pSem = pFile->pInode->pSem;
13547 int rc = SQLITE_OK;
13548
13549 /* if we already have a lock, it is exclusive.
13550 ** Just adjust level and punt on outta here. */
13551 if (pFile->eFileLock > NO_LOCK) {
13552 pFile->eFileLock = eFileLock;
13553 rc = SQLITE_OK;
13554 goto sem_end_lock;
13555 }
13556
13557 /* lock semaphore now but bail out when already locked. */
13558 if( sem_trywait(pSem)==-1 ){
13559 rc = SQLITE_BUSY;
13560 goto sem_end_lock;
13561 }
13562
13563 /* got it, set the type and return ok */
13564 pFile->eFileLock = eFileLock;
13565
13566 sem_end_lock:
13567 return rc;
13568 }
13569
13570 /*
13571 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
13572 ** must be either NO_LOCK or SHARED_LOCK.
13573 **
13574 ** If the locking level of the file descriptor is already at or below
13575 ** the requested locking level, this routine is a no-op.
13576 */
13577 static int semXUnlock(sqlite3_file *id, int eFileLock) {
13578 unixFile *pFile = (unixFile*)id;
13579 sem_t *pSem = pFile->pInode->pSem;
13580
13581 assert( pFile );
13582 assert( pSem );
13583 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
13584 pFile->eFileLock, osGetpid(0)));
13585 assert( eFileLock<=SHARED_LOCK );
13586
13587 /* no-op if possible */
13588 if( pFile->eFileLock==eFileLock ){
13589 return SQLITE_OK;
13590 }
13591
13592 /* shared can just be set because we always have an exclusive */
13593 if (eFileLock==SHARED_LOCK) {
13594 pFile->eFileLock = eFileLock;
13595 return SQLITE_OK;
13596 }
13597
13598 /* no, really unlock. */
13599 if ( sem_post(pSem)==-1 ) {
13600 int rc, tErrno = errno;
13601 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
13602 if( IS_LOCK_ERROR(rc) ){
13603 storeLastErrno(pFile, tErrno);
13604 }
13605 return rc;
13606 }
13607 pFile->eFileLock = NO_LOCK;
13608 return SQLITE_OK;
13609 }
13610
13611 /*
13612 ** Close a file.
13613 */
13614 static int semXClose(sqlite3_file *id) {
13615 if( id ){
13616 unixFile *pFile = (unixFile*)id;
13617 semXUnlock(id, NO_LOCK);
13618 assert( pFile );
13619 unixEnterMutex();
13620 releaseInodeInfo(pFile);
13621 unixLeaveMutex();
13622 closeUnixFile(id);
13623 }
13624 return SQLITE_OK;
13625 }
13626
13627 #endif /* OS_VXWORKS */
13628 /*
13629 ** Named semaphore locking is only available on VxWorks.
13630 **
13631 *************** End of the named semaphore lock implementation ****************
13632 ******************************************************************************/
13633
13634
13635 /******************************************************************************
13636 *************************** Begin AFP Locking *********************************
13637 **
13638 ** AFP is the Apple Filing Protocol. AFP is a network filesystem found
13639 ** on Apple Macintosh computers - both OS9 and OSX.
13640 **
13641 ** Third-party implementations of AFP are available. But this code here
13642 ** only works on OSX.
13643 */
13644
13645 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
13646 /*
13647 ** The afpLockingContext structure contains all afp lock specific state
13648 */
13649 typedef struct afpLockingContext afpLockingContext;
13650 struct afpLockingContext {
13651 int reserved;
13652 const char *dbPath; /* Name of the open file */
13653 };
13654
13655 struct ByteRangeLockPB2
13656 {
13657 unsigned long long offset; /* offset to first byte to lock */
13658 unsigned long long length; /* nbr of bytes to lock */
13659 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
13660 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
13661 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
13662 int fd; /* file desc to assoc this lock with */
13663 };
13664
13665 #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
13666
13667 /*
13668 ** This is a utility for setting or clearing a bit-range lock on an
13669 ** AFP filesystem.
13670 **
13671 ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
13672 */
13673 static int afpSetLock(
13674 const char *path, /* Name of the file to be locked or unlocked */
13675 unixFile *pFile, /* Open file descriptor on path */
13676 unsigned long long offset, /* First byte to be locked */
13677 unsigned long long length, /* Number of bytes to lock */
13678 int setLockFlag /* True to set lock. False to clear lock */
13679 ){
13680 struct ByteRangeLockPB2 pb;
13681 int err;
13682
13683 pb.unLockFlag = setLockFlag ? 0 : 1;
13684 pb.startEndFlag = 0;
13685 pb.offset = offset;
13686 pb.length = length;
13687 pb.fd = pFile->h;
13688
13689 OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
13690 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
13691 offset, length));
13692 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
13693 if ( err==-1 ) {
13694 int rc;
13695 int tErrno = errno;
13696 OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
13697 path, tErrno, strerror(tErrno)));
13698 #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
13699 rc = SQLITE_BUSY;
13700 #else
13701 rc = sqliteErrorFromPosixError(tErrno,
13702 setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
13703 #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
13704 if( IS_LOCK_ERROR(rc) ){
13705 storeLastErrno(pFile, tErrno);
13706 }
13707 return rc;
13708 } else {
13709 return SQLITE_OK;
13710 }
13711 }
13712
13713 /*
13714 ** This routine checks if there is a RESERVED lock held on the specified
13715 ** file by this or any other process. If such a lock is held, set *pResOut
13716 ** to a non-zero value otherwise *pResOut is set to zero. The return value
13717 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
13718 */
13719 static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
13720 int rc = SQLITE_OK;
13721 int reserved = 0;
13722 unixFile *pFile = (unixFile*)id;
13723 afpLockingContext *context;
13724
13725 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
13726
13727 assert( pFile );
13728 context = (afpLockingContext *) pFile->lockingContext;
13729 if( context->reserved ){
13730 *pResOut = 1;
13731 return SQLITE_OK;
13732 }
13733 unixEnterMutex(); /* Because pFile->pInode is shared across threads */
13734
13735 /* Check if a thread in this process holds such a lock */
13736 if( pFile->pInode->eFileLock>SHARED_LOCK ){
13737 reserved = 1;
13738 }
13739
13740 /* Otherwise see if some other process holds it.
13741 */
13742 if( !reserved ){
13743 /* lock the RESERVED byte */
13744 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
13745 if( SQLITE_OK==lrc ){
13746 /* if we succeeded in taking the reserved lock, unlock it to restore
13747 ** the original state */
13748 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
13749 } else {
13750 /* if we failed to get the lock then someone else must have it */
13751 reserved = 1;
13752 }
13753 if( IS_LOCK_ERROR(lrc) ){
13754 rc=lrc;
13755 }
13756 }
13757
13758 unixLeaveMutex();
13759 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
13760
13761 *pResOut = reserved;
13762 return rc;
13763 }
13764
13765 /*
13766 ** Lock the file with the lock specified by parameter eFileLock - one
13767 ** of the following:
13768 **
13769 ** (1) SHARED_LOCK
13770 ** (2) RESERVED_LOCK
13771 ** (3) PENDING_LOCK
13772 ** (4) EXCLUSIVE_LOCK
13773 **
13774 ** Sometimes when requesting one lock state, additional lock states
13775 ** are inserted in between. The locking might fail on one of the later
13776 ** transitions leaving the lock state different from what it started but
13777 ** still short of its goal. The following chart shows the allowed
13778 ** transitions and the inserted intermediate states:
13779 **
13780 ** UNLOCKED -> SHARED
13781 ** SHARED -> RESERVED
13782 ** SHARED -> (PENDING) -> EXCLUSIVE
13783 ** RESERVED -> (PENDING) -> EXCLUSIVE
13784 ** PENDING -> EXCLUSIVE
13785 **
13786 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
13787 ** routine to lower a locking level.
13788 */
13789 static int afpLock(sqlite3_file *id, int eFileLock){
13790 int rc = SQLITE_OK;
13791 unixFile *pFile = (unixFile*)id;
13792 unixInodeInfo *pInode = pFile->pInode;
13793 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
13794
13795 assert( pFile );
13796 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
13797 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
13798 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
13799
13800 /* If there is already a lock of this type or more restrictive on the
13801 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
13802 ** unixEnterMutex() hasn't been called yet.
13803 */
13804 if( pFile->eFileLock>=eFileLock ){
13805 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
13806 azFileLock(eFileLock)));
13807 return SQLITE_OK;
13808 }
13809
13810 /* Make sure the locking sequence is correct
13811 ** (1) We never move from unlocked to anything higher than shared lock.
13812 ** (2) SQLite never explicitly requests a pendig lock.
13813 ** (3) A shared lock is always held when a reserve lock is requested.
13814 */
13815 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
13816 assert( eFileLock!=PENDING_LOCK );
13817 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
13818
13819 /* This mutex is needed because pFile->pInode is shared across threads
13820 */
13821 unixEnterMutex();
13822 pInode = pFile->pInode;
13823
13824 /* If some thread using this PID has a lock via a different unixFile*
13825 ** handle that precludes the requested lock, return BUSY.
13826 */
13827 if( (pFile->eFileLock!=pInode->eFileLock &&
13828 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
13829 ){
13830 rc = SQLITE_BUSY;
13831 goto afp_end_lock;
13832 }
13833
13834 /* If a SHARED lock is requested, and some thread using this PID already
13835 ** has a SHARED or RESERVED lock, then increment reference counts and
13836 ** return SQLITE_OK.
13837 */
13838 if( eFileLock==SHARED_LOCK &&
13839 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
13840 assert( eFileLock==SHARED_LOCK );
13841 assert( pFile->eFileLock==0 );
13842 assert( pInode->nShared>0 );
13843 pFile->eFileLock = SHARED_LOCK;
13844 pInode->nShared++;
13845 pInode->nLock++;
13846 goto afp_end_lock;
13847 }
13848
13849 /* A PENDING lock is needed before acquiring a SHARED lock and before
13850 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
13851 ** be released.
13852 */
13853 if( eFileLock==SHARED_LOCK
13854 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
13855 ){
13856 int failed;
13857 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
13858 if (failed) {
13859 rc = failed;
13860 goto afp_end_lock;
13861 }
13862 }
13863
13864 /* If control gets to this point, then actually go ahead and make
13865 ** operating system calls for the specified lock.
13866 */
13867 if( eFileLock==SHARED_LOCK ){
13868 int lrc1, lrc2, lrc1Errno = 0;
13869 long lk, mask;
13870
13871 assert( pInode->nShared==0 );
13872 assert( pInode->eFileLock==0 );
13873
13874 mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
13875 /* Now get the read-lock SHARED_LOCK */
13876 /* note that the quality of the randomness doesn't matter that much */
13877 lk = random();
13878 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
13879 lrc1 = afpSetLock(context->dbPath, pFile,
13880 SHARED_FIRST+pInode->sharedByte, 1, 1);
13881 if( IS_LOCK_ERROR(lrc1) ){
13882 lrc1Errno = pFile->lastErrno;
13883 }
13884 /* Drop the temporary PENDING lock */
13885 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
13886
13887 if( IS_LOCK_ERROR(lrc1) ) {
13888 storeLastErrno(pFile, lrc1Errno);
13889 rc = lrc1;
13890 goto afp_end_lock;
13891 } else if( IS_LOCK_ERROR(lrc2) ){
13892 rc = lrc2;
13893 goto afp_end_lock;
13894 } else if( lrc1 != SQLITE_OK ) {
13895 rc = lrc1;
13896 } else {
13897 pFile->eFileLock = SHARED_LOCK;
13898 pInode->nLock++;
13899 pInode->nShared = 1;
13900 }
13901 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
13902 /* We are trying for an exclusive lock but another thread in this
13903 ** same process is still holding a shared lock. */
13904 rc = SQLITE_BUSY;
13905 }else{
13906 /* The request was for a RESERVED or EXCLUSIVE lock. It is
13907 ** assumed that there is a SHARED or greater lock on the file
13908 ** already.
13909 */
13910 int failed = 0;
13911 assert( 0!=pFile->eFileLock );
13912 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
13913 /* Acquire a RESERVED lock */
13914 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
13915 if( !failed ){
13916 context->reserved = 1;
13917 }
13918 }
13919 if (!failed && eFileLock == EXCLUSIVE_LOCK) {
13920 /* Acquire an EXCLUSIVE lock */
13921
13922 /* Remove the shared lock before trying the range. we'll need to
13923 ** reestablish the shared lock if we can't get the afpUnlock
13924 */
13925 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
13926 pInode->sharedByte, 1, 0)) ){
13927 int failed2 = SQLITE_OK;
13928 /* now attemmpt to get the exclusive lock range */
13929 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
13930 SHARED_SIZE, 1);
13931 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
13932 SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
13933 /* Can't reestablish the shared lock. Sqlite can't deal, this is
13934 ** a critical I/O error
13935 */
13936 rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 :
13937 SQLITE_IOERR_LOCK;
13938 goto afp_end_lock;
13939 }
13940 }else{
13941 rc = failed;
13942 }
13943 }
13944 if( failed ){
13945 rc = failed;
13946 }
13947 }
13948
13949 if( rc==SQLITE_OK ){
13950 pFile->eFileLock = eFileLock;
13951 pInode->eFileLock = eFileLock;
13952 }else if( eFileLock==EXCLUSIVE_LOCK ){
13953 pFile->eFileLock = PENDING_LOCK;
13954 pInode->eFileLock = PENDING_LOCK;
13955 }
13956
13957 afp_end_lock:
13958 unixLeaveMutex();
13959 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
13960 rc==SQLITE_OK ? "ok" : "failed"));
13961 return rc;
13962 }
13963
13964 /*
13965 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
13966 ** must be either NO_LOCK or SHARED_LOCK.
13967 **
13968 ** If the locking level of the file descriptor is already at or below
13969 ** the requested locking level, this routine is a no-op.
13970 */
13971 static int afpUnlock(sqlite3_file *id, int eFileLock) {
13972 int rc = SQLITE_OK;
13973 unixFile *pFile = (unixFile*)id;
13974 unixInodeInfo *pInode;
13975 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
13976 int skipShared = 0;
13977 #ifdef SQLITE_TEST
13978 int h = pFile->h;
13979 #endif
13980
13981 assert( pFile );
13982 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
13983 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
13984 osGetpid(0)));
13985
13986 assert( eFileLock<=SHARED_LOCK );
13987 if( pFile->eFileLock<=eFileLock ){
13988 return SQLITE_OK;
13989 }
13990 unixEnterMutex();
13991 pInode = pFile->pInode;
13992 assert( pInode->nShared!=0 );
13993 if( pFile->eFileLock>SHARED_LOCK ){
13994 assert( pInode->eFileLock==pFile->eFileLock );
13995 SimulateIOErrorBenign(1);
13996 SimulateIOError( h=(-1) )
13997 SimulateIOErrorBenign(0);
13998
13999 #ifdef SQLITE_DEBUG
14000 /* When reducing a lock such that other processes can start
14001 ** reading the database file again, make sure that the
14002 ** transaction counter was updated if any part of the database
14003 ** file changed. If the transaction counter is not updated,
14004 ** other connections to the same file might not realize that
14005 ** the file has changed and hence might not know to flush their
14006 ** cache. The use of a stale cache can lead to database corruption.
14007 */
14008 assert( pFile->inNormalWrite==0
14009 || pFile->dbUpdate==0
14010 || pFile->transCntrChng==1 );
14011 pFile->inNormalWrite = 0;
14012 #endif
14013
14014 if( pFile->eFileLock==EXCLUSIVE_LOCK ){
14015 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
14016 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
14017 /* only re-establish the shared lock if necessary */
14018 int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
14019 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
14020 } else {
14021 skipShared = 1;
14022 }
14023 }
14024 if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
14025 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
14026 }
14027 if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
14028 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
14029 if( !rc ){
14030 context->reserved = 0;
14031 }
14032 }
14033 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
14034 pInode->eFileLock = SHARED_LOCK;
14035 }
14036 }
14037 if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
14038
14039 /* Decrement the shared lock counter. Release the lock using an
14040 ** OS call only when all threads in this same process have released
14041 ** the lock.
14042 */
14043 unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
14044 pInode->nShared--;
14045 if( pInode->nShared==0 ){
14046 SimulateIOErrorBenign(1);
14047 SimulateIOError( h=(-1) )
14048 SimulateIOErrorBenign(0);
14049 if( !skipShared ){
14050 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
14051 }
14052 if( !rc ){
14053 pInode->eFileLock = NO_LOCK;
14054 pFile->eFileLock = NO_LOCK;
14055 }
14056 }
14057 if( rc==SQLITE_OK ){
14058 pInode->nLock--;
14059 assert( pInode->nLock>=0 );
14060 if( pInode->nLock==0 ){
14061 closePendingFds(pFile);
14062 }
14063 }
14064 }
14065
14066 unixLeaveMutex();
14067 if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
14068 return rc;
14069 }
14070
14071 /*
14072 ** Close a file & cleanup AFP specific locking context
14073 */
14074 static int afpClose(sqlite3_file *id) {
14075 int rc = SQLITE_OK;
14076 unixFile *pFile = (unixFile*)id;
14077 assert( id!=0 );
14078 afpUnlock(id, NO_LOCK);
14079 unixEnterMutex();
14080 if( pFile->pInode && pFile->pInode->nLock ){
14081 /* If there are outstanding locks, do not actually close the file just
14082 ** yet because that would clear those locks. Instead, add the file
14083 ** descriptor to pInode->aPending. It will be automatically closed when
14084 ** the last lock is cleared.
14085 */
14086 setPendingFd(pFile);
14087 }
14088 releaseInodeInfo(pFile);
14089 sqlite3_free(pFile->lockingContext);
14090 rc = closeUnixFile(id);
14091 unixLeaveMutex();
14092 return rc;
14093 }
14094
14095 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
14096 /*
14097 ** The code above is the AFP lock implementation. The code is specific
14098 ** to MacOSX and does not work on other unix platforms. No alternative
14099 ** is available. If you don't compile for a mac, then the "unix-afp"
14100 ** VFS is not available.
14101 **
14102 ********************* End of the AFP lock implementation **********************
14103 ******************************************************************************/
14104
14105 /******************************************************************************
14106 *************************** Begin NFS Locking ********************************/
14107
14108 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
14109 /*
14110 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
14111 ** must be either NO_LOCK or SHARED_LOCK.
14112 **
14113 ** If the locking level of the file descriptor is already at or below
14114 ** the requested locking level, this routine is a no-op.
14115 */
14116 static int nfsUnlock(sqlite3_file *id, int eFileLock){
14117 return posixUnlock(id, eFileLock, 1);
14118 }
14119
14120 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
14121 /*
14122 ** The code above is the NFS lock implementation. The code is specific
14123 ** to MacOSX and does not work on other unix platforms. No alternative
14124 ** is available.
14125 **
14126 ********************* End of the NFS lock implementation **********************
14127 ******************************************************************************/
14128
14129 /******************************************************************************
14130 **************** Non-locking sqlite3_file methods *****************************
14131 **
14132 ** The next division contains implementations for all methods of the
14133 ** sqlite3_file object other than the locking methods. The locking
14134 ** methods were defined in divisions above (one locking method per
14135 ** division). Those methods that are common to all locking modes
14136 ** are gather together into this division.
14137 */
14138
14139 /*
14140 ** Seek to the offset passed as the second argument, then read cnt
14141 ** bytes into pBuf. Return the number of bytes actually read.
14142 **
14143 ** NB: If you define USE_PREAD or USE_PREAD64, then it might also
14144 ** be necessary to define _XOPEN_SOURCE to be 500. This varies from
14145 ** one system to another. Since SQLite does not define USE_PREAD
14146 ** in any form by default, we will not attempt to define _XOPEN_SOURCE.
14147 ** See tickets #2741 and #2681.
14148 **
14149 ** To avoid stomping the errno value on a failed read the lastErrno value
14150 ** is set before returning.
14151 */
14152 static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
14153 int got;
14154 int prior = 0;
14155 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
14156 i64 newOffset;
14157 #endif
14158 TIMER_START;
14159 assert( cnt==(cnt&0x1ffff) );
14160 assert( id->h>2 );
14161 do{
14162 #if defined(USE_PREAD)
14163 got = osPread(id->h, pBuf, cnt, offset);
14164 SimulateIOError( got = -1 );
14165 #elif defined(USE_PREAD64)
14166 got = osPread64(id->h, pBuf, cnt, offset);
14167 SimulateIOError( got = -1 );
14168 #else
14169 newOffset = lseek(id->h, offset, SEEK_SET);
14170 SimulateIOError( newOffset = -1 );
14171 if( newOffset<0 ){
14172 storeLastErrno((unixFile*)id, errno);
14173 return -1;
14174 }
14175 got = osRead(id->h, pBuf, cnt);
14176 #endif
14177 if( got==cnt ) break;
14178 if( got<0 ){
14179 if( errno==EINTR ){ got = 1; continue; }
14180 prior = 0;
14181 storeLastErrno((unixFile*)id, errno);
14182 break;
14183 }else if( got>0 ){
14184 cnt -= got;
14185 offset += got;
14186 prior += got;
14187 pBuf = (void*)(got + (char*)pBuf);
14188 }
14189 }while( got>0 );
14190 TIMER_END;
14191 OSTRACE(("READ %-3d %5d %7lld %llu\n",
14192 id->h, got+prior, offset-prior, TIMER_ELAPSED));
14193 return got+prior;
14194 }
14195
14196 /*
14197 ** Read data from a file into a buffer. Return SQLITE_OK if all
14198 ** bytes were read successfully and SQLITE_IOERR if anything goes
14199 ** wrong.
14200 */
14201 static int unixRead(
14202 sqlite3_file *id,
14203 void *pBuf,
14204 int amt,
14205 sqlite3_int64 offset
14206 ){
14207 unixFile *pFile = (unixFile *)id;
14208 int got;
14209 assert( id );
14210 assert( offset>=0 );
14211 assert( amt>0 );
14212
14213 /* If this is a database file (not a journal, master-journal or temp
14214 ** file), the bytes in the locking range should never be read or written. */
14215 #if 0
14216 assert( pFile->pUnused==0
14217 || offset>=PENDING_BYTE+512
14218 || offset+amt<=PENDING_BYTE
14219 );
14220 #endif
14221
14222 #if SQLITE_MAX_MMAP_SIZE>0
14223 /* Deal with as much of this read request as possible by transfering
14224 ** data from the memory mapping using memcpy(). */
14225 if( offset<pFile->mmapSize ){
14226 if( offset+amt <= pFile->mmapSize ){
14227 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
14228 return SQLITE_OK;
14229 }else{
14230 int nCopy = pFile->mmapSize - offset;
14231 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
14232 pBuf = &((u8 *)pBuf)[nCopy];
14233 amt -= nCopy;
14234 offset += nCopy;
14235 }
14236 }
14237 #endif
14238
14239 got = seekAndRead(pFile, offset, pBuf, amt);
14240 if( got==amt ){
14241 return SQLITE_OK;
14242 }else if( got<0 ){
14243 /* lastErrno set by seekAndRead */
14244 return SQLITE_IOERR_READ;
14245 }else{
14246 storeLastErrno(pFile, 0); /* not a system error */
14247 /* Unread parts of the buffer must be zero-filled */
14248 memset(&((char*)pBuf)[got], 0, amt-got);
14249 return SQLITE_IOERR_SHORT_READ;
14250 }
14251 }
14252
14253 /*
14254 ** Attempt to seek the file-descriptor passed as the first argument to
14255 ** absolute offset iOff, then attempt to write nBuf bytes of data from
14256 ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
14257 ** return the actual number of bytes written (which may be less than
14258 ** nBuf).
14259 */
14260 static int seekAndWriteFd(
14261 int fd, /* File descriptor to write to */
14262 i64 iOff, /* File offset to begin writing at */
14263 const void *pBuf, /* Copy data from this buffer to the file */
14264 int nBuf, /* Size of buffer pBuf in bytes */
14265 int *piErrno /* OUT: Error number if error occurs */
14266 ){
14267 int rc = 0; /* Value returned by system call */
14268
14269 assert( nBuf==(nBuf&0x1ffff) );
14270 assert( fd>2 );
14271 assert( piErrno!=0 );
14272 nBuf &= 0x1ffff;
14273 TIMER_START;
14274
14275 #if defined(USE_PREAD)
14276 do{ rc = (int)osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
14277 #elif defined(USE_PREAD64)
14278 do{ rc = (int)osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR);
14279 #else
14280 do{
14281 i64 iSeek = lseek(fd, iOff, SEEK_SET);
14282 SimulateIOError( iSeek = -1 );
14283 if( iSeek<0 ){
14284 rc = -1;
14285 break;
14286 }
14287 rc = osWrite(fd, pBuf, nBuf);
14288 }while( rc<0 && errno==EINTR );
14289 #endif
14290
14291 TIMER_END;
14292 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
14293
14294 if( rc<0 ) *piErrno = errno;
14295 return rc;
14296 }
14297
14298
14299 /*
14300 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
14301 ** Return the number of bytes actually read. Update the offset.
14302 **
14303 ** To avoid stomping the errno value on a failed write the lastErrno value
14304 ** is set before returning.
14305 */
14306 static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
14307 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
14308 }
14309
14310
14311 /*
14312 ** Write data from a buffer into a file. Return SQLITE_OK on success
14313 ** or some other error code on failure.
14314 */
14315 static int unixWrite(
14316 sqlite3_file *id,
14317 const void *pBuf,
14318 int amt,
14319 sqlite3_int64 offset
14320 ){
14321 unixFile *pFile = (unixFile*)id;
14322 int wrote = 0;
14323 assert( id );
14324 assert( amt>0 );
14325
14326 /* If this is a database file (not a journal, master-journal or temp
14327 ** file), the bytes in the locking range should never be read or written. */
14328 #if 0
14329 assert( pFile->pUnused==0
14330 || offset>=PENDING_BYTE+512
14331 || offset+amt<=PENDING_BYTE
14332 );
14333 #endif
14334
14335 #ifdef SQLITE_DEBUG
14336 /* If we are doing a normal write to a database file (as opposed to
14337 ** doing a hot-journal rollback or a write to some file other than a
14338 ** normal database file) then record the fact that the database
14339 ** has changed. If the transaction counter is modified, record that
14340 ** fact too.
14341 */
14342 if( pFile->inNormalWrite ){
14343 pFile->dbUpdate = 1; /* The database has been modified */
14344 if( offset<=24 && offset+amt>=27 ){
14345 int rc;
14346 char oldCntr[4];
14347 SimulateIOErrorBenign(1);
14348 rc = seekAndRead(pFile, 24, oldCntr, 4);
14349 SimulateIOErrorBenign(0);
14350 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
14351 pFile->transCntrChng = 1; /* The transaction counter has changed */
14352 }
14353 }
14354 }
14355 #endif
14356
14357 #if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
14358 /* Deal with as much of this write request as possible by transfering
14359 ** data from the memory mapping using memcpy(). */
14360 if( offset<pFile->mmapSize ){
14361 if( offset+amt <= pFile->mmapSize ){
14362 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
14363 return SQLITE_OK;
14364 }else{
14365 int nCopy = pFile->mmapSize - offset;
14366 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
14367 pBuf = &((u8 *)pBuf)[nCopy];
14368 amt -= nCopy;
14369 offset += nCopy;
14370 }
14371 }
14372 #endif
14373
14374 while( (wrote = seekAndWrite(pFile, offset, pBuf, amt))<amt && wrote>0 ){
14375 amt -= wrote;
14376 offset += wrote;
14377 pBuf = &((char*)pBuf)[wrote];
14378 }
14379 SimulateIOError(( wrote=(-1), amt=1 ));
14380 SimulateDiskfullError(( wrote=0, amt=1 ));
14381
14382 if( amt>wrote ){
14383 if( wrote<0 && pFile->lastErrno!=ENOSPC ){
14384 /* lastErrno set by seekAndWrite */
14385 return SQLITE_IOERR_WRITE;
14386 }else{
14387 storeLastErrno(pFile, 0); /* not a system error */
14388 return SQLITE_FULL;
14389 }
14390 }
14391
14392 return SQLITE_OK;
14393 }
14394
14395 #ifdef SQLITE_TEST
14396 /*
14397 ** Count the number of fullsyncs and normal syncs. This is used to test
14398 ** that syncs and fullsyncs are occurring at the right times.
14399 */
14400 SQLITE_API int sqlite3_sync_count = 0;
14401 SQLITE_API int sqlite3_fullsync_count = 0;
14402 #endif
14403
14404 /*
14405 ** We do not trust systems to provide a working fdatasync(). Some do.
14406 ** Others do no. To be safe, we will stick with the (slightly slower)
14407 ** fsync(). If you know that your system does support fdatasync() correctly,
14408 ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
14409 */
14410 #if !defined(fdatasync) && !HAVE_FDATASYNC
14411 # define fdatasync fsync
14412 #endif
14413
14414 /*
14415 ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
14416 ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
14417 ** only available on Mac OS X. But that could change.
14418 */
14419 #ifdef F_FULLFSYNC
14420 # define HAVE_FULLFSYNC 1
14421 #else
14422 # define HAVE_FULLFSYNC 0
14423 #endif
14424
14425
14426 /*
14427 ** The fsync() system call does not work as advertised on many
14428 ** unix systems. The following procedure is an attempt to make
14429 ** it work better.
14430 **
14431 ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
14432 ** for testing when we want to run through the test suite quickly.
14433 ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
14434 ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
14435 ** or power failure will likely corrupt the database file.
14436 **
14437 ** SQLite sets the dataOnly flag if the size of the file is unchanged.
14438 ** The idea behind dataOnly is that it should only write the file content
14439 ** to disk, not the inode. We only set dataOnly if the file size is
14440 ** unchanged since the file size is part of the inode. However,
14441 ** Ted Ts'o tells us that fdatasync() will also write the inode if the
14442 ** file size has changed. The only real difference between fdatasync()
14443 ** and fsync(), Ted tells us, is that fdatasync() will not flush the
14444 ** inode if the mtime or owner or other inode attributes have changed.
14445 ** We only care about the file size, not the other file attributes, so
14446 ** as far as SQLite is concerned, an fdatasync() is always adequate.
14447 ** So, we always use fdatasync() if it is available, regardless of
14448 ** the value of the dataOnly flag.
14449 */
14450 static int full_fsync(int fd, int fullSync, int dataOnly){
14451 int rc;
14452
14453 /* The following "ifdef/elif/else/" block has the same structure as
14454 ** the one below. It is replicated here solely to avoid cluttering
14455 ** up the real code with the UNUSED_PARAMETER() macros.
14456 */
14457 #ifdef SQLITE_NO_SYNC
14458 UNUSED_PARAMETER(fd);
14459 UNUSED_PARAMETER(fullSync);
14460 UNUSED_PARAMETER(dataOnly);
14461 #elif HAVE_FULLFSYNC
14462 UNUSED_PARAMETER(dataOnly);
14463 #else
14464 UNUSED_PARAMETER(fullSync);
14465 UNUSED_PARAMETER(dataOnly);
14466 #endif
14467
14468 /* Record the number of times that we do a normal fsync() and
14469 ** FULLSYNC. This is used during testing to verify that this procedure
14470 ** gets called with the correct arguments.
14471 */
14472 #ifdef SQLITE_TEST
14473 if( fullSync ) sqlite3_fullsync_count++;
14474 sqlite3_sync_count++;
14475 #endif
14476
14477 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
14478 ** no-op. But go ahead and call fstat() to validate the file
14479 ** descriptor as we need a method to provoke a failure during
14480 ** coverate testing.
14481 */
14482 #ifdef SQLITE_NO_SYNC
14483 {
14484 struct stat buf;
14485 rc = osFstat(fd, &buf);
14486 }
14487 #elif HAVE_FULLFSYNC
14488 if( fullSync ){
14489 rc = osFcntl(fd, F_FULLFSYNC, 0);
14490 }else{
14491 rc = 1;
14492 }
14493 /* If the FULLFSYNC failed, fall back to attempting an fsync().
14494 ** It shouldn't be possible for fullfsync to fail on the local
14495 ** file system (on OSX), so failure indicates that FULLFSYNC
14496 ** isn't supported for this file system. So, attempt an fsync
14497 ** and (for now) ignore the overhead of a superfluous fcntl call.
14498 ** It'd be better to detect fullfsync support once and avoid
14499 ** the fcntl call every time sync is called.
14500 */
14501 if( rc ) rc = fsync(fd);
14502
14503 #elif defined(__APPLE__)
14504 /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
14505 ** so currently we default to the macro that redefines fdatasync to fsync
14506 */
14507 rc = fsync(fd);
14508 #else
14509 rc = fdatasync(fd);
14510 #if OS_VXWORKS
14511 if( rc==-1 && errno==ENOTSUP ){
14512 rc = fsync(fd);
14513 }
14514 #endif /* OS_VXWORKS */
14515 #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
14516
14517 if( OS_VXWORKS && rc!= -1 ){
14518 rc = 0;
14519 }
14520 return rc;
14521 }
14522
14523 /*
14524 ** Open a file descriptor to the directory containing file zFilename.
14525 ** If successful, *pFd is set to the opened file descriptor and
14526 ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
14527 ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
14528 ** value.
14529 **
14530 ** The directory file descriptor is used for only one thing - to
14531 ** fsync() a directory to make sure file creation and deletion events
14532 ** are flushed to disk. Such fsyncs are not needed on newer
14533 ** journaling filesystems, but are required on older filesystems.
14534 **
14535 ** This routine can be overridden using the xSetSysCall interface.
14536 ** The ability to override this routine was added in support of the
14537 ** chromium sandbox. Opening a directory is a security risk (we are
14538 ** told) so making it overrideable allows the chromium sandbox to
14539 ** replace this routine with a harmless no-op. To make this routine
14540 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
14541 ** *pFd set to a negative number.
14542 **
14543 ** If SQLITE_OK is returned, the caller is responsible for closing
14544 ** the file descriptor *pFd using close().
14545 */
14546 static int openDirectory(const char *zFilename, int *pFd){
14547 int ii;
14548 int fd = -1;
14549 char zDirname[MAX_PATHNAME+1];
14550
14551 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
14552 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
14553 if( ii>0 ){
14554 zDirname[ii] = '\0';
14555 }else{
14556 if( zDirname[0]!='/' ) zDirname[0] = '.';
14557 zDirname[1] = 0;
14558 }
14559 fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
14560 if( fd>=0 ){
14561 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
14562 }
14563 *pFd = fd;
14564 if( fd>=0 ) return SQLITE_OK;
14565 return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname);
14566 }
14567
14568 /*
14569 ** Make sure all writes to a particular file are committed to disk.
14570 **
14571 ** If dataOnly==0 then both the file itself and its metadata (file
14572 ** size, access time, etc) are synced. If dataOnly!=0 then only the
14573 ** file data is synced.
14574 **
14575 ** Under Unix, also make sure that the directory entry for the file
14576 ** has been created by fsync-ing the directory that contains the file.
14577 ** If we do not do this and we encounter a power failure, the directory
14578 ** entry for the journal might not exist after we reboot. The next
14579 ** SQLite to access the file will not know that the journal exists (because
14580 ** the directory entry for the journal was never created) and the transaction
14581 ** will not roll back - possibly leading to database corruption.
14582 */
14583 static int unixSync(sqlite3_file *id, int flags){
14584 int rc;
14585 unixFile *pFile = (unixFile*)id;
14586
14587 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
14588 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
14589
14590 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
14591 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
14592 || (flags&0x0F)==SQLITE_SYNC_FULL
14593 );
14594
14595 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
14596 ** line is to test that doing so does not cause any problems.
14597 */
14598 SimulateDiskfullError( return SQLITE_FULL );
14599
14600 assert( pFile );
14601 OSTRACE(("SYNC %-3d\n", pFile->h));
14602 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
14603 SimulateIOError( rc=1 );
14604 if( rc ){
14605 storeLastErrno(pFile, errno);
14606 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
14607 }
14608
14609 /* Also fsync the directory containing the file if the DIRSYNC flag
14610 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
14611 ** are unable to fsync a directory, so ignore errors on the fsync.
14612 */
14613 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
14614 int dirfd;
14615 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
14616 HAVE_FULLFSYNC, isFullsync));
14617 rc = osOpenDirectory(pFile->zPath, &dirfd);
14618 if( rc==SQLITE_OK ){
14619 full_fsync(dirfd, 0, 0);
14620 robust_close(pFile, dirfd, __LINE__);
14621 }else{
14622 assert( rc==SQLITE_CANTOPEN );
14623 rc = SQLITE_OK;
14624 }
14625 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
14626 }
14627 return rc;
14628 }
14629
14630 /*
14631 ** Truncate an open file to a specified size
14632 */
14633 static int unixTruncate(sqlite3_file *id, i64 nByte){
14634 unixFile *pFile = (unixFile *)id;
14635 int rc;
14636 assert( pFile );
14637 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
14638
14639 /* If the user has configured a chunk-size for this file, truncate the
14640 ** file so that it consists of an integer number of chunks (i.e. the
14641 ** actual file size after the operation may be larger than the requested
14642 ** size).
14643 */
14644 if( pFile->szChunk>0 ){
14645 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
14646 }
14647
14648 rc = robust_ftruncate(pFile->h, nByte);
14649 if( rc ){
14650 storeLastErrno(pFile, errno);
14651 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
14652 }else{
14653 #ifdef SQLITE_DEBUG
14654 /* If we are doing a normal write to a database file (as opposed to
14655 ** doing a hot-journal rollback or a write to some file other than a
14656 ** normal database file) and we truncate the file to zero length,
14657 ** that effectively updates the change counter. This might happen
14658 ** when restoring a database using the backup API from a zero-length
14659 ** source.
14660 */
14661 if( pFile->inNormalWrite && nByte==0 ){
14662 pFile->transCntrChng = 1;
14663 }
14664 #endif
14665
14666 #if SQLITE_MAX_MMAP_SIZE>0
14667 /* If the file was just truncated to a size smaller than the currently
14668 ** mapped region, reduce the effective mapping size as well. SQLite will
14669 ** use read() and write() to access data beyond this point from now on.
14670 */
14671 if( nByte<pFile->mmapSize ){
14672 pFile->mmapSize = nByte;
14673 }
14674 #endif
14675
14676 return SQLITE_OK;
14677 }
14678 }
14679
14680 /*
14681 ** Determine the current size of a file in bytes
14682 */
14683 static int unixFileSize(sqlite3_file *id, i64 *pSize){
14684 int rc;
14685 struct stat buf;
14686 assert( id );
14687 rc = osFstat(((unixFile*)id)->h, &buf);
14688 SimulateIOError( rc=1 );
14689 if( rc!=0 ){
14690 storeLastErrno((unixFile*)id, errno);
14691 return SQLITE_IOERR_FSTAT;
14692 }
14693 *pSize = buf.st_size;
14694
14695 /* When opening a zero-size database, the findInodeInfo() procedure
14696 ** writes a single byte into that file in order to work around a bug
14697 ** in the OS-X msdos filesystem. In order to avoid problems with upper
14698 ** layers, we need to report this file size as zero even though it is
14699 ** really 1. Ticket #3260.
14700 */
14701 if( *pSize==1 ) *pSize = 0;
14702
14703
14704 return SQLITE_OK;
14705 }
14706
14707 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
14708 /*
14709 ** Handler for proxy-locking file-control verbs. Defined below in the
14710 ** proxying locking division.
14711 */
14712 static int proxyFileControl(sqlite3_file*,int,void*);
14713 #endif
14714
14715 /*
14716 ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
14717 ** file-control operation. Enlarge the database to nBytes in size
14718 ** (rounded up to the next chunk-size). If the database is already
14719 ** nBytes or larger, this routine is a no-op.
14720 */
14721 static int fcntlSizeHint(unixFile *pFile, i64 nByte){
14722 if( pFile->szChunk>0 ){
14723 i64 nSize; /* Required file size */
14724 struct stat buf; /* Used to hold return values of fstat() */
14725
14726 if( osFstat(pFile->h, &buf) ){
14727 return SQLITE_IOERR_FSTAT;
14728 }
14729
14730 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
14731 if( nSize>(i64)buf.st_size ){
14732
14733 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
14734 /* The code below is handling the return value of osFallocate()
14735 ** correctly. posix_fallocate() is defined to "returns zero on success,
14736 ** or an error number on failure". See the manpage for details. */
14737 int err;
14738 do{
14739 err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
14740 }while( err==EINTR );
14741 if( err ) return SQLITE_IOERR_WRITE;
14742 #else
14743 /* If the OS does not have posix_fallocate(), fake it. Write a
14744 ** single byte to the last byte in each block that falls entirely
14745 ** within the extended region. Then, if required, a single byte
14746 ** at offset (nSize-1), to set the size of the file correctly.
14747 ** This is a similar technique to that used by glibc on systems
14748 ** that do not have a real fallocate() call.
14749 */
14750 int nBlk = buf.st_blksize; /* File-system block size */
14751 int nWrite = 0; /* Number of bytes written by seekAndWrite */
14752 i64 iWrite; /* Next offset to write to */
14753
14754 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
14755 assert( iWrite>=buf.st_size );
14756 assert( ((iWrite+1)%nBlk)==0 );
14757 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
14758 if( iWrite>=nSize ) iWrite = nSize - 1;
14759 nWrite = seekAndWrite(pFile, iWrite, "", 1);
14760 if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
14761 }
14762 #endif
14763 }
14764 }
14765
14766 #if SQLITE_MAX_MMAP_SIZE>0
14767 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
14768 int rc;
14769 if( pFile->szChunk<=0 ){
14770 if( robust_ftruncate(pFile->h, nByte) ){
14771 storeLastErrno(pFile, errno);
14772 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
14773 }
14774 }
14775
14776 rc = unixMapfile(pFile, nByte);
14777 return rc;
14778 }
14779 #endif
14780
14781 return SQLITE_OK;
14782 }
14783
14784 /*
14785 ** If *pArg is initially negative then this is a query. Set *pArg to
14786 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
14787 **
14788 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
14789 */
14790 static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
14791 if( *pArg<0 ){
14792 *pArg = (pFile->ctrlFlags & mask)!=0;
14793 }else if( (*pArg)==0 ){
14794 pFile->ctrlFlags &= ~mask;
14795 }else{
14796 pFile->ctrlFlags |= mask;
14797 }
14798 }
14799
14800 /* Forward declaration */
14801 static int unixGetTempname(int nBuf, char *zBuf);
14802
14803 /*
14804 ** Information and control of an open file handle.
14805 */
14806 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
14807 unixFile *pFile = (unixFile*)id;
14808 switch( op ){
14809 case SQLITE_FCNTL_LOCKSTATE: {
14810 *(int*)pArg = pFile->eFileLock;
14811 return SQLITE_OK;
14812 }
14813 case SQLITE_FCNTL_LAST_ERRNO: {
14814 *(int*)pArg = pFile->lastErrno;
14815 return SQLITE_OK;
14816 }
14817 case SQLITE_FCNTL_CHUNK_SIZE: {
14818 pFile->szChunk = *(int *)pArg;
14819 return SQLITE_OK;
14820 }
14821 case SQLITE_FCNTL_SIZE_HINT: {
14822 int rc;
14823 SimulateIOErrorBenign(1);
14824 rc = fcntlSizeHint(pFile, *(i64 *)pArg);
14825 SimulateIOErrorBenign(0);
14826 return rc;
14827 }
14828 case SQLITE_FCNTL_PERSIST_WAL: {
14829 unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg);
14830 return SQLITE_OK;
14831 }
14832 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
14833 unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg);
14834 return SQLITE_OK;
14835 }
14836 case SQLITE_FCNTL_VFSNAME: {
14837 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
14838 return SQLITE_OK;
14839 }
14840 case SQLITE_FCNTL_TEMPFILENAME: {
14841 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
14842 if( zTFile ){
14843 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
14844 *(char**)pArg = zTFile;
14845 }
14846 return SQLITE_OK;
14847 }
14848 case SQLITE_FCNTL_HAS_MOVED: {
14849 *(int*)pArg = fileHasMoved(pFile);
14850 return SQLITE_OK;
14851 }
14852 #if SQLITE_MAX_MMAP_SIZE>0
14853 case SQLITE_FCNTL_MMAP_SIZE: {
14854 i64 newLimit = *(i64*)pArg;
14855 int rc = SQLITE_OK;
14856 if( newLimit>sqlite3GlobalConfig.mxMmap ){
14857 newLimit = sqlite3GlobalConfig.mxMmap;
14858 }
14859 *(i64*)pArg = pFile->mmapSizeMax;
14860 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
14861 pFile->mmapSizeMax = newLimit;
14862 if( pFile->mmapSize>0 ){
14863 unixUnmapfile(pFile);
14864 rc = unixMapfile(pFile, -1);
14865 }
14866 }
14867 return rc;
14868 }
14869 #endif
14870 #ifdef SQLITE_DEBUG
14871 /* The pager calls this method to signal that it has done
14872 ** a rollback and that the database is therefore unchanged and
14873 ** it hence it is OK for the transaction change counter to be
14874 ** unchanged.
14875 */
14876 case SQLITE_FCNTL_DB_UNCHANGED: {
14877 ((unixFile*)id)->dbUpdate = 0;
14878 return SQLITE_OK;
14879 }
14880 #endif
14881 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
14882 case SQLITE_FCNTL_SET_LOCKPROXYFILE:
14883 case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
14884 return proxyFileControl(id,op,pArg);
14885 }
14886 #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
14887 }
14888 return SQLITE_NOTFOUND;
14889 }
14890
14891 /*
14892 ** Return the sector size in bytes of the underlying block device for
14893 ** the specified file. This is almost always 512 bytes, but may be
14894 ** larger for some devices.
14895 **
14896 ** SQLite code assumes this function cannot fail. It also assumes that
14897 ** if two files are created in the same file-system directory (i.e.
14898 ** a database and its journal file) that the sector size will be the
14899 ** same for both.
14900 */
14901 #ifndef __QNXNTO__
14902 static int unixSectorSize(sqlite3_file *NotUsed){
14903 UNUSED_PARAMETER(NotUsed);
14904 return SQLITE_DEFAULT_SECTOR_SIZE;
14905 }
14906 #endif
14907
14908 /*
14909 ** The following version of unixSectorSize() is optimized for QNX.
14910 */
14911 #ifdef __QNXNTO__
14912 #include <sys/dcmd_blk.h>
14913 #include <sys/statvfs.h>
14914 static int unixSectorSize(sqlite3_file *id){
14915 unixFile *pFile = (unixFile*)id;
14916 if( pFile->sectorSize == 0 ){
14917 struct statvfs fsInfo;
14918
14919 /* Set defaults for non-supported filesystems */
14920 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
14921 pFile->deviceCharacteristics = 0;
14922 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
14923 return pFile->sectorSize;
14924 }
14925
14926 if( !strcmp(fsInfo.f_basetype, "tmp") ) {
14927 pFile->sectorSize = fsInfo.f_bsize;
14928 pFile->deviceCharacteristics =
14929 SQLITE_IOCAP_ATOMIC4K | /* All ram filesystem writes are atomic */
14930 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
14931 ** the write succeeds */
14932 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
14933 ** so it is ordered */
14934 0;
14935 }else if( strstr(fsInfo.f_basetype, "etfs") ){
14936 pFile->sectorSize = fsInfo.f_bsize;
14937 pFile->deviceCharacteristics =
14938 /* etfs cluster size writes are atomic */
14939 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
14940 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
14941 ** the write succeeds */
14942 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
14943 ** so it is ordered */
14944 0;
14945 }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
14946 pFile->sectorSize = fsInfo.f_bsize;
14947 pFile->deviceCharacteristics =
14948 SQLITE_IOCAP_ATOMIC | /* All filesystem writes are atomic */
14949 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
14950 ** the write succeeds */
14951 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
14952 ** so it is ordered */
14953 0;
14954 }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
14955 pFile->sectorSize = fsInfo.f_bsize;
14956 pFile->deviceCharacteristics =
14957 /* full bitset of atomics from max sector size and smaller */
14958 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
14959 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
14960 ** so it is ordered */
14961 0;
14962 }else if( strstr(fsInfo.f_basetype, "dos") ){
14963 pFile->sectorSize = fsInfo.f_bsize;
14964 pFile->deviceCharacteristics =
14965 /* full bitset of atomics from max sector size and smaller */
14966 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
14967 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
14968 ** so it is ordered */
14969 0;
14970 }else{
14971 pFile->deviceCharacteristics =
14972 SQLITE_IOCAP_ATOMIC512 | /* blocks are atomic */
14973 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
14974 ** the write succeeds */
14975 0;
14976 }
14977 }
14978 /* Last chance verification. If the sector size isn't a multiple of 512
14979 ** then it isn't valid.*/
14980 if( pFile->sectorSize % 512 != 0 ){
14981 pFile->deviceCharacteristics = 0;
14982 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
14983 }
14984 return pFile->sectorSize;
14985 }
14986 #endif /* __QNXNTO__ */
14987
14988 /*
14989 ** Return the device characteristics for the file.
14990 **
14991 ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
14992 ** However, that choice is controversial since technically the underlying
14993 ** file system does not always provide powersafe overwrites. (In other
14994 ** words, after a power-loss event, parts of the file that were never
14995 ** written might end up being altered.) However, non-PSOW behavior is very,
14996 ** very rare. And asserting PSOW makes a large reduction in the amount
14997 ** of required I/O for journaling, since a lot of padding is eliminated.
14998 ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
14999 ** available to turn it off and URI query parameter available to turn it off.
15000 */
15001 static int unixDeviceCharacteristics(sqlite3_file *id){
15002 unixFile *p = (unixFile*)id;
15003 int rc = 0;
15004 #ifdef __QNXNTO__
15005 if( p->sectorSize==0 ) unixSectorSize(id);
15006 rc = p->deviceCharacteristics;
15007 #endif
15008 if( p->ctrlFlags & UNIXFILE_PSOW ){
15009 rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
15010 }
15011 return rc;
15012 }
15013
15014 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
15015
15016 /*
15017 ** Return the system page size.
15018 **
15019 ** This function should not be called directly by other code in this file.
15020 ** Instead, it should be called via macro osGetpagesize().
15021 */
15022 static int unixGetpagesize(void){
15023 #if OS_VXWORKS
15024 return 1024;
15025 #elif defined(_BSD_SOURCE)
15026 return getpagesize();
15027 #else
15028 return (int)sysconf(_SC_PAGESIZE);
15029 #endif
15030 }
15031
15032 #endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */
15033
15034 #ifndef SQLITE_OMIT_WAL
15035
15036 /*
15037 ** Object used to represent an shared memory buffer.
15038 **
15039 ** When multiple threads all reference the same wal-index, each thread
15040 ** has its own unixShm object, but they all point to a single instance
15041 ** of this unixShmNode object. In other words, each wal-index is opened
15042 ** only once per process.
15043 **
15044 ** Each unixShmNode object is connected to a single unixInodeInfo object.
15045 ** We could coalesce this object into unixInodeInfo, but that would mean
15046 ** every open file that does not use shared memory (in other words, most
15047 ** open files) would have to carry around this extra information. So
15048 ** the unixInodeInfo object contains a pointer to this unixShmNode object
15049 ** and the unixShmNode object is created only when needed.
15050 **
15051 ** unixMutexHeld() must be true when creating or destroying
15052 ** this object or while reading or writing the following fields:
15053 **
15054 ** nRef
15055 **
15056 ** The following fields are read-only after the object is created:
15057 **
15058 ** fid
15059 ** zFilename
15060 **
15061 ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
15062 ** unixMutexHeld() is true when reading or writing any other field
15063 ** in this structure.
15064 */
15065 struct unixShmNode {
15066 unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */
15067 sqlite3_mutex *mutex; /* Mutex to access this object */
15068 char *zFilename; /* Name of the mmapped file */
15069 int h; /* Open file descriptor */
15070 int szRegion; /* Size of shared-memory regions */
15071 u16 nRegion; /* Size of array apRegion */
15072 u8 isReadonly; /* True if read-only */
15073 char **apRegion; /* Array of mapped shared-memory regions */
15074 int nRef; /* Number of unixShm objects pointing to this */
15075 unixShm *pFirst; /* All unixShm objects pointing to this */
15076 #ifdef SQLITE_DEBUG
15077 u8 exclMask; /* Mask of exclusive locks held */
15078 u8 sharedMask; /* Mask of shared locks held */
15079 u8 nextShmId; /* Next available unixShm.id value */
15080 #endif
15081 };
15082
15083 /*
15084 ** Structure used internally by this VFS to record the state of an
15085 ** open shared memory connection.
15086 **
15087 ** The following fields are initialized when this object is created and
15088 ** are read-only thereafter:
15089 **
15090 ** unixShm.pFile
15091 ** unixShm.id
15092 **
15093 ** All other fields are read/write. The unixShm.pFile->mutex must be held
15094 ** while accessing any read/write fields.
15095 */
15096 struct unixShm {
15097 unixShmNode *pShmNode; /* The underlying unixShmNode object */
15098 unixShm *pNext; /* Next unixShm with the same unixShmNode */
15099 u8 hasMutex; /* True if holding the unixShmNode mutex */
15100 u8 id; /* Id of this connection within its unixShmNode */
15101 u16 sharedMask; /* Mask of shared locks held */
15102 u16 exclMask; /* Mask of exclusive locks held */
15103 };
15104
15105 /*
15106 ** Constants used for locking
15107 */
15108 #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
15109 #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
15110
15111 /*
15112 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
15113 **
15114 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
15115 ** otherwise.
15116 */
15117 static int unixShmSystemLock(
15118 unixFile *pFile, /* Open connection to the WAL file */
15119 int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */
15120 int ofst, /* First byte of the locking range */
15121 int n /* Number of bytes to lock */
15122 ){
15123 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
15124 struct flock f; /* The posix advisory locking structure */
15125 int rc = SQLITE_OK; /* Result code form fcntl() */
15126
15127 /* Access to the unixShmNode object is serialized by the caller */
15128 pShmNode = pFile->pInode->pShmNode;
15129 assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
15130
15131 /* Shared locks never span more than one byte */
15132 assert( n==1 || lockType!=F_RDLCK );
15133
15134 /* Locks are within range */
15135 assert( n>=1 && n<=SQLITE_SHM_NLOCK );
15136
15137 if( pShmNode->h>=0 ){
15138 /* Initialize the locking parameters */
15139 memset(&f, 0, sizeof(f));
15140 f.l_type = lockType;
15141 f.l_whence = SEEK_SET;
15142 f.l_start = ofst;
15143 f.l_len = n;
15144
15145 rc = osFcntl(pShmNode->h, F_SETLK, &f);
15146 rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
15147 }
15148
15149 /* Update the global lock state and do debug tracing */
15150 #ifdef SQLITE_DEBUG
15151 { u16 mask;
15152 OSTRACE(("SHM-LOCK "));
15153 mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
15154 if( rc==SQLITE_OK ){
15155 if( lockType==F_UNLCK ){
15156 OSTRACE(("unlock %d ok", ofst));
15157 pShmNode->exclMask &= ~mask;
15158 pShmNode->sharedMask &= ~mask;
15159 }else if( lockType==F_RDLCK ){
15160 OSTRACE(("read-lock %d ok", ofst));
15161 pShmNode->exclMask &= ~mask;
15162 pShmNode->sharedMask |= mask;
15163 }else{
15164 assert( lockType==F_WRLCK );
15165 OSTRACE(("write-lock %d ok", ofst));
15166 pShmNode->exclMask |= mask;
15167 pShmNode->sharedMask &= ~mask;
15168 }
15169 }else{
15170 if( lockType==F_UNLCK ){
15171 OSTRACE(("unlock %d failed", ofst));
15172 }else if( lockType==F_RDLCK ){
15173 OSTRACE(("read-lock failed"));
15174 }else{
15175 assert( lockType==F_WRLCK );
15176 OSTRACE(("write-lock %d failed", ofst));
15177 }
15178 }
15179 OSTRACE((" - afterwards %03x,%03x\n",
15180 pShmNode->sharedMask, pShmNode->exclMask));
15181 }
15182 #endif
15183
15184 return rc;
15185 }
15186
15187 /*
15188 ** Return the minimum number of 32KB shm regions that should be mapped at
15189 ** a time, assuming that each mapping must be an integer multiple of the
15190 ** current system page-size.
15191 **
15192 ** Usually, this is 1. The exception seems to be systems that are configured
15193 ** to use 64KB pages - in this case each mapping must cover at least two
15194 ** shm regions.
15195 */
15196 static int unixShmRegionPerMap(void){
15197 int shmsz = 32*1024; /* SHM region size */
15198 int pgsz = osGetpagesize(); /* System page size */
15199 assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */
15200 if( pgsz<shmsz ) return 1;
15201 return pgsz/shmsz;
15202 }
15203
15204 /*
15205 ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
15206 **
15207 ** This is not a VFS shared-memory method; it is a utility function called
15208 ** by VFS shared-memory methods.
15209 */
15210 static void unixShmPurge(unixFile *pFd){
15211 unixShmNode *p = pFd->pInode->pShmNode;
15212 assert( unixMutexHeld() );
15213 if( p && ALWAYS(p->nRef==0) ){
15214 int nShmPerMap = unixShmRegionPerMap();
15215 int i;
15216 assert( p->pInode==pFd->pInode );
15217 sqlite3_mutex_free(p->mutex);
15218 for(i=0; i<p->nRegion; i+=nShmPerMap){
15219 if( p->h>=0 ){
15220 osMunmap(p->apRegion[i], p->szRegion);
15221 }else{
15222 sqlite3_free(p->apRegion[i]);
15223 }
15224 }
15225 sqlite3_free(p->apRegion);
15226 if( p->h>=0 ){
15227 robust_close(pFd, p->h, __LINE__);
15228 p->h = -1;
15229 }
15230 p->pInode->pShmNode = 0;
15231 sqlite3_free(p);
15232 }
15233 }
15234
15235 /*
15236 ** Open a shared-memory area associated with open database file pDbFd.
15237 ** This particular implementation uses mmapped files.
15238 **
15239 ** The file used to implement shared-memory is in the same directory
15240 ** as the open database file and has the same name as the open database
15241 ** file with the "-shm" suffix added. For example, if the database file
15242 ** is "/home/user1/config.db" then the file that is created and mmapped
15243 ** for shared memory will be called "/home/user1/config.db-shm".
15244 **
15245 ** Another approach to is to use files in /dev/shm or /dev/tmp or an
15246 ** some other tmpfs mount. But if a file in a different directory
15247 ** from the database file is used, then differing access permissions
15248 ** or a chroot() might cause two different processes on the same
15249 ** database to end up using different files for shared memory -
15250 ** meaning that their memory would not really be shared - resulting
15251 ** in database corruption. Nevertheless, this tmpfs file usage
15252 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
15253 ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
15254 ** option results in an incompatible build of SQLite; builds of SQLite
15255 ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
15256 ** same database file at the same time, database corruption will likely
15257 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
15258 ** "unsupported" and may go away in a future SQLite release.
15259 **
15260 ** When opening a new shared-memory file, if no other instances of that
15261 ** file are currently open, in this process or in other processes, then
15262 ** the file must be truncated to zero length or have its header cleared.
15263 **
15264 ** If the original database file (pDbFd) is using the "unix-excl" VFS
15265 ** that means that an exclusive lock is held on the database file and
15266 ** that no other processes are able to read or write the database. In
15267 ** that case, we do not really need shared memory. No shared memory
15268 ** file is created. The shared memory will be simulated with heap memory.
15269 */
15270 static int unixOpenSharedMemory(unixFile *pDbFd){
15271 struct unixShm *p = 0; /* The connection to be opened */
15272 struct unixShmNode *pShmNode; /* The underlying mmapped file */
15273 int rc; /* Result code */
15274 unixInodeInfo *pInode; /* The inode of fd */
15275 char *zShmFilename; /* Name of the file used for SHM */
15276 int nShmFilename; /* Size of the SHM filename in bytes */
15277
15278 /* Allocate space for the new unixShm object. */
15279 p = sqlite3_malloc64( sizeof(*p) );
15280 if( p==0 ) return SQLITE_NOMEM_BKPT;
15281 memset(p, 0, sizeof(*p));
15282 assert( pDbFd->pShm==0 );
15283
15284 /* Check to see if a unixShmNode object already exists. Reuse an existing
15285 ** one if present. Create a new one if necessary.
15286 */
15287 unixEnterMutex();
15288 pInode = pDbFd->pInode;
15289 pShmNode = pInode->pShmNode;
15290 if( pShmNode==0 ){
15291 struct stat sStat; /* fstat() info for database file */
15292 #ifndef SQLITE_SHM_DIRECTORY
15293 const char *zBasePath = pDbFd->zPath;
15294 #endif
15295
15296 /* Call fstat() to figure out the permissions on the database file. If
15297 ** a new *-shm file is created, an attempt will be made to create it
15298 ** with the same permissions.
15299 */
15300 if( osFstat(pDbFd->h, &sStat) ){
15301 rc = SQLITE_IOERR_FSTAT;
15302 goto shm_open_err;
15303 }
15304
15305 #ifdef SQLITE_SHM_DIRECTORY
15306 nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
15307 #else
15308 nShmFilename = 6 + (int)strlen(zBasePath);
15309 #endif
15310 pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename );
15311 if( pShmNode==0 ){
15312 rc = SQLITE_NOMEM_BKPT;
15313 goto shm_open_err;
15314 }
15315 memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
15316 zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
15317 #ifdef SQLITE_SHM_DIRECTORY
15318 sqlite3_snprintf(nShmFilename, zShmFilename,
15319 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
15320 (u32)sStat.st_ino, (u32)sStat.st_dev);
15321 #else
15322 sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", zBasePath);
15323 sqlite3FileSuffix3(pDbFd->zPath, zShmFilename);
15324 #endif
15325 pShmNode->h = -1;
15326 pDbFd->pInode->pShmNode = pShmNode;
15327 pShmNode->pInode = pDbFd->pInode;
15328 if( sqlite3GlobalConfig.bCoreMutex ){
15329 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
15330 if( pShmNode->mutex==0 ){
15331 rc = SQLITE_NOMEM_BKPT;
15332 goto shm_open_err;
15333 }
15334 }
15335
15336 if( pInode->bProcessLock==0 ){
15337 int openFlags = O_RDWR | O_CREAT;
15338 if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
15339 openFlags = O_RDONLY;
15340 pShmNode->isReadonly = 1;
15341 }
15342 pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));
15343 if( pShmNode->h<0 ){
15344 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
15345 goto shm_open_err;
15346 }
15347
15348 /* If this process is running as root, make sure that the SHM file
15349 ** is owned by the same user that owns the original database. Otherwise,
15350 ** the original owner will not be able to connect.
15351 */
15352 robustFchown(pShmNode->h, sStat.st_uid, sStat.st_gid);
15353
15354 /* Check to see if another process is holding the dead-man switch.
15355 ** If not, truncate the file to zero length.
15356 */
15357 rc = SQLITE_OK;
15358 if( unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
15359 if( robust_ftruncate(pShmNode->h, 0) ){
15360 rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
15361 }
15362 }
15363 if( rc==SQLITE_OK ){
15364 rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1);
15365 }
15366 if( rc ) goto shm_open_err;
15367 }
15368 }
15369
15370 /* Make the new connection a child of the unixShmNode */
15371 p->pShmNode = pShmNode;
15372 #ifdef SQLITE_DEBUG
15373 p->id = pShmNode->nextShmId++;
15374 #endif
15375 pShmNode->nRef++;
15376 pDbFd->pShm = p;
15377 unixLeaveMutex();
15378
15379 /* The reference count on pShmNode has already been incremented under
15380 ** the cover of the unixEnterMutex() mutex and the pointer from the
15381 ** new (struct unixShm) object to the pShmNode has been set. All that is
15382 ** left to do is to link the new object into the linked list starting
15383 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
15384 ** mutex.
15385 */
15386 sqlite3_mutex_enter(pShmNode->mutex);
15387 p->pNext = pShmNode->pFirst;
15388 pShmNode->pFirst = p;
15389 sqlite3_mutex_leave(pShmNode->mutex);
15390 return SQLITE_OK;
15391
15392 /* Jump here on any error */
15393 shm_open_err:
15394 unixShmPurge(pDbFd); /* This call frees pShmNode if required */
15395 sqlite3_free(p);
15396 unixLeaveMutex();
15397 return rc;
15398 }
15399
15400 /*
15401 ** This function is called to obtain a pointer to region iRegion of the
15402 ** shared-memory associated with the database file fd. Shared-memory regions
15403 ** are numbered starting from zero. Each shared-memory region is szRegion
15404 ** bytes in size.
15405 **
15406 ** If an error occurs, an error code is returned and *pp is set to NULL.
15407 **
15408 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
15409 ** region has not been allocated (by any client, including one running in a
15410 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
15411 ** bExtend is non-zero and the requested shared-memory region has not yet
15412 ** been allocated, it is allocated by this function.
15413 **
15414 ** If the shared-memory region has already been allocated or is allocated by
15415 ** this call as described above, then it is mapped into this processes
15416 ** address space (if it is not already), *pp is set to point to the mapped
15417 ** memory and SQLITE_OK returned.
15418 */
15419 static int unixShmMap(
15420 sqlite3_file *fd, /* Handle open on database file */
15421 int iRegion, /* Region to retrieve */
15422 int szRegion, /* Size of regions */
15423 int bExtend, /* True to extend file if necessary */
15424 void volatile **pp /* OUT: Mapped memory */
15425 ){
15426 unixFile *pDbFd = (unixFile*)fd;
15427 unixShm *p;
15428 unixShmNode *pShmNode;
15429 int rc = SQLITE_OK;
15430 int nShmPerMap = unixShmRegionPerMap();
15431 int nReqRegion;
15432
15433 /* If the shared-memory file has not yet been opened, open it now. */
15434 if( pDbFd->pShm==0 ){
15435 rc = unixOpenSharedMemory(pDbFd);
15436 if( rc!=SQLITE_OK ) return rc;
15437 }
15438
15439 p = pDbFd->pShm;
15440 pShmNode = p->pShmNode;
15441 sqlite3_mutex_enter(pShmNode->mutex);
15442 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
15443 assert( pShmNode->pInode==pDbFd->pInode );
15444 assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
15445 assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
15446
15447 /* Minimum number of regions required to be mapped. */
15448 nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
15449
15450 if( pShmNode->nRegion<nReqRegion ){
15451 char **apNew; /* New apRegion[] array */
15452 int nByte = nReqRegion*szRegion; /* Minimum required file size */
15453 struct stat sStat; /* Used by fstat() */
15454
15455 pShmNode->szRegion = szRegion;
15456
15457 if( pShmNode->h>=0 ){
15458 /* The requested region is not mapped into this processes address space.
15459 ** Check to see if it has been allocated (i.e. if the wal-index file is
15460 ** large enough to contain the requested region).
15461 */
15462 if( osFstat(pShmNode->h, &sStat) ){
15463 rc = SQLITE_IOERR_SHMSIZE;
15464 goto shmpage_out;
15465 }
15466
15467 if( sStat.st_size<nByte ){
15468 /* The requested memory region does not exist. If bExtend is set to
15469 ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
15470 */
15471 if( !bExtend ){
15472 goto shmpage_out;
15473 }
15474
15475 /* Alternatively, if bExtend is true, extend the file. Do this by
15476 ** writing a single byte to the end of each (OS) page being
15477 ** allocated or extended. Technically, we need only write to the
15478 ** last page in order to extend the file. But writing to all new
15479 ** pages forces the OS to allocate them immediately, which reduces
15480 ** the chances of SIGBUS while accessing the mapped region later on.
15481 */
15482 else{
15483 static const int pgsz = 4096;
15484 int iPg;
15485
15486 /* Write to the last byte of each newly allocated or extended page */
15487 assert( (nByte % pgsz)==0 );
15488 for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
15489 int x = 0;
15490 if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, &x)!=1 ){
15491 const char *zFile = pShmNode->zFilename;
15492 rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile);
15493 goto shmpage_out;
15494 }
15495 }
15496 }
15497 }
15498 }
15499
15500 /* Map the requested memory region into this processes address space. */
15501 apNew = (char **)sqlite3_realloc(
15502 pShmNode->apRegion, nReqRegion*sizeof(char *)
15503 );
15504 if( !apNew ){
15505 rc = SQLITE_IOERR_NOMEM_BKPT;
15506 goto shmpage_out;
15507 }
15508 pShmNode->apRegion = apNew;
15509 while( pShmNode->nRegion<nReqRegion ){
15510 int nMap = szRegion*nShmPerMap;
15511 int i;
15512 void *pMem;
15513 if( pShmNode->h>=0 ){
15514 pMem = osMmap(0, nMap,
15515 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
15516 MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
15517 );
15518 if( pMem==MAP_FAILED ){
15519 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
15520 goto shmpage_out;
15521 }
15522 }else{
15523 pMem = sqlite3_malloc64(szRegion);
15524 if( pMem==0 ){
15525 rc = SQLITE_NOMEM_BKPT;
15526 goto shmpage_out;
15527 }
15528 memset(pMem, 0, szRegion);
15529 }
15530
15531 for(i=0; i<nShmPerMap; i++){
15532 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
15533 }
15534 pShmNode->nRegion += nShmPerMap;
15535 }
15536 }
15537
15538 shmpage_out:
15539 if( pShmNode->nRegion>iRegion ){
15540 *pp = pShmNode->apRegion[iRegion];
15541 }else{
15542 *pp = 0;
15543 }
15544 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
15545 sqlite3_mutex_leave(pShmNode->mutex);
15546 return rc;
15547 }
15548
15549 /*
15550 ** Change the lock state for a shared-memory segment.
15551 **
15552 ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
15553 ** different here than in posix. In xShmLock(), one can go from unlocked
15554 ** to shared and back or from unlocked to exclusive and back. But one may
15555 ** not go from shared to exclusive or from exclusive to shared.
15556 */
15557 static int unixShmLock(
15558 sqlite3_file *fd, /* Database file holding the shared memory */
15559 int ofst, /* First lock to acquire or release */
15560 int n, /* Number of locks to acquire or release */
15561 int flags /* What to do with the lock */
15562 ){
15563 unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */
15564 unixShm *p = pDbFd->pShm; /* The shared memory being locked */
15565 unixShm *pX; /* For looping over all siblings */
15566 unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */
15567 int rc = SQLITE_OK; /* Result code */
15568 u16 mask; /* Mask of locks to take or release */
15569
15570 assert( pShmNode==pDbFd->pInode->pShmNode );
15571 assert( pShmNode->pInode==pDbFd->pInode );
15572 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
15573 assert( n>=1 );
15574 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
15575 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
15576 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
15577 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
15578 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
15579 assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
15580 assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
15581
15582 mask = (1<<(ofst+n)) - (1<<ofst);
15583 assert( n>1 || mask==(1<<ofst) );
15584 sqlite3_mutex_enter(pShmNode->mutex);
15585 if( flags & SQLITE_SHM_UNLOCK ){
15586 u16 allMask = 0; /* Mask of locks held by siblings */
15587
15588 /* See if any siblings hold this same lock */
15589 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
15590 if( pX==p ) continue;
15591 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
15592 allMask |= pX->sharedMask;
15593 }
15594
15595 /* Unlock the system-level locks */
15596 if( (mask & allMask)==0 ){
15597 rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n);
15598 }else{
15599 rc = SQLITE_OK;
15600 }
15601
15602 /* Undo the local locks */
15603 if( rc==SQLITE_OK ){
15604 p->exclMask &= ~mask;
15605 p->sharedMask &= ~mask;
15606 }
15607 }else if( flags & SQLITE_SHM_SHARED ){
15608 u16 allShared = 0; /* Union of locks held by connections other than "p" */
15609
15610 /* Find out which shared locks are already held by sibling connections.
15611 ** If any sibling already holds an exclusive lock, go ahead and return
15612 ** SQLITE_BUSY.
15613 */
15614 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
15615 if( (pX->exclMask & mask)!=0 ){
15616 rc = SQLITE_BUSY;
15617 break;
15618 }
15619 allShared |= pX->sharedMask;
15620 }
15621
15622 /* Get shared locks at the system level, if necessary */
15623 if( rc==SQLITE_OK ){
15624 if( (allShared & mask)==0 ){
15625 rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n);
15626 }else{
15627 rc = SQLITE_OK;
15628 }
15629 }
15630
15631 /* Get the local shared locks */
15632 if( rc==SQLITE_OK ){
15633 p->sharedMask |= mask;
15634 }
15635 }else{
15636 /* Make sure no sibling connections hold locks that will block this
15637 ** lock. If any do, return SQLITE_BUSY right away.
15638 */
15639 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
15640 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
15641 rc = SQLITE_BUSY;
15642 break;
15643 }
15644 }
15645
15646 /* Get the exclusive locks at the system level. Then if successful
15647 ** also mark the local connection as being locked.
15648 */
15649 if( rc==SQLITE_OK ){
15650 rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n);
15651 if( rc==SQLITE_OK ){
15652 assert( (p->sharedMask & mask)==0 );
15653 p->exclMask |= mask;
15654 }
15655 }
15656 }
15657 sqlite3_mutex_leave(pShmNode->mutex);
15658 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
15659 p->id, osGetpid(0), p->sharedMask, p->exclMask));
15660 return rc;
15661 }
15662
15663 /*
15664 ** Implement a memory barrier or memory fence on shared memory.
15665 **
15666 ** All loads and stores begun before the barrier must complete before
15667 ** any load or store begun after the barrier.
15668 */
15669 static void unixShmBarrier(
15670 sqlite3_file *fd /* Database file holding the shared memory */
15671 ){
15672 UNUSED_PARAMETER(fd);
15673 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
15674 unixEnterMutex(); /* Also mutex, for redundancy */
15675 unixLeaveMutex();
15676 }
15677
15678 /*
15679 ** Close a connection to shared-memory. Delete the underlying
15680 ** storage if deleteFlag is true.
15681 **
15682 ** If there is no shared memory associated with the connection then this
15683 ** routine is a harmless no-op.
15684 */
15685 static int unixShmUnmap(
15686 sqlite3_file *fd, /* The underlying database file */
15687 int deleteFlag /* Delete shared-memory if true */
15688 ){
15689 unixShm *p; /* The connection to be closed */
15690 unixShmNode *pShmNode; /* The underlying shared-memory file */
15691 unixShm **pp; /* For looping over sibling connections */
15692 unixFile *pDbFd; /* The underlying database file */
15693
15694 pDbFd = (unixFile*)fd;
15695 p = pDbFd->pShm;
15696 if( p==0 ) return SQLITE_OK;
15697 pShmNode = p->pShmNode;
15698
15699 assert( pShmNode==pDbFd->pInode->pShmNode );
15700 assert( pShmNode->pInode==pDbFd->pInode );
15701
15702 /* Remove connection p from the set of connections associated
15703 ** with pShmNode */
15704 sqlite3_mutex_enter(pShmNode->mutex);
15705 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
15706 *pp = p->pNext;
15707
15708 /* Free the connection p */
15709 sqlite3_free(p);
15710 pDbFd->pShm = 0;
15711 sqlite3_mutex_leave(pShmNode->mutex);
15712
15713 /* If pShmNode->nRef has reached 0, then close the underlying
15714 ** shared-memory file, too */
15715 unixEnterMutex();
15716 assert( pShmNode->nRef>0 );
15717 pShmNode->nRef--;
15718 if( pShmNode->nRef==0 ){
15719 if( deleteFlag && pShmNode->h>=0 ){
15720 osUnlink(pShmNode->zFilename);
15721 }
15722 unixShmPurge(pDbFd);
15723 }
15724 unixLeaveMutex();
15725
15726 return SQLITE_OK;
15727 }
15728
15729
15730 #else
15731 # define unixShmMap 0
15732 # define unixShmLock 0
15733 # define unixShmBarrier 0
15734 # define unixShmUnmap 0
15735 #endif /* #ifndef SQLITE_OMIT_WAL */
15736
15737 #if SQLITE_MAX_MMAP_SIZE>0
15738 /*
15739 ** If it is currently memory mapped, unmap file pFd.
15740 */
15741 static void unixUnmapfile(unixFile *pFd){
15742 assert( pFd->nFetchOut==0 );
15743 if( pFd->pMapRegion ){
15744 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
15745 pFd->pMapRegion = 0;
15746 pFd->mmapSize = 0;
15747 pFd->mmapSizeActual = 0;
15748 }
15749 }
15750
15751 /*
15752 ** Attempt to set the size of the memory mapping maintained by file
15753 ** descriptor pFd to nNew bytes. Any existing mapping is discarded.
15754 **
15755 ** If successful, this function sets the following variables:
15756 **
15757 ** unixFile.pMapRegion
15758 ** unixFile.mmapSize
15759 ** unixFile.mmapSizeActual
15760 **
15761 ** If unsuccessful, an error message is logged via sqlite3_log() and
15762 ** the three variables above are zeroed. In this case SQLite should
15763 ** continue accessing the database using the xRead() and xWrite()
15764 ** methods.
15765 */
15766 static void unixRemapfile(
15767 unixFile *pFd, /* File descriptor object */
15768 i64 nNew /* Required mapping size */
15769 ){
15770 const char *zErr = "mmap";
15771 int h = pFd->h; /* File descriptor open on db file */
15772 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
15773 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
15774 u8 *pNew = 0; /* Location of new mapping */
15775 int flags = PROT_READ; /* Flags to pass to mmap() */
15776
15777 assert( pFd->nFetchOut==0 );
15778 assert( nNew>pFd->mmapSize );
15779 assert( nNew<=pFd->mmapSizeMax );
15780 assert( nNew>0 );
15781 assert( pFd->mmapSizeActual>=pFd->mmapSize );
15782 assert( MAP_FAILED!=0 );
15783
15784 #ifdef SQLITE_MMAP_READWRITE
15785 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
15786 #endif
15787
15788 if( pOrig ){
15789 #if HAVE_MREMAP
15790 i64 nReuse = pFd->mmapSize;
15791 #else
15792 const int szSyspage = osGetpagesize();
15793 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
15794 #endif
15795 u8 *pReq = &pOrig[nReuse];
15796
15797 /* Unmap any pages of the existing mapping that cannot be reused. */
15798 if( nReuse!=nOrig ){
15799 osMunmap(pReq, nOrig-nReuse);
15800 }
15801
15802 #if HAVE_MREMAP
15803 pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE);
15804 zErr = "mremap";
15805 #else
15806 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
15807 if( pNew!=MAP_FAILED ){
15808 if( pNew!=pReq ){
15809 osMunmap(pNew, nNew - nReuse);
15810 pNew = 0;
15811 }else{
15812 pNew = pOrig;
15813 }
15814 }
15815 #endif
15816
15817 /* The attempt to extend the existing mapping failed. Free it. */
15818 if( pNew==MAP_FAILED || pNew==0 ){
15819 osMunmap(pOrig, nReuse);
15820 }
15821 }
15822
15823 /* If pNew is still NULL, try to create an entirely new mapping. */
15824 if( pNew==0 ){
15825 pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0);
15826 }
15827
15828 if( pNew==MAP_FAILED ){
15829 pNew = 0;
15830 nNew = 0;
15831 unixLogError(SQLITE_OK, zErr, pFd->zPath);
15832
15833 /* If the mmap() above failed, assume that all subsequent mmap() calls
15834 ** will probably fail too. Fall back to using xRead/xWrite exclusively
15835 ** in this case. */
15836 pFd->mmapSizeMax = 0;
15837 }
15838 pFd->pMapRegion = (void *)pNew;
15839 pFd->mmapSize = pFd->mmapSizeActual = nNew;
15840 }
15841
15842 /*
15843 ** Memory map or remap the file opened by file-descriptor pFd (if the file
15844 ** is already mapped, the existing mapping is replaced by the new). Or, if
15845 ** there already exists a mapping for this file, and there are still
15846 ** outstanding xFetch() references to it, this function is a no-op.
15847 **
15848 ** If parameter nByte is non-negative, then it is the requested size of
15849 ** the mapping to create. Otherwise, if nByte is less than zero, then the
15850 ** requested size is the size of the file on disk. The actual size of the
15851 ** created mapping is either the requested size or the value configured
15852 ** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
15853 **
15854 ** SQLITE_OK is returned if no error occurs (even if the mapping is not
15855 ** recreated as a result of outstanding references) or an SQLite error
15856 ** code otherwise.
15857 */
15858 static int unixMapfile(unixFile *pFd, i64 nMap){
15859 assert( nMap>=0 || pFd->nFetchOut==0 );
15860 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
15861 if( pFd->nFetchOut>0 ) return SQLITE_OK;
15862
15863 if( nMap<0 ){
15864 struct stat statbuf; /* Low-level file information */
15865 if( osFstat(pFd->h, &statbuf) ){
15866 return SQLITE_IOERR_FSTAT;
15867 }
15868 nMap = statbuf.st_size;
15869 }
15870 if( nMap>pFd->mmapSizeMax ){
15871 nMap = pFd->mmapSizeMax;
15872 }
15873
15874 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
15875 if( nMap!=pFd->mmapSize ){
15876 unixRemapfile(pFd, nMap);
15877 }
15878
15879 return SQLITE_OK;
15880 }
15881 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
15882
15883 /*
15884 ** If possible, return a pointer to a mapping of file fd starting at offset
15885 ** iOff. The mapping must be valid for at least nAmt bytes.
15886 **
15887 ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
15888 ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
15889 ** Finally, if an error does occur, return an SQLite error code. The final
15890 ** value of *pp is undefined in this case.
15891 **
15892 ** If this function does return a pointer, the caller must eventually
15893 ** release the reference by calling unixUnfetch().
15894 */
15895 static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
15896 #if SQLITE_MAX_MMAP_SIZE>0
15897 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
15898 #endif
15899 *pp = 0;
15900
15901 #if SQLITE_MAX_MMAP_SIZE>0
15902 if( pFd->mmapSizeMax>0 ){
15903 if( pFd->pMapRegion==0 ){
15904 int rc = unixMapfile(pFd, -1);
15905 if( rc!=SQLITE_OK ) return rc;
15906 }
15907 if( pFd->mmapSize >= iOff+nAmt ){
15908 *pp = &((u8 *)pFd->pMapRegion)[iOff];
15909 pFd->nFetchOut++;
15910 }
15911 }
15912 #endif
15913 return SQLITE_OK;
15914 }
15915
15916 /*
15917 ** If the third argument is non-NULL, then this function releases a
15918 ** reference obtained by an earlier call to unixFetch(). The second
15919 ** argument passed to this function must be the same as the corresponding
15920 ** argument that was passed to the unixFetch() invocation.
15921 **
15922 ** Or, if the third argument is NULL, then this function is being called
15923 ** to inform the VFS layer that, according to POSIX, any existing mapping
15924 ** may now be invalid and should be unmapped.
15925 */
15926 static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
15927 #if SQLITE_MAX_MMAP_SIZE>0
15928 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
15929 UNUSED_PARAMETER(iOff);
15930
15931 /* If p==0 (unmap the entire file) then there must be no outstanding
15932 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
15933 ** then there must be at least one outstanding. */
15934 assert( (p==0)==(pFd->nFetchOut==0) );
15935
15936 /* If p!=0, it must match the iOff value. */
15937 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
15938
15939 if( p ){
15940 pFd->nFetchOut--;
15941 }else{
15942 unixUnmapfile(pFd);
15943 }
15944
15945 assert( pFd->nFetchOut>=0 );
15946 #else
15947 UNUSED_PARAMETER(fd);
15948 UNUSED_PARAMETER(p);
15949 UNUSED_PARAMETER(iOff);
15950 #endif
15951 return SQLITE_OK;
15952 }
15953
15954 /*
15955 ** Here ends the implementation of all sqlite3_file methods.
15956 **
15957 ********************** End sqlite3_file Methods *******************************
15958 ******************************************************************************/
15959
15960 /*
15961 ** This division contains definitions of sqlite3_io_methods objects that
15962 ** implement various file locking strategies. It also contains definitions
15963 ** of "finder" functions. A finder-function is used to locate the appropriate
15964 ** sqlite3_io_methods object for a particular database file. The pAppData
15965 ** field of the sqlite3_vfs VFS objects are initialized to be pointers to
15966 ** the correct finder-function for that VFS.
15967 **
15968 ** Most finder functions return a pointer to a fixed sqlite3_io_methods
15969 ** object. The only interesting finder-function is autolockIoFinder, which
15970 ** looks at the filesystem type and tries to guess the best locking
15971 ** strategy from that.
15972 **
15973 ** For finder-function F, two objects are created:
15974 **
15975 ** (1) The real finder-function named "FImpt()".
15976 **
15977 ** (2) A constant pointer to this function named just "F".
15978 **
15979 **
15980 ** A pointer to the F pointer is used as the pAppData value for VFS
15981 ** objects. We have to do this instead of letting pAppData point
15982 ** directly at the finder-function since C90 rules prevent a void*
15983 ** from be cast into a function pointer.
15984 **
15985 **
15986 ** Each instance of this macro generates two objects:
15987 **
15988 ** * A constant sqlite3_io_methods object call METHOD that has locking
15989 ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
15990 **
15991 ** * An I/O method finder function called FINDER that returns a pointer
15992 ** to the METHOD object in the previous bullet.
15993 */
15994 #define IOMETHODS(FINDER,METHOD,VERSION,CLOSE,LOCK,UNLOCK,CKLOCK,SHMMAP) \
15995 static const sqlite3_io_methods METHOD = { \
15996 VERSION, /* iVersion */ \
15997 CLOSE, /* xClose */ \
15998 unixRead, /* xRead */ \
15999 unixWrite, /* xWrite */ \
16000 unixTruncate, /* xTruncate */ \
16001 unixSync, /* xSync */ \
16002 unixFileSize, /* xFileSize */ \
16003 LOCK, /* xLock */ \
16004 UNLOCK, /* xUnlock */ \
16005 CKLOCK, /* xCheckReservedLock */ \
16006 unixFileControl, /* xFileControl */ \
16007 unixSectorSize, /* xSectorSize */ \
16008 unixDeviceCharacteristics, /* xDeviceCapabilities */ \
16009 SHMMAP, /* xShmMap */ \
16010 unixShmLock, /* xShmLock */ \
16011 unixShmBarrier, /* xShmBarrier */ \
16012 unixShmUnmap, /* xShmUnmap */ \
16013 unixFetch, /* xFetch */ \
16014 unixUnfetch, /* xUnfetch */ \
16015 }; \
16016 static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \
16017 UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \
16018 return &METHOD; \
16019 } \
16020 static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \
16021 = FINDER##Impl;
16022
16023 /*
16024 ** Here are all of the sqlite3_io_methods objects for each of the
16025 ** locking strategies. Functions that return pointers to these methods
16026 ** are also created.
16027 */
16028 IOMETHODS(
16029 posixIoFinder, /* Finder function name */
16030 posixIoMethods, /* sqlite3_io_methods object name */
16031 3, /* shared memory and mmap are enabled */
16032 unixClose, /* xClose method */
16033 unixLock, /* xLock method */
16034 unixUnlock, /* xUnlock method */
16035 unixCheckReservedLock, /* xCheckReservedLock method */
16036 unixShmMap /* xShmMap method */
16037 )
16038 IOMETHODS(
16039 nolockIoFinder, /* Finder function name */
16040 nolockIoMethods, /* sqlite3_io_methods object name */
16041 3, /* shared memory is disabled */
16042 nolockClose, /* xClose method */
16043 nolockLock, /* xLock method */
16044 nolockUnlock, /* xUnlock method */
16045 nolockCheckReservedLock, /* xCheckReservedLock method */
16046 0 /* xShmMap method */
16047 )
16048 IOMETHODS(
16049 dotlockIoFinder, /* Finder function name */
16050 dotlockIoMethods, /* sqlite3_io_methods object name */
16051 1, /* shared memory is disabled */
16052 dotlockClose, /* xClose method */
16053 dotlockLock, /* xLock method */
16054 dotlockUnlock, /* xUnlock method */
16055 dotlockCheckReservedLock, /* xCheckReservedLock method */
16056 0 /* xShmMap method */
16057 )
16058
16059 #if SQLITE_ENABLE_LOCKING_STYLE
16060 IOMETHODS(
16061 flockIoFinder, /* Finder function name */
16062 flockIoMethods, /* sqlite3_io_methods object name */
16063 1, /* shared memory is disabled */
16064 flockClose, /* xClose method */
16065 flockLock, /* xLock method */
16066 flockUnlock, /* xUnlock method */
16067 flockCheckReservedLock, /* xCheckReservedLock method */
16068 0 /* xShmMap method */
16069 )
16070 #endif
16071
16072 #if OS_VXWORKS
16073 IOMETHODS(
16074 semIoFinder, /* Finder function name */
16075 semIoMethods, /* sqlite3_io_methods object name */
16076 1, /* shared memory is disabled */
16077 semXClose, /* xClose method */
16078 semXLock, /* xLock method */
16079 semXUnlock, /* xUnlock method */
16080 semXCheckReservedLock, /* xCheckReservedLock method */
16081 0 /* xShmMap method */
16082 )
16083 #endif
16084
16085 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
16086 IOMETHODS(
16087 afpIoFinder, /* Finder function name */
16088 afpIoMethods, /* sqlite3_io_methods object name */
16089 1, /* shared memory is disabled */
16090 afpClose, /* xClose method */
16091 afpLock, /* xLock method */
16092 afpUnlock, /* xUnlock method */
16093 afpCheckReservedLock, /* xCheckReservedLock method */
16094 0 /* xShmMap method */
16095 )
16096 #endif
16097
16098 /*
16099 ** The proxy locking method is a "super-method" in the sense that it
16100 ** opens secondary file descriptors for the conch and lock files and
16101 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
16102 ** secondary files. For this reason, the division that implements
16103 ** proxy locking is located much further down in the file. But we need
16104 ** to go ahead and define the sqlite3_io_methods and finder function
16105 ** for proxy locking here. So we forward declare the I/O methods.
16106 */
16107 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
16108 static int proxyClose(sqlite3_file*);
16109 static int proxyLock(sqlite3_file*, int);
16110 static int proxyUnlock(sqlite3_file*, int);
16111 static int proxyCheckReservedLock(sqlite3_file*, int*);
16112 IOMETHODS(
16113 proxyIoFinder, /* Finder function name */
16114 proxyIoMethods, /* sqlite3_io_methods object name */
16115 1, /* shared memory is disabled */
16116 proxyClose, /* xClose method */
16117 proxyLock, /* xLock method */
16118 proxyUnlock, /* xUnlock method */
16119 proxyCheckReservedLock, /* xCheckReservedLock method */
16120 0 /* xShmMap method */
16121 )
16122 #endif
16123
16124 /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
16125 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
16126 IOMETHODS(
16127 nfsIoFinder, /* Finder function name */
16128 nfsIoMethods, /* sqlite3_io_methods object name */
16129 1, /* shared memory is disabled */
16130 unixClose, /* xClose method */
16131 unixLock, /* xLock method */
16132 nfsUnlock, /* xUnlock method */
16133 unixCheckReservedLock, /* xCheckReservedLock method */
16134 0 /* xShmMap method */
16135 )
16136 #endif
16137
16138 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
16139 /*
16140 ** This "finder" function attempts to determine the best locking strategy
16141 ** for the database file "filePath". It then returns the sqlite3_io_methods
16142 ** object that implements that strategy.
16143 **
16144 ** This is for MacOSX only.
16145 */
16146 static const sqlite3_io_methods *autolockIoFinderImpl(
16147 const char *filePath, /* name of the database file */
16148 unixFile *pNew /* open file object for the database file */
16149 ){
16150 static const struct Mapping {
16151 const char *zFilesystem; /* Filesystem type name */
16152 const sqlite3_io_methods *pMethods; /* Appropriate locking method */
16153 } aMap[] = {
16154 { "hfs", &posixIoMethods },
16155 { "ufs", &posixIoMethods },
16156 { "afpfs", &afpIoMethods },
16157 { "smbfs", &afpIoMethods },
16158 { "webdav", &nolockIoMethods },
16159 { 0, 0 }
16160 };
16161 int i;
16162 struct statfs fsInfo;
16163 struct flock lockInfo;
16164
16165 if( !filePath ){
16166 /* If filePath==NULL that means we are dealing with a transient file
16167 ** that does not need to be locked. */
16168 return &nolockIoMethods;
16169 }
16170 if( statfs(filePath, &fsInfo) != -1 ){
16171 if( fsInfo.f_flags & MNT_RDONLY ){
16172 return &nolockIoMethods;
16173 }
16174 for(i=0; aMap[i].zFilesystem; i++){
16175 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
16176 return aMap[i].pMethods;
16177 }
16178 }
16179 }
16180
16181 /* Default case. Handles, amongst others, "nfs".
16182 ** Test byte-range lock using fcntl(). If the call succeeds,
16183 ** assume that the file-system supports POSIX style locks.
16184 */
16185 lockInfo.l_len = 1;
16186 lockInfo.l_start = 0;
16187 lockInfo.l_whence = SEEK_SET;
16188 lockInfo.l_type = F_RDLCK;
16189 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
16190 if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
16191 return &nfsIoMethods;
16192 } else {
16193 return &posixIoMethods;
16194 }
16195 }else{
16196 return &dotlockIoMethods;
16197 }
16198 }
16199 static const sqlite3_io_methods
16200 *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
16201
16202 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
16203
16204 #if OS_VXWORKS
16205 /*
16206 ** This "finder" function for VxWorks checks to see if posix advisory
16207 ** locking works. If it does, then that is what is used. If it does not
16208 ** work, then fallback to named semaphore locking.
16209 */
16210 static const sqlite3_io_methods *vxworksIoFinderImpl(
16211 const char *filePath, /* name of the database file */
16212 unixFile *pNew /* the open file object */
16213 ){
16214 struct flock lockInfo;
16215
16216 if( !filePath ){
16217 /* If filePath==NULL that means we are dealing with a transient file
16218 ** that does not need to be locked. */
16219 return &nolockIoMethods;
16220 }
16221
16222 /* Test if fcntl() is supported and use POSIX style locks.
16223 ** Otherwise fall back to the named semaphore method.
16224 */
16225 lockInfo.l_len = 1;
16226 lockInfo.l_start = 0;
16227 lockInfo.l_whence = SEEK_SET;
16228 lockInfo.l_type = F_RDLCK;
16229 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
16230 return &posixIoMethods;
16231 }else{
16232 return &semIoMethods;
16233 }
16234 }
16235 static const sqlite3_io_methods
16236 *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl;
16237
16238 #endif /* OS_VXWORKS */
16239
16240 /*
16241 ** An abstract type for a pointer to an IO method finder function:
16242 */
16243 typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
16244
16245
16246 /****************************************************************************
16247 **************************** sqlite3_vfs methods ****************************
16248 **
16249 ** This division contains the implementation of methods on the
16250 ** sqlite3_vfs object.
16251 */
16252
16253 /*
16254 ** Initialize the contents of the unixFile structure pointed to by pId.
16255 */
16256 static int fillInUnixFile(
16257 sqlite3_vfs *pVfs, /* Pointer to vfs object */
16258 int h, /* Open file descriptor of file being opened */
16259 sqlite3_file *pId, /* Write to the unixFile structure here */
16260 const char *zFilename, /* Name of the file being opened */
16261 int ctrlFlags /* Zero or more UNIXFILE_* values */
16262 ){
16263 const sqlite3_io_methods *pLockingStyle;
16264 unixFile *pNew = (unixFile *)pId;
16265 int rc = SQLITE_OK;
16266
16267 assert( pNew->pInode==NULL );
16268
16269 /* Usually the path zFilename should not be a relative pathname. The
16270 ** exception is when opening the proxy "conch" file in builds that
16271 ** include the special Apple locking styles.
16272 */
16273 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
16274 assert( zFilename==0 || zFilename[0]=='/'
16275 || pVfs->pAppData==(void*)&autolockIoFinder );
16276 #else
16277 assert( zFilename==0 || zFilename[0]=='/' );
16278 #endif
16279
16280 /* No locking occurs in temporary files */
16281 assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
16282
16283 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
16284 pNew->h = h;
16285 pNew->pVfs = pVfs;
16286 pNew->zPath = zFilename;
16287 pNew->ctrlFlags = (u8)ctrlFlags;
16288 #if SQLITE_MAX_MMAP_SIZE>0
16289 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
16290 #endif
16291 if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
16292 "psow", SQLITE_POWERSAFE_OVERWRITE) ){
16293 pNew->ctrlFlags |= UNIXFILE_PSOW;
16294 }
16295 if( strcmp(pVfs->zName,"unix-excl")==0 ){
16296 pNew->ctrlFlags |= UNIXFILE_EXCL;
16297 }
16298
16299 #if OS_VXWORKS
16300 pNew->pId = vxworksFindFileId(zFilename);
16301 if( pNew->pId==0 ){
16302 ctrlFlags |= UNIXFILE_NOLOCK;
16303 rc = SQLITE_NOMEM_BKPT;
16304 }
16305 #endif
16306
16307 if( ctrlFlags & UNIXFILE_NOLOCK ){
16308 pLockingStyle = &nolockIoMethods;
16309 }else{
16310 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
16311 #if SQLITE_ENABLE_LOCKING_STYLE
16312 /* Cache zFilename in the locking context (AFP and dotlock override) for
16313 ** proxyLock activation is possible (remote proxy is based on db name)
16314 ** zFilename remains valid until file is closed, to support */
16315 pNew->lockingContext = (void*)zFilename;
16316 #endif
16317 }
16318
16319 if( pLockingStyle == &posixIoMethods
16320 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
16321 || pLockingStyle == &nfsIoMethods
16322 #endif
16323 ){
16324 unixEnterMutex();
16325 rc = findInodeInfo(pNew, &pNew->pInode);
16326 if( rc!=SQLITE_OK ){
16327 /* If an error occurred in findInodeInfo(), close the file descriptor
16328 ** immediately, before releasing the mutex. findInodeInfo() may fail
16329 ** in two scenarios:
16330 **
16331 ** (a) A call to fstat() failed.
16332 ** (b) A malloc failed.
16333 **
16334 ** Scenario (b) may only occur if the process is holding no other
16335 ** file descriptors open on the same file. If there were other file
16336 ** descriptors on this file, then no malloc would be required by
16337 ** findInodeInfo(). If this is the case, it is quite safe to close
16338 ** handle h - as it is guaranteed that no posix locks will be released
16339 ** by doing so.
16340 **
16341 ** If scenario (a) caused the error then things are not so safe. The
16342 ** implicit assumption here is that if fstat() fails, things are in
16343 ** such bad shape that dropping a lock or two doesn't matter much.
16344 */
16345 robust_close(pNew, h, __LINE__);
16346 h = -1;
16347 }
16348 unixLeaveMutex();
16349 }
16350
16351 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
16352 else if( pLockingStyle == &afpIoMethods ){
16353 /* AFP locking uses the file path so it needs to be included in
16354 ** the afpLockingContext.
16355 */
16356 afpLockingContext *pCtx;
16357 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
16358 if( pCtx==0 ){
16359 rc = SQLITE_NOMEM_BKPT;
16360 }else{
16361 /* NB: zFilename exists and remains valid until the file is closed
16362 ** according to requirement F11141. So we do not need to make a
16363 ** copy of the filename. */
16364 pCtx->dbPath = zFilename;
16365 pCtx->reserved = 0;
16366 srandomdev();
16367 unixEnterMutex();
16368 rc = findInodeInfo(pNew, &pNew->pInode);
16369 if( rc!=SQLITE_OK ){
16370 sqlite3_free(pNew->lockingContext);
16371 robust_close(pNew, h, __LINE__);
16372 h = -1;
16373 }
16374 unixLeaveMutex();
16375 }
16376 }
16377 #endif
16378
16379 else if( pLockingStyle == &dotlockIoMethods ){
16380 /* Dotfile locking uses the file path so it needs to be included in
16381 ** the dotlockLockingContext
16382 */
16383 char *zLockFile;
16384 int nFilename;
16385 assert( zFilename!=0 );
16386 nFilename = (int)strlen(zFilename) + 6;
16387 zLockFile = (char *)sqlite3_malloc64(nFilename);
16388 if( zLockFile==0 ){
16389 rc = SQLITE_NOMEM_BKPT;
16390 }else{
16391 sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
16392 }
16393 pNew->lockingContext = zLockFile;
16394 }
16395
16396 #if OS_VXWORKS
16397 else if( pLockingStyle == &semIoMethods ){
16398 /* Named semaphore locking uses the file path so it needs to be
16399 ** included in the semLockingContext
16400 */
16401 unixEnterMutex();
16402 rc = findInodeInfo(pNew, &pNew->pInode);
16403 if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
16404 char *zSemName = pNew->pInode->aSemName;
16405 int n;
16406 sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
16407 pNew->pId->zCanonicalName);
16408 for( n=1; zSemName[n]; n++ )
16409 if( zSemName[n]=='/' ) zSemName[n] = '_';
16410 pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
16411 if( pNew->pInode->pSem == SEM_FAILED ){
16412 rc = SQLITE_NOMEM_BKPT;
16413 pNew->pInode->aSemName[0] = '\0';
16414 }
16415 }
16416 unixLeaveMutex();
16417 }
16418 #endif
16419
16420 storeLastErrno(pNew, 0);
16421 #if OS_VXWORKS
16422 if( rc!=SQLITE_OK ){
16423 if( h>=0 ) robust_close(pNew, h, __LINE__);
16424 h = -1;
16425 osUnlink(zFilename);
16426 pNew->ctrlFlags |= UNIXFILE_DELETE;
16427 }
16428 #endif
16429 if( rc!=SQLITE_OK ){
16430 if( h>=0 ) robust_close(pNew, h, __LINE__);
16431 }else{
16432 pNew->pMethod = pLockingStyle;
16433 OpenCounter(+1);
16434 verifyDbFile(pNew);
16435 }
16436 return rc;
16437 }
16438
16439 /*
16440 ** Return the name of a directory in which to put temporary files.
16441 ** If no suitable temporary file directory can be found, return NULL.
16442 */
16443 static const char *unixTempFileDir(void){
16444 static const char *azDirs[] = {
16445 0,
16446 0,
16447 "/var/tmp",
16448 "/usr/tmp",
16449 "/tmp",
16450 "."
16451 };
16452 unsigned int i = 0;
16453 struct stat buf;
16454 const char *zDir = sqlite3_temp_directory;
16455
16456 if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
16457 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
16458 while(1){
16459 if( zDir!=0
16460 && osStat(zDir, &buf)==0
16461 && S_ISDIR(buf.st_mode)
16462 && osAccess(zDir, 03)==0
16463 ){
16464 return zDir;
16465 }
16466 if( i>=sizeof(azDirs)/sizeof(azDirs[0]) ) break;
16467 zDir = azDirs[i++];
16468 }
16469 return 0;
16470 }
16471
16472 /*
16473 ** Create a temporary file name in zBuf. zBuf must be allocated
16474 ** by the calling process and must be big enough to hold at least
16475 ** pVfs->mxPathname bytes.
16476 */
16477 static int unixGetTempname(int nBuf, char *zBuf){
16478 const char *zDir;
16479 int iLimit = 0;
16480
16481 /* It's odd to simulate an io-error here, but really this is just
16482 ** using the io-error infrastructure to test that SQLite handles this
16483 ** function failing.
16484 */
16485 zBuf[0] = 0;
16486 SimulateIOError( return SQLITE_IOERR );
16487
16488 zDir = unixTempFileDir();
16489 if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH;
16490 do{
16491 u64 r;
16492 sqlite3_randomness(sizeof(r), &r);
16493 assert( nBuf>2 );
16494 zBuf[nBuf-2] = 0;
16495 sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c",
16496 zDir, r, 0);
16497 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR;
16498 }while( osAccess(zBuf,0)==0 );
16499 return SQLITE_OK;
16500 }
16501
16502 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
16503 /*
16504 ** Routine to transform a unixFile into a proxy-locking unixFile.
16505 ** Implementation in the proxy-lock division, but used by unixOpen()
16506 ** if SQLITE_PREFER_PROXY_LOCKING is defined.
16507 */
16508 static int proxyTransformUnixFile(unixFile*, const char*);
16509 #endif
16510
16511 /*
16512 ** Search for an unused file descriptor that was opened on the database
16513 ** file (not a journal or master-journal file) identified by pathname
16514 ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
16515 ** argument to this function.
16516 **
16517 ** Such a file descriptor may exist if a database connection was closed
16518 ** but the associated file descriptor could not be closed because some
16519 ** other file descriptor open on the same file is holding a file-lock.
16520 ** Refer to comments in the unixClose() function and the lengthy comment
16521 ** describing "Posix Advisory Locking" at the start of this file for
16522 ** further details. Also, ticket #4018.
16523 **
16524 ** If a suitable file descriptor is found, then it is returned. If no
16525 ** such file descriptor is located, -1 is returned.
16526 */
16527 static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
16528 UnixUnusedFd *pUnused = 0;
16529
16530 /* Do not search for an unused file descriptor on vxworks. Not because
16531 ** vxworks would not benefit from the change (it might, we're not sure),
16532 ** but because no way to test it is currently available. It is better
16533 ** not to risk breaking vxworks support for the sake of such an obscure
16534 ** feature. */
16535 #if !OS_VXWORKS
16536 struct stat sStat; /* Results of stat() call */
16537
16538 /* A stat() call may fail for various reasons. If this happens, it is
16539 ** almost certain that an open() call on the same path will also fail.
16540 ** For this reason, if an error occurs in the stat() call here, it is
16541 ** ignored and -1 is returned. The caller will try to open a new file
16542 ** descriptor on the same path, fail, and return an error to SQLite.
16543 **
16544 ** Even if a subsequent open() call does succeed, the consequences of
16545 ** not searching for a reusable file descriptor are not dire. */
16546 if( 0==osStat(zPath, &sStat) ){
16547 unixInodeInfo *pInode;
16548
16549 unixEnterMutex();
16550 pInode = inodeList;
16551 while( pInode && (pInode->fileId.dev!=sStat.st_dev
16552 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
16553 pInode = pInode->pNext;
16554 }
16555 if( pInode ){
16556 UnixUnusedFd **pp;
16557 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
16558 pUnused = *pp;
16559 if( pUnused ){
16560 *pp = pUnused->pNext;
16561 }
16562 }
16563 unixLeaveMutex();
16564 }
16565 #endif /* if !OS_VXWORKS */
16566 return pUnused;
16567 }
16568
16569 /*
16570 ** Find the mode, uid and gid of file zFile.
16571 */
16572 static int getFileMode(
16573 const char *zFile, /* File name */
16574 mode_t *pMode, /* OUT: Permissions of zFile */
16575 uid_t *pUid, /* OUT: uid of zFile. */
16576 gid_t *pGid /* OUT: gid of zFile. */
16577 ){
16578 struct stat sStat; /* Output of stat() on database file */
16579 int rc = SQLITE_OK;
16580 if( 0==osStat(zFile, &sStat) ){
16581 *pMode = sStat.st_mode & 0777;
16582 *pUid = sStat.st_uid;
16583 *pGid = sStat.st_gid;
16584 }else{
16585 rc = SQLITE_IOERR_FSTAT;
16586 }
16587 return rc;
16588 }
16589
16590 /*
16591 ** This function is called by unixOpen() to determine the unix permissions
16592 ** to create new files with. If no error occurs, then SQLITE_OK is returned
16593 ** and a value suitable for passing as the third argument to open(2) is
16594 ** written to *pMode. If an IO error occurs, an SQLite error code is
16595 ** returned and the value of *pMode is not modified.
16596 **
16597 ** In most cases, this routine sets *pMode to 0, which will become
16598 ** an indication to robust_open() to create the file using
16599 ** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask.
16600 ** But if the file being opened is a WAL or regular journal file, then
16601 ** this function queries the file-system for the permissions on the
16602 ** corresponding database file and sets *pMode to this value. Whenever
16603 ** possible, WAL and journal files are created using the same permissions
16604 ** as the associated database file.
16605 **
16606 ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
16607 ** original filename is unavailable. But 8_3_NAMES is only used for
16608 ** FAT filesystems and permissions do not matter there, so just use
16609 ** the default permissions.
16610 */
16611 static int findCreateFileMode(
16612 const char *zPath, /* Path of file (possibly) being created */
16613 int flags, /* Flags passed as 4th argument to xOpen() */
16614 mode_t *pMode, /* OUT: Permissions to open file with */
16615 uid_t *pUid, /* OUT: uid to set on the file */
16616 gid_t *pGid /* OUT: gid to set on the file */
16617 ){
16618 int rc = SQLITE_OK; /* Return Code */
16619 *pMode = 0;
16620 *pUid = 0;
16621 *pGid = 0;
16622 if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
16623 char zDb[MAX_PATHNAME+1]; /* Database file path */
16624 int nDb; /* Number of valid bytes in zDb */
16625
16626 /* zPath is a path to a WAL or journal file. The following block derives
16627 ** the path to the associated database file from zPath. This block handles
16628 ** the following naming conventions:
16629 **
16630 ** "<path to db>-journal"
16631 ** "<path to db>-wal"
16632 ** "<path to db>-journalNN"
16633 ** "<path to db>-walNN"
16634 **
16635 ** where NN is a decimal number. The NN naming schemes are
16636 ** used by the test_multiplex.c module.
16637 */
16638 nDb = sqlite3Strlen30(zPath) - 1;
16639 while( zPath[nDb]!='-' ){
16640 #ifndef SQLITE_ENABLE_8_3_NAMES
16641 /* In the normal case (8+3 filenames disabled) the journal filename
16642 ** is guaranteed to contain a '-' character. */
16643 assert( nDb>0 );
16644 assert( sqlite3Isalnum(zPath[nDb]) );
16645 #else
16646 /* If 8+3 names are possible, then the journal file might not contain
16647 ** a '-' character. So check for that case and return early. */
16648 if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK;
16649 #endif
16650 nDb--;
16651 }
16652 memcpy(zDb, zPath, nDb);
16653 zDb[nDb] = '\0';
16654
16655 rc = getFileMode(zDb, pMode, pUid, pGid);
16656 }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
16657 *pMode = 0600;
16658 }else if( flags & SQLITE_OPEN_URI ){
16659 /* If this is a main database file and the file was opened using a URI
16660 ** filename, check for the "modeof" parameter. If present, interpret
16661 ** its value as a filename and try to copy the mode, uid and gid from
16662 ** that file. */
16663 const char *z = sqlite3_uri_parameter(zPath, "modeof");
16664 if( z ){
16665 rc = getFileMode(z, pMode, pUid, pGid);
16666 }
16667 }
16668 return rc;
16669 }
16670
16671 /*
16672 ** Initialize |unixFile| internals of |file| on behalf of chromiumOpen() in
16673 ** WebDatabase SQLiteFileSystemPosix.cpp. Function is a subset of unixOpen(),
16674 ** each duplicated piece is marked by "Duplicated in" comment in unixOpen().
16675 */
16676 CHROMIUM_SQLITE_API
16677 int chromium_sqlite3_fill_in_unix_sqlite3_file(sqlite3_vfs* pVfs,
16678 int fd,
16679 sqlite3_file* pFile,
16680 const char* zPath,
16681 int noLock,
16682 int flags) {
16683 unixFile *p = (unixFile *)pFile;
16684 const int eType = flags&0xFFFFFF00; /* Type of file to open */
16685 const int ctrlFlags = (noLock ? UNIXFILE_NOLOCK : 0);
16686 int rc;
16687
16688 memset(p, 0, sizeof(unixFile));
16689
16690 /* osStat() will not work in the sandbox, so findReusableFd() will always
16691 ** fail, so directly include the failure-case setup then initialize pUnused.
16692 */
16693 if( eType==SQLITE_OPEN_MAIN_DB ){
16694 p->pUnused = sqlite3_malloc(sizeof(*p->pUnused));
16695 if (!p->pUnused) {
16696 return SQLITE_NOMEM_BKPT;
16697 }
16698 p->pUnused->fd = fd;
16699 p->pUnused->flags = flags;
16700 }
16701
16702 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
16703 if( rc!=SQLITE_OK ){
16704 sqlite3_free(p->pUnused);
16705 }
16706 return rc;
16707 }
16708
16709 /*
16710 ** Open the file zPath.
16711 **
16712 ** Previously, the SQLite OS layer used three functions in place of this
16713 ** one:
16714 **
16715 ** sqlite3OsOpenReadWrite();
16716 ** sqlite3OsOpenReadOnly();
16717 ** sqlite3OsOpenExclusive();
16718 **
16719 ** These calls correspond to the following combinations of flags:
16720 **
16721 ** ReadWrite() -> (READWRITE | CREATE)
16722 ** ReadOnly() -> (READONLY)
16723 ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
16724 **
16725 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
16726 ** true, the file was configured to be automatically deleted when the
16727 ** file handle closed. To achieve the same effect using this new
16728 ** interface, add the DELETEONCLOSE flag to those specified above for
16729 ** OpenExclusive().
16730 */
16731 static int unixOpen(
16732 sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */
16733 const char *zPath, /* Pathname of file to be opened */
16734 sqlite3_file *pFile, /* The file descriptor to be filled in */
16735 int flags, /* Input flags to control the opening */
16736 int *pOutFlags /* Output flags returned to SQLite core */
16737 ){
16738 unixFile *p = (unixFile *)pFile;
16739 int fd = -1; /* File descriptor returned by open() */
16740 int openFlags = 0; /* Flags to pass to open() */
16741 int eType = flags&0xFFFFFF00; /* Type of file to open */
16742 int noLock; /* True to omit locking primitives */
16743 int rc = SQLITE_OK; /* Function Return Code */
16744 int ctrlFlags = 0; /* UNIXFILE_* flags */
16745
16746 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
16747 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
16748 int isCreate = (flags & SQLITE_OPEN_CREATE);
16749 int isReadonly = (flags & SQLITE_OPEN_READONLY);
16750 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
16751 #if SQLITE_ENABLE_LOCKING_STYLE
16752 int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY);
16753 #endif
16754 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
16755 struct statfs fsInfo;
16756 #endif
16757
16758 /* If creating a master or main-file journal, this function will open
16759 ** a file-descriptor on the directory too. The first time unixSync()
16760 ** is called the directory file descriptor will be fsync()ed and close()d.
16761 */
16762 int syncDir = (isCreate && (
16763 eType==SQLITE_OPEN_MASTER_JOURNAL
16764 || eType==SQLITE_OPEN_MAIN_JOURNAL
16765 || eType==SQLITE_OPEN_WAL
16766 ));
16767
16768 /* If argument zPath is a NULL pointer, this function is required to open
16769 ** a temporary file. Use this buffer to store the file name in.
16770 */
16771 char zTmpname[MAX_PATHNAME+2];
16772 const char *zName = zPath;
16773
16774 /* Check the following statements are true:
16775 **
16776 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
16777 ** (b) if CREATE is set, then READWRITE must also be set, and
16778 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
16779 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
16780 */
16781 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
16782 assert(isCreate==0 || isReadWrite);
16783 assert(isExclusive==0 || isCreate);
16784 assert(isDelete==0 || isCreate);
16785
16786 /* The main DB, main journal, WAL file and master journal are never
16787 ** automatically deleted. Nor are they ever temporary files. */
16788 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
16789 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
16790 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
16791 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
16792
16793 /* Assert that the upper layer has set one of the "file-type" flags. */
16794 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
16795 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
16796 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
16797 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
16798 );
16799
16800 /* Detect a pid change and reset the PRNG. There is a race condition
16801 ** here such that two or more threads all trying to open databases at
16802 ** the same instant might all reset the PRNG. But multiple resets
16803 ** are harmless.
16804 */
16805 if( randomnessPid!=osGetpid(0) ){
16806 randomnessPid = osGetpid(0);
16807 sqlite3_randomness(0,0);
16808 }
16809
16810 /* Duplicated in chromium_sqlite3_fill_in_unix_sqlite3_file(). */
16811 memset(p, 0, sizeof(unixFile));
16812
16813 if( eType==SQLITE_OPEN_MAIN_DB ){
16814 UnixUnusedFd *pUnused;
16815 pUnused = findReusableFd(zName, flags);
16816 if( pUnused ){
16817 fd = pUnused->fd;
16818 }else{
16819 /* Duplicated in chromium_sqlite3_fill_in_unix_sqlite3_file(). */
16820 pUnused = sqlite3_malloc64(sizeof(*pUnused));
16821 if( !pUnused ){
16822 return SQLITE_NOMEM_BKPT;
16823 }
16824 }
16825 p->pUnused = pUnused;
16826
16827 /* Database filenames are double-zero terminated if they are not
16828 ** URIs with parameters. Hence, they can always be passed into
16829 ** sqlite3_uri_parameter(). */
16830 assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
16831
16832 }else if( !zName ){
16833 /* If zName is NULL, the upper layer is requesting a temp file. */
16834 assert(isDelete && !syncDir);
16835 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
16836 if( rc!=SQLITE_OK ){
16837 return rc;
16838 }
16839 zName = zTmpname;
16840
16841 /* Generated temporary filenames are always double-zero terminated
16842 ** for use by sqlite3_uri_parameter(). */
16843 assert( zName[strlen(zName)+1]==0 );
16844 }
16845
16846 /* Determine the value of the flags parameter passed to POSIX function
16847 ** open(). These must be calculated even if open() is not called, as
16848 ** they may be stored as part of the file handle and used by the
16849 ** 'conch file' locking functions later on. */
16850 if( isReadonly ) openFlags |= O_RDONLY;
16851 if( isReadWrite ) openFlags |= O_RDWR;
16852 if( isCreate ) openFlags |= O_CREAT;
16853 if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
16854 openFlags |= (O_LARGEFILE|O_BINARY);
16855
16856 if( fd<0 ){
16857 mode_t openMode; /* Permissions to create file with */
16858 uid_t uid; /* Userid for the file */
16859 gid_t gid; /* Groupid for the file */
16860 rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
16861 if( rc!=SQLITE_OK ){
16862 assert( !p->pUnused );
16863 assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
16864 return rc;
16865 }
16866 fd = robust_open(zName, openFlags, openMode);
16867 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
16868 assert( !isExclusive || (openFlags & O_CREAT)!=0 );
16869 if( fd<0 && errno!=EISDIR && isReadWrite ){
16870 /* Failed to open the file for read/write access. Try read-only. */
16871 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
16872 openFlags &= ~(O_RDWR|O_CREAT);
16873 flags |= SQLITE_OPEN_READONLY;
16874 openFlags |= O_RDONLY;
16875 isReadonly = 1;
16876 fd = robust_open(zName, openFlags, openMode);
16877 }
16878 if( fd<0 ){
16879 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
16880 goto open_finished;
16881 }
16882
16883 /* If this process is running as root and if creating a new rollback
16884 ** journal or WAL file, set the ownership of the journal or WAL to be
16885 ** the same as the original database.
16886 */
16887 if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
16888 robustFchown(fd, uid, gid);
16889 }
16890 }
16891 assert( fd>=0 );
16892 if( pOutFlags ){
16893 *pOutFlags = flags;
16894 }
16895
16896 if( p->pUnused ){
16897 /* Duplicated in chromium_sqlite3_fill_in_unix_sqlite3_file(). */
16898 p->pUnused->fd = fd;
16899 p->pUnused->flags = flags;
16900 }
16901
16902 if( isDelete ){
16903 #if OS_VXWORKS
16904 zPath = zName;
16905 #elif defined(SQLITE_UNLINK_AFTER_CLOSE)
16906 zPath = sqlite3_mprintf("%s", zName);
16907 if( zPath==0 ){
16908 robust_close(p, fd, __LINE__);
16909 return SQLITE_NOMEM_BKPT;
16910 }
16911 #else
16912 osUnlink(zName);
16913 #endif
16914 }
16915 #if SQLITE_ENABLE_LOCKING_STYLE
16916 else{
16917 p->openFlags = openFlags;
16918 }
16919 #endif
16920
16921 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
16922 if( fstatfs(fd, &fsInfo) == -1 ){
16923 storeLastErrno(p, errno);
16924 robust_close(p, fd, __LINE__);
16925 return SQLITE_IOERR_ACCESS;
16926 }
16927 if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
16928 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
16929 }
16930 if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) {
16931 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
16932 }
16933 #endif
16934
16935 /* Set up appropriate ctrlFlags */
16936 if( isDelete ) ctrlFlags |= UNIXFILE_DELETE;
16937 if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY;
16938 noLock = eType!=SQLITE_OPEN_MAIN_DB;
16939 if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK;
16940 if( syncDir ) ctrlFlags |= UNIXFILE_DIRSYNC;
16941 if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
16942
16943 #if SQLITE_ENABLE_LOCKING_STYLE
16944 #if SQLITE_PREFER_PROXY_LOCKING
16945 isAutoProxy = 1;
16946 #endif
16947 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
16948 char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
16949 int useProxy = 0;
16950
16951 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
16952 ** never use proxy, NULL means use proxy for non-local files only. */
16953 if( envforce!=NULL ){
16954 useProxy = atoi(envforce)>0;
16955 }else{
16956 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
16957 }
16958 if( useProxy ){
16959 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
16960 if( rc==SQLITE_OK ){
16961 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
16962 if( rc!=SQLITE_OK ){
16963 /* Use unixClose to clean up the resources added in fillInUnixFile
16964 ** and clear all the structure's references. Specifically,
16965 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
16966 */
16967 unixClose(pFile);
16968 return rc;
16969 }
16970 }
16971 goto open_finished;
16972 }
16973 }
16974 #endif
16975
16976 /* Duplicated in chromium_sqlite3_fill_in_unix_sqlite3_file(). */
16977 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
16978
16979 open_finished:
16980 if( rc!=SQLITE_OK ){
16981 /* Duplicated in chromium_sqlite3_fill_in_unix_sqlite3_file(). */
16982 sqlite3_free(p->pUnused);
16983 }
16984 return rc;
16985 }
16986
16987
16988 /*
16989 ** Delete the file at zPath. If the dirSync argument is true, fsync()
16990 ** the directory after deleting the file.
16991 */
16992 static int unixDelete(
16993 sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */
16994 const char *zPath, /* Name of file to be deleted */
16995 int dirSync /* If true, fsync() directory after deleting file */
16996 ){
16997 int rc = SQLITE_OK;
16998 UNUSED_PARAMETER(NotUsed);
16999 SimulateIOError(return SQLITE_IOERR_DELETE);
17000 if( osUnlink(zPath)==(-1) ){
17001 if( errno==ENOENT
17002 #if OS_VXWORKS
17003 || osAccess(zPath,0)!=0
17004 #endif
17005 ){
17006 rc = SQLITE_IOERR_DELETE_NOENT;
17007 }else{
17008 rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
17009 }
17010 return rc;
17011 }
17012 #ifndef SQLITE_DISABLE_DIRSYNC
17013 if( (dirSync & 1)!=0 ){
17014 int fd;
17015 rc = osOpenDirectory(zPath, &fd);
17016 if( rc==SQLITE_OK ){
17017 if( full_fsync(fd,0,0) ){
17018 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
17019 }
17020 robust_close(0, fd, __LINE__);
17021 }else{
17022 assert( rc==SQLITE_CANTOPEN );
17023 rc = SQLITE_OK;
17024 }
17025 }
17026 #endif
17027 return rc;
17028 }
17029
17030 /*
17031 ** Test the existence of or access permissions of file zPath. The
17032 ** test performed depends on the value of flags:
17033 **
17034 ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
17035 ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
17036 ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
17037 **
17038 ** Otherwise return 0.
17039 */
17040 static int unixAccess(
17041 sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */
17042 const char *zPath, /* Path of the file to examine */
17043 int flags, /* What do we want to learn about the zPath file? */
17044 int *pResOut /* Write result boolean here */
17045 ){
17046 UNUSED_PARAMETER(NotUsed);
17047 SimulateIOError( return SQLITE_IOERR_ACCESS; );
17048 assert( pResOut!=0 );
17049
17050 /* The spec says there are three possible values for flags. But only
17051 ** two of them are actually used */
17052 assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE );
17053
17054 if( flags==SQLITE_ACCESS_EXISTS ){
17055 struct stat buf;
17056 *pResOut = (0==osStat(zPath, &buf) && buf.st_size>0);
17057 }else{
17058 *pResOut = osAccess(zPath, W_OK|R_OK)==0;
17059 }
17060 return SQLITE_OK;
17061 }
17062
17063 /*
17064 **
17065 */
17066 static int mkFullPathname(
17067 const char *zPath, /* Input path */
17068 char *zOut, /* Output buffer */
17069 int nOut /* Allocated size of buffer zOut */
17070 ){
17071 int nPath = sqlite3Strlen30(zPath);
17072 int iOff = 0;
17073 if( zPath[0]!='/' ){
17074 if( osGetcwd(zOut, nOut-2)==0 ){
17075 return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
17076 }
17077 iOff = sqlite3Strlen30(zOut);
17078 zOut[iOff++] = '/';
17079 }
17080 if( (iOff+nPath+1)>nOut ){
17081 /* SQLite assumes that xFullPathname() nul-terminates the output buffer
17082 ** even if it returns an error. */
17083 zOut[iOff] = '\0';
17084 return SQLITE_CANTOPEN_BKPT;
17085 }
17086 sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath);
17087 return SQLITE_OK;
17088 }
17089
17090 /*
17091 ** Turn a relative pathname into a full pathname. The relative path
17092 ** is stored as a nul-terminated string in the buffer pointed to by
17093 ** zPath.
17094 **
17095 ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
17096 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
17097 ** this buffer before returning.
17098 */
17099 static int unixFullPathname(
17100 sqlite3_vfs *pVfs, /* Pointer to vfs object */
17101 const char *zPath, /* Possibly relative input path */
17102 int nOut, /* Size of output buffer in bytes */
17103 char *zOut /* Output buffer */
17104 ){
17105 #if !defined(HAVE_READLINK) || !defined(HAVE_LSTAT)
17106 return mkFullPathname(zPath, zOut, nOut);
17107 #else
17108 int rc = SQLITE_OK;
17109 int nByte;
17110 int nLink = 1; /* Number of symbolic links followed so far */
17111 const char *zIn = zPath; /* Input path for each iteration of loop */
17112 char *zDel = 0;
17113
17114 assert( pVfs->mxPathname==MAX_PATHNAME );
17115 UNUSED_PARAMETER(pVfs);
17116
17117 /* It's odd to simulate an io-error here, but really this is just
17118 ** using the io-error infrastructure to test that SQLite handles this
17119 ** function failing. This function could fail if, for example, the
17120 ** current working directory has been unlinked.
17121 */
17122 SimulateIOError( return SQLITE_ERROR );
17123
17124 do {
17125
17126 /* Call stat() on path zIn. Set bLink to true if the path is a symbolic
17127 ** link, or false otherwise. */
17128 int bLink = 0;
17129 struct stat buf;
17130 if( osLstat(zIn, &buf)!=0 ){
17131 if( errno!=ENOENT ){
17132 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn);
17133 }
17134 }else{
17135 bLink = S_ISLNK(buf.st_mode);
17136 }
17137
17138 if( bLink ){
17139 if( zDel==0 ){
17140 zDel = sqlite3_malloc(nOut);
17141 if( zDel==0 ) rc = SQLITE_NOMEM_BKPT;
17142 }else if( ++nLink>SQLITE_MAX_SYMLINKS ){
17143 rc = SQLITE_CANTOPEN_BKPT;
17144 }
17145
17146 if( rc==SQLITE_OK ){
17147 nByte = osReadlink(zIn, zDel, nOut-1);
17148 if( nByte<0 ){
17149 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn);
17150 }else{
17151 if( zDel[0]!='/' ){
17152 int n;
17153 for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--);
17154 if( nByte+n+1>nOut ){
17155 rc = SQLITE_CANTOPEN_BKPT;
17156 }else{
17157 memmove(&zDel[n], zDel, nByte+1);
17158 memcpy(zDel, zIn, n);
17159 nByte += n;
17160 }
17161 }
17162 zDel[nByte] = '\0';
17163 }
17164 }
17165
17166 zIn = zDel;
17167 }
17168
17169 assert( rc!=SQLITE_OK || zIn!=zOut || zIn[0]=='/' );
17170 if( rc==SQLITE_OK && zIn!=zOut ){
17171 rc = mkFullPathname(zIn, zOut, nOut);
17172 }
17173 if( bLink==0 ) break;
17174 zIn = zOut;
17175 }while( rc==SQLITE_OK );
17176
17177 sqlite3_free(zDel);
17178 return rc;
17179 #endif /* HAVE_READLINK && HAVE_LSTAT */
17180 }
17181
17182
17183 #ifndef SQLITE_OMIT_LOAD_EXTENSION
17184 /*
17185 ** Interfaces for opening a shared library, finding entry points
17186 ** within the shared library, and closing the shared library.
17187 */
17188 #include <dlfcn.h>
17189 static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
17190 UNUSED_PARAMETER(NotUsed);
17191 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
17192 }
17193
17194 /*
17195 ** SQLite calls this function immediately after a call to unixDlSym() or
17196 ** unixDlOpen() fails (returns a null pointer). If a more detailed error
17197 ** message is available, it is written to zBufOut. If no error message
17198 ** is available, zBufOut is left unmodified and SQLite uses a default
17199 ** error message.
17200 */
17201 static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
17202 const char *zErr;
17203 UNUSED_PARAMETER(NotUsed);
17204 unixEnterMutex();
17205 zErr = dlerror();
17206 if( zErr ){
17207 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
17208 }
17209 unixLeaveMutex();
17210 }
17211 static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
17212 /*
17213 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
17214 ** cast into a pointer to a function. And yet the library dlsym() routine
17215 ** returns a void* which is really a pointer to a function. So how do we
17216 ** use dlsym() with -pedantic-errors?
17217 **
17218 ** Variable x below is defined to be a pointer to a function taking
17219 ** parameters void* and const char* and returning a pointer to a function.
17220 ** We initialize x by assigning it a pointer to the dlsym() function.
17221 ** (That assignment requires a cast.) Then we call the function that
17222 ** x points to.
17223 **
17224 ** This work-around is unlikely to work correctly on any system where
17225 ** you really cannot cast a function pointer into void*. But then, on the
17226 ** other hand, dlsym() will not work on such a system either, so we have
17227 ** not really lost anything.
17228 */
17229 void (*(*x)(void*,const char*))(void);
17230 UNUSED_PARAMETER(NotUsed);
17231 x = (void(*(*)(void*,const char*))(void))dlsym;
17232 return (*x)(p, zSym);
17233 }
17234 static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
17235 UNUSED_PARAMETER(NotUsed);
17236 dlclose(pHandle);
17237 }
17238 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
17239 #define unixDlOpen 0
17240 #define unixDlError 0
17241 #define unixDlSym 0
17242 #define unixDlClose 0
17243 #endif
17244
17245 /*
17246 ** Write nBuf bytes of random data to the supplied buffer zBuf.
17247 */
17248 static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
17249 UNUSED_PARAMETER(NotUsed);
17250 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
17251
17252 /* We have to initialize zBuf to prevent valgrind from reporting
17253 ** errors. The reports issued by valgrind are incorrect - we would
17254 ** prefer that the randomness be increased by making use of the
17255 ** uninitialized space in zBuf - but valgrind errors tend to worry
17256 ** some users. Rather than argue, it seems easier just to initialize
17257 ** the whole array and silence valgrind, even if that means less randomness
17258 ** in the random seed.
17259 **
17260 ** When testing, initializing zBuf[] to zero is all we do. That means
17261 ** that we always use the same random number sequence. This makes the
17262 ** tests repeatable.
17263 */
17264 memset(zBuf, 0, nBuf);
17265 randomnessPid = osGetpid(0);
17266 #if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
17267 {
17268 int fd, got;
17269 fd = robust_open("/dev/urandom", O_RDONLY, 0);
17270 if( fd<0 ){
17271 time_t t;
17272 time(&t);
17273 memcpy(zBuf, &t, sizeof(t));
17274 memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid));
17275 assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf );
17276 nBuf = sizeof(t) + sizeof(randomnessPid);
17277 }else{
17278 do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR );
17279 robust_close(0, fd, __LINE__);
17280 }
17281 }
17282 #endif
17283 return nBuf;
17284 }
17285
17286
17287 /*
17288 ** Sleep for a little while. Return the amount of time slept.
17289 ** The argument is the number of microseconds we want to sleep.
17290 ** The return value is the number of microseconds of sleep actually
17291 ** requested from the underlying operating system, a number which
17292 ** might be greater than or equal to the argument, but not less
17293 ** than the argument.
17294 */
17295 static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
17296 #if OS_VXWORKS
17297 struct timespec sp;
17298
17299 sp.tv_sec = microseconds / 1000000;
17300 sp.tv_nsec = (microseconds % 1000000) * 1000;
17301 nanosleep(&sp, NULL);
17302 UNUSED_PARAMETER(NotUsed);
17303 return microseconds;
17304 #elif defined(HAVE_USLEEP) && HAVE_USLEEP
17305 usleep(microseconds);
17306 UNUSED_PARAMETER(NotUsed);
17307 return microseconds;
17308 #else
17309 int seconds = (microseconds+999999)/1000000;
17310 sleep(seconds);
17311 UNUSED_PARAMETER(NotUsed);
17312 return seconds*1000000;
17313 #endif
17314 }
17315
17316 /*
17317 ** The following variable, if set to a non-zero value, is interpreted as
17318 ** the number of seconds since 1970 and is used to set the result of
17319 ** sqlite3OsCurrentTime() during testing.
17320 */
17321 #ifdef SQLITE_TEST
17322 SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1 970. */
17323 #endif
17324
17325 /*
17326 ** Find the current time (in Universal Coordinated Time). Write into *piNow
17327 ** the current time and date as a Julian Day number times 86_400_000. In
17328 ** other words, write into *piNow the number of milliseconds since the Julian
17329 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
17330 ** proleptic Gregorian calendar.
17331 **
17332 ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
17333 ** cannot be found.
17334 */
17335 static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
17336 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
17337 int rc = SQLITE_OK;
17338 #if defined(NO_GETTOD)
17339 time_t t;
17340 time(&t);
17341 *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
17342 #elif OS_VXWORKS
17343 struct timespec sNow;
17344 clock_gettime(CLOCK_REALTIME, &sNow);
17345 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
17346 #else
17347 struct timeval sNow;
17348 (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */
17349 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
17350 #endif
17351
17352 #ifdef SQLITE_TEST
17353 if( sqlite3_current_time ){
17354 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
17355 }
17356 #endif
17357 UNUSED_PARAMETER(NotUsed);
17358 return rc;
17359 }
17360
17361 #ifndef SQLITE_OMIT_DEPRECATED
17362 /*
17363 ** Find the current time (in Universal Coordinated Time). Write the
17364 ** current time and date as a Julian Day number into *prNow and
17365 ** return 0. Return 1 if the time and date cannot be found.
17366 */
17367 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
17368 sqlite3_int64 i = 0;
17369 int rc;
17370 UNUSED_PARAMETER(NotUsed);
17371 rc = unixCurrentTimeInt64(0, &i);
17372 *prNow = i/86400000.0;
17373 return rc;
17374 }
17375 #else
17376 # define unixCurrentTime 0
17377 #endif
17378
17379 /*
17380 ** The xGetLastError() method is designed to return a better
17381 ** low-level error message when operating-system problems come up
17382 ** during SQLite operation. Only the integer return code is currently
17383 ** used.
17384 */
17385 static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
17386 UNUSED_PARAMETER(NotUsed);
17387 UNUSED_PARAMETER(NotUsed2);
17388 UNUSED_PARAMETER(NotUsed3);
17389 return errno;
17390 }
17391
17392
17393 /*
17394 ************************ End of sqlite3_vfs methods ***************************
17395 ******************************************************************************/
17396
17397 /******************************************************************************
17398 ************************** Begin Proxy Locking ********************************
17399 **
17400 ** Proxy locking is a "uber-locking-method" in this sense: It uses the
17401 ** other locking methods on secondary lock files. Proxy locking is a
17402 ** meta-layer over top of the primitive locking implemented above. For
17403 ** this reason, the division that implements of proxy locking is deferred
17404 ** until late in the file (here) after all of the other I/O methods have
17405 ** been defined - so that the primitive locking methods are available
17406 ** as services to help with the implementation of proxy locking.
17407 **
17408 ****
17409 **
17410 ** The default locking schemes in SQLite use byte-range locks on the
17411 ** database file to coordinate safe, concurrent access by multiple readers
17412 ** and writers [http://sqlite.org/lockingv3.html]. The five file locking
17413 ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
17414 ** as POSIX read & write locks over fixed set of locations (via fsctl),
17415 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
17416 ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
17417 ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
17418 ** address in the shared range is taken for a SHARED lock, the entire
17419 ** shared range is taken for an EXCLUSIVE lock):
17420 **
17421 ** PENDING_BYTE 0x40000000
17422 ** RESERVED_BYTE 0x40000001
17423 ** SHARED_RANGE 0x40000002 -> 0x40000200
17424 **
17425 ** This works well on the local file system, but shows a nearly 100x
17426 ** slowdown in read performance on AFP because the AFP client disables
17427 ** the read cache when byte-range locks are present. Enabling the read
17428 ** cache exposes a cache coherency problem that is present on all OS X
17429 ** supported network file systems. NFS and AFP both observe the
17430 ** close-to-open semantics for ensuring cache coherency
17431 ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
17432 ** address the requirements for concurrent database access by multiple
17433 ** readers and writers
17434 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
17435 **
17436 ** To address the performance and cache coherency issues, proxy file locking
17437 ** changes the way database access is controlled by limiting access to a
17438 ** single host at a time and moving file locks off of the database file
17439 ** and onto a proxy file on the local file system.
17440 **
17441 **
17442 ** Using proxy locks
17443 ** -----------------
17444 **
17445 ** C APIs
17446 **
17447 ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE,
17448 ** <proxy_path> | ":auto:");
17449 ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE,
17450 ** &<proxy_path>);
17451 **
17452 **
17453 ** SQL pragmas
17454 **
17455 ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
17456 ** PRAGMA [database.]lock_proxy_file
17457 **
17458 ** Specifying ":auto:" means that if there is a conch file with a matching
17459 ** host ID in it, the proxy path in the conch file will be used, otherwise
17460 ** a proxy path based on the user's temp dir
17461 ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
17462 ** actual proxy file name is generated from the name and path of the
17463 ** database file. For example:
17464 **
17465 ** For database path "/Users/me/foo.db"
17466 ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
17467 **
17468 ** Once a lock proxy is configured for a database connection, it can not
17469 ** be removed, however it may be switched to a different proxy path via
17470 ** the above APIs (assuming the conch file is not being held by another
17471 ** connection or process).
17472 **
17473 **
17474 ** How proxy locking works
17475 ** -----------------------
17476 **
17477 ** Proxy file locking relies primarily on two new supporting files:
17478 **
17479 ** * conch file to limit access to the database file to a single host
17480 ** at a time
17481 **
17482 ** * proxy file to act as a proxy for the advisory locks normally
17483 ** taken on the database
17484 **
17485 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
17486 ** by taking an sqlite-style shared lock on the conch file, reading the
17487 ** contents and comparing the host's unique host ID (see below) and lock
17488 ** proxy path against the values stored in the conch. The conch file is
17489 ** stored in the same directory as the database file and the file name
17490 ** is patterned after the database file name as ".<databasename>-conch".
17491 ** If the conch file does not exist, or its contents do not match the
17492 ** host ID and/or proxy path, then the lock is escalated to an exclusive
17493 ** lock and the conch file contents is updated with the host ID and proxy
17494 ** path and the lock is downgraded to a shared lock again. If the conch
17495 ** is held by another process (with a shared lock), the exclusive lock
17496 ** will fail and SQLITE_BUSY is returned.
17497 **
17498 ** The proxy file - a single-byte file used for all advisory file locks
17499 ** normally taken on the database file. This allows for safe sharing
17500 ** of the database file for multiple readers and writers on the same
17501 ** host (the conch ensures that they all use the same local lock file).
17502 **
17503 ** Requesting the lock proxy does not immediately take the conch, it is
17504 ** only taken when the first request to lock database file is made.
17505 ** This matches the semantics of the traditional locking behavior, where
17506 ** opening a connection to a database file does not take a lock on it.
17507 ** The shared lock and an open file descriptor are maintained until
17508 ** the connection to the database is closed.
17509 **
17510 ** The proxy file and the lock file are never deleted so they only need
17511 ** to be created the first time they are used.
17512 **
17513 ** Configuration options
17514 ** ---------------------
17515 **
17516 ** SQLITE_PREFER_PROXY_LOCKING
17517 **
17518 ** Database files accessed on non-local file systems are
17519 ** automatically configured for proxy locking, lock files are
17520 ** named automatically using the same logic as
17521 ** PRAGMA lock_proxy_file=":auto:"
17522 **
17523 ** SQLITE_PROXY_DEBUG
17524 **
17525 ** Enables the logging of error messages during host id file
17526 ** retrieval and creation
17527 **
17528 ** LOCKPROXYDIR
17529 **
17530 ** Overrides the default directory used for lock proxy files that
17531 ** are named automatically via the ":auto:" setting
17532 **
17533 ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
17534 **
17535 ** Permissions to use when creating a directory for storing the
17536 ** lock proxy files, only used when LOCKPROXYDIR is not set.
17537 **
17538 **
17539 ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
17540 ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
17541 ** force proxy locking to be used for every database file opened, and 0
17542 ** will force automatic proxy locking to be disabled for all database
17543 ** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or
17544 ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
17545 */
17546
17547 /*
17548 ** Proxy locking is only available on MacOSX
17549 */
17550 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
17551
17552 /*
17553 ** The proxyLockingContext has the path and file structures for the remote
17554 ** and local proxy files in it
17555 */
17556 typedef struct proxyLockingContext proxyLockingContext;
17557 struct proxyLockingContext {
17558 unixFile *conchFile; /* Open conch file */
17559 char *conchFilePath; /* Name of the conch file */
17560 unixFile *lockProxy; /* Open proxy lock file */
17561 char *lockProxyPath; /* Name of the proxy lock file */
17562 char *dbPath; /* Name of the open file */
17563 int conchHeld; /* 1 if the conch is held, -1 if lockless */
17564 int nFails; /* Number of conch taking failures */
17565 void *oldLockingContext; /* Original lockingcontext to restore on close */
17566 sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */
17567 };
17568
17569 /*
17570 ** The proxy lock file path for the database at dbPath is written into lPath,
17571 ** which must point to valid, writable memory large enough for a maxLen length
17572 ** file path.
17573 */
17574 static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
17575 int len;
17576 int dbLen;
17577 int i;
17578
17579 #ifdef LOCKPROXYDIR
17580 len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
17581 #else
17582 # ifdef _CS_DARWIN_USER_TEMP_DIR
17583 {
17584 if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
17585 OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n",
17586 lPath, errno, osGetpid(0)));
17587 return SQLITE_IOERR_LOCK;
17588 }
17589 len = strlcat(lPath, "sqliteplocks", maxLen);
17590 }
17591 # else
17592 len = strlcpy(lPath, "/tmp/", maxLen);
17593 # endif
17594 #endif
17595
17596 if( lPath[len-1]!='/' ){
17597 len = strlcat(lPath, "/", maxLen);
17598 }
17599
17600 /* transform the db path to a unique cache name */
17601 dbLen = (int)strlen(dbPath);
17602 for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
17603 char c = dbPath[i];
17604 lPath[i+len] = (c=='/')?'_':c;
17605 }
17606 lPath[i+len]='\0';
17607 strlcat(lPath, ":auto:", maxLen);
17608 OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0)));
17609 return SQLITE_OK;
17610 }
17611
17612 /*
17613 ** Creates the lock file and any missing directories in lockPath
17614 */
17615 static int proxyCreateLockPath(const char *lockPath){
17616 int i, len;
17617 char buf[MAXPATHLEN];
17618 int start = 0;
17619
17620 assert(lockPath!=NULL);
17621 /* try to create all the intermediate directories */
17622 len = (int)strlen(lockPath);
17623 buf[0] = lockPath[0];
17624 for( i=1; i<len; i++ ){
17625 if( lockPath[i] == '/' && (i - start > 0) ){
17626 /* only mkdir if leaf dir != "." or "/" or ".." */
17627 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
17628 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
17629 buf[i]='\0';
17630 if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
17631 int err=errno;
17632 if( err!=EEXIST ) {
17633 OSTRACE(("CREATELOCKPATH FAILED creating %s, "
17634 "'%s' proxy lock path=%s pid=%d\n",
17635 buf, strerror(err), lockPath, osGetpid(0)));
17636 return err;
17637 }
17638 }
17639 }
17640 start=i+1;
17641 }
17642 buf[i] = lockPath[i];
17643 }
17644 OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n",lockPath,osGetpid(0)));
17645 return 0;
17646 }
17647
17648 /*
17649 ** Create a new VFS file descriptor (stored in memory obtained from
17650 ** sqlite3_malloc) and open the file named "path" in the file descriptor.
17651 **
17652 ** The caller is responsible not only for closing the file descriptor
17653 ** but also for freeing the memory associated with the file descriptor.
17654 */
17655 static int proxyCreateUnixFile(
17656 const char *path, /* path for the new unixFile */
17657 unixFile **ppFile, /* unixFile created and returned by ref */
17658 int islockfile /* if non zero missing dirs will be created */
17659 ) {
17660 int fd = -1;
17661 unixFile *pNew;
17662 int rc = SQLITE_OK;
17663 int openFlags = O_RDWR | O_CREAT;
17664 sqlite3_vfs dummyVfs;
17665 int terrno = 0;
17666 UnixUnusedFd *pUnused = NULL;
17667
17668 /* 1. first try to open/create the file
17669 ** 2. if that fails, and this is a lock file (not-conch), try creating
17670 ** the parent directories and then try again.
17671 ** 3. if that fails, try to open the file read-only
17672 ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
17673 */
17674 pUnused = findReusableFd(path, openFlags);
17675 if( pUnused ){
17676 fd = pUnused->fd;
17677 }else{
17678 pUnused = sqlite3_malloc64(sizeof(*pUnused));
17679 if( !pUnused ){
17680 return SQLITE_NOMEM_BKPT;
17681 }
17682 }
17683 if( fd<0 ){
17684 fd = robust_open(path, openFlags, 0);
17685 terrno = errno;
17686 if( fd<0 && errno==ENOENT && islockfile ){
17687 if( proxyCreateLockPath(path) == SQLITE_OK ){
17688 fd = robust_open(path, openFlags, 0);
17689 }
17690 }
17691 }
17692 if( fd<0 ){
17693 openFlags = O_RDONLY;
17694 fd = robust_open(path, openFlags, 0);
17695 terrno = errno;
17696 }
17697 if( fd<0 ){
17698 if( islockfile ){
17699 return SQLITE_BUSY;
17700 }
17701 switch (terrno) {
17702 case EACCES:
17703 return SQLITE_PERM;
17704 case EIO:
17705 return SQLITE_IOERR_LOCK; /* even though it is the conch */
17706 default:
17707 return SQLITE_CANTOPEN_BKPT;
17708 }
17709 }
17710
17711 pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew));
17712 if( pNew==NULL ){
17713 rc = SQLITE_NOMEM_BKPT;
17714 goto end_create_proxy;
17715 }
17716 memset(pNew, 0, sizeof(unixFile));
17717 pNew->openFlags = openFlags;
17718 memset(&dummyVfs, 0, sizeof(dummyVfs));
17719 dummyVfs.pAppData = (void*)&autolockIoFinder;
17720 dummyVfs.zName = "dummy";
17721 pUnused->fd = fd;
17722 pUnused->flags = openFlags;
17723 pNew->pUnused = pUnused;
17724
17725 rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
17726 if( rc==SQLITE_OK ){
17727 *ppFile = pNew;
17728 return SQLITE_OK;
17729 }
17730 end_create_proxy:
17731 robust_close(pNew, fd, __LINE__);
17732 sqlite3_free(pNew);
17733 sqlite3_free(pUnused);
17734 return rc;
17735 }
17736
17737 #ifdef SQLITE_TEST
17738 /* simulate multiple hosts by creating unique hostid file paths */
17739 SQLITE_API int sqlite3_hostid_num = 0;
17740 #endif
17741
17742 #define PROXY_HOSTIDLEN 16 /* conch file host id length */
17743
17744 #ifdef HAVE_GETHOSTUUID
17745 /* Not always defined in the headers as it ought to be */
17746 extern int gethostuuid(uuid_t id, const struct timespec *wait);
17747 #endif
17748
17749 /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN
17750 ** bytes of writable memory.
17751 */
17752 static int proxyGetHostID(unsigned char *pHostID, int *pError){
17753 assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
17754 memset(pHostID, 0, PROXY_HOSTIDLEN);
17755 #ifdef HAVE_GETHOSTUUID
17756 {
17757 struct timespec timeout = {1, 0}; /* 1 sec timeout */
17758 if( gethostuuid(pHostID, &timeout) ){
17759 int err = errno;
17760 if( pError ){
17761 *pError = err;
17762 }
17763 return SQLITE_IOERR;
17764 }
17765 }
17766 #else
17767 UNUSED_PARAMETER(pError);
17768 #endif
17769 #ifdef SQLITE_TEST
17770 /* simulate multiple hosts by creating unique hostid file paths */
17771 if( sqlite3_hostid_num != 0){
17772 pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
17773 }
17774 #endif
17775
17776 return SQLITE_OK;
17777 }
17778
17779 /* The conch file contains the header, host id and lock file path
17780 */
17781 #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
17782 #define PROXY_HEADERLEN 1 /* conch file header length */
17783 #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
17784 #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
17785
17786 /*
17787 ** Takes an open conch file, copies the contents to a new path and then moves
17788 ** it back. The newly created file's file descriptor is assigned to the
17789 ** conch file structure and finally the original conch file descriptor is
17790 ** closed. Returns zero if successful.
17791 */
17792 static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
17793 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
17794 unixFile *conchFile = pCtx->conchFile;
17795 char tPath[MAXPATHLEN];
17796 char buf[PROXY_MAXCONCHLEN];
17797 char *cPath = pCtx->conchFilePath;
17798 size_t readLen = 0;
17799 size_t pathLen = 0;
17800 char errmsg[64] = "";
17801 int fd = -1;
17802 int rc = -1;
17803 UNUSED_PARAMETER(myHostID);
17804
17805 /* create a new path by replace the trailing '-conch' with '-break' */
17806 pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
17807 if( pathLen>MAXPATHLEN || pathLen<6 ||
17808 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
17809 sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
17810 goto end_breaklock;
17811 }
17812 /* read the conch content */
17813 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
17814 if( readLen<PROXY_PATHINDEX ){
17815 sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
17816 goto end_breaklock;
17817 }
17818 /* write it out to the temporary break file */
17819 fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0);
17820 if( fd<0 ){
17821 sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
17822 goto end_breaklock;
17823 }
17824 if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
17825 sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
17826 goto end_breaklock;
17827 }
17828 if( rename(tPath, cPath) ){
17829 sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
17830 goto end_breaklock;
17831 }
17832 rc = 0;
17833 fprintf(stderr, "broke stale lock on %s\n", cPath);
17834 robust_close(pFile, conchFile->h, __LINE__);
17835 conchFile->h = fd;
17836 conchFile->openFlags = O_RDWR | O_CREAT;
17837
17838 end_breaklock:
17839 if( rc ){
17840 if( fd>=0 ){
17841 osUnlink(tPath);
17842 robust_close(pFile, fd, __LINE__);
17843 }
17844 fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
17845 }
17846 return rc;
17847 }
17848
17849 /* Take the requested lock on the conch file and break a stale lock if the
17850 ** host id matches.
17851 */
17852 static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
17853 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
17854 unixFile *conchFile = pCtx->conchFile;
17855 int rc = SQLITE_OK;
17856 int nTries = 0;
17857 struct timespec conchModTime;
17858
17859 memset(&conchModTime, 0, sizeof(conchModTime));
17860 do {
17861 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
17862 nTries ++;
17863 if( rc==SQLITE_BUSY ){
17864 /* If the lock failed (busy):
17865 * 1st try: get the mod time of the conch, wait 0.5s and try again.
17866 * 2nd try: fail if the mod time changed or host id is different, wait
17867 * 10 sec and try again
17868 * 3rd try: break the lock unless the mod time has changed.
17869 */
17870 struct stat buf;
17871 if( osFstat(conchFile->h, &buf) ){
17872 storeLastErrno(pFile, errno);
17873 return SQLITE_IOERR_LOCK;
17874 }
17875
17876 if( nTries==1 ){
17877 conchModTime = buf.st_mtimespec;
17878 usleep(500000); /* wait 0.5 sec and try the lock again*/
17879 continue;
17880 }
17881
17882 assert( nTries>1 );
17883 if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec ||
17884 conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
17885 return SQLITE_BUSY;
17886 }
17887
17888 if( nTries==2 ){
17889 char tBuf[PROXY_MAXCONCHLEN];
17890 int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
17891 if( len<0 ){
17892 storeLastErrno(pFile, errno);
17893 return SQLITE_IOERR_LOCK;
17894 }
17895 if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
17896 /* don't break the lock if the host id doesn't match */
17897 if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
17898 return SQLITE_BUSY;
17899 }
17900 }else{
17901 /* don't break the lock on short read or a version mismatch */
17902 return SQLITE_BUSY;
17903 }
17904 usleep(10000000); /* wait 10 sec and try the lock again */
17905 continue;
17906 }
17907
17908 assert( nTries==3 );
17909 if( 0==proxyBreakConchLock(pFile, myHostID) ){
17910 rc = SQLITE_OK;
17911 if( lockType==EXCLUSIVE_LOCK ){
17912 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
17913 }
17914 if( !rc ){
17915 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
17916 }
17917 }
17918 }
17919 } while( rc==SQLITE_BUSY && nTries<3 );
17920
17921 return rc;
17922 }
17923
17924 /* Takes the conch by taking a shared lock and read the contents conch, if
17925 ** lockPath is non-NULL, the host ID and lock file path must match. A NULL
17926 ** lockPath means that the lockPath in the conch file will be used if the
17927 ** host IDs match, or a new lock path will be generated automatically
17928 ** and written to the conch file.
17929 */
17930 static int proxyTakeConch(unixFile *pFile){
17931 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
17932
17933 if( pCtx->conchHeld!=0 ){
17934 return SQLITE_OK;
17935 }else{
17936 unixFile *conchFile = pCtx->conchFile;
17937 uuid_t myHostID;
17938 int pError = 0;
17939 char readBuf[PROXY_MAXCONCHLEN];
17940 char lockPath[MAXPATHLEN];
17941 char *tempLockPath = NULL;
17942 int rc = SQLITE_OK;
17943 int createConch = 0;
17944 int hostIdMatch = 0;
17945 int readLen = 0;
17946 int tryOldLockPath = 0;
17947 int forceNewLockPath = 0;
17948
17949 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
17950 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
17951 osGetpid(0)));
17952
17953 rc = proxyGetHostID(myHostID, &pError);
17954 if( (rc&0xff)==SQLITE_IOERR ){
17955 storeLastErrno(pFile, pError);
17956 goto end_takeconch;
17957 }
17958 rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
17959 if( rc!=SQLITE_OK ){
17960 goto end_takeconch;
17961 }
17962 /* read the existing conch file */
17963 readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
17964 if( readLen<0 ){
17965 /* I/O error: lastErrno set by seekAndRead */
17966 storeLastErrno(pFile, conchFile->lastErrno);
17967 rc = SQLITE_IOERR_READ;
17968 goto end_takeconch;
17969 }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) ||
17970 readBuf[0]!=(char)PROXY_CONCHVERSION ){
17971 /* a short read or version format mismatch means we need to create a new
17972 ** conch file.
17973 */
17974 createConch = 1;
17975 }
17976 /* if the host id matches and the lock path already exists in the conch
17977 ** we'll try to use the path there, if we can't open that path, we'll
17978 ** retry with a new auto-generated path
17979 */
17980 do { /* in case we need to try again for an :auto: named lock file */
17981
17982 if( !createConch && !forceNewLockPath ){
17983 hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID,
17984 PROXY_HOSTIDLEN);
17985 /* if the conch has data compare the contents */
17986 if( !pCtx->lockProxyPath ){
17987 /* for auto-named local lock file, just check the host ID and we'll
17988 ** use the local lock file path that's already in there
17989 */
17990 if( hostIdMatch ){
17991 size_t pathLen = (readLen - PROXY_PATHINDEX);
17992
17993 if( pathLen>=MAXPATHLEN ){
17994 pathLen=MAXPATHLEN-1;
17995 }
17996 memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
17997 lockPath[pathLen] = 0;
17998 tempLockPath = lockPath;
17999 tryOldLockPath = 1;
18000 /* create a copy of the lock path if the conch is taken */
18001 goto end_takeconch;
18002 }
18003 }else if( hostIdMatch
18004 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
18005 readLen-PROXY_PATHINDEX)
18006 ){
18007 /* conch host and lock path match */
18008 goto end_takeconch;
18009 }
18010 }
18011
18012 /* if the conch isn't writable and doesn't match, we can't take it */
18013 if( (conchFile->openFlags&O_RDWR) == 0 ){
18014 rc = SQLITE_BUSY;
18015 goto end_takeconch;
18016 }
18017
18018 /* either the conch didn't match or we need to create a new one */
18019 if( !pCtx->lockProxyPath ){
18020 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
18021 tempLockPath = lockPath;
18022 /* create a copy of the lock path _only_ if the conch is taken */
18023 }
18024
18025 /* update conch with host and path (this will fail if other process
18026 ** has a shared lock already), if the host id matches, use the big
18027 ** stick.
18028 */
18029 futimes(conchFile->h, NULL);
18030 if( hostIdMatch && !createConch ){
18031 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
18032 /* We are trying for an exclusive lock but another thread in this
18033 ** same process is still holding a shared lock. */
18034 rc = SQLITE_BUSY;
18035 } else {
18036 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
18037 }
18038 }else{
18039 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
18040 }
18041 if( rc==SQLITE_OK ){
18042 char writeBuffer[PROXY_MAXCONCHLEN];
18043 int writeSize = 0;
18044
18045 writeBuffer[0] = (char)PROXY_CONCHVERSION;
18046 memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
18047 if( pCtx->lockProxyPath!=NULL ){
18048 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
18049 MAXPATHLEN);
18050 }else{
18051 strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
18052 }
18053 writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
18054 robust_ftruncate(conchFile->h, writeSize);
18055 rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
18056 full_fsync(conchFile->h,0,0);
18057 /* If we created a new conch file (not just updated the contents of a
18058 ** valid conch file), try to match the permissions of the database
18059 */
18060 if( rc==SQLITE_OK && createConch ){
18061 struct stat buf;
18062 int err = osFstat(pFile->h, &buf);
18063 if( err==0 ){
18064 mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
18065 S_IROTH|S_IWOTH);
18066 /* try to match the database file R/W permissions, ignore failure */
18067 #ifndef SQLITE_PROXY_DEBUG
18068 osFchmod(conchFile->h, cmode);
18069 #else
18070 do{
18071 rc = osFchmod(conchFile->h, cmode);
18072 }while( rc==(-1) && errno==EINTR );
18073 if( rc!=0 ){
18074 int code = errno;
18075 fprintf(stderr, "fchmod %o FAILED with %d %s\n",
18076 cmode, code, strerror(code));
18077 } else {
18078 fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
18079 }
18080 }else{
18081 int code = errno;
18082 fprintf(stderr, "STAT FAILED[%d] with %d %s\n",
18083 err, code, strerror(code));
18084 #endif
18085 }
18086 }
18087 }
18088 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
18089
18090 end_takeconch:
18091 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
18092 if( rc==SQLITE_OK && pFile->openFlags ){
18093 int fd;
18094 if( pFile->h>=0 ){
18095 robust_close(pFile, pFile->h, __LINE__);
18096 }
18097 pFile->h = -1;
18098 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
18099 OSTRACE(("TRANSPROXY: OPEN %d\n", fd));
18100 if( fd>=0 ){
18101 pFile->h = fd;
18102 }else{
18103 rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
18104 during locking */
18105 }
18106 }
18107 if( rc==SQLITE_OK && !pCtx->lockProxy ){
18108 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
18109 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
18110 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){
18111 /* we couldn't create the proxy lock file with the old lock file path
18112 ** so try again via auto-naming
18113 */
18114 forceNewLockPath = 1;
18115 tryOldLockPath = 0;
18116 continue; /* go back to the do {} while start point, try again */
18117 }
18118 }
18119 if( rc==SQLITE_OK ){
18120 /* Need to make a copy of path if we extracted the value
18121 ** from the conch file or the path was allocated on the stack
18122 */
18123 if( tempLockPath ){
18124 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
18125 if( !pCtx->lockProxyPath ){
18126 rc = SQLITE_NOMEM_BKPT;
18127 }
18128 }
18129 }
18130 if( rc==SQLITE_OK ){
18131 pCtx->conchHeld = 1;
18132
18133 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
18134 afpLockingContext *afpCtx;
18135 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
18136 afpCtx->dbPath = pCtx->lockProxyPath;
18137 }
18138 } else {
18139 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
18140 }
18141 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
18142 rc==SQLITE_OK?"ok":"failed"));
18143 return rc;
18144 } while (1); /* in case we need to retry the :auto: lock file -
18145 ** we should never get here except via the 'continue' call. */
18146 }
18147 }
18148
18149 /*
18150 ** If pFile holds a lock on a conch file, then release that lock.
18151 */
18152 static int proxyReleaseConch(unixFile *pFile){
18153 int rc = SQLITE_OK; /* Subroutine return code */
18154 proxyLockingContext *pCtx; /* The locking context for the proxy lock */
18155 unixFile *conchFile; /* Name of the conch file */
18156
18157 pCtx = (proxyLockingContext *)pFile->lockingContext;
18158 conchFile = pCtx->conchFile;
18159 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
18160 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
18161 osGetpid(0)));
18162 if( pCtx->conchHeld>0 ){
18163 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
18164 }
18165 pCtx->conchHeld = 0;
18166 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
18167 (rc==SQLITE_OK ? "ok" : "failed")));
18168 return rc;
18169 }
18170
18171 /*
18172 ** Given the name of a database file, compute the name of its conch file.
18173 ** Store the conch filename in memory obtained from sqlite3_malloc64().
18174 ** Make *pConchPath point to the new name. Return SQLITE_OK on success
18175 ** or SQLITE_NOMEM if unable to obtain memory.
18176 **
18177 ** The caller is responsible for ensuring that the allocated memory
18178 ** space is eventually freed.
18179 **
18180 ** *pConchPath is set to NULL if a memory allocation error occurs.
18181 */
18182 static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
18183 int i; /* Loop counter */
18184 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
18185 char *conchPath; /* buffer in which to construct conch name */
18186
18187 /* Allocate space for the conch filename and initialize the name to
18188 ** the name of the original database file. */
18189 *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8);
18190 if( conchPath==0 ){
18191 return SQLITE_NOMEM_BKPT;
18192 }
18193 memcpy(conchPath, dbPath, len+1);
18194
18195 /* now insert a "." before the last / character */
18196 for( i=(len-1); i>=0; i-- ){
18197 if( conchPath[i]=='/' ){
18198 i++;
18199 break;
18200 }
18201 }
18202 conchPath[i]='.';
18203 while ( i<len ){
18204 conchPath[i+1]=dbPath[i];
18205 i++;
18206 }
18207
18208 /* append the "-conch" suffix to the file */
18209 memcpy(&conchPath[i+1], "-conch", 7);
18210 assert( (int)strlen(conchPath) == len+7 );
18211
18212 return SQLITE_OK;
18213 }
18214
18215
18216 /* Takes a fully configured proxy locking-style unix file and switches
18217 ** the local lock file path
18218 */
18219 static int switchLockProxyPath(unixFile *pFile, const char *path) {
18220 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
18221 char *oldPath = pCtx->lockProxyPath;
18222 int rc = SQLITE_OK;
18223
18224 if( pFile->eFileLock!=NO_LOCK ){
18225 return SQLITE_BUSY;
18226 }
18227
18228 /* nothing to do if the path is NULL, :auto: or matches the existing path */
18229 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
18230 (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
18231 return SQLITE_OK;
18232 }else{
18233 unixFile *lockProxy = pCtx->lockProxy;
18234 pCtx->lockProxy=NULL;
18235 pCtx->conchHeld = 0;
18236 if( lockProxy!=NULL ){
18237 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
18238 if( rc ) return rc;
18239 sqlite3_free(lockProxy);
18240 }
18241 sqlite3_free(oldPath);
18242 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
18243 }
18244
18245 return rc;
18246 }
18247
18248 /*
18249 ** pFile is a file that has been opened by a prior xOpen call. dbPath
18250 ** is a string buffer at least MAXPATHLEN+1 characters in size.
18251 **
18252 ** This routine find the filename associated with pFile and writes it
18253 ** int dbPath.
18254 */
18255 static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
18256 #if defined(__APPLE__)
18257 if( pFile->pMethod == &afpIoMethods ){
18258 /* afp style keeps a reference to the db path in the filePath field
18259 ** of the struct */
18260 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
18261 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
18262 MAXPATHLEN);
18263 } else
18264 #endif
18265 if( pFile->pMethod == &dotlockIoMethods ){
18266 /* dot lock style uses the locking context to store the dot lock
18267 ** file path */
18268 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
18269 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
18270 }else{
18271 /* all other styles use the locking context to store the db file path */
18272 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
18273 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
18274 }
18275 return SQLITE_OK;
18276 }
18277
18278 /*
18279 ** Takes an already filled in unix file and alters it so all file locking
18280 ** will be performed on the local proxy lock file. The following fields
18281 ** are preserved in the locking context so that they can be restored and
18282 ** the unix structure properly cleaned up at close time:
18283 ** ->lockingContext
18284 ** ->pMethod
18285 */
18286 static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
18287 proxyLockingContext *pCtx;
18288 char dbPath[MAXPATHLEN+1]; /* Name of the database file */
18289 char *lockPath=NULL;
18290 int rc = SQLITE_OK;
18291
18292 if( pFile->eFileLock!=NO_LOCK ){
18293 return SQLITE_BUSY;
18294 }
18295 proxyGetDbPathForUnixFile(pFile, dbPath);
18296 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
18297 lockPath=NULL;
18298 }else{
18299 lockPath=(char *)path;
18300 }
18301
18302 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
18303 (lockPath ? lockPath : ":auto:"), osGetpid(0)));
18304
18305 pCtx = sqlite3_malloc64( sizeof(*pCtx) );
18306 if( pCtx==0 ){
18307 return SQLITE_NOMEM_BKPT;
18308 }
18309 memset(pCtx, 0, sizeof(*pCtx));
18310
18311 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
18312 if( rc==SQLITE_OK ){
18313 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
18314 if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
18315 /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
18316 ** (c) the file system is read-only, then enable no-locking access.
18317 ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
18318 ** that openFlags will have only one of O_RDONLY or O_RDWR.
18319 */
18320 struct statfs fsInfo;
18321 struct stat conchInfo;
18322 int goLockless = 0;
18323
18324 if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
18325 int err = errno;
18326 if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
18327 goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
18328 }
18329 }
18330 if( goLockless ){
18331 pCtx->conchHeld = -1; /* read only FS/ lockless */
18332 rc = SQLITE_OK;
18333 }
18334 }
18335 }
18336 if( rc==SQLITE_OK && lockPath ){
18337 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
18338 }
18339
18340 if( rc==SQLITE_OK ){
18341 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
18342 if( pCtx->dbPath==NULL ){
18343 rc = SQLITE_NOMEM_BKPT;
18344 }
18345 }
18346 if( rc==SQLITE_OK ){
18347 /* all memory is allocated, proxys are created and assigned,
18348 ** switch the locking context and pMethod then return.
18349 */
18350 pCtx->oldLockingContext = pFile->lockingContext;
18351 pFile->lockingContext = pCtx;
18352 pCtx->pOldMethod = pFile->pMethod;
18353 pFile->pMethod = &proxyIoMethods;
18354 }else{
18355 if( pCtx->conchFile ){
18356 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
18357 sqlite3_free(pCtx->conchFile);
18358 }
18359 sqlite3DbFree(0, pCtx->lockProxyPath);
18360 sqlite3_free(pCtx->conchFilePath);
18361 sqlite3_free(pCtx);
18362 }
18363 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
18364 (rc==SQLITE_OK ? "ok" : "failed")));
18365 return rc;
18366 }
18367
18368
18369 /*
18370 ** This routine handles sqlite3_file_control() calls that are specific
18371 ** to proxy locking.
18372 */
18373 static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
18374 switch( op ){
18375 case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
18376 unixFile *pFile = (unixFile*)id;
18377 if( pFile->pMethod == &proxyIoMethods ){
18378 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
18379 proxyTakeConch(pFile);
18380 if( pCtx->lockProxyPath ){
18381 *(const char **)pArg = pCtx->lockProxyPath;
18382 }else{
18383 *(const char **)pArg = ":auto: (not held)";
18384 }
18385 } else {
18386 *(const char **)pArg = NULL;
18387 }
18388 return SQLITE_OK;
18389 }
18390 case SQLITE_FCNTL_SET_LOCKPROXYFILE: {
18391 unixFile *pFile = (unixFile*)id;
18392 int rc = SQLITE_OK;
18393 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
18394 if( pArg==NULL || (const char *)pArg==0 ){
18395 if( isProxyStyle ){
18396 /* turn off proxy locking - not supported. If support is added for
18397 ** switching proxy locking mode off then it will need to fail if
18398 ** the journal mode is WAL mode.
18399 */
18400 rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
18401 }else{
18402 /* turn off proxy locking - already off - NOOP */
18403 rc = SQLITE_OK;
18404 }
18405 }else{
18406 const char *proxyPath = (const char *)pArg;
18407 if( isProxyStyle ){
18408 proxyLockingContext *pCtx =
18409 (proxyLockingContext*)pFile->lockingContext;
18410 if( !strcmp(pArg, ":auto:")
18411 || (pCtx->lockProxyPath &&
18412 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
18413 ){
18414 rc = SQLITE_OK;
18415 }else{
18416 rc = switchLockProxyPath(pFile, proxyPath);
18417 }
18418 }else{
18419 /* turn on proxy file locking */
18420 rc = proxyTransformUnixFile(pFile, proxyPath);
18421 }
18422 }
18423 return rc;
18424 }
18425 default: {
18426 assert( 0 ); /* The call assures that only valid opcodes are sent */
18427 }
18428 }
18429 /*NOTREACHED*/
18430 return SQLITE_ERROR;
18431 }
18432
18433 /*
18434 ** Within this division (the proxying locking implementation) the procedures
18435 ** above this point are all utilities. The lock-related methods of the
18436 ** proxy-locking sqlite3_io_method object follow.
18437 */
18438
18439
18440 /*
18441 ** This routine checks if there is a RESERVED lock held on the specified
18442 ** file by this or any other process. If such a lock is held, set *pResOut
18443 ** to a non-zero value otherwise *pResOut is set to zero. The return value
18444 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
18445 */
18446 static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
18447 unixFile *pFile = (unixFile*)id;
18448 int rc = proxyTakeConch(pFile);
18449 if( rc==SQLITE_OK ){
18450 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
18451 if( pCtx->conchHeld>0 ){
18452 unixFile *proxy = pCtx->lockProxy;
18453 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
18454 }else{ /* conchHeld < 0 is lockless */
18455 pResOut=0;
18456 }
18457 }
18458 return rc;
18459 }
18460
18461 /*
18462 ** Lock the file with the lock specified by parameter eFileLock - one
18463 ** of the following:
18464 **
18465 ** (1) SHARED_LOCK
18466 ** (2) RESERVED_LOCK
18467 ** (3) PENDING_LOCK
18468 ** (4) EXCLUSIVE_LOCK
18469 **
18470 ** Sometimes when requesting one lock state, additional lock states
18471 ** are inserted in between. The locking might fail on one of the later
18472 ** transitions leaving the lock state different from what it started but
18473 ** still short of its goal. The following chart shows the allowed
18474 ** transitions and the inserted intermediate states:
18475 **
18476 ** UNLOCKED -> SHARED
18477 ** SHARED -> RESERVED
18478 ** SHARED -> (PENDING) -> EXCLUSIVE
18479 ** RESERVED -> (PENDING) -> EXCLUSIVE
18480 ** PENDING -> EXCLUSIVE
18481 **
18482 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
18483 ** routine to lower a locking level.
18484 */
18485 static int proxyLock(sqlite3_file *id, int eFileLock) {
18486 unixFile *pFile = (unixFile*)id;
18487 int rc = proxyTakeConch(pFile);
18488 if( rc==SQLITE_OK ){
18489 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
18490 if( pCtx->conchHeld>0 ){
18491 unixFile *proxy = pCtx->lockProxy;
18492 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
18493 pFile->eFileLock = proxy->eFileLock;
18494 }else{
18495 /* conchHeld < 0 is lockless */
18496 }
18497 }
18498 return rc;
18499 }
18500
18501
18502 /*
18503 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
18504 ** must be either NO_LOCK or SHARED_LOCK.
18505 **
18506 ** If the locking level of the file descriptor is already at or below
18507 ** the requested locking level, this routine is a no-op.
18508 */
18509 static int proxyUnlock(sqlite3_file *id, int eFileLock) {
18510 unixFile *pFile = (unixFile*)id;
18511 int rc = proxyTakeConch(pFile);
18512 if( rc==SQLITE_OK ){
18513 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
18514 if( pCtx->conchHeld>0 ){
18515 unixFile *proxy = pCtx->lockProxy;
18516 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
18517 pFile->eFileLock = proxy->eFileLock;
18518 }else{
18519 /* conchHeld < 0 is lockless */
18520 }
18521 }
18522 return rc;
18523 }
18524
18525 /*
18526 ** Close a file that uses proxy locks.
18527 */
18528 static int proxyClose(sqlite3_file *id) {
18529 if( ALWAYS(id) ){
18530 unixFile *pFile = (unixFile*)id;
18531 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
18532 unixFile *lockProxy = pCtx->lockProxy;
18533 unixFile *conchFile = pCtx->conchFile;
18534 int rc = SQLITE_OK;
18535
18536 if( lockProxy ){
18537 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
18538 if( rc ) return rc;
18539 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
18540 if( rc ) return rc;
18541 sqlite3_free(lockProxy);
18542 pCtx->lockProxy = 0;
18543 }
18544 if( conchFile ){
18545 if( pCtx->conchHeld ){
18546 rc = proxyReleaseConch(pFile);
18547 if( rc ) return rc;
18548 }
18549 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
18550 if( rc ) return rc;
18551 sqlite3_free(conchFile);
18552 }
18553 sqlite3DbFree(0, pCtx->lockProxyPath);
18554 sqlite3_free(pCtx->conchFilePath);
18555 sqlite3DbFree(0, pCtx->dbPath);
18556 /* restore the original locking context and pMethod then close it */
18557 pFile->lockingContext = pCtx->oldLockingContext;
18558 pFile->pMethod = pCtx->pOldMethod;
18559 sqlite3_free(pCtx);
18560 return pFile->pMethod->xClose(id);
18561 }
18562 return SQLITE_OK;
18563 }
18564
18565
18566
18567 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
18568 /*
18569 ** The proxy locking style is intended for use with AFP filesystems.
18570 ** And since AFP is only supported on MacOSX, the proxy locking is also
18571 ** restricted to MacOSX.
18572 **
18573 **
18574 ******************* End of the proxy lock implementation **********************
18575 ******************************************************************************/
18576
18577 /*
18578 ** Initialize the operating system interface.
18579 **
18580 ** This routine registers all VFS implementations for unix-like operating
18581 ** systems. This routine, and the sqlite3_os_end() routine that follows,
18582 ** should be the only routines in this file that are visible from other
18583 ** files.
18584 **
18585 ** This routine is called once during SQLite initialization and by a
18586 ** single thread. The memory allocation and mutex subsystems have not
18587 ** necessarily been initialized when this routine is called, and so they
18588 ** should not be used.
18589 */
18590 SQLITE_API int sqlite3_os_init(void){
18591 /*
18592 ** The following macro defines an initializer for an sqlite3_vfs object.
18593 ** The name of the VFS is NAME. The pAppData is a pointer to a pointer
18594 ** to the "finder" function. (pAppData is a pointer to a pointer because
18595 ** silly C90 rules prohibit a void* from being cast to a function pointer
18596 ** and so we have to go through the intermediate pointer to avoid problems
18597 ** when compiling with -pedantic-errors on GCC.)
18598 **
18599 ** The FINDER parameter to this macro is the name of the pointer to the
18600 ** finder-function. The finder-function returns a pointer to the
18601 ** sqlite_io_methods object that implements the desired locking
18602 ** behaviors. See the division above that contains the IOMETHODS
18603 ** macro for addition information on finder-functions.
18604 **
18605 ** Most finders simply return a pointer to a fixed sqlite3_io_methods
18606 ** object. But the "autolockIoFinder" available on MacOSX does a little
18607 ** more than that; it looks at the filesystem type that hosts the
18608 ** database file and tries to choose an locking method appropriate for
18609 ** that filesystem time.
18610 */
18611 #define UNIXVFS(VFSNAME, FINDER) { \
18612 3, /* iVersion */ \
18613 sizeof(unixFile), /* szOsFile */ \
18614 MAX_PATHNAME, /* mxPathname */ \
18615 0, /* pNext */ \
18616 VFSNAME, /* zName */ \
18617 (void*)&FINDER, /* pAppData */ \
18618 unixOpen, /* xOpen */ \
18619 unixDelete, /* xDelete */ \
18620 unixAccess, /* xAccess */ \
18621 unixFullPathname, /* xFullPathname */ \
18622 unixDlOpen, /* xDlOpen */ \
18623 unixDlError, /* xDlError */ \
18624 unixDlSym, /* xDlSym */ \
18625 unixDlClose, /* xDlClose */ \
18626 unixRandomness, /* xRandomness */ \
18627 unixSleep, /* xSleep */ \
18628 unixCurrentTime, /* xCurrentTime */ \
18629 unixGetLastError, /* xGetLastError */ \
18630 unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \
18631 unixSetSystemCall, /* xSetSystemCall */ \
18632 unixGetSystemCall, /* xGetSystemCall */ \
18633 unixNextSystemCall, /* xNextSystemCall */ \
18634 }
18635
18636 /*
18637 ** All default VFSes for unix are contained in the following array.
18638 **
18639 ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
18640 ** by the SQLite core when the VFS is registered. So the following
18641 ** array cannot be const.
18642 */
18643 static sqlite3_vfs aVfs[] = {
18644 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
18645 UNIXVFS("unix", autolockIoFinder ),
18646 #elif OS_VXWORKS
18647 UNIXVFS("unix", vxworksIoFinder ),
18648 #else
18649 UNIXVFS("unix", posixIoFinder ),
18650 #endif
18651 UNIXVFS("unix-none", nolockIoFinder ),
18652 UNIXVFS("unix-dotfile", dotlockIoFinder ),
18653 UNIXVFS("unix-excl", posixIoFinder ),
18654 #if OS_VXWORKS
18655 UNIXVFS("unix-namedsem", semIoFinder ),
18656 #endif
18657 #if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS
18658 UNIXVFS("unix-posix", posixIoFinder ),
18659 #endif
18660 #if SQLITE_ENABLE_LOCKING_STYLE
18661 UNIXVFS("unix-flock", flockIoFinder ),
18662 #endif
18663 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
18664 UNIXVFS("unix-afp", afpIoFinder ),
18665 UNIXVFS("unix-nfs", nfsIoFinder ),
18666 UNIXVFS("unix-proxy", proxyIoFinder ),
18667 #endif
18668 };
18669 unsigned int i; /* Loop counter */
18670
18671 /* Double-check that the aSyscall[] array has been constructed
18672 ** correctly. See ticket [bb3a86e890c8e96ab] */
18673 assert( ArraySize(aSyscall)==28 );
18674
18675 /* Register all VFSes defined in the aVfs[] array */
18676 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
18677 sqlite3_vfs_register(&aVfs[i], i==0);
18678 }
18679 return SQLITE_OK;
18680 }
18681
18682 /*
18683 ** Shutdown the operating system interface.
18684 **
18685 ** Some operating systems might need to do some cleanup in this routine,
18686 ** to release dynamically allocated objects. But not on unix.
18687 ** This routine is a no-op for unix.
18688 */
18689 SQLITE_API int sqlite3_os_end(void){
18690 return SQLITE_OK;
18691 }
18692
18693 #endif /* SQLITE_OS_UNIX */
18694
18695 /************** End of os_unix.c *********************************************/
18696 /************** Begin file os_win.c ******************************************/
18697 /*
18698 ** 2004 May 22
18699 **
18700 ** The author disclaims copyright to this source code. In place of
18701 ** a legal notice, here is a blessing:
18702 **
18703 ** May you do good and not evil.
18704 ** May you find forgiveness for yourself and forgive others.
18705 ** May you share freely, never taking more than you give.
18706 **
18707 ******************************************************************************
18708 **
18709 ** This file contains code that is specific to Windows.
18710 */
18711 /* #include "sqliteInt.h" */
18712 #if SQLITE_OS_WIN /* This file is used for Windows only */
18713
18714 /*
18715 ** Include code that is common to all os_*.c files
18716 */
18717 /************** Include os_common.h in the middle of os_win.c ****************/
18718 /************** Begin file os_common.h ***************************************/
18719 /*
18720 ** 2004 May 22
18721 **
18722 ** The author disclaims copyright to this source code. In place of
18723 ** a legal notice, here is a blessing:
18724 **
18725 ** May you do good and not evil.
18726 ** May you find forgiveness for yourself and forgive others.
18727 ** May you share freely, never taking more than you give.
18728 **
18729 ******************************************************************************
18730 **
18731 ** This file contains macros and a little bit of code that is common to
18732 ** all of the platform-specific files (os_*.c) and is #included into those
18733 ** files.
18734 **
18735 ** This file should be #included by the os_*.c files only. It is not a
18736 ** general purpose header file.
18737 */
18738 #ifndef _OS_COMMON_H_
18739 #define _OS_COMMON_H_
18740
18741 /*
18742 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
18743 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
18744 ** switch. The following code should catch this problem at compile-time.
18745 */
18746 #ifdef MEMORY_DEBUG
18747 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
18748 #endif
18749
18750 /*
18751 ** Macros for performance tracing. Normally turned off. Only works
18752 ** on i486 hardware.
18753 */
18754 #ifdef SQLITE_PERFORMANCE_TRACE
18755
18756 /*
18757 ** hwtime.h contains inline assembler code for implementing
18758 ** high-performance timing routines.
18759 */
18760 /************** Include hwtime.h in the middle of os_common.h ****************/
18761 /************** Begin file hwtime.h ******************************************/
18762 /*
18763 ** 2008 May 27
18764 **
18765 ** The author disclaims copyright to this source code. In place of
18766 ** a legal notice, here is a blessing:
18767 **
18768 ** May you do good and not evil.
18769 ** May you find forgiveness for yourself and forgive others.
18770 ** May you share freely, never taking more than you give.
18771 **
18772 ******************************************************************************
18773 **
18774 ** This file contains inline asm code for retrieving "high-performance"
18775 ** counters for x86 class CPUs.
18776 */
18777 #ifndef SQLITE_HWTIME_H
18778 #define SQLITE_HWTIME_H
18779
18780 /*
18781 ** The following routine only works on pentium-class (or newer) processors.
18782 ** It uses the RDTSC opcode to read the cycle count value out of the
18783 ** processor and returns that value. This can be used for high-res
18784 ** profiling.
18785 */
18786 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
18787 (defined(i386) || defined(__i386__) || defined(_M_IX86))
18788
18789 #if defined(__GNUC__)
18790
18791 __inline__ sqlite_uint64 sqlite3Hwtime(void){
18792 unsigned int lo, hi;
18793 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
18794 return (sqlite_uint64)hi << 32 | lo;
18795 }
18796
18797 #elif defined(_MSC_VER)
18798
18799 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
18800 __asm {
18801 rdtsc
18802 ret ; return value at EDX:EAX
18803 }
18804 }
18805
18806 #endif
18807
18808 #elif (defined(__GNUC__) && defined(__x86_64__))
18809
18810 __inline__ sqlite_uint64 sqlite3Hwtime(void){
18811 unsigned long val;
18812 __asm__ __volatile__ ("rdtsc" : "=A" (val));
18813 return val;
18814 }
18815
18816 #elif (defined(__GNUC__) && defined(__ppc__))
18817
18818 __inline__ sqlite_uint64 sqlite3Hwtime(void){
18819 unsigned long long retval;
18820 unsigned long junk;
18821 __asm__ __volatile__ ("\n\
18822 1: mftbu %1\n\
18823 mftb %L0\n\
18824 mftbu %0\n\
18825 cmpw %0,%1\n\
18826 bne 1b"
18827 : "=r" (retval), "=r" (junk));
18828 return retval;
18829 }
18830
18831 #else
18832
18833 #error Need implementation of sqlite3Hwtime() for your platform.
18834
18835 /*
18836 ** To compile without implementing sqlite3Hwtime() for your platform,
18837 ** you can remove the above #error and use the following
18838 ** stub function. You will lose timing support for many
18839 ** of the debugging and testing utilities, but it should at
18840 ** least compile and run.
18841 */
18842 SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
18843
18844 #endif
18845
18846 #endif /* !defined(SQLITE_HWTIME_H) */
18847
18848 /************** End of hwtime.h **********************************************/
18849 /************** Continuing where we left off in os_common.h ******************/
18850
18851 static sqlite_uint64 g_start;
18852 static sqlite_uint64 g_elapsed;
18853 #define TIMER_START g_start=sqlite3Hwtime()
18854 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
18855 #define TIMER_ELAPSED g_elapsed
18856 #else
18857 #define TIMER_START
18858 #define TIMER_END
18859 #define TIMER_ELAPSED ((sqlite_uint64)0)
18860 #endif
18861
18862 /*
18863 ** If we compile with the SQLITE_TEST macro set, then the following block
18864 ** of code will give us the ability to simulate a disk I/O error. This
18865 ** is used for testing the I/O recovery logic.
18866 */
18867 #if defined(SQLITE_TEST)
18868 SQLITE_API extern int sqlite3_io_error_hit;
18869 SQLITE_API extern int sqlite3_io_error_hardhit;
18870 SQLITE_API extern int sqlite3_io_error_pending;
18871 SQLITE_API extern int sqlite3_io_error_persist;
18872 SQLITE_API extern int sqlite3_io_error_benign;
18873 SQLITE_API extern int sqlite3_diskfull_pending;
18874 SQLITE_API extern int sqlite3_diskfull;
18875 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
18876 #define SimulateIOError(CODE) \
18877 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
18878 || sqlite3_io_error_pending-- == 1 ) \
18879 { local_ioerr(); CODE; }
18880 static void local_ioerr(){
18881 IOTRACE(("IOERR\n"));
18882 sqlite3_io_error_hit++;
18883 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
18884 }
18885 #define SimulateDiskfullError(CODE) \
18886 if( sqlite3_diskfull_pending ){ \
18887 if( sqlite3_diskfull_pending == 1 ){ \
18888 local_ioerr(); \
18889 sqlite3_diskfull = 1; \
18890 sqlite3_io_error_hit = 1; \
18891 CODE; \
18892 }else{ \
18893 sqlite3_diskfull_pending--; \
18894 } \
18895 }
18896 #else
18897 #define SimulateIOErrorBenign(X)
18898 #define SimulateIOError(A)
18899 #define SimulateDiskfullError(A)
18900 #endif /* defined(SQLITE_TEST) */
18901
18902 /*
18903 ** When testing, keep a count of the number of open files.
18904 */
18905 #if defined(SQLITE_TEST)
18906 SQLITE_API extern int sqlite3_open_file_count;
18907 #define OpenCounter(X) sqlite3_open_file_count+=(X)
18908 #else
18909 #define OpenCounter(X)
18910 #endif /* defined(SQLITE_TEST) */
18911
18912 #endif /* !defined(_OS_COMMON_H_) */
18913
18914 /************** End of os_common.h *******************************************/
18915 /************** Continuing where we left off in os_win.c *********************/
18916
18917 /*
18918 ** Include the header file for the Windows VFS.
18919 */
18920 /* #include "os_win.h" */
18921
18922 /*
18923 ** Compiling and using WAL mode requires several APIs that are only
18924 ** available in Windows platforms based on the NT kernel.
18925 */
18926 #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
18927 # error "WAL mode requires support from the Windows NT kernel, compile\
18928 with SQLITE_OMIT_WAL."
18929 #endif
18930
18931 #if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE>0
18932 # error "Memory mapped files require support from the Windows NT kernel,\
18933 compile with SQLITE_MAX_MMAP_SIZE=0."
18934 #endif
18935
18936 /*
18937 ** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
18938 ** based on the sub-platform)?
18939 */
18940 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
18941 # define SQLITE_WIN32_HAS_ANSI
18942 #endif
18943
18944 /*
18945 ** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
18946 ** based on the sub-platform)?
18947 */
18948 #if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \
18949 !defined(SQLITE_WIN32_NO_WIDE)
18950 # define SQLITE_WIN32_HAS_WIDE
18951 #endif
18952
18953 /*
18954 ** Make sure at least one set of Win32 APIs is available.
18955 */
18956 #if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE)
18957 # error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\
18958 must be defined."
18959 #endif
18960
18961 /*
18962 ** Define the required Windows SDK version constants if they are not
18963 ** already available.
18964 */
18965 #ifndef NTDDI_WIN8
18966 # define NTDDI_WIN8 0x06020000
18967 #endif
18968
18969 #ifndef NTDDI_WINBLUE
18970 # define NTDDI_WINBLUE 0x06030000
18971 #endif
18972
18973 #ifndef NTDDI_WINTHRESHOLD
18974 # define NTDDI_WINTHRESHOLD 0x06040000
18975 #endif
18976
18977 /*
18978 ** Check to see if the GetVersionEx[AW] functions are deprecated on the
18979 ** target system. GetVersionEx was first deprecated in Win8.1.
18980 */
18981 #ifndef SQLITE_WIN32_GETVERSIONEX
18982 # if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
18983 # define SQLITE_WIN32_GETVERSIONEX 0 /* GetVersionEx() is deprecated */
18984 # else
18985 # define SQLITE_WIN32_GETVERSIONEX 1 /* GetVersionEx() is current */
18986 # endif
18987 #endif
18988
18989 /*
18990 ** Check to see if the CreateFileMappingA function is supported on the
18991 ** target system. It is unavailable when using "mincore.lib" on Win10.
18992 ** When compiling for Windows 10, always assume "mincore.lib" is in use.
18993 */
18994 #ifndef SQLITE_WIN32_CREATEFILEMAPPINGA
18995 # if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINTHRESHOLD
18996 # define SQLITE_WIN32_CREATEFILEMAPPINGA 0
18997 # else
18998 # define SQLITE_WIN32_CREATEFILEMAPPINGA 1
18999 # endif
19000 #endif
19001
19002 /*
19003 ** This constant should already be defined (in the "WinDef.h" SDK file).
19004 */
19005 #ifndef MAX_PATH
19006 # define MAX_PATH (260)
19007 #endif
19008
19009 /*
19010 ** Maximum pathname length (in chars) for Win32. This should normally be
19011 ** MAX_PATH.
19012 */
19013 #ifndef SQLITE_WIN32_MAX_PATH_CHARS
19014 # define SQLITE_WIN32_MAX_PATH_CHARS (MAX_PATH)
19015 #endif
19016
19017 /*
19018 ** This constant should already be defined (in the "WinNT.h" SDK file).
19019 */
19020 #ifndef UNICODE_STRING_MAX_CHARS
19021 # define UNICODE_STRING_MAX_CHARS (32767)
19022 #endif
19023
19024 /*
19025 ** Maximum pathname length (in chars) for WinNT. This should normally be
19026 ** UNICODE_STRING_MAX_CHARS.
19027 */
19028 #ifndef SQLITE_WINNT_MAX_PATH_CHARS
19029 # define SQLITE_WINNT_MAX_PATH_CHARS (UNICODE_STRING_MAX_CHARS)
19030 #endif
19031
19032 /*
19033 ** Maximum pathname length (in bytes) for Win32. The MAX_PATH macro is in
19034 ** characters, so we allocate 4 bytes per character assuming worst-case of
19035 ** 4-bytes-per-character for UTF8.
19036 */
19037 #ifndef SQLITE_WIN32_MAX_PATH_BYTES
19038 # define SQLITE_WIN32_MAX_PATH_BYTES (SQLITE_WIN32_MAX_PATH_CHARS*4)
19039 #endif
19040
19041 /*
19042 ** Maximum pathname length (in bytes) for WinNT. This should normally be
19043 ** UNICODE_STRING_MAX_CHARS * sizeof(WCHAR).
19044 */
19045 #ifndef SQLITE_WINNT_MAX_PATH_BYTES
19046 # define SQLITE_WINNT_MAX_PATH_BYTES \
19047 (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
19048 #endif
19049
19050 /*
19051 ** Maximum error message length (in chars) for WinRT.
19052 */
19053 #ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
19054 # define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
19055 #endif
19056
19057 /*
19058 ** Returns non-zero if the character should be treated as a directory
19059 ** separator.
19060 */
19061 #ifndef winIsDirSep
19062 # define winIsDirSep(a) (((a) == '/') || ((a) == '\\'))
19063 #endif
19064
19065 /*
19066 ** This macro is used when a local variable is set to a value that is
19067 ** [sometimes] not used by the code (e.g. via conditional compilation).
19068 */
19069 #ifndef UNUSED_VARIABLE_VALUE
19070 # define UNUSED_VARIABLE_VALUE(x) (void)(x)
19071 #endif
19072
19073 /*
19074 ** Returns the character that should be used as the directory separator.
19075 */
19076 #ifndef winGetDirSep
19077 # define winGetDirSep() '\\'
19078 #endif
19079
19080 /*
19081 ** Do we need to manually define the Win32 file mapping APIs for use with WAL
19082 ** mode or memory mapped files (e.g. these APIs are available in the Windows
19083 ** CE SDK; however, they are not present in the header file)?
19084 */
19085 #if SQLITE_WIN32_FILEMAPPING_API && \
19086 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
19087 /*
19088 ** Two of the file mapping APIs are different under WinRT. Figure out which
19089 ** set we need.
19090 */
19091 #if SQLITE_OS_WINRT
19092 WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \
19093 LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR);
19094
19095 WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T);
19096 #else
19097 #if defined(SQLITE_WIN32_HAS_ANSI)
19098 WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \
19099 DWORD, DWORD, DWORD, LPCSTR);
19100 #endif /* defined(SQLITE_WIN32_HAS_ANSI) */
19101
19102 #if defined(SQLITE_WIN32_HAS_WIDE)
19103 WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \
19104 DWORD, DWORD, DWORD, LPCWSTR);
19105 #endif /* defined(SQLITE_WIN32_HAS_WIDE) */
19106
19107 WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
19108 #endif /* SQLITE_OS_WINRT */
19109
19110 /*
19111 ** These file mapping APIs are common to both Win32 and WinRT.
19112 */
19113
19114 WINBASEAPI BOOL WINAPI FlushViewOfFile(LPCVOID, SIZE_T);
19115 WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
19116 #endif /* SQLITE_WIN32_FILEMAPPING_API */
19117
19118 /*
19119 ** Some Microsoft compilers lack this definition.
19120 */
19121 #ifndef INVALID_FILE_ATTRIBUTES
19122 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
19123 #endif
19124
19125 #ifndef FILE_FLAG_MASK
19126 # define FILE_FLAG_MASK (0xFF3C0000)
19127 #endif
19128
19129 #ifndef FILE_ATTRIBUTE_MASK
19130 # define FILE_ATTRIBUTE_MASK (0x0003FFF7)
19131 #endif
19132
19133 #ifndef SQLITE_OMIT_WAL
19134 /* Forward references to structures used for WAL */
19135 typedef struct winShm winShm; /* A connection to shared-memory */
19136 typedef struct winShmNode winShmNode; /* A region of shared-memory */
19137 #endif
19138
19139 /*
19140 ** WinCE lacks native support for file locking so we have to fake it
19141 ** with some code of our own.
19142 */
19143 #if SQLITE_OS_WINCE
19144 typedef struct winceLock {
19145 int nReaders; /* Number of reader locks obtained */
19146 BOOL bPending; /* Indicates a pending lock has been obtained */
19147 BOOL bReserved; /* Indicates a reserved lock has been obtained */
19148 BOOL bExclusive; /* Indicates an exclusive lock has been obtained */
19149 } winceLock;
19150 #endif
19151
19152 /*
19153 ** The winFile structure is a subclass of sqlite3_file* specific to the win32
19154 ** portability layer.
19155 */
19156 typedef struct winFile winFile;
19157 struct winFile {
19158 const sqlite3_io_methods *pMethod; /*** Must be first ***/
19159 sqlite3_vfs *pVfs; /* The VFS used to open this file */
19160 HANDLE h; /* Handle for accessing the file */
19161 u8 locktype; /* Type of lock currently held on this file */
19162 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
19163 u8 ctrlFlags; /* Flags. See WINFILE_* below */
19164 DWORD lastErrno; /* The Windows errno from the last I/O error */
19165 #ifndef SQLITE_OMIT_WAL
19166 winShm *pShm; /* Instance of shared memory on this file */
19167 #endif
19168 const char *zPath; /* Full pathname of this file */
19169 int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
19170 #if SQLITE_OS_WINCE
19171 LPWSTR zDeleteOnClose; /* Name of file to delete when closing */
19172 HANDLE hMutex; /* Mutex used to control access to shared lock */
19173 HANDLE hShared; /* Shared memory segment used for locking */
19174 winceLock local; /* Locks obtained by this instance of winFile */
19175 winceLock *shared; /* Global shared lock memory for the file */
19176 #endif
19177 #if SQLITE_MAX_MMAP_SIZE>0
19178 int nFetchOut; /* Number of outstanding xFetch references */
19179 HANDLE hMap; /* Handle for accessing memory mapping */
19180 void *pMapRegion; /* Area memory mapped */
19181 sqlite3_int64 mmapSize; /* Usable size of mapped region */
19182 sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */
19183 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
19184 #endif
19185 };
19186
19187 /*
19188 ** The winVfsAppData structure is used for the pAppData member for all of the
19189 ** Win32 VFS variants.
19190 */
19191 typedef struct winVfsAppData winVfsAppData;
19192 struct winVfsAppData {
19193 const sqlite3_io_methods *pMethod; /* The file I/O methods to use. */
19194 void *pAppData; /* The extra pAppData, if any. */
19195 BOOL bNoLock; /* Non-zero if locking is disabled. */
19196 };
19197
19198 /*
19199 ** Allowed values for winFile.ctrlFlags
19200 */
19201 #define WINFILE_RDONLY 0x02 /* Connection is read only */
19202 #define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
19203 #define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
19204
19205 /*
19206 * The size of the buffer used by sqlite3_win32_write_debug().
19207 */
19208 #ifndef SQLITE_WIN32_DBG_BUF_SIZE
19209 # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
19210 #endif
19211
19212 /*
19213 * The value used with sqlite3_win32_set_directory() to specify that
19214 * the data directory should be changed.
19215 */
19216 #ifndef SQLITE_WIN32_DATA_DIRECTORY_TYPE
19217 # define SQLITE_WIN32_DATA_DIRECTORY_TYPE (1)
19218 #endif
19219
19220 /*
19221 * The value used with sqlite3_win32_set_directory() to specify that
19222 * the temporary directory should be changed.
19223 */
19224 #ifndef SQLITE_WIN32_TEMP_DIRECTORY_TYPE
19225 # define SQLITE_WIN32_TEMP_DIRECTORY_TYPE (2)
19226 #endif
19227
19228 /*
19229 * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
19230 * various Win32 API heap functions instead of our own.
19231 */
19232 #ifdef SQLITE_WIN32_MALLOC
19233
19234 /*
19235 * If this is non-zero, an isolated heap will be created by the native Win32
19236 * allocator subsystem; otherwise, the default process heap will be used. This
19237 * setting has no effect when compiling for WinRT. By default, this is enabled
19238 * and an isolated heap will be created to store all allocated data.
19239 *
19240 ******************************************************************************
19241 * WARNING: It is important to note that when this setting is non-zero and the
19242 * winMemShutdown function is called (e.g. by the sqlite3_shutdown
19243 * function), all data that was allocated using the isolated heap will
19244 * be freed immediately and any attempt to access any of that freed
19245 * data will almost certainly result in an immediate access violation.
19246 ******************************************************************************
19247 */
19248 #ifndef SQLITE_WIN32_HEAP_CREATE
19249 # define SQLITE_WIN32_HEAP_CREATE (TRUE)
19250 #endif
19251
19252 /*
19253 * This is cache size used in the calculation of the initial size of the
19254 * Win32-specific heap. It cannot be negative.
19255 */
19256 #ifndef SQLITE_WIN32_CACHE_SIZE
19257 # if SQLITE_DEFAULT_CACHE_SIZE>=0
19258 # define SQLITE_WIN32_CACHE_SIZE (SQLITE_DEFAULT_CACHE_SIZE)
19259 # else
19260 # define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
19261 # endif
19262 #endif
19263
19264 /*
19265 * The initial size of the Win32-specific heap. This value may be zero.
19266 */
19267 #ifndef SQLITE_WIN32_HEAP_INIT_SIZE
19268 # define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_WIN32_CACHE_SIZE) * \
19269 (SQLITE_DEFAULT_PAGE_SIZE) + 4194304)
19270 #endif
19271
19272 /*
19273 * The maximum size of the Win32-specific heap. This value may be zero.
19274 */
19275 #ifndef SQLITE_WIN32_HEAP_MAX_SIZE
19276 # define SQLITE_WIN32_HEAP_MAX_SIZE (0)
19277 #endif
19278
19279 /*
19280 * The extra flags to use in calls to the Win32 heap APIs. This value may be
19281 * zero for the default behavior.
19282 */
19283 #ifndef SQLITE_WIN32_HEAP_FLAGS
19284 # define SQLITE_WIN32_HEAP_FLAGS (0)
19285 #endif
19286
19287
19288 /*
19289 ** The winMemData structure stores information required by the Win32-specific
19290 ** sqlite3_mem_methods implementation.
19291 */
19292 typedef struct winMemData winMemData;
19293 struct winMemData {
19294 #ifndef NDEBUG
19295 u32 magic1; /* Magic number to detect structure corruption. */
19296 #endif
19297 HANDLE hHeap; /* The handle to our heap. */
19298 BOOL bOwned; /* Do we own the heap (i.e. destroy it on shutdown)? */
19299 #ifndef NDEBUG
19300 u32 magic2; /* Magic number to detect structure corruption. */
19301 #endif
19302 };
19303
19304 #ifndef NDEBUG
19305 #define WINMEM_MAGIC1 0x42b2830b
19306 #define WINMEM_MAGIC2 0xbd4d7cf4
19307 #endif
19308
19309 static struct winMemData win_mem_data = {
19310 #ifndef NDEBUG
19311 WINMEM_MAGIC1,
19312 #endif
19313 NULL, FALSE
19314 #ifndef NDEBUG
19315 ,WINMEM_MAGIC2
19316 #endif
19317 };
19318
19319 #ifndef NDEBUG
19320 #define winMemAssertMagic1() assert( win_mem_data.magic1==WINMEM_MAGIC1 )
19321 #define winMemAssertMagic2() assert( win_mem_data.magic2==WINMEM_MAGIC2 )
19322 #define winMemAssertMagic() winMemAssertMagic1(); winMemAssertMagic2();
19323 #else
19324 #define winMemAssertMagic()
19325 #endif
19326
19327 #define winMemGetDataPtr() &win_mem_data
19328 #define winMemGetHeap() win_mem_data.hHeap
19329 #define winMemGetOwned() win_mem_data.bOwned
19330
19331 static void *winMemMalloc(int nBytes);
19332 static void winMemFree(void *pPrior);
19333 static void *winMemRealloc(void *pPrior, int nBytes);
19334 static int winMemSize(void *p);
19335 static int winMemRoundup(int n);
19336 static int winMemInit(void *pAppData);
19337 static void winMemShutdown(void *pAppData);
19338
19339 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
19340 #endif /* SQLITE_WIN32_MALLOC */
19341
19342 /*
19343 ** The following variable is (normally) set once and never changes
19344 ** thereafter. It records whether the operating system is Win9x
19345 ** or WinNT.
19346 **
19347 ** 0: Operating system unknown.
19348 ** 1: Operating system is Win9x.
19349 ** 2: Operating system is WinNT.
19350 **
19351 ** In order to facilitate testing on a WinNT system, the test fixture
19352 ** can manually set this value to 1 to emulate Win98 behavior.
19353 */
19354 #ifdef SQLITE_TEST
19355 SQLITE_API LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
19356 #else
19357 static LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
19358 #endif
19359
19360 #ifndef SYSCALL
19361 # define SYSCALL sqlite3_syscall_ptr
19362 #endif
19363
19364 /*
19365 ** This function is not available on Windows CE or WinRT.
19366 */
19367
19368 #if SQLITE_OS_WINCE || SQLITE_OS_WINRT
19369 # define osAreFileApisANSI() 1
19370 #endif
19371
19372 /*
19373 ** Many system calls are accessed through pointer-to-functions so that
19374 ** they may be overridden at runtime to facilitate fault injection during
19375 ** testing and sandboxing. The following array holds the names and pointers
19376 ** to all overrideable system calls.
19377 */
19378 static struct win_syscall {
19379 const char *zName; /* Name of the system call */
19380 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
19381 sqlite3_syscall_ptr pDefault; /* Default value */
19382 } aSyscall[] = {
19383 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
19384 { "AreFileApisANSI", (SYSCALL)AreFileApisANSI, 0 },
19385 #else
19386 { "AreFileApisANSI", (SYSCALL)0, 0 },
19387 #endif
19388
19389 #ifndef osAreFileApisANSI
19390 #define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
19391 #endif
19392
19393 #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
19394 { "CharLowerW", (SYSCALL)CharLowerW, 0 },
19395 #else
19396 { "CharLowerW", (SYSCALL)0, 0 },
19397 #endif
19398
19399 #define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
19400
19401 #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
19402 { "CharUpperW", (SYSCALL)CharUpperW, 0 },
19403 #else
19404 { "CharUpperW", (SYSCALL)0, 0 },
19405 #endif
19406
19407 #define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
19408
19409 { "CloseHandle", (SYSCALL)CloseHandle, 0 },
19410
19411 #define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
19412
19413 #if defined(SQLITE_WIN32_HAS_ANSI)
19414 { "CreateFileA", (SYSCALL)CreateFileA, 0 },
19415 #else
19416 { "CreateFileA", (SYSCALL)0, 0 },
19417 #endif
19418
19419 #define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
19420 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
19421
19422 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
19423 { "CreateFileW", (SYSCALL)CreateFileW, 0 },
19424 #else
19425 { "CreateFileW", (SYSCALL)0, 0 },
19426 #endif
19427
19428 #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
19429 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
19430
19431 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
19432 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) && \
19433 SQLITE_WIN32_CREATEFILEMAPPINGA
19434 { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 },
19435 #else
19436 { "CreateFileMappingA", (SYSCALL)0, 0 },
19437 #endif
19438
19439 #define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
19440 DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
19441
19442 #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
19443 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
19444 { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 },
19445 #else
19446 { "CreateFileMappingW", (SYSCALL)0, 0 },
19447 #endif
19448
19449 #define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
19450 DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
19451
19452 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
19453 { "CreateMutexW", (SYSCALL)CreateMutexW, 0 },
19454 #else
19455 { "CreateMutexW", (SYSCALL)0, 0 },
19456 #endif
19457
19458 #define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
19459 LPCWSTR))aSyscall[8].pCurrent)
19460
19461 #if defined(SQLITE_WIN32_HAS_ANSI)
19462 { "DeleteFileA", (SYSCALL)DeleteFileA, 0 },
19463 #else
19464 { "DeleteFileA", (SYSCALL)0, 0 },
19465 #endif
19466
19467 #define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
19468
19469 #if defined(SQLITE_WIN32_HAS_WIDE)
19470 { "DeleteFileW", (SYSCALL)DeleteFileW, 0 },
19471 #else
19472 { "DeleteFileW", (SYSCALL)0, 0 },
19473 #endif
19474
19475 #define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
19476
19477 #if SQLITE_OS_WINCE
19478 { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
19479 #else
19480 { "FileTimeToLocalFileTime", (SYSCALL)0, 0 },
19481 #endif
19482
19483 #define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
19484 LPFILETIME))aSyscall[11].pCurrent)
19485
19486 #if SQLITE_OS_WINCE
19487 { "FileTimeToSystemTime", (SYSCALL)FileTimeToSystemTime, 0 },
19488 #else
19489 { "FileTimeToSystemTime", (SYSCALL)0, 0 },
19490 #endif
19491
19492 #define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
19493 LPSYSTEMTIME))aSyscall[12].pCurrent)
19494
19495 { "FlushFileBuffers", (SYSCALL)FlushFileBuffers, 0 },
19496
19497 #define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
19498
19499 #if defined(SQLITE_WIN32_HAS_ANSI)
19500 { "FormatMessageA", (SYSCALL)FormatMessageA, 0 },
19501 #else
19502 { "FormatMessageA", (SYSCALL)0, 0 },
19503 #endif
19504
19505 #define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
19506 DWORD,va_list*))aSyscall[14].pCurrent)
19507
19508 #if defined(SQLITE_WIN32_HAS_WIDE)
19509 { "FormatMessageW", (SYSCALL)FormatMessageW, 0 },
19510 #else
19511 { "FormatMessageW", (SYSCALL)0, 0 },
19512 #endif
19513
19514 #define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
19515 DWORD,va_list*))aSyscall[15].pCurrent)
19516
19517 #if !defined(SQLITE_OMIT_LOAD_EXTENSION)
19518 { "FreeLibrary", (SYSCALL)FreeLibrary, 0 },
19519 #else
19520 { "FreeLibrary", (SYSCALL)0, 0 },
19521 #endif
19522
19523 #define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
19524
19525 { "GetCurrentProcessId", (SYSCALL)GetCurrentProcessId, 0 },
19526
19527 #define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
19528
19529 #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
19530 { "GetDiskFreeSpaceA", (SYSCALL)GetDiskFreeSpaceA, 0 },
19531 #else
19532 { "GetDiskFreeSpaceA", (SYSCALL)0, 0 },
19533 #endif
19534
19535 #define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
19536 LPDWORD))aSyscall[18].pCurrent)
19537
19538 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
19539 { "GetDiskFreeSpaceW", (SYSCALL)GetDiskFreeSpaceW, 0 },
19540 #else
19541 { "GetDiskFreeSpaceW", (SYSCALL)0, 0 },
19542 #endif
19543
19544 #define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
19545 LPDWORD))aSyscall[19].pCurrent)
19546
19547 #if defined(SQLITE_WIN32_HAS_ANSI)
19548 { "GetFileAttributesA", (SYSCALL)GetFileAttributesA, 0 },
19549 #else
19550 { "GetFileAttributesA", (SYSCALL)0, 0 },
19551 #endif
19552
19553 #define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
19554
19555 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
19556 { "GetFileAttributesW", (SYSCALL)GetFileAttributesW, 0 },
19557 #else
19558 { "GetFileAttributesW", (SYSCALL)0, 0 },
19559 #endif
19560
19561 #define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
19562
19563 #if defined(SQLITE_WIN32_HAS_WIDE)
19564 { "GetFileAttributesExW", (SYSCALL)GetFileAttributesExW, 0 },
19565 #else
19566 { "GetFileAttributesExW", (SYSCALL)0, 0 },
19567 #endif
19568
19569 #define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
19570 LPVOID))aSyscall[22].pCurrent)
19571
19572 #if !SQLITE_OS_WINRT
19573 { "GetFileSize", (SYSCALL)GetFileSize, 0 },
19574 #else
19575 { "GetFileSize", (SYSCALL)0, 0 },
19576 #endif
19577
19578 #define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
19579
19580 #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
19581 { "GetFullPathNameA", (SYSCALL)GetFullPathNameA, 0 },
19582 #else
19583 { "GetFullPathNameA", (SYSCALL)0, 0 },
19584 #endif
19585
19586 #define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
19587 LPSTR*))aSyscall[24].pCurrent)
19588
19589 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
19590 { "GetFullPathNameW", (SYSCALL)GetFullPathNameW, 0 },
19591 #else
19592 { "GetFullPathNameW", (SYSCALL)0, 0 },
19593 #endif
19594
19595 #define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
19596 LPWSTR*))aSyscall[25].pCurrent)
19597
19598 { "GetLastError", (SYSCALL)GetLastError, 0 },
19599
19600 #define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
19601
19602 #if !defined(SQLITE_OMIT_LOAD_EXTENSION)
19603 #if SQLITE_OS_WINCE
19604 /* The GetProcAddressA() routine is only available on Windows CE. */
19605 { "GetProcAddressA", (SYSCALL)GetProcAddressA, 0 },
19606 #else
19607 /* All other Windows platforms expect GetProcAddress() to take
19608 ** an ANSI string regardless of the _UNICODE setting */
19609 { "GetProcAddressA", (SYSCALL)GetProcAddress, 0 },
19610 #endif
19611 #else
19612 { "GetProcAddressA", (SYSCALL)0, 0 },
19613 #endif
19614
19615 #define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
19616 LPCSTR))aSyscall[27].pCurrent)
19617
19618 #if !SQLITE_OS_WINRT
19619 { "GetSystemInfo", (SYSCALL)GetSystemInfo, 0 },
19620 #else
19621 { "GetSystemInfo", (SYSCALL)0, 0 },
19622 #endif
19623
19624 #define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
19625
19626 { "GetSystemTime", (SYSCALL)GetSystemTime, 0 },
19627
19628 #define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
19629
19630 #if !SQLITE_OS_WINCE
19631 { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
19632 #else
19633 { "GetSystemTimeAsFileTime", (SYSCALL)0, 0 },
19634 #endif
19635
19636 #define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
19637 LPFILETIME))aSyscall[30].pCurrent)
19638
19639 #if defined(SQLITE_WIN32_HAS_ANSI)
19640 { "GetTempPathA", (SYSCALL)GetTempPathA, 0 },
19641 #else
19642 { "GetTempPathA", (SYSCALL)0, 0 },
19643 #endif
19644
19645 #define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
19646
19647 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
19648 { "GetTempPathW", (SYSCALL)GetTempPathW, 0 },
19649 #else
19650 { "GetTempPathW", (SYSCALL)0, 0 },
19651 #endif
19652
19653 #define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
19654
19655 #if !SQLITE_OS_WINRT
19656 { "GetTickCount", (SYSCALL)GetTickCount, 0 },
19657 #else
19658 { "GetTickCount", (SYSCALL)0, 0 },
19659 #endif
19660
19661 #define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
19662
19663 #if defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_GETVERSIONEX
19664 { "GetVersionExA", (SYSCALL)GetVersionExA, 0 },
19665 #else
19666 { "GetVersionExA", (SYSCALL)0, 0 },
19667 #endif
19668
19669 #define osGetVersionExA ((BOOL(WINAPI*)( \
19670 LPOSVERSIONINFOA))aSyscall[34].pCurrent)
19671
19672 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
19673 SQLITE_WIN32_GETVERSIONEX
19674 { "GetVersionExW", (SYSCALL)GetVersionExW, 0 },
19675 #else
19676 { "GetVersionExW", (SYSCALL)0, 0 },
19677 #endif
19678
19679 #define osGetVersionExW ((BOOL(WINAPI*)( \
19680 LPOSVERSIONINFOW))aSyscall[35].pCurrent)
19681
19682 { "HeapAlloc", (SYSCALL)HeapAlloc, 0 },
19683
19684 #define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
19685 SIZE_T))aSyscall[36].pCurrent)
19686
19687 #if !SQLITE_OS_WINRT
19688 { "HeapCreate", (SYSCALL)HeapCreate, 0 },
19689 #else
19690 { "HeapCreate", (SYSCALL)0, 0 },
19691 #endif
19692
19693 #define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
19694 SIZE_T))aSyscall[37].pCurrent)
19695
19696 #if !SQLITE_OS_WINRT
19697 { "HeapDestroy", (SYSCALL)HeapDestroy, 0 },
19698 #else
19699 { "HeapDestroy", (SYSCALL)0, 0 },
19700 #endif
19701
19702 #define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[38].pCurrent)
19703
19704 { "HeapFree", (SYSCALL)HeapFree, 0 },
19705
19706 #define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[39].pCurrent)
19707
19708 { "HeapReAlloc", (SYSCALL)HeapReAlloc, 0 },
19709
19710 #define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
19711 SIZE_T))aSyscall[40].pCurrent)
19712
19713 { "HeapSize", (SYSCALL)HeapSize, 0 },
19714
19715 #define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
19716 LPCVOID))aSyscall[41].pCurrent)
19717
19718 #if !SQLITE_OS_WINRT
19719 { "HeapValidate", (SYSCALL)HeapValidate, 0 },
19720 #else
19721 { "HeapValidate", (SYSCALL)0, 0 },
19722 #endif
19723
19724 #define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
19725 LPCVOID))aSyscall[42].pCurrent)
19726
19727 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
19728 { "HeapCompact", (SYSCALL)HeapCompact, 0 },
19729 #else
19730 { "HeapCompact", (SYSCALL)0, 0 },
19731 #endif
19732
19733 #define osHeapCompact ((UINT(WINAPI*)(HANDLE,DWORD))aSyscall[43].pCurrent)
19734
19735 #if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
19736 { "LoadLibraryA", (SYSCALL)LoadLibraryA, 0 },
19737 #else
19738 { "LoadLibraryA", (SYSCALL)0, 0 },
19739 #endif
19740
19741 #define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[44].pCurrent)
19742
19743 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
19744 !defined(SQLITE_OMIT_LOAD_EXTENSION)
19745 { "LoadLibraryW", (SYSCALL)LoadLibraryW, 0 },
19746 #else
19747 { "LoadLibraryW", (SYSCALL)0, 0 },
19748 #endif
19749
19750 #define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[45].pCurrent)
19751
19752 #if !SQLITE_OS_WINRT
19753 { "LocalFree", (SYSCALL)LocalFree, 0 },
19754 #else
19755 { "LocalFree", (SYSCALL)0, 0 },
19756 #endif
19757
19758 #define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[46].pCurrent)
19759
19760 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
19761 { "LockFile", (SYSCALL)LockFile, 0 },
19762 #else
19763 { "LockFile", (SYSCALL)0, 0 },
19764 #endif
19765
19766 #ifndef osLockFile
19767 #define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
19768 DWORD))aSyscall[47].pCurrent)
19769 #endif
19770
19771 #if !SQLITE_OS_WINCE
19772 { "LockFileEx", (SYSCALL)LockFileEx, 0 },
19773 #else
19774 { "LockFileEx", (SYSCALL)0, 0 },
19775 #endif
19776
19777 #ifndef osLockFileEx
19778 #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
19779 LPOVERLAPPED))aSyscall[48].pCurrent)
19780 #endif
19781
19782 #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \
19783 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
19784 { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 },
19785 #else
19786 { "MapViewOfFile", (SYSCALL)0, 0 },
19787 #endif
19788
19789 #define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
19790 SIZE_T))aSyscall[49].pCurrent)
19791
19792 { "MultiByteToWideChar", (SYSCALL)MultiByteToWideChar, 0 },
19793
19794 #define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
19795 int))aSyscall[50].pCurrent)
19796
19797 { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
19798
19799 #define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
19800 LARGE_INTEGER*))aSyscall[51].pCurrent)
19801
19802 { "ReadFile", (SYSCALL)ReadFile, 0 },
19803
19804 #define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
19805 LPOVERLAPPED))aSyscall[52].pCurrent)
19806
19807 { "SetEndOfFile", (SYSCALL)SetEndOfFile, 0 },
19808
19809 #define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[53].pCurrent)
19810
19811 #if !SQLITE_OS_WINRT
19812 { "SetFilePointer", (SYSCALL)SetFilePointer, 0 },
19813 #else
19814 { "SetFilePointer", (SYSCALL)0, 0 },
19815 #endif
19816
19817 #define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
19818 DWORD))aSyscall[54].pCurrent)
19819
19820 #if !SQLITE_OS_WINRT
19821 { "Sleep", (SYSCALL)Sleep, 0 },
19822 #else
19823 { "Sleep", (SYSCALL)0, 0 },
19824 #endif
19825
19826 #define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[55].pCurrent)
19827
19828 { "SystemTimeToFileTime", (SYSCALL)SystemTimeToFileTime, 0 },
19829
19830 #define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
19831 LPFILETIME))aSyscall[56].pCurrent)
19832
19833 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
19834 { "UnlockFile", (SYSCALL)UnlockFile, 0 },
19835 #else
19836 { "UnlockFile", (SYSCALL)0, 0 },
19837 #endif
19838
19839 #ifndef osUnlockFile
19840 #define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
19841 DWORD))aSyscall[57].pCurrent)
19842 #endif
19843
19844 #if !SQLITE_OS_WINCE
19845 { "UnlockFileEx", (SYSCALL)UnlockFileEx, 0 },
19846 #else
19847 { "UnlockFileEx", (SYSCALL)0, 0 },
19848 #endif
19849
19850 #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
19851 LPOVERLAPPED))aSyscall[58].pCurrent)
19852
19853 #if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
19854 { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 },
19855 #else
19856 { "UnmapViewOfFile", (SYSCALL)0, 0 },
19857 #endif
19858
19859 #define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[59].pCurrent)
19860
19861 { "WideCharToMultiByte", (SYSCALL)WideCharToMultiByte, 0 },
19862
19863 #define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
19864 LPCSTR,LPBOOL))aSyscall[60].pCurrent)
19865
19866 { "WriteFile", (SYSCALL)WriteFile, 0 },
19867
19868 #define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
19869 LPOVERLAPPED))aSyscall[61].pCurrent)
19870
19871 #if SQLITE_OS_WINRT
19872 { "CreateEventExW", (SYSCALL)CreateEventExW, 0 },
19873 #else
19874 { "CreateEventExW", (SYSCALL)0, 0 },
19875 #endif
19876
19877 #define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
19878 DWORD,DWORD))aSyscall[62].pCurrent)
19879
19880 #if !SQLITE_OS_WINRT
19881 { "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 },
19882 #else
19883 { "WaitForSingleObject", (SYSCALL)0, 0 },
19884 #endif
19885
19886 #define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
19887 DWORD))aSyscall[63].pCurrent)
19888
19889 #if !SQLITE_OS_WINCE
19890 { "WaitForSingleObjectEx", (SYSCALL)WaitForSingleObjectEx, 0 },
19891 #else
19892 { "WaitForSingleObjectEx", (SYSCALL)0, 0 },
19893 #endif
19894
19895 #define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
19896 BOOL))aSyscall[64].pCurrent)
19897
19898 #if SQLITE_OS_WINRT
19899 { "SetFilePointerEx", (SYSCALL)SetFilePointerEx, 0 },
19900 #else
19901 { "SetFilePointerEx", (SYSCALL)0, 0 },
19902 #endif
19903
19904 #define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
19905 PLARGE_INTEGER,DWORD))aSyscall[65].pCurrent)
19906
19907 #if SQLITE_OS_WINRT
19908 { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
19909 #else
19910 { "GetFileInformationByHandleEx", (SYSCALL)0, 0 },
19911 #endif
19912
19913 #define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
19914 FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)
19915
19916 #if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
19917 { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 },
19918 #else
19919 { "MapViewOfFileFromApp", (SYSCALL)0, 0 },
19920 #endif
19921
19922 #define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
19923 SIZE_T))aSyscall[67].pCurrent)
19924
19925 #if SQLITE_OS_WINRT
19926 { "CreateFile2", (SYSCALL)CreateFile2, 0 },
19927 #else
19928 { "CreateFile2", (SYSCALL)0, 0 },
19929 #endif
19930
19931 #define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
19932 LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[68].pCurrent)
19933
19934 #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION)
19935 { "LoadPackagedLibrary", (SYSCALL)LoadPackagedLibrary, 0 },
19936 #else
19937 { "LoadPackagedLibrary", (SYSCALL)0, 0 },
19938 #endif
19939
19940 #define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
19941 DWORD))aSyscall[69].pCurrent)
19942
19943 #if SQLITE_OS_WINRT
19944 { "GetTickCount64", (SYSCALL)GetTickCount64, 0 },
19945 #else
19946 { "GetTickCount64", (SYSCALL)0, 0 },
19947 #endif
19948
19949 #define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[70].pCurrent)
19950
19951 #if SQLITE_OS_WINRT
19952 { "GetNativeSystemInfo", (SYSCALL)GetNativeSystemInfo, 0 },
19953 #else
19954 { "GetNativeSystemInfo", (SYSCALL)0, 0 },
19955 #endif
19956
19957 #define osGetNativeSystemInfo ((VOID(WINAPI*)( \
19958 LPSYSTEM_INFO))aSyscall[71].pCurrent)
19959
19960 #if defined(SQLITE_WIN32_HAS_ANSI)
19961 { "OutputDebugStringA", (SYSCALL)OutputDebugStringA, 0 },
19962 #else
19963 { "OutputDebugStringA", (SYSCALL)0, 0 },
19964 #endif
19965
19966 #define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[72].pCurrent)
19967
19968 #if defined(SQLITE_WIN32_HAS_WIDE)
19969 { "OutputDebugStringW", (SYSCALL)OutputDebugStringW, 0 },
19970 #else
19971 { "OutputDebugStringW", (SYSCALL)0, 0 },
19972 #endif
19973
19974 #define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[73].pCurrent)
19975
19976 { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 },
19977
19978 #define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)
19979
19980 #if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
19981 { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
19982 #else
19983 { "CreateFileMappingFromApp", (SYSCALL)0, 0 },
19984 #endif
19985
19986 #define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
19987 LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
19988
19989 /*
19990 ** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
19991 ** is really just a macro that uses a compiler intrinsic (e.g. x64).
19992 ** So do not try to make this is into a redefinable interface.
19993 */
19994 #if defined(InterlockedCompareExchange)
19995 { "InterlockedCompareExchange", (SYSCALL)0, 0 },
19996
19997 #define osInterlockedCompareExchange InterlockedCompareExchange
19998 #else
19999 { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 },
20000
20001 #define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG \
20002 SQLITE_WIN32_VOLATILE*, LONG,LONG))aSyscall[76].pCurrent)
20003 #endif /* defined(InterlockedCompareExchange) */
20004
20005 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
20006 { "UuidCreate", (SYSCALL)UuidCreate, 0 },
20007 #else
20008 { "UuidCreate", (SYSCALL)0, 0 },
20009 #endif
20010
20011 #define osUuidCreate ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[77].pCurrent)
20012
20013 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
20014 { "UuidCreateSequential", (SYSCALL)UuidCreateSequential, 0 },
20015 #else
20016 { "UuidCreateSequential", (SYSCALL)0, 0 },
20017 #endif
20018
20019 #define osUuidCreateSequential \
20020 ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[78].pCurrent)
20021
20022 #if !defined(SQLITE_NO_SYNC) && SQLITE_MAX_MMAP_SIZE>0
20023 { "FlushViewOfFile", (SYSCALL)FlushViewOfFile, 0 },
20024 #else
20025 { "FlushViewOfFile", (SYSCALL)0, 0 },
20026 #endif
20027
20028 #define osFlushViewOfFile \
20029 ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent)
20030
20031 }; /* End of the overrideable system calls */
20032
20033 /*
20034 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
20035 ** "win32" VFSes. Return SQLITE_OK opon successfully updating the
20036 ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
20037 ** system call named zName.
20038 */
20039 static int winSetSystemCall(
20040 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
20041 const char *zName, /* Name of system call to override */
20042 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
20043 ){
20044 unsigned int i;
20045 int rc = SQLITE_NOTFOUND;
20046
20047 UNUSED_PARAMETER(pNotUsed);
20048 if( zName==0 ){
20049 /* If no zName is given, restore all system calls to their default
20050 ** settings and return NULL
20051 */
20052 rc = SQLITE_OK;
20053 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
20054 if( aSyscall[i].pDefault ){
20055 aSyscall[i].pCurrent = aSyscall[i].pDefault;
20056 }
20057 }
20058 }else{
20059 /* If zName is specified, operate on only the one system call
20060 ** specified.
20061 */
20062 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
20063 if( strcmp(zName, aSyscall[i].zName)==0 ){
20064 if( aSyscall[i].pDefault==0 ){
20065 aSyscall[i].pDefault = aSyscall[i].pCurrent;
20066 }
20067 rc = SQLITE_OK;
20068 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
20069 aSyscall[i].pCurrent = pNewFunc;
20070 break;
20071 }
20072 }
20073 }
20074 return rc;
20075 }
20076
20077 /*
20078 ** Return the value of a system call. Return NULL if zName is not a
20079 ** recognized system call name. NULL is also returned if the system call
20080 ** is currently undefined.
20081 */
20082 static sqlite3_syscall_ptr winGetSystemCall(
20083 sqlite3_vfs *pNotUsed,
20084 const char *zName
20085 ){
20086 unsigned int i;
20087
20088 UNUSED_PARAMETER(pNotUsed);
20089 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
20090 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
20091 }
20092 return 0;
20093 }
20094
20095 /*
20096 ** Return the name of the first system call after zName. If zName==NULL
20097 ** then return the name of the first system call. Return NULL if zName
20098 ** is the last system call or if zName is not the name of a valid
20099 ** system call.
20100 */
20101 static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
20102 int i = -1;
20103
20104 UNUSED_PARAMETER(p);
20105 if( zName ){
20106 for(i=0; i<ArraySize(aSyscall)-1; i++){
20107 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
20108 }
20109 }
20110 for(i++; i<ArraySize(aSyscall); i++){
20111 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
20112 }
20113 return 0;
20114 }
20115
20116 #ifdef SQLITE_WIN32_MALLOC
20117 /*
20118 ** If a Win32 native heap has been configured, this function will attempt to
20119 ** compact it. Upon success, SQLITE_OK will be returned. Upon failure, one
20120 ** of SQLITE_NOMEM, SQLITE_ERROR, or SQLITE_NOTFOUND will be returned. The
20121 ** "pnLargest" argument, if non-zero, will be used to return the size of the
20122 ** largest committed free block in the heap, in bytes.
20123 */
20124 SQLITE_API int sqlite3_win32_compact_heap(LPUINT pnLargest){
20125 int rc = SQLITE_OK;
20126 UINT nLargest = 0;
20127 HANDLE hHeap;
20128
20129 winMemAssertMagic();
20130 hHeap = winMemGetHeap();
20131 assert( hHeap!=0 );
20132 assert( hHeap!=INVALID_HANDLE_VALUE );
20133 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
20134 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
20135 #endif
20136 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
20137 if( (nLargest=osHeapCompact(hHeap, SQLITE_WIN32_HEAP_FLAGS))==0 ){
20138 DWORD lastErrno = osGetLastError();
20139 if( lastErrno==NO_ERROR ){
20140 sqlite3_log(SQLITE_NOMEM, "failed to HeapCompact (no space), heap=%p",
20141 (void*)hHeap);
20142 rc = SQLITE_NOMEM_BKPT;
20143 }else{
20144 sqlite3_log(SQLITE_ERROR, "failed to HeapCompact (%lu), heap=%p",
20145 osGetLastError(), (void*)hHeap);
20146 rc = SQLITE_ERROR;
20147 }
20148 }
20149 #else
20150 sqlite3_log(SQLITE_NOTFOUND, "failed to HeapCompact, heap=%p",
20151 (void*)hHeap);
20152 rc = SQLITE_NOTFOUND;
20153 #endif
20154 if( pnLargest ) *pnLargest = nLargest;
20155 return rc;
20156 }
20157
20158 /*
20159 ** If a Win32 native heap has been configured, this function will attempt to
20160 ** destroy and recreate it. If the Win32 native heap is not isolated and/or
20161 ** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will
20162 ** be returned and no changes will be made to the Win32 native heap.
20163 */
20164 SQLITE_API int sqlite3_win32_reset_heap(){
20165 int rc;
20166 MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */
20167 MUTEX_LOGIC( sqlite3_mutex *pMem; ) /* The memsys static mutex */
20168 MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
20169 MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); )
20170 sqlite3_mutex_enter(pMaster);
20171 sqlite3_mutex_enter(pMem);
20172 winMemAssertMagic();
20173 if( winMemGetHeap()!=NULL && winMemGetOwned() && sqlite3_memory_used()==0 ){
20174 /*
20175 ** At this point, there should be no outstanding memory allocations on
20176 ** the heap. Also, since both the master and memsys locks are currently
20177 ** being held by us, no other function (i.e. from another thread) should
20178 ** be able to even access the heap. Attempt to destroy and recreate our
20179 ** isolated Win32 native heap now.
20180 */
20181 assert( winMemGetHeap()!=NULL );
20182 assert( winMemGetOwned() );
20183 assert( sqlite3_memory_used()==0 );
20184 winMemShutdown(winMemGetDataPtr());
20185 assert( winMemGetHeap()==NULL );
20186 assert( !winMemGetOwned() );
20187 assert( sqlite3_memory_used()==0 );
20188 rc = winMemInit(winMemGetDataPtr());
20189 assert( rc!=SQLITE_OK || winMemGetHeap()!=NULL );
20190 assert( rc!=SQLITE_OK || winMemGetOwned() );
20191 assert( rc!=SQLITE_OK || sqlite3_memory_used()==0 );
20192 }else{
20193 /*
20194 ** The Win32 native heap cannot be modified because it may be in use.
20195 */
20196 rc = SQLITE_BUSY;
20197 }
20198 sqlite3_mutex_leave(pMem);
20199 sqlite3_mutex_leave(pMaster);
20200 return rc;
20201 }
20202 #endif /* SQLITE_WIN32_MALLOC */
20203
20204 /*
20205 ** This function outputs the specified (ANSI) string to the Win32 debugger
20206 ** (if available).
20207 */
20208
20209 SQLITE_API void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
20210 char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
20211 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
20212 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
20213 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
20214 #ifdef SQLITE_ENABLE_API_ARMOR
20215 if( !zBuf ){
20216 (void)SQLITE_MISUSE_BKPT;
20217 return;
20218 }
20219 #endif
20220 #if defined(SQLITE_WIN32_HAS_ANSI)
20221 if( nMin>0 ){
20222 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
20223 memcpy(zDbgBuf, zBuf, nMin);
20224 osOutputDebugStringA(zDbgBuf);
20225 }else{
20226 osOutputDebugStringA(zBuf);
20227 }
20228 #elif defined(SQLITE_WIN32_HAS_WIDE)
20229 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
20230 if ( osMultiByteToWideChar(
20231 osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf,
20232 nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){
20233 return;
20234 }
20235 osOutputDebugStringW((LPCWSTR)zDbgBuf);
20236 #else
20237 if( nMin>0 ){
20238 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
20239 memcpy(zDbgBuf, zBuf, nMin);
20240 fprintf(stderr, "%s", zDbgBuf);
20241 }else{
20242 fprintf(stderr, "%s", zBuf);
20243 }
20244 #endif
20245 }
20246
20247 /*
20248 ** The following routine suspends the current thread for at least ms
20249 ** milliseconds. This is equivalent to the Win32 Sleep() interface.
20250 */
20251 #if SQLITE_OS_WINRT
20252 static HANDLE sleepObj = NULL;
20253 #endif
20254
20255 SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds){
20256 #if SQLITE_OS_WINRT
20257 if ( sleepObj==NULL ){
20258 sleepObj = osCreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET,
20259 SYNCHRONIZE);
20260 }
20261 assert( sleepObj!=NULL );
20262 osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE);
20263 #else
20264 osSleep(milliseconds);
20265 #endif
20266 }
20267
20268 #if SQLITE_MAX_WORKER_THREADS>0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
20269 SQLITE_THREADSAFE>0
20270 SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject){
20271 DWORD rc;
20272 while( (rc = osWaitForSingleObjectEx(hObject, INFINITE,
20273 TRUE))==WAIT_IO_COMPLETION ){}
20274 return rc;
20275 }
20276 #endif
20277
20278 /*
20279 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
20280 ** or WinCE. Return false (zero) for Win95, Win98, or WinME.
20281 **
20282 ** Here is an interesting observation: Win95, Win98, and WinME lack
20283 ** the LockFileEx() API. But we can still statically link against that
20284 ** API as long as we don't call it when running Win95/98/ME. A call to
20285 ** this routine is used to determine if the host is Win95/98/ME or
20286 ** WinNT/2K/XP so that we will know whether or not we can safely call
20287 ** the LockFileEx() API.
20288 */
20289
20290 #if !SQLITE_WIN32_GETVERSIONEX
20291 # define osIsNT() (1)
20292 #elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
20293 # define osIsNT() (1)
20294 #elif !defined(SQLITE_WIN32_HAS_WIDE)
20295 # define osIsNT() (0)
20296 #else
20297 # define osIsNT() ((sqlite3_os_type==2) || sqlite3_win32_is_nt())
20298 #endif
20299
20300 /*
20301 ** This function determines if the machine is running a version of Windows
20302 ** based on the NT kernel.
20303 */
20304 SQLITE_API int sqlite3_win32_is_nt(void){
20305 #if SQLITE_OS_WINRT
20306 /*
20307 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
20308 ** kernel.
20309 */
20310 return 1;
20311 #elif SQLITE_WIN32_GETVERSIONEX
20312 if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
20313 #if defined(SQLITE_WIN32_HAS_ANSI)
20314 OSVERSIONINFOA sInfo;
20315 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
20316 osGetVersionExA(&sInfo);
20317 osInterlockedCompareExchange(&sqlite3_os_type,
20318 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
20319 #elif defined(SQLITE_WIN32_HAS_WIDE)
20320 OSVERSIONINFOW sInfo;
20321 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
20322 osGetVersionExW(&sInfo);
20323 osInterlockedCompareExchange(&sqlite3_os_type,
20324 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
20325 #endif
20326 }
20327 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
20328 #elif SQLITE_TEST
20329 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
20330 #else
20331 /*
20332 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
20333 ** deprecated are always assumed to be based on the NT kernel.
20334 */
20335 return 1;
20336 #endif
20337 }
20338
20339 #ifdef SQLITE_WIN32_MALLOC
20340 /*
20341 ** Allocate nBytes of memory.
20342 */
20343 static void *winMemMalloc(int nBytes){
20344 HANDLE hHeap;
20345 void *p;
20346
20347 winMemAssertMagic();
20348 hHeap = winMemGetHeap();
20349 assert( hHeap!=0 );
20350 assert( hHeap!=INVALID_HANDLE_VALUE );
20351 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
20352 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
20353 #endif
20354 assert( nBytes>=0 );
20355 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
20356 if( !p ){
20357 sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%lu), heap=%p",
20358 nBytes, osGetLastError(), (void*)hHeap);
20359 }
20360 return p;
20361 }
20362
20363 /*
20364 ** Free memory.
20365 */
20366 static void winMemFree(void *pPrior){
20367 HANDLE hHeap;
20368
20369 winMemAssertMagic();
20370 hHeap = winMemGetHeap();
20371 assert( hHeap!=0 );
20372 assert( hHeap!=INVALID_HANDLE_VALUE );
20373 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
20374 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
20375 #endif
20376 if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
20377 if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
20378 sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%lu), heap=%p",
20379 pPrior, osGetLastError(), (void*)hHeap);
20380 }
20381 }
20382
20383 /*
20384 ** Change the size of an existing memory allocation
20385 */
20386 static void *winMemRealloc(void *pPrior, int nBytes){
20387 HANDLE hHeap;
20388 void *p;
20389
20390 winMemAssertMagic();
20391 hHeap = winMemGetHeap();
20392 assert( hHeap!=0 );
20393 assert( hHeap!=INVALID_HANDLE_VALUE );
20394 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
20395 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
20396 #endif
20397 assert( nBytes>=0 );
20398 if( !pPrior ){
20399 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
20400 }else{
20401 p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
20402 }
20403 if( !p ){
20404 sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%lu), heap=%p",
20405 pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
20406 (void*)hHeap);
20407 }
20408 return p;
20409 }
20410
20411 /*
20412 ** Return the size of an outstanding allocation, in bytes.
20413 */
20414 static int winMemSize(void *p){
20415 HANDLE hHeap;
20416 SIZE_T n;
20417
20418 winMemAssertMagic();
20419 hHeap = winMemGetHeap();
20420 assert( hHeap!=0 );
20421 assert( hHeap!=INVALID_HANDLE_VALUE );
20422 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
20423 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, p) );
20424 #endif
20425 if( !p ) return 0;
20426 n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
20427 if( n==(SIZE_T)-1 ){
20428 sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%lu), heap=%p",
20429 p, osGetLastError(), (void*)hHeap);
20430 return 0;
20431 }
20432 return (int)n;
20433 }
20434
20435 /*
20436 ** Round up a request size to the next valid allocation size.
20437 */
20438 static int winMemRoundup(int n){
20439 return n;
20440 }
20441
20442 /*
20443 ** Initialize this module.
20444 */
20445 static int winMemInit(void *pAppData){
20446 winMemData *pWinMemData = (winMemData *)pAppData;
20447
20448 if( !pWinMemData ) return SQLITE_ERROR;
20449 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
20450 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
20451
20452 #if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE
20453 if( !pWinMemData->hHeap ){
20454 DWORD dwInitialSize = SQLITE_WIN32_HEAP_INIT_SIZE;
20455 DWORD dwMaximumSize = (DWORD)sqlite3GlobalConfig.nHeap;
20456 if( dwMaximumSize==0 ){
20457 dwMaximumSize = SQLITE_WIN32_HEAP_MAX_SIZE;
20458 }else if( dwInitialSize>dwMaximumSize ){
20459 dwInitialSize = dwMaximumSize;
20460 }
20461 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
20462 dwInitialSize, dwMaximumSize);
20463 if( !pWinMemData->hHeap ){
20464 sqlite3_log(SQLITE_NOMEM,
20465 "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu",
20466 osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize,
20467 dwMaximumSize);
20468 return SQLITE_NOMEM_BKPT;
20469 }
20470 pWinMemData->bOwned = TRUE;
20471 assert( pWinMemData->bOwned );
20472 }
20473 #else
20474 pWinMemData->hHeap = osGetProcessHeap();
20475 if( !pWinMemData->hHeap ){
20476 sqlite3_log(SQLITE_NOMEM,
20477 "failed to GetProcessHeap (%lu)", osGetLastError());
20478 return SQLITE_NOMEM_BKPT;
20479 }
20480 pWinMemData->bOwned = FALSE;
20481 assert( !pWinMemData->bOwned );
20482 #endif
20483 assert( pWinMemData->hHeap!=0 );
20484 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
20485 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
20486 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
20487 #endif
20488 return SQLITE_OK;
20489 }
20490
20491 /*
20492 ** Deinitialize this module.
20493 */
20494 static void winMemShutdown(void *pAppData){
20495 winMemData *pWinMemData = (winMemData *)pAppData;
20496
20497 if( !pWinMemData ) return;
20498 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
20499 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
20500
20501 if( pWinMemData->hHeap ){
20502 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
20503 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
20504 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
20505 #endif
20506 if( pWinMemData->bOwned ){
20507 if( !osHeapDestroy(pWinMemData->hHeap) ){
20508 sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%lu), heap=%p",
20509 osGetLastError(), (void*)pWinMemData->hHeap);
20510 }
20511 pWinMemData->bOwned = FALSE;
20512 }
20513 pWinMemData->hHeap = NULL;
20514 }
20515 }
20516
20517 /*
20518 ** Populate the low-level memory allocation function pointers in
20519 ** sqlite3GlobalConfig.m with pointers to the routines in this file. The
20520 ** arguments specify the block of memory to manage.
20521 **
20522 ** This routine is only called by sqlite3_config(), and therefore
20523 ** is not required to be threadsafe (it is not).
20524 */
20525 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){
20526 static const sqlite3_mem_methods winMemMethods = {
20527 winMemMalloc,
20528 winMemFree,
20529 winMemRealloc,
20530 winMemSize,
20531 winMemRoundup,
20532 winMemInit,
20533 winMemShutdown,
20534 &win_mem_data
20535 };
20536 return &winMemMethods;
20537 }
20538
20539 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
20540 sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
20541 }
20542 #endif /* SQLITE_WIN32_MALLOC */
20543
20544 /*
20545 ** Convert a UTF-8 string to Microsoft Unicode.
20546 **
20547 ** Space to hold the returned string is obtained from sqlite3_malloc().
20548 */
20549 static LPWSTR winUtf8ToUnicode(const char *zText){
20550 int nChar;
20551 LPWSTR zWideText;
20552
20553 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
20554 if( nChar==0 ){
20555 return 0;
20556 }
20557 zWideText = sqlite3MallocZero( nChar*sizeof(WCHAR) );
20558 if( zWideText==0 ){
20559 return 0;
20560 }
20561 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
20562 nChar);
20563 if( nChar==0 ){
20564 sqlite3_free(zWideText);
20565 zWideText = 0;
20566 }
20567 return zWideText;
20568 }
20569
20570 /*
20571 ** Convert a Microsoft Unicode string to UTF-8.
20572 **
20573 ** Space to hold the returned string is obtained from sqlite3_malloc().
20574 */
20575 static char *winUnicodeToUtf8(LPCWSTR zWideText){
20576 int nByte;
20577 char *zText;
20578
20579 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
20580 if( nByte == 0 ){
20581 return 0;
20582 }
20583 zText = sqlite3MallocZero( nByte );
20584 if( zText==0 ){
20585 return 0;
20586 }
20587 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
20588 0, 0);
20589 if( nByte == 0 ){
20590 sqlite3_free(zText);
20591 zText = 0;
20592 }
20593 return zText;
20594 }
20595
20596 /*
20597 ** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM
20598 ** code page.
20599 **
20600 ** Space to hold the returned string is obtained from sqlite3_malloc().
20601 */
20602 static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){
20603 int nByte;
20604 LPWSTR zMbcsText;
20605 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
20606
20607 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
20608 0)*sizeof(WCHAR);
20609 if( nByte==0 ){
20610 return 0;
20611 }
20612 zMbcsText = sqlite3MallocZero( nByte*sizeof(WCHAR) );
20613 if( zMbcsText==0 ){
20614 return 0;
20615 }
20616 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
20617 nByte);
20618 if( nByte==0 ){
20619 sqlite3_free(zMbcsText);
20620 zMbcsText = 0;
20621 }
20622 return zMbcsText;
20623 }
20624
20625 /*
20626 ** Convert a Microsoft Unicode string to a multi-byte character string,
20627 ** using the ANSI or OEM code page.
20628 **
20629 ** Space to hold the returned string is obtained from sqlite3_malloc().
20630 */
20631 static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){
20632 int nByte;
20633 char *zText;
20634 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
20635
20636 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
20637 if( nByte == 0 ){
20638 return 0;
20639 }
20640 zText = sqlite3MallocZero( nByte );
20641 if( zText==0 ){
20642 return 0;
20643 }
20644 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
20645 nByte, 0, 0);
20646 if( nByte == 0 ){
20647 sqlite3_free(zText);
20648 zText = 0;
20649 }
20650 return zText;
20651 }
20652
20653 /*
20654 ** Convert a multi-byte character string to UTF-8.
20655 **
20656 ** Space to hold the returned string is obtained from sqlite3_malloc().
20657 */
20658 static char *winMbcsToUtf8(const char *zText, int useAnsi){
20659 char *zTextUtf8;
20660 LPWSTR zTmpWide;
20661
20662 zTmpWide = winMbcsToUnicode(zText, useAnsi);
20663 if( zTmpWide==0 ){
20664 return 0;
20665 }
20666 zTextUtf8 = winUnicodeToUtf8(zTmpWide);
20667 sqlite3_free(zTmpWide);
20668 return zTextUtf8;
20669 }
20670
20671 /*
20672 ** Convert a UTF-8 string to a multi-byte character string.
20673 **
20674 ** Space to hold the returned string is obtained from sqlite3_malloc().
20675 */
20676 static char *winUtf8ToMbcs(const char *zText, int useAnsi){
20677 char *zTextMbcs;
20678 LPWSTR zTmpWide;
20679
20680 zTmpWide = winUtf8ToUnicode(zText);
20681 if( zTmpWide==0 ){
20682 return 0;
20683 }
20684 zTextMbcs = winUnicodeToMbcs(zTmpWide, useAnsi);
20685 sqlite3_free(zTmpWide);
20686 return zTextMbcs;
20687 }
20688
20689 /*
20690 ** This is a public wrapper for the winUtf8ToUnicode() function.
20691 */
20692 SQLITE_API LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText){
20693 #ifdef SQLITE_ENABLE_API_ARMOR
20694 if( !zText ){
20695 (void)SQLITE_MISUSE_BKPT;
20696 return 0;
20697 }
20698 #endif
20699 #ifndef SQLITE_OMIT_AUTOINIT
20700 if( sqlite3_initialize() ) return 0;
20701 #endif
20702 return winUtf8ToUnicode(zText);
20703 }
20704
20705 /*
20706 ** This is a public wrapper for the winUnicodeToUtf8() function.
20707 */
20708 SQLITE_API char *sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){
20709 #ifdef SQLITE_ENABLE_API_ARMOR
20710 if( !zWideText ){
20711 (void)SQLITE_MISUSE_BKPT;
20712 return 0;
20713 }
20714 #endif
20715 #ifndef SQLITE_OMIT_AUTOINIT
20716 if( sqlite3_initialize() ) return 0;
20717 #endif
20718 return winUnicodeToUtf8(zWideText);
20719 }
20720
20721 /*
20722 ** This is a public wrapper for the winMbcsToUtf8() function.
20723 */
20724 SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zText){
20725 #ifdef SQLITE_ENABLE_API_ARMOR
20726 if( !zText ){
20727 (void)SQLITE_MISUSE_BKPT;
20728 return 0;
20729 }
20730 #endif
20731 #ifndef SQLITE_OMIT_AUTOINIT
20732 if( sqlite3_initialize() ) return 0;
20733 #endif
20734 return winMbcsToUtf8(zText, osAreFileApisANSI());
20735 }
20736
20737 /*
20738 ** This is a public wrapper for the winMbcsToUtf8() function.
20739 */
20740 SQLITE_API char *sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){
20741 #ifdef SQLITE_ENABLE_API_ARMOR
20742 if( !zText ){
20743 (void)SQLITE_MISUSE_BKPT;
20744 return 0;
20745 }
20746 #endif
20747 #ifndef SQLITE_OMIT_AUTOINIT
20748 if( sqlite3_initialize() ) return 0;
20749 #endif
20750 return winMbcsToUtf8(zText, useAnsi);
20751 }
20752
20753 /*
20754 ** This is a public wrapper for the winUtf8ToMbcs() function.
20755 */
20756 SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zText){
20757 #ifdef SQLITE_ENABLE_API_ARMOR
20758 if( !zText ){
20759 (void)SQLITE_MISUSE_BKPT;
20760 return 0;
20761 }
20762 #endif
20763 #ifndef SQLITE_OMIT_AUTOINIT
20764 if( sqlite3_initialize() ) return 0;
20765 #endif
20766 return winUtf8ToMbcs(zText, osAreFileApisANSI());
20767 }
20768
20769 /*
20770 ** This is a public wrapper for the winUtf8ToMbcs() function.
20771 */
20772 SQLITE_API char *sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){
20773 #ifdef SQLITE_ENABLE_API_ARMOR
20774 if( !zText ){
20775 (void)SQLITE_MISUSE_BKPT;
20776 return 0;
20777 }
20778 #endif
20779 #ifndef SQLITE_OMIT_AUTOINIT
20780 if( sqlite3_initialize() ) return 0;
20781 #endif
20782 return winUtf8ToMbcs(zText, useAnsi);
20783 }
20784
20785 /*
20786 ** This function sets the data directory or the temporary directory based on
20787 ** the provided arguments. The type argument must be 1 in order to set the
20788 ** data directory or 2 in order to set the temporary directory. The zValue
20789 ** argument is the name of the directory to use. The return value will be
20790 ** SQLITE_OK if successful.
20791 */
20792 SQLITE_API int sqlite3_win32_set_directory(DWORD type, LPCWSTR zValue){
20793 char **ppDirectory = 0;
20794 #ifndef SQLITE_OMIT_AUTOINIT
20795 int rc = sqlite3_initialize();
20796 if( rc ) return rc;
20797 #endif
20798 if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
20799 ppDirectory = &sqlite3_data_directory;
20800 }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
20801 ppDirectory = &sqlite3_temp_directory;
20802 }
20803 assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
20804 || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
20805 );
20806 assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
20807 if( ppDirectory ){
20808 char *zValueUtf8 = 0;
20809 if( zValue && zValue[0] ){
20810 zValueUtf8 = winUnicodeToUtf8(zValue);
20811 if ( zValueUtf8==0 ){
20812 return SQLITE_NOMEM_BKPT;
20813 }
20814 }
20815 sqlite3_free(*ppDirectory);
20816 *ppDirectory = zValueUtf8;
20817 return SQLITE_OK;
20818 }
20819 return SQLITE_ERROR;
20820 }
20821
20822 /*
20823 ** The return value of winGetLastErrorMsg
20824 ** is zero if the error message fits in the buffer, or non-zero
20825 ** otherwise (if the message was truncated).
20826 */
20827 static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
20828 /* FormatMessage returns 0 on failure. Otherwise it
20829 ** returns the number of TCHARs written to the output
20830 ** buffer, excluding the terminating null char.
20831 */
20832 DWORD dwLen = 0;
20833 char *zOut = 0;
20834
20835 if( osIsNT() ){
20836 #if SQLITE_OS_WINRT
20837 WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
20838 dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
20839 FORMAT_MESSAGE_IGNORE_INSERTS,
20840 NULL,
20841 lastErrno,
20842 0,
20843 zTempWide,
20844 SQLITE_WIN32_MAX_ERRMSG_CHARS,
20845 0);
20846 #else
20847 LPWSTR zTempWide = NULL;
20848 dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
20849 FORMAT_MESSAGE_FROM_SYSTEM |
20850 FORMAT_MESSAGE_IGNORE_INSERTS,
20851 NULL,
20852 lastErrno,
20853 0,
20854 (LPWSTR) &zTempWide,
20855 0,
20856 0);
20857 #endif
20858 if( dwLen > 0 ){
20859 /* allocate a buffer and convert to UTF8 */
20860 sqlite3BeginBenignMalloc();
20861 zOut = winUnicodeToUtf8(zTempWide);
20862 sqlite3EndBenignMalloc();
20863 #if !SQLITE_OS_WINRT
20864 /* free the system buffer allocated by FormatMessage */
20865 osLocalFree(zTempWide);
20866 #endif
20867 }
20868 }
20869 #ifdef SQLITE_WIN32_HAS_ANSI
20870 else{
20871 char *zTemp = NULL;
20872 dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
20873 FORMAT_MESSAGE_FROM_SYSTEM |
20874 FORMAT_MESSAGE_IGNORE_INSERTS,
20875 NULL,
20876 lastErrno,
20877 0,
20878 (LPSTR) &zTemp,
20879 0,
20880 0);
20881 if( dwLen > 0 ){
20882 /* allocate a buffer and convert to UTF8 */
20883 sqlite3BeginBenignMalloc();
20884 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
20885 sqlite3EndBenignMalloc();
20886 /* free the system buffer allocated by FormatMessage */
20887 osLocalFree(zTemp);
20888 }
20889 }
20890 #endif
20891 if( 0 == dwLen ){
20892 sqlite3_snprintf(nBuf, zBuf, "OsError 0x%lx (%lu)", lastErrno, lastErrno);
20893 }else{
20894 /* copy a maximum of nBuf chars to output buffer */
20895 sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
20896 /* free the UTF8 buffer */
20897 sqlite3_free(zOut);
20898 }
20899 return 0;
20900 }
20901
20902 /*
20903 **
20904 ** This function - winLogErrorAtLine() - is only ever called via the macro
20905 ** winLogError().
20906 **
20907 ** This routine is invoked after an error occurs in an OS function.
20908 ** It logs a message using sqlite3_log() containing the current value of
20909 ** error code and, if possible, the human-readable equivalent from
20910 ** FormatMessage.
20911 **
20912 ** The first argument passed to the macro should be the error code that
20913 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
20914 ** The two subsequent arguments should be the name of the OS function that
20915 ** failed and the associated file-system path, if any.
20916 */
20917 #define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__)
20918 static int winLogErrorAtLine(
20919 int errcode, /* SQLite error code */
20920 DWORD lastErrno, /* Win32 last error */
20921 const char *zFunc, /* Name of OS function that failed */
20922 const char *zPath, /* File path associated with error */
20923 int iLine /* Source line number where error occurred */
20924 ){
20925 char zMsg[500]; /* Human readable error text */
20926 int i; /* Loop counter */
20927
20928 zMsg[0] = 0;
20929 winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
20930 assert( errcode!=SQLITE_OK );
20931 if( zPath==0 ) zPath = "";
20932 for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
20933 zMsg[i] = 0;
20934 sqlite3_log(errcode,
20935 "os_win.c:%d: (%lu) %s(%s) - %s",
20936 iLine, lastErrno, zFunc, zPath, zMsg
20937 );
20938
20939 return errcode;
20940 }
20941
20942 /*
20943 ** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
20944 ** will be retried following a locking error - probably caused by
20945 ** antivirus software. Also the initial delay before the first retry.
20946 ** The delay increases linearly with each retry.
20947 */
20948 #ifndef SQLITE_WIN32_IOERR_RETRY
20949 # define SQLITE_WIN32_IOERR_RETRY 10
20950 #endif
20951 #ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
20952 # define SQLITE_WIN32_IOERR_RETRY_DELAY 25
20953 #endif
20954 static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
20955 static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
20956
20957 /*
20958 ** The "winIoerrCanRetry1" macro is used to determine if a particular I/O
20959 ** error code obtained via GetLastError() is eligible to be retried. It
20960 ** must accept the error code DWORD as its only argument and should return
20961 ** non-zero if the error code is transient in nature and the operation
20962 ** responsible for generating the original error might succeed upon being
20963 ** retried. The argument to this macro should be a variable.
20964 **
20965 ** Additionally, a macro named "winIoerrCanRetry2" may be defined. If it
20966 ** is defined, it will be consulted only when the macro "winIoerrCanRetry1"
20967 ** returns zero. The "winIoerrCanRetry2" macro is completely optional and
20968 ** may be used to include additional error codes in the set that should
20969 ** result in the failing I/O operation being retried by the caller. If
20970 ** defined, the "winIoerrCanRetry2" macro must exhibit external semantics
20971 ** identical to those of the "winIoerrCanRetry1" macro.
20972 */
20973 #if !defined(winIoerrCanRetry1)
20974 #define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED) || \
20975 ((a)==ERROR_SHARING_VIOLATION) || \
20976 ((a)==ERROR_LOCK_VIOLATION) || \
20977 ((a)==ERROR_DEV_NOT_EXIST) || \
20978 ((a)==ERROR_NETNAME_DELETED) || \
20979 ((a)==ERROR_SEM_TIMEOUT) || \
20980 ((a)==ERROR_NETWORK_UNREACHABLE))
20981 #endif
20982
20983 /*
20984 ** If a ReadFile() or WriteFile() error occurs, invoke this routine
20985 ** to see if it should be retried. Return TRUE to retry. Return FALSE
20986 ** to give up with an error.
20987 */
20988 static int winRetryIoerr(int *pnRetry, DWORD *pError){
20989 DWORD e = osGetLastError();
20990 if( *pnRetry>=winIoerrRetry ){
20991 if( pError ){
20992 *pError = e;
20993 }
20994 return 0;
20995 }
20996 if( winIoerrCanRetry1(e) ){
20997 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
20998 ++*pnRetry;
20999 return 1;
21000 }
21001 #if defined(winIoerrCanRetry2)
21002 else if( winIoerrCanRetry2(e) ){
21003 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
21004 ++*pnRetry;
21005 return 1;
21006 }
21007 #endif
21008 if( pError ){
21009 *pError = e;
21010 }
21011 return 0;
21012 }
21013
21014 /*
21015 ** Log a I/O error retry episode.
21016 */
21017 static void winLogIoerr(int nRetry, int lineno){
21018 if( nRetry ){
21019 sqlite3_log(SQLITE_NOTICE,
21020 "delayed %dms for lock/sharing conflict at line %d",
21021 winIoerrRetryDelay*nRetry*(nRetry+1)/2, lineno
21022 );
21023 }
21024 }
21025
21026 /*
21027 ** This #if does not rely on the SQLITE_OS_WINCE define because the
21028 ** corresponding section in "date.c" cannot use it.
21029 */
21030 #if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
21031 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
21032 /*
21033 ** The MSVC CRT on Windows CE may not have a localtime() function.
21034 ** So define a substitute.
21035 */
21036 /* # include <time.h> */
21037 struct tm *__cdecl localtime(const time_t *t)
21038 {
21039 static struct tm y;
21040 FILETIME uTm, lTm;
21041 SYSTEMTIME pTm;
21042 sqlite3_int64 t64;
21043 t64 = *t;
21044 t64 = (t64 + 11644473600)*10000000;
21045 uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
21046 uTm.dwHighDateTime= (DWORD)(t64 >> 32);
21047 osFileTimeToLocalFileTime(&uTm,&lTm);
21048 osFileTimeToSystemTime(&lTm,&pTm);
21049 y.tm_year = pTm.wYear - 1900;
21050 y.tm_mon = pTm.wMonth - 1;
21051 y.tm_wday = pTm.wDayOfWeek;
21052 y.tm_mday = pTm.wDay;
21053 y.tm_hour = pTm.wHour;
21054 y.tm_min = pTm.wMinute;
21055 y.tm_sec = pTm.wSecond;
21056 return &y;
21057 }
21058 #endif
21059
21060 #if SQLITE_OS_WINCE
21061 /*************************************************************************
21062 ** This section contains code for WinCE only.
21063 */
21064 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
21065
21066 /*
21067 ** Acquire a lock on the handle h
21068 */
21069 static void winceMutexAcquire(HANDLE h){
21070 DWORD dwErr;
21071 do {
21072 dwErr = osWaitForSingleObject(h, INFINITE);
21073 } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
21074 }
21075 /*
21076 ** Release a lock acquired by winceMutexAcquire()
21077 */
21078 #define winceMutexRelease(h) ReleaseMutex(h)
21079
21080 /*
21081 ** Create the mutex and shared memory used for locking in the file
21082 ** descriptor pFile
21083 */
21084 static int winceCreateLock(const char *zFilename, winFile *pFile){
21085 LPWSTR zTok;
21086 LPWSTR zName;
21087 DWORD lastErrno;
21088 BOOL bLogged = FALSE;
21089 BOOL bInit = TRUE;
21090
21091 zName = winUtf8ToUnicode(zFilename);
21092 if( zName==0 ){
21093 /* out of memory */
21094 return SQLITE_IOERR_NOMEM_BKPT;
21095 }
21096
21097 /* Initialize the local lockdata */
21098 memset(&pFile->local, 0, sizeof(pFile->local));
21099
21100 /* Replace the backslashes from the filename and lowercase it
21101 ** to derive a mutex name. */
21102 zTok = osCharLowerW(zName);
21103 for (;*zTok;zTok++){
21104 if (*zTok == '\\') *zTok = '_';
21105 }
21106
21107 /* Create/open the named mutex */
21108 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
21109 if (!pFile->hMutex){
21110 pFile->lastErrno = osGetLastError();
21111 sqlite3_free(zName);
21112 return winLogError(SQLITE_IOERR, pFile->lastErrno,
21113 "winceCreateLock1", zFilename);
21114 }
21115
21116 /* Acquire the mutex before continuing */
21117 winceMutexAcquire(pFile->hMutex);
21118
21119 /* Since the names of named mutexes, semaphores, file mappings etc are
21120 ** case-sensitive, take advantage of that by uppercasing the mutex name
21121 ** and using that as the shared filemapping name.
21122 */
21123 osCharUpperW(zName);
21124 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
21125 PAGE_READWRITE, 0, sizeof(winceLock),
21126 zName);
21127
21128 /* Set a flag that indicates we're the first to create the memory so it
21129 ** must be zero-initialized */
21130 lastErrno = osGetLastError();
21131 if (lastErrno == ERROR_ALREADY_EXISTS){
21132 bInit = FALSE;
21133 }
21134
21135 sqlite3_free(zName);
21136
21137 /* If we succeeded in making the shared memory handle, map it. */
21138 if( pFile->hShared ){
21139 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
21140 FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
21141 /* If mapping failed, close the shared memory handle and erase it */
21142 if( !pFile->shared ){
21143 pFile->lastErrno = osGetLastError();
21144 winLogError(SQLITE_IOERR, pFile->lastErrno,
21145 "winceCreateLock2", zFilename);
21146 bLogged = TRUE;
21147 osCloseHandle(pFile->hShared);
21148 pFile->hShared = NULL;
21149 }
21150 }
21151
21152 /* If shared memory could not be created, then close the mutex and fail */
21153 if( pFile->hShared==NULL ){
21154 if( !bLogged ){
21155 pFile->lastErrno = lastErrno;
21156 winLogError(SQLITE_IOERR, pFile->lastErrno,
21157 "winceCreateLock3", zFilename);
21158 bLogged = TRUE;
21159 }
21160 winceMutexRelease(pFile->hMutex);
21161 osCloseHandle(pFile->hMutex);
21162 pFile->hMutex = NULL;
21163 return SQLITE_IOERR;
21164 }
21165
21166 /* Initialize the shared memory if we're supposed to */
21167 if( bInit ){
21168 memset(pFile->shared, 0, sizeof(winceLock));
21169 }
21170
21171 winceMutexRelease(pFile->hMutex);
21172 return SQLITE_OK;
21173 }
21174
21175 /*
21176 ** Destroy the part of winFile that deals with wince locks
21177 */
21178 static void winceDestroyLock(winFile *pFile){
21179 if (pFile->hMutex){
21180 /* Acquire the mutex */
21181 winceMutexAcquire(pFile->hMutex);
21182
21183 /* The following blocks should probably assert in debug mode, but they
21184 are to cleanup in case any locks remained open */
21185 if (pFile->local.nReaders){
21186 pFile->shared->nReaders --;
21187 }
21188 if (pFile->local.bReserved){
21189 pFile->shared->bReserved = FALSE;
21190 }
21191 if (pFile->local.bPending){
21192 pFile->shared->bPending = FALSE;
21193 }
21194 if (pFile->local.bExclusive){
21195 pFile->shared->bExclusive = FALSE;
21196 }
21197
21198 /* De-reference and close our copy of the shared memory handle */
21199 osUnmapViewOfFile(pFile->shared);
21200 osCloseHandle(pFile->hShared);
21201
21202 /* Done with the mutex */
21203 winceMutexRelease(pFile->hMutex);
21204 osCloseHandle(pFile->hMutex);
21205 pFile->hMutex = NULL;
21206 }
21207 }
21208
21209 /*
21210 ** An implementation of the LockFile() API of Windows for CE
21211 */
21212 static BOOL winceLockFile(
21213 LPHANDLE phFile,
21214 DWORD dwFileOffsetLow,
21215 DWORD dwFileOffsetHigh,
21216 DWORD nNumberOfBytesToLockLow,
21217 DWORD nNumberOfBytesToLockHigh
21218 ){
21219 winFile *pFile = HANDLE_TO_WINFILE(phFile);
21220 BOOL bReturn = FALSE;
21221
21222 UNUSED_PARAMETER(dwFileOffsetHigh);
21223 UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
21224
21225 if (!pFile->hMutex) return TRUE;
21226 winceMutexAcquire(pFile->hMutex);
21227
21228 /* Wanting an exclusive lock? */
21229 if (dwFileOffsetLow == (DWORD)SHARED_FIRST
21230 && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
21231 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
21232 pFile->shared->bExclusive = TRUE;
21233 pFile->local.bExclusive = TRUE;
21234 bReturn = TRUE;
21235 }
21236 }
21237
21238 /* Want a read-only lock? */
21239 else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
21240 nNumberOfBytesToLockLow == 1){
21241 if (pFile->shared->bExclusive == 0){
21242 pFile->local.nReaders ++;
21243 if (pFile->local.nReaders == 1){
21244 pFile->shared->nReaders ++;
21245 }
21246 bReturn = TRUE;
21247 }
21248 }
21249
21250 /* Want a pending lock? */
21251 else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
21252 && nNumberOfBytesToLockLow == 1){
21253 /* If no pending lock has been acquired, then acquire it */
21254 if (pFile->shared->bPending == 0) {
21255 pFile->shared->bPending = TRUE;
21256 pFile->local.bPending = TRUE;
21257 bReturn = TRUE;
21258 }
21259 }
21260
21261 /* Want a reserved lock? */
21262 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
21263 && nNumberOfBytesToLockLow == 1){
21264 if (pFile->shared->bReserved == 0) {
21265 pFile->shared->bReserved = TRUE;
21266 pFile->local.bReserved = TRUE;
21267 bReturn = TRUE;
21268 }
21269 }
21270
21271 winceMutexRelease(pFile->hMutex);
21272 return bReturn;
21273 }
21274
21275 /*
21276 ** An implementation of the UnlockFile API of Windows for CE
21277 */
21278 static BOOL winceUnlockFile(
21279 LPHANDLE phFile,
21280 DWORD dwFileOffsetLow,
21281 DWORD dwFileOffsetHigh,
21282 DWORD nNumberOfBytesToUnlockLow,
21283 DWORD nNumberOfBytesToUnlockHigh
21284 ){
21285 winFile *pFile = HANDLE_TO_WINFILE(phFile);
21286 BOOL bReturn = FALSE;
21287
21288 UNUSED_PARAMETER(dwFileOffsetHigh);
21289 UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
21290
21291 if (!pFile->hMutex) return TRUE;
21292 winceMutexAcquire(pFile->hMutex);
21293
21294 /* Releasing a reader lock or an exclusive lock */
21295 if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
21296 /* Did we have an exclusive lock? */
21297 if (pFile->local.bExclusive){
21298 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
21299 pFile->local.bExclusive = FALSE;
21300 pFile->shared->bExclusive = FALSE;
21301 bReturn = TRUE;
21302 }
21303
21304 /* Did we just have a reader lock? */
21305 else if (pFile->local.nReaders){
21306 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE
21307 || nNumberOfBytesToUnlockLow == 1);
21308 pFile->local.nReaders --;
21309 if (pFile->local.nReaders == 0)
21310 {
21311 pFile->shared->nReaders --;
21312 }
21313 bReturn = TRUE;
21314 }
21315 }
21316
21317 /* Releasing a pending lock */
21318 else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
21319 && nNumberOfBytesToUnlockLow == 1){
21320 if (pFile->local.bPending){
21321 pFile->local.bPending = FALSE;
21322 pFile->shared->bPending = FALSE;
21323 bReturn = TRUE;
21324 }
21325 }
21326 /* Releasing a reserved lock */
21327 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
21328 && nNumberOfBytesToUnlockLow == 1){
21329 if (pFile->local.bReserved) {
21330 pFile->local.bReserved = FALSE;
21331 pFile->shared->bReserved = FALSE;
21332 bReturn = TRUE;
21333 }
21334 }
21335
21336 winceMutexRelease(pFile->hMutex);
21337 return bReturn;
21338 }
21339 /*
21340 ** End of the special code for wince
21341 *****************************************************************************/
21342 #endif /* SQLITE_OS_WINCE */
21343
21344 /*
21345 ** Lock a file region.
21346 */
21347 static BOOL winLockFile(
21348 LPHANDLE phFile,
21349 DWORD flags,
21350 DWORD offsetLow,
21351 DWORD offsetHigh,
21352 DWORD numBytesLow,
21353 DWORD numBytesHigh
21354 ){
21355 #if SQLITE_OS_WINCE
21356 /*
21357 ** NOTE: Windows CE is handled differently here due its lack of the Win32
21358 ** API LockFile.
21359 */
21360 return winceLockFile(phFile, offsetLow, offsetHigh,
21361 numBytesLow, numBytesHigh);
21362 #else
21363 if( osIsNT() ){
21364 OVERLAPPED ovlp;
21365 memset(&ovlp, 0, sizeof(OVERLAPPED));
21366 ovlp.Offset = offsetLow;
21367 ovlp.OffsetHigh = offsetHigh;
21368 return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
21369 }else{
21370 return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
21371 numBytesHigh);
21372 }
21373 #endif
21374 }
21375
21376 /*
21377 ** Unlock a file region.
21378 */
21379 static BOOL winUnlockFile(
21380 LPHANDLE phFile,
21381 DWORD offsetLow,
21382 DWORD offsetHigh,
21383 DWORD numBytesLow,
21384 DWORD numBytesHigh
21385 ){
21386 #if SQLITE_OS_WINCE
21387 /*
21388 ** NOTE: Windows CE is handled differently here due its lack of the Win32
21389 ** API UnlockFile.
21390 */
21391 return winceUnlockFile(phFile, offsetLow, offsetHigh,
21392 numBytesLow, numBytesHigh);
21393 #else
21394 if( osIsNT() ){
21395 OVERLAPPED ovlp;
21396 memset(&ovlp, 0, sizeof(OVERLAPPED));
21397 ovlp.Offset = offsetLow;
21398 ovlp.OffsetHigh = offsetHigh;
21399 return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
21400 }else{
21401 return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
21402 numBytesHigh);
21403 }
21404 #endif
21405 }
21406
21407 /*****************************************************************************
21408 ** The next group of routines implement the I/O methods specified
21409 ** by the sqlite3_io_methods object.
21410 ******************************************************************************/
21411
21412 /*
21413 ** Some Microsoft compilers lack this definition.
21414 */
21415 #ifndef INVALID_SET_FILE_POINTER
21416 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
21417 #endif
21418
21419 /*
21420 ** Move the current position of the file handle passed as the first
21421 ** argument to offset iOffset within the file. If successful, return 0.
21422 ** Otherwise, set pFile->lastErrno and return non-zero.
21423 */
21424 static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
21425 #if !SQLITE_OS_WINRT
21426 LONG upperBits; /* Most sig. 32 bits of new offset */
21427 LONG lowerBits; /* Least sig. 32 bits of new offset */
21428 DWORD dwRet; /* Value returned by SetFilePointer() */
21429 DWORD lastErrno; /* Value returned by GetLastError() */
21430
21431 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
21432
21433 upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
21434 lowerBits = (LONG)(iOffset & 0xffffffff);
21435
21436 /* API oddity: If successful, SetFilePointer() returns a dword
21437 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
21438 ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
21439 ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
21440 ** whether an error has actually occurred, it is also necessary to call
21441 ** GetLastError().
21442 */
21443 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
21444
21445 if( (dwRet==INVALID_SET_FILE_POINTER
21446 && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
21447 pFile->lastErrno = lastErrno;
21448 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
21449 "winSeekFile", pFile->zPath);
21450 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
21451 return 1;
21452 }
21453
21454 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
21455 return 0;
21456 #else
21457 /*
21458 ** Same as above, except that this implementation works for WinRT.
21459 */
21460
21461 LARGE_INTEGER x; /* The new offset */
21462 BOOL bRet; /* Value returned by SetFilePointerEx() */
21463
21464 x.QuadPart = iOffset;
21465 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
21466
21467 if(!bRet){
21468 pFile->lastErrno = osGetLastError();
21469 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
21470 "winSeekFile", pFile->zPath);
21471 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
21472 return 1;
21473 }
21474
21475 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
21476 return 0;
21477 #endif
21478 }
21479
21480 #if SQLITE_MAX_MMAP_SIZE>0
21481 /* Forward references to VFS helper methods used for memory mapped files */
21482 static int winMapfile(winFile*, sqlite3_int64);
21483 static int winUnmapfile(winFile*);
21484 #endif
21485
21486 /*
21487 ** Close a file.
21488 **
21489 ** It is reported that an attempt to close a handle might sometimes
21490 ** fail. This is a very unreasonable result, but Windows is notorious
21491 ** for being unreasonable so I do not doubt that it might happen. If
21492 ** the close fails, we pause for 100 milliseconds and try again. As
21493 ** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
21494 ** giving up and returning an error.
21495 */
21496 #define MX_CLOSE_ATTEMPT 3
21497 static int winClose(sqlite3_file *id){
21498 int rc, cnt = 0;
21499 winFile *pFile = (winFile*)id;
21500
21501 assert( id!=0 );
21502 #ifndef SQLITE_OMIT_WAL
21503 assert( pFile->pShm==0 );
21504 #endif
21505 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
21506 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p\n",
21507 osGetCurrentProcessId(), pFile, pFile->h));
21508
21509 #if SQLITE_MAX_MMAP_SIZE>0
21510 winUnmapfile(pFile);
21511 #endif
21512
21513 do{
21514 rc = osCloseHandle(pFile->h);
21515 /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
21516 }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
21517 #if SQLITE_OS_WINCE
21518 #define WINCE_DELETION_ATTEMPTS 3
21519 {
21520 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
21521 if( pAppData==NULL || !pAppData->bNoLock ){
21522 winceDestroyLock(pFile);
21523 }
21524 }
21525 if( pFile->zDeleteOnClose ){
21526 int cnt = 0;
21527 while(
21528 osDeleteFileW(pFile->zDeleteOnClose)==0
21529 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
21530 && cnt++ < WINCE_DELETION_ATTEMPTS
21531 ){
21532 sqlite3_win32_sleep(100); /* Wait a little before trying again */
21533 }
21534 sqlite3_free(pFile->zDeleteOnClose);
21535 }
21536 #endif
21537 if( rc ){
21538 pFile->h = NULL;
21539 }
21540 OpenCounter(-1);
21541 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p, rc=%s\n",
21542 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
21543 return rc ? SQLITE_OK
21544 : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
21545 "winClose", pFile->zPath);
21546 }
21547
21548 /*
21549 ** Read data from a file into a buffer. Return SQLITE_OK if all
21550 ** bytes were read successfully and SQLITE_IOERR if anything goes
21551 ** wrong.
21552 */
21553 static int winRead(
21554 sqlite3_file *id, /* File to read from */
21555 void *pBuf, /* Write content into this buffer */
21556 int amt, /* Number of bytes to read */
21557 sqlite3_int64 offset /* Begin reading at this offset */
21558 ){
21559 #if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
21560 OVERLAPPED overlapped; /* The offset for ReadFile. */
21561 #endif
21562 winFile *pFile = (winFile*)id; /* file handle */
21563 DWORD nRead; /* Number of bytes actually read from file */
21564 int nRetry = 0; /* Number of retrys */
21565
21566 assert( id!=0 );
21567 assert( amt>0 );
21568 assert( offset>=0 );
21569 SimulateIOError(return SQLITE_IOERR_READ);
21570 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
21571 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
21572 pFile->h, pBuf, amt, offset, pFile->locktype));
21573
21574 #if SQLITE_MAX_MMAP_SIZE>0
21575 /* Deal with as much of this read request as possible by transfering
21576 ** data from the memory mapping using memcpy(). */
21577 if( offset<pFile->mmapSize ){
21578 if( offset+amt <= pFile->mmapSize ){
21579 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
21580 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
21581 osGetCurrentProcessId(), pFile, pFile->h));
21582 return SQLITE_OK;
21583 }else{
21584 int nCopy = (int)(pFile->mmapSize - offset);
21585 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
21586 pBuf = &((u8 *)pBuf)[nCopy];
21587 amt -= nCopy;
21588 offset += nCopy;
21589 }
21590 }
21591 #endif
21592
21593 #if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
21594 if( winSeekFile(pFile, offset) ){
21595 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
21596 osGetCurrentProcessId(), pFile, pFile->h));
21597 return SQLITE_FULL;
21598 }
21599 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
21600 #else
21601 memset(&overlapped, 0, sizeof(OVERLAPPED));
21602 overlapped.Offset = (LONG)(offset & 0xffffffff);
21603 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
21604 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
21605 osGetLastError()!=ERROR_HANDLE_EOF ){
21606 #endif
21607 DWORD lastErrno;
21608 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
21609 pFile->lastErrno = lastErrno;
21610 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_READ\n",
21611 osGetCurrentProcessId(), pFile, pFile->h));
21612 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
21613 "winRead", pFile->zPath);
21614 }
21615 winLogIoerr(nRetry, __LINE__);
21616 if( nRead<(DWORD)amt ){
21617 /* Unread parts of the buffer must be zero-filled */
21618 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
21619 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_SHORT_READ\n",
21620 osGetCurrentProcessId(), pFile, pFile->h));
21621 return SQLITE_IOERR_SHORT_READ;
21622 }
21623
21624 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
21625 osGetCurrentProcessId(), pFile, pFile->h));
21626 return SQLITE_OK;
21627 }
21628
21629 /*
21630 ** Write data from a buffer into a file. Return SQLITE_OK on success
21631 ** or some other error code on failure.
21632 */
21633 static int winWrite(
21634 sqlite3_file *id, /* File to write into */
21635 const void *pBuf, /* The bytes to be written */
21636 int amt, /* Number of bytes to write */
21637 sqlite3_int64 offset /* Offset into the file to begin writing at */
21638 ){
21639 int rc = 0; /* True if error has occurred, else false */
21640 winFile *pFile = (winFile*)id; /* File handle */
21641 int nRetry = 0; /* Number of retries */
21642
21643 assert( amt>0 );
21644 assert( pFile );
21645 SimulateIOError(return SQLITE_IOERR_WRITE);
21646 SimulateDiskfullError(return SQLITE_FULL);
21647
21648 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
21649 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
21650 pFile->h, pBuf, amt, offset, pFile->locktype));
21651
21652 #if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
21653 /* Deal with as much of this write request as possible by transfering
21654 ** data from the memory mapping using memcpy(). */
21655 if( offset<pFile->mmapSize ){
21656 if( offset+amt <= pFile->mmapSize ){
21657 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
21658 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
21659 osGetCurrentProcessId(), pFile, pFile->h));
21660 return SQLITE_OK;
21661 }else{
21662 int nCopy = (int)(pFile->mmapSize - offset);
21663 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
21664 pBuf = &((u8 *)pBuf)[nCopy];
21665 amt -= nCopy;
21666 offset += nCopy;
21667 }
21668 }
21669 #endif
21670
21671 #if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
21672 rc = winSeekFile(pFile, offset);
21673 if( rc==0 ){
21674 #else
21675 {
21676 #endif
21677 #if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
21678 OVERLAPPED overlapped; /* The offset for WriteFile. */
21679 #endif
21680 u8 *aRem = (u8 *)pBuf; /* Data yet to be written */
21681 int nRem = amt; /* Number of bytes yet to be written */
21682 DWORD nWrite; /* Bytes written by each WriteFile() call */
21683 DWORD lastErrno = NO_ERROR; /* Value returned by GetLastError() */
21684
21685 #if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
21686 memset(&overlapped, 0, sizeof(OVERLAPPED));
21687 overlapped.Offset = (LONG)(offset & 0xffffffff);
21688 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
21689 #endif
21690
21691 while( nRem>0 ){
21692 #if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
21693 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
21694 #else
21695 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
21696 #endif
21697 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
21698 break;
21699 }
21700 assert( nWrite==0 || nWrite<=(DWORD)nRem );
21701 if( nWrite==0 || nWrite>(DWORD)nRem ){
21702 lastErrno = osGetLastError();
21703 break;
21704 }
21705 #if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
21706 offset += nWrite;
21707 overlapped.Offset = (LONG)(offset & 0xffffffff);
21708 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
21709 #endif
21710 aRem += nWrite;
21711 nRem -= nWrite;
21712 }
21713 if( nRem>0 ){
21714 pFile->lastErrno = lastErrno;
21715 rc = 1;
21716 }
21717 }
21718
21719 if( rc ){
21720 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
21721 || ( pFile->lastErrno==ERROR_DISK_FULL )){
21722 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
21723 osGetCurrentProcessId(), pFile, pFile->h));
21724 return winLogError(SQLITE_FULL, pFile->lastErrno,
21725 "winWrite1", pFile->zPath);
21726 }
21727 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_WRITE\n",
21728 osGetCurrentProcessId(), pFile, pFile->h));
21729 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
21730 "winWrite2", pFile->zPath);
21731 }else{
21732 winLogIoerr(nRetry, __LINE__);
21733 }
21734 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
21735 osGetCurrentProcessId(), pFile, pFile->h));
21736 return SQLITE_OK;
21737 }
21738
21739 /*
21740 ** Truncate an open file to a specified size
21741 */
21742 static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
21743 winFile *pFile = (winFile*)id; /* File handle object */
21744 int rc = SQLITE_OK; /* Return code for this function */
21745 DWORD lastErrno;
21746
21747 assert( pFile );
21748 SimulateIOError(return SQLITE_IOERR_TRUNCATE);
21749 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n",
21750 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
21751
21752 /* If the user has configured a chunk-size for this file, truncate the
21753 ** file so that it consists of an integer number of chunks (i.e. the
21754 ** actual file size after the operation may be larger than the requested
21755 ** size).
21756 */
21757 if( pFile->szChunk>0 ){
21758 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
21759 }
21760
21761 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
21762 if( winSeekFile(pFile, nByte) ){
21763 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
21764 "winTruncate1", pFile->zPath);
21765 }else if( 0==osSetEndOfFile(pFile->h) &&
21766 ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
21767 pFile->lastErrno = lastErrno;
21768 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
21769 "winTruncate2", pFile->zPath);
21770 }
21771
21772 #if SQLITE_MAX_MMAP_SIZE>0
21773 /* If the file was truncated to a size smaller than the currently
21774 ** mapped region, reduce the effective mapping size as well. SQLite will
21775 ** use read() and write() to access data beyond this point from now on.
21776 */
21777 if( pFile->pMapRegion && nByte<pFile->mmapSize ){
21778 pFile->mmapSize = nByte;
21779 }
21780 #endif
21781
21782 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, rc=%s\n",
21783 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
21784 return rc;
21785 }
21786
21787 #ifdef SQLITE_TEST
21788 /*
21789 ** Count the number of fullsyncs and normal syncs. This is used to test
21790 ** that syncs and fullsyncs are occuring at the right times.
21791 */
21792 SQLITE_API int sqlite3_sync_count = 0;
21793 SQLITE_API int sqlite3_fullsync_count = 0;
21794 #endif
21795
21796 /*
21797 ** Make sure all writes to a particular file are committed to disk.
21798 */
21799 static int winSync(sqlite3_file *id, int flags){
21800 #ifndef SQLITE_NO_SYNC
21801 /*
21802 ** Used only when SQLITE_NO_SYNC is not defined.
21803 */
21804 BOOL rc;
21805 #endif
21806 #if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \
21807 defined(SQLITE_HAVE_OS_TRACE)
21808 /*
21809 ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
21810 ** OSTRACE() macros.
21811 */
21812 winFile *pFile = (winFile*)id;
21813 #else
21814 UNUSED_PARAMETER(id);
21815 #endif
21816
21817 assert( pFile );
21818 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
21819 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
21820 || (flags&0x0F)==SQLITE_SYNC_FULL
21821 );
21822
21823 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
21824 ** line is to test that doing so does not cause any problems.
21825 */
21826 SimulateDiskfullError( return SQLITE_FULL );
21827
21828 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, flags=%x, lock=%d\n",
21829 osGetCurrentProcessId(), pFile, pFile->h, flags,
21830 pFile->locktype));
21831
21832 #ifndef SQLITE_TEST
21833 UNUSED_PARAMETER(flags);
21834 #else
21835 if( (flags&0x0F)==SQLITE_SYNC_FULL ){
21836 sqlite3_fullsync_count++;
21837 }
21838 sqlite3_sync_count++;
21839 #endif
21840
21841 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
21842 ** no-op
21843 */
21844 #ifdef SQLITE_NO_SYNC
21845 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
21846 osGetCurrentProcessId(), pFile, pFile->h));
21847 return SQLITE_OK;
21848 #else
21849 #if SQLITE_MAX_MMAP_SIZE>0
21850 if( pFile->pMapRegion ){
21851 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
21852 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
21853 "rc=SQLITE_OK\n", osGetCurrentProcessId(),
21854 pFile, pFile->pMapRegion));
21855 }else{
21856 pFile->lastErrno = osGetLastError();
21857 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
21858 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(),
21859 pFile, pFile->pMapRegion));
21860 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
21861 "winSync1", pFile->zPath);
21862 }
21863 }
21864 #endif
21865 rc = osFlushFileBuffers(pFile->h);
21866 SimulateIOError( rc=FALSE );
21867 if( rc ){
21868 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
21869 osGetCurrentProcessId(), pFile, pFile->h));
21870 return SQLITE_OK;
21871 }else{
21872 pFile->lastErrno = osGetLastError();
21873 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_FSYNC\n",
21874 osGetCurrentProcessId(), pFile, pFile->h));
21875 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
21876 "winSync2", pFile->zPath);
21877 }
21878 #endif
21879 }
21880
21881 /*
21882 ** Determine the current size of a file in bytes
21883 */
21884 static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
21885 winFile *pFile = (winFile*)id;
21886 int rc = SQLITE_OK;
21887
21888 assert( id!=0 );
21889 assert( pSize!=0 );
21890 SimulateIOError(return SQLITE_IOERR_FSTAT);
21891 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
21892
21893 #if SQLITE_OS_WINRT
21894 {
21895 FILE_STANDARD_INFO info;
21896 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
21897 &info, sizeof(info)) ){
21898 *pSize = info.EndOfFile.QuadPart;
21899 }else{
21900 pFile->lastErrno = osGetLastError();
21901 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
21902 "winFileSize", pFile->zPath);
21903 }
21904 }
21905 #else
21906 {
21907 DWORD upperBits;
21908 DWORD lowerBits;
21909 DWORD lastErrno;
21910
21911 lowerBits = osGetFileSize(pFile->h, &upperBits);
21912 *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
21913 if( (lowerBits == INVALID_FILE_SIZE)
21914 && ((lastErrno = osGetLastError())!=NO_ERROR) ){
21915 pFile->lastErrno = lastErrno;
21916 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
21917 "winFileSize", pFile->zPath);
21918 }
21919 }
21920 #endif
21921 OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n",
21922 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
21923 return rc;
21924 }
21925
21926 /*
21927 ** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
21928 */
21929 #ifndef LOCKFILE_FAIL_IMMEDIATELY
21930 # define LOCKFILE_FAIL_IMMEDIATELY 1
21931 #endif
21932
21933 #ifndef LOCKFILE_EXCLUSIVE_LOCK
21934 # define LOCKFILE_EXCLUSIVE_LOCK 2
21935 #endif
21936
21937 /*
21938 ** Historically, SQLite has used both the LockFile and LockFileEx functions.
21939 ** When the LockFile function was used, it was always expected to fail
21940 ** immediately if the lock could not be obtained. Also, it always expected to
21941 ** obtain an exclusive lock. These flags are used with the LockFileEx function
21942 ** and reflect those expectations; therefore, they should not be changed.
21943 */
21944 #ifndef SQLITE_LOCKFILE_FLAGS
21945 # define SQLITE_LOCKFILE_FLAGS (LOCKFILE_FAIL_IMMEDIATELY | \
21946 LOCKFILE_EXCLUSIVE_LOCK)
21947 #endif
21948
21949 /*
21950 ** Currently, SQLite never calls the LockFileEx function without wanting the
21951 ** call to fail immediately if the lock cannot be obtained.
21952 */
21953 #ifndef SQLITE_LOCKFILEEX_FLAGS
21954 # define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY)
21955 #endif
21956
21957 /*
21958 ** Acquire a reader lock.
21959 ** Different API routines are called depending on whether or not this
21960 ** is Win9x or WinNT.
21961 */
21962 static int winGetReadLock(winFile *pFile){
21963 int res;
21964 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
21965 if( osIsNT() ){
21966 #if SQLITE_OS_WINCE
21967 /*
21968 ** NOTE: Windows CE is handled differently here due its lack of the Win32
21969 ** API LockFileEx.
21970 */
21971 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
21972 #else
21973 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
21974 SHARED_SIZE, 0);
21975 #endif
21976 }
21977 #ifdef SQLITE_WIN32_HAS_ANSI
21978 else{
21979 int lk;
21980 sqlite3_randomness(sizeof(lk), &lk);
21981 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
21982 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
21983 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
21984 }
21985 #endif
21986 if( res == 0 ){
21987 pFile->lastErrno = osGetLastError();
21988 /* No need to log a failure to lock */
21989 }
21990 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
21991 return res;
21992 }
21993
21994 /*
21995 ** Undo a readlock
21996 */
21997 static int winUnlockReadLock(winFile *pFile){
21998 int res;
21999 DWORD lastErrno;
22000 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
22001 if( osIsNT() ){
22002 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
22003 }
22004 #ifdef SQLITE_WIN32_HAS_ANSI
22005 else{
22006 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
22007 }
22008 #endif
22009 if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
22010 pFile->lastErrno = lastErrno;
22011 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
22012 "winUnlockReadLock", pFile->zPath);
22013 }
22014 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
22015 return res;
22016 }
22017
22018 /*
22019 ** Lock the file with the lock specified by parameter locktype - one
22020 ** of the following:
22021 **
22022 ** (1) SHARED_LOCK
22023 ** (2) RESERVED_LOCK
22024 ** (3) PENDING_LOCK
22025 ** (4) EXCLUSIVE_LOCK
22026 **
22027 ** Sometimes when requesting one lock state, additional lock states
22028 ** are inserted in between. The locking might fail on one of the later
22029 ** transitions leaving the lock state different from what it started but
22030 ** still short of its goal. The following chart shows the allowed
22031 ** transitions and the inserted intermediate states:
22032 **
22033 ** UNLOCKED -> SHARED
22034 ** SHARED -> RESERVED
22035 ** SHARED -> (PENDING) -> EXCLUSIVE
22036 ** RESERVED -> (PENDING) -> EXCLUSIVE
22037 ** PENDING -> EXCLUSIVE
22038 **
22039 ** This routine will only increase a lock. The winUnlock() routine
22040 ** erases all locks at once and returns us immediately to locking level 0.
22041 ** It is not possible to lower the locking level one step at a time. You
22042 ** must go straight to locking level 0.
22043 */
22044 static int winLock(sqlite3_file *id, int locktype){
22045 int rc = SQLITE_OK; /* Return code from subroutines */
22046 int res = 1; /* Result of a Windows lock call */
22047 int newLocktype; /* Set pFile->locktype to this value before exiting */
22048 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
22049 winFile *pFile = (winFile*)id;
22050 DWORD lastErrno = NO_ERROR;
22051
22052 assert( id!=0 );
22053 OSTRACE(("LOCK file=%p, oldLock=%d(%d), newLock=%d\n",
22054 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
22055
22056 /* If there is already a lock of this type or more restrictive on the
22057 ** OsFile, do nothing. Don't use the end_lock: exit path, as
22058 ** sqlite3OsEnterMutex() hasn't been called yet.
22059 */
22060 if( pFile->locktype>=locktype ){
22061 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
22062 return SQLITE_OK;
22063 }
22064
22065 /* Do not allow any kind of write-lock on a read-only database
22066 */
22067 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
22068 return SQLITE_IOERR_LOCK;
22069 }
22070
22071 /* Make sure the locking sequence is correct
22072 */
22073 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
22074 assert( locktype!=PENDING_LOCK );
22075 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
22076
22077 /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
22078 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
22079 ** the PENDING_LOCK byte is temporary.
22080 */
22081 newLocktype = pFile->locktype;
22082 if( pFile->locktype==NO_LOCK
22083 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
22084 ){
22085 int cnt = 3;
22086 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
22087 PENDING_BYTE, 0, 1, 0))==0 ){
22088 /* Try 3 times to get the pending lock. This is needed to work
22089 ** around problems caused by indexing and/or anti-virus software on
22090 ** Windows systems.
22091 ** If you are using this code as a model for alternative VFSes, do not
22092 ** copy this retry logic. It is a hack intended for Windows only.
22093 */
22094 lastErrno = osGetLastError();
22095 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
22096 pFile->h, cnt, res));
22097 if( lastErrno==ERROR_INVALID_HANDLE ){
22098 pFile->lastErrno = lastErrno;
22099 rc = SQLITE_IOERR_LOCK;
22100 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
22101 pFile->h, cnt, sqlite3ErrName(rc)));
22102 return rc;
22103 }
22104 if( cnt ) sqlite3_win32_sleep(1);
22105 }
22106 gotPendingLock = res;
22107 if( !res ){
22108 lastErrno = osGetLastError();
22109 }
22110 }
22111
22112 /* Acquire a shared lock
22113 */
22114 if( locktype==SHARED_LOCK && res ){
22115 assert( pFile->locktype==NO_LOCK );
22116 res = winGetReadLock(pFile);
22117 if( res ){
22118 newLocktype = SHARED_LOCK;
22119 }else{
22120 lastErrno = osGetLastError();
22121 }
22122 }
22123
22124 /* Acquire a RESERVED lock
22125 */
22126 if( locktype==RESERVED_LOCK && res ){
22127 assert( pFile->locktype==SHARED_LOCK );
22128 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
22129 if( res ){
22130 newLocktype = RESERVED_LOCK;
22131 }else{
22132 lastErrno = osGetLastError();
22133 }
22134 }
22135
22136 /* Acquire a PENDING lock
22137 */
22138 if( locktype==EXCLUSIVE_LOCK && res ){
22139 newLocktype = PENDING_LOCK;
22140 gotPendingLock = 0;
22141 }
22142
22143 /* Acquire an EXCLUSIVE lock
22144 */
22145 if( locktype==EXCLUSIVE_LOCK && res ){
22146 assert( pFile->locktype>=SHARED_LOCK );
22147 res = winUnlockReadLock(pFile);
22148 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
22149 SHARED_SIZE, 0);
22150 if( res ){
22151 newLocktype = EXCLUSIVE_LOCK;
22152 }else{
22153 lastErrno = osGetLastError();
22154 winGetReadLock(pFile);
22155 }
22156 }
22157
22158 /* If we are holding a PENDING lock that ought to be released, then
22159 ** release it now.
22160 */
22161 if( gotPendingLock && locktype==SHARED_LOCK ){
22162 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
22163 }
22164
22165 /* Update the state of the lock has held in the file descriptor then
22166 ** return the appropriate result code.
22167 */
22168 if( res ){
22169 rc = SQLITE_OK;
22170 }else{
22171 pFile->lastErrno = lastErrno;
22172 rc = SQLITE_BUSY;
22173 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
22174 pFile->h, locktype, newLocktype));
22175 }
22176 pFile->locktype = (u8)newLocktype;
22177 OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
22178 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
22179 return rc;
22180 }
22181
22182 /*
22183 ** This routine checks if there is a RESERVED lock held on the specified
22184 ** file by this or any other process. If such a lock is held, return
22185 ** non-zero, otherwise zero.
22186 */
22187 static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
22188 int res;
22189 winFile *pFile = (winFile*)id;
22190
22191 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
22192 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
22193
22194 assert( id!=0 );
22195 if( pFile->locktype>=RESERVED_LOCK ){
22196 res = 1;
22197 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
22198 }else{
22199 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
22200 if( res ){
22201 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
22202 }
22203 res = !res;
22204 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
22205 }
22206 *pResOut = res;
22207 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
22208 pFile->h, pResOut, *pResOut));
22209 return SQLITE_OK;
22210 }
22211
22212 /*
22213 ** Lower the locking level on file descriptor id to locktype. locktype
22214 ** must be either NO_LOCK or SHARED_LOCK.
22215 **
22216 ** If the locking level of the file descriptor is already at or below
22217 ** the requested locking level, this routine is a no-op.
22218 **
22219 ** It is not possible for this routine to fail if the second argument
22220 ** is NO_LOCK. If the second argument is SHARED_LOCK then this routine
22221 ** might return SQLITE_IOERR;
22222 */
22223 static int winUnlock(sqlite3_file *id, int locktype){
22224 int type;
22225 winFile *pFile = (winFile*)id;
22226 int rc = SQLITE_OK;
22227 assert( pFile!=0 );
22228 assert( locktype<=SHARED_LOCK );
22229 OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
22230 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
22231 type = pFile->locktype;
22232 if( type>=EXCLUSIVE_LOCK ){
22233 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
22234 if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){
22235 /* This should never happen. We should always be able to
22236 ** reacquire the read lock */
22237 rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
22238 "winUnlock", pFile->zPath);
22239 }
22240 }
22241 if( type>=RESERVED_LOCK ){
22242 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
22243 }
22244 if( locktype==NO_LOCK && type>=SHARED_LOCK ){
22245 winUnlockReadLock(pFile);
22246 }
22247 if( type>=PENDING_LOCK ){
22248 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
22249 }
22250 pFile->locktype = (u8)locktype;
22251 OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
22252 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
22253 return rc;
22254 }
22255
22256 /******************************************************************************
22257 ****************************** No-op Locking **********************************
22258 **
22259 ** Of the various locking implementations available, this is by far the
22260 ** simplest: locking is ignored. No attempt is made to lock the database
22261 ** file for reading or writing.
22262 **
22263 ** This locking mode is appropriate for use on read-only databases
22264 ** (ex: databases that are burned into CD-ROM, for example.) It can
22265 ** also be used if the application employs some external mechanism to
22266 ** prevent simultaneous access of the same database by two or more
22267 ** database connections. But there is a serious risk of database
22268 ** corruption if this locking mode is used in situations where multiple
22269 ** database connections are accessing the same database file at the same
22270 ** time and one or more of those connections are writing.
22271 */
22272
22273 static int winNolockLock(sqlite3_file *id, int locktype){
22274 UNUSED_PARAMETER(id);
22275 UNUSED_PARAMETER(locktype);
22276 return SQLITE_OK;
22277 }
22278
22279 static int winNolockCheckReservedLock(sqlite3_file *id, int *pResOut){
22280 UNUSED_PARAMETER(id);
22281 UNUSED_PARAMETER(pResOut);
22282 return SQLITE_OK;
22283 }
22284
22285 static int winNolockUnlock(sqlite3_file *id, int locktype){
22286 UNUSED_PARAMETER(id);
22287 UNUSED_PARAMETER(locktype);
22288 return SQLITE_OK;
22289 }
22290
22291 /******************* End of the no-op lock implementation *********************
22292 ******************************************************************************/
22293
22294 /*
22295 ** If *pArg is initially negative then this is a query. Set *pArg to
22296 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
22297 **
22298 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
22299 */
22300 static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
22301 if( *pArg<0 ){
22302 *pArg = (pFile->ctrlFlags & mask)!=0;
22303 }else if( (*pArg)==0 ){
22304 pFile->ctrlFlags &= ~mask;
22305 }else{
22306 pFile->ctrlFlags |= mask;
22307 }
22308 }
22309
22310 /* Forward references to VFS helper methods used for temporary files */
22311 static int winGetTempname(sqlite3_vfs *, char **);
22312 static int winIsDir(const void *);
22313 static BOOL winIsDriveLetterAndColon(const char *);
22314
22315 /*
22316 ** Control and query of the open file handle.
22317 */
22318 static int winFileControl(sqlite3_file *id, int op, void *pArg){
22319 winFile *pFile = (winFile*)id;
22320 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
22321 switch( op ){
22322 case SQLITE_FCNTL_LOCKSTATE: {
22323 *(int*)pArg = pFile->locktype;
22324 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
22325 return SQLITE_OK;
22326 }
22327 case SQLITE_FCNTL_LAST_ERRNO: {
22328 *(int*)pArg = (int)pFile->lastErrno;
22329 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
22330 return SQLITE_OK;
22331 }
22332 case SQLITE_FCNTL_CHUNK_SIZE: {
22333 pFile->szChunk = *(int *)pArg;
22334 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
22335 return SQLITE_OK;
22336 }
22337 case SQLITE_FCNTL_SIZE_HINT: {
22338 if( pFile->szChunk>0 ){
22339 sqlite3_int64 oldSz;
22340 int rc = winFileSize(id, &oldSz);
22341 if( rc==SQLITE_OK ){
22342 sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
22343 if( newSz>oldSz ){
22344 SimulateIOErrorBenign(1);
22345 rc = winTruncate(id, newSz);
22346 SimulateIOErrorBenign(0);
22347 }
22348 }
22349 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
22350 return rc;
22351 }
22352 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
22353 return SQLITE_OK;
22354 }
22355 case SQLITE_FCNTL_PERSIST_WAL: {
22356 winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
22357 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
22358 return SQLITE_OK;
22359 }
22360 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
22361 winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
22362 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
22363 return SQLITE_OK;
22364 }
22365 case SQLITE_FCNTL_VFSNAME: {
22366 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
22367 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
22368 return SQLITE_OK;
22369 }
22370 case SQLITE_FCNTL_WIN32_AV_RETRY: {
22371 int *a = (int*)pArg;
22372 if( a[0]>0 ){
22373 winIoerrRetry = a[0];
22374 }else{
22375 a[0] = winIoerrRetry;
22376 }
22377 if( a[1]>0 ){
22378 winIoerrRetryDelay = a[1];
22379 }else{
22380 a[1] = winIoerrRetryDelay;
22381 }
22382 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
22383 return SQLITE_OK;
22384 }
22385 case SQLITE_FCNTL_WIN32_GET_HANDLE: {
22386 LPHANDLE phFile = (LPHANDLE)pArg;
22387 *phFile = pFile->h;
22388 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
22389 return SQLITE_OK;
22390 }
22391 #ifdef SQLITE_TEST
22392 case SQLITE_FCNTL_WIN32_SET_HANDLE: {
22393 LPHANDLE phFile = (LPHANDLE)pArg;
22394 HANDLE hOldFile = pFile->h;
22395 pFile->h = *phFile;
22396 *phFile = hOldFile;
22397 OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n",
22398 hOldFile, pFile->h));
22399 return SQLITE_OK;
22400 }
22401 #endif
22402 case SQLITE_FCNTL_TEMPFILENAME: {
22403 char *zTFile = 0;
22404 int rc = winGetTempname(pFile->pVfs, &zTFile);
22405 if( rc==SQLITE_OK ){
22406 *(char**)pArg = zTFile;
22407 }
22408 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
22409 return rc;
22410 }
22411 #if SQLITE_MAX_MMAP_SIZE>0
22412 case SQLITE_FCNTL_MMAP_SIZE: {
22413 i64 newLimit = *(i64*)pArg;
22414 int rc = SQLITE_OK;
22415 if( newLimit>sqlite3GlobalConfig.mxMmap ){
22416 newLimit = sqlite3GlobalConfig.mxMmap;
22417 }
22418 *(i64*)pArg = pFile->mmapSizeMax;
22419 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
22420 pFile->mmapSizeMax = newLimit;
22421 if( pFile->mmapSize>0 ){
22422 winUnmapfile(pFile);
22423 rc = winMapfile(pFile, -1);
22424 }
22425 }
22426 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
22427 return rc;
22428 }
22429 #endif
22430 }
22431 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
22432 return SQLITE_NOTFOUND;
22433 }
22434
22435 /*
22436 ** Return the sector size in bytes of the underlying block device for
22437 ** the specified file. This is almost always 512 bytes, but may be
22438 ** larger for some devices.
22439 **
22440 ** SQLite code assumes this function cannot fail. It also assumes that
22441 ** if two files are created in the same file-system directory (i.e.
22442 ** a database and its journal file) that the sector size will be the
22443 ** same for both.
22444 */
22445 static int winSectorSize(sqlite3_file *id){
22446 (void)id;
22447 return SQLITE_DEFAULT_SECTOR_SIZE;
22448 }
22449
22450 /*
22451 ** Return a vector of device characteristics.
22452 */
22453 static int winDeviceCharacteristics(sqlite3_file *id){
22454 winFile *p = (winFile*)id;
22455 return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
22456 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
22457 }
22458
22459 /*
22460 ** Windows will only let you create file view mappings
22461 ** on allocation size granularity boundaries.
22462 ** During sqlite3_os_init() we do a GetSystemInfo()
22463 ** to get the granularity size.
22464 */
22465 static SYSTEM_INFO winSysInfo;
22466
22467 #ifndef SQLITE_OMIT_WAL
22468
22469 /*
22470 ** Helper functions to obtain and relinquish the global mutex. The
22471 ** global mutex is used to protect the winLockInfo objects used by
22472 ** this file, all of which may be shared by multiple threads.
22473 **
22474 ** Function winShmMutexHeld() is used to assert() that the global mutex
22475 ** is held when required. This function is only used as part of assert()
22476 ** statements. e.g.
22477 **
22478 ** winShmEnterMutex()
22479 ** assert( winShmMutexHeld() );
22480 ** winShmLeaveMutex()
22481 */
22482 static void winShmEnterMutex(void){
22483 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
22484 }
22485 static void winShmLeaveMutex(void){
22486 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
22487 }
22488 #ifndef NDEBUG
22489 static int winShmMutexHeld(void) {
22490 return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
22491 }
22492 #endif
22493
22494 /*
22495 ** Object used to represent a single file opened and mmapped to provide
22496 ** shared memory. When multiple threads all reference the same
22497 ** log-summary, each thread has its own winFile object, but they all
22498 ** point to a single instance of this object. In other words, each
22499 ** log-summary is opened only once per process.
22500 **
22501 ** winShmMutexHeld() must be true when creating or destroying
22502 ** this object or while reading or writing the following fields:
22503 **
22504 ** nRef
22505 ** pNext
22506 **
22507 ** The following fields are read-only after the object is created:
22508 **
22509 ** fid
22510 ** zFilename
22511 **
22512 ** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
22513 ** winShmMutexHeld() is true when reading or writing any other field
22514 ** in this structure.
22515 **
22516 */
22517 struct winShmNode {
22518 sqlite3_mutex *mutex; /* Mutex to access this object */
22519 char *zFilename; /* Name of the file */
22520 winFile hFile; /* File handle from winOpen */
22521
22522 int szRegion; /* Size of shared-memory regions */
22523 int nRegion; /* Size of array apRegion */
22524 struct ShmRegion {
22525 HANDLE hMap; /* File handle from CreateFileMapping */
22526 void *pMap;
22527 } *aRegion;
22528 DWORD lastErrno; /* The Windows errno from the last I/O error */
22529
22530 int nRef; /* Number of winShm objects pointing to this */
22531 winShm *pFirst; /* All winShm objects pointing to this */
22532 winShmNode *pNext; /* Next in list of all winShmNode objects */
22533 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
22534 u8 nextShmId; /* Next available winShm.id value */
22535 #endif
22536 };
22537
22538 /*
22539 ** A global array of all winShmNode objects.
22540 **
22541 ** The winShmMutexHeld() must be true while reading or writing this list.
22542 */
22543 static winShmNode *winShmNodeList = 0;
22544
22545 /*
22546 ** Structure used internally by this VFS to record the state of an
22547 ** open shared memory connection.
22548 **
22549 ** The following fields are initialized when this object is created and
22550 ** are read-only thereafter:
22551 **
22552 ** winShm.pShmNode
22553 ** winShm.id
22554 **
22555 ** All other fields are read/write. The winShm.pShmNode->mutex must be held
22556 ** while accessing any read/write fields.
22557 */
22558 struct winShm {
22559 winShmNode *pShmNode; /* The underlying winShmNode object */
22560 winShm *pNext; /* Next winShm with the same winShmNode */
22561 u8 hasMutex; /* True if holding the winShmNode mutex */
22562 u16 sharedMask; /* Mask of shared locks held */
22563 u16 exclMask; /* Mask of exclusive locks held */
22564 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
22565 u8 id; /* Id of this connection with its winShmNode */
22566 #endif
22567 };
22568
22569 /*
22570 ** Constants used for locking
22571 */
22572 #define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
22573 #define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
22574
22575 /*
22576 ** Apply advisory locks for all n bytes beginning at ofst.
22577 */
22578 #define WINSHM_UNLCK 1
22579 #define WINSHM_RDLCK 2
22580 #define WINSHM_WRLCK 3
22581 static int winShmSystemLock(
22582 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
22583 int lockType, /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */
22584 int ofst, /* Offset to first byte to be locked/unlocked */
22585 int nByte /* Number of bytes to lock or unlock */
22586 ){
22587 int rc = 0; /* Result code form Lock/UnlockFileEx() */
22588
22589 /* Access to the winShmNode object is serialized by the caller */
22590 assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
22591
22592 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
22593 pFile->hFile.h, lockType, ofst, nByte));
22594
22595 /* Release/Acquire the system-level lock */
22596 if( lockType==WINSHM_UNLCK ){
22597 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
22598 }else{
22599 /* Initialize the locking parameters */
22600 DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
22601 if( lockType == WINSHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
22602 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
22603 }
22604
22605 if( rc!= 0 ){
22606 rc = SQLITE_OK;
22607 }else{
22608 pFile->lastErrno = osGetLastError();
22609 rc = SQLITE_BUSY;
22610 }
22611
22612 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
22613 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
22614 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
22615
22616 return rc;
22617 }
22618
22619 /* Forward references to VFS methods */
22620 static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
22621 static int winDelete(sqlite3_vfs *,const char*,int);
22622
22623 /*
22624 ** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
22625 **
22626 ** This is not a VFS shared-memory method; it is a utility function called
22627 ** by VFS shared-memory methods.
22628 */
22629 static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
22630 winShmNode **pp;
22631 winShmNode *p;
22632 assert( winShmMutexHeld() );
22633 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
22634 osGetCurrentProcessId(), deleteFlag));
22635 pp = &winShmNodeList;
22636 while( (p = *pp)!=0 ){
22637 if( p->nRef==0 ){
22638 int i;
22639 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
22640 for(i=0; i<p->nRegion; i++){
22641 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
22642 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
22643 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
22644 UNUSED_VARIABLE_VALUE(bRc);
22645 bRc = osCloseHandle(p->aRegion[i].hMap);
22646 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
22647 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
22648 UNUSED_VARIABLE_VALUE(bRc);
22649 }
22650 if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
22651 SimulateIOErrorBenign(1);
22652 winClose((sqlite3_file *)&p->hFile);
22653 SimulateIOErrorBenign(0);
22654 }
22655 if( deleteFlag ){
22656 SimulateIOErrorBenign(1);
22657 sqlite3BeginBenignMalloc();
22658 winDelete(pVfs, p->zFilename, 0);
22659 sqlite3EndBenignMalloc();
22660 SimulateIOErrorBenign(0);
22661 }
22662 *pp = p->pNext;
22663 sqlite3_free(p->aRegion);
22664 sqlite3_free(p);
22665 }else{
22666 pp = &p->pNext;
22667 }
22668 }
22669 }
22670
22671 /*
22672 ** Open the shared-memory area associated with database file pDbFd.
22673 **
22674 ** When opening a new shared-memory file, if no other instances of that
22675 ** file are currently open, in this process or in other processes, then
22676 ** the file must be truncated to zero length or have its header cleared.
22677 */
22678 static int winOpenSharedMemory(winFile *pDbFd){
22679 struct winShm *p; /* The connection to be opened */
22680 struct winShmNode *pShmNode = 0; /* The underlying mmapped file */
22681 int rc; /* Result code */
22682 struct winShmNode *pNew; /* Newly allocated winShmNode */
22683 int nName; /* Size of zName in bytes */
22684
22685 assert( pDbFd->pShm==0 ); /* Not previously opened */
22686
22687 /* Allocate space for the new sqlite3_shm object. Also speculatively
22688 ** allocate space for a new winShmNode and filename.
22689 */
22690 p = sqlite3MallocZero( sizeof(*p) );
22691 if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT;
22692 nName = sqlite3Strlen30(pDbFd->zPath);
22693 pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
22694 if( pNew==0 ){
22695 sqlite3_free(p);
22696 return SQLITE_IOERR_NOMEM_BKPT;
22697 }
22698 pNew->zFilename = (char*)&pNew[1];
22699 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
22700 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
22701
22702 /* Look to see if there is an existing winShmNode that can be used.
22703 ** If no matching winShmNode currently exists, create a new one.
22704 */
22705 winShmEnterMutex();
22706 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
22707 /* TBD need to come up with better match here. Perhaps
22708 ** use FILE_ID_BOTH_DIR_INFO Structure.
22709 */
22710 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
22711 }
22712 if( pShmNode ){
22713 sqlite3_free(pNew);
22714 }else{
22715 pShmNode = pNew;
22716 pNew = 0;
22717 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
22718 pShmNode->pNext = winShmNodeList;
22719 winShmNodeList = pShmNode;
22720
22721 if( sqlite3GlobalConfig.bCoreMutex ){
22722 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
22723 if( pShmNode->mutex==0 ){
22724 rc = SQLITE_IOERR_NOMEM_BKPT;
22725 goto shm_open_err;
22726 }
22727 }
22728
22729 rc = winOpen(pDbFd->pVfs,
22730 pShmNode->zFilename, /* Name of the file (UTF-8) */
22731 (sqlite3_file*)&pShmNode->hFile, /* File handle here */
22732 SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
22733 0);
22734 if( SQLITE_OK!=rc ){
22735 goto shm_open_err;
22736 }
22737
22738 /* Check to see if another process is holding the dead-man switch.
22739 ** If not, truncate the file to zero length.
22740 */
22741 if( winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
22742 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
22743 if( rc!=SQLITE_OK ){
22744 rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
22745 "winOpenShm", pDbFd->zPath);
22746 }
22747 }
22748 if( rc==SQLITE_OK ){
22749 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
22750 rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1);
22751 }
22752 if( rc ) goto shm_open_err;
22753 }
22754
22755 /* Make the new connection a child of the winShmNode */
22756 p->pShmNode = pShmNode;
22757 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
22758 p->id = pShmNode->nextShmId++;
22759 #endif
22760 pShmNode->nRef++;
22761 pDbFd->pShm = p;
22762 winShmLeaveMutex();
22763
22764 /* The reference count on pShmNode has already been incremented under
22765 ** the cover of the winShmEnterMutex() mutex and the pointer from the
22766 ** new (struct winShm) object to the pShmNode has been set. All that is
22767 ** left to do is to link the new object into the linked list starting
22768 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
22769 ** mutex.
22770 */
22771 sqlite3_mutex_enter(pShmNode->mutex);
22772 p->pNext = pShmNode->pFirst;
22773 pShmNode->pFirst = p;
22774 sqlite3_mutex_leave(pShmNode->mutex);
22775 return SQLITE_OK;
22776
22777 /* Jump here on any error */
22778 shm_open_err:
22779 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
22780 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
22781 sqlite3_free(p);
22782 sqlite3_free(pNew);
22783 winShmLeaveMutex();
22784 return rc;
22785 }
22786
22787 /*
22788 ** Close a connection to shared-memory. Delete the underlying
22789 ** storage if deleteFlag is true.
22790 */
22791 static int winShmUnmap(
22792 sqlite3_file *fd, /* Database holding shared memory */
22793 int deleteFlag /* Delete after closing if true */
22794 ){
22795 winFile *pDbFd; /* Database holding shared-memory */
22796 winShm *p; /* The connection to be closed */
22797 winShmNode *pShmNode; /* The underlying shared-memory file */
22798 winShm **pp; /* For looping over sibling connections */
22799
22800 pDbFd = (winFile*)fd;
22801 p = pDbFd->pShm;
22802 if( p==0 ) return SQLITE_OK;
22803 pShmNode = p->pShmNode;
22804
22805 /* Remove connection p from the set of connections associated
22806 ** with pShmNode */
22807 sqlite3_mutex_enter(pShmNode->mutex);
22808 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
22809 *pp = p->pNext;
22810
22811 /* Free the connection p */
22812 sqlite3_free(p);
22813 pDbFd->pShm = 0;
22814 sqlite3_mutex_leave(pShmNode->mutex);
22815
22816 /* If pShmNode->nRef has reached 0, then close the underlying
22817 ** shared-memory file, too */
22818 winShmEnterMutex();
22819 assert( pShmNode->nRef>0 );
22820 pShmNode->nRef--;
22821 if( pShmNode->nRef==0 ){
22822 winShmPurge(pDbFd->pVfs, deleteFlag);
22823 }
22824 winShmLeaveMutex();
22825
22826 return SQLITE_OK;
22827 }
22828
22829 /*
22830 ** Change the lock state for a shared-memory segment.
22831 */
22832 static int winShmLock(
22833 sqlite3_file *fd, /* Database file holding the shared memory */
22834 int ofst, /* First lock to acquire or release */
22835 int n, /* Number of locks to acquire or release */
22836 int flags /* What to do with the lock */
22837 ){
22838 winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */
22839 winShm *p = pDbFd->pShm; /* The shared memory being locked */
22840 winShm *pX; /* For looping over all siblings */
22841 winShmNode *pShmNode = p->pShmNode;
22842 int rc = SQLITE_OK; /* Result code */
22843 u16 mask; /* Mask of locks to take or release */
22844
22845 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
22846 assert( n>=1 );
22847 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
22848 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
22849 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
22850 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
22851 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
22852
22853 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
22854 assert( n>1 || mask==(1<<ofst) );
22855 sqlite3_mutex_enter(pShmNode->mutex);
22856 if( flags & SQLITE_SHM_UNLOCK ){
22857 u16 allMask = 0; /* Mask of locks held by siblings */
22858
22859 /* See if any siblings hold this same lock */
22860 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
22861 if( pX==p ) continue;
22862 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
22863 allMask |= pX->sharedMask;
22864 }
22865
22866 /* Unlock the system-level locks */
22867 if( (mask & allMask)==0 ){
22868 rc = winShmSystemLock(pShmNode, WINSHM_UNLCK, ofst+WIN_SHM_BASE, n);
22869 }else{
22870 rc = SQLITE_OK;
22871 }
22872
22873 /* Undo the local locks */
22874 if( rc==SQLITE_OK ){
22875 p->exclMask &= ~mask;
22876 p->sharedMask &= ~mask;
22877 }
22878 }else if( flags & SQLITE_SHM_SHARED ){
22879 u16 allShared = 0; /* Union of locks held by connections other than "p" */
22880
22881 /* Find out which shared locks are already held by sibling connections.
22882 ** If any sibling already holds an exclusive lock, go ahead and return
22883 ** SQLITE_BUSY.
22884 */
22885 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
22886 if( (pX->exclMask & mask)!=0 ){
22887 rc = SQLITE_BUSY;
22888 break;
22889 }
22890 allShared |= pX->sharedMask;
22891 }
22892
22893 /* Get shared locks at the system level, if necessary */
22894 if( rc==SQLITE_OK ){
22895 if( (allShared & mask)==0 ){
22896 rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, ofst+WIN_SHM_BASE, n);
22897 }else{
22898 rc = SQLITE_OK;
22899 }
22900 }
22901
22902 /* Get the local shared locks */
22903 if( rc==SQLITE_OK ){
22904 p->sharedMask |= mask;
22905 }
22906 }else{
22907 /* Make sure no sibling connections hold locks that will block this
22908 ** lock. If any do, return SQLITE_BUSY right away.
22909 */
22910 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
22911 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
22912 rc = SQLITE_BUSY;
22913 break;
22914 }
22915 }
22916
22917 /* Get the exclusive locks at the system level. Then if successful
22918 ** also mark the local connection as being locked.
22919 */
22920 if( rc==SQLITE_OK ){
22921 rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, ofst+WIN_SHM_BASE, n);
22922 if( rc==SQLITE_OK ){
22923 assert( (p->sharedMask & mask)==0 );
22924 p->exclMask |= mask;
22925 }
22926 }
22927 }
22928 sqlite3_mutex_leave(pShmNode->mutex);
22929 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
22930 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
22931 sqlite3ErrName(rc)));
22932 return rc;
22933 }
22934
22935 /*
22936 ** Implement a memory barrier or memory fence on shared memory.
22937 **
22938 ** All loads and stores begun before the barrier must complete before
22939 ** any load or store begun after the barrier.
22940 */
22941 static void winShmBarrier(
22942 sqlite3_file *fd /* Database holding the shared memory */
22943 ){
22944 UNUSED_PARAMETER(fd);
22945 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
22946 winShmEnterMutex(); /* Also mutex, for redundancy */
22947 winShmLeaveMutex();
22948 }
22949
22950 /*
22951 ** This function is called to obtain a pointer to region iRegion of the
22952 ** shared-memory associated with the database file fd. Shared-memory regions
22953 ** are numbered starting from zero. Each shared-memory region is szRegion
22954 ** bytes in size.
22955 **
22956 ** If an error occurs, an error code is returned and *pp is set to NULL.
22957 **
22958 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
22959 ** region has not been allocated (by any client, including one running in a
22960 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
22961 ** isWrite is non-zero and the requested shared-memory region has not yet
22962 ** been allocated, it is allocated by this function.
22963 **
22964 ** If the shared-memory region has already been allocated or is allocated by
22965 ** this call as described above, then it is mapped into this processes
22966 ** address space (if it is not already), *pp is set to point to the mapped
22967 ** memory and SQLITE_OK returned.
22968 */
22969 static int winShmMap(
22970 sqlite3_file *fd, /* Handle open on database file */
22971 int iRegion, /* Region to retrieve */
22972 int szRegion, /* Size of regions */
22973 int isWrite, /* True to extend file if necessary */
22974 void volatile **pp /* OUT: Mapped memory */
22975 ){
22976 winFile *pDbFd = (winFile*)fd;
22977 winShm *pShm = pDbFd->pShm;
22978 winShmNode *pShmNode;
22979 int rc = SQLITE_OK;
22980
22981 if( !pShm ){
22982 rc = winOpenSharedMemory(pDbFd);
22983 if( rc!=SQLITE_OK ) return rc;
22984 pShm = pDbFd->pShm;
22985 }
22986 pShmNode = pShm->pShmNode;
22987
22988 sqlite3_mutex_enter(pShmNode->mutex);
22989 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
22990
22991 if( pShmNode->nRegion<=iRegion ){
22992 struct ShmRegion *apNew; /* New aRegion[] array */
22993 int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
22994 sqlite3_int64 sz; /* Current size of wal-index file */
22995
22996 pShmNode->szRegion = szRegion;
22997
22998 /* The requested region is not mapped into this processes address space.
22999 ** Check to see if it has been allocated (i.e. if the wal-index file is
23000 ** large enough to contain the requested region).
23001 */
23002 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
23003 if( rc!=SQLITE_OK ){
23004 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
23005 "winShmMap1", pDbFd->zPath);
23006 goto shmpage_out;
23007 }
23008
23009 if( sz<nByte ){
23010 /* The requested memory region does not exist. If isWrite is set to
23011 ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
23012 **
23013 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
23014 ** the requested memory region.
23015 */
23016 if( !isWrite ) goto shmpage_out;
23017 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
23018 if( rc!=SQLITE_OK ){
23019 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
23020 "winShmMap2", pDbFd->zPath);
23021 goto shmpage_out;
23022 }
23023 }
23024
23025 /* Map the requested memory region into this processes address space. */
23026 apNew = (struct ShmRegion *)sqlite3_realloc64(
23027 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
23028 );
23029 if( !apNew ){
23030 rc = SQLITE_IOERR_NOMEM_BKPT;
23031 goto shmpage_out;
23032 }
23033 pShmNode->aRegion = apNew;
23034
23035 while( pShmNode->nRegion<=iRegion ){
23036 HANDLE hMap = NULL; /* file-mapping handle */
23037 void *pMap = 0; /* Mapped memory region */
23038
23039 #if SQLITE_OS_WINRT
23040 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
23041 NULL, PAGE_READWRITE, nByte, NULL
23042 );
23043 #elif defined(SQLITE_WIN32_HAS_WIDE)
23044 hMap = osCreateFileMappingW(pShmNode->hFile.h,
23045 NULL, PAGE_READWRITE, 0, nByte, NULL
23046 );
23047 #elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
23048 hMap = osCreateFileMappingA(pShmNode->hFile.h,
23049 NULL, PAGE_READWRITE, 0, nByte, NULL
23050 );
23051 #endif
23052 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
23053 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
23054 hMap ? "ok" : "failed"));
23055 if( hMap ){
23056 int iOffset = pShmNode->nRegion*szRegion;
23057 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
23058 #if SQLITE_OS_WINRT
23059 pMap = osMapViewOfFileFromApp(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
23060 iOffset - iOffsetShift, szRegion + iOffsetShift
23061 );
23062 #else
23063 pMap = osMapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
23064 0, iOffset - iOffsetShift, szRegion + iOffsetShift
23065 );
23066 #endif
23067 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
23068 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
23069 szRegion, pMap ? "ok" : "failed"));
23070 }
23071 if( !pMap ){
23072 pShmNode->lastErrno = osGetLastError();
23073 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
23074 "winShmMap3", pDbFd->zPath);
23075 if( hMap ) osCloseHandle(hMap);
23076 goto shmpage_out;
23077 }
23078
23079 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
23080 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
23081 pShmNode->nRegion++;
23082 }
23083 }
23084
23085 shmpage_out:
23086 if( pShmNode->nRegion>iRegion ){
23087 int iOffset = iRegion*szRegion;
23088 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
23089 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
23090 *pp = (void *)&p[iOffsetShift];
23091 }else{
23092 *pp = 0;
23093 }
23094 sqlite3_mutex_leave(pShmNode->mutex);
23095 return rc;
23096 }
23097
23098 #else
23099 # define winShmMap 0
23100 # define winShmLock 0
23101 # define winShmBarrier 0
23102 # define winShmUnmap 0
23103 #endif /* #ifndef SQLITE_OMIT_WAL */
23104
23105 /*
23106 ** Cleans up the mapped region of the specified file, if any.
23107 */
23108 #if SQLITE_MAX_MMAP_SIZE>0
23109 static int winUnmapfile(winFile *pFile){
23110 assert( pFile!=0 );
23111 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
23112 "mmapSize=%lld, mmapSizeActual=%lld, mmapSizeMax=%lld\n",
23113 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
23114 pFile->mmapSize, pFile->mmapSizeActual, pFile->mmapSizeMax));
23115 if( pFile->pMapRegion ){
23116 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
23117 pFile->lastErrno = osGetLastError();
23118 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
23119 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
23120 pFile->pMapRegion));
23121 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
23122 "winUnmapfile1", pFile->zPath);
23123 }
23124 pFile->pMapRegion = 0;
23125 pFile->mmapSize = 0;
23126 pFile->mmapSizeActual = 0;
23127 }
23128 if( pFile->hMap!=NULL ){
23129 if( !osCloseHandle(pFile->hMap) ){
23130 pFile->lastErrno = osGetLastError();
23131 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
23132 osGetCurrentProcessId(), pFile, pFile->hMap));
23133 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
23134 "winUnmapfile2", pFile->zPath);
23135 }
23136 pFile->hMap = NULL;
23137 }
23138 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
23139 osGetCurrentProcessId(), pFile));
23140 return SQLITE_OK;
23141 }
23142
23143 /*
23144 ** Memory map or remap the file opened by file-descriptor pFd (if the file
23145 ** is already mapped, the existing mapping is replaced by the new). Or, if
23146 ** there already exists a mapping for this file, and there are still
23147 ** outstanding xFetch() references to it, this function is a no-op.
23148 **
23149 ** If parameter nByte is non-negative, then it is the requested size of
23150 ** the mapping to create. Otherwise, if nByte is less than zero, then the
23151 ** requested size is the size of the file on disk. The actual size of the
23152 ** created mapping is either the requested size or the value configured
23153 ** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
23154 **
23155 ** SQLITE_OK is returned if no error occurs (even if the mapping is not
23156 ** recreated as a result of outstanding references) or an SQLite error
23157 ** code otherwise.
23158 */
23159 static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
23160 sqlite3_int64 nMap = nByte;
23161 int rc;
23162
23163 assert( nMap>=0 || pFd->nFetchOut==0 );
23164 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
23165 osGetCurrentProcessId(), pFd, nByte));
23166
23167 if( pFd->nFetchOut>0 ) return SQLITE_OK;
23168
23169 if( nMap<0 ){
23170 rc = winFileSize((sqlite3_file*)pFd, &nMap);
23171 if( rc ){
23172 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
23173 osGetCurrentProcessId(), pFd));
23174 return SQLITE_IOERR_FSTAT;
23175 }
23176 }
23177 if( nMap>pFd->mmapSizeMax ){
23178 nMap = pFd->mmapSizeMax;
23179 }
23180 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
23181
23182 if( nMap==0 && pFd->mmapSize>0 ){
23183 winUnmapfile(pFd);
23184 }
23185 if( nMap!=pFd->mmapSize ){
23186 void *pNew = 0;
23187 DWORD protect = PAGE_READONLY;
23188 DWORD flags = FILE_MAP_READ;
23189
23190 winUnmapfile(pFd);
23191 #ifdef SQLITE_MMAP_READWRITE
23192 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
23193 protect = PAGE_READWRITE;
23194 flags |= FILE_MAP_WRITE;
23195 }
23196 #endif
23197 #if SQLITE_OS_WINRT
23198 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
23199 #elif defined(SQLITE_WIN32_HAS_WIDE)
23200 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
23201 (DWORD)((nMap>>32) & 0xffffffff),
23202 (DWORD)(nMap & 0xffffffff), NULL);
23203 #elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
23204 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
23205 (DWORD)((nMap>>32) & 0xffffffff),
23206 (DWORD)(nMap & 0xffffffff), NULL);
23207 #endif
23208 if( pFd->hMap==NULL ){
23209 pFd->lastErrno = osGetLastError();
23210 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
23211 "winMapfile1", pFd->zPath);
23212 /* Log the error, but continue normal operation using xRead/xWrite */
23213 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
23214 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
23215 return SQLITE_OK;
23216 }
23217 assert( (nMap % winSysInfo.dwPageSize)==0 );
23218 assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff );
23219 #if SQLITE_OS_WINRT
23220 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
23221 #else
23222 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
23223 #endif
23224 if( pNew==NULL ){
23225 osCloseHandle(pFd->hMap);
23226 pFd->hMap = NULL;
23227 pFd->lastErrno = osGetLastError();
23228 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
23229 "winMapfile2", pFd->zPath);
23230 /* Log the error, but continue normal operation using xRead/xWrite */
23231 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
23232 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
23233 return SQLITE_OK;
23234 }
23235 pFd->pMapRegion = pNew;
23236 pFd->mmapSize = nMap;
23237 pFd->mmapSizeActual = nMap;
23238 }
23239
23240 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
23241 osGetCurrentProcessId(), pFd));
23242 return SQLITE_OK;
23243 }
23244 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
23245
23246 /*
23247 ** If possible, return a pointer to a mapping of file fd starting at offset
23248 ** iOff. The mapping must be valid for at least nAmt bytes.
23249 **
23250 ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
23251 ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
23252 ** Finally, if an error does occur, return an SQLite error code. The final
23253 ** value of *pp is undefined in this case.
23254 **
23255 ** If this function does return a pointer, the caller must eventually
23256 ** release the reference by calling winUnfetch().
23257 */
23258 static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
23259 #if SQLITE_MAX_MMAP_SIZE>0
23260 winFile *pFd = (winFile*)fd; /* The underlying database file */
23261 #endif
23262 *pp = 0;
23263
23264 OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n",
23265 osGetCurrentProcessId(), fd, iOff, nAmt, pp));
23266
23267 #if SQLITE_MAX_MMAP_SIZE>0
23268 if( pFd->mmapSizeMax>0 ){
23269 if( pFd->pMapRegion==0 ){
23270 int rc = winMapfile(pFd, -1);
23271 if( rc!=SQLITE_OK ){
23272 OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n",
23273 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
23274 return rc;
23275 }
23276 }
23277 if( pFd->mmapSize >= iOff+nAmt ){
23278 *pp = &((u8 *)pFd->pMapRegion)[iOff];
23279 pFd->nFetchOut++;
23280 }
23281 }
23282 #endif
23283
23284 OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n",
23285 osGetCurrentProcessId(), fd, pp, *pp));
23286 return SQLITE_OK;
23287 }
23288
23289 /*
23290 ** If the third argument is non-NULL, then this function releases a
23291 ** reference obtained by an earlier call to winFetch(). The second
23292 ** argument passed to this function must be the same as the corresponding
23293 ** argument that was passed to the winFetch() invocation.
23294 **
23295 ** Or, if the third argument is NULL, then this function is being called
23296 ** to inform the VFS layer that, according to POSIX, any existing mapping
23297 ** may now be invalid and should be unmapped.
23298 */
23299 static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
23300 #if SQLITE_MAX_MMAP_SIZE>0
23301 winFile *pFd = (winFile*)fd; /* The underlying database file */
23302
23303 /* If p==0 (unmap the entire file) then there must be no outstanding
23304 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
23305 ** then there must be at least one outstanding. */
23306 assert( (p==0)==(pFd->nFetchOut==0) );
23307
23308 /* If p!=0, it must match the iOff value. */
23309 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
23310
23311 OSTRACE(("UNFETCH pid=%lu, pFile=%p, offset=%lld, p=%p\n",
23312 osGetCurrentProcessId(), pFd, iOff, p));
23313
23314 if( p ){
23315 pFd->nFetchOut--;
23316 }else{
23317 /* FIXME: If Windows truly always prevents truncating or deleting a
23318 ** file while a mapping is held, then the following winUnmapfile() call
23319 ** is unnecessary can be omitted - potentially improving
23320 ** performance. */
23321 winUnmapfile(pFd);
23322 }
23323
23324 assert( pFd->nFetchOut>=0 );
23325 #endif
23326
23327 OSTRACE(("UNFETCH pid=%lu, pFile=%p, rc=SQLITE_OK\n",
23328 osGetCurrentProcessId(), fd));
23329 return SQLITE_OK;
23330 }
23331
23332 /*
23333 ** Here ends the implementation of all sqlite3_file methods.
23334 **
23335 ********************** End sqlite3_file Methods *******************************
23336 ******************************************************************************/
23337
23338 /*
23339 ** This vector defines all the methods that can operate on an
23340 ** sqlite3_file for win32.
23341 */
23342 static const sqlite3_io_methods winIoMethod = {
23343 3, /* iVersion */
23344 winClose, /* xClose */
23345 winRead, /* xRead */
23346 winWrite, /* xWrite */
23347 winTruncate, /* xTruncate */
23348 winSync, /* xSync */
23349 winFileSize, /* xFileSize */
23350 winLock, /* xLock */
23351 winUnlock, /* xUnlock */
23352 winCheckReservedLock, /* xCheckReservedLock */
23353 winFileControl, /* xFileControl */
23354 winSectorSize, /* xSectorSize */
23355 winDeviceCharacteristics, /* xDeviceCharacteristics */
23356 winShmMap, /* xShmMap */
23357 winShmLock, /* xShmLock */
23358 winShmBarrier, /* xShmBarrier */
23359 winShmUnmap, /* xShmUnmap */
23360 winFetch, /* xFetch */
23361 winUnfetch /* xUnfetch */
23362 };
23363
23364 /*
23365 ** This vector defines all the methods that can operate on an
23366 ** sqlite3_file for win32 without performing any locking.
23367 */
23368 static const sqlite3_io_methods winIoNolockMethod = {
23369 3, /* iVersion */
23370 winClose, /* xClose */
23371 winRead, /* xRead */
23372 winWrite, /* xWrite */
23373 winTruncate, /* xTruncate */
23374 winSync, /* xSync */
23375 winFileSize, /* xFileSize */
23376 winNolockLock, /* xLock */
23377 winNolockUnlock, /* xUnlock */
23378 winNolockCheckReservedLock, /* xCheckReservedLock */
23379 winFileControl, /* xFileControl */
23380 winSectorSize, /* xSectorSize */
23381 winDeviceCharacteristics, /* xDeviceCharacteristics */
23382 winShmMap, /* xShmMap */
23383 winShmLock, /* xShmLock */
23384 winShmBarrier, /* xShmBarrier */
23385 winShmUnmap, /* xShmUnmap */
23386 winFetch, /* xFetch */
23387 winUnfetch /* xUnfetch */
23388 };
23389
23390 static winVfsAppData winAppData = {
23391 &winIoMethod, /* pMethod */
23392 0, /* pAppData */
23393 0 /* bNoLock */
23394 };
23395
23396 static winVfsAppData winNolockAppData = {
23397 &winIoNolockMethod, /* pMethod */
23398 0, /* pAppData */
23399 1 /* bNoLock */
23400 };
23401
23402 /****************************************************************************
23403 **************************** sqlite3_vfs methods ****************************
23404 **
23405 ** This division contains the implementation of methods on the
23406 ** sqlite3_vfs object.
23407 */
23408
23409 #if defined(__CYGWIN__)
23410 /*
23411 ** Convert a filename from whatever the underlying operating system
23412 ** supports for filenames into UTF-8. Space to hold the result is
23413 ** obtained from malloc and must be freed by the calling function.
23414 */
23415 static char *winConvertToUtf8Filename(const void *zFilename){
23416 char *zConverted = 0;
23417 if( osIsNT() ){
23418 zConverted = winUnicodeToUtf8(zFilename);
23419 }
23420 #ifdef SQLITE_WIN32_HAS_ANSI
23421 else{
23422 zConverted = winMbcsToUtf8(zFilename, osAreFileApisANSI());
23423 }
23424 #endif
23425 /* caller will handle out of memory */
23426 return zConverted;
23427 }
23428 #endif
23429
23430 /*
23431 ** Convert a UTF-8 filename into whatever form the underlying
23432 ** operating system wants filenames in. Space to hold the result
23433 ** is obtained from malloc and must be freed by the calling
23434 ** function.
23435 */
23436 static void *winConvertFromUtf8Filename(const char *zFilename){
23437 void *zConverted = 0;
23438 if( osIsNT() ){
23439 zConverted = winUtf8ToUnicode(zFilename);
23440 }
23441 #ifdef SQLITE_WIN32_HAS_ANSI
23442 else{
23443 zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
23444 }
23445 #endif
23446 /* caller will handle out of memory */
23447 return zConverted;
23448 }
23449
23450 /*
23451 ** This function returns non-zero if the specified UTF-8 string buffer
23452 ** ends with a directory separator character or one was successfully
23453 ** added to it.
23454 */
23455 static int winMakeEndInDirSep(int nBuf, char *zBuf){
23456 if( zBuf ){
23457 int nLen = sqlite3Strlen30(zBuf);
23458 if( nLen>0 ){
23459 if( winIsDirSep(zBuf[nLen-1]) ){
23460 return 1;
23461 }else if( nLen+1<nBuf ){
23462 zBuf[nLen] = winGetDirSep();
23463 zBuf[nLen+1] = '\0';
23464 return 1;
23465 }
23466 }
23467 }
23468 return 0;
23469 }
23470
23471 /*
23472 ** Create a temporary file name and store the resulting pointer into pzBuf.
23473 ** The pointer returned in pzBuf must be freed via sqlite3_free().
23474 */
23475 static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
23476 static char zChars[] =
23477 "abcdefghijklmnopqrstuvwxyz"
23478 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
23479 "0123456789";
23480 size_t i, j;
23481 int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX);
23482 int nMax, nBuf, nDir, nLen;
23483 char *zBuf;
23484
23485 /* It's odd to simulate an io-error here, but really this is just
23486 ** using the io-error infrastructure to test that SQLite handles this
23487 ** function failing.
23488 */
23489 SimulateIOError( return SQLITE_IOERR );
23490
23491 /* Allocate a temporary buffer to store the fully qualified file
23492 ** name for the temporary file. If this fails, we cannot continue.
23493 */
23494 nMax = pVfs->mxPathname; nBuf = nMax + 2;
23495 zBuf = sqlite3MallocZero( nBuf );
23496 if( !zBuf ){
23497 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
23498 return SQLITE_IOERR_NOMEM_BKPT;
23499 }
23500
23501 /* Figure out the effective temporary directory. First, check if one
23502 ** has been explicitly set by the application; otherwise, use the one
23503 ** configured by the operating system.
23504 */
23505 nDir = nMax - (nPre + 15);
23506 assert( nDir>0 );
23507 if( sqlite3_temp_directory ){
23508 int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
23509 if( nDirLen>0 ){
23510 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
23511 nDirLen++;
23512 }
23513 if( nDirLen>nDir ){
23514 sqlite3_free(zBuf);
23515 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
23516 return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0);
23517 }
23518 sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
23519 }
23520 }
23521 #if defined(__CYGWIN__)
23522 else{
23523 static const char *azDirs[] = {
23524 0, /* getenv("SQLITE_TMPDIR") */
23525 0, /* getenv("TMPDIR") */
23526 0, /* getenv("TMP") */
23527 0, /* getenv("TEMP") */
23528 0, /* getenv("USERPROFILE") */
23529 "/var/tmp",
23530 "/usr/tmp",
23531 "/tmp",
23532 ".",
23533 0 /* List terminator */
23534 };
23535 unsigned int i;
23536 const char *zDir = 0;
23537
23538 if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
23539 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
23540 if( !azDirs[2] ) azDirs[2] = getenv("TMP");
23541 if( !azDirs[3] ) azDirs[3] = getenv("TEMP");
23542 if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE");
23543 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
23544 void *zConverted;
23545 if( zDir==0 ) continue;
23546 /* If the path starts with a drive letter followed by the colon
23547 ** character, assume it is already a native Win32 path; otherwise,
23548 ** it must be converted to a native Win32 path via the Cygwin API
23549 ** prior to using it.
23550 */
23551 if( winIsDriveLetterAndColon(zDir) ){
23552 zConverted = winConvertFromUtf8Filename(zDir);
23553 if( !zConverted ){
23554 sqlite3_free(zBuf);
23555 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
23556 return SQLITE_IOERR_NOMEM_BKPT;
23557 }
23558 if( winIsDir(zConverted) ){
23559 sqlite3_snprintf(nMax, zBuf, "%s", zDir);
23560 sqlite3_free(zConverted);
23561 break;
23562 }
23563 sqlite3_free(zConverted);
23564 }else{
23565 zConverted = sqlite3MallocZero( nMax+1 );
23566 if( !zConverted ){
23567 sqlite3_free(zBuf);
23568 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
23569 return SQLITE_IOERR_NOMEM_BKPT;
23570 }
23571 if( cygwin_conv_path(
23572 osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
23573 zConverted, nMax+1)<0 ){
23574 sqlite3_free(zConverted);
23575 sqlite3_free(zBuf);
23576 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
23577 return winLogError(SQLITE_IOERR_CONVPATH, (DWORD)errno,
23578 "winGetTempname2", zDir);
23579 }
23580 if( winIsDir(zConverted) ){
23581 /* At this point, we know the candidate directory exists and should
23582 ** be used. However, we may need to convert the string containing
23583 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
23584 */
23585 char *zUtf8 = winConvertToUtf8Filename(zConverted);
23586 if( !zUtf8 ){
23587 sqlite3_free(zConverted);
23588 sqlite3_free(zBuf);
23589 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
23590 return SQLITE_IOERR_NOMEM_BKPT;
23591 }
23592 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
23593 sqlite3_free(zUtf8);
23594 sqlite3_free(zConverted);
23595 break;
23596 }
23597 sqlite3_free(zConverted);
23598 }
23599 }
23600 }
23601 #elif !SQLITE_OS_WINRT && !defined(__CYGWIN__)
23602 else if( osIsNT() ){
23603 char *zMulti;
23604 LPWSTR zWidePath = sqlite3MallocZero( nMax*sizeof(WCHAR) );
23605 if( !zWidePath ){
23606 sqlite3_free(zBuf);
23607 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
23608 return SQLITE_IOERR_NOMEM_BKPT;
23609 }
23610 if( osGetTempPathW(nMax, zWidePath)==0 ){
23611 sqlite3_free(zWidePath);
23612 sqlite3_free(zBuf);
23613 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
23614 return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
23615 "winGetTempname2", 0);
23616 }
23617 zMulti = winUnicodeToUtf8(zWidePath);
23618 if( zMulti ){
23619 sqlite3_snprintf(nMax, zBuf, "%s", zMulti);
23620 sqlite3_free(zMulti);
23621 sqlite3_free(zWidePath);
23622 }else{
23623 sqlite3_free(zWidePath);
23624 sqlite3_free(zBuf);
23625 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
23626 return SQLITE_IOERR_NOMEM_BKPT;
23627 }
23628 }
23629 #ifdef SQLITE_WIN32_HAS_ANSI
23630 else{
23631 char *zUtf8;
23632 char *zMbcsPath = sqlite3MallocZero( nMax );
23633 if( !zMbcsPath ){
23634 sqlite3_free(zBuf);
23635 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
23636 return SQLITE_IOERR_NOMEM_BKPT;
23637 }
23638 if( osGetTempPathA(nMax, zMbcsPath)==0 ){
23639 sqlite3_free(zBuf);
23640 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
23641 return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
23642 "winGetTempname3", 0);
23643 }
23644 zUtf8 = winMbcsToUtf8(zMbcsPath, osAreFileApisANSI());
23645 if( zUtf8 ){
23646 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
23647 sqlite3_free(zUtf8);
23648 }else{
23649 sqlite3_free(zBuf);
23650 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
23651 return SQLITE_IOERR_NOMEM_BKPT;
23652 }
23653 }
23654 #endif /* SQLITE_WIN32_HAS_ANSI */
23655 #endif /* !SQLITE_OS_WINRT */
23656
23657 /*
23658 ** Check to make sure the temporary directory ends with an appropriate
23659 ** separator. If it does not and there is not enough space left to add
23660 ** one, fail.
23661 */
23662 if( !winMakeEndInDirSep(nDir+1, zBuf) ){
23663 sqlite3_free(zBuf);
23664 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
23665 return winLogError(SQLITE_ERROR, 0, "winGetTempname4", 0);
23666 }
23667
23668 /*
23669 ** Check that the output buffer is large enough for the temporary file
23670 ** name in the following format:
23671 **
23672 ** "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
23673 **
23674 ** If not, return SQLITE_ERROR. The number 17 is used here in order to
23675 ** account for the space used by the 15 character random suffix and the
23676 ** two trailing NUL characters. The final directory separator character
23677 ** has already added if it was not already present.
23678 */
23679 nLen = sqlite3Strlen30(zBuf);
23680 if( (nLen + nPre + 17) > nBuf ){
23681 sqlite3_free(zBuf);
23682 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
23683 return winLogError(SQLITE_ERROR, 0, "winGetTempname5", 0);
23684 }
23685
23686 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
23687
23688 j = sqlite3Strlen30(zBuf);
23689 sqlite3_randomness(15, &zBuf[j]);
23690 for(i=0; i<15; i++, j++){
23691 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
23692 }
23693 zBuf[j] = 0;
23694 zBuf[j+1] = 0;
23695 *pzBuf = zBuf;
23696
23697 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
23698 return SQLITE_OK;
23699 }
23700
23701 /*
23702 ** Return TRUE if the named file is really a directory. Return false if
23703 ** it is something other than a directory, or if there is any kind of memory
23704 ** allocation failure.
23705 */
23706 static int winIsDir(const void *zConverted){
23707 DWORD attr;
23708 int rc = 0;
23709 DWORD lastErrno;
23710
23711 if( osIsNT() ){
23712 int cnt = 0;
23713 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
23714 memset(&sAttrData, 0, sizeof(sAttrData));
23715 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
23716 GetFileExInfoStandard,
23717 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
23718 if( !rc ){
23719 return 0; /* Invalid name? */
23720 }
23721 attr = sAttrData.dwFileAttributes;
23722 #if SQLITE_OS_WINCE==0
23723 }else{
23724 attr = osGetFileAttributesA((char*)zConverted);
23725 #endif
23726 }
23727 return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
23728 }
23729
23730 /*
23731 ** Open a file.
23732 */
23733 static int winOpen(
23734 sqlite3_vfs *pVfs, /* Used to get maximum path length and AppData */
23735 const char *zName, /* Name of the file (UTF-8) */
23736 sqlite3_file *id, /* Write the SQLite file handle here */
23737 int flags, /* Open mode flags */
23738 int *pOutFlags /* Status return flags */
23739 ){
23740 HANDLE h;
23741 DWORD lastErrno = 0;
23742 DWORD dwDesiredAccess;
23743 DWORD dwShareMode;
23744 DWORD dwCreationDisposition;
23745 DWORD dwFlagsAndAttributes = 0;
23746 #if SQLITE_OS_WINCE
23747 int isTemp = 0;
23748 #endif
23749 winVfsAppData *pAppData;
23750 winFile *pFile = (winFile*)id;
23751 void *zConverted; /* Filename in OS encoding */
23752 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
23753 int cnt = 0;
23754
23755 /* If argument zPath is a NULL pointer, this function is required to open
23756 ** a temporary file. Use this buffer to store the file name in.
23757 */
23758 char *zTmpname = 0; /* For temporary filename, if necessary. */
23759
23760 int rc = SQLITE_OK; /* Function Return Code */
23761 #if !defined(NDEBUG) || SQLITE_OS_WINCE
23762 int eType = flags&0xFFFFFF00; /* Type of file to open */
23763 #endif
23764
23765 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
23766 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
23767 int isCreate = (flags & SQLITE_OPEN_CREATE);
23768 int isReadonly = (flags & SQLITE_OPEN_READONLY);
23769 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
23770
23771 #ifndef NDEBUG
23772 int isOpenJournal = (isCreate && (
23773 eType==SQLITE_OPEN_MASTER_JOURNAL
23774 || eType==SQLITE_OPEN_MAIN_JOURNAL
23775 || eType==SQLITE_OPEN_WAL
23776 ));
23777 #endif
23778
23779 OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
23780 zUtf8Name, id, flags, pOutFlags));
23781
23782 /* Check the following statements are true:
23783 **
23784 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
23785 ** (b) if CREATE is set, then READWRITE must also be set, and
23786 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
23787 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
23788 */
23789 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
23790 assert(isCreate==0 || isReadWrite);
23791 assert(isExclusive==0 || isCreate);
23792 assert(isDelete==0 || isCreate);
23793
23794 /* The main DB, main journal, WAL file and master journal are never
23795 ** automatically deleted. Nor are they ever temporary files. */
23796 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
23797 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
23798 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
23799 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
23800
23801 /* Assert that the upper layer has set one of the "file-type" flags. */
23802 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
23803 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
23804 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
23805 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
23806 );
23807
23808 assert( pFile!=0 );
23809 memset(pFile, 0, sizeof(winFile));
23810 pFile->h = INVALID_HANDLE_VALUE;
23811
23812 #if SQLITE_OS_WINRT
23813 if( !zUtf8Name && !sqlite3_temp_directory ){
23814 sqlite3_log(SQLITE_ERROR,
23815 "sqlite3_temp_directory variable should be set for WinRT");
23816 }
23817 #endif
23818
23819 /* If the second argument to this function is NULL, generate a
23820 ** temporary file name to use
23821 */
23822 if( !zUtf8Name ){
23823 assert( isDelete && !isOpenJournal );
23824 rc = winGetTempname(pVfs, &zTmpname);
23825 if( rc!=SQLITE_OK ){
23826 OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
23827 return rc;
23828 }
23829 zUtf8Name = zTmpname;
23830 }
23831
23832 /* Database filenames are double-zero terminated if they are not
23833 ** URIs with parameters. Hence, they can always be passed into
23834 ** sqlite3_uri_parameter().
23835 */
23836 assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
23837 zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 );
23838
23839 /* Convert the filename to the system encoding. */
23840 zConverted = winConvertFromUtf8Filename(zUtf8Name);
23841 if( zConverted==0 ){
23842 sqlite3_free(zTmpname);
23843 OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
23844 return SQLITE_IOERR_NOMEM_BKPT;
23845 }
23846
23847 if( winIsDir(zConverted) ){
23848 sqlite3_free(zConverted);
23849 sqlite3_free(zTmpname);
23850 OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
23851 return SQLITE_CANTOPEN_ISDIR;
23852 }
23853
23854 if( isReadWrite ){
23855 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
23856 }else{
23857 dwDesiredAccess = GENERIC_READ;
23858 }
23859
23860 /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
23861 ** created. SQLite doesn't use it to indicate "exclusive access"
23862 ** as it is usually understood.
23863 */
23864 if( isExclusive ){
23865 /* Creates a new file, only if it does not already exist. */
23866 /* If the file exists, it fails. */
23867 dwCreationDisposition = CREATE_NEW;
23868 }else if( isCreate ){
23869 /* Open existing file, or create if it doesn't exist */
23870 dwCreationDisposition = OPEN_ALWAYS;
23871 }else{
23872 /* Opens a file, only if it exists. */
23873 dwCreationDisposition = OPEN_EXISTING;
23874 }
23875
23876 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
23877
23878 if( isDelete ){
23879 #if SQLITE_OS_WINCE
23880 dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
23881 isTemp = 1;
23882 #else
23883 dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
23884 | FILE_ATTRIBUTE_HIDDEN
23885 | FILE_FLAG_DELETE_ON_CLOSE;
23886 #endif
23887 }else{
23888 dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
23889 }
23890 /* Reports from the internet are that performance is always
23891 ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */
23892 #if SQLITE_OS_WINCE
23893 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
23894 #endif
23895
23896 if( osIsNT() ){
23897 #if SQLITE_OS_WINRT
23898 CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
23899 extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
23900 extendedParameters.dwFileAttributes =
23901 dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
23902 extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
23903 extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
23904 extendedParameters.lpSecurityAttributes = NULL;
23905 extendedParameters.hTemplateFile = NULL;
23906 while( (h = osCreateFile2((LPCWSTR)zConverted,
23907 dwDesiredAccess,
23908 dwShareMode,
23909 dwCreationDisposition,
23910 &extendedParameters))==INVALID_HANDLE_VALUE &&
23911 winRetryIoerr(&cnt, &lastErrno) ){
23912 /* Noop */
23913 }
23914 #else
23915 while( (h = osCreateFileW((LPCWSTR)zConverted,
23916 dwDesiredAccess,
23917 dwShareMode, NULL,
23918 dwCreationDisposition,
23919 dwFlagsAndAttributes,
23920 NULL))==INVALID_HANDLE_VALUE &&
23921 winRetryIoerr(&cnt, &lastErrno) ){
23922 /* Noop */
23923 }
23924 #endif
23925 }
23926 #ifdef SQLITE_WIN32_HAS_ANSI
23927 else{
23928 while( (h = osCreateFileA((LPCSTR)zConverted,
23929 dwDesiredAccess,
23930 dwShareMode, NULL,
23931 dwCreationDisposition,
23932 dwFlagsAndAttributes,
23933 NULL))==INVALID_HANDLE_VALUE &&
23934 winRetryIoerr(&cnt, &lastErrno) ){
23935 /* Noop */
23936 }
23937 }
23938 #endif
23939 winLogIoerr(cnt, __LINE__);
23940
23941 OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
23942 dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
23943
23944 if( h==INVALID_HANDLE_VALUE ){
23945 pFile->lastErrno = lastErrno;
23946 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
23947 sqlite3_free(zConverted);
23948 sqlite3_free(zTmpname);
23949 if( isReadWrite && !isExclusive ){
23950 return winOpen(pVfs, zName, id,
23951 ((flags|SQLITE_OPEN_READONLY) &
23952 ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
23953 pOutFlags);
23954 }else{
23955 return SQLITE_CANTOPEN_BKPT;
23956 }
23957 }
23958
23959 if( pOutFlags ){
23960 if( isReadWrite ){
23961 *pOutFlags = SQLITE_OPEN_READWRITE;
23962 }else{
23963 *pOutFlags = SQLITE_OPEN_READONLY;
23964 }
23965 }
23966
23967 OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, "
23968 "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ?
23969 *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
23970
23971 pAppData = (winVfsAppData*)pVfs->pAppData;
23972
23973 #if SQLITE_OS_WINCE
23974 {
23975 if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
23976 && ((pAppData==NULL) || !pAppData->bNoLock)
23977 && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
23978 ){
23979 osCloseHandle(h);
23980 sqlite3_free(zConverted);
23981 sqlite3_free(zTmpname);
23982 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
23983 return rc;
23984 }
23985 }
23986 if( isTemp ){
23987 pFile->zDeleteOnClose = zConverted;
23988 }else
23989 #endif
23990 {
23991 sqlite3_free(zConverted);
23992 }
23993
23994 sqlite3_free(zTmpname);
23995 pFile->pMethod = pAppData ? pAppData->pMethod : &winIoMethod;
23996 pFile->pVfs = pVfs;
23997 pFile->h = h;
23998 if( isReadonly ){
23999 pFile->ctrlFlags |= WINFILE_RDONLY;
24000 }
24001 if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
24002 pFile->ctrlFlags |= WINFILE_PSOW;
24003 }
24004 pFile->lastErrno = NO_ERROR;
24005 pFile->zPath = zName;
24006 #if SQLITE_MAX_MMAP_SIZE>0
24007 pFile->hMap = NULL;
24008 pFile->pMapRegion = 0;
24009 pFile->mmapSize = 0;
24010 pFile->mmapSizeActual = 0;
24011 pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
24012 #endif
24013
24014 OpenCounter(+1);
24015 return rc;
24016 }
24017
24018 /*
24019 ** Delete the named file.
24020 **
24021 ** Note that Windows does not allow a file to be deleted if some other
24022 ** process has it open. Sometimes a virus scanner or indexing program
24023 ** will open a journal file shortly after it is created in order to do
24024 ** whatever it does. While this other process is holding the
24025 ** file open, we will be unable to delete it. To work around this
24026 ** problem, we delay 100 milliseconds and try to delete again. Up
24027 ** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
24028 ** up and returning an error.
24029 */
24030 static int winDelete(
24031 sqlite3_vfs *pVfs, /* Not used on win32 */
24032 const char *zFilename, /* Name of file to delete */
24033 int syncDir /* Not used on win32 */
24034 ){
24035 int cnt = 0;
24036 int rc;
24037 DWORD attr;
24038 DWORD lastErrno = 0;
24039 void *zConverted;
24040 UNUSED_PARAMETER(pVfs);
24041 UNUSED_PARAMETER(syncDir);
24042
24043 SimulateIOError(return SQLITE_IOERR_DELETE);
24044 OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
24045
24046 zConverted = winConvertFromUtf8Filename(zFilename);
24047 if( zConverted==0 ){
24048 OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
24049 return SQLITE_IOERR_NOMEM_BKPT;
24050 }
24051 if( osIsNT() ){
24052 do {
24053 #if SQLITE_OS_WINRT
24054 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
24055 memset(&sAttrData, 0, sizeof(sAttrData));
24056 if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
24057 &sAttrData) ){
24058 attr = sAttrData.dwFileAttributes;
24059 }else{
24060 lastErrno = osGetLastError();
24061 if( lastErrno==ERROR_FILE_NOT_FOUND
24062 || lastErrno==ERROR_PATH_NOT_FOUND ){
24063 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
24064 }else{
24065 rc = SQLITE_ERROR;
24066 }
24067 break;
24068 }
24069 #else
24070 attr = osGetFileAttributesW(zConverted);
24071 #endif
24072 if ( attr==INVALID_FILE_ATTRIBUTES ){
24073 lastErrno = osGetLastError();
24074 if( lastErrno==ERROR_FILE_NOT_FOUND
24075 || lastErrno==ERROR_PATH_NOT_FOUND ){
24076 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
24077 }else{
24078 rc = SQLITE_ERROR;
24079 }
24080 break;
24081 }
24082 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
24083 rc = SQLITE_ERROR; /* Files only. */
24084 break;
24085 }
24086 if ( osDeleteFileW(zConverted) ){
24087 rc = SQLITE_OK; /* Deleted OK. */
24088 break;
24089 }
24090 if ( !winRetryIoerr(&cnt, &lastErrno) ){
24091 rc = SQLITE_ERROR; /* No more retries. */
24092 break;
24093 }
24094 } while(1);
24095 }
24096 #ifdef SQLITE_WIN32_HAS_ANSI
24097 else{
24098 do {
24099 attr = osGetFileAttributesA(zConverted);
24100 if ( attr==INVALID_FILE_ATTRIBUTES ){
24101 lastErrno = osGetLastError();
24102 if( lastErrno==ERROR_FILE_NOT_FOUND
24103 || lastErrno==ERROR_PATH_NOT_FOUND ){
24104 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
24105 }else{
24106 rc = SQLITE_ERROR;
24107 }
24108 break;
24109 }
24110 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
24111 rc = SQLITE_ERROR; /* Files only. */
24112 break;
24113 }
24114 if ( osDeleteFileA(zConverted) ){
24115 rc = SQLITE_OK; /* Deleted OK. */
24116 break;
24117 }
24118 if ( !winRetryIoerr(&cnt, &lastErrno) ){
24119 rc = SQLITE_ERROR; /* No more retries. */
24120 break;
24121 }
24122 } while(1);
24123 }
24124 #endif
24125 if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
24126 rc = winLogError(SQLITE_IOERR_DELETE, lastErrno, "winDelete", zFilename);
24127 }else{
24128 winLogIoerr(cnt, __LINE__);
24129 }
24130 sqlite3_free(zConverted);
24131 OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
24132 return rc;
24133 }
24134
24135 /*
24136 ** Check the existence and status of a file.
24137 */
24138 static int winAccess(
24139 sqlite3_vfs *pVfs, /* Not used on win32 */
24140 const char *zFilename, /* Name of file to check */
24141 int flags, /* Type of test to make on this file */
24142 int *pResOut /* OUT: Result */
24143 ){
24144 DWORD attr;
24145 int rc = 0;
24146 DWORD lastErrno = 0;
24147 void *zConverted;
24148 UNUSED_PARAMETER(pVfs);
24149
24150 SimulateIOError( return SQLITE_IOERR_ACCESS; );
24151 OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
24152 zFilename, flags, pResOut));
24153
24154 zConverted = winConvertFromUtf8Filename(zFilename);
24155 if( zConverted==0 ){
24156 OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
24157 return SQLITE_IOERR_NOMEM_BKPT;
24158 }
24159 if( osIsNT() ){
24160 int cnt = 0;
24161 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
24162 memset(&sAttrData, 0, sizeof(sAttrData));
24163 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
24164 GetFileExInfoStandard,
24165 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
24166 if( rc ){
24167 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
24168 ** as if it does not exist.
24169 */
24170 if( flags==SQLITE_ACCESS_EXISTS
24171 && sAttrData.nFileSizeHigh==0
24172 && sAttrData.nFileSizeLow==0 ){
24173 attr = INVALID_FILE_ATTRIBUTES;
24174 }else{
24175 attr = sAttrData.dwFileAttributes;
24176 }
24177 }else{
24178 winLogIoerr(cnt, __LINE__);
24179 if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
24180 sqlite3_free(zConverted);
24181 return winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess",
24182 zFilename);
24183 }else{
24184 attr = INVALID_FILE_ATTRIBUTES;
24185 }
24186 }
24187 }
24188 #ifdef SQLITE_WIN32_HAS_ANSI
24189 else{
24190 attr = osGetFileAttributesA((char*)zConverted);
24191 }
24192 #endif
24193 sqlite3_free(zConverted);
24194 switch( flags ){
24195 case SQLITE_ACCESS_READ:
24196 case SQLITE_ACCESS_EXISTS:
24197 rc = attr!=INVALID_FILE_ATTRIBUTES;
24198 break;
24199 case SQLITE_ACCESS_READWRITE:
24200 rc = attr!=INVALID_FILE_ATTRIBUTES &&
24201 (attr & FILE_ATTRIBUTE_READONLY)==0;
24202 break;
24203 default:
24204 assert(!"Invalid flags argument");
24205 }
24206 *pResOut = rc;
24207 OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
24208 zFilename, pResOut, *pResOut));
24209 return SQLITE_OK;
24210 }
24211
24212 /*
24213 ** Returns non-zero if the specified path name starts with a drive letter
24214 ** followed by a colon character.
24215 */
24216 static BOOL winIsDriveLetterAndColon(
24217 const char *zPathname
24218 ){
24219 return ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' );
24220 }
24221
24222 /*
24223 ** Returns non-zero if the specified path name should be used verbatim. If
24224 ** non-zero is returned from this function, the calling function must simply
24225 ** use the provided path name verbatim -OR- resolve it into a full path name
24226 ** using the GetFullPathName Win32 API function (if available).
24227 */
24228 static BOOL winIsVerbatimPathname(
24229 const char *zPathname
24230 ){
24231 /*
24232 ** If the path name starts with a forward slash or a backslash, it is either
24233 ** a legal UNC name, a volume relative path, or an absolute path name in the
24234 ** "Unix" format on Windows. There is no easy way to differentiate between
24235 ** the final two cases; therefore, we return the safer return value of TRUE
24236 ** so that callers of this function will simply use it verbatim.
24237 */
24238 if ( winIsDirSep(zPathname[0]) ){
24239 return TRUE;
24240 }
24241
24242 /*
24243 ** If the path name starts with a letter and a colon it is either a volume
24244 ** relative path or an absolute path. Callers of this function must not
24245 ** attempt to treat it as a relative path name (i.e. they should simply use
24246 ** it verbatim).
24247 */
24248 if ( winIsDriveLetterAndColon(zPathname) ){
24249 return TRUE;
24250 }
24251
24252 /*
24253 ** If we get to this point, the path name should almost certainly be a purely
24254 ** relative one (i.e. not a UNC name, not absolute, and not volume relative).
24255 */
24256 return FALSE;
24257 }
24258
24259 /*
24260 ** Turn a relative pathname into a full pathname. Write the full
24261 ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
24262 ** bytes in size.
24263 */
24264 static int winFullPathname(
24265 sqlite3_vfs *pVfs, /* Pointer to vfs object */
24266 const char *zRelative, /* Possibly relative input path */
24267 int nFull, /* Size of output buffer in bytes */
24268 char *zFull /* Output buffer */
24269 ){
24270 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
24271 DWORD nByte;
24272 void *zConverted;
24273 char *zOut;
24274 #endif
24275
24276 /* If this path name begins with "/X:", where "X" is any alphabetic
24277 ** character, discard the initial "/" from the pathname.
24278 */
24279 if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){
24280 zRelative++;
24281 }
24282
24283 #if defined(__CYGWIN__)
24284 SimulateIOError( return SQLITE_ERROR );
24285 UNUSED_PARAMETER(nFull);
24286 assert( nFull>=pVfs->mxPathname );
24287 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
24288 /*
24289 ** NOTE: We are dealing with a relative path name and the data
24290 ** directory has been set. Therefore, use it as the basis
24291 ** for converting the relative path name to an absolute
24292 ** one by prepending the data directory and a slash.
24293 */
24294 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
24295 if( !zOut ){
24296 return SQLITE_IOERR_NOMEM_BKPT;
24297 }
24298 if( cygwin_conv_path(
24299 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) |
24300 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
24301 sqlite3_free(zOut);
24302 return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
24303 "winFullPathname1", zRelative);
24304 }else{
24305 char *zUtf8 = winConvertToUtf8Filename(zOut);
24306 if( !zUtf8 ){
24307 sqlite3_free(zOut);
24308 return SQLITE_IOERR_NOMEM_BKPT;
24309 }
24310 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
24311 sqlite3_data_directory, winGetDirSep(), zUtf8);
24312 sqlite3_free(zUtf8);
24313 sqlite3_free(zOut);
24314 }
24315 }else{
24316 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
24317 if( !zOut ){
24318 return SQLITE_IOERR_NOMEM_BKPT;
24319 }
24320 if( cygwin_conv_path(
24321 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A),
24322 zRelative, zOut, pVfs->mxPathname+1)<0 ){
24323 sqlite3_free(zOut);
24324 return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
24325 "winFullPathname2", zRelative);
24326 }else{
24327 char *zUtf8 = winConvertToUtf8Filename(zOut);
24328 if( !zUtf8 ){
24329 sqlite3_free(zOut);
24330 return SQLITE_IOERR_NOMEM_BKPT;
24331 }
24332 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
24333 sqlite3_free(zUtf8);
24334 sqlite3_free(zOut);
24335 }
24336 }
24337 return SQLITE_OK;
24338 #endif
24339
24340 #if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
24341 SimulateIOError( return SQLITE_ERROR );
24342 /* WinCE has no concept of a relative pathname, or so I am told. */
24343 /* WinRT has no way to convert a relative path to an absolute one. */
24344 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
24345 /*
24346 ** NOTE: We are dealing with a relative path name and the data
24347 ** directory has been set. Therefore, use it as the basis
24348 ** for converting the relative path name to an absolute
24349 ** one by prepending the data directory and a backslash.
24350 */
24351 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
24352 sqlite3_data_directory, winGetDirSep(), zRelative);
24353 }else{
24354 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
24355 }
24356 return SQLITE_OK;
24357 #endif
24358
24359 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
24360 /* It's odd to simulate an io-error here, but really this is just
24361 ** using the io-error infrastructure to test that SQLite handles this
24362 ** function failing. This function could fail if, for example, the
24363 ** current working directory has been unlinked.
24364 */
24365 SimulateIOError( return SQLITE_ERROR );
24366 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
24367 /*
24368 ** NOTE: We are dealing with a relative path name and the data
24369 ** directory has been set. Therefore, use it as the basis
24370 ** for converting the relative path name to an absolute
24371 ** one by prepending the data directory and a backslash.
24372 */
24373 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
24374 sqlite3_data_directory, winGetDirSep(), zRelative);
24375 return SQLITE_OK;
24376 }
24377 zConverted = winConvertFromUtf8Filename(zRelative);
24378 if( zConverted==0 ){
24379 return SQLITE_IOERR_NOMEM_BKPT;
24380 }
24381 if( osIsNT() ){
24382 LPWSTR zTemp;
24383 nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
24384 if( nByte==0 ){
24385 sqlite3_free(zConverted);
24386 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
24387 "winFullPathname1", zRelative);
24388 }
24389 nByte += 3;
24390 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
24391 if( zTemp==0 ){
24392 sqlite3_free(zConverted);
24393 return SQLITE_IOERR_NOMEM_BKPT;
24394 }
24395 nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
24396 if( nByte==0 ){
24397 sqlite3_free(zConverted);
24398 sqlite3_free(zTemp);
24399 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
24400 "winFullPathname2", zRelative);
24401 }
24402 sqlite3_free(zConverted);
24403 zOut = winUnicodeToUtf8(zTemp);
24404 sqlite3_free(zTemp);
24405 }
24406 #ifdef SQLITE_WIN32_HAS_ANSI
24407 else{
24408 char *zTemp;
24409 nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
24410 if( nByte==0 ){
24411 sqlite3_free(zConverted);
24412 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
24413 "winFullPathname3", zRelative);
24414 }
24415 nByte += 3;
24416 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
24417 if( zTemp==0 ){
24418 sqlite3_free(zConverted);
24419 return SQLITE_IOERR_NOMEM_BKPT;
24420 }
24421 nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
24422 if( nByte==0 ){
24423 sqlite3_free(zConverted);
24424 sqlite3_free(zTemp);
24425 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
24426 "winFullPathname4", zRelative);
24427 }
24428 sqlite3_free(zConverted);
24429 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
24430 sqlite3_free(zTemp);
24431 }
24432 #endif
24433 if( zOut ){
24434 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
24435 sqlite3_free(zOut);
24436 return SQLITE_OK;
24437 }else{
24438 return SQLITE_IOERR_NOMEM_BKPT;
24439 }
24440 #endif
24441 }
24442
24443 #ifndef SQLITE_OMIT_LOAD_EXTENSION
24444 /*
24445 ** Interfaces for opening a shared library, finding entry points
24446 ** within the shared library, and closing the shared library.
24447 */
24448 static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
24449 HANDLE h;
24450 #if defined(__CYGWIN__)
24451 int nFull = pVfs->mxPathname+1;
24452 char *zFull = sqlite3MallocZero( nFull );
24453 void *zConverted = 0;
24454 if( zFull==0 ){
24455 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
24456 return 0;
24457 }
24458 if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK ){
24459 sqlite3_free(zFull);
24460 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
24461 return 0;
24462 }
24463 zConverted = winConvertFromUtf8Filename(zFull);
24464 sqlite3_free(zFull);
24465 #else
24466 void *zConverted = winConvertFromUtf8Filename(zFilename);
24467 UNUSED_PARAMETER(pVfs);
24468 #endif
24469 if( zConverted==0 ){
24470 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
24471 return 0;
24472 }
24473 if( osIsNT() ){
24474 #if SQLITE_OS_WINRT
24475 h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
24476 #else
24477 h = osLoadLibraryW((LPCWSTR)zConverted);
24478 #endif
24479 }
24480 #ifdef SQLITE_WIN32_HAS_ANSI
24481 else{
24482 h = osLoadLibraryA((char*)zConverted);
24483 }
24484 #endif
24485 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h));
24486 sqlite3_free(zConverted);
24487 return (void*)h;
24488 }
24489 static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
24490 UNUSED_PARAMETER(pVfs);
24491 winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
24492 }
24493 static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
24494 FARPROC proc;
24495 UNUSED_PARAMETER(pVfs);
24496 proc = osGetProcAddressA((HANDLE)pH, zSym);
24497 OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n",
24498 (void*)pH, zSym, (void*)proc));
24499 return (void(*)(void))proc;
24500 }
24501 static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
24502 UNUSED_PARAMETER(pVfs);
24503 osFreeLibrary((HANDLE)pHandle);
24504 OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle));
24505 }
24506 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
24507 #define winDlOpen 0
24508 #define winDlError 0
24509 #define winDlSym 0
24510 #define winDlClose 0
24511 #endif
24512
24513 /* State information for the randomness gatherer. */
24514 typedef struct EntropyGatherer EntropyGatherer;
24515 struct EntropyGatherer {
24516 unsigned char *a; /* Gather entropy into this buffer */
24517 int na; /* Size of a[] in bytes */
24518 int i; /* XOR next input into a[i] */
24519 int nXor; /* Number of XOR operations done */
24520 };
24521
24522 #if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
24523 /* Mix sz bytes of entropy into p. */
24524 static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){
24525 int j, k;
24526 for(j=0, k=p->i; j<sz; j++){
24527 p->a[k++] ^= x[j];
24528 if( k>=p->na ) k = 0;
24529 }
24530 p->i = k;
24531 p->nXor += sz;
24532 }
24533 #endif /* !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) */
24534
24535 /*
24536 ** Write up to nBuf bytes of randomness into zBuf.
24537 */
24538 static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
24539 #if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS)
24540 UNUSED_PARAMETER(pVfs);
24541 memset(zBuf, 0, nBuf);
24542 return nBuf;
24543 #else
24544 EntropyGatherer e;
24545 UNUSED_PARAMETER(pVfs);
24546 memset(zBuf, 0, nBuf);
24547 #if defined(_MSC_VER) && _MSC_VER>=1400 && !SQLITE_OS_WINCE
24548 rand_s((unsigned int*)zBuf); /* rand_s() is not available with MinGW */
24549 #endif /* defined(_MSC_VER) && _MSC_VER>=1400 */
24550 e.a = (unsigned char*)zBuf;
24551 e.na = nBuf;
24552 e.nXor = 0;
24553 e.i = 0;
24554 {
24555 SYSTEMTIME x;
24556 osGetSystemTime(&x);
24557 xorMemory(&e, (unsigned char*)&x, sizeof(SYSTEMTIME));
24558 }
24559 {
24560 DWORD pid = osGetCurrentProcessId();
24561 xorMemory(&e, (unsigned char*)&pid, sizeof(DWORD));
24562 }
24563 #if SQLITE_OS_WINRT
24564 {
24565 ULONGLONG cnt = osGetTickCount64();
24566 xorMemory(&e, (unsigned char*)&cnt, sizeof(ULONGLONG));
24567 }
24568 #else
24569 {
24570 DWORD cnt = osGetTickCount();
24571 xorMemory(&e, (unsigned char*)&cnt, sizeof(DWORD));
24572 }
24573 #endif /* SQLITE_OS_WINRT */
24574 {
24575 LARGE_INTEGER i;
24576 osQueryPerformanceCounter(&i);
24577 xorMemory(&e, (unsigned char*)&i, sizeof(LARGE_INTEGER));
24578 }
24579 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
24580 {
24581 UUID id;
24582 memset(&id, 0, sizeof(UUID));
24583 osUuidCreate(&id);
24584 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
24585 memset(&id, 0, sizeof(UUID));
24586 osUuidCreateSequential(&id);
24587 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
24588 }
24589 #endif /* !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID */
24590 return e.nXor>nBuf ? nBuf : e.nXor;
24591 #endif /* defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) */
24592 }
24593
24594
24595 /*
24596 ** Sleep for a little while. Return the amount of time slept.
24597 */
24598 static int winSleep(sqlite3_vfs *pVfs, int microsec){
24599 sqlite3_win32_sleep((microsec+999)/1000);
24600 UNUSED_PARAMETER(pVfs);
24601 return ((microsec+999)/1000)*1000;
24602 }
24603
24604 /*
24605 ** The following variable, if set to a non-zero value, is interpreted as
24606 ** the number of seconds since 1970 and is used to set the result of
24607 ** sqlite3OsCurrentTime() during testing.
24608 */
24609 #ifdef SQLITE_TEST
24610 SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1 970. */
24611 #endif
24612
24613 /*
24614 ** Find the current time (in Universal Coordinated Time). Write into *piNow
24615 ** the current time and date as a Julian Day number times 86_400_000. In
24616 ** other words, write into *piNow the number of milliseconds since the Julian
24617 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
24618 ** proleptic Gregorian calendar.
24619 **
24620 ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
24621 ** cannot be found.
24622 */
24623 static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
24624 /* FILETIME structure is a 64-bit value representing the number of
24625 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
24626 */
24627 FILETIME ft;
24628 static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
24629 #ifdef SQLITE_TEST
24630 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
24631 #endif
24632 /* 2^32 - to avoid use of LL and warnings in gcc */
24633 static const sqlite3_int64 max32BitValue =
24634 (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
24635 (sqlite3_int64)294967296;
24636
24637 #if SQLITE_OS_WINCE
24638 SYSTEMTIME time;
24639 osGetSystemTime(&time);
24640 /* if SystemTimeToFileTime() fails, it returns zero. */
24641 if (!osSystemTimeToFileTime(&time,&ft)){
24642 return SQLITE_ERROR;
24643 }
24644 #else
24645 osGetSystemTimeAsFileTime( &ft );
24646 #endif
24647
24648 *piNow = winFiletimeEpoch +
24649 ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
24650 (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
24651
24652 #ifdef SQLITE_TEST
24653 if( sqlite3_current_time ){
24654 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
24655 }
24656 #endif
24657 UNUSED_PARAMETER(pVfs);
24658 return SQLITE_OK;
24659 }
24660
24661 /*
24662 ** Find the current time (in Universal Coordinated Time). Write the
24663 ** current time and date as a Julian Day number into *prNow and
24664 ** return 0. Return 1 if the time and date cannot be found.
24665 */
24666 static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
24667 int rc;
24668 sqlite3_int64 i;
24669 rc = winCurrentTimeInt64(pVfs, &i);
24670 if( !rc ){
24671 *prNow = i/86400000.0;
24672 }
24673 return rc;
24674 }
24675
24676 /*
24677 ** The idea is that this function works like a combination of
24678 ** GetLastError() and FormatMessage() on Windows (or errno and
24679 ** strerror_r() on Unix). After an error is returned by an OS
24680 ** function, SQLite calls this function with zBuf pointing to
24681 ** a buffer of nBuf bytes. The OS layer should populate the
24682 ** buffer with a nul-terminated UTF-8 encoded error message
24683 ** describing the last IO error to have occurred within the calling
24684 ** thread.
24685 **
24686 ** If the error message is too large for the supplied buffer,
24687 ** it should be truncated. The return value of xGetLastError
24688 ** is zero if the error message fits in the buffer, or non-zero
24689 ** otherwise (if the message was truncated). If non-zero is returned,
24690 ** then it is not necessary to include the nul-terminator character
24691 ** in the output buffer.
24692 **
24693 ** Not supplying an error message will have no adverse effect
24694 ** on SQLite. It is fine to have an implementation that never
24695 ** returns an error message:
24696 **
24697 ** int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
24698 ** assert(zBuf[0]=='\0');
24699 ** return 0;
24700 ** }
24701 **
24702 ** However if an error message is supplied, it will be incorporated
24703 ** by sqlite into the error message available to the user using
24704 ** sqlite3_errmsg(), possibly making IO errors easier to debug.
24705 */
24706 static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
24707 DWORD e = osGetLastError();
24708 UNUSED_PARAMETER(pVfs);
24709 if( nBuf>0 ) winGetLastErrorMsg(e, nBuf, zBuf);
24710 return e;
24711 }
24712
24713 /*
24714 ** Initialize and deinitialize the operating system interface.
24715 */
24716 SQLITE_API int sqlite3_os_init(void){
24717 static sqlite3_vfs winVfs = {
24718 3, /* iVersion */
24719 sizeof(winFile), /* szOsFile */
24720 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
24721 0, /* pNext */
24722 "win32", /* zName */
24723 &winAppData, /* pAppData */
24724 winOpen, /* xOpen */
24725 winDelete, /* xDelete */
24726 winAccess, /* xAccess */
24727 winFullPathname, /* xFullPathname */
24728 winDlOpen, /* xDlOpen */
24729 winDlError, /* xDlError */
24730 winDlSym, /* xDlSym */
24731 winDlClose, /* xDlClose */
24732 winRandomness, /* xRandomness */
24733 winSleep, /* xSleep */
24734 winCurrentTime, /* xCurrentTime */
24735 winGetLastError, /* xGetLastError */
24736 winCurrentTimeInt64, /* xCurrentTimeInt64 */
24737 winSetSystemCall, /* xSetSystemCall */
24738 winGetSystemCall, /* xGetSystemCall */
24739 winNextSystemCall, /* xNextSystemCall */
24740 };
24741 #if defined(SQLITE_WIN32_HAS_WIDE)
24742 static sqlite3_vfs winLongPathVfs = {
24743 3, /* iVersion */
24744 sizeof(winFile), /* szOsFile */
24745 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
24746 0, /* pNext */
24747 "win32-longpath", /* zName */
24748 &winAppData, /* pAppData */
24749 winOpen, /* xOpen */
24750 winDelete, /* xDelete */
24751 winAccess, /* xAccess */
24752 winFullPathname, /* xFullPathname */
24753 winDlOpen, /* xDlOpen */
24754 winDlError, /* xDlError */
24755 winDlSym, /* xDlSym */
24756 winDlClose, /* xDlClose */
24757 winRandomness, /* xRandomness */
24758 winSleep, /* xSleep */
24759 winCurrentTime, /* xCurrentTime */
24760 winGetLastError, /* xGetLastError */
24761 winCurrentTimeInt64, /* xCurrentTimeInt64 */
24762 winSetSystemCall, /* xSetSystemCall */
24763 winGetSystemCall, /* xGetSystemCall */
24764 winNextSystemCall, /* xNextSystemCall */
24765 };
24766 #endif
24767 static sqlite3_vfs winNolockVfs = {
24768 3, /* iVersion */
24769 sizeof(winFile), /* szOsFile */
24770 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
24771 0, /* pNext */
24772 "win32-none", /* zName */
24773 &winNolockAppData, /* pAppData */
24774 winOpen, /* xOpen */
24775 winDelete, /* xDelete */
24776 winAccess, /* xAccess */
24777 winFullPathname, /* xFullPathname */
24778 winDlOpen, /* xDlOpen */
24779 winDlError, /* xDlError */
24780 winDlSym, /* xDlSym */
24781 winDlClose, /* xDlClose */
24782 winRandomness, /* xRandomness */
24783 winSleep, /* xSleep */
24784 winCurrentTime, /* xCurrentTime */
24785 winGetLastError, /* xGetLastError */
24786 winCurrentTimeInt64, /* xCurrentTimeInt64 */
24787 winSetSystemCall, /* xSetSystemCall */
24788 winGetSystemCall, /* xGetSystemCall */
24789 winNextSystemCall, /* xNextSystemCall */
24790 };
24791 #if defined(SQLITE_WIN32_HAS_WIDE)
24792 static sqlite3_vfs winLongPathNolockVfs = {
24793 3, /* iVersion */
24794 sizeof(winFile), /* szOsFile */
24795 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
24796 0, /* pNext */
24797 "win32-longpath-none", /* zName */
24798 &winNolockAppData, /* pAppData */
24799 winOpen, /* xOpen */
24800 winDelete, /* xDelete */
24801 winAccess, /* xAccess */
24802 winFullPathname, /* xFullPathname */
24803 winDlOpen, /* xDlOpen */
24804 winDlError, /* xDlError */
24805 winDlSym, /* xDlSym */
24806 winDlClose, /* xDlClose */
24807 winRandomness, /* xRandomness */
24808 winSleep, /* xSleep */
24809 winCurrentTime, /* xCurrentTime */
24810 winGetLastError, /* xGetLastError */
24811 winCurrentTimeInt64, /* xCurrentTimeInt64 */
24812 winSetSystemCall, /* xSetSystemCall */
24813 winGetSystemCall, /* xGetSystemCall */
24814 winNextSystemCall, /* xNextSystemCall */
24815 };
24816 #endif
24817
24818 /* Double-check that the aSyscall[] array has been constructed
24819 ** correctly. See ticket [bb3a86e890c8e96ab] */
24820 assert( ArraySize(aSyscall)==80 );
24821
24822 /* get memory map allocation granularity */
24823 memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
24824 #if SQLITE_OS_WINRT
24825 osGetNativeSystemInfo(&winSysInfo);
24826 #else
24827 osGetSystemInfo(&winSysInfo);
24828 #endif
24829 assert( winSysInfo.dwAllocationGranularity>0 );
24830 assert( winSysInfo.dwPageSize>0 );
24831
24832 sqlite3_vfs_register(&winVfs, 1);
24833
24834 #if defined(SQLITE_WIN32_HAS_WIDE)
24835 sqlite3_vfs_register(&winLongPathVfs, 0);
24836 #endif
24837
24838 sqlite3_vfs_register(&winNolockVfs, 0);
24839
24840 #if defined(SQLITE_WIN32_HAS_WIDE)
24841 sqlite3_vfs_register(&winLongPathNolockVfs, 0);
24842 #endif
24843
24844 return SQLITE_OK;
24845 }
24846
24847 SQLITE_API int sqlite3_os_end(void){
24848 #if SQLITE_OS_WINRT
24849 if( sleepObj!=NULL ){
24850 osCloseHandle(sleepObj);
24851 sleepObj = NULL;
24852 }
24853 #endif
24854 return SQLITE_OK;
24855 }
24856
24857 CHROMIUM_SQLITE_API
24858 void chromium_sqlite3_initialize_win_sqlite3_file(sqlite3_file* file, HANDLE han dle) {
24859 winFile* winSQLite3File = (winFile*)file;
24860 memset(file, 0, sizeof(*file));
24861 winSQLite3File->pMethod = &winIoMethod;
24862 winSQLite3File->h = handle;
24863 }
24864
24865 #endif /* SQLITE_OS_WIN */
24866
24867 /************** End of os_win.c **********************************************/
24868 /************** Begin file bitvec.c ******************************************/
24869 /*
24870 ** 2008 February 16
24871 **
24872 ** The author disclaims copyright to this source code. In place of
24873 ** a legal notice, here is a blessing:
24874 **
24875 ** May you do good and not evil.
24876 ** May you find forgiveness for yourself and forgive others.
24877 ** May you share freely, never taking more than you give.
24878 **
24879 *************************************************************************
24880 ** This file implements an object that represents a fixed-length
24881 ** bitmap. Bits are numbered starting with 1.
24882 **
24883 ** A bitmap is used to record which pages of a database file have been
24884 ** journalled during a transaction, or which pages have the "dont-write"
24885 ** property. Usually only a few pages are meet either condition.
24886 ** So the bitmap is usually sparse and has low cardinality.
24887 ** But sometimes (for example when during a DROP of a large table) most
24888 ** or all of the pages in a database can get journalled. In those cases,
24889 ** the bitmap becomes dense with high cardinality. The algorithm needs
24890 ** to handle both cases well.
24891 **
24892 ** The size of the bitmap is fixed when the object is created.
24893 **
24894 ** All bits are clear when the bitmap is created. Individual bits
24895 ** may be set or cleared one at a time.
24896 **
24897 ** Test operations are about 100 times more common that set operations.
24898 ** Clear operations are exceedingly rare. There are usually between
24899 ** 5 and 500 set operations per Bitvec object, though the number of sets can
24900 ** sometimes grow into tens of thousands or larger. The size of the
24901 ** Bitvec object is the number of pages in the database file at the
24902 ** start of a transaction, and is thus usually less than a few thousand,
24903 ** but can be as large as 2 billion for a really big database.
24904 */
24905 /* #include "sqliteInt.h" */
24906
24907 /* Size of the Bitvec structure in bytes. */
24908 #define BITVEC_SZ 512
24909
24910 /* Round the union size down to the nearest pointer boundary, since that's how
24911 ** it will be aligned within the Bitvec struct. */
24912 #define BITVEC_USIZE \
24913 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
24914
24915 /* Type of the array "element" for the bitmap representation.
24916 ** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
24917 ** Setting this to the "natural word" size of your CPU may improve
24918 ** performance. */
24919 #define BITVEC_TELEM u8
24920 /* Size, in bits, of the bitmap element. */
24921 #define BITVEC_SZELEM 8
24922 /* Number of elements in a bitmap array. */
24923 #define BITVEC_NELEM (BITVEC_USIZE/sizeof(BITVEC_TELEM))
24924 /* Number of bits in the bitmap array. */
24925 #define BITVEC_NBIT (BITVEC_NELEM*BITVEC_SZELEM)
24926
24927 /* Number of u32 values in hash table. */
24928 #define BITVEC_NINT (BITVEC_USIZE/sizeof(u32))
24929 /* Maximum number of entries in hash table before
24930 ** sub-dividing and re-hashing. */
24931 #define BITVEC_MXHASH (BITVEC_NINT/2)
24932 /* Hashing function for the aHash representation.
24933 ** Empirical testing showed that the *37 multiplier
24934 ** (an arbitrary prime)in the hash function provided
24935 ** no fewer collisions than the no-op *1. */
24936 #define BITVEC_HASH(X) (((X)*1)%BITVEC_NINT)
24937
24938 #define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *))
24939
24940
24941 /*
24942 ** A bitmap is an instance of the following structure.
24943 **
24944 ** This bitmap records the existence of zero or more bits
24945 ** with values between 1 and iSize, inclusive.
24946 **
24947 ** There are three possible representations of the bitmap.
24948 ** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
24949 ** bitmap. The least significant bit is bit 1.
24950 **
24951 ** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
24952 ** a hash table that will hold up to BITVEC_MXHASH distinct values.
24953 **
24954 ** Otherwise, the value i is redirected into one of BITVEC_NPTR
24955 ** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
24956 ** handles up to iDivisor separate values of i. apSub[0] holds
24957 ** values between 1 and iDivisor. apSub[1] holds values between
24958 ** iDivisor+1 and 2*iDivisor. apSub[N] holds values between
24959 ** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized
24960 ** to hold deal with values between 1 and iDivisor.
24961 */
24962 struct Bitvec {
24963 u32 iSize; /* Maximum bit index. Max iSize is 4,294,967,296. */
24964 u32 nSet; /* Number of bits that are set - only valid for aHash
24965 ** element. Max is BITVEC_NINT. For BITVEC_SZ of 512,
24966 ** this would be 125. */
24967 u32 iDivisor; /* Number of bits handled by each apSub[] entry. */
24968 /* Should >=0 for apSub element. */
24969 /* Max iDivisor is max(u32) / BITVEC_NPTR + 1. */
24970 /* For a BITVEC_SZ of 512, this would be 34,359,739. */
24971 union {
24972 BITVEC_TELEM aBitmap[BITVEC_NELEM]; /* Bitmap representation */
24973 u32 aHash[BITVEC_NINT]; /* Hash table representation */
24974 Bitvec *apSub[BITVEC_NPTR]; /* Recursive representation */
24975 } u;
24976 };
24977
24978 /*
24979 ** Create a new bitmap object able to handle bits between 0 and iSize,
24980 ** inclusive. Return a pointer to the new object. Return NULL if
24981 ** malloc fails.
24982 */
24983 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
24984 Bitvec *p;
24985 assert( sizeof(*p)==BITVEC_SZ );
24986 p = sqlite3MallocZero( sizeof(*p) );
24987 if( p ){
24988 p->iSize = iSize;
24989 }
24990 return p;
24991 }
24992
24993 /*
24994 ** Check to see if the i-th bit is set. Return true or false.
24995 ** If p is NULL (if the bitmap has not been created) or if
24996 ** i is out of range, then return false.
24997 */
24998 SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){
24999 assert( p!=0 );
25000 i--;
25001 if( i>=p->iSize ) return 0;
25002 while( p->iDivisor ){
25003 u32 bin = i/p->iDivisor;
25004 i = i%p->iDivisor;
25005 p = p->u.apSub[bin];
25006 if (!p) {
25007 return 0;
25008 }
25009 }
25010 if( p->iSize<=BITVEC_NBIT ){
25011 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
25012 } else{
25013 u32 h = BITVEC_HASH(i++);
25014 while( p->u.aHash[h] ){
25015 if( p->u.aHash[h]==i ) return 1;
25016 h = (h+1) % BITVEC_NINT;
25017 }
25018 return 0;
25019 }
25020 }
25021 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
25022 return p!=0 && sqlite3BitvecTestNotNull(p,i);
25023 }
25024
25025 /*
25026 ** Set the i-th bit. Return 0 on success and an error code if
25027 ** anything goes wrong.
25028 **
25029 ** This routine might cause sub-bitmaps to be allocated. Failing
25030 ** to get the memory needed to hold the sub-bitmap is the only
25031 ** that can go wrong with an insert, assuming p and i are valid.
25032 **
25033 ** The calling function must ensure that p is a valid Bitvec object
25034 ** and that the value for "i" is within range of the Bitvec object.
25035 ** Otherwise the behavior is undefined.
25036 */
25037 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
25038 u32 h;
25039 if( p==0 ) return SQLITE_OK;
25040 assert( i>0 );
25041 assert( i<=p->iSize );
25042 i--;
25043 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
25044 u32 bin = i/p->iDivisor;
25045 i = i%p->iDivisor;
25046 if( p->u.apSub[bin]==0 ){
25047 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
25048 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
25049 }
25050 p = p->u.apSub[bin];
25051 }
25052 if( p->iSize<=BITVEC_NBIT ){
25053 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
25054 return SQLITE_OK;
25055 }
25056 h = BITVEC_HASH(i++);
25057 /* if there wasn't a hash collision, and this doesn't */
25058 /* completely fill the hash, then just add it without */
25059 /* worring about sub-dividing and re-hashing. */
25060 if( !p->u.aHash[h] ){
25061 if (p->nSet<(BITVEC_NINT-1)) {
25062 goto bitvec_set_end;
25063 } else {
25064 goto bitvec_set_rehash;
25065 }
25066 }
25067 /* there was a collision, check to see if it's already */
25068 /* in hash, if not, try to find a spot for it */
25069 do {
25070 if( p->u.aHash[h]==i ) return SQLITE_OK;
25071 h++;
25072 if( h>=BITVEC_NINT ) h = 0;
25073 } while( p->u.aHash[h] );
25074 /* we didn't find it in the hash. h points to the first */
25075 /* available free spot. check to see if this is going to */
25076 /* make our hash too "full". */
25077 bitvec_set_rehash:
25078 if( p->nSet>=BITVEC_MXHASH ){
25079 unsigned int j;
25080 int rc;
25081 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
25082 if( aiValues==0 ){
25083 return SQLITE_NOMEM_BKPT;
25084 }else{
25085 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
25086 memset(p->u.apSub, 0, sizeof(p->u.apSub));
25087 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
25088 rc = sqlite3BitvecSet(p, i);
25089 for(j=0; j<BITVEC_NINT; j++){
25090 if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
25091 }
25092 sqlite3StackFree(0, aiValues);
25093 return rc;
25094 }
25095 }
25096 bitvec_set_end:
25097 p->nSet++;
25098 p->u.aHash[h] = i;
25099 return SQLITE_OK;
25100 }
25101
25102 /*
25103 ** Clear the i-th bit.
25104 **
25105 ** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
25106 ** that BitvecClear can use to rebuilt its hash table.
25107 */
25108 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
25109 if( p==0 ) return;
25110 assert( i>0 );
25111 i--;
25112 while( p->iDivisor ){
25113 u32 bin = i/p->iDivisor;
25114 i = i%p->iDivisor;
25115 p = p->u.apSub[bin];
25116 if (!p) {
25117 return;
25118 }
25119 }
25120 if( p->iSize<=BITVEC_NBIT ){
25121 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
25122 }else{
25123 unsigned int j;
25124 u32 *aiValues = pBuf;
25125 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
25126 memset(p->u.aHash, 0, sizeof(p->u.aHash));
25127 p->nSet = 0;
25128 for(j=0; j<BITVEC_NINT; j++){
25129 if( aiValues[j] && aiValues[j]!=(i+1) ){
25130 u32 h = BITVEC_HASH(aiValues[j]-1);
25131 p->nSet++;
25132 while( p->u.aHash[h] ){
25133 h++;
25134 if( h>=BITVEC_NINT ) h = 0;
25135 }
25136 p->u.aHash[h] = aiValues[j];
25137 }
25138 }
25139 }
25140 }
25141
25142 /*
25143 ** Destroy a bitmap object. Reclaim all memory used.
25144 */
25145 SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
25146 if( p==0 ) return;
25147 if( p->iDivisor ){
25148 unsigned int i;
25149 for(i=0; i<BITVEC_NPTR; i++){
25150 sqlite3BitvecDestroy(p->u.apSub[i]);
25151 }
25152 }
25153 sqlite3_free(p);
25154 }
25155
25156 /*
25157 ** Return the value of the iSize parameter specified when Bitvec *p
25158 ** was created.
25159 */
25160 SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
25161 return p->iSize;
25162 }
25163
25164 #ifndef SQLITE_UNTESTABLE
25165 /*
25166 ** Let V[] be an array of unsigned characters sufficient to hold
25167 ** up to N bits. Let I be an integer between 0 and N. 0<=I<N.
25168 ** Then the following macros can be used to set, clear, or test
25169 ** individual bits within V.
25170 */
25171 #define SETBIT(V,I) V[I>>3] |= (1<<(I&7))
25172 #define CLEARBIT(V,I) V[I>>3] &= ~(1<<(I&7))
25173 #define TESTBIT(V,I) (V[I>>3]&(1<<(I&7)))!=0
25174
25175 /*
25176 ** This routine runs an extensive test of the Bitvec code.
25177 **
25178 ** The input is an array of integers that acts as a program
25179 ** to test the Bitvec. The integers are opcodes followed
25180 ** by 0, 1, or 3 operands, depending on the opcode. Another
25181 ** opcode follows immediately after the last operand.
25182 **
25183 ** There are 6 opcodes numbered from 0 through 5. 0 is the
25184 ** "halt" opcode and causes the test to end.
25185 **
25186 ** 0 Halt and return the number of errors
25187 ** 1 N S X Set N bits beginning with S and incrementing by X
25188 ** 2 N S X Clear N bits beginning with S and incrementing by X
25189 ** 3 N Set N randomly chosen bits
25190 ** 4 N Clear N randomly chosen bits
25191 ** 5 N S X Set N bits from S increment X in array only, not in bitvec
25192 **
25193 ** The opcodes 1 through 4 perform set and clear operations are performed
25194 ** on both a Bitvec object and on a linear array of bits obtained from malloc.
25195 ** Opcode 5 works on the linear array only, not on the Bitvec.
25196 ** Opcode 5 is used to deliberately induce a fault in order to
25197 ** confirm that error detection works.
25198 **
25199 ** At the conclusion of the test the linear array is compared
25200 ** against the Bitvec object. If there are any differences,
25201 ** an error is returned. If they are the same, zero is returned.
25202 **
25203 ** If a memory allocation error occurs, return -1.
25204 */
25205 SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
25206 Bitvec *pBitvec = 0;
25207 unsigned char *pV = 0;
25208 int rc = -1;
25209 int i, nx, pc, op;
25210 void *pTmpSpace;
25211
25212 /* Allocate the Bitvec to be tested and a linear array of
25213 ** bits to act as the reference */
25214 pBitvec = sqlite3BitvecCreate( sz );
25215 pV = sqlite3MallocZero( (sz+7)/8 + 1 );
25216 pTmpSpace = sqlite3_malloc64(BITVEC_SZ);
25217 if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end;
25218
25219 /* NULL pBitvec tests */
25220 sqlite3BitvecSet(0, 1);
25221 sqlite3BitvecClear(0, 1, pTmpSpace);
25222
25223 /* Run the program */
25224 pc = 0;
25225 while( (op = aOp[pc])!=0 ){
25226 switch( op ){
25227 case 1:
25228 case 2:
25229 case 5: {
25230 nx = 4;
25231 i = aOp[pc+2] - 1;
25232 aOp[pc+2] += aOp[pc+3];
25233 break;
25234 }
25235 case 3:
25236 case 4:
25237 default: {
25238 nx = 2;
25239 sqlite3_randomness(sizeof(i), &i);
25240 break;
25241 }
25242 }
25243 if( (--aOp[pc+1]) > 0 ) nx = 0;
25244 pc += nx;
25245 i = (i & 0x7fffffff)%sz;
25246 if( (op & 1)!=0 ){
25247 SETBIT(pV, (i+1));
25248 if( op!=5 ){
25249 if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
25250 }
25251 }else{
25252 CLEARBIT(pV, (i+1));
25253 sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
25254 }
25255 }
25256
25257 /* Test to make sure the linear array exactly matches the
25258 ** Bitvec object. Start with the assumption that they do
25259 ** match (rc==0). Change rc to non-zero if a discrepancy
25260 ** is found.
25261 */
25262 rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
25263 + sqlite3BitvecTest(pBitvec, 0)
25264 + (sqlite3BitvecSize(pBitvec) - sz);
25265 for(i=1; i<=sz; i++){
25266 if( (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
25267 rc = i;
25268 break;
25269 }
25270 }
25271
25272 /* Free allocated structure */
25273 bitvec_end:
25274 sqlite3_free(pTmpSpace);
25275 sqlite3_free(pV);
25276 sqlite3BitvecDestroy(pBitvec);
25277 return rc;
25278 }
25279 #endif /* SQLITE_UNTESTABLE */
25280
25281 /************** End of bitvec.c **********************************************/
25282 /************** Begin file pcache.c ******************************************/
25283 /*
25284 ** 2008 August 05
25285 **
25286 ** The author disclaims copyright to this source code. In place of
25287 ** a legal notice, here is a blessing:
25288 **
25289 ** May you do good and not evil.
25290 ** May you find forgiveness for yourself and forgive others.
25291 ** May you share freely, never taking more than you give.
25292 **
25293 *************************************************************************
25294 ** This file implements that page cache.
25295 */
25296 /* #include "sqliteInt.h" */
25297
25298 /*
25299 ** A complete page cache is an instance of this structure. Every
25300 ** entry in the cache holds a single page of the database file. The
25301 ** btree layer only operates on the cached copy of the database pages.
25302 **
25303 ** A page cache entry is "clean" if it exactly matches what is currently
25304 ** on disk. A page is "dirty" if it has been modified and needs to be
25305 ** persisted to disk.
25306 **
25307 ** pDirty, pDirtyTail, pSynced:
25308 ** All dirty pages are linked into the doubly linked list using
25309 ** PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order
25310 ** such that p was added to the list more recently than p->pDirtyNext.
25311 ** PCache.pDirty points to the first (newest) element in the list and
25312 ** pDirtyTail to the last (oldest).
25313 **
25314 ** The PCache.pSynced variable is used to optimize searching for a dirty
25315 ** page to eject from the cache mid-transaction. It is better to eject
25316 ** a page that does not require a journal sync than one that does.
25317 ** Therefore, pSynced is maintained to that it *almost* always points
25318 ** to either the oldest page in the pDirty/pDirtyTail list that has a
25319 ** clear PGHDR_NEED_SYNC flag or to a page that is older than this one
25320 ** (so that the right page to eject can be found by following pDirtyPrev
25321 ** pointers).
25322 */
25323 struct PCache {
25324 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
25325 PgHdr *pSynced; /* Last synced page in dirty page list */
25326 int nRefSum; /* Sum of ref counts over all pages */
25327 int szCache; /* Configured cache size */
25328 int szSpill; /* Size before spilling occurs */
25329 int szPage; /* Size of every page in this cache */
25330 int szExtra; /* Size of extra space for each page */
25331 u8 bPurgeable; /* True if pages are on backing store */
25332 u8 eCreate; /* eCreate value for for xFetch() */
25333 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
25334 void *pStress; /* Argument to xStress */
25335 sqlite3_pcache *pCache; /* Pluggable cache module */
25336 };
25337
25338 /********************************** Test and Debug Logic **********************/
25339 /*
25340 ** Debug tracing macros. Enable by by changing the "0" to "1" and
25341 ** recompiling.
25342 **
25343 ** When sqlite3PcacheTrace is 1, single line trace messages are issued.
25344 ** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries
25345 ** is displayed for many operations, resulting in a lot of output.
25346 */
25347 #if defined(SQLITE_DEBUG) && 0
25348 int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */
25349 int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */
25350 # define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
25351 void pcacheDump(PCache *pCache){
25352 int N;
25353 int i, j;
25354 sqlite3_pcache_page *pLower;
25355 PgHdr *pPg;
25356 unsigned char *a;
25357
25358 if( sqlite3PcacheTrace<2 ) return;
25359 if( pCache->pCache==0 ) return;
25360 N = sqlite3PcachePagecount(pCache);
25361 if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
25362 for(i=1; i<=N; i++){
25363 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
25364 if( pLower==0 ) continue;
25365 pPg = (PgHdr*)pLower->pExtra;
25366 printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
25367 a = (unsigned char *)pLower->pBuf;
25368 for(j=0; j<12; j++) printf("%02x", a[j]);
25369 printf("\n");
25370 if( pPg->pPage==0 ){
25371 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
25372 }
25373 }
25374 }
25375 #else
25376 # define pcacheTrace(X)
25377 # define pcacheDump(X)
25378 #endif
25379
25380 /*
25381 ** Check invariants on a PgHdr entry. Return true if everything is OK.
25382 ** Return false if any invariant is violated.
25383 **
25384 ** This routine is for use inside of assert() statements only. For
25385 ** example:
25386 **
25387 ** assert( sqlite3PcachePageSanity(pPg) );
25388 */
25389 #if SQLITE_DEBUG
25390 SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr *pPg){
25391 PCache *pCache;
25392 assert( pPg!=0 );
25393 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
25394 pCache = pPg->pCache;
25395 assert( pCache!=0 ); /* Every page has an associated PCache */
25396 if( pPg->flags & PGHDR_CLEAN ){
25397 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
25398 assert( pCache->pDirty!=pPg ); /* CLEAN pages not on dirty list */
25399 assert( pCache->pDirtyTail!=pPg );
25400 }
25401 /* WRITEABLE pages must also be DIRTY */
25402 if( pPg->flags & PGHDR_WRITEABLE ){
25403 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
25404 }
25405 /* NEED_SYNC can be set independently of WRITEABLE. This can happen,
25406 ** for example, when using the sqlite3PagerDontWrite() optimization:
25407 ** (1) Page X is journalled, and gets WRITEABLE and NEED_SEEK.
25408 ** (2) Page X moved to freelist, WRITEABLE is cleared
25409 ** (3) Page X reused, WRITEABLE is set again
25410 ** If NEED_SYNC had been cleared in step 2, then it would not be reset
25411 ** in step 3, and page might be written into the database without first
25412 ** syncing the rollback journal, which might cause corruption on a power
25413 ** loss.
25414 **
25415 ** Another example is when the database page size is smaller than the
25416 ** disk sector size. When any page of a sector is journalled, all pages
25417 ** in that sector are marked NEED_SYNC even if they are still CLEAN, just
25418 ** in case they are later modified, since all pages in the same sector
25419 ** must be journalled and synced before any of those pages can be safely
25420 ** written.
25421 */
25422 return 1;
25423 }
25424 #endif /* SQLITE_DEBUG */
25425
25426
25427 /********************************** Linked List Management ********************/
25428
25429 /* Allowed values for second argument to pcacheManageDirtyList() */
25430 #define PCACHE_DIRTYLIST_REMOVE 1 /* Remove pPage from dirty list */
25431 #define PCACHE_DIRTYLIST_ADD 2 /* Add pPage to the dirty list */
25432 #define PCACHE_DIRTYLIST_FRONT 3 /* Move pPage to the front of the list */
25433
25434 /*
25435 ** Manage pPage's participation on the dirty list. Bits of the addRemove
25436 ** argument determines what operation to do. The 0x01 bit means first
25437 ** remove pPage from the dirty list. The 0x02 means add pPage back to
25438 ** the dirty list. Doing both moves pPage to the front of the dirty list.
25439 */
25440 static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
25441 PCache *p = pPage->pCache;
25442
25443 pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
25444 addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
25445 pPage->pgno));
25446 if( addRemove & PCACHE_DIRTYLIST_REMOVE ){
25447 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
25448 assert( pPage->pDirtyPrev || pPage==p->pDirty );
25449
25450 /* Update the PCache1.pSynced variable if necessary. */
25451 if( p->pSynced==pPage ){
25452 p->pSynced = pPage->pDirtyPrev;
25453 }
25454
25455 if( pPage->pDirtyNext ){
25456 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
25457 }else{
25458 assert( pPage==p->pDirtyTail );
25459 p->pDirtyTail = pPage->pDirtyPrev;
25460 }
25461 if( pPage->pDirtyPrev ){
25462 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
25463 }else{
25464 /* If there are now no dirty pages in the cache, set eCreate to 2.
25465 ** This is an optimization that allows sqlite3PcacheFetch() to skip
25466 ** searching for a dirty page to eject from the cache when it might
25467 ** otherwise have to. */
25468 assert( pPage==p->pDirty );
25469 p->pDirty = pPage->pDirtyNext;
25470 assert( p->bPurgeable || p->eCreate==2 );
25471 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
25472 assert( p->bPurgeable==0 || p->eCreate==1 );
25473 p->eCreate = 2;
25474 }
25475 }
25476 pPage->pDirtyNext = 0;
25477 pPage->pDirtyPrev = 0;
25478 }
25479 if( addRemove & PCACHE_DIRTYLIST_ADD ){
25480 assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
25481
25482 pPage->pDirtyNext = p->pDirty;
25483 if( pPage->pDirtyNext ){
25484 assert( pPage->pDirtyNext->pDirtyPrev==0 );
25485 pPage->pDirtyNext->pDirtyPrev = pPage;
25486 }else{
25487 p->pDirtyTail = pPage;
25488 if( p->bPurgeable ){
25489 assert( p->eCreate==2 );
25490 p->eCreate = 1;
25491 }
25492 }
25493 p->pDirty = pPage;
25494
25495 /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
25496 ** pSynced to point to it. Checking the NEED_SYNC flag is an
25497 ** optimization, as if pSynced points to a page with the NEED_SYNC
25498 ** flag set sqlite3PcacheFetchStress() searches through all newer
25499 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
25500 if( !p->pSynced
25501 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
25502 ){
25503 p->pSynced = pPage;
25504 }
25505 }
25506 pcacheDump(p);
25507 }
25508
25509 /*
25510 ** Wrapper around the pluggable caches xUnpin method. If the cache is
25511 ** being used for an in-memory database, this function is a no-op.
25512 */
25513 static void pcacheUnpin(PgHdr *p){
25514 if( p->pCache->bPurgeable ){
25515 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
25516 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
25517 pcacheDump(p->pCache);
25518 }
25519 }
25520
25521 /*
25522 ** Compute the number of pages of cache requested. p->szCache is the
25523 ** cache size requested by the "PRAGMA cache_size" statement.
25524 */
25525 static int numberOfCachePages(PCache *p){
25526 if( p->szCache>=0 ){
25527 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
25528 ** suggested cache size is set to N. */
25529 return p->szCache;
25530 }else{
25531 /* IMPLEMENTATION-OF: R-61436-13639 If the argument N is negative, then
25532 ** the number of cache pages is adjusted to use approximately abs(N*1024)
25533 ** bytes of memory. */
25534 return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
25535 }
25536 }
25537
25538 /*************************************************** General Interfaces ******
25539 **
25540 ** Initialize and shutdown the page cache subsystem. Neither of these
25541 ** functions are threadsafe.
25542 */
25543 SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
25544 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
25545 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
25546 ** built-in default page cache is used instead of the application defined
25547 ** page cache. */
25548 sqlite3PCacheSetDefault();
25549 }
25550 return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
25551 }
25552 SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
25553 if( sqlite3GlobalConfig.pcache2.xShutdown ){
25554 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
25555 sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
25556 }
25557 }
25558
25559 /*
25560 ** Return the size in bytes of a PCache object.
25561 */
25562 SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
25563
25564 /*
25565 ** Create a new PCache object. Storage space to hold the object
25566 ** has already been allocated and is passed in as the p pointer.
25567 ** The caller discovers how much space needs to be allocated by
25568 ** calling sqlite3PcacheSize().
25569 **
25570 ** szExtra is some extra space allocated for each page. The first
25571 ** 8 bytes of the extra space will be zeroed as the page is allocated,
25572 ** but remaining content will be uninitialized. Though it is opaque
25573 ** to this module, the extra space really ends up being the MemPage
25574 ** structure in the pager.
25575 */
25576 SQLITE_PRIVATE int sqlite3PcacheOpen(
25577 int szPage, /* Size of every page */
25578 int szExtra, /* Extra space associated with each page */
25579 int bPurgeable, /* True if pages are on backing store */
25580 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
25581 void *pStress, /* Argument to xStress */
25582 PCache *p /* Preallocated space for the PCache */
25583 ){
25584 memset(p, 0, sizeof(PCache));
25585 p->szPage = 1;
25586 p->szExtra = szExtra;
25587 assert( szExtra>=8 ); /* First 8 bytes will be zeroed */
25588 p->bPurgeable = bPurgeable;
25589 p->eCreate = 2;
25590 p->xStress = xStress;
25591 p->pStress = pStress;
25592 p->szCache = 100;
25593 p->szSpill = 1;
25594 pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
25595 return sqlite3PcacheSetPageSize(p, szPage);
25596 }
25597
25598 /*
25599 ** Change the page size for PCache object. The caller must ensure that there
25600 ** are no outstanding page references when this function is called.
25601 */
25602 SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
25603 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
25604 if( pCache->szPage ){
25605 sqlite3_pcache *pNew;
25606 pNew = sqlite3GlobalConfig.pcache2.xCreate(
25607 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
25608 pCache->bPurgeable
25609 );
25610 if( pNew==0 ) return SQLITE_NOMEM_BKPT;
25611 sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
25612 if( pCache->pCache ){
25613 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
25614 }
25615 pCache->pCache = pNew;
25616 pCache->szPage = szPage;
25617 pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
25618 }
25619 return SQLITE_OK;
25620 }
25621
25622 /*
25623 ** Try to obtain a page from the cache.
25624 **
25625 ** This routine returns a pointer to an sqlite3_pcache_page object if
25626 ** such an object is already in cache, or if a new one is created.
25627 ** This routine returns a NULL pointer if the object was not in cache
25628 ** and could not be created.
25629 **
25630 ** The createFlags should be 0 to check for existing pages and should
25631 ** be 3 (not 1, but 3) to try to create a new page.
25632 **
25633 ** If the createFlag is 0, then NULL is always returned if the page
25634 ** is not already in the cache. If createFlag is 1, then a new page
25635 ** is created only if that can be done without spilling dirty pages
25636 ** and without exceeding the cache size limit.
25637 **
25638 ** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
25639 ** initialize the sqlite3_pcache_page object and convert it into a
25640 ** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
25641 ** routines are split this way for performance reasons. When separated
25642 ** they can both (usually) operate without having to push values to
25643 ** the stack on entry and pop them back off on exit, which saves a
25644 ** lot of pushing and popping.
25645 */
25646 SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch(
25647 PCache *pCache, /* Obtain the page from this cache */
25648 Pgno pgno, /* Page number to obtain */
25649 int createFlag /* If true, create page if it does not exist already */
25650 ){
25651 int eCreate;
25652 sqlite3_pcache_page *pRes;
25653
25654 assert( pCache!=0 );
25655 assert( pCache->pCache!=0 );
25656 assert( createFlag==3 || createFlag==0 );
25657 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
25658
25659 /* eCreate defines what to do if the page does not exist.
25660 ** 0 Do not allocate a new page. (createFlag==0)
25661 ** 1 Allocate a new page if doing so is inexpensive.
25662 ** (createFlag==1 AND bPurgeable AND pDirty)
25663 ** 2 Allocate a new page even it doing so is difficult.
25664 ** (createFlag==1 AND !(bPurgeable AND pDirty)
25665 */
25666 eCreate = createFlag & pCache->eCreate;
25667 assert( eCreate==0 || eCreate==1 || eCreate==2 );
25668 assert( createFlag==0 || pCache->eCreate==eCreate );
25669 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
25670 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
25671 pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno,
25672 createFlag?" create":"",pRes));
25673 return pRes;
25674 }
25675
25676 /*
25677 ** If the sqlite3PcacheFetch() routine is unable to allocate a new
25678 ** page because no clean pages are available for reuse and the cache
25679 ** size limit has been reached, then this routine can be invoked to
25680 ** try harder to allocate a page. This routine might invoke the stress
25681 ** callback to spill dirty pages to the journal. It will then try to
25682 ** allocate the new page and will only fail to allocate a new page on
25683 ** an OOM error.
25684 **
25685 ** This routine should be invoked only after sqlite3PcacheFetch() fails.
25686 */
25687 SQLITE_PRIVATE int sqlite3PcacheFetchStress(
25688 PCache *pCache, /* Obtain the page from this cache */
25689 Pgno pgno, /* Page number to obtain */
25690 sqlite3_pcache_page **ppPage /* Write result here */
25691 ){
25692 PgHdr *pPg;
25693 if( pCache->eCreate==2 ) return 0;
25694
25695 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
25696 /* Find a dirty page to write-out and recycle. First try to find a
25697 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
25698 ** cleared), but if that is not possible settle for any other
25699 ** unreferenced dirty page.
25700 **
25701 ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
25702 ** flag is currently referenced, then the following may leave pSynced
25703 ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
25704 ** cleared). This is Ok, as pSynced is just an optimization. */
25705 for(pPg=pCache->pSynced;
25706 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
25707 pPg=pPg->pDirtyPrev
25708 );
25709 pCache->pSynced = pPg;
25710 if( !pPg ){
25711 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
25712 }
25713 if( pPg ){
25714 int rc;
25715 #ifdef SQLITE_LOG_CACHE_SPILL
25716 sqlite3_log(SQLITE_FULL,
25717 "spill page %d making room for %d - cache used: %d/%d",
25718 pPg->pgno, pgno,
25719 sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
25720 numberOfCachePages(pCache));
25721 #endif
25722 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
25723 rc = pCache->xStress(pCache->pStress, pPg);
25724 pcacheDump(pCache);
25725 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
25726 return rc;
25727 }
25728 }
25729 }
25730 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
25731 return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
25732 }
25733
25734 /*
25735 ** This is a helper routine for sqlite3PcacheFetchFinish()
25736 **
25737 ** In the uncommon case where the page being fetched has not been
25738 ** initialized, this routine is invoked to do the initialization.
25739 ** This routine is broken out into a separate function since it
25740 ** requires extra stack manipulation that can be avoided in the common
25741 ** case.
25742 */
25743 static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
25744 PCache *pCache, /* Obtain the page from this cache */
25745 Pgno pgno, /* Page number obtained */
25746 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
25747 ){
25748 PgHdr *pPgHdr;
25749 assert( pPage!=0 );
25750 pPgHdr = (PgHdr*)pPage->pExtra;
25751 assert( pPgHdr->pPage==0 );
25752 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
25753 pPgHdr->pPage = pPage;
25754 pPgHdr->pData = pPage->pBuf;
25755 pPgHdr->pExtra = (void *)&pPgHdr[1];
25756 memset(pPgHdr->pExtra, 0, 8);
25757 pPgHdr->pCache = pCache;
25758 pPgHdr->pgno = pgno;
25759 pPgHdr->flags = PGHDR_CLEAN;
25760 return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
25761 }
25762
25763 /*
25764 ** This routine converts the sqlite3_pcache_page object returned by
25765 ** sqlite3PcacheFetch() into an initialized PgHdr object. This routine
25766 ** must be called after sqlite3PcacheFetch() in order to get a usable
25767 ** result.
25768 */
25769 SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish(
25770 PCache *pCache, /* Obtain the page from this cache */
25771 Pgno pgno, /* Page number obtained */
25772 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
25773 ){
25774 PgHdr *pPgHdr;
25775
25776 assert( pPage!=0 );
25777 pPgHdr = (PgHdr *)pPage->pExtra;
25778
25779 if( !pPgHdr->pPage ){
25780 return pcacheFetchFinishWithInit(pCache, pgno, pPage);
25781 }
25782 pCache->nRefSum++;
25783 pPgHdr->nRef++;
25784 assert( sqlite3PcachePageSanity(pPgHdr) );
25785 return pPgHdr;
25786 }
25787
25788 /*
25789 ** Decrement the reference count on a page. If the page is clean and the
25790 ** reference count drops to 0, then it is made eligible for recycling.
25791 */
25792 SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
25793 assert( p->nRef>0 );
25794 p->pCache->nRefSum--;
25795 if( (--p->nRef)==0 ){
25796 if( p->flags&PGHDR_CLEAN ){
25797 pcacheUnpin(p);
25798 }else if( p->pDirtyPrev!=0 ){ /*OPTIMIZATION-IF-FALSE*/
25799 /* Move the page to the head of the dirty list. If p->pDirtyPrev==0,
25800 ** then page p is already at the head of the dirty list and the
25801 ** following call would be a no-op. Hence the OPTIMIZATION-IF-FALSE
25802 ** tag above. */
25803 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
25804 }
25805 }
25806 }
25807
25808 /*
25809 ** Increase the reference count of a supplied page by 1.
25810 */
25811 SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
25812 assert(p->nRef>0);
25813 assert( sqlite3PcachePageSanity(p) );
25814 p->nRef++;
25815 p->pCache->nRefSum++;
25816 }
25817
25818 /*
25819 ** Drop a page from the cache. There must be exactly one reference to the
25820 ** page. This function deletes that reference, so after it returns the
25821 ** page pointed to by p is invalid.
25822 */
25823 SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
25824 assert( p->nRef==1 );
25825 assert( sqlite3PcachePageSanity(p) );
25826 if( p->flags&PGHDR_DIRTY ){
25827 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
25828 }
25829 p->pCache->nRefSum--;
25830 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
25831 }
25832
25833 /*
25834 ** Make sure the page is marked as dirty. If it isn't dirty already,
25835 ** make it so.
25836 */
25837 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
25838 assert( p->nRef>0 );
25839 assert( sqlite3PcachePageSanity(p) );
25840 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
25841 p->flags &= ~PGHDR_DONT_WRITE;
25842 if( p->flags & PGHDR_CLEAN ){
25843 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
25844 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
25845 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
25846 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
25847 }
25848 assert( sqlite3PcachePageSanity(p) );
25849 }
25850 }
25851
25852 /*
25853 ** Make sure the page is marked as clean. If it isn't clean already,
25854 ** make it so.
25855 */
25856 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
25857 assert( sqlite3PcachePageSanity(p) );
25858 if( ALWAYS((p->flags & PGHDR_DIRTY)!=0) ){
25859 assert( (p->flags & PGHDR_CLEAN)==0 );
25860 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
25861 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
25862 p->flags |= PGHDR_CLEAN;
25863 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
25864 assert( sqlite3PcachePageSanity(p) );
25865 if( p->nRef==0 ){
25866 pcacheUnpin(p);
25867 }
25868 }
25869 }
25870
25871 /*
25872 ** Make every page in the cache clean.
25873 */
25874 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
25875 PgHdr *p;
25876 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
25877 while( (p = pCache->pDirty)!=0 ){
25878 sqlite3PcacheMakeClean(p);
25879 }
25880 }
25881
25882 /*
25883 ** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
25884 */
25885 SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache *pCache){
25886 PgHdr *p;
25887 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
25888 for(p=pCache->pDirty; p; p=p->pDirtyNext){
25889 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
25890 }
25891 pCache->pSynced = pCache->pDirtyTail;
25892 }
25893
25894 /*
25895 ** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
25896 */
25897 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
25898 PgHdr *p;
25899 for(p=pCache->pDirty; p; p=p->pDirtyNext){
25900 p->flags &= ~PGHDR_NEED_SYNC;
25901 }
25902 pCache->pSynced = pCache->pDirtyTail;
25903 }
25904
25905 /*
25906 ** Change the page number of page p to newPgno.
25907 */
25908 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
25909 PCache *pCache = p->pCache;
25910 assert( p->nRef>0 );
25911 assert( newPgno>0 );
25912 assert( sqlite3PcachePageSanity(p) );
25913 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
25914 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
25915 p->pgno = newPgno;
25916 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
25917 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
25918 }
25919 }
25920
25921 /*
25922 ** Drop every cache entry whose page number is greater than "pgno". The
25923 ** caller must ensure that there are no outstanding references to any pages
25924 ** other than page 1 with a page number greater than pgno.
25925 **
25926 ** If there is a reference to page 1 and the pgno parameter passed to this
25927 ** function is 0, then the data area associated with page 1 is zeroed, but
25928 ** the page object is not dropped.
25929 */
25930 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
25931 if( pCache->pCache ){
25932 PgHdr *p;
25933 PgHdr *pNext;
25934 pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
25935 for(p=pCache->pDirty; p; p=pNext){
25936 pNext = p->pDirtyNext;
25937 /* This routine never gets call with a positive pgno except right
25938 ** after sqlite3PcacheCleanAll(). So if there are dirty pages,
25939 ** it must be that pgno==0.
25940 */
25941 assert( p->pgno>0 );
25942 if( p->pgno>pgno ){
25943 assert( p->flags&PGHDR_DIRTY );
25944 sqlite3PcacheMakeClean(p);
25945 }
25946 }
25947 if( pgno==0 && pCache->nRefSum ){
25948 sqlite3_pcache_page *pPage1;
25949 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
25950 if( ALWAYS(pPage1) ){ /* Page 1 is always available in cache, because
25951 ** pCache->nRefSum>0 */
25952 memset(pPage1->pBuf, 0, pCache->szPage);
25953 pgno = 1;
25954 }
25955 }
25956 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
25957 }
25958 }
25959
25960 /*
25961 ** Close a cache.
25962 */
25963 SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
25964 assert( pCache->pCache!=0 );
25965 pcacheTrace(("%p.CLOSE\n",pCache));
25966 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
25967 }
25968
25969 /*
25970 ** Discard the contents of the cache.
25971 */
25972 SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
25973 sqlite3PcacheTruncate(pCache, 0);
25974 }
25975
25976 /*
25977 ** Merge two lists of pages connected by pDirty and in pgno order.
25978 ** Do not bother fixing the pDirtyPrev pointers.
25979 */
25980 static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
25981 PgHdr result, *pTail;
25982 pTail = &result;
25983 assert( pA!=0 && pB!=0 );
25984 for(;;){
25985 if( pA->pgno<pB->pgno ){
25986 pTail->pDirty = pA;
25987 pTail = pA;
25988 pA = pA->pDirty;
25989 if( pA==0 ){
25990 pTail->pDirty = pB;
25991 break;
25992 }
25993 }else{
25994 pTail->pDirty = pB;
25995 pTail = pB;
25996 pB = pB->pDirty;
25997 if( pB==0 ){
25998 pTail->pDirty = pA;
25999 break;
26000 }
26001 }
26002 }
26003 return result.pDirty;
26004 }
26005
26006 /*
26007 ** Sort the list of pages in accending order by pgno. Pages are
26008 ** connected by pDirty pointers. The pDirtyPrev pointers are
26009 ** corrupted by this sort.
26010 **
26011 ** Since there cannot be more than 2^31 distinct pages in a database,
26012 ** there cannot be more than 31 buckets required by the merge sorter.
26013 ** One extra bucket is added to catch overflow in case something
26014 ** ever changes to make the previous sentence incorrect.
26015 */
26016 #define N_SORT_BUCKET 32
26017 static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
26018 PgHdr *a[N_SORT_BUCKET], *p;
26019 int i;
26020 memset(a, 0, sizeof(a));
26021 while( pIn ){
26022 p = pIn;
26023 pIn = p->pDirty;
26024 p->pDirty = 0;
26025 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
26026 if( a[i]==0 ){
26027 a[i] = p;
26028 break;
26029 }else{
26030 p = pcacheMergeDirtyList(a[i], p);
26031 a[i] = 0;
26032 }
26033 }
26034 if( NEVER(i==N_SORT_BUCKET-1) ){
26035 /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
26036 ** the input list. But that is impossible.
26037 */
26038 a[i] = pcacheMergeDirtyList(a[i], p);
26039 }
26040 }
26041 p = a[0];
26042 for(i=1; i<N_SORT_BUCKET; i++){
26043 if( a[i]==0 ) continue;
26044 p = p ? pcacheMergeDirtyList(p, a[i]) : a[i];
26045 }
26046 return p;
26047 }
26048
26049 /*
26050 ** Return a list of all dirty pages in the cache, sorted by page number.
26051 */
26052 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
26053 PgHdr *p;
26054 for(p=pCache->pDirty; p; p=p->pDirtyNext){
26055 p->pDirty = p->pDirtyNext;
26056 }
26057 return pcacheSortDirtyList(pCache->pDirty);
26058 }
26059
26060 /*
26061 ** Return the total number of references to all pages held by the cache.
26062 **
26063 ** This is not the total number of pages referenced, but the sum of the
26064 ** reference count for all pages.
26065 */
26066 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
26067 return pCache->nRefSum;
26068 }
26069
26070 /*
26071 ** Return the number of references to the page supplied as an argument.
26072 */
26073 SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
26074 return p->nRef;
26075 }
26076
26077 /*
26078 ** Return the total number of pages in the cache.
26079 */
26080 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
26081 assert( pCache->pCache!=0 );
26082 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
26083 }
26084
26085 #ifdef SQLITE_TEST
26086 /*
26087 ** Get the suggested cache-size value.
26088 */
26089 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
26090 return numberOfCachePages(pCache);
26091 }
26092 #endif
26093
26094 /*
26095 ** Set the suggested cache-size value.
26096 */
26097 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
26098 assert( pCache->pCache!=0 );
26099 pCache->szCache = mxPage;
26100 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
26101 numberOfCachePages(pCache));
26102 }
26103
26104 /*
26105 ** Set the suggested cache-spill value. Make no changes if if the
26106 ** argument is zero. Return the effective cache-spill size, which will
26107 ** be the larger of the szSpill and szCache.
26108 */
26109 SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
26110 int res;
26111 assert( p->pCache!=0 );
26112 if( mxPage ){
26113 if( mxPage<0 ){
26114 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
26115 }
26116 p->szSpill = mxPage;
26117 }
26118 res = numberOfCachePages(p);
26119 if( res<p->szSpill ) res = p->szSpill;
26120 return res;
26121 }
26122
26123 /*
26124 ** Free up as much memory as possible from the page cache.
26125 */
26126 SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
26127 assert( pCache->pCache!=0 );
26128 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
26129 }
26130
26131 /*
26132 ** Return the size of the header added by this middleware layer
26133 ** in the page-cache hierarchy.
26134 */
26135 SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }
26136
26137 /*
26138 ** Return the number of dirty pages currently in the cache, as a percentage
26139 ** of the configured cache size.
26140 */
26141 SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache *pCache){
26142 PgHdr *pDirty;
26143 int nDirty = 0;
26144 int nCache = numberOfCachePages(pCache);
26145 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
26146 return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
26147 }
26148
26149 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
26150 /*
26151 ** For all dirty pages currently in the cache, invoke the specified
26152 ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
26153 ** defined.
26154 */
26155 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHd r *)){
26156 PgHdr *pDirty;
26157 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
26158 xIter(pDirty);
26159 }
26160 }
26161 #endif
26162
26163 /************** End of pcache.c **********************************************/
26164
26165 /* Chain include. */
26166 #include "sqlite3.02.c"
OLDNEW
« no previous file with comments | « third_party/sqlite/amalgamation/sqlite3.00.c ('k') | third_party/sqlite/amalgamation/sqlite3.02.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698