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

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

Issue 2767473003: NCI: CL to trybot SQLite 3.17 + clang fix. (Closed)
Patch Set: 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): Removing clang support because on many platforms it generates a
10109 ** link error for this intrinsic:
10110 ** undefined reference to '__mulodi4'
10111 ** http://crbug.com/701524
10112 */
10113 #if GCC_VERSION>=5004000
10114 return __builtin_mul_overflow(*pA, iB, pA);
10115 #else
10116 i64 iA = *pA;
10117 if( iB>0 ){
10118 if( iA>LARGEST_INT64/iB ) return 1;
10119 if( iA<SMALLEST_INT64/iB ) return 1;
10120 }else if( iB<0 ){
10121 if( iA>0 ){
10122 if( iB<SMALLEST_INT64/iA ) return 1;
10123 }else if( iA<0 ){
10124 if( iB==SMALLEST_INT64 ) return 1;
10125 if( iA==SMALLEST_INT64 ) return 1;
10126 if( -iA>LARGEST_INT64/-iB ) return 1;
10127 }
10128 }
10129 *pA = iA*iB;
10130 return 0;
10131 #endif
10132 }
10133
10134 /*
10135 ** Compute the absolute value of a 32-bit signed integer, of possible. Or
10136 ** if the integer has a value of -2147483648, return +2147483647
10137 */
10138 SQLITE_PRIVATE int sqlite3AbsInt32(int x){
10139 if( x>=0 ) return x;
10140 if( x==(int)0x80000000 ) return 0x7fffffff;
10141 return -x;
10142 }
10143
10144 #ifdef SQLITE_ENABLE_8_3_NAMES
10145 /*
10146 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
10147 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
10148 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
10149 ** three characters, then shorten the suffix on z[] to be the last three
10150 ** characters of the original suffix.
10151 **
10152 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
10153 ** do the suffix shortening regardless of URI parameter.
10154 **
10155 ** Examples:
10156 **
10157 ** test.db-journal => test.nal
10158 ** test.db-wal => test.wal
10159 ** test.db-shm => test.shm
10160 ** test.db-mj7f3319fa => test.9fa
10161 */
10162 SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
10163 #if SQLITE_ENABLE_8_3_NAMES<2
10164 if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
10165 #endif
10166 {
10167 int i, sz;
10168 sz = sqlite3Strlen30(z);
10169 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
10170 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
10171 }
10172 }
10173 #endif
10174
10175 /*
10176 ** Find (an approximate) sum of two LogEst values. This computation is
10177 ** not a simple "+" operator because LogEst is stored as a logarithmic
10178 ** value.
10179 **
10180 */
10181 SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
10182 static const unsigned char x[] = {
10183 10, 10, /* 0,1 */
10184 9, 9, /* 2,3 */
10185 8, 8, /* 4,5 */
10186 7, 7, 7, /* 6,7,8 */
10187 6, 6, 6, /* 9,10,11 */
10188 5, 5, 5, /* 12-14 */
10189 4, 4, 4, 4, /* 15-18 */
10190 3, 3, 3, 3, 3, 3, /* 19-24 */
10191 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
10192 };
10193 if( a>=b ){
10194 if( a>b+49 ) return a;
10195 if( a>b+31 ) return a+1;
10196 return a+x[a-b];
10197 }else{
10198 if( b>a+49 ) return b;
10199 if( b>a+31 ) return b+1;
10200 return b+x[b-a];
10201 }
10202 }
10203
10204 /*
10205 ** Convert an integer into a LogEst. In other words, compute an
10206 ** approximation for 10*log2(x).
10207 */
10208 SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
10209 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
10210 LogEst y = 40;
10211 if( x<8 ){
10212 if( x<2 ) return 0;
10213 while( x<8 ){ y -= 10; x <<= 1; }
10214 }else{
10215 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/
10216 while( x>15 ){ y += 10; x >>= 1; }
10217 }
10218 return a[x&7] + y - 10;
10219 }
10220
10221 #ifndef SQLITE_OMIT_VIRTUALTABLE
10222 /*
10223 ** Convert a double into a LogEst
10224 ** In other words, compute an approximation for 10*log2(x).
10225 */
10226 SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double x){
10227 u64 a;
10228 LogEst e;
10229 assert( sizeof(x)==8 && sizeof(a)==8 );
10230 if( x<=1 ) return 0;
10231 if( x<=2000000000 ) return sqlite3LogEst((u64)x);
10232 memcpy(&a, &x, 8);
10233 e = (a>>52) - 1022;
10234 return e*10;
10235 }
10236 #endif /* SQLITE_OMIT_VIRTUALTABLE */
10237
10238 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
10239 defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \
10240 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
10241 /*
10242 ** Convert a LogEst into an integer.
10243 **
10244 ** Note that this routine is only used when one or more of various
10245 ** non-standard compile-time options is enabled.
10246 */
10247 SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst x){
10248 u64 n;
10249 n = x%10;
10250 x /= 10;
10251 if( n>=5 ) n -= 2;
10252 else if( n>=1 ) n -= 1;
10253 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
10254 defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
10255 if( x>60 ) return (u64)LARGEST_INT64;
10256 #else
10257 /* If only SQLITE_ENABLE_STAT3_OR_STAT4 is on, then the largest input
10258 ** possible to this routine is 310, resulting in a maximum x of 31 */
10259 assert( x<=60 );
10260 #endif
10261 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
10262 }
10263 #endif /* defined SCANSTAT or STAT4 or ESTIMATED_ROWS */
10264
10265 /*
10266 ** Add a new name/number pair to a VList. This might require that the
10267 ** VList object be reallocated, so return the new VList. If an OOM
10268 ** error occurs, the original VList returned and the
10269 ** db->mallocFailed flag is set.
10270 **
10271 ** A VList is really just an array of integers. To destroy a VList,
10272 ** simply pass it to sqlite3DbFree().
10273 **
10274 ** The first integer is the number of integers allocated for the whole
10275 ** VList. The second integer is the number of integers actually used.
10276 ** Each name/number pair is encoded by subsequent groups of 3 or more
10277 ** integers.
10278 **
10279 ** Each name/number pair starts with two integers which are the numeric
10280 ** value for the pair and the size of the name/number pair, respectively.
10281 ** The text name overlays one or more following integers. The text name
10282 ** is always zero-terminated.
10283 **
10284 ** Conceptually:
10285 **
10286 ** struct VList {
10287 ** int nAlloc; // Number of allocated slots
10288 ** int nUsed; // Number of used slots
10289 ** struct VListEntry {
10290 ** int iValue; // Value for this entry
10291 ** int nSlot; // Slots used by this entry
10292 ** // ... variable name goes here
10293 ** } a[0];
10294 ** }
10295 **
10296 ** During code generation, pointers to the variable names within the
10297 ** VList are taken. When that happens, nAlloc is set to zero as an
10298 ** indication that the VList may never again be enlarged, since the
10299 ** accompanying realloc() would invalidate the pointers.
10300 */
10301 SQLITE_PRIVATE VList *sqlite3VListAdd(
10302 sqlite3 *db, /* The database connection used for malloc() */
10303 VList *pIn, /* The input VList. Might be NULL */
10304 const char *zName, /* Name of symbol to add */
10305 int nName, /* Bytes of text in zName */
10306 int iVal /* Value to associate with zName */
10307 ){
10308 int nInt; /* number of sizeof(int) objects needed for zName */
10309 char *z; /* Pointer to where zName will be stored */
10310 int i; /* Index in pIn[] where zName is stored */
10311
10312 nInt = nName/4 + 3;
10313 assert( pIn==0 || pIn[0]>=3 ); /* Verify ok to add new elements */
10314 if( pIn==0 || pIn[1]+nInt > pIn[0] ){
10315 /* Enlarge the allocation */
10316 int nAlloc = (pIn ? pIn[0]*2 : 10) + nInt;
10317 VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int));
10318 if( pOut==0 ) return pIn;
10319 if( pIn==0 ) pOut[1] = 2;
10320 pIn = pOut;
10321 pIn[0] = nAlloc;
10322 }
10323 i = pIn[1];
10324 pIn[i] = iVal;
10325 pIn[i+1] = nInt;
10326 z = (char*)&pIn[i+2];
10327 pIn[1] = i+nInt;
10328 assert( pIn[1]<=pIn[0] );
10329 memcpy(z, zName, nName);
10330 z[nName] = 0;
10331 return pIn;
10332 }
10333
10334 /*
10335 ** Return a pointer to the name of a variable in the given VList that
10336 ** has the value iVal. Or return a NULL if there is no such variable in
10337 ** the list
10338 */
10339 SQLITE_PRIVATE const char *sqlite3VListNumToName(VList *pIn, int iVal){
10340 int i, mx;
10341 if( pIn==0 ) return 0;
10342 mx = pIn[1];
10343 i = 2;
10344 do{
10345 if( pIn[i]==iVal ) return (char*)&pIn[i+2];
10346 i += pIn[i+1];
10347 }while( i<mx );
10348 return 0;
10349 }
10350
10351 /*
10352 ** Return the number of the variable named zName, if it is in VList.
10353 ** or return 0 if there is no such variable.
10354 */
10355 SQLITE_PRIVATE int sqlite3VListNameToNum(VList *pIn, const char *zName, int nNam e){
10356 int i, mx;
10357 if( pIn==0 ) return 0;
10358 mx = pIn[1];
10359 i = 2;
10360 do{
10361 const char *z = (const char*)&pIn[i+2];
10362 if( strncmp(z,zName,nName)==0 && z[nName]==0 ) return pIn[i];
10363 i += pIn[i+1];
10364 }while( i<mx );
10365 return 0;
10366 }
10367
10368 /************** End of util.c ************************************************/
10369 /************** Begin file hash.c ********************************************/
10370 /*
10371 ** 2001 September 22
10372 **
10373 ** The author disclaims copyright to this source code. In place of
10374 ** a legal notice, here is a blessing:
10375 **
10376 ** May you do good and not evil.
10377 ** May you find forgiveness for yourself and forgive others.
10378 ** May you share freely, never taking more than you give.
10379 **
10380 *************************************************************************
10381 ** This is the implementation of generic hash-tables
10382 ** used in SQLite.
10383 */
10384 /* #include "sqliteInt.h" */
10385 /* #include <assert.h> */
10386
10387 /* Turn bulk memory into a hash table object by initializing the
10388 ** fields of the Hash structure.
10389 **
10390 ** "pNew" is a pointer to the hash table that is to be initialized.
10391 */
10392 SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){
10393 assert( pNew!=0 );
10394 pNew->first = 0;
10395 pNew->count = 0;
10396 pNew->htsize = 0;
10397 pNew->ht = 0;
10398 }
10399
10400 /* Remove all entries from a hash table. Reclaim all memory.
10401 ** Call this routine to delete a hash table or to reset a hash table
10402 ** to the empty state.
10403 */
10404 SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
10405 HashElem *elem; /* For looping over all elements of the table */
10406
10407 assert( pH!=0 );
10408 elem = pH->first;
10409 pH->first = 0;
10410 sqlite3_free(pH->ht);
10411 pH->ht = 0;
10412 pH->htsize = 0;
10413 while( elem ){
10414 HashElem *next_elem = elem->next;
10415 sqlite3_free(elem);
10416 elem = next_elem;
10417 }
10418 pH->count = 0;
10419 }
10420
10421 /*
10422 ** The hashing function.
10423 */
10424 static unsigned int strHash(const char *z){
10425 unsigned int h = 0;
10426 unsigned char c;
10427 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
10428 /* Knuth multiplicative hashing. (Sorting & Searching, p. 510).
10429 ** 0x9e3779b1 is 2654435761 which is the closest prime number to
10430 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
10431 h += sqlite3UpperToLower[c];
10432 h *= 0x9e3779b1;
10433 }
10434 return h;
10435 }
10436
10437
10438 /* Link pNew element into the hash table pH. If pEntry!=0 then also
10439 ** insert pNew into the pEntry hash bucket.
10440 */
10441 static void insertElement(
10442 Hash *pH, /* The complete hash table */
10443 struct _ht *pEntry, /* The entry into which pNew is inserted */
10444 HashElem *pNew /* The element to be inserted */
10445 ){
10446 HashElem *pHead; /* First element already in pEntry */
10447 if( pEntry ){
10448 pHead = pEntry->count ? pEntry->chain : 0;
10449 pEntry->count++;
10450 pEntry->chain = pNew;
10451 }else{
10452 pHead = 0;
10453 }
10454 if( pHead ){
10455 pNew->next = pHead;
10456 pNew->prev = pHead->prev;
10457 if( pHead->prev ){ pHead->prev->next = pNew; }
10458 else { pH->first = pNew; }
10459 pHead->prev = pNew;
10460 }else{
10461 pNew->next = pH->first;
10462 if( pH->first ){ pH->first->prev = pNew; }
10463 pNew->prev = 0;
10464 pH->first = pNew;
10465 }
10466 }
10467
10468
10469 /* Resize the hash table so that it cantains "new_size" buckets.
10470 **
10471 ** The hash table might fail to resize if sqlite3_malloc() fails or
10472 ** if the new size is the same as the prior size.
10473 ** Return TRUE if the resize occurs and false if not.
10474 */
10475 static int rehash(Hash *pH, unsigned int new_size){
10476 struct _ht *new_ht; /* The new hash table */
10477 HashElem *elem, *next_elem; /* For looping over existing elements */
10478
10479 #if SQLITE_MALLOC_SOFT_LIMIT>0
10480 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
10481 new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
10482 }
10483 if( new_size==pH->htsize ) return 0;
10484 #endif
10485
10486 /* The inability to allocates space for a larger hash table is
10487 ** a performance hit but it is not a fatal error. So mark the
10488 ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of
10489 ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
10490 ** only zeroes the requested number of bytes whereas this module will
10491 ** use the actual amount of space allocated for the hash table (which
10492 ** may be larger than the requested amount).
10493 */
10494 sqlite3BeginBenignMalloc();
10495 new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
10496 sqlite3EndBenignMalloc();
10497
10498 if( new_ht==0 ) return 0;
10499 sqlite3_free(pH->ht);
10500 pH->ht = new_ht;
10501 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
10502 memset(new_ht, 0, new_size*sizeof(struct _ht));
10503 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
10504 unsigned int h = strHash(elem->pKey) % new_size;
10505 next_elem = elem->next;
10506 insertElement(pH, &new_ht[h], elem);
10507 }
10508 return 1;
10509 }
10510
10511 /* This function (for internal use only) locates an element in an
10512 ** hash table that matches the given key. The hash for this key is
10513 ** also computed and returned in the *pH parameter.
10514 */
10515 static HashElem *findElementWithHash(
10516 const Hash *pH, /* The pH to be searched */
10517 const char *pKey, /* The key we are searching for */
10518 unsigned int *pHash /* Write the hash value here */
10519 ){
10520 HashElem *elem; /* Used to loop thru the element list */
10521 int count; /* Number of elements left to test */
10522 unsigned int h; /* The computed hash */
10523
10524 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
10525 struct _ht *pEntry;
10526 h = strHash(pKey) % pH->htsize;
10527 pEntry = &pH->ht[h];
10528 elem = pEntry->chain;
10529 count = pEntry->count;
10530 }else{
10531 h = 0;
10532 elem = pH->first;
10533 count = pH->count;
10534 }
10535 *pHash = h;
10536 while( count-- ){
10537 assert( elem!=0 );
10538 if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
10539 return elem;
10540 }
10541 elem = elem->next;
10542 }
10543 return 0;
10544 }
10545
10546 /* Remove a single entry from the hash table given a pointer to that
10547 ** element and a hash on the element's key.
10548 */
10549 static void removeElementGivenHash(
10550 Hash *pH, /* The pH containing "elem" */
10551 HashElem* elem, /* The element to be removed from the pH */
10552 unsigned int h /* Hash value for the element */
10553 ){
10554 struct _ht *pEntry;
10555 if( elem->prev ){
10556 elem->prev->next = elem->next;
10557 }else{
10558 pH->first = elem->next;
10559 }
10560 if( elem->next ){
10561 elem->next->prev = elem->prev;
10562 }
10563 if( pH->ht ){
10564 pEntry = &pH->ht[h];
10565 if( pEntry->chain==elem ){
10566 pEntry->chain = elem->next;
10567 }
10568 pEntry->count--;
10569 assert( pEntry->count>=0 );
10570 }
10571 sqlite3_free( elem );
10572 pH->count--;
10573 if( pH->count==0 ){
10574 assert( pH->first==0 );
10575 assert( pH->count==0 );
10576 sqlite3HashClear(pH);
10577 }
10578 }
10579
10580 /* Attempt to locate an element of the hash table pH with a key
10581 ** that matches pKey. Return the data for this element if it is
10582 ** found, or NULL if there is no match.
10583 */
10584 SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey){
10585 HashElem *elem; /* The element that matches key */
10586 unsigned int h; /* A hash on key */
10587
10588 assert( pH!=0 );
10589 assert( pKey!=0 );
10590 elem = findElementWithHash(pH, pKey, &h);
10591 return elem ? elem->data : 0;
10592 }
10593
10594 /* Insert an element into the hash table pH. The key is pKey
10595 ** and the data is "data".
10596 **
10597 ** If no element exists with a matching key, then a new
10598 ** element is created and NULL is returned.
10599 **
10600 ** If another element already exists with the same key, then the
10601 ** new data replaces the old data and the old data is returned.
10602 ** The key is not copied in this instance. If a malloc fails, then
10603 ** the new data is returned and the hash table is unchanged.
10604 **
10605 ** If the "data" parameter to this function is NULL, then the
10606 ** element corresponding to "key" is removed from the hash table.
10607 */
10608 SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){
10609 unsigned int h; /* the hash of the key modulo hash table size */
10610 HashElem *elem; /* Used to loop thru the element list */
10611 HashElem *new_elem; /* New element added to the pH */
10612
10613 assert( pH!=0 );
10614 assert( pKey!=0 );
10615 elem = findElementWithHash(pH,pKey,&h);
10616 if( elem ){
10617 void *old_data = elem->data;
10618 if( data==0 ){
10619 removeElementGivenHash(pH,elem,h);
10620 }else{
10621 elem->data = data;
10622 elem->pKey = pKey;
10623 }
10624 return old_data;
10625 }
10626 if( data==0 ) return 0;
10627 new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
10628 if( new_elem==0 ) return data;
10629 new_elem->pKey = pKey;
10630 new_elem->data = data;
10631 pH->count++;
10632 if( pH->count>=10 && pH->count > 2*pH->htsize ){
10633 if( rehash(pH, pH->count*2) ){
10634 assert( pH->htsize>0 );
10635 h = strHash(pKey) % pH->htsize;
10636 }
10637 }
10638 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
10639 return 0;
10640 }
10641
10642 /************** End of hash.c ************************************************/
10643 /************** Begin file opcodes.c *****************************************/
10644 /* Automatically generated. Do not edit */
10645 /* See the tool/mkopcodec.tcl script for details. */
10646 #if !defined(SQLITE_OMIT_EXPLAIN) \
10647 || defined(VDBE_PROFILE) \
10648 || defined(SQLITE_DEBUG)
10649 #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG)
10650 # define OpHelp(X) "\0" X
10651 #else
10652 # define OpHelp(X)
10653 #endif
10654 SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
10655 static const char *const azName[] = {
10656 /* 0 */ "Savepoint" OpHelp(""),
10657 /* 1 */ "AutoCommit" OpHelp(""),
10658 /* 2 */ "Transaction" OpHelp(""),
10659 /* 3 */ "SorterNext" OpHelp(""),
10660 /* 4 */ "PrevIfOpen" OpHelp(""),
10661 /* 5 */ "NextIfOpen" OpHelp(""),
10662 /* 6 */ "Prev" OpHelp(""),
10663 /* 7 */ "Next" OpHelp(""),
10664 /* 8 */ "Checkpoint" OpHelp(""),
10665 /* 9 */ "JournalMode" OpHelp(""),
10666 /* 10 */ "Vacuum" OpHelp(""),
10667 /* 11 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
10668 /* 12 */ "VUpdate" OpHelp("data=r[P3@P2]"),
10669 /* 13 */ "Goto" OpHelp(""),
10670 /* 14 */ "Gosub" OpHelp(""),
10671 /* 15 */ "InitCoroutine" OpHelp(""),
10672 /* 16 */ "Yield" OpHelp(""),
10673 /* 17 */ "MustBeInt" OpHelp(""),
10674 /* 18 */ "Jump" OpHelp(""),
10675 /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
10676 /* 20 */ "Once" OpHelp(""),
10677 /* 21 */ "If" OpHelp(""),
10678 /* 22 */ "IfNot" OpHelp(""),
10679 /* 23 */ "SeekLT" OpHelp("key=r[P3@P4]"),
10680 /* 24 */ "SeekLE" OpHelp("key=r[P3@P4]"),
10681 /* 25 */ "SeekGE" OpHelp("key=r[P3@P4]"),
10682 /* 26 */ "SeekGT" OpHelp("key=r[P3@P4]"),
10683 /* 27 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
10684 /* 28 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
10685 /* 29 */ "NoConflict" OpHelp("key=r[P3@P4]"),
10686 /* 30 */ "NotFound" OpHelp("key=r[P3@P4]"),
10687 /* 31 */ "Found" OpHelp("key=r[P3@P4]"),
10688 /* 32 */ "SeekRowid" OpHelp("intkey=r[P3]"),
10689 /* 33 */ "NotExists" OpHelp("intkey=r[P3]"),
10690 /* 34 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
10691 /* 35 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
10692 /* 36 */ "Ne" OpHelp("IF r[P3]!=r[P1]"),
10693 /* 37 */ "Eq" OpHelp("IF r[P3]==r[P1]"),
10694 /* 38 */ "Gt" OpHelp("IF r[P3]>r[P1]"),
10695 /* 39 */ "Le" OpHelp("IF r[P3]<=r[P1]"),
10696 /* 40 */ "Lt" OpHelp("IF r[P3]<r[P1]"),
10697 /* 41 */ "Ge" OpHelp("IF r[P3]>=r[P1]"),
10698 /* 42 */ "ElseNotEq" OpHelp(""),
10699 /* 43 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
10700 /* 44 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
10701 /* 45 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
10702 /* 46 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
10703 /* 47 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
10704 /* 48 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
10705 /* 49 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
10706 /* 50 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
10707 /* 51 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
10708 /* 52 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
10709 /* 53 */ "Last" OpHelp(""),
10710 /* 54 */ "BitNot" OpHelp("r[P1]= ~r[P1]"),
10711 /* 55 */ "SorterSort" OpHelp(""),
10712 /* 56 */ "Sort" OpHelp(""),
10713 /* 57 */ "Rewind" OpHelp(""),
10714 /* 58 */ "IdxLE" OpHelp("key=r[P3@P4]"),
10715 /* 59 */ "IdxGT" OpHelp("key=r[P3@P4]"),
10716 /* 60 */ "IdxLT" OpHelp("key=r[P3@P4]"),
10717 /* 61 */ "IdxGE" OpHelp("key=r[P3@P4]"),
10718 /* 62 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
10719 /* 63 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
10720 /* 64 */ "Program" OpHelp(""),
10721 /* 65 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
10722 /* 66 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
10723 /* 67 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
10724 /* 68 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
10725 /* 69 */ "IncrVacuum" OpHelp(""),
10726 /* 70 */ "VNext" OpHelp(""),
10727 /* 71 */ "Init" OpHelp("Start at P2"),
10728 /* 72 */ "Return" OpHelp(""),
10729 /* 73 */ "EndCoroutine" OpHelp(""),
10730 /* 74 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
10731 /* 75 */ "Halt" OpHelp(""),
10732 /* 76 */ "Integer" OpHelp("r[P2]=P1"),
10733 /* 77 */ "Int64" OpHelp("r[P2]=P4"),
10734 /* 78 */ "String" OpHelp("r[P2]='P4' (len=P1)"),
10735 /* 79 */ "Null" OpHelp("r[P2..P3]=NULL"),
10736 /* 80 */ "SoftNull" OpHelp("r[P1]=NULL"),
10737 /* 81 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"),
10738 /* 82 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"),
10739 /* 83 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"),
10740 /* 84 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
10741 /* 85 */ "SCopy" OpHelp("r[P2]=r[P1]"),
10742 /* 86 */ "IntCopy" OpHelp("r[P2]=r[P1]"),
10743 /* 87 */ "ResultRow" OpHelp("output=r[P1@P2]"),
10744 /* 88 */ "CollSeq" OpHelp(""),
10745 /* 89 */ "Function0" OpHelp("r[P3]=func(r[P2@P5])"),
10746 /* 90 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"),
10747 /* 91 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"),
10748 /* 92 */ "RealAffinity" OpHelp(""),
10749 /* 93 */ "Cast" OpHelp("affinity(r[P1])"),
10750 /* 94 */ "Permutation" OpHelp(""),
10751 /* 95 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
10752 /* 96 */ "Column" OpHelp("r[P3]=PX"),
10753 /* 97 */ "String8" OpHelp("r[P2]='P4'"),
10754 /* 98 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
10755 /* 99 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
10756 /* 100 */ "Count" OpHelp("r[P2]=count()"),
10757 /* 101 */ "ReadCookie" OpHelp(""),
10758 /* 102 */ "SetCookie" OpHelp(""),
10759 /* 103 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
10760 /* 104 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
10761 /* 105 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
10762 /* 106 */ "OpenAutoindex" OpHelp("nColumn=P2"),
10763 /* 107 */ "OpenEphemeral" OpHelp("nColumn=P2"),
10764 /* 108 */ "SorterOpen" OpHelp(""),
10765 /* 109 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
10766 /* 110 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
10767 /* 111 */ "Close" OpHelp(""),
10768 /* 112 */ "ColumnsUsed" OpHelp(""),
10769 /* 113 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
10770 /* 114 */ "NewRowid" OpHelp("r[P2]=rowid"),
10771 /* 115 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
10772 /* 116 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"),
10773 /* 117 */ "Delete" OpHelp(""),
10774 /* 118 */ "ResetCount" OpHelp(""),
10775 /* 119 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
10776 /* 120 */ "SorterData" OpHelp("r[P2]=data"),
10777 /* 121 */ "RowData" OpHelp("r[P2]=data"),
10778 /* 122 */ "Rowid" OpHelp("r[P2]=rowid"),
10779 /* 123 */ "NullRow" OpHelp(""),
10780 /* 124 */ "SorterInsert" OpHelp("key=r[P2]"),
10781 /* 125 */ "IdxInsert" OpHelp("key=r[P2]"),
10782 /* 126 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
10783 /* 127 */ "Seek" OpHelp("Move P3 to P1.rowid"),
10784 /* 128 */ "IdxRowid" OpHelp("r[P2]=rowid"),
10785 /* 129 */ "Destroy" OpHelp(""),
10786 /* 130 */ "Clear" OpHelp(""),
10787 /* 131 */ "ResetSorter" OpHelp(""),
10788 /* 132 */ "Real" OpHelp("r[P2]=P4"),
10789 /* 133 */ "CreateIndex" OpHelp("r[P2]=root iDb=P1"),
10790 /* 134 */ "CreateTable" OpHelp("r[P2]=root iDb=P1"),
10791 /* 135 */ "ParseSchema" OpHelp(""),
10792 /* 136 */ "LoadAnalysis" OpHelp(""),
10793 /* 137 */ "DropTable" OpHelp(""),
10794 /* 138 */ "DropIndex" OpHelp(""),
10795 /* 139 */ "DropTrigger" OpHelp(""),
10796 /* 140 */ "IntegrityCk" OpHelp(""),
10797 /* 141 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
10798 /* 142 */ "Param" OpHelp(""),
10799 /* 143 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
10800 /* 144 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
10801 /* 145 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3] ) else r[P2]=(-1)"),
10802 /* 146 */ "AggStep0" OpHelp("accum=r[P3] step(r[P2@P5])"),
10803 /* 147 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
10804 /* 148 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
10805 /* 149 */ "Expire" OpHelp(""),
10806 /* 150 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
10807 /* 151 */ "VBegin" OpHelp(""),
10808 /* 152 */ "VCreate" OpHelp(""),
10809 /* 153 */ "VDestroy" OpHelp(""),
10810 /* 154 */ "VOpen" OpHelp(""),
10811 /* 155 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
10812 /* 156 */ "VRename" OpHelp(""),
10813 /* 157 */ "Pagecount" OpHelp(""),
10814 /* 158 */ "MaxPgcnt" OpHelp(""),
10815 /* 159 */ "CursorHint" OpHelp(""),
10816 /* 160 */ "Noop" OpHelp(""),
10817 /* 161 */ "Explain" OpHelp(""),
10818 };
10819 return azName[i];
10820 }
10821 #endif
10822
10823 /************** End of opcodes.c *********************************************/
10824 /************** Begin file os_unix.c *****************************************/
10825 /*
10826 ** 2004 May 22
10827 **
10828 ** The author disclaims copyright to this source code. In place of
10829 ** a legal notice, here is a blessing:
10830 **
10831 ** May you do good and not evil.
10832 ** May you find forgiveness for yourself and forgive others.
10833 ** May you share freely, never taking more than you give.
10834 **
10835 ******************************************************************************
10836 **
10837 ** This file contains the VFS implementation for unix-like operating systems
10838 ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
10839 **
10840 ** There are actually several different VFS implementations in this file.
10841 ** The differences are in the way that file locking is done. The default
10842 ** implementation uses Posix Advisory Locks. Alternative implementations
10843 ** use flock(), dot-files, various proprietary locking schemas, or simply
10844 ** skip locking all together.
10845 **
10846 ** This source file is organized into divisions where the logic for various
10847 ** subfunctions is contained within the appropriate division. PLEASE
10848 ** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed
10849 ** in the correct division and should be clearly labeled.
10850 **
10851 ** The layout of divisions is as follows:
10852 **
10853 ** * General-purpose declarations and utility functions.
10854 ** * Unique file ID logic used by VxWorks.
10855 ** * Various locking primitive implementations (all except proxy locking):
10856 ** + for Posix Advisory Locks
10857 ** + for no-op locks
10858 ** + for dot-file locks
10859 ** + for flock() locking
10860 ** + for named semaphore locks (VxWorks only)
10861 ** + for AFP filesystem locks (MacOSX only)
10862 ** * sqlite3_file methods not associated with locking.
10863 ** * Definitions of sqlite3_io_methods objects for all locking
10864 ** methods plus "finder" functions for each locking method.
10865 ** * sqlite3_vfs method implementations.
10866 ** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
10867 ** * Definitions of sqlite3_vfs objects for all locking methods
10868 ** plus implementations of sqlite3_os_init() and sqlite3_os_end().
10869 */
10870 /* #include "sqliteInt.h" */
10871 #if SQLITE_OS_UNIX /* This file is used on unix only */
10872
10873 /*
10874 ** There are various methods for file locking used for concurrency
10875 ** control:
10876 **
10877 ** 1. POSIX locking (the default),
10878 ** 2. No locking,
10879 ** 3. Dot-file locking,
10880 ** 4. flock() locking,
10881 ** 5. AFP locking (OSX only),
10882 ** 6. Named POSIX semaphores (VXWorks only),
10883 ** 7. proxy locking. (OSX only)
10884 **
10885 ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
10886 ** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
10887 ** selection of the appropriate locking style based on the filesystem
10888 ** where the database is located.
10889 */
10890 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
10891 # if defined(__APPLE__)
10892 # define SQLITE_ENABLE_LOCKING_STYLE 1
10893 # else
10894 # define SQLITE_ENABLE_LOCKING_STYLE 0
10895 # endif
10896 #endif
10897
10898 /* Use pread() and pwrite() if they are available */
10899 #if defined(__APPLE__)
10900 # define HAVE_PREAD 1
10901 # define HAVE_PWRITE 1
10902 #endif
10903 #if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64)
10904 # undef USE_PREAD
10905 # define USE_PREAD64 1
10906 #elif defined(HAVE_PREAD) && defined(HAVE_PWRITE)
10907 # undef USE_PREAD64
10908 # define USE_PREAD 1
10909 #endif
10910
10911 /*
10912 ** standard include files.
10913 */
10914 #include <sys/types.h>
10915 #include <sys/stat.h>
10916 #include <fcntl.h>
10917 #include <unistd.h>
10918 /* #include <time.h> */
10919 #include <sys/time.h>
10920 #include <errno.h>
10921 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
10922 # include <sys/mman.h>
10923 #endif
10924
10925 #if SQLITE_ENABLE_LOCKING_STYLE
10926 # include <sys/ioctl.h>
10927 # include <sys/file.h>
10928 # include <sys/param.h>
10929 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
10930
10931 #if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
10932 (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
10933 # if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \
10934 && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0))
10935 # define HAVE_GETHOSTUUID 1
10936 # else
10937 # warning "gethostuuid() is disabled."
10938 # endif
10939 #endif
10940
10941
10942 #if OS_VXWORKS
10943 /* # include <sys/ioctl.h> */
10944 # include <semaphore.h>
10945 # include <limits.h>
10946 #endif /* OS_VXWORKS */
10947
10948 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
10949 # include <sys/mount.h>
10950 #endif
10951
10952 #ifdef HAVE_UTIME
10953 # include <utime.h>
10954 #endif
10955
10956 /*
10957 ** Allowed values of unixFile.fsFlags
10958 */
10959 #define SQLITE_FSFLAGS_IS_MSDOS 0x1
10960
10961 /*
10962 ** If we are to be thread-safe, include the pthreads header and define
10963 ** the SQLITE_UNIX_THREADS macro.
10964 */
10965 #if SQLITE_THREADSAFE
10966 /* # include <pthread.h> */
10967 # define SQLITE_UNIX_THREADS 1
10968 #endif
10969
10970 /*
10971 ** Default permissions when creating a new file
10972 */
10973 #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
10974 # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
10975 #endif
10976
10977 /*
10978 ** Default permissions when creating auto proxy dir
10979 */
10980 #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
10981 # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
10982 #endif
10983
10984 /*
10985 ** Maximum supported path-length.
10986 */
10987 #define MAX_PATHNAME 512
10988
10989 /*
10990 ** Maximum supported symbolic links
10991 */
10992 #define SQLITE_MAX_SYMLINKS 100
10993
10994 /* Always cast the getpid() return type for compatibility with
10995 ** kernel modules in VxWorks. */
10996 #define osGetpid(X) (pid_t)getpid()
10997
10998 /*
10999 ** Only set the lastErrno if the error code is a real error and not
11000 ** a normal expected return code of SQLITE_BUSY or SQLITE_OK
11001 */
11002 #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY))
11003
11004 /* Forward references */
11005 typedef struct unixShm unixShm; /* Connection shared memory */
11006 typedef struct unixShmNode unixShmNode; /* Shared memory instance */
11007 typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
11008 typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */
11009
11010 /*
11011 ** Sometimes, after a file handle is closed by SQLite, the file descriptor
11012 ** cannot be closed immediately. In these cases, instances of the following
11013 ** structure are used to store the file descriptor while waiting for an
11014 ** opportunity to either close or reuse it.
11015 */
11016 struct UnixUnusedFd {
11017 int fd; /* File descriptor to close */
11018 int flags; /* Flags this file descriptor was opened with */
11019 UnixUnusedFd *pNext; /* Next unused file descriptor on same file */
11020 };
11021
11022 /*
11023 ** The unixFile structure is subclass of sqlite3_file specific to the unix
11024 ** VFS implementations.
11025 */
11026 typedef struct unixFile unixFile;
11027 struct unixFile {
11028 sqlite3_io_methods const *pMethod; /* Always the first entry */
11029 sqlite3_vfs *pVfs; /* The VFS that created this unixFile */
11030 unixInodeInfo *pInode; /* Info about locks on this inode */
11031 int h; /* The file descriptor */
11032 unsigned char eFileLock; /* The type of lock held on this fd */
11033 unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
11034 int lastErrno; /* The unix errno from last I/O error */
11035 void *lockingContext; /* Locking style specific state */
11036 UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
11037 const char *zPath; /* Name of the file */
11038 unixShm *pShm; /* Shared memory segment information */
11039 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
11040 #if SQLITE_MAX_MMAP_SIZE>0
11041 int nFetchOut; /* Number of outstanding xFetch refs */
11042 sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */
11043 sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */
11044 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
11045 void *pMapRegion; /* Memory mapped region */
11046 #endif
11047 #ifdef __QNXNTO__
11048 int sectorSize; /* Device sector size */
11049 int deviceCharacteristics; /* Precomputed device characteristics */
11050 #endif
11051 #if SQLITE_ENABLE_LOCKING_STYLE
11052 int openFlags; /* The flags specified at open() */
11053 #endif
11054 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
11055 unsigned fsFlags; /* cached details from statfs() */
11056 #endif
11057 #if OS_VXWORKS
11058 struct vxworksFileId *pId; /* Unique file ID */
11059 #endif
11060 #ifdef SQLITE_DEBUG
11061 /* The next group of variables are used to track whether or not the
11062 ** transaction counter in bytes 24-27 of database files are updated
11063 ** whenever any part of the database changes. An assertion fault will
11064 ** occur if a file is updated without also updating the transaction
11065 ** counter. This test is made to avoid new problems similar to the
11066 ** one described by ticket #3584.
11067 */
11068 unsigned char transCntrChng; /* True if the transaction counter changed */
11069 unsigned char dbUpdate; /* True if any part of database file changed */
11070 unsigned char inNormalWrite; /* True if in a normal write operation */
11071
11072 #endif
11073
11074 #ifdef SQLITE_TEST
11075 /* In test mode, increase the size of this structure a bit so that
11076 ** it is larger than the struct CrashFile defined in test6.c.
11077 */
11078 char aPadding[32];
11079 #endif
11080 };
11081
11082 /* This variable holds the process id (pid) from when the xRandomness()
11083 ** method was called. If xOpen() is called from a different process id,
11084 ** indicating that a fork() has occurred, the PRNG will be reset.
11085 */
11086 static pid_t randomnessPid = 0;
11087
11088 /*
11089 ** Allowed values for the unixFile.ctrlFlags bitmask:
11090 */
11091 #define UNIXFILE_EXCL 0x01 /* Connections from one process only */
11092 #define UNIXFILE_RDONLY 0x02 /* Connection is read only */
11093 #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
11094 #ifndef SQLITE_DISABLE_DIRSYNC
11095 # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
11096 #else
11097 # define UNIXFILE_DIRSYNC 0x00
11098 #endif
11099 #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
11100 #define UNIXFILE_DELETE 0x20 /* Delete on close */
11101 #define UNIXFILE_URI 0x40 /* Filename might have query parameters */
11102 #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */
11103
11104 /*
11105 ** Include code that is common to all os_*.c files
11106 */
11107 /************** Include os_common.h in the middle of os_unix.c ***************/
11108 /************** Begin file os_common.h ***************************************/
11109 /*
11110 ** 2004 May 22
11111 **
11112 ** The author disclaims copyright to this source code. In place of
11113 ** a legal notice, here is a blessing:
11114 **
11115 ** May you do good and not evil.
11116 ** May you find forgiveness for yourself and forgive others.
11117 ** May you share freely, never taking more than you give.
11118 **
11119 ******************************************************************************
11120 **
11121 ** This file contains macros and a little bit of code that is common to
11122 ** all of the platform-specific files (os_*.c) and is #included into those
11123 ** files.
11124 **
11125 ** This file should be #included by the os_*.c files only. It is not a
11126 ** general purpose header file.
11127 */
11128 #ifndef _OS_COMMON_H_
11129 #define _OS_COMMON_H_
11130
11131 /*
11132 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
11133 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
11134 ** switch. The following code should catch this problem at compile-time.
11135 */
11136 #ifdef MEMORY_DEBUG
11137 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
11138 #endif
11139
11140 /*
11141 ** Macros for performance tracing. Normally turned off. Only works
11142 ** on i486 hardware.
11143 */
11144 #ifdef SQLITE_PERFORMANCE_TRACE
11145
11146 /*
11147 ** hwtime.h contains inline assembler code for implementing
11148 ** high-performance timing routines.
11149 */
11150 /************** Include hwtime.h in the middle of os_common.h ****************/
11151 /************** Begin file hwtime.h ******************************************/
11152 /*
11153 ** 2008 May 27
11154 **
11155 ** The author disclaims copyright to this source code. In place of
11156 ** a legal notice, here is a blessing:
11157 **
11158 ** May you do good and not evil.
11159 ** May you find forgiveness for yourself and forgive others.
11160 ** May you share freely, never taking more than you give.
11161 **
11162 ******************************************************************************
11163 **
11164 ** This file contains inline asm code for retrieving "high-performance"
11165 ** counters for x86 class CPUs.
11166 */
11167 #ifndef SQLITE_HWTIME_H
11168 #define SQLITE_HWTIME_H
11169
11170 /*
11171 ** The following routine only works on pentium-class (or newer) processors.
11172 ** It uses the RDTSC opcode to read the cycle count value out of the
11173 ** processor and returns that value. This can be used for high-res
11174 ** profiling.
11175 */
11176 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
11177 (defined(i386) || defined(__i386__) || defined(_M_IX86))
11178
11179 #if defined(__GNUC__)
11180
11181 __inline__ sqlite_uint64 sqlite3Hwtime(void){
11182 unsigned int lo, hi;
11183 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
11184 return (sqlite_uint64)hi << 32 | lo;
11185 }
11186
11187 #elif defined(_MSC_VER)
11188
11189 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
11190 __asm {
11191 rdtsc
11192 ret ; return value at EDX:EAX
11193 }
11194 }
11195
11196 #endif
11197
11198 #elif (defined(__GNUC__) && defined(__x86_64__))
11199
11200 __inline__ sqlite_uint64 sqlite3Hwtime(void){
11201 unsigned long val;
11202 __asm__ __volatile__ ("rdtsc" : "=A" (val));
11203 return val;
11204 }
11205
11206 #elif (defined(__GNUC__) && defined(__ppc__))
11207
11208 __inline__ sqlite_uint64 sqlite3Hwtime(void){
11209 unsigned long long retval;
11210 unsigned long junk;
11211 __asm__ __volatile__ ("\n\
11212 1: mftbu %1\n\
11213 mftb %L0\n\
11214 mftbu %0\n\
11215 cmpw %0,%1\n\
11216 bne 1b"
11217 : "=r" (retval), "=r" (junk));
11218 return retval;
11219 }
11220
11221 #else
11222
11223 #error Need implementation of sqlite3Hwtime() for your platform.
11224
11225 /*
11226 ** To compile without implementing sqlite3Hwtime() for your platform,
11227 ** you can remove the above #error and use the following
11228 ** stub function. You will lose timing support for many
11229 ** of the debugging and testing utilities, but it should at
11230 ** least compile and run.
11231 */
11232 SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
11233
11234 #endif
11235
11236 #endif /* !defined(SQLITE_HWTIME_H) */
11237
11238 /************** End of hwtime.h **********************************************/
11239 /************** Continuing where we left off in os_common.h ******************/
11240
11241 static sqlite_uint64 g_start;
11242 static sqlite_uint64 g_elapsed;
11243 #define TIMER_START g_start=sqlite3Hwtime()
11244 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
11245 #define TIMER_ELAPSED g_elapsed
11246 #else
11247 #define TIMER_START
11248 #define TIMER_END
11249 #define TIMER_ELAPSED ((sqlite_uint64)0)
11250 #endif
11251
11252 /*
11253 ** If we compile with the SQLITE_TEST macro set, then the following block
11254 ** of code will give us the ability to simulate a disk I/O error. This
11255 ** is used for testing the I/O recovery logic.
11256 */
11257 #if defined(SQLITE_TEST)
11258 SQLITE_API extern int sqlite3_io_error_hit;
11259 SQLITE_API extern int sqlite3_io_error_hardhit;
11260 SQLITE_API extern int sqlite3_io_error_pending;
11261 SQLITE_API extern int sqlite3_io_error_persist;
11262 SQLITE_API extern int sqlite3_io_error_benign;
11263 SQLITE_API extern int sqlite3_diskfull_pending;
11264 SQLITE_API extern int sqlite3_diskfull;
11265 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
11266 #define SimulateIOError(CODE) \
11267 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
11268 || sqlite3_io_error_pending-- == 1 ) \
11269 { local_ioerr(); CODE; }
11270 static void local_ioerr(){
11271 IOTRACE(("IOERR\n"));
11272 sqlite3_io_error_hit++;
11273 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
11274 }
11275 #define SimulateDiskfullError(CODE) \
11276 if( sqlite3_diskfull_pending ){ \
11277 if( sqlite3_diskfull_pending == 1 ){ \
11278 local_ioerr(); \
11279 sqlite3_diskfull = 1; \
11280 sqlite3_io_error_hit = 1; \
11281 CODE; \
11282 }else{ \
11283 sqlite3_diskfull_pending--; \
11284 } \
11285 }
11286 #else
11287 #define SimulateIOErrorBenign(X)
11288 #define SimulateIOError(A)
11289 #define SimulateDiskfullError(A)
11290 #endif /* defined(SQLITE_TEST) */
11291
11292 /*
11293 ** When testing, keep a count of the number of open files.
11294 */
11295 #if defined(SQLITE_TEST)
11296 SQLITE_API extern int sqlite3_open_file_count;
11297 #define OpenCounter(X) sqlite3_open_file_count+=(X)
11298 #else
11299 #define OpenCounter(X)
11300 #endif /* defined(SQLITE_TEST) */
11301
11302 #endif /* !defined(_OS_COMMON_H_) */
11303
11304 /************** End of os_common.h *******************************************/
11305 /************** Continuing where we left off in os_unix.c ********************/
11306
11307 /*
11308 ** Define various macros that are missing from some systems.
11309 */
11310 #ifndef O_LARGEFILE
11311 # define O_LARGEFILE 0
11312 #endif
11313 #ifdef SQLITE_DISABLE_LFS
11314 # undef O_LARGEFILE
11315 # define O_LARGEFILE 0
11316 #endif
11317 #ifndef O_NOFOLLOW
11318 # define O_NOFOLLOW 0
11319 #endif
11320 #ifndef O_BINARY
11321 # define O_BINARY 0
11322 #endif
11323
11324 /*
11325 ** The threadid macro resolves to the thread-id or to 0. Used for
11326 ** testing and debugging only.
11327 */
11328 #if SQLITE_THREADSAFE
11329 #define threadid pthread_self()
11330 #else
11331 #define threadid 0
11332 #endif
11333
11334 /*
11335 ** HAVE_MREMAP defaults to true on Linux and false everywhere else.
11336 */
11337 #if !defined(HAVE_MREMAP)
11338 # if defined(__linux__) && defined(_GNU_SOURCE)
11339 # define HAVE_MREMAP 1
11340 # else
11341 # define HAVE_MREMAP 0
11342 # endif
11343 #endif
11344
11345 /*
11346 ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
11347 ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
11348 */
11349 #ifdef __ANDROID__
11350 # define lseek lseek64
11351 #endif
11352
11353 /*
11354 ** Different Unix systems declare open() in different ways. Same use
11355 ** open(const char*,int,mode_t). Others use open(const char*,int,...).
11356 ** The difference is important when using a pointer to the function.
11357 **
11358 ** The safest way to deal with the problem is to always use this wrapper
11359 ** which always has the same well-defined interface.
11360 */
11361 static int posixOpen(const char *zFile, int flags, int mode){
11362 return open(zFile, flags, mode);
11363 }
11364
11365 /* Forward reference */
11366 static int openDirectory(const char*, int*);
11367 static int unixGetpagesize(void);
11368
11369 /*
11370 ** Many system calls are accessed through pointer-to-functions so that
11371 ** they may be overridden at runtime to facilitate fault injection during
11372 ** testing and sandboxing. The following array holds the names and pointers
11373 ** to all overrideable system calls.
11374 */
11375 static struct unix_syscall {
11376 const char *zName; /* Name of the system call */
11377 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
11378 sqlite3_syscall_ptr pDefault; /* Default value */
11379 } aSyscall[] = {
11380 { "open", (sqlite3_syscall_ptr)posixOpen, 0 },
11381 #define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
11382
11383 { "close", (sqlite3_syscall_ptr)close, 0 },
11384 #define osClose ((int(*)(int))aSyscall[1].pCurrent)
11385
11386 { "access", (sqlite3_syscall_ptr)access, 0 },
11387 #define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent)
11388
11389 { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 },
11390 #define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
11391
11392 { "stat", (sqlite3_syscall_ptr)stat, 0 },
11393 #define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
11394
11395 /*
11396 ** The DJGPP compiler environment looks mostly like Unix, but it
11397 ** lacks the fcntl() system call. So redefine fcntl() to be something
11398 ** that always succeeds. This means that locking does not occur under
11399 ** DJGPP. But it is DOS - what did you expect?
11400 */
11401 #ifdef __DJGPP__
11402 { "fstat", 0, 0 },
11403 #define osFstat(a,b,c) 0
11404 #else
11405 { "fstat", (sqlite3_syscall_ptr)fstat, 0 },
11406 #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
11407 #endif
11408
11409 { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 },
11410 #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
11411
11412 { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 },
11413 #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent)
11414
11415 { "read", (sqlite3_syscall_ptr)read, 0 },
11416 #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
11417
11418 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
11419 { "pread", (sqlite3_syscall_ptr)pread, 0 },
11420 #else
11421 { "pread", (sqlite3_syscall_ptr)0, 0 },
11422 #endif
11423 #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
11424
11425 #if defined(USE_PREAD64)
11426 { "pread64", (sqlite3_syscall_ptr)pread64, 0 },
11427 #else
11428 { "pread64", (sqlite3_syscall_ptr)0, 0 },
11429 #endif
11430 #define osPread64 ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent)
11431
11432 { "write", (sqlite3_syscall_ptr)write, 0 },
11433 #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
11434
11435 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
11436 { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 },
11437 #else
11438 { "pwrite", (sqlite3_syscall_ptr)0, 0 },
11439 #endif
11440 #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\
11441 aSyscall[12].pCurrent)
11442
11443 #if defined(USE_PREAD64)
11444 { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 },
11445 #else
11446 { "pwrite64", (sqlite3_syscall_ptr)0, 0 },
11447 #endif
11448 #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off64_t))\
11449 aSyscall[13].pCurrent)
11450
11451 { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 },
11452 #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent)
11453
11454 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
11455 { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 },
11456 #else
11457 { "fallocate", (sqlite3_syscall_ptr)0, 0 },
11458 #endif
11459 #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
11460
11461 { "unlink", (sqlite3_syscall_ptr)unlink, 0 },
11462 #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent)
11463
11464 { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
11465 #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
11466
11467 { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 },
11468 #define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
11469
11470 { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 },
11471 #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
11472
11473 #if defined(HAVE_FCHOWN)
11474 { "fchown", (sqlite3_syscall_ptr)fchown, 0 },
11475 #else
11476 { "fchown", (sqlite3_syscall_ptr)0, 0 },
11477 #endif
11478 #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
11479
11480 { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 },
11481 #define osGeteuid ((uid_t(*)(void))aSyscall[21].pCurrent)
11482
11483 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
11484 { "mmap", (sqlite3_syscall_ptr)mmap, 0 },
11485 #else
11486 { "mmap", (sqlite3_syscall_ptr)0, 0 },
11487 #endif
11488 #define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent)
11489
11490 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
11491 { "munmap", (sqlite3_syscall_ptr)munmap, 0 },
11492 #else
11493 { "munmap", (sqlite3_syscall_ptr)0, 0 },
11494 #endif
11495 #define osMunmap ((void*(*)(void*,size_t))aSyscall[23].pCurrent)
11496
11497 #if HAVE_MREMAP && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
11498 { "mremap", (sqlite3_syscall_ptr)mremap, 0 },
11499 #else
11500 { "mremap", (sqlite3_syscall_ptr)0, 0 },
11501 #endif
11502 #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent)
11503
11504 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
11505 { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 },
11506 #else
11507 { "getpagesize", (sqlite3_syscall_ptr)0, 0 },
11508 #endif
11509 #define osGetpagesize ((int(*)(void))aSyscall[25].pCurrent)
11510
11511 #if defined(HAVE_READLINK)
11512 { "readlink", (sqlite3_syscall_ptr)readlink, 0 },
11513 #else
11514 { "readlink", (sqlite3_syscall_ptr)0, 0 },
11515 #endif
11516 #define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)
11517
11518 #if defined(HAVE_LSTAT)
11519 { "lstat", (sqlite3_syscall_ptr)lstat, 0 },
11520 #else
11521 { "lstat", (sqlite3_syscall_ptr)0, 0 },
11522 #endif
11523 #define osLstat ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
11524
11525 }; /* End of the overrideable system calls */
11526
11527
11528 /*
11529 ** On some systems, calls to fchown() will trigger a message in a security
11530 ** log if they come from non-root processes. So avoid calling fchown() if
11531 ** we are not running as root.
11532 */
11533 static int robustFchown(int fd, uid_t uid, gid_t gid){
11534 #if defined(HAVE_FCHOWN)
11535 return osGeteuid() ? 0 : osFchown(fd,uid,gid);
11536 #else
11537 return 0;
11538 #endif
11539 }
11540
11541 /*
11542 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
11543 ** "unix" VFSes. Return SQLITE_OK opon successfully updating the
11544 ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
11545 ** system call named zName.
11546 */
11547 static int unixSetSystemCall(
11548 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
11549 const char *zName, /* Name of system call to override */
11550 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
11551 ){
11552 unsigned int i;
11553 int rc = SQLITE_NOTFOUND;
11554
11555 UNUSED_PARAMETER(pNotUsed);
11556 if( zName==0 ){
11557 /* If no zName is given, restore all system calls to their default
11558 ** settings and return NULL
11559 */
11560 rc = SQLITE_OK;
11561 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
11562 if( aSyscall[i].pDefault ){
11563 aSyscall[i].pCurrent = aSyscall[i].pDefault;
11564 }
11565 }
11566 }else{
11567 /* If zName is specified, operate on only the one system call
11568 ** specified.
11569 */
11570 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
11571 if( strcmp(zName, aSyscall[i].zName)==0 ){
11572 if( aSyscall[i].pDefault==0 ){
11573 aSyscall[i].pDefault = aSyscall[i].pCurrent;
11574 }
11575 rc = SQLITE_OK;
11576 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
11577 aSyscall[i].pCurrent = pNewFunc;
11578 break;
11579 }
11580 }
11581 }
11582 return rc;
11583 }
11584
11585 /*
11586 ** Return the value of a system call. Return NULL if zName is not a
11587 ** recognized system call name. NULL is also returned if the system call
11588 ** is currently undefined.
11589 */
11590 static sqlite3_syscall_ptr unixGetSystemCall(
11591 sqlite3_vfs *pNotUsed,
11592 const char *zName
11593 ){
11594 unsigned int i;
11595
11596 UNUSED_PARAMETER(pNotUsed);
11597 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
11598 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
11599 }
11600 return 0;
11601 }
11602
11603 /*
11604 ** Return the name of the first system call after zName. If zName==NULL
11605 ** then return the name of the first system call. Return NULL if zName
11606 ** is the last system call or if zName is not the name of a valid
11607 ** system call.
11608 */
11609 static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
11610 int i = -1;
11611
11612 UNUSED_PARAMETER(p);
11613 if( zName ){
11614 for(i=0; i<ArraySize(aSyscall)-1; i++){
11615 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
11616 }
11617 }
11618 for(i++; i<ArraySize(aSyscall); i++){
11619 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
11620 }
11621 return 0;
11622 }
11623
11624 /*
11625 ** Do not accept any file descriptor less than this value, in order to avoid
11626 ** opening database file using file descriptors that are commonly used for
11627 ** standard input, output, and error.
11628 */
11629 #ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR
11630 # define SQLITE_MINIMUM_FILE_DESCRIPTOR 3
11631 #endif
11632
11633 /*
11634 ** Invoke open(). Do so multiple times, until it either succeeds or
11635 ** fails for some reason other than EINTR.
11636 **
11637 ** If the file creation mode "m" is 0 then set it to the default for
11638 ** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
11639 ** 0644) as modified by the system umask. If m is not 0, then
11640 ** make the file creation mode be exactly m ignoring the umask.
11641 **
11642 ** The m parameter will be non-zero only when creating -wal, -journal,
11643 ** and -shm files. We want those files to have *exactly* the same
11644 ** permissions as their original database, unadulterated by the umask.
11645 ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
11646 ** transaction crashes and leaves behind hot journals, then any
11647 ** process that is able to write to the database will also be able to
11648 ** recover the hot journals.
11649 */
11650 static int robust_open(const char *z, int f, mode_t m){
11651 int fd;
11652 mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
11653 while(1){
11654 #if defined(O_CLOEXEC)
11655 fd = osOpen(z,f|O_CLOEXEC,m2);
11656 #else
11657 fd = osOpen(z,f,m2);
11658 #endif
11659 if( fd<0 ){
11660 if( errno==EINTR ) continue;
11661 break;
11662 }
11663 if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break;
11664 osClose(fd);
11665 sqlite3_log(SQLITE_WARNING,
11666 "attempt to open \"%s\" as file descriptor %d", z, fd);
11667 fd = -1;
11668 if( osOpen("/dev/null", f, m)<0 ) break;
11669 }
11670 if( fd>=0 ){
11671 if( m!=0 ){
11672 struct stat statbuf;
11673 if( osFstat(fd, &statbuf)==0
11674 && statbuf.st_size==0
11675 && (statbuf.st_mode&0777)!=m
11676 ){
11677 osFchmod(fd, m);
11678 }
11679 }
11680 #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
11681 osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
11682 #endif
11683 }
11684 return fd;
11685 }
11686
11687 /*
11688 ** Helper functions to obtain and relinquish the global mutex. The
11689 ** global mutex is used to protect the unixInodeInfo and
11690 ** vxworksFileId objects used by this file, all of which may be
11691 ** shared by multiple threads.
11692 **
11693 ** Function unixMutexHeld() is used to assert() that the global mutex
11694 ** is held when required. This function is only used as part of assert()
11695 ** statements. e.g.
11696 **
11697 ** unixEnterMutex()
11698 ** assert( unixMutexHeld() );
11699 ** unixEnterLeave()
11700 */
11701 static void unixEnterMutex(void){
11702 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
11703 }
11704 static void unixLeaveMutex(void){
11705 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
11706 }
11707 #ifdef SQLITE_DEBUG
11708 static int unixMutexHeld(void) {
11709 return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
11710 }
11711 #endif
11712
11713
11714 #ifdef SQLITE_HAVE_OS_TRACE
11715 /*
11716 ** Helper function for printing out trace information from debugging
11717 ** binaries. This returns the string representation of the supplied
11718 ** integer lock-type.
11719 */
11720 static const char *azFileLock(int eFileLock){
11721 switch( eFileLock ){
11722 case NO_LOCK: return "NONE";
11723 case SHARED_LOCK: return "SHARED";
11724 case RESERVED_LOCK: return "RESERVED";
11725 case PENDING_LOCK: return "PENDING";
11726 case EXCLUSIVE_LOCK: return "EXCLUSIVE";
11727 }
11728 return "ERROR";
11729 }
11730 #endif
11731
11732 #ifdef SQLITE_LOCK_TRACE
11733 /*
11734 ** Print out information about all locking operations.
11735 **
11736 ** This routine is used for troubleshooting locks on multithreaded
11737 ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
11738 ** command-line option on the compiler. This code is normally
11739 ** turned off.
11740 */
11741 static int lockTrace(int fd, int op, struct flock *p){
11742 char *zOpName, *zType;
11743 int s;
11744 int savedErrno;
11745 if( op==F_GETLK ){
11746 zOpName = "GETLK";
11747 }else if( op==F_SETLK ){
11748 zOpName = "SETLK";
11749 }else{
11750 s = osFcntl(fd, op, p);
11751 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
11752 return s;
11753 }
11754 if( p->l_type==F_RDLCK ){
11755 zType = "RDLCK";
11756 }else if( p->l_type==F_WRLCK ){
11757 zType = "WRLCK";
11758 }else if( p->l_type==F_UNLCK ){
11759 zType = "UNLCK";
11760 }else{
11761 assert( 0 );
11762 }
11763 assert( p->l_whence==SEEK_SET );
11764 s = osFcntl(fd, op, p);
11765 savedErrno = errno;
11766 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
11767 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
11768 (int)p->l_pid, s);
11769 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
11770 struct flock l2;
11771 l2 = *p;
11772 osFcntl(fd, F_GETLK, &l2);
11773 if( l2.l_type==F_RDLCK ){
11774 zType = "RDLCK";
11775 }else if( l2.l_type==F_WRLCK ){
11776 zType = "WRLCK";
11777 }else if( l2.l_type==F_UNLCK ){
11778 zType = "UNLCK";
11779 }else{
11780 assert( 0 );
11781 }
11782 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
11783 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
11784 }
11785 errno = savedErrno;
11786 return s;
11787 }
11788 #undef osFcntl
11789 #define osFcntl lockTrace
11790 #endif /* SQLITE_LOCK_TRACE */
11791
11792 /*
11793 ** Retry ftruncate() calls that fail due to EINTR
11794 **
11795 ** All calls to ftruncate() within this file should be made through
11796 ** this wrapper. On the Android platform, bypassing the logic below
11797 ** could lead to a corrupt database.
11798 */
11799 static int robust_ftruncate(int h, sqlite3_int64 sz){
11800 int rc;
11801 #ifdef __ANDROID__
11802 /* On Android, ftruncate() always uses 32-bit offsets, even if
11803 ** _FILE_OFFSET_BITS=64 is defined. This means it is unsafe to attempt to
11804 ** truncate a file to any size larger than 2GiB. Silently ignore any
11805 ** such attempts. */
11806 if( sz>(sqlite3_int64)0x7FFFFFFF ){
11807 rc = SQLITE_OK;
11808 }else
11809 #endif
11810 do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
11811 return rc;
11812 }
11813
11814 /*
11815 ** This routine translates a standard POSIX errno code into something
11816 ** useful to the clients of the sqlite3 functions. Specifically, it is
11817 ** intended to translate a variety of "try again" errors into SQLITE_BUSY
11818 ** and a variety of "please close the file descriptor NOW" errors into
11819 ** SQLITE_IOERR
11820 **
11821 ** Errors during initialization of locks, or file system support for locks,
11822 ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
11823 */
11824 static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
11825 assert( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
11826 (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
11827 (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
11828 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) );
11829 switch (posixError) {
11830 case EACCES:
11831 case EAGAIN:
11832 case ETIMEDOUT:
11833 case EBUSY:
11834 case EINTR:
11835 case ENOLCK:
11836 /* random NFS retry error, unless during file system support
11837 * introspection, in which it actually means what it says */
11838 return SQLITE_BUSY;
11839
11840 case EPERM:
11841 return SQLITE_PERM;
11842
11843 default:
11844 return sqliteIOErr;
11845 }
11846 }
11847
11848
11849 /******************************************************************************
11850 ****************** Begin Unique File ID Utility Used By VxWorks ***************
11851 **
11852 ** On most versions of unix, we can get a unique ID for a file by concatenating
11853 ** the device number and the inode number. But this does not work on VxWorks.
11854 ** On VxWorks, a unique file id must be based on the canonical filename.
11855 **
11856 ** A pointer to an instance of the following structure can be used as a
11857 ** unique file ID in VxWorks. Each instance of this structure contains
11858 ** a copy of the canonical filename. There is also a reference count.
11859 ** The structure is reclaimed when the number of pointers to it drops to
11860 ** zero.
11861 **
11862 ** There are never very many files open at one time and lookups are not
11863 ** a performance-critical path, so it is sufficient to put these
11864 ** structures on a linked list.
11865 */
11866 struct vxworksFileId {
11867 struct vxworksFileId *pNext; /* Next in a list of them all */
11868 int nRef; /* Number of references to this one */
11869 int nName; /* Length of the zCanonicalName[] string */
11870 char *zCanonicalName; /* Canonical filename */
11871 };
11872
11873 #if OS_VXWORKS
11874 /*
11875 ** All unique filenames are held on a linked list headed by this
11876 ** variable:
11877 */
11878 static struct vxworksFileId *vxworksFileList = 0;
11879
11880 /*
11881 ** Simplify a filename into its canonical form
11882 ** by making the following changes:
11883 **
11884 ** * removing any trailing and duplicate /
11885 ** * convert /./ into just /
11886 ** * convert /A/../ where A is any simple name into just /
11887 **
11888 ** Changes are made in-place. Return the new name length.
11889 **
11890 ** The original filename is in z[0..n-1]. Return the number of
11891 ** characters in the simplified name.
11892 */
11893 static int vxworksSimplifyName(char *z, int n){
11894 int i, j;
11895 while( n>1 && z[n-1]=='/' ){ n--; }
11896 for(i=j=0; i<n; i++){
11897 if( z[i]=='/' ){
11898 if( z[i+1]=='/' ) continue;
11899 if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
11900 i += 1;
11901 continue;
11902 }
11903 if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
11904 while( j>0 && z[j-1]!='/' ){ j--; }
11905 if( j>0 ){ j--; }
11906 i += 2;
11907 continue;
11908 }
11909 }
11910 z[j++] = z[i];
11911 }
11912 z[j] = 0;
11913 return j;
11914 }
11915
11916 /*
11917 ** Find a unique file ID for the given absolute pathname. Return
11918 ** a pointer to the vxworksFileId object. This pointer is the unique
11919 ** file ID.
11920 **
11921 ** The nRef field of the vxworksFileId object is incremented before
11922 ** the object is returned. A new vxworksFileId object is created
11923 ** and added to the global list if necessary.
11924 **
11925 ** If a memory allocation error occurs, return NULL.
11926 */
11927 static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
11928 struct vxworksFileId *pNew; /* search key and new file ID */
11929 struct vxworksFileId *pCandidate; /* For looping over existing file IDs */
11930 int n; /* Length of zAbsoluteName string */
11931
11932 assert( zAbsoluteName[0]=='/' );
11933 n = (int)strlen(zAbsoluteName);
11934 pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) );
11935 if( pNew==0 ) return 0;
11936 pNew->zCanonicalName = (char*)&pNew[1];
11937 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
11938 n = vxworksSimplifyName(pNew->zCanonicalName, n);
11939
11940 /* Search for an existing entry that matching the canonical name.
11941 ** If found, increment the reference count and return a pointer to
11942 ** the existing file ID.
11943 */
11944 unixEnterMutex();
11945 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
11946 if( pCandidate->nName==n
11947 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
11948 ){
11949 sqlite3_free(pNew);
11950 pCandidate->nRef++;
11951 unixLeaveMutex();
11952 return pCandidate;
11953 }
11954 }
11955
11956 /* No match was found. We will make a new file ID */
11957 pNew->nRef = 1;
11958 pNew->nName = n;
11959 pNew->pNext = vxworksFileList;
11960 vxworksFileList = pNew;
11961 unixLeaveMutex();
11962 return pNew;
11963 }
11964
11965 /*
11966 ** Decrement the reference count on a vxworksFileId object. Free
11967 ** the object when the reference count reaches zero.
11968 */
11969 static void vxworksReleaseFileId(struct vxworksFileId *pId){
11970 unixEnterMutex();
11971 assert( pId->nRef>0 );
11972 pId->nRef--;
11973 if( pId->nRef==0 ){
11974 struct vxworksFileId **pp;
11975 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
11976 assert( *pp==pId );
11977 *pp = pId->pNext;
11978 sqlite3_free(pId);
11979 }
11980 unixLeaveMutex();
11981 }
11982 #endif /* OS_VXWORKS */
11983 /*************** End of Unique File ID Utility Used By VxWorks ****************
11984 ******************************************************************************/
11985
11986
11987 /******************************************************************************
11988 *************************** Posix Advisory Locking ****************************
11989 **
11990 ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996)
11991 ** section 6.5.2.2 lines 483 through 490 specify that when a process
11992 ** sets or clears a lock, that operation overrides any prior locks set
11993 ** by the same process. It does not explicitly say so, but this implies
11994 ** that it overrides locks set by the same process using a different
11995 ** file descriptor. Consider this test case:
11996 **
11997 ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
11998 ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
11999 **
12000 ** Suppose ./file1 and ./file2 are really the same file (because
12001 ** one is a hard or symbolic link to the other) then if you set
12002 ** an exclusive lock on fd1, then try to get an exclusive lock
12003 ** on fd2, it works. I would have expected the second lock to
12004 ** fail since there was already a lock on the file due to fd1.
12005 ** But not so. Since both locks came from the same process, the
12006 ** second overrides the first, even though they were on different
12007 ** file descriptors opened on different file names.
12008 **
12009 ** This means that we cannot use POSIX locks to synchronize file access
12010 ** among competing threads of the same process. POSIX locks will work fine
12011 ** to synchronize access for threads in separate processes, but not
12012 ** threads within the same process.
12013 **
12014 ** To work around the problem, SQLite has to manage file locks internally
12015 ** on its own. Whenever a new database is opened, we have to find the
12016 ** specific inode of the database file (the inode is determined by the
12017 ** st_dev and st_ino fields of the stat structure that fstat() fills in)
12018 ** and check for locks already existing on that inode. When locks are
12019 ** created or removed, we have to look at our own internal record of the
12020 ** locks to see if another thread has previously set a lock on that same
12021 ** inode.
12022 **
12023 ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
12024 ** For VxWorks, we have to use the alternative unique ID system based on
12025 ** canonical filename and implemented in the previous division.)
12026 **
12027 ** The sqlite3_file structure for POSIX is no longer just an integer file
12028 ** descriptor. It is now a structure that holds the integer file
12029 ** descriptor and a pointer to a structure that describes the internal
12030 ** locks on the corresponding inode. There is one locking structure
12031 ** per inode, so if the same inode is opened twice, both unixFile structures
12032 ** point to the same locking structure. The locking structure keeps
12033 ** a reference count (so we will know when to delete it) and a "cnt"
12034 ** field that tells us its internal lock status. cnt==0 means the
12035 ** file is unlocked. cnt==-1 means the file has an exclusive lock.
12036 ** cnt>0 means there are cnt shared locks on the file.
12037 **
12038 ** Any attempt to lock or unlock a file first checks the locking
12039 ** structure. The fcntl() system call is only invoked to set a
12040 ** POSIX lock if the internal lock structure transitions between
12041 ** a locked and an unlocked state.
12042 **
12043 ** But wait: there are yet more problems with POSIX advisory locks.
12044 **
12045 ** If you close a file descriptor that points to a file that has locks,
12046 ** all locks on that file that are owned by the current process are
12047 ** released. To work around this problem, each unixInodeInfo object
12048 ** maintains a count of the number of pending locks on tha inode.
12049 ** When an attempt is made to close an unixFile, if there are
12050 ** other unixFile open on the same inode that are holding locks, the call
12051 ** to close() the file descriptor is deferred until all of the locks clear.
12052 ** The unixInodeInfo structure keeps a list of file descriptors that need to
12053 ** be closed and that list is walked (and cleared) when the last lock
12054 ** clears.
12055 **
12056 ** Yet another problem: LinuxThreads do not play well with posix locks.
12057 **
12058 ** Many older versions of linux use the LinuxThreads library which is
12059 ** not posix compliant. Under LinuxThreads, a lock created by thread
12060 ** A cannot be modified or overridden by a different thread B.
12061 ** Only thread A can modify the lock. Locking behavior is correct
12062 ** if the appliation uses the newer Native Posix Thread Library (NPTL)
12063 ** on linux - with NPTL a lock created by thread A can override locks
12064 ** in thread B. But there is no way to know at compile-time which
12065 ** threading library is being used. So there is no way to know at
12066 ** compile-time whether or not thread A can override locks on thread B.
12067 ** One has to do a run-time check to discover the behavior of the
12068 ** current process.
12069 **
12070 ** SQLite used to support LinuxThreads. But support for LinuxThreads
12071 ** was dropped beginning with version 3.7.0. SQLite will still work with
12072 ** LinuxThreads provided that (1) there is no more than one connection
12073 ** per database file in the same process and (2) database connections
12074 ** do not move across threads.
12075 */
12076
12077 /*
12078 ** An instance of the following structure serves as the key used
12079 ** to locate a particular unixInodeInfo object.
12080 */
12081 struct unixFileId {
12082 dev_t dev; /* Device number */
12083 #if OS_VXWORKS
12084 struct vxworksFileId *pId; /* Unique file ID for vxworks. */
12085 #else
12086 /* We are told that some versions of Android contain a bug that
12087 ** sizes ino_t at only 32-bits instead of 64-bits. (See
12088 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
12089 ** To work around this, always allocate 64-bits for the inode number.
12090 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
12091 ** but that should not be a big deal. */
12092 /* WAS: ino_t ino; */
12093 u64 ino; /* Inode number */
12094 #endif
12095 };
12096
12097 /*
12098 ** An instance of the following structure is allocated for each open
12099 ** inode. Or, on LinuxThreads, there is one of these structures for
12100 ** each inode opened by each thread.
12101 **
12102 ** A single inode can have multiple file descriptors, so each unixFile
12103 ** structure contains a pointer to an instance of this object and this
12104 ** object keeps a count of the number of unixFile pointing to it.
12105 */
12106 struct unixInodeInfo {
12107 struct unixFileId fileId; /* The lookup key */
12108 int nShared; /* Number of SHARED locks held */
12109 unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */
12110 unsigned char bProcessLock; /* An exclusive process lock is held */
12111 int nRef; /* Number of pointers to this structure */
12112 unixShmNode *pShmNode; /* Shared memory associated with this inode */
12113 int nLock; /* Number of outstanding file locks */
12114 UnixUnusedFd *pUnused; /* Unused file descriptors to close */
12115 unixInodeInfo *pNext; /* List of all unixInodeInfo objects */
12116 unixInodeInfo *pPrev; /* .... doubly linked */
12117 #if SQLITE_ENABLE_LOCKING_STYLE
12118 unsigned long long sharedByte; /* for AFP simulated shared lock */
12119 #endif
12120 #if OS_VXWORKS
12121 sem_t *pSem; /* Named POSIX semaphore */
12122 char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */
12123 #endif
12124 };
12125
12126 /*
12127 ** A lists of all unixInodeInfo objects.
12128 */
12129 static unixInodeInfo *inodeList = 0;
12130
12131 /*
12132 **
12133 ** This function - unixLogErrorAtLine(), is only ever called via the macro
12134 ** unixLogError().
12135 **
12136 ** It is invoked after an error occurs in an OS function and errno has been
12137 ** set. It logs a message using sqlite3_log() containing the current value of
12138 ** errno and, if possible, the human-readable equivalent from strerror() or
12139 ** strerror_r().
12140 **
12141 ** The first argument passed to the macro should be the error code that
12142 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
12143 ** The two subsequent arguments should be the name of the OS function that
12144 ** failed (e.g. "unlink", "open") and the associated file-system path,
12145 ** if any.
12146 */
12147 #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__)
12148 static int unixLogErrorAtLine(
12149 int errcode, /* SQLite error code */
12150 const char *zFunc, /* Name of OS function that failed */
12151 const char *zPath, /* File path associated with error */
12152 int iLine /* Source line number where error occurred */
12153 ){
12154 char *zErr; /* Message from strerror() or equivalent */
12155 int iErrno = errno; /* Saved syscall error number */
12156
12157 /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
12158 ** the strerror() function to obtain the human-readable error message
12159 ** equivalent to errno. Otherwise, use strerror_r().
12160 */
12161 #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
12162 char aErr[80];
12163 memset(aErr, 0, sizeof(aErr));
12164 zErr = aErr;
12165
12166 /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
12167 ** assume that the system provides the GNU version of strerror_r() that
12168 ** returns a pointer to a buffer containing the error message. That pointer
12169 ** may point to aErr[], or it may point to some static storage somewhere.
12170 ** Otherwise, assume that the system provides the POSIX version of
12171 ** strerror_r(), which always writes an error message into aErr[].
12172 **
12173 ** If the code incorrectly assumes that it is the POSIX version that is
12174 ** available, the error message will often be an empty string. Not a
12175 ** huge problem. Incorrectly concluding that the GNU version is available
12176 ** could lead to a segfault though.
12177 */
12178 #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
12179 zErr =
12180 # endif
12181 strerror_r(iErrno, aErr, sizeof(aErr)-1);
12182
12183 #elif SQLITE_THREADSAFE
12184 /* This is a threadsafe build, but strerror_r() is not available. */
12185 zErr = "";
12186 #else
12187 /* Non-threadsafe build, use strerror(). */
12188 zErr = strerror(iErrno);
12189 #endif
12190
12191 if( zPath==0 ) zPath = "";
12192 sqlite3_log(errcode,
12193 "os_unix.c:%d: (%d) %s(%s) - %s",
12194 iLine, iErrno, zFunc, zPath, zErr
12195 );
12196
12197 return errcode;
12198 }
12199
12200 /*
12201 ** Close a file descriptor.
12202 **
12203 ** We assume that close() almost always works, since it is only in a
12204 ** very sick application or on a very sick platform that it might fail.
12205 ** If it does fail, simply leak the file descriptor, but do log the
12206 ** error.
12207 **
12208 ** Note that it is not safe to retry close() after EINTR since the
12209 ** file descriptor might have already been reused by another thread.
12210 ** So we don't even try to recover from an EINTR. Just log the error
12211 ** and move on.
12212 */
12213 static void robust_close(unixFile *pFile, int h, int lineno){
12214 if( osClose(h) ){
12215 unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
12216 pFile ? pFile->zPath : 0, lineno);
12217 }
12218 }
12219
12220 /*
12221 ** Set the pFile->lastErrno. Do this in a subroutine as that provides
12222 ** a convenient place to set a breakpoint.
12223 */
12224 static void storeLastErrno(unixFile *pFile, int error){
12225 pFile->lastErrno = error;
12226 }
12227
12228 /*
12229 ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
12230 */
12231 static void closePendingFds(unixFile *pFile){
12232 unixInodeInfo *pInode = pFile->pInode;
12233 UnixUnusedFd *p;
12234 UnixUnusedFd *pNext;
12235 for(p=pInode->pUnused; p; p=pNext){
12236 pNext = p->pNext;
12237 robust_close(pFile, p->fd, __LINE__);
12238 sqlite3_free(p);
12239 }
12240 pInode->pUnused = 0;
12241 }
12242
12243 /*
12244 ** Release a unixInodeInfo structure previously allocated by findInodeInfo().
12245 **
12246 ** The mutex entered using the unixEnterMutex() function must be held
12247 ** when this function is called.
12248 */
12249 static void releaseInodeInfo(unixFile *pFile){
12250 unixInodeInfo *pInode = pFile->pInode;
12251 assert( unixMutexHeld() );
12252 if( ALWAYS(pInode) ){
12253 pInode->nRef--;
12254 if( pInode->nRef==0 ){
12255 assert( pInode->pShmNode==0 );
12256 closePendingFds(pFile);
12257 if( pInode->pPrev ){
12258 assert( pInode->pPrev->pNext==pInode );
12259 pInode->pPrev->pNext = pInode->pNext;
12260 }else{
12261 assert( inodeList==pInode );
12262 inodeList = pInode->pNext;
12263 }
12264 if( pInode->pNext ){
12265 assert( pInode->pNext->pPrev==pInode );
12266 pInode->pNext->pPrev = pInode->pPrev;
12267 }
12268 sqlite3_free(pInode);
12269 }
12270 }
12271 }
12272
12273 /*
12274 ** Given a file descriptor, locate the unixInodeInfo object that
12275 ** describes that file descriptor. Create a new one if necessary. The
12276 ** return value might be uninitialized if an error occurs.
12277 **
12278 ** The mutex entered using the unixEnterMutex() function must be held
12279 ** when this function is called.
12280 **
12281 ** Return an appropriate error code.
12282 */
12283 static int findInodeInfo(
12284 unixFile *pFile, /* Unix file with file desc used in the key */
12285 unixInodeInfo **ppInode /* Return the unixInodeInfo object here */
12286 ){
12287 int rc; /* System call return code */
12288 int fd; /* The file descriptor for pFile */
12289 struct unixFileId fileId; /* Lookup key for the unixInodeInfo */
12290 struct stat statbuf; /* Low-level file information */
12291 unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */
12292
12293 assert( unixMutexHeld() );
12294
12295 /* Get low-level information about the file that we can used to
12296 ** create a unique name for the file.
12297 */
12298 fd = pFile->h;
12299 rc = osFstat(fd, &statbuf);
12300 if( rc!=0 ){
12301 storeLastErrno(pFile, errno);
12302 #if defined(EOVERFLOW) && defined(SQLITE_DISABLE_LFS)
12303 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
12304 #endif
12305 return SQLITE_IOERR;
12306 }
12307
12308 #ifdef __APPLE__
12309 /* On OS X on an msdos filesystem, the inode number is reported
12310 ** incorrectly for zero-size files. See ticket #3260. To work
12311 ** around this problem (we consider it a bug in OS X, not SQLite)
12312 ** we always increase the file size to 1 by writing a single byte
12313 ** prior to accessing the inode number. The one byte written is
12314 ** an ASCII 'S' character which also happens to be the first byte
12315 ** in the header of every SQLite database. In this way, if there
12316 ** is a race condition such that another thread has already populated
12317 ** the first page of the database, no damage is done.
12318 */
12319 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
12320 do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
12321 if( rc!=1 ){
12322 storeLastErrno(pFile, errno);
12323 return SQLITE_IOERR;
12324 }
12325 rc = osFstat(fd, &statbuf);
12326 if( rc!=0 ){
12327 storeLastErrno(pFile, errno);
12328 return SQLITE_IOERR;
12329 }
12330 }
12331 #endif
12332
12333 memset(&fileId, 0, sizeof(fileId));
12334 fileId.dev = statbuf.st_dev;
12335 #if OS_VXWORKS
12336 fileId.pId = pFile->pId;
12337 #else
12338 fileId.ino = (u64)statbuf.st_ino;
12339 #endif
12340 pInode = inodeList;
12341 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
12342 pInode = pInode->pNext;
12343 }
12344 if( pInode==0 ){
12345 pInode = sqlite3_malloc64( sizeof(*pInode) );
12346 if( pInode==0 ){
12347 return SQLITE_NOMEM_BKPT;
12348 }
12349 memset(pInode, 0, sizeof(*pInode));
12350 memcpy(&pInode->fileId, &fileId, sizeof(fileId));
12351 pInode->nRef = 1;
12352 pInode->pNext = inodeList;
12353 pInode->pPrev = 0;
12354 if( inodeList ) inodeList->pPrev = pInode;
12355 inodeList = pInode;
12356 }else{
12357 pInode->nRef++;
12358 }
12359 *ppInode = pInode;
12360 return SQLITE_OK;
12361 }
12362
12363 /*
12364 ** Return TRUE if pFile has been renamed or unlinked since it was first opened.
12365 */
12366 static int fileHasMoved(unixFile *pFile){
12367 #if OS_VXWORKS
12368 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
12369 #else
12370 struct stat buf;
12371
12372 /* TODO(shess): This check doesn't work when the Chromium's WebDB code is
12373 ** running in the sandbox.
12374 */
12375 return 0;
12376
12377 return pFile->pInode!=0 &&
12378 (osStat(pFile->zPath, &buf)!=0
12379 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
12380 #endif
12381 }
12382
12383
12384 /*
12385 ** Check a unixFile that is a database. Verify the following:
12386 **
12387 ** (1) There is exactly one hard link on the file
12388 ** (2) The file is not a symbolic link
12389 ** (3) The file has not been renamed or unlinked
12390 **
12391 ** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
12392 */
12393 static void verifyDbFile(unixFile *pFile){
12394 struct stat buf;
12395 int rc;
12396
12397 /* These verifications occurs for the main database only */
12398 if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
12399
12400 rc = osFstat(pFile->h, &buf);
12401 if( rc!=0 ){
12402 sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
12403 return;
12404 }
12405 if( buf.st_nlink==0 ){
12406 sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
12407 return;
12408 }
12409 if( buf.st_nlink>1 ){
12410 sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
12411 return;
12412 }
12413 if( fileHasMoved(pFile) ){
12414 sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
12415 return;
12416 }
12417 }
12418
12419
12420 /*
12421 ** This routine checks if there is a RESERVED lock held on the specified
12422 ** file by this or any other process. If such a lock is held, set *pResOut
12423 ** to a non-zero value otherwise *pResOut is set to zero. The return value
12424 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
12425 */
12426 static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
12427 int rc = SQLITE_OK;
12428 int reserved = 0;
12429 unixFile *pFile = (unixFile*)id;
12430
12431 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
12432
12433 assert( pFile );
12434 assert( pFile->eFileLock<=SHARED_LOCK );
12435 unixEnterMutex(); /* Because pFile->pInode is shared across threads */
12436
12437 /* Check if a thread in this process holds such a lock */
12438 if( pFile->pInode->eFileLock>SHARED_LOCK ){
12439 reserved = 1;
12440 }
12441
12442 /* Otherwise see if some other process holds it.
12443 */
12444 #ifndef __DJGPP__
12445 if( !reserved && !pFile->pInode->bProcessLock ){
12446 struct flock lock;
12447 lock.l_whence = SEEK_SET;
12448 lock.l_start = RESERVED_BYTE;
12449 lock.l_len = 1;
12450 lock.l_type = F_WRLCK;
12451 if( osFcntl(pFile->h, F_GETLK, &lock) ){
12452 rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
12453 storeLastErrno(pFile, errno);
12454 } else if( lock.l_type!=F_UNLCK ){
12455 reserved = 1;
12456 }
12457 }
12458 #endif
12459
12460 unixLeaveMutex();
12461 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
12462
12463 *pResOut = reserved;
12464 return rc;
12465 }
12466
12467 /*
12468 ** Attempt to set a system-lock on the file pFile. The lock is
12469 ** described by pLock.
12470 **
12471 ** If the pFile was opened read/write from unix-excl, then the only lock
12472 ** ever obtained is an exclusive lock, and it is obtained exactly once
12473 ** the first time any lock is attempted. All subsequent system locking
12474 ** operations become no-ops. Locking operations still happen internally,
12475 ** in order to coordinate access between separate database connections
12476 ** within this process, but all of that is handled in memory and the
12477 ** operating system does not participate.
12478 **
12479 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
12480 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
12481 ** and is read-only.
12482 **
12483 ** Zero is returned if the call completes successfully, or -1 if a call
12484 ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
12485 */
12486 static int unixFileLock(unixFile *pFile, struct flock *pLock){
12487 int rc;
12488 unixInodeInfo *pInode = pFile->pInode;
12489 assert( unixMutexHeld() );
12490 assert( pInode!=0 );
12491 if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){
12492 if( pInode->bProcessLock==0 ){
12493 struct flock lock;
12494 assert( pInode->nLock==0 );
12495 lock.l_whence = SEEK_SET;
12496 lock.l_start = SHARED_FIRST;
12497 lock.l_len = SHARED_SIZE;
12498 lock.l_type = F_WRLCK;
12499 rc = osFcntl(pFile->h, F_SETLK, &lock);
12500 if( rc<0 ) return rc;
12501 pInode->bProcessLock = 1;
12502 pInode->nLock++;
12503 }else{
12504 rc = 0;
12505 }
12506 }else{
12507 rc = osFcntl(pFile->h, F_SETLK, pLock);
12508 }
12509 return rc;
12510 }
12511
12512 /*
12513 ** Lock the file with the lock specified by parameter eFileLock - one
12514 ** of the following:
12515 **
12516 ** (1) SHARED_LOCK
12517 ** (2) RESERVED_LOCK
12518 ** (3) PENDING_LOCK
12519 ** (4) EXCLUSIVE_LOCK
12520 **
12521 ** Sometimes when requesting one lock state, additional lock states
12522 ** are inserted in between. The locking might fail on one of the later
12523 ** transitions leaving the lock state different from what it started but
12524 ** still short of its goal. The following chart shows the allowed
12525 ** transitions and the inserted intermediate states:
12526 **
12527 ** UNLOCKED -> SHARED
12528 ** SHARED -> RESERVED
12529 ** SHARED -> (PENDING) -> EXCLUSIVE
12530 ** RESERVED -> (PENDING) -> EXCLUSIVE
12531 ** PENDING -> EXCLUSIVE
12532 **
12533 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
12534 ** routine to lower a locking level.
12535 */
12536 static int unixLock(sqlite3_file *id, int eFileLock){
12537 /* The following describes the implementation of the various locks and
12538 ** lock transitions in terms of the POSIX advisory shared and exclusive
12539 ** lock primitives (called read-locks and write-locks below, to avoid
12540 ** confusion with SQLite lock names). The algorithms are complicated
12541 ** slightly in order to be compatible with Windows95 systems simultaneously
12542 ** accessing the same database file, in case that is ever required.
12543 **
12544 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
12545 ** byte', each single bytes at well known offsets, and the 'shared byte
12546 ** range', a range of 510 bytes at a well known offset.
12547 **
12548 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
12549 ** byte'. If this is successful, 'shared byte range' is read-locked
12550 ** and the lock on the 'pending byte' released. (Legacy note: When
12551 ** SQLite was first developed, Windows95 systems were still very common,
12552 ** and Widnows95 lacks a shared-lock capability. So on Windows95, a
12553 ** single randomly selected by from the 'shared byte range' is locked.
12554 ** Windows95 is now pretty much extinct, but this work-around for the
12555 ** lack of shared-locks on Windows95 lives on, for backwards
12556 ** compatibility.)
12557 **
12558 ** A process may only obtain a RESERVED lock after it has a SHARED lock.
12559 ** A RESERVED lock is implemented by grabbing a write-lock on the
12560 ** 'reserved byte'.
12561 **
12562 ** A process may only obtain a PENDING lock after it has obtained a
12563 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
12564 ** on the 'pending byte'. This ensures that no new SHARED locks can be
12565 ** obtained, but existing SHARED locks are allowed to persist. A process
12566 ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
12567 ** This property is used by the algorithm for rolling back a journal file
12568 ** after a crash.
12569 **
12570 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
12571 ** implemented by obtaining a write-lock on the entire 'shared byte
12572 ** range'. Since all other locks require a read-lock on one of the bytes
12573 ** within this range, this ensures that no other locks are held on the
12574 ** database.
12575 */
12576 int rc = SQLITE_OK;
12577 unixFile *pFile = (unixFile*)id;
12578 unixInodeInfo *pInode;
12579 struct flock lock;
12580 int tErrno = 0;
12581
12582 assert( pFile );
12583 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
12584 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
12585 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
12586 osGetpid(0)));
12587
12588 /* If there is already a lock of this type or more restrictive on the
12589 ** unixFile, do nothing. Don't use the end_lock: exit path, as
12590 ** unixEnterMutex() hasn't been called yet.
12591 */
12592 if( pFile->eFileLock>=eFileLock ){
12593 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
12594 azFileLock(eFileLock)));
12595 return SQLITE_OK;
12596 }
12597
12598 /* Make sure the locking sequence is correct.
12599 ** (1) We never move from unlocked to anything higher than shared lock.
12600 ** (2) SQLite never explicitly requests a pendig lock.
12601 ** (3) A shared lock is always held when a reserve lock is requested.
12602 */
12603 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
12604 assert( eFileLock!=PENDING_LOCK );
12605 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
12606
12607 /* This mutex is needed because pFile->pInode is shared across threads
12608 */
12609 unixEnterMutex();
12610 pInode = pFile->pInode;
12611
12612 /* If some thread using this PID has a lock via a different unixFile*
12613 ** handle that precludes the requested lock, return BUSY.
12614 */
12615 if( (pFile->eFileLock!=pInode->eFileLock &&
12616 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
12617 ){
12618 rc = SQLITE_BUSY;
12619 goto end_lock;
12620 }
12621
12622 /* If a SHARED lock is requested, and some thread using this PID already
12623 ** has a SHARED or RESERVED lock, then increment reference counts and
12624 ** return SQLITE_OK.
12625 */
12626 if( eFileLock==SHARED_LOCK &&
12627 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
12628 assert( eFileLock==SHARED_LOCK );
12629 assert( pFile->eFileLock==0 );
12630 assert( pInode->nShared>0 );
12631 pFile->eFileLock = SHARED_LOCK;
12632 pInode->nShared++;
12633 pInode->nLock++;
12634 goto end_lock;
12635 }
12636
12637
12638 /* A PENDING lock is needed before acquiring a SHARED lock and before
12639 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
12640 ** be released.
12641 */
12642 lock.l_len = 1L;
12643 lock.l_whence = SEEK_SET;
12644 if( eFileLock==SHARED_LOCK
12645 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
12646 ){
12647 lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
12648 lock.l_start = PENDING_BYTE;
12649 if( unixFileLock(pFile, &lock) ){
12650 tErrno = errno;
12651 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
12652 if( rc!=SQLITE_BUSY ){
12653 storeLastErrno(pFile, tErrno);
12654 }
12655 goto end_lock;
12656 }
12657 }
12658
12659
12660 /* If control gets to this point, then actually go ahead and make
12661 ** operating system calls for the specified lock.
12662 */
12663 if( eFileLock==SHARED_LOCK ){
12664 assert( pInode->nShared==0 );
12665 assert( pInode->eFileLock==0 );
12666 assert( rc==SQLITE_OK );
12667
12668 /* Now get the read-lock */
12669 lock.l_start = SHARED_FIRST;
12670 lock.l_len = SHARED_SIZE;
12671 if( unixFileLock(pFile, &lock) ){
12672 tErrno = errno;
12673 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
12674 }
12675
12676 /* Drop the temporary PENDING lock */
12677 lock.l_start = PENDING_BYTE;
12678 lock.l_len = 1L;
12679 lock.l_type = F_UNLCK;
12680 if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
12681 /* This could happen with a network mount */
12682 tErrno = errno;
12683 rc = SQLITE_IOERR_UNLOCK;
12684 }
12685
12686 if( rc ){
12687 if( rc!=SQLITE_BUSY ){
12688 storeLastErrno(pFile, tErrno);
12689 }
12690 goto end_lock;
12691 }else{
12692 pFile->eFileLock = SHARED_LOCK;
12693 pInode->nLock++;
12694 pInode->nShared = 1;
12695 }
12696 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
12697 /* We are trying for an exclusive lock but another thread in this
12698 ** same process is still holding a shared lock. */
12699 rc = SQLITE_BUSY;
12700 }else{
12701 /* The request was for a RESERVED or EXCLUSIVE lock. It is
12702 ** assumed that there is a SHARED or greater lock on the file
12703 ** already.
12704 */
12705 assert( 0!=pFile->eFileLock );
12706 lock.l_type = F_WRLCK;
12707
12708 assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
12709 if( eFileLock==RESERVED_LOCK ){
12710 lock.l_start = RESERVED_BYTE;
12711 lock.l_len = 1L;
12712 }else{
12713 lock.l_start = SHARED_FIRST;
12714 lock.l_len = SHARED_SIZE;
12715 }
12716
12717 if( unixFileLock(pFile, &lock) ){
12718 tErrno = errno;
12719 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
12720 if( rc!=SQLITE_BUSY ){
12721 storeLastErrno(pFile, tErrno);
12722 }
12723 }
12724 }
12725
12726
12727 #ifdef SQLITE_DEBUG
12728 /* Set up the transaction-counter change checking flags when
12729 ** transitioning from a SHARED to a RESERVED lock. The change
12730 ** from SHARED to RESERVED marks the beginning of a normal
12731 ** write operation (not a hot journal rollback).
12732 */
12733 if( rc==SQLITE_OK
12734 && pFile->eFileLock<=SHARED_LOCK
12735 && eFileLock==RESERVED_LOCK
12736 ){
12737 pFile->transCntrChng = 0;
12738 pFile->dbUpdate = 0;
12739 pFile->inNormalWrite = 1;
12740 }
12741 #endif
12742
12743
12744 if( rc==SQLITE_OK ){
12745 pFile->eFileLock = eFileLock;
12746 pInode->eFileLock = eFileLock;
12747 }else if( eFileLock==EXCLUSIVE_LOCK ){
12748 pFile->eFileLock = PENDING_LOCK;
12749 pInode->eFileLock = PENDING_LOCK;
12750 }
12751
12752 end_lock:
12753 unixLeaveMutex();
12754 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
12755 rc==SQLITE_OK ? "ok" : "failed"));
12756 return rc;
12757 }
12758
12759 /*
12760 ** Add the file descriptor used by file handle pFile to the corresponding
12761 ** pUnused list.
12762 */
12763 static void setPendingFd(unixFile *pFile){
12764 unixInodeInfo *pInode = pFile->pInode;
12765 UnixUnusedFd *p = pFile->pUnused;
12766 p->pNext = pInode->pUnused;
12767 pInode->pUnused = p;
12768 pFile->h = -1;
12769 pFile->pUnused = 0;
12770 }
12771
12772 /*
12773 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
12774 ** must be either NO_LOCK or SHARED_LOCK.
12775 **
12776 ** If the locking level of the file descriptor is already at or below
12777 ** the requested locking level, this routine is a no-op.
12778 **
12779 ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
12780 ** the byte range is divided into 2 parts and the first part is unlocked then
12781 ** set to a read lock, then the other part is simply unlocked. This works
12782 ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
12783 ** remove the write lock on a region when a read lock is set.
12784 */
12785 static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
12786 unixFile *pFile = (unixFile*)id;
12787 unixInodeInfo *pInode;
12788 struct flock lock;
12789 int rc = SQLITE_OK;
12790
12791 assert( pFile );
12792 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
12793 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
12794 osGetpid(0)));
12795
12796 assert( eFileLock<=SHARED_LOCK );
12797 if( pFile->eFileLock<=eFileLock ){
12798 return SQLITE_OK;
12799 }
12800 unixEnterMutex();
12801 pInode = pFile->pInode;
12802 assert( pInode->nShared!=0 );
12803 if( pFile->eFileLock>SHARED_LOCK ){
12804 assert( pInode->eFileLock==pFile->eFileLock );
12805
12806 #ifdef SQLITE_DEBUG
12807 /* When reducing a lock such that other processes can start
12808 ** reading the database file again, make sure that the
12809 ** transaction counter was updated if any part of the database
12810 ** file changed. If the transaction counter is not updated,
12811 ** other connections to the same file might not realize that
12812 ** the file has changed and hence might not know to flush their
12813 ** cache. The use of a stale cache can lead to database corruption.
12814 */
12815 pFile->inNormalWrite = 0;
12816 #endif
12817
12818 /* downgrading to a shared lock on NFS involves clearing the write lock
12819 ** before establishing the readlock - to avoid a race condition we downgrade
12820 ** the lock in 2 blocks, so that part of the range will be covered by a
12821 ** write lock until the rest is covered by a read lock:
12822 ** 1: [WWWWW]
12823 ** 2: [....W]
12824 ** 3: [RRRRW]
12825 ** 4: [RRRR.]
12826 */
12827 if( eFileLock==SHARED_LOCK ){
12828 #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
12829 (void)handleNFSUnlock;
12830 assert( handleNFSUnlock==0 );
12831 #endif
12832 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
12833 if( handleNFSUnlock ){
12834 int tErrno; /* Error code from system call errors */
12835 off_t divSize = SHARED_SIZE - 1;
12836
12837 lock.l_type = F_UNLCK;
12838 lock.l_whence = SEEK_SET;
12839 lock.l_start = SHARED_FIRST;
12840 lock.l_len = divSize;
12841 if( unixFileLock(pFile, &lock)==(-1) ){
12842 tErrno = errno;
12843 rc = SQLITE_IOERR_UNLOCK;
12844 storeLastErrno(pFile, tErrno);
12845 goto end_unlock;
12846 }
12847 lock.l_type = F_RDLCK;
12848 lock.l_whence = SEEK_SET;
12849 lock.l_start = SHARED_FIRST;
12850 lock.l_len = divSize;
12851 if( unixFileLock(pFile, &lock)==(-1) ){
12852 tErrno = errno;
12853 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
12854 if( IS_LOCK_ERROR(rc) ){
12855 storeLastErrno(pFile, tErrno);
12856 }
12857 goto end_unlock;
12858 }
12859 lock.l_type = F_UNLCK;
12860 lock.l_whence = SEEK_SET;
12861 lock.l_start = SHARED_FIRST+divSize;
12862 lock.l_len = SHARED_SIZE-divSize;
12863 if( unixFileLock(pFile, &lock)==(-1) ){
12864 tErrno = errno;
12865 rc = SQLITE_IOERR_UNLOCK;
12866 storeLastErrno(pFile, tErrno);
12867 goto end_unlock;
12868 }
12869 }else
12870 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
12871 {
12872 lock.l_type = F_RDLCK;
12873 lock.l_whence = SEEK_SET;
12874 lock.l_start = SHARED_FIRST;
12875 lock.l_len = SHARED_SIZE;
12876 if( unixFileLock(pFile, &lock) ){
12877 /* In theory, the call to unixFileLock() cannot fail because another
12878 ** process is holding an incompatible lock. If it does, this
12879 ** indicates that the other process is not following the locking
12880 ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
12881 ** SQLITE_BUSY would confuse the upper layer (in practice it causes
12882 ** an assert to fail). */
12883 rc = SQLITE_IOERR_RDLOCK;
12884 storeLastErrno(pFile, errno);
12885 goto end_unlock;
12886 }
12887 }
12888 }
12889 lock.l_type = F_UNLCK;
12890 lock.l_whence = SEEK_SET;
12891 lock.l_start = PENDING_BYTE;
12892 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE );
12893 if( unixFileLock(pFile, &lock)==0 ){
12894 pInode->eFileLock = SHARED_LOCK;
12895 }else{
12896 rc = SQLITE_IOERR_UNLOCK;
12897 storeLastErrno(pFile, errno);
12898 goto end_unlock;
12899 }
12900 }
12901 if( eFileLock==NO_LOCK ){
12902 /* Decrement the shared lock counter. Release the lock using an
12903 ** OS call only when all threads in this same process have released
12904 ** the lock.
12905 */
12906 pInode->nShared--;
12907 if( pInode->nShared==0 ){
12908 lock.l_type = F_UNLCK;
12909 lock.l_whence = SEEK_SET;
12910 lock.l_start = lock.l_len = 0L;
12911 if( unixFileLock(pFile, &lock)==0 ){
12912 pInode->eFileLock = NO_LOCK;
12913 }else{
12914 rc = SQLITE_IOERR_UNLOCK;
12915 storeLastErrno(pFile, errno);
12916 pInode->eFileLock = NO_LOCK;
12917 pFile->eFileLock = NO_LOCK;
12918 }
12919 }
12920
12921 /* Decrement the count of locks against this same file. When the
12922 ** count reaches zero, close any other file descriptors whose close
12923 ** was deferred because of outstanding locks.
12924 */
12925 pInode->nLock--;
12926 assert( pInode->nLock>=0 );
12927 if( pInode->nLock==0 ){
12928 closePendingFds(pFile);
12929 }
12930 }
12931
12932 end_unlock:
12933 unixLeaveMutex();
12934 if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
12935 return rc;
12936 }
12937
12938 /*
12939 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
12940 ** must be either NO_LOCK or SHARED_LOCK.
12941 **
12942 ** If the locking level of the file descriptor is already at or below
12943 ** the requested locking level, this routine is a no-op.
12944 */
12945 static int unixUnlock(sqlite3_file *id, int eFileLock){
12946 #if SQLITE_MAX_MMAP_SIZE>0
12947 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
12948 #endif
12949 return posixUnlock(id, eFileLock, 0);
12950 }
12951
12952 #if SQLITE_MAX_MMAP_SIZE>0
12953 static int unixMapfile(unixFile *pFd, i64 nByte);
12954 static void unixUnmapfile(unixFile *pFd);
12955 #endif
12956
12957 /*
12958 ** This function performs the parts of the "close file" operation
12959 ** common to all locking schemes. It closes the directory and file
12960 ** handles, if they are valid, and sets all fields of the unixFile
12961 ** structure to 0.
12962 **
12963 ** It is *not* necessary to hold the mutex when this routine is called,
12964 ** even on VxWorks. A mutex will be acquired on VxWorks by the
12965 ** vxworksReleaseFileId() routine.
12966 */
12967 static int closeUnixFile(sqlite3_file *id){
12968 unixFile *pFile = (unixFile*)id;
12969 #if SQLITE_MAX_MMAP_SIZE>0
12970 unixUnmapfile(pFile);
12971 #endif
12972 if( pFile->h>=0 ){
12973 robust_close(pFile, pFile->h, __LINE__);
12974 pFile->h = -1;
12975 }
12976 #if OS_VXWORKS
12977 if( pFile->pId ){
12978 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
12979 osUnlink(pFile->pId->zCanonicalName);
12980 }
12981 vxworksReleaseFileId(pFile->pId);
12982 pFile->pId = 0;
12983 }
12984 #endif
12985 #ifdef SQLITE_UNLINK_AFTER_CLOSE
12986 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
12987 osUnlink(pFile->zPath);
12988 sqlite3_free(*(char**)&pFile->zPath);
12989 pFile->zPath = 0;
12990 }
12991 #endif
12992 OSTRACE(("CLOSE %-3d\n", pFile->h));
12993 OpenCounter(-1);
12994 sqlite3_free(pFile->pUnused);
12995 memset(pFile, 0, sizeof(unixFile));
12996 return SQLITE_OK;
12997 }
12998
12999 /*
13000 ** Close a file.
13001 */
13002 static int unixClose(sqlite3_file *id){
13003 int rc = SQLITE_OK;
13004 unixFile *pFile = (unixFile *)id;
13005 verifyDbFile(pFile);
13006 unixUnlock(id, NO_LOCK);
13007 unixEnterMutex();
13008
13009 /* unixFile.pInode is always valid here. Otherwise, a different close
13010 ** routine (e.g. nolockClose()) would be called instead.
13011 */
13012 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
13013 if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
13014 /* If there are outstanding locks, do not actually close the file just
13015 ** yet because that would clear those locks. Instead, add the file
13016 ** descriptor to pInode->pUnused list. It will be automatically closed
13017 ** when the last lock is cleared.
13018 */
13019 setPendingFd(pFile);
13020 }
13021 releaseInodeInfo(pFile);
13022 rc = closeUnixFile(id);
13023 unixLeaveMutex();
13024 return rc;
13025 }
13026
13027 /************** End of the posix advisory lock implementation *****************
13028 ******************************************************************************/
13029
13030 /******************************************************************************
13031 ****************************** No-op Locking **********************************
13032 **
13033 ** Of the various locking implementations available, this is by far the
13034 ** simplest: locking is ignored. No attempt is made to lock the database
13035 ** file for reading or writing.
13036 **
13037 ** This locking mode is appropriate for use on read-only databases
13038 ** (ex: databases that are burned into CD-ROM, for example.) It can
13039 ** also be used if the application employs some external mechanism to
13040 ** prevent simultaneous access of the same database by two or more
13041 ** database connections. But there is a serious risk of database
13042 ** corruption if this locking mode is used in situations where multiple
13043 ** database connections are accessing the same database file at the same
13044 ** time and one or more of those connections are writing.
13045 */
13046
13047 static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
13048 UNUSED_PARAMETER(NotUsed);
13049 *pResOut = 0;
13050 return SQLITE_OK;
13051 }
13052 static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
13053 UNUSED_PARAMETER2(NotUsed, NotUsed2);
13054 return SQLITE_OK;
13055 }
13056 static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
13057 UNUSED_PARAMETER2(NotUsed, NotUsed2);
13058 return SQLITE_OK;
13059 }
13060
13061 /*
13062 ** Close the file.
13063 */
13064 static int nolockClose(sqlite3_file *id) {
13065 return closeUnixFile(id);
13066 }
13067
13068 /******************* End of the no-op lock implementation *********************
13069 ******************************************************************************/
13070
13071 /******************************************************************************
13072 ************************* Begin dot-file Locking ******************************
13073 **
13074 ** The dotfile locking implementation uses the existence of separate lock
13075 ** files (really a directory) to control access to the database. This works
13076 ** on just about every filesystem imaginable. But there are serious downsides:
13077 **
13078 ** (1) There is zero concurrency. A single reader blocks all other
13079 ** connections from reading or writing the database.
13080 **
13081 ** (2) An application crash or power loss can leave stale lock files
13082 ** sitting around that need to be cleared manually.
13083 **
13084 ** Nevertheless, a dotlock is an appropriate locking mode for use if no
13085 ** other locking strategy is available.
13086 **
13087 ** Dotfile locking works by creating a subdirectory in the same directory as
13088 ** the database and with the same name but with a ".lock" extension added.
13089 ** The existence of a lock directory implies an EXCLUSIVE lock. All other
13090 ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
13091 */
13092
13093 /*
13094 ** The file suffix added to the data base filename in order to create the
13095 ** lock directory.
13096 */
13097 #define DOTLOCK_SUFFIX ".lock"
13098
13099 /*
13100 ** This routine checks if there is a RESERVED lock held on the specified
13101 ** file by this or any other process. If such a lock is held, set *pResOut
13102 ** to a non-zero value otherwise *pResOut is set to zero. The return value
13103 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
13104 **
13105 ** In dotfile locking, either a lock exists or it does not. So in this
13106 ** variation of CheckReservedLock(), *pResOut is set to true if any lock
13107 ** is held on the file and false if the file is unlocked.
13108 */
13109 static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
13110 int rc = SQLITE_OK;
13111 int reserved = 0;
13112 unixFile *pFile = (unixFile*)id;
13113
13114 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
13115
13116 assert( pFile );
13117 reserved = osAccess((const char*)pFile->lockingContext, 0)==0;
13118 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
13119 *pResOut = reserved;
13120 return rc;
13121 }
13122
13123 /*
13124 ** Lock the file with the lock specified by parameter eFileLock - one
13125 ** of the following:
13126 **
13127 ** (1) SHARED_LOCK
13128 ** (2) RESERVED_LOCK
13129 ** (3) PENDING_LOCK
13130 ** (4) EXCLUSIVE_LOCK
13131 **
13132 ** Sometimes when requesting one lock state, additional lock states
13133 ** are inserted in between. The locking might fail on one of the later
13134 ** transitions leaving the lock state different from what it started but
13135 ** still short of its goal. The following chart shows the allowed
13136 ** transitions and the inserted intermediate states:
13137 **
13138 ** UNLOCKED -> SHARED
13139 ** SHARED -> RESERVED
13140 ** SHARED -> (PENDING) -> EXCLUSIVE
13141 ** RESERVED -> (PENDING) -> EXCLUSIVE
13142 ** PENDING -> EXCLUSIVE
13143 **
13144 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
13145 ** routine to lower a locking level.
13146 **
13147 ** With dotfile locking, we really only support state (4): EXCLUSIVE.
13148 ** But we track the other locking levels internally.
13149 */
13150 static int dotlockLock(sqlite3_file *id, int eFileLock) {
13151 unixFile *pFile = (unixFile*)id;
13152 char *zLockFile = (char *)pFile->lockingContext;
13153 int rc = SQLITE_OK;
13154
13155
13156 /* If we have any lock, then the lock file already exists. All we have
13157 ** to do is adjust our internal record of the lock level.
13158 */
13159 if( pFile->eFileLock > NO_LOCK ){
13160 pFile->eFileLock = eFileLock;
13161 /* Always update the timestamp on the old file */
13162 #ifdef HAVE_UTIME
13163 utime(zLockFile, NULL);
13164 #else
13165 utimes(zLockFile, NULL);
13166 #endif
13167 return SQLITE_OK;
13168 }
13169
13170 /* grab an exclusive lock */
13171 rc = osMkdir(zLockFile, 0777);
13172 if( rc<0 ){
13173 /* failed to open/create the lock directory */
13174 int tErrno = errno;
13175 if( EEXIST == tErrno ){
13176 rc = SQLITE_BUSY;
13177 } else {
13178 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
13179 if( rc!=SQLITE_BUSY ){
13180 storeLastErrno(pFile, tErrno);
13181 }
13182 }
13183 return rc;
13184 }
13185
13186 /* got it, set the type and return ok */
13187 pFile->eFileLock = eFileLock;
13188 return rc;
13189 }
13190
13191 /*
13192 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
13193 ** must be either NO_LOCK or SHARED_LOCK.
13194 **
13195 ** If the locking level of the file descriptor is already at or below
13196 ** the requested locking level, this routine is a no-op.
13197 **
13198 ** When the locking level reaches NO_LOCK, delete the lock file.
13199 */
13200 static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
13201 unixFile *pFile = (unixFile*)id;
13202 char *zLockFile = (char *)pFile->lockingContext;
13203 int rc;
13204
13205 assert( pFile );
13206 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
13207 pFile->eFileLock, osGetpid(0)));
13208 assert( eFileLock<=SHARED_LOCK );
13209
13210 /* no-op if possible */
13211 if( pFile->eFileLock==eFileLock ){
13212 return SQLITE_OK;
13213 }
13214
13215 /* To downgrade to shared, simply update our internal notion of the
13216 ** lock state. No need to mess with the file on disk.
13217 */
13218 if( eFileLock==SHARED_LOCK ){
13219 pFile->eFileLock = SHARED_LOCK;
13220 return SQLITE_OK;
13221 }
13222
13223 /* To fully unlock the database, delete the lock file */
13224 assert( eFileLock==NO_LOCK );
13225 rc = osRmdir(zLockFile);
13226 if( rc<0 ){
13227 int tErrno = errno;
13228 if( tErrno==ENOENT ){
13229 rc = SQLITE_OK;
13230 }else{
13231 rc = SQLITE_IOERR_UNLOCK;
13232 storeLastErrno(pFile, tErrno);
13233 }
13234 return rc;
13235 }
13236 pFile->eFileLock = NO_LOCK;
13237 return SQLITE_OK;
13238 }
13239
13240 /*
13241 ** Close a file. Make sure the lock has been released before closing.
13242 */
13243 static int dotlockClose(sqlite3_file *id) {
13244 unixFile *pFile = (unixFile*)id;
13245 assert( id!=0 );
13246 dotlockUnlock(id, NO_LOCK);
13247 sqlite3_free(pFile->lockingContext);
13248 return closeUnixFile(id);
13249 }
13250 /****************** End of the dot-file lock implementation *******************
13251 ******************************************************************************/
13252
13253 /******************************************************************************
13254 ************************** Begin flock Locking ********************************
13255 **
13256 ** Use the flock() system call to do file locking.
13257 **
13258 ** flock() locking is like dot-file locking in that the various
13259 ** fine-grain locking levels supported by SQLite are collapsed into
13260 ** a single exclusive lock. In other words, SHARED, RESERVED, and
13261 ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite
13262 ** still works when you do this, but concurrency is reduced since
13263 ** only a single process can be reading the database at a time.
13264 **
13265 ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off
13266 */
13267 #if SQLITE_ENABLE_LOCKING_STYLE
13268
13269 /*
13270 ** Retry flock() calls that fail with EINTR
13271 */
13272 #ifdef EINTR
13273 static int robust_flock(int fd, int op){
13274 int rc;
13275 do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
13276 return rc;
13277 }
13278 #else
13279 # define robust_flock(a,b) flock(a,b)
13280 #endif
13281
13282
13283 /*
13284 ** This routine checks if there is a RESERVED lock held on the specified
13285 ** file by this or any other process. If such a lock is held, set *pResOut
13286 ** to a non-zero value otherwise *pResOut is set to zero. The return value
13287 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
13288 */
13289 static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
13290 int rc = SQLITE_OK;
13291 int reserved = 0;
13292 unixFile *pFile = (unixFile*)id;
13293
13294 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
13295
13296 assert( pFile );
13297
13298 /* Check if a thread in this process holds such a lock */
13299 if( pFile->eFileLock>SHARED_LOCK ){
13300 reserved = 1;
13301 }
13302
13303 /* Otherwise see if some other process holds it. */
13304 if( !reserved ){
13305 /* attempt to get the lock */
13306 int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
13307 if( !lrc ){
13308 /* got the lock, unlock it */
13309 lrc = robust_flock(pFile->h, LOCK_UN);
13310 if ( lrc ) {
13311 int tErrno = errno;
13312 /* unlock failed with an error */
13313 lrc = SQLITE_IOERR_UNLOCK;
13314 storeLastErrno(pFile, tErrno);
13315 rc = lrc;
13316 }
13317 } else {
13318 int tErrno = errno;
13319 reserved = 1;
13320 /* someone else might have it reserved */
13321 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
13322 if( IS_LOCK_ERROR(lrc) ){
13323 storeLastErrno(pFile, tErrno);
13324 rc = lrc;
13325 }
13326 }
13327 }
13328 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
13329
13330 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
13331 if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
13332 rc = SQLITE_OK;
13333 reserved=1;
13334 }
13335 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
13336 *pResOut = reserved;
13337 return rc;
13338 }
13339
13340 /*
13341 ** Lock the file with the lock specified by parameter eFileLock - one
13342 ** of the following:
13343 **
13344 ** (1) SHARED_LOCK
13345 ** (2) RESERVED_LOCK
13346 ** (3) PENDING_LOCK
13347 ** (4) EXCLUSIVE_LOCK
13348 **
13349 ** Sometimes when requesting one lock state, additional lock states
13350 ** are inserted in between. The locking might fail on one of the later
13351 ** transitions leaving the lock state different from what it started but
13352 ** still short of its goal. The following chart shows the allowed
13353 ** transitions and the inserted intermediate states:
13354 **
13355 ** UNLOCKED -> SHARED
13356 ** SHARED -> RESERVED
13357 ** SHARED -> (PENDING) -> EXCLUSIVE
13358 ** RESERVED -> (PENDING) -> EXCLUSIVE
13359 ** PENDING -> EXCLUSIVE
13360 **
13361 ** flock() only really support EXCLUSIVE locks. We track intermediate
13362 ** lock states in the sqlite3_file structure, but all locks SHARED or
13363 ** above are really EXCLUSIVE locks and exclude all other processes from
13364 ** access the file.
13365 **
13366 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
13367 ** routine to lower a locking level.
13368 */
13369 static int flockLock(sqlite3_file *id, int eFileLock) {
13370 int rc = SQLITE_OK;
13371 unixFile *pFile = (unixFile*)id;
13372
13373 assert( pFile );
13374
13375 /* if we already have a lock, it is exclusive.
13376 ** Just adjust level and punt on outta here. */
13377 if (pFile->eFileLock > NO_LOCK) {
13378 pFile->eFileLock = eFileLock;
13379 return SQLITE_OK;
13380 }
13381
13382 /* grab an exclusive lock */
13383
13384 if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
13385 int tErrno = errno;
13386 /* didn't get, must be busy */
13387 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
13388 if( IS_LOCK_ERROR(rc) ){
13389 storeLastErrno(pFile, tErrno);
13390 }
13391 } else {
13392 /* got it, set the type and return ok */
13393 pFile->eFileLock = eFileLock;
13394 }
13395 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
13396 rc==SQLITE_OK ? "ok" : "failed"));
13397 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
13398 if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
13399 rc = SQLITE_BUSY;
13400 }
13401 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
13402 return rc;
13403 }
13404
13405
13406 /*
13407 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
13408 ** must be either NO_LOCK or SHARED_LOCK.
13409 **
13410 ** If the locking level of the file descriptor is already at or below
13411 ** the requested locking level, this routine is a no-op.
13412 */
13413 static int flockUnlock(sqlite3_file *id, int eFileLock) {
13414 unixFile *pFile = (unixFile*)id;
13415
13416 assert( pFile );
13417 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
13418 pFile->eFileLock, osGetpid(0)));
13419 assert( eFileLock<=SHARED_LOCK );
13420
13421 /* no-op if possible */
13422 if( pFile->eFileLock==eFileLock ){
13423 return SQLITE_OK;
13424 }
13425
13426 /* shared can just be set because we always have an exclusive */
13427 if (eFileLock==SHARED_LOCK) {
13428 pFile->eFileLock = eFileLock;
13429 return SQLITE_OK;
13430 }
13431
13432 /* no, really, unlock. */
13433 if( robust_flock(pFile->h, LOCK_UN) ){
13434 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
13435 return SQLITE_OK;
13436 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
13437 return SQLITE_IOERR_UNLOCK;
13438 }else{
13439 pFile->eFileLock = NO_LOCK;
13440 return SQLITE_OK;
13441 }
13442 }
13443
13444 /*
13445 ** Close a file.
13446 */
13447 static int flockClose(sqlite3_file *id) {
13448 assert( id!=0 );
13449 flockUnlock(id, NO_LOCK);
13450 return closeUnixFile(id);
13451 }
13452
13453 #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
13454
13455 /******************* End of the flock lock implementation *********************
13456 ******************************************************************************/
13457
13458 /******************************************************************************
13459 ************************ Begin Named Semaphore Locking ************************
13460 **
13461 ** Named semaphore locking is only supported on VxWorks.
13462 **
13463 ** Semaphore locking is like dot-lock and flock in that it really only
13464 ** supports EXCLUSIVE locking. Only a single process can read or write
13465 ** the database file at a time. This reduces potential concurrency, but
13466 ** makes the lock implementation much easier.
13467 */
13468 #if OS_VXWORKS
13469
13470 /*
13471 ** This routine checks if there is a RESERVED lock held on the specified
13472 ** file by this or any other process. If such a lock is held, set *pResOut
13473 ** to a non-zero value otherwise *pResOut is set to zero. The return value
13474 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
13475 */
13476 static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) {
13477 int rc = SQLITE_OK;
13478 int reserved = 0;
13479 unixFile *pFile = (unixFile*)id;
13480
13481 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
13482
13483 assert( pFile );
13484
13485 /* Check if a thread in this process holds such a lock */
13486 if( pFile->eFileLock>SHARED_LOCK ){
13487 reserved = 1;
13488 }
13489
13490 /* Otherwise see if some other process holds it. */
13491 if( !reserved ){
13492 sem_t *pSem = pFile->pInode->pSem;
13493
13494 if( sem_trywait(pSem)==-1 ){
13495 int tErrno = errno;
13496 if( EAGAIN != tErrno ){
13497 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
13498 storeLastErrno(pFile, tErrno);
13499 } else {
13500 /* someone else has the lock when we are in NO_LOCK */
13501 reserved = (pFile->eFileLock < SHARED_LOCK);
13502 }
13503 }else{
13504 /* we could have it if we want it */
13505 sem_post(pSem);
13506 }
13507 }
13508 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
13509
13510 *pResOut = reserved;
13511 return rc;
13512 }
13513
13514 /*
13515 ** Lock the file with the lock specified by parameter eFileLock - one
13516 ** of the following:
13517 **
13518 ** (1) SHARED_LOCK
13519 ** (2) RESERVED_LOCK
13520 ** (3) PENDING_LOCK
13521 ** (4) EXCLUSIVE_LOCK
13522 **
13523 ** Sometimes when requesting one lock state, additional lock states
13524 ** are inserted in between. The locking might fail on one of the later
13525 ** transitions leaving the lock state different from what it started but
13526 ** still short of its goal. The following chart shows the allowed
13527 ** transitions and the inserted intermediate states:
13528 **
13529 ** UNLOCKED -> SHARED
13530 ** SHARED -> RESERVED
13531 ** SHARED -> (PENDING) -> EXCLUSIVE
13532 ** RESERVED -> (PENDING) -> EXCLUSIVE
13533 ** PENDING -> EXCLUSIVE
13534 **
13535 ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate
13536 ** lock states in the sqlite3_file structure, but all locks SHARED or
13537 ** above are really EXCLUSIVE locks and exclude all other processes from
13538 ** access the file.
13539 **
13540 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
13541 ** routine to lower a locking level.
13542 */
13543 static int semXLock(sqlite3_file *id, int eFileLock) {
13544 unixFile *pFile = (unixFile*)id;
13545 sem_t *pSem = pFile->pInode->pSem;
13546 int rc = SQLITE_OK;
13547
13548 /* if we already have a lock, it is exclusive.
13549 ** Just adjust level and punt on outta here. */
13550 if (pFile->eFileLock > NO_LOCK) {
13551 pFile->eFileLock = eFileLock;
13552 rc = SQLITE_OK;
13553 goto sem_end_lock;
13554 }
13555
13556 /* lock semaphore now but bail out when already locked. */
13557 if( sem_trywait(pSem)==-1 ){
13558 rc = SQLITE_BUSY;
13559 goto sem_end_lock;
13560 }
13561
13562 /* got it, set the type and return ok */
13563 pFile->eFileLock = eFileLock;
13564
13565 sem_end_lock:
13566 return rc;
13567 }
13568
13569 /*
13570 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
13571 ** must be either NO_LOCK or SHARED_LOCK.
13572 **
13573 ** If the locking level of the file descriptor is already at or below
13574 ** the requested locking level, this routine is a no-op.
13575 */
13576 static int semXUnlock(sqlite3_file *id, int eFileLock) {
13577 unixFile *pFile = (unixFile*)id;
13578 sem_t *pSem = pFile->pInode->pSem;
13579
13580 assert( pFile );
13581 assert( pSem );
13582 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
13583 pFile->eFileLock, osGetpid(0)));
13584 assert( eFileLock<=SHARED_LOCK );
13585
13586 /* no-op if possible */
13587 if( pFile->eFileLock==eFileLock ){
13588 return SQLITE_OK;
13589 }
13590
13591 /* shared can just be set because we always have an exclusive */
13592 if (eFileLock==SHARED_LOCK) {
13593 pFile->eFileLock = eFileLock;
13594 return SQLITE_OK;
13595 }
13596
13597 /* no, really unlock. */
13598 if ( sem_post(pSem)==-1 ) {
13599 int rc, tErrno = errno;
13600 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
13601 if( IS_LOCK_ERROR(rc) ){
13602 storeLastErrno(pFile, tErrno);
13603 }
13604 return rc;
13605 }
13606 pFile->eFileLock = NO_LOCK;
13607 return SQLITE_OK;
13608 }
13609
13610 /*
13611 ** Close a file.
13612 */
13613 static int semXClose(sqlite3_file *id) {
13614 if( id ){
13615 unixFile *pFile = (unixFile*)id;
13616 semXUnlock(id, NO_LOCK);
13617 assert( pFile );
13618 unixEnterMutex();
13619 releaseInodeInfo(pFile);
13620 unixLeaveMutex();
13621 closeUnixFile(id);
13622 }
13623 return SQLITE_OK;
13624 }
13625
13626 #endif /* OS_VXWORKS */
13627 /*
13628 ** Named semaphore locking is only available on VxWorks.
13629 **
13630 *************** End of the named semaphore lock implementation ****************
13631 ******************************************************************************/
13632
13633
13634 /******************************************************************************
13635 *************************** Begin AFP Locking *********************************
13636 **
13637 ** AFP is the Apple Filing Protocol. AFP is a network filesystem found
13638 ** on Apple Macintosh computers - both OS9 and OSX.
13639 **
13640 ** Third-party implementations of AFP are available. But this code here
13641 ** only works on OSX.
13642 */
13643
13644 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
13645 /*
13646 ** The afpLockingContext structure contains all afp lock specific state
13647 */
13648 typedef struct afpLockingContext afpLockingContext;
13649 struct afpLockingContext {
13650 int reserved;
13651 const char *dbPath; /* Name of the open file */
13652 };
13653
13654 struct ByteRangeLockPB2
13655 {
13656 unsigned long long offset; /* offset to first byte to lock */
13657 unsigned long long length; /* nbr of bytes to lock */
13658 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
13659 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */
13660 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */
13661 int fd; /* file desc to assoc this lock with */
13662 };
13663
13664 #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2)
13665
13666 /*
13667 ** This is a utility for setting or clearing a bit-range lock on an
13668 ** AFP filesystem.
13669 **
13670 ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
13671 */
13672 static int afpSetLock(
13673 const char *path, /* Name of the file to be locked or unlocked */
13674 unixFile *pFile, /* Open file descriptor on path */
13675 unsigned long long offset, /* First byte to be locked */
13676 unsigned long long length, /* Number of bytes to lock */
13677 int setLockFlag /* True to set lock. False to clear lock */
13678 ){
13679 struct ByteRangeLockPB2 pb;
13680 int err;
13681
13682 pb.unLockFlag = setLockFlag ? 0 : 1;
13683 pb.startEndFlag = 0;
13684 pb.offset = offset;
13685 pb.length = length;
13686 pb.fd = pFile->h;
13687
13688 OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
13689 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
13690 offset, length));
13691 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
13692 if ( err==-1 ) {
13693 int rc;
13694 int tErrno = errno;
13695 OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
13696 path, tErrno, strerror(tErrno)));
13697 #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
13698 rc = SQLITE_BUSY;
13699 #else
13700 rc = sqliteErrorFromPosixError(tErrno,
13701 setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
13702 #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
13703 if( IS_LOCK_ERROR(rc) ){
13704 storeLastErrno(pFile, tErrno);
13705 }
13706 return rc;
13707 } else {
13708 return SQLITE_OK;
13709 }
13710 }
13711
13712 /*
13713 ** This routine checks if there is a RESERVED lock held on the specified
13714 ** file by this or any other process. If such a lock is held, set *pResOut
13715 ** to a non-zero value otherwise *pResOut is set to zero. The return value
13716 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
13717 */
13718 static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
13719 int rc = SQLITE_OK;
13720 int reserved = 0;
13721 unixFile *pFile = (unixFile*)id;
13722 afpLockingContext *context;
13723
13724 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
13725
13726 assert( pFile );
13727 context = (afpLockingContext *) pFile->lockingContext;
13728 if( context->reserved ){
13729 *pResOut = 1;
13730 return SQLITE_OK;
13731 }
13732 unixEnterMutex(); /* Because pFile->pInode is shared across threads */
13733
13734 /* Check if a thread in this process holds such a lock */
13735 if( pFile->pInode->eFileLock>SHARED_LOCK ){
13736 reserved = 1;
13737 }
13738
13739 /* Otherwise see if some other process holds it.
13740 */
13741 if( !reserved ){
13742 /* lock the RESERVED byte */
13743 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
13744 if( SQLITE_OK==lrc ){
13745 /* if we succeeded in taking the reserved lock, unlock it to restore
13746 ** the original state */
13747 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
13748 } else {
13749 /* if we failed to get the lock then someone else must have it */
13750 reserved = 1;
13751 }
13752 if( IS_LOCK_ERROR(lrc) ){
13753 rc=lrc;
13754 }
13755 }
13756
13757 unixLeaveMutex();
13758 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
13759
13760 *pResOut = reserved;
13761 return rc;
13762 }
13763
13764 /*
13765 ** Lock the file with the lock specified by parameter eFileLock - one
13766 ** of the following:
13767 **
13768 ** (1) SHARED_LOCK
13769 ** (2) RESERVED_LOCK
13770 ** (3) PENDING_LOCK
13771 ** (4) EXCLUSIVE_LOCK
13772 **
13773 ** Sometimes when requesting one lock state, additional lock states
13774 ** are inserted in between. The locking might fail on one of the later
13775 ** transitions leaving the lock state different from what it started but
13776 ** still short of its goal. The following chart shows the allowed
13777 ** transitions and the inserted intermediate states:
13778 **
13779 ** UNLOCKED -> SHARED
13780 ** SHARED -> RESERVED
13781 ** SHARED -> (PENDING) -> EXCLUSIVE
13782 ** RESERVED -> (PENDING) -> EXCLUSIVE
13783 ** PENDING -> EXCLUSIVE
13784 **
13785 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
13786 ** routine to lower a locking level.
13787 */
13788 static int afpLock(sqlite3_file *id, int eFileLock){
13789 int rc = SQLITE_OK;
13790 unixFile *pFile = (unixFile*)id;
13791 unixInodeInfo *pInode = pFile->pInode;
13792 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
13793
13794 assert( pFile );
13795 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
13796 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
13797 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
13798
13799 /* If there is already a lock of this type or more restrictive on the
13800 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
13801 ** unixEnterMutex() hasn't been called yet.
13802 */
13803 if( pFile->eFileLock>=eFileLock ){
13804 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
13805 azFileLock(eFileLock)));
13806 return SQLITE_OK;
13807 }
13808
13809 /* Make sure the locking sequence is correct
13810 ** (1) We never move from unlocked to anything higher than shared lock.
13811 ** (2) SQLite never explicitly requests a pendig lock.
13812 ** (3) A shared lock is always held when a reserve lock is requested.
13813 */
13814 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
13815 assert( eFileLock!=PENDING_LOCK );
13816 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
13817
13818 /* This mutex is needed because pFile->pInode is shared across threads
13819 */
13820 unixEnterMutex();
13821 pInode = pFile->pInode;
13822
13823 /* If some thread using this PID has a lock via a different unixFile*
13824 ** handle that precludes the requested lock, return BUSY.
13825 */
13826 if( (pFile->eFileLock!=pInode->eFileLock &&
13827 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
13828 ){
13829 rc = SQLITE_BUSY;
13830 goto afp_end_lock;
13831 }
13832
13833 /* If a SHARED lock is requested, and some thread using this PID already
13834 ** has a SHARED or RESERVED lock, then increment reference counts and
13835 ** return SQLITE_OK.
13836 */
13837 if( eFileLock==SHARED_LOCK &&
13838 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
13839 assert( eFileLock==SHARED_LOCK );
13840 assert( pFile->eFileLock==0 );
13841 assert( pInode->nShared>0 );
13842 pFile->eFileLock = SHARED_LOCK;
13843 pInode->nShared++;
13844 pInode->nLock++;
13845 goto afp_end_lock;
13846 }
13847
13848 /* A PENDING lock is needed before acquiring a SHARED lock and before
13849 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will
13850 ** be released.
13851 */
13852 if( eFileLock==SHARED_LOCK
13853 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
13854 ){
13855 int failed;
13856 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
13857 if (failed) {
13858 rc = failed;
13859 goto afp_end_lock;
13860 }
13861 }
13862
13863 /* If control gets to this point, then actually go ahead and make
13864 ** operating system calls for the specified lock.
13865 */
13866 if( eFileLock==SHARED_LOCK ){
13867 int lrc1, lrc2, lrc1Errno = 0;
13868 long lk, mask;
13869
13870 assert( pInode->nShared==0 );
13871 assert( pInode->eFileLock==0 );
13872
13873 mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
13874 /* Now get the read-lock SHARED_LOCK */
13875 /* note that the quality of the randomness doesn't matter that much */
13876 lk = random();
13877 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
13878 lrc1 = afpSetLock(context->dbPath, pFile,
13879 SHARED_FIRST+pInode->sharedByte, 1, 1);
13880 if( IS_LOCK_ERROR(lrc1) ){
13881 lrc1Errno = pFile->lastErrno;
13882 }
13883 /* Drop the temporary PENDING lock */
13884 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
13885
13886 if( IS_LOCK_ERROR(lrc1) ) {
13887 storeLastErrno(pFile, lrc1Errno);
13888 rc = lrc1;
13889 goto afp_end_lock;
13890 } else if( IS_LOCK_ERROR(lrc2) ){
13891 rc = lrc2;
13892 goto afp_end_lock;
13893 } else if( lrc1 != SQLITE_OK ) {
13894 rc = lrc1;
13895 } else {
13896 pFile->eFileLock = SHARED_LOCK;
13897 pInode->nLock++;
13898 pInode->nShared = 1;
13899 }
13900 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
13901 /* We are trying for an exclusive lock but another thread in this
13902 ** same process is still holding a shared lock. */
13903 rc = SQLITE_BUSY;
13904 }else{
13905 /* The request was for a RESERVED or EXCLUSIVE lock. It is
13906 ** assumed that there is a SHARED or greater lock on the file
13907 ** already.
13908 */
13909 int failed = 0;
13910 assert( 0!=pFile->eFileLock );
13911 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
13912 /* Acquire a RESERVED lock */
13913 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
13914 if( !failed ){
13915 context->reserved = 1;
13916 }
13917 }
13918 if (!failed && eFileLock == EXCLUSIVE_LOCK) {
13919 /* Acquire an EXCLUSIVE lock */
13920
13921 /* Remove the shared lock before trying the range. we'll need to
13922 ** reestablish the shared lock if we can't get the afpUnlock
13923 */
13924 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
13925 pInode->sharedByte, 1, 0)) ){
13926 int failed2 = SQLITE_OK;
13927 /* now attemmpt to get the exclusive lock range */
13928 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
13929 SHARED_SIZE, 1);
13930 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
13931 SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
13932 /* Can't reestablish the shared lock. Sqlite can't deal, this is
13933 ** a critical I/O error
13934 */
13935 rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 :
13936 SQLITE_IOERR_LOCK;
13937 goto afp_end_lock;
13938 }
13939 }else{
13940 rc = failed;
13941 }
13942 }
13943 if( failed ){
13944 rc = failed;
13945 }
13946 }
13947
13948 if( rc==SQLITE_OK ){
13949 pFile->eFileLock = eFileLock;
13950 pInode->eFileLock = eFileLock;
13951 }else if( eFileLock==EXCLUSIVE_LOCK ){
13952 pFile->eFileLock = PENDING_LOCK;
13953 pInode->eFileLock = PENDING_LOCK;
13954 }
13955
13956 afp_end_lock:
13957 unixLeaveMutex();
13958 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
13959 rc==SQLITE_OK ? "ok" : "failed"));
13960 return rc;
13961 }
13962
13963 /*
13964 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
13965 ** must be either NO_LOCK or SHARED_LOCK.
13966 **
13967 ** If the locking level of the file descriptor is already at or below
13968 ** the requested locking level, this routine is a no-op.
13969 */
13970 static int afpUnlock(sqlite3_file *id, int eFileLock) {
13971 int rc = SQLITE_OK;
13972 unixFile *pFile = (unixFile*)id;
13973 unixInodeInfo *pInode;
13974 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
13975 int skipShared = 0;
13976 #ifdef SQLITE_TEST
13977 int h = pFile->h;
13978 #endif
13979
13980 assert( pFile );
13981 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
13982 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
13983 osGetpid(0)));
13984
13985 assert( eFileLock<=SHARED_LOCK );
13986 if( pFile->eFileLock<=eFileLock ){
13987 return SQLITE_OK;
13988 }
13989 unixEnterMutex();
13990 pInode = pFile->pInode;
13991 assert( pInode->nShared!=0 );
13992 if( pFile->eFileLock>SHARED_LOCK ){
13993 assert( pInode->eFileLock==pFile->eFileLock );
13994 SimulateIOErrorBenign(1);
13995 SimulateIOError( h=(-1) )
13996 SimulateIOErrorBenign(0);
13997
13998 #ifdef SQLITE_DEBUG
13999 /* When reducing a lock such that other processes can start
14000 ** reading the database file again, make sure that the
14001 ** transaction counter was updated if any part of the database
14002 ** file changed. If the transaction counter is not updated,
14003 ** other connections to the same file might not realize that
14004 ** the file has changed and hence might not know to flush their
14005 ** cache. The use of a stale cache can lead to database corruption.
14006 */
14007 assert( pFile->inNormalWrite==0
14008 || pFile->dbUpdate==0
14009 || pFile->transCntrChng==1 );
14010 pFile->inNormalWrite = 0;
14011 #endif
14012
14013 if( pFile->eFileLock==EXCLUSIVE_LOCK ){
14014 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
14015 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
14016 /* only re-establish the shared lock if necessary */
14017 int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
14018 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
14019 } else {
14020 skipShared = 1;
14021 }
14022 }
14023 if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
14024 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
14025 }
14026 if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
14027 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
14028 if( !rc ){
14029 context->reserved = 0;
14030 }
14031 }
14032 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
14033 pInode->eFileLock = SHARED_LOCK;
14034 }
14035 }
14036 if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
14037
14038 /* Decrement the shared lock counter. Release the lock using an
14039 ** OS call only when all threads in this same process have released
14040 ** the lock.
14041 */
14042 unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
14043 pInode->nShared--;
14044 if( pInode->nShared==0 ){
14045 SimulateIOErrorBenign(1);
14046 SimulateIOError( h=(-1) )
14047 SimulateIOErrorBenign(0);
14048 if( !skipShared ){
14049 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
14050 }
14051 if( !rc ){
14052 pInode->eFileLock = NO_LOCK;
14053 pFile->eFileLock = NO_LOCK;
14054 }
14055 }
14056 if( rc==SQLITE_OK ){
14057 pInode->nLock--;
14058 assert( pInode->nLock>=0 );
14059 if( pInode->nLock==0 ){
14060 closePendingFds(pFile);
14061 }
14062 }
14063 }
14064
14065 unixLeaveMutex();
14066 if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
14067 return rc;
14068 }
14069
14070 /*
14071 ** Close a file & cleanup AFP specific locking context
14072 */
14073 static int afpClose(sqlite3_file *id) {
14074 int rc = SQLITE_OK;
14075 unixFile *pFile = (unixFile*)id;
14076 assert( id!=0 );
14077 afpUnlock(id, NO_LOCK);
14078 unixEnterMutex();
14079 if( pFile->pInode && pFile->pInode->nLock ){
14080 /* If there are outstanding locks, do not actually close the file just
14081 ** yet because that would clear those locks. Instead, add the file
14082 ** descriptor to pInode->aPending. It will be automatically closed when
14083 ** the last lock is cleared.
14084 */
14085 setPendingFd(pFile);
14086 }
14087 releaseInodeInfo(pFile);
14088 sqlite3_free(pFile->lockingContext);
14089 rc = closeUnixFile(id);
14090 unixLeaveMutex();
14091 return rc;
14092 }
14093
14094 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
14095 /*
14096 ** The code above is the AFP lock implementation. The code is specific
14097 ** to MacOSX and does not work on other unix platforms. No alternative
14098 ** is available. If you don't compile for a mac, then the "unix-afp"
14099 ** VFS is not available.
14100 **
14101 ********************* End of the AFP lock implementation **********************
14102 ******************************************************************************/
14103
14104 /******************************************************************************
14105 *************************** Begin NFS Locking ********************************/
14106
14107 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
14108 /*
14109 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
14110 ** must be either NO_LOCK or SHARED_LOCK.
14111 **
14112 ** If the locking level of the file descriptor is already at or below
14113 ** the requested locking level, this routine is a no-op.
14114 */
14115 static int nfsUnlock(sqlite3_file *id, int eFileLock){
14116 return posixUnlock(id, eFileLock, 1);
14117 }
14118
14119 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
14120 /*
14121 ** The code above is the NFS lock implementation. The code is specific
14122 ** to MacOSX and does not work on other unix platforms. No alternative
14123 ** is available.
14124 **
14125 ********************* End of the NFS lock implementation **********************
14126 ******************************************************************************/
14127
14128 /******************************************************************************
14129 **************** Non-locking sqlite3_file methods *****************************
14130 **
14131 ** The next division contains implementations for all methods of the
14132 ** sqlite3_file object other than the locking methods. The locking
14133 ** methods were defined in divisions above (one locking method per
14134 ** division). Those methods that are common to all locking modes
14135 ** are gather together into this division.
14136 */
14137
14138 /*
14139 ** Seek to the offset passed as the second argument, then read cnt
14140 ** bytes into pBuf. Return the number of bytes actually read.
14141 **
14142 ** NB: If you define USE_PREAD or USE_PREAD64, then it might also
14143 ** be necessary to define _XOPEN_SOURCE to be 500. This varies from
14144 ** one system to another. Since SQLite does not define USE_PREAD
14145 ** in any form by default, we will not attempt to define _XOPEN_SOURCE.
14146 ** See tickets #2741 and #2681.
14147 **
14148 ** To avoid stomping the errno value on a failed read the lastErrno value
14149 ** is set before returning.
14150 */
14151 static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
14152 int got;
14153 int prior = 0;
14154 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
14155 i64 newOffset;
14156 #endif
14157 TIMER_START;
14158 assert( cnt==(cnt&0x1ffff) );
14159 assert( id->h>2 );
14160 do{
14161 #if defined(USE_PREAD)
14162 got = osPread(id->h, pBuf, cnt, offset);
14163 SimulateIOError( got = -1 );
14164 #elif defined(USE_PREAD64)
14165 got = osPread64(id->h, pBuf, cnt, offset);
14166 SimulateIOError( got = -1 );
14167 #else
14168 newOffset = lseek(id->h, offset, SEEK_SET);
14169 SimulateIOError( newOffset = -1 );
14170 if( newOffset<0 ){
14171 storeLastErrno((unixFile*)id, errno);
14172 return -1;
14173 }
14174 got = osRead(id->h, pBuf, cnt);
14175 #endif
14176 if( got==cnt ) break;
14177 if( got<0 ){
14178 if( errno==EINTR ){ got = 1; continue; }
14179 prior = 0;
14180 storeLastErrno((unixFile*)id, errno);
14181 break;
14182 }else if( got>0 ){
14183 cnt -= got;
14184 offset += got;
14185 prior += got;
14186 pBuf = (void*)(got + (char*)pBuf);
14187 }
14188 }while( got>0 );
14189 TIMER_END;
14190 OSTRACE(("READ %-3d %5d %7lld %llu\n",
14191 id->h, got+prior, offset-prior, TIMER_ELAPSED));
14192 return got+prior;
14193 }
14194
14195 /*
14196 ** Read data from a file into a buffer. Return SQLITE_OK if all
14197 ** bytes were read successfully and SQLITE_IOERR if anything goes
14198 ** wrong.
14199 */
14200 static int unixRead(
14201 sqlite3_file *id,
14202 void *pBuf,
14203 int amt,
14204 sqlite3_int64 offset
14205 ){
14206 unixFile *pFile = (unixFile *)id;
14207 int got;
14208 assert( id );
14209 assert( offset>=0 );
14210 assert( amt>0 );
14211
14212 /* If this is a database file (not a journal, master-journal or temp
14213 ** file), the bytes in the locking range should never be read or written. */
14214 #if 0
14215 assert( pFile->pUnused==0
14216 || offset>=PENDING_BYTE+512
14217 || offset+amt<=PENDING_BYTE
14218 );
14219 #endif
14220
14221 #if SQLITE_MAX_MMAP_SIZE>0
14222 /* Deal with as much of this read request as possible by transfering
14223 ** data from the memory mapping using memcpy(). */
14224 if( offset<pFile->mmapSize ){
14225 if( offset+amt <= pFile->mmapSize ){
14226 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
14227 return SQLITE_OK;
14228 }else{
14229 int nCopy = pFile->mmapSize - offset;
14230 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
14231 pBuf = &((u8 *)pBuf)[nCopy];
14232 amt -= nCopy;
14233 offset += nCopy;
14234 }
14235 }
14236 #endif
14237
14238 got = seekAndRead(pFile, offset, pBuf, amt);
14239 if( got==amt ){
14240 return SQLITE_OK;
14241 }else if( got<0 ){
14242 /* lastErrno set by seekAndRead */
14243 return SQLITE_IOERR_READ;
14244 }else{
14245 storeLastErrno(pFile, 0); /* not a system error */
14246 /* Unread parts of the buffer must be zero-filled */
14247 memset(&((char*)pBuf)[got], 0, amt-got);
14248 return SQLITE_IOERR_SHORT_READ;
14249 }
14250 }
14251
14252 /*
14253 ** Attempt to seek the file-descriptor passed as the first argument to
14254 ** absolute offset iOff, then attempt to write nBuf bytes of data from
14255 ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
14256 ** return the actual number of bytes written (which may be less than
14257 ** nBuf).
14258 */
14259 static int seekAndWriteFd(
14260 int fd, /* File descriptor to write to */
14261 i64 iOff, /* File offset to begin writing at */
14262 const void *pBuf, /* Copy data from this buffer to the file */
14263 int nBuf, /* Size of buffer pBuf in bytes */
14264 int *piErrno /* OUT: Error number if error occurs */
14265 ){
14266 int rc = 0; /* Value returned by system call */
14267
14268 assert( nBuf==(nBuf&0x1ffff) );
14269 assert( fd>2 );
14270 assert( piErrno!=0 );
14271 nBuf &= 0x1ffff;
14272 TIMER_START;
14273
14274 #if defined(USE_PREAD)
14275 do{ rc = (int)osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
14276 #elif defined(USE_PREAD64)
14277 do{ rc = (int)osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR);
14278 #else
14279 do{
14280 i64 iSeek = lseek(fd, iOff, SEEK_SET);
14281 SimulateIOError( iSeek = -1 );
14282 if( iSeek<0 ){
14283 rc = -1;
14284 break;
14285 }
14286 rc = osWrite(fd, pBuf, nBuf);
14287 }while( rc<0 && errno==EINTR );
14288 #endif
14289
14290 TIMER_END;
14291 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
14292
14293 if( rc<0 ) *piErrno = errno;
14294 return rc;
14295 }
14296
14297
14298 /*
14299 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
14300 ** Return the number of bytes actually read. Update the offset.
14301 **
14302 ** To avoid stomping the errno value on a failed write the lastErrno value
14303 ** is set before returning.
14304 */
14305 static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
14306 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
14307 }
14308
14309
14310 /*
14311 ** Write data from a buffer into a file. Return SQLITE_OK on success
14312 ** or some other error code on failure.
14313 */
14314 static int unixWrite(
14315 sqlite3_file *id,
14316 const void *pBuf,
14317 int amt,
14318 sqlite3_int64 offset
14319 ){
14320 unixFile *pFile = (unixFile*)id;
14321 int wrote = 0;
14322 assert( id );
14323 assert( amt>0 );
14324
14325 /* If this is a database file (not a journal, master-journal or temp
14326 ** file), the bytes in the locking range should never be read or written. */
14327 #if 0
14328 assert( pFile->pUnused==0
14329 || offset>=PENDING_BYTE+512
14330 || offset+amt<=PENDING_BYTE
14331 );
14332 #endif
14333
14334 #ifdef SQLITE_DEBUG
14335 /* If we are doing a normal write to a database file (as opposed to
14336 ** doing a hot-journal rollback or a write to some file other than a
14337 ** normal database file) then record the fact that the database
14338 ** has changed. If the transaction counter is modified, record that
14339 ** fact too.
14340 */
14341 if( pFile->inNormalWrite ){
14342 pFile->dbUpdate = 1; /* The database has been modified */
14343 if( offset<=24 && offset+amt>=27 ){
14344 int rc;
14345 char oldCntr[4];
14346 SimulateIOErrorBenign(1);
14347 rc = seekAndRead(pFile, 24, oldCntr, 4);
14348 SimulateIOErrorBenign(0);
14349 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
14350 pFile->transCntrChng = 1; /* The transaction counter has changed */
14351 }
14352 }
14353 }
14354 #endif
14355
14356 #if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
14357 /* Deal with as much of this write request as possible by transfering
14358 ** data from the memory mapping using memcpy(). */
14359 if( offset<pFile->mmapSize ){
14360 if( offset+amt <= pFile->mmapSize ){
14361 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
14362 return SQLITE_OK;
14363 }else{
14364 int nCopy = pFile->mmapSize - offset;
14365 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
14366 pBuf = &((u8 *)pBuf)[nCopy];
14367 amt -= nCopy;
14368 offset += nCopy;
14369 }
14370 }
14371 #endif
14372
14373 while( (wrote = seekAndWrite(pFile, offset, pBuf, amt))<amt && wrote>0 ){
14374 amt -= wrote;
14375 offset += wrote;
14376 pBuf = &((char*)pBuf)[wrote];
14377 }
14378 SimulateIOError(( wrote=(-1), amt=1 ));
14379 SimulateDiskfullError(( wrote=0, amt=1 ));
14380
14381 if( amt>wrote ){
14382 if( wrote<0 && pFile->lastErrno!=ENOSPC ){
14383 /* lastErrno set by seekAndWrite */
14384 return SQLITE_IOERR_WRITE;
14385 }else{
14386 storeLastErrno(pFile, 0); /* not a system error */
14387 return SQLITE_FULL;
14388 }
14389 }
14390
14391 return SQLITE_OK;
14392 }
14393
14394 #ifdef SQLITE_TEST
14395 /*
14396 ** Count the number of fullsyncs and normal syncs. This is used to test
14397 ** that syncs and fullsyncs are occurring at the right times.
14398 */
14399 SQLITE_API int sqlite3_sync_count = 0;
14400 SQLITE_API int sqlite3_fullsync_count = 0;
14401 #endif
14402
14403 /*
14404 ** We do not trust systems to provide a working fdatasync(). Some do.
14405 ** Others do no. To be safe, we will stick with the (slightly slower)
14406 ** fsync(). If you know that your system does support fdatasync() correctly,
14407 ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
14408 */
14409 #if !defined(fdatasync) && !HAVE_FDATASYNC
14410 # define fdatasync fsync
14411 #endif
14412
14413 /*
14414 ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
14415 ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently
14416 ** only available on Mac OS X. But that could change.
14417 */
14418 #ifdef F_FULLFSYNC
14419 # define HAVE_FULLFSYNC 1
14420 #else
14421 # define HAVE_FULLFSYNC 0
14422 #endif
14423
14424
14425 /*
14426 ** The fsync() system call does not work as advertised on many
14427 ** unix systems. The following procedure is an attempt to make
14428 ** it work better.
14429 **
14430 ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful
14431 ** for testing when we want to run through the test suite quickly.
14432 ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
14433 ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
14434 ** or power failure will likely corrupt the database file.
14435 **
14436 ** SQLite sets the dataOnly flag if the size of the file is unchanged.
14437 ** The idea behind dataOnly is that it should only write the file content
14438 ** to disk, not the inode. We only set dataOnly if the file size is
14439 ** unchanged since the file size is part of the inode. However,
14440 ** Ted Ts'o tells us that fdatasync() will also write the inode if the
14441 ** file size has changed. The only real difference between fdatasync()
14442 ** and fsync(), Ted tells us, is that fdatasync() will not flush the
14443 ** inode if the mtime or owner or other inode attributes have changed.
14444 ** We only care about the file size, not the other file attributes, so
14445 ** as far as SQLite is concerned, an fdatasync() is always adequate.
14446 ** So, we always use fdatasync() if it is available, regardless of
14447 ** the value of the dataOnly flag.
14448 */
14449 static int full_fsync(int fd, int fullSync, int dataOnly){
14450 int rc;
14451
14452 /* The following "ifdef/elif/else/" block has the same structure as
14453 ** the one below. It is replicated here solely to avoid cluttering
14454 ** up the real code with the UNUSED_PARAMETER() macros.
14455 */
14456 #ifdef SQLITE_NO_SYNC
14457 UNUSED_PARAMETER(fd);
14458 UNUSED_PARAMETER(fullSync);
14459 UNUSED_PARAMETER(dataOnly);
14460 #elif HAVE_FULLFSYNC
14461 UNUSED_PARAMETER(dataOnly);
14462 #else
14463 UNUSED_PARAMETER(fullSync);
14464 UNUSED_PARAMETER(dataOnly);
14465 #endif
14466
14467 /* Record the number of times that we do a normal fsync() and
14468 ** FULLSYNC. This is used during testing to verify that this procedure
14469 ** gets called with the correct arguments.
14470 */
14471 #ifdef SQLITE_TEST
14472 if( fullSync ) sqlite3_fullsync_count++;
14473 sqlite3_sync_count++;
14474 #endif
14475
14476 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
14477 ** no-op. But go ahead and call fstat() to validate the file
14478 ** descriptor as we need a method to provoke a failure during
14479 ** coverate testing.
14480 */
14481 #ifdef SQLITE_NO_SYNC
14482 {
14483 struct stat buf;
14484 rc = osFstat(fd, &buf);
14485 }
14486 #elif HAVE_FULLFSYNC
14487 if( fullSync ){
14488 rc = osFcntl(fd, F_FULLFSYNC, 0);
14489 }else{
14490 rc = 1;
14491 }
14492 /* If the FULLFSYNC failed, fall back to attempting an fsync().
14493 ** It shouldn't be possible for fullfsync to fail on the local
14494 ** file system (on OSX), so failure indicates that FULLFSYNC
14495 ** isn't supported for this file system. So, attempt an fsync
14496 ** and (for now) ignore the overhead of a superfluous fcntl call.
14497 ** It'd be better to detect fullfsync support once and avoid
14498 ** the fcntl call every time sync is called.
14499 */
14500 if( rc ) rc = fsync(fd);
14501
14502 #elif defined(__APPLE__)
14503 /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
14504 ** so currently we default to the macro that redefines fdatasync to fsync
14505 */
14506 rc = fsync(fd);
14507 #else
14508 rc = fdatasync(fd);
14509 #if OS_VXWORKS
14510 if( rc==-1 && errno==ENOTSUP ){
14511 rc = fsync(fd);
14512 }
14513 #endif /* OS_VXWORKS */
14514 #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
14515
14516 if( OS_VXWORKS && rc!= -1 ){
14517 rc = 0;
14518 }
14519 return rc;
14520 }
14521
14522 /*
14523 ** Open a file descriptor to the directory containing file zFilename.
14524 ** If successful, *pFd is set to the opened file descriptor and
14525 ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
14526 ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
14527 ** value.
14528 **
14529 ** The directory file descriptor is used for only one thing - to
14530 ** fsync() a directory to make sure file creation and deletion events
14531 ** are flushed to disk. Such fsyncs are not needed on newer
14532 ** journaling filesystems, but are required on older filesystems.
14533 **
14534 ** This routine can be overridden using the xSetSysCall interface.
14535 ** The ability to override this routine was added in support of the
14536 ** chromium sandbox. Opening a directory is a security risk (we are
14537 ** told) so making it overrideable allows the chromium sandbox to
14538 ** replace this routine with a harmless no-op. To make this routine
14539 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
14540 ** *pFd set to a negative number.
14541 **
14542 ** If SQLITE_OK is returned, the caller is responsible for closing
14543 ** the file descriptor *pFd using close().
14544 */
14545 static int openDirectory(const char *zFilename, int *pFd){
14546 int ii;
14547 int fd = -1;
14548 char zDirname[MAX_PATHNAME+1];
14549
14550 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
14551 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
14552 if( ii>0 ){
14553 zDirname[ii] = '\0';
14554 }else{
14555 if( zDirname[0]!='/' ) zDirname[0] = '.';
14556 zDirname[1] = 0;
14557 }
14558 fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
14559 if( fd>=0 ){
14560 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
14561 }
14562 *pFd = fd;
14563 if( fd>=0 ) return SQLITE_OK;
14564 return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname);
14565 }
14566
14567 /*
14568 ** Make sure all writes to a particular file are committed to disk.
14569 **
14570 ** If dataOnly==0 then both the file itself and its metadata (file
14571 ** size, access time, etc) are synced. If dataOnly!=0 then only the
14572 ** file data is synced.
14573 **
14574 ** Under Unix, also make sure that the directory entry for the file
14575 ** has been created by fsync-ing the directory that contains the file.
14576 ** If we do not do this and we encounter a power failure, the directory
14577 ** entry for the journal might not exist after we reboot. The next
14578 ** SQLite to access the file will not know that the journal exists (because
14579 ** the directory entry for the journal was never created) and the transaction
14580 ** will not roll back - possibly leading to database corruption.
14581 */
14582 static int unixSync(sqlite3_file *id, int flags){
14583 int rc;
14584 unixFile *pFile = (unixFile*)id;
14585
14586 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
14587 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
14588
14589 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
14590 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
14591 || (flags&0x0F)==SQLITE_SYNC_FULL
14592 );
14593
14594 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
14595 ** line is to test that doing so does not cause any problems.
14596 */
14597 SimulateDiskfullError( return SQLITE_FULL );
14598
14599 assert( pFile );
14600 OSTRACE(("SYNC %-3d\n", pFile->h));
14601 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
14602 SimulateIOError( rc=1 );
14603 if( rc ){
14604 storeLastErrno(pFile, errno);
14605 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
14606 }
14607
14608 /* Also fsync the directory containing the file if the DIRSYNC flag
14609 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
14610 ** are unable to fsync a directory, so ignore errors on the fsync.
14611 */
14612 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
14613 int dirfd;
14614 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
14615 HAVE_FULLFSYNC, isFullsync));
14616 rc = osOpenDirectory(pFile->zPath, &dirfd);
14617 if( rc==SQLITE_OK ){
14618 full_fsync(dirfd, 0, 0);
14619 robust_close(pFile, dirfd, __LINE__);
14620 }else{
14621 assert( rc==SQLITE_CANTOPEN );
14622 rc = SQLITE_OK;
14623 }
14624 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
14625 }
14626 return rc;
14627 }
14628
14629 /*
14630 ** Truncate an open file to a specified size
14631 */
14632 static int unixTruncate(sqlite3_file *id, i64 nByte){
14633 unixFile *pFile = (unixFile *)id;
14634 int rc;
14635 assert( pFile );
14636 SimulateIOError( return SQLITE_IOERR_TRUNCATE );
14637
14638 /* If the user has configured a chunk-size for this file, truncate the
14639 ** file so that it consists of an integer number of chunks (i.e. the
14640 ** actual file size after the operation may be larger than the requested
14641 ** size).
14642 */
14643 if( pFile->szChunk>0 ){
14644 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
14645 }
14646
14647 rc = robust_ftruncate(pFile->h, nByte);
14648 if( rc ){
14649 storeLastErrno(pFile, errno);
14650 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
14651 }else{
14652 #ifdef SQLITE_DEBUG
14653 /* If we are doing a normal write to a database file (as opposed to
14654 ** doing a hot-journal rollback or a write to some file other than a
14655 ** normal database file) and we truncate the file to zero length,
14656 ** that effectively updates the change counter. This might happen
14657 ** when restoring a database using the backup API from a zero-length
14658 ** source.
14659 */
14660 if( pFile->inNormalWrite && nByte==0 ){
14661 pFile->transCntrChng = 1;
14662 }
14663 #endif
14664
14665 #if SQLITE_MAX_MMAP_SIZE>0
14666 /* If the file was just truncated to a size smaller than the currently
14667 ** mapped region, reduce the effective mapping size as well. SQLite will
14668 ** use read() and write() to access data beyond this point from now on.
14669 */
14670 if( nByte<pFile->mmapSize ){
14671 pFile->mmapSize = nByte;
14672 }
14673 #endif
14674
14675 return SQLITE_OK;
14676 }
14677 }
14678
14679 /*
14680 ** Determine the current size of a file in bytes
14681 */
14682 static int unixFileSize(sqlite3_file *id, i64 *pSize){
14683 int rc;
14684 struct stat buf;
14685 assert( id );
14686 rc = osFstat(((unixFile*)id)->h, &buf);
14687 SimulateIOError( rc=1 );
14688 if( rc!=0 ){
14689 storeLastErrno((unixFile*)id, errno);
14690 return SQLITE_IOERR_FSTAT;
14691 }
14692 *pSize = buf.st_size;
14693
14694 /* When opening a zero-size database, the findInodeInfo() procedure
14695 ** writes a single byte into that file in order to work around a bug
14696 ** in the OS-X msdos filesystem. In order to avoid problems with upper
14697 ** layers, we need to report this file size as zero even though it is
14698 ** really 1. Ticket #3260.
14699 */
14700 if( *pSize==1 ) *pSize = 0;
14701
14702
14703 return SQLITE_OK;
14704 }
14705
14706 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
14707 /*
14708 ** Handler for proxy-locking file-control verbs. Defined below in the
14709 ** proxying locking division.
14710 */
14711 static int proxyFileControl(sqlite3_file*,int,void*);
14712 #endif
14713
14714 /*
14715 ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
14716 ** file-control operation. Enlarge the database to nBytes in size
14717 ** (rounded up to the next chunk-size). If the database is already
14718 ** nBytes or larger, this routine is a no-op.
14719 */
14720 static int fcntlSizeHint(unixFile *pFile, i64 nByte){
14721 if( pFile->szChunk>0 ){
14722 i64 nSize; /* Required file size */
14723 struct stat buf; /* Used to hold return values of fstat() */
14724
14725 if( osFstat(pFile->h, &buf) ){
14726 return SQLITE_IOERR_FSTAT;
14727 }
14728
14729 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
14730 if( nSize>(i64)buf.st_size ){
14731
14732 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
14733 /* The code below is handling the return value of osFallocate()
14734 ** correctly. posix_fallocate() is defined to "returns zero on success,
14735 ** or an error number on failure". See the manpage for details. */
14736 int err;
14737 do{
14738 err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
14739 }while( err==EINTR );
14740 if( err ) return SQLITE_IOERR_WRITE;
14741 #else
14742 /* If the OS does not have posix_fallocate(), fake it. Write a
14743 ** single byte to the last byte in each block that falls entirely
14744 ** within the extended region. Then, if required, a single byte
14745 ** at offset (nSize-1), to set the size of the file correctly.
14746 ** This is a similar technique to that used by glibc on systems
14747 ** that do not have a real fallocate() call.
14748 */
14749 int nBlk = buf.st_blksize; /* File-system block size */
14750 int nWrite = 0; /* Number of bytes written by seekAndWrite */
14751 i64 iWrite; /* Next offset to write to */
14752
14753 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
14754 assert( iWrite>=buf.st_size );
14755 assert( ((iWrite+1)%nBlk)==0 );
14756 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
14757 if( iWrite>=nSize ) iWrite = nSize - 1;
14758 nWrite = seekAndWrite(pFile, iWrite, "", 1);
14759 if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
14760 }
14761 #endif
14762 }
14763 }
14764
14765 #if SQLITE_MAX_MMAP_SIZE>0
14766 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
14767 int rc;
14768 if( pFile->szChunk<=0 ){
14769 if( robust_ftruncate(pFile->h, nByte) ){
14770 storeLastErrno(pFile, errno);
14771 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
14772 }
14773 }
14774
14775 rc = unixMapfile(pFile, nByte);
14776 return rc;
14777 }
14778 #endif
14779
14780 return SQLITE_OK;
14781 }
14782
14783 /*
14784 ** If *pArg is initially negative then this is a query. Set *pArg to
14785 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
14786 **
14787 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
14788 */
14789 static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
14790 if( *pArg<0 ){
14791 *pArg = (pFile->ctrlFlags & mask)!=0;
14792 }else if( (*pArg)==0 ){
14793 pFile->ctrlFlags &= ~mask;
14794 }else{
14795 pFile->ctrlFlags |= mask;
14796 }
14797 }
14798
14799 /* Forward declaration */
14800 static int unixGetTempname(int nBuf, char *zBuf);
14801
14802 /*
14803 ** Information and control of an open file handle.
14804 */
14805 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
14806 unixFile *pFile = (unixFile*)id;
14807 switch( op ){
14808 case SQLITE_FCNTL_LOCKSTATE: {
14809 *(int*)pArg = pFile->eFileLock;
14810 return SQLITE_OK;
14811 }
14812 case SQLITE_FCNTL_LAST_ERRNO: {
14813 *(int*)pArg = pFile->lastErrno;
14814 return SQLITE_OK;
14815 }
14816 case SQLITE_FCNTL_CHUNK_SIZE: {
14817 pFile->szChunk = *(int *)pArg;
14818 return SQLITE_OK;
14819 }
14820 case SQLITE_FCNTL_SIZE_HINT: {
14821 int rc;
14822 SimulateIOErrorBenign(1);
14823 rc = fcntlSizeHint(pFile, *(i64 *)pArg);
14824 SimulateIOErrorBenign(0);
14825 return rc;
14826 }
14827 case SQLITE_FCNTL_PERSIST_WAL: {
14828 unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg);
14829 return SQLITE_OK;
14830 }
14831 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
14832 unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg);
14833 return SQLITE_OK;
14834 }
14835 case SQLITE_FCNTL_VFSNAME: {
14836 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
14837 return SQLITE_OK;
14838 }
14839 case SQLITE_FCNTL_TEMPFILENAME: {
14840 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
14841 if( zTFile ){
14842 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
14843 *(char**)pArg = zTFile;
14844 }
14845 return SQLITE_OK;
14846 }
14847 case SQLITE_FCNTL_HAS_MOVED: {
14848 *(int*)pArg = fileHasMoved(pFile);
14849 return SQLITE_OK;
14850 }
14851 #if SQLITE_MAX_MMAP_SIZE>0
14852 case SQLITE_FCNTL_MMAP_SIZE: {
14853 i64 newLimit = *(i64*)pArg;
14854 int rc = SQLITE_OK;
14855 if( newLimit>sqlite3GlobalConfig.mxMmap ){
14856 newLimit = sqlite3GlobalConfig.mxMmap;
14857 }
14858 *(i64*)pArg = pFile->mmapSizeMax;
14859 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
14860 pFile->mmapSizeMax = newLimit;
14861 if( pFile->mmapSize>0 ){
14862 unixUnmapfile(pFile);
14863 rc = unixMapfile(pFile, -1);
14864 }
14865 }
14866 return rc;
14867 }
14868 #endif
14869 #ifdef SQLITE_DEBUG
14870 /* The pager calls this method to signal that it has done
14871 ** a rollback and that the database is therefore unchanged and
14872 ** it hence it is OK for the transaction change counter to be
14873 ** unchanged.
14874 */
14875 case SQLITE_FCNTL_DB_UNCHANGED: {
14876 ((unixFile*)id)->dbUpdate = 0;
14877 return SQLITE_OK;
14878 }
14879 #endif
14880 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
14881 case SQLITE_FCNTL_SET_LOCKPROXYFILE:
14882 case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
14883 return proxyFileControl(id,op,pArg);
14884 }
14885 #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
14886 }
14887 return SQLITE_NOTFOUND;
14888 }
14889
14890 /*
14891 ** Return the sector size in bytes of the underlying block device for
14892 ** the specified file. This is almost always 512 bytes, but may be
14893 ** larger for some devices.
14894 **
14895 ** SQLite code assumes this function cannot fail. It also assumes that
14896 ** if two files are created in the same file-system directory (i.e.
14897 ** a database and its journal file) that the sector size will be the
14898 ** same for both.
14899 */
14900 #ifndef __QNXNTO__
14901 static int unixSectorSize(sqlite3_file *NotUsed){
14902 UNUSED_PARAMETER(NotUsed);
14903 return SQLITE_DEFAULT_SECTOR_SIZE;
14904 }
14905 #endif
14906
14907 /*
14908 ** The following version of unixSectorSize() is optimized for QNX.
14909 */
14910 #ifdef __QNXNTO__
14911 #include <sys/dcmd_blk.h>
14912 #include <sys/statvfs.h>
14913 static int unixSectorSize(sqlite3_file *id){
14914 unixFile *pFile = (unixFile*)id;
14915 if( pFile->sectorSize == 0 ){
14916 struct statvfs fsInfo;
14917
14918 /* Set defaults for non-supported filesystems */
14919 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
14920 pFile->deviceCharacteristics = 0;
14921 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
14922 return pFile->sectorSize;
14923 }
14924
14925 if( !strcmp(fsInfo.f_basetype, "tmp") ) {
14926 pFile->sectorSize = fsInfo.f_bsize;
14927 pFile->deviceCharacteristics =
14928 SQLITE_IOCAP_ATOMIC4K | /* All ram filesystem writes are atomic */
14929 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
14930 ** the write succeeds */
14931 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
14932 ** so it is ordered */
14933 0;
14934 }else if( strstr(fsInfo.f_basetype, "etfs") ){
14935 pFile->sectorSize = fsInfo.f_bsize;
14936 pFile->deviceCharacteristics =
14937 /* etfs cluster size writes are atomic */
14938 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
14939 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
14940 ** the write succeeds */
14941 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
14942 ** so it is ordered */
14943 0;
14944 }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
14945 pFile->sectorSize = fsInfo.f_bsize;
14946 pFile->deviceCharacteristics =
14947 SQLITE_IOCAP_ATOMIC | /* All filesystem writes are atomic */
14948 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
14949 ** the write succeeds */
14950 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
14951 ** so it is ordered */
14952 0;
14953 }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
14954 pFile->sectorSize = fsInfo.f_bsize;
14955 pFile->deviceCharacteristics =
14956 /* full bitset of atomics from max sector size and smaller */
14957 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
14958 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
14959 ** so it is ordered */
14960 0;
14961 }else if( strstr(fsInfo.f_basetype, "dos") ){
14962 pFile->sectorSize = fsInfo.f_bsize;
14963 pFile->deviceCharacteristics =
14964 /* full bitset of atomics from max sector size and smaller */
14965 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
14966 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind
14967 ** so it is ordered */
14968 0;
14969 }else{
14970 pFile->deviceCharacteristics =
14971 SQLITE_IOCAP_ATOMIC512 | /* blocks are atomic */
14972 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until
14973 ** the write succeeds */
14974 0;
14975 }
14976 }
14977 /* Last chance verification. If the sector size isn't a multiple of 512
14978 ** then it isn't valid.*/
14979 if( pFile->sectorSize % 512 != 0 ){
14980 pFile->deviceCharacteristics = 0;
14981 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
14982 }
14983 return pFile->sectorSize;
14984 }
14985 #endif /* __QNXNTO__ */
14986
14987 /*
14988 ** Return the device characteristics for the file.
14989 **
14990 ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
14991 ** However, that choice is controversial since technically the underlying
14992 ** file system does not always provide powersafe overwrites. (In other
14993 ** words, after a power-loss event, parts of the file that were never
14994 ** written might end up being altered.) However, non-PSOW behavior is very,
14995 ** very rare. And asserting PSOW makes a large reduction in the amount
14996 ** of required I/O for journaling, since a lot of padding is eliminated.
14997 ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
14998 ** available to turn it off and URI query parameter available to turn it off.
14999 */
15000 static int unixDeviceCharacteristics(sqlite3_file *id){
15001 unixFile *p = (unixFile*)id;
15002 int rc = 0;
15003 #ifdef __QNXNTO__
15004 if( p->sectorSize==0 ) unixSectorSize(id);
15005 rc = p->deviceCharacteristics;
15006 #endif
15007 if( p->ctrlFlags & UNIXFILE_PSOW ){
15008 rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
15009 }
15010 return rc;
15011 }
15012
15013 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
15014
15015 /*
15016 ** Return the system page size.
15017 **
15018 ** This function should not be called directly by other code in this file.
15019 ** Instead, it should be called via macro osGetpagesize().
15020 */
15021 static int unixGetpagesize(void){
15022 #if OS_VXWORKS
15023 return 1024;
15024 #elif defined(_BSD_SOURCE)
15025 return getpagesize();
15026 #else
15027 return (int)sysconf(_SC_PAGESIZE);
15028 #endif
15029 }
15030
15031 #endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */
15032
15033 #ifndef SQLITE_OMIT_WAL
15034
15035 /*
15036 ** Object used to represent an shared memory buffer.
15037 **
15038 ** When multiple threads all reference the same wal-index, each thread
15039 ** has its own unixShm object, but they all point to a single instance
15040 ** of this unixShmNode object. In other words, each wal-index is opened
15041 ** only once per process.
15042 **
15043 ** Each unixShmNode object is connected to a single unixInodeInfo object.
15044 ** We could coalesce this object into unixInodeInfo, but that would mean
15045 ** every open file that does not use shared memory (in other words, most
15046 ** open files) would have to carry around this extra information. So
15047 ** the unixInodeInfo object contains a pointer to this unixShmNode object
15048 ** and the unixShmNode object is created only when needed.
15049 **
15050 ** unixMutexHeld() must be true when creating or destroying
15051 ** this object or while reading or writing the following fields:
15052 **
15053 ** nRef
15054 **
15055 ** The following fields are read-only after the object is created:
15056 **
15057 ** fid
15058 ** zFilename
15059 **
15060 ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
15061 ** unixMutexHeld() is true when reading or writing any other field
15062 ** in this structure.
15063 */
15064 struct unixShmNode {
15065 unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */
15066 sqlite3_mutex *mutex; /* Mutex to access this object */
15067 char *zFilename; /* Name of the mmapped file */
15068 int h; /* Open file descriptor */
15069 int szRegion; /* Size of shared-memory regions */
15070 u16 nRegion; /* Size of array apRegion */
15071 u8 isReadonly; /* True if read-only */
15072 char **apRegion; /* Array of mapped shared-memory regions */
15073 int nRef; /* Number of unixShm objects pointing to this */
15074 unixShm *pFirst; /* All unixShm objects pointing to this */
15075 #ifdef SQLITE_DEBUG
15076 u8 exclMask; /* Mask of exclusive locks held */
15077 u8 sharedMask; /* Mask of shared locks held */
15078 u8 nextShmId; /* Next available unixShm.id value */
15079 #endif
15080 };
15081
15082 /*
15083 ** Structure used internally by this VFS to record the state of an
15084 ** open shared memory connection.
15085 **
15086 ** The following fields are initialized when this object is created and
15087 ** are read-only thereafter:
15088 **
15089 ** unixShm.pFile
15090 ** unixShm.id
15091 **
15092 ** All other fields are read/write. The unixShm.pFile->mutex must be held
15093 ** while accessing any read/write fields.
15094 */
15095 struct unixShm {
15096 unixShmNode *pShmNode; /* The underlying unixShmNode object */
15097 unixShm *pNext; /* Next unixShm with the same unixShmNode */
15098 u8 hasMutex; /* True if holding the unixShmNode mutex */
15099 u8 id; /* Id of this connection within its unixShmNode */
15100 u16 sharedMask; /* Mask of shared locks held */
15101 u16 exclMask; /* Mask of exclusive locks held */
15102 };
15103
15104 /*
15105 ** Constants used for locking
15106 */
15107 #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
15108 #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
15109
15110 /*
15111 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
15112 **
15113 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
15114 ** otherwise.
15115 */
15116 static int unixShmSystemLock(
15117 unixFile *pFile, /* Open connection to the WAL file */
15118 int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */
15119 int ofst, /* First byte of the locking range */
15120 int n /* Number of bytes to lock */
15121 ){
15122 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
15123 struct flock f; /* The posix advisory locking structure */
15124 int rc = SQLITE_OK; /* Result code form fcntl() */
15125
15126 /* Access to the unixShmNode object is serialized by the caller */
15127 pShmNode = pFile->pInode->pShmNode;
15128 assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
15129
15130 /* Shared locks never span more than one byte */
15131 assert( n==1 || lockType!=F_RDLCK );
15132
15133 /* Locks are within range */
15134 assert( n>=1 && n<=SQLITE_SHM_NLOCK );
15135
15136 if( pShmNode->h>=0 ){
15137 /* Initialize the locking parameters */
15138 memset(&f, 0, sizeof(f));
15139 f.l_type = lockType;
15140 f.l_whence = SEEK_SET;
15141 f.l_start = ofst;
15142 f.l_len = n;
15143
15144 rc = osFcntl(pShmNode->h, F_SETLK, &f);
15145 rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
15146 }
15147
15148 /* Update the global lock state and do debug tracing */
15149 #ifdef SQLITE_DEBUG
15150 { u16 mask;
15151 OSTRACE(("SHM-LOCK "));
15152 mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
15153 if( rc==SQLITE_OK ){
15154 if( lockType==F_UNLCK ){
15155 OSTRACE(("unlock %d ok", ofst));
15156 pShmNode->exclMask &= ~mask;
15157 pShmNode->sharedMask &= ~mask;
15158 }else if( lockType==F_RDLCK ){
15159 OSTRACE(("read-lock %d ok", ofst));
15160 pShmNode->exclMask &= ~mask;
15161 pShmNode->sharedMask |= mask;
15162 }else{
15163 assert( lockType==F_WRLCK );
15164 OSTRACE(("write-lock %d ok", ofst));
15165 pShmNode->exclMask |= mask;
15166 pShmNode->sharedMask &= ~mask;
15167 }
15168 }else{
15169 if( lockType==F_UNLCK ){
15170 OSTRACE(("unlock %d failed", ofst));
15171 }else if( lockType==F_RDLCK ){
15172 OSTRACE(("read-lock failed"));
15173 }else{
15174 assert( lockType==F_WRLCK );
15175 OSTRACE(("write-lock %d failed", ofst));
15176 }
15177 }
15178 OSTRACE((" - afterwards %03x,%03x\n",
15179 pShmNode->sharedMask, pShmNode->exclMask));
15180 }
15181 #endif
15182
15183 return rc;
15184 }
15185
15186 /*
15187 ** Return the minimum number of 32KB shm regions that should be mapped at
15188 ** a time, assuming that each mapping must be an integer multiple of the
15189 ** current system page-size.
15190 **
15191 ** Usually, this is 1. The exception seems to be systems that are configured
15192 ** to use 64KB pages - in this case each mapping must cover at least two
15193 ** shm regions.
15194 */
15195 static int unixShmRegionPerMap(void){
15196 int shmsz = 32*1024; /* SHM region size */
15197 int pgsz = osGetpagesize(); /* System page size */
15198 assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */
15199 if( pgsz<shmsz ) return 1;
15200 return pgsz/shmsz;
15201 }
15202
15203 /*
15204 ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
15205 **
15206 ** This is not a VFS shared-memory method; it is a utility function called
15207 ** by VFS shared-memory methods.
15208 */
15209 static void unixShmPurge(unixFile *pFd){
15210 unixShmNode *p = pFd->pInode->pShmNode;
15211 assert( unixMutexHeld() );
15212 if( p && ALWAYS(p->nRef==0) ){
15213 int nShmPerMap = unixShmRegionPerMap();
15214 int i;
15215 assert( p->pInode==pFd->pInode );
15216 sqlite3_mutex_free(p->mutex);
15217 for(i=0; i<p->nRegion; i+=nShmPerMap){
15218 if( p->h>=0 ){
15219 osMunmap(p->apRegion[i], p->szRegion);
15220 }else{
15221 sqlite3_free(p->apRegion[i]);
15222 }
15223 }
15224 sqlite3_free(p->apRegion);
15225 if( p->h>=0 ){
15226 robust_close(pFd, p->h, __LINE__);
15227 p->h = -1;
15228 }
15229 p->pInode->pShmNode = 0;
15230 sqlite3_free(p);
15231 }
15232 }
15233
15234 /*
15235 ** Open a shared-memory area associated with open database file pDbFd.
15236 ** This particular implementation uses mmapped files.
15237 **
15238 ** The file used to implement shared-memory is in the same directory
15239 ** as the open database file and has the same name as the open database
15240 ** file with the "-shm" suffix added. For example, if the database file
15241 ** is "/home/user1/config.db" then the file that is created and mmapped
15242 ** for shared memory will be called "/home/user1/config.db-shm".
15243 **
15244 ** Another approach to is to use files in /dev/shm or /dev/tmp or an
15245 ** some other tmpfs mount. But if a file in a different directory
15246 ** from the database file is used, then differing access permissions
15247 ** or a chroot() might cause two different processes on the same
15248 ** database to end up using different files for shared memory -
15249 ** meaning that their memory would not really be shared - resulting
15250 ** in database corruption. Nevertheless, this tmpfs file usage
15251 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
15252 ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
15253 ** option results in an incompatible build of SQLite; builds of SQLite
15254 ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
15255 ** same database file at the same time, database corruption will likely
15256 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
15257 ** "unsupported" and may go away in a future SQLite release.
15258 **
15259 ** When opening a new shared-memory file, if no other instances of that
15260 ** file are currently open, in this process or in other processes, then
15261 ** the file must be truncated to zero length or have its header cleared.
15262 **
15263 ** If the original database file (pDbFd) is using the "unix-excl" VFS
15264 ** that means that an exclusive lock is held on the database file and
15265 ** that no other processes are able to read or write the database. In
15266 ** that case, we do not really need shared memory. No shared memory
15267 ** file is created. The shared memory will be simulated with heap memory.
15268 */
15269 static int unixOpenSharedMemory(unixFile *pDbFd){
15270 struct unixShm *p = 0; /* The connection to be opened */
15271 struct unixShmNode *pShmNode; /* The underlying mmapped file */
15272 int rc; /* Result code */
15273 unixInodeInfo *pInode; /* The inode of fd */
15274 char *zShmFilename; /* Name of the file used for SHM */
15275 int nShmFilename; /* Size of the SHM filename in bytes */
15276
15277 /* Allocate space for the new unixShm object. */
15278 p = sqlite3_malloc64( sizeof(*p) );
15279 if( p==0 ) return SQLITE_NOMEM_BKPT;
15280 memset(p, 0, sizeof(*p));
15281 assert( pDbFd->pShm==0 );
15282
15283 /* Check to see if a unixShmNode object already exists. Reuse an existing
15284 ** one if present. Create a new one if necessary.
15285 */
15286 unixEnterMutex();
15287 pInode = pDbFd->pInode;
15288 pShmNode = pInode->pShmNode;
15289 if( pShmNode==0 ){
15290 struct stat sStat; /* fstat() info for database file */
15291 #ifndef SQLITE_SHM_DIRECTORY
15292 const char *zBasePath = pDbFd->zPath;
15293 #endif
15294
15295 /* Call fstat() to figure out the permissions on the database file. If
15296 ** a new *-shm file is created, an attempt will be made to create it
15297 ** with the same permissions.
15298 */
15299 if( osFstat(pDbFd->h, &sStat) ){
15300 rc = SQLITE_IOERR_FSTAT;
15301 goto shm_open_err;
15302 }
15303
15304 #ifdef SQLITE_SHM_DIRECTORY
15305 nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
15306 #else
15307 nShmFilename = 6 + (int)strlen(zBasePath);
15308 #endif
15309 pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename );
15310 if( pShmNode==0 ){
15311 rc = SQLITE_NOMEM_BKPT;
15312 goto shm_open_err;
15313 }
15314 memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
15315 zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
15316 #ifdef SQLITE_SHM_DIRECTORY
15317 sqlite3_snprintf(nShmFilename, zShmFilename,
15318 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
15319 (u32)sStat.st_ino, (u32)sStat.st_dev);
15320 #else
15321 sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", zBasePath);
15322 sqlite3FileSuffix3(pDbFd->zPath, zShmFilename);
15323 #endif
15324 pShmNode->h = -1;
15325 pDbFd->pInode->pShmNode = pShmNode;
15326 pShmNode->pInode = pDbFd->pInode;
15327 if( sqlite3GlobalConfig.bCoreMutex ){
15328 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
15329 if( pShmNode->mutex==0 ){
15330 rc = SQLITE_NOMEM_BKPT;
15331 goto shm_open_err;
15332 }
15333 }
15334
15335 if( pInode->bProcessLock==0 ){
15336 int openFlags = O_RDWR | O_CREAT;
15337 if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
15338 openFlags = O_RDONLY;
15339 pShmNode->isReadonly = 1;
15340 }
15341 pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));
15342 if( pShmNode->h<0 ){
15343 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
15344 goto shm_open_err;
15345 }
15346
15347 /* If this process is running as root, make sure that the SHM file
15348 ** is owned by the same user that owns the original database. Otherwise,
15349 ** the original owner will not be able to connect.
15350 */
15351 robustFchown(pShmNode->h, sStat.st_uid, sStat.st_gid);
15352
15353 /* Check to see if another process is holding the dead-man switch.
15354 ** If not, truncate the file to zero length.
15355 */
15356 rc = SQLITE_OK;
15357 if( unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
15358 if( robust_ftruncate(pShmNode->h, 0) ){
15359 rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
15360 }
15361 }
15362 if( rc==SQLITE_OK ){
15363 rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1);
15364 }
15365 if( rc ) goto shm_open_err;
15366 }
15367 }
15368
15369 /* Make the new connection a child of the unixShmNode */
15370 p->pShmNode = pShmNode;
15371 #ifdef SQLITE_DEBUG
15372 p->id = pShmNode->nextShmId++;
15373 #endif
15374 pShmNode->nRef++;
15375 pDbFd->pShm = p;
15376 unixLeaveMutex();
15377
15378 /* The reference count on pShmNode has already been incremented under
15379 ** the cover of the unixEnterMutex() mutex and the pointer from the
15380 ** new (struct unixShm) object to the pShmNode has been set. All that is
15381 ** left to do is to link the new object into the linked list starting
15382 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
15383 ** mutex.
15384 */
15385 sqlite3_mutex_enter(pShmNode->mutex);
15386 p->pNext = pShmNode->pFirst;
15387 pShmNode->pFirst = p;
15388 sqlite3_mutex_leave(pShmNode->mutex);
15389 return SQLITE_OK;
15390
15391 /* Jump here on any error */
15392 shm_open_err:
15393 unixShmPurge(pDbFd); /* This call frees pShmNode if required */
15394 sqlite3_free(p);
15395 unixLeaveMutex();
15396 return rc;
15397 }
15398
15399 /*
15400 ** This function is called to obtain a pointer to region iRegion of the
15401 ** shared-memory associated with the database file fd. Shared-memory regions
15402 ** are numbered starting from zero. Each shared-memory region is szRegion
15403 ** bytes in size.
15404 **
15405 ** If an error occurs, an error code is returned and *pp is set to NULL.
15406 **
15407 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
15408 ** region has not been allocated (by any client, including one running in a
15409 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
15410 ** bExtend is non-zero and the requested shared-memory region has not yet
15411 ** been allocated, it is allocated by this function.
15412 **
15413 ** If the shared-memory region has already been allocated or is allocated by
15414 ** this call as described above, then it is mapped into this processes
15415 ** address space (if it is not already), *pp is set to point to the mapped
15416 ** memory and SQLITE_OK returned.
15417 */
15418 static int unixShmMap(
15419 sqlite3_file *fd, /* Handle open on database file */
15420 int iRegion, /* Region to retrieve */
15421 int szRegion, /* Size of regions */
15422 int bExtend, /* True to extend file if necessary */
15423 void volatile **pp /* OUT: Mapped memory */
15424 ){
15425 unixFile *pDbFd = (unixFile*)fd;
15426 unixShm *p;
15427 unixShmNode *pShmNode;
15428 int rc = SQLITE_OK;
15429 int nShmPerMap = unixShmRegionPerMap();
15430 int nReqRegion;
15431
15432 /* If the shared-memory file has not yet been opened, open it now. */
15433 if( pDbFd->pShm==0 ){
15434 rc = unixOpenSharedMemory(pDbFd);
15435 if( rc!=SQLITE_OK ) return rc;
15436 }
15437
15438 p = pDbFd->pShm;
15439 pShmNode = p->pShmNode;
15440 sqlite3_mutex_enter(pShmNode->mutex);
15441 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
15442 assert( pShmNode->pInode==pDbFd->pInode );
15443 assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
15444 assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
15445
15446 /* Minimum number of regions required to be mapped. */
15447 nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap;
15448
15449 if( pShmNode->nRegion<nReqRegion ){
15450 char **apNew; /* New apRegion[] array */
15451 int nByte = nReqRegion*szRegion; /* Minimum required file size */
15452 struct stat sStat; /* Used by fstat() */
15453
15454 pShmNode->szRegion = szRegion;
15455
15456 if( pShmNode->h>=0 ){
15457 /* The requested region is not mapped into this processes address space.
15458 ** Check to see if it has been allocated (i.e. if the wal-index file is
15459 ** large enough to contain the requested region).
15460 */
15461 if( osFstat(pShmNode->h, &sStat) ){
15462 rc = SQLITE_IOERR_SHMSIZE;
15463 goto shmpage_out;
15464 }
15465
15466 if( sStat.st_size<nByte ){
15467 /* The requested memory region does not exist. If bExtend is set to
15468 ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
15469 */
15470 if( !bExtend ){
15471 goto shmpage_out;
15472 }
15473
15474 /* Alternatively, if bExtend is true, extend the file. Do this by
15475 ** writing a single byte to the end of each (OS) page being
15476 ** allocated or extended. Technically, we need only write to the
15477 ** last page in order to extend the file. But writing to all new
15478 ** pages forces the OS to allocate them immediately, which reduces
15479 ** the chances of SIGBUS while accessing the mapped region later on.
15480 */
15481 else{
15482 static const int pgsz = 4096;
15483 int iPg;
15484
15485 /* Write to the last byte of each newly allocated or extended page */
15486 assert( (nByte % pgsz)==0 );
15487 for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
15488 int x = 0;
15489 if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, &x)!=1 ){
15490 const char *zFile = pShmNode->zFilename;
15491 rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile);
15492 goto shmpage_out;
15493 }
15494 }
15495 }
15496 }
15497 }
15498
15499 /* Map the requested memory region into this processes address space. */
15500 apNew = (char **)sqlite3_realloc(
15501 pShmNode->apRegion, nReqRegion*sizeof(char *)
15502 );
15503 if( !apNew ){
15504 rc = SQLITE_IOERR_NOMEM_BKPT;
15505 goto shmpage_out;
15506 }
15507 pShmNode->apRegion = apNew;
15508 while( pShmNode->nRegion<nReqRegion ){
15509 int nMap = szRegion*nShmPerMap;
15510 int i;
15511 void *pMem;
15512 if( pShmNode->h>=0 ){
15513 pMem = osMmap(0, nMap,
15514 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
15515 MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
15516 );
15517 if( pMem==MAP_FAILED ){
15518 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
15519 goto shmpage_out;
15520 }
15521 }else{
15522 pMem = sqlite3_malloc64(szRegion);
15523 if( pMem==0 ){
15524 rc = SQLITE_NOMEM_BKPT;
15525 goto shmpage_out;
15526 }
15527 memset(pMem, 0, szRegion);
15528 }
15529
15530 for(i=0; i<nShmPerMap; i++){
15531 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
15532 }
15533 pShmNode->nRegion += nShmPerMap;
15534 }
15535 }
15536
15537 shmpage_out:
15538 if( pShmNode->nRegion>iRegion ){
15539 *pp = pShmNode->apRegion[iRegion];
15540 }else{
15541 *pp = 0;
15542 }
15543 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
15544 sqlite3_mutex_leave(pShmNode->mutex);
15545 return rc;
15546 }
15547
15548 /*
15549 ** Change the lock state for a shared-memory segment.
15550 **
15551 ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
15552 ** different here than in posix. In xShmLock(), one can go from unlocked
15553 ** to shared and back or from unlocked to exclusive and back. But one may
15554 ** not go from shared to exclusive or from exclusive to shared.
15555 */
15556 static int unixShmLock(
15557 sqlite3_file *fd, /* Database file holding the shared memory */
15558 int ofst, /* First lock to acquire or release */
15559 int n, /* Number of locks to acquire or release */
15560 int flags /* What to do with the lock */
15561 ){
15562 unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */
15563 unixShm *p = pDbFd->pShm; /* The shared memory being locked */
15564 unixShm *pX; /* For looping over all siblings */
15565 unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */
15566 int rc = SQLITE_OK; /* Result code */
15567 u16 mask; /* Mask of locks to take or release */
15568
15569 assert( pShmNode==pDbFd->pInode->pShmNode );
15570 assert( pShmNode->pInode==pDbFd->pInode );
15571 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
15572 assert( n>=1 );
15573 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
15574 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
15575 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
15576 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
15577 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
15578 assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
15579 assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
15580
15581 mask = (1<<(ofst+n)) - (1<<ofst);
15582 assert( n>1 || mask==(1<<ofst) );
15583 sqlite3_mutex_enter(pShmNode->mutex);
15584 if( flags & SQLITE_SHM_UNLOCK ){
15585 u16 allMask = 0; /* Mask of locks held by siblings */
15586
15587 /* See if any siblings hold this same lock */
15588 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
15589 if( pX==p ) continue;
15590 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
15591 allMask |= pX->sharedMask;
15592 }
15593
15594 /* Unlock the system-level locks */
15595 if( (mask & allMask)==0 ){
15596 rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n);
15597 }else{
15598 rc = SQLITE_OK;
15599 }
15600
15601 /* Undo the local locks */
15602 if( rc==SQLITE_OK ){
15603 p->exclMask &= ~mask;
15604 p->sharedMask &= ~mask;
15605 }
15606 }else if( flags & SQLITE_SHM_SHARED ){
15607 u16 allShared = 0; /* Union of locks held by connections other than "p" */
15608
15609 /* Find out which shared locks are already held by sibling connections.
15610 ** If any sibling already holds an exclusive lock, go ahead and return
15611 ** SQLITE_BUSY.
15612 */
15613 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
15614 if( (pX->exclMask & mask)!=0 ){
15615 rc = SQLITE_BUSY;
15616 break;
15617 }
15618 allShared |= pX->sharedMask;
15619 }
15620
15621 /* Get shared locks at the system level, if necessary */
15622 if( rc==SQLITE_OK ){
15623 if( (allShared & mask)==0 ){
15624 rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n);
15625 }else{
15626 rc = SQLITE_OK;
15627 }
15628 }
15629
15630 /* Get the local shared locks */
15631 if( rc==SQLITE_OK ){
15632 p->sharedMask |= mask;
15633 }
15634 }else{
15635 /* Make sure no sibling connections hold locks that will block this
15636 ** lock. If any do, return SQLITE_BUSY right away.
15637 */
15638 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
15639 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
15640 rc = SQLITE_BUSY;
15641 break;
15642 }
15643 }
15644
15645 /* Get the exclusive locks at the system level. Then if successful
15646 ** also mark the local connection as being locked.
15647 */
15648 if( rc==SQLITE_OK ){
15649 rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n);
15650 if( rc==SQLITE_OK ){
15651 assert( (p->sharedMask & mask)==0 );
15652 p->exclMask |= mask;
15653 }
15654 }
15655 }
15656 sqlite3_mutex_leave(pShmNode->mutex);
15657 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
15658 p->id, osGetpid(0), p->sharedMask, p->exclMask));
15659 return rc;
15660 }
15661
15662 /*
15663 ** Implement a memory barrier or memory fence on shared memory.
15664 **
15665 ** All loads and stores begun before the barrier must complete before
15666 ** any load or store begun after the barrier.
15667 */
15668 static void unixShmBarrier(
15669 sqlite3_file *fd /* Database file holding the shared memory */
15670 ){
15671 UNUSED_PARAMETER(fd);
15672 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
15673 unixEnterMutex(); /* Also mutex, for redundancy */
15674 unixLeaveMutex();
15675 }
15676
15677 /*
15678 ** Close a connection to shared-memory. Delete the underlying
15679 ** storage if deleteFlag is true.
15680 **
15681 ** If there is no shared memory associated with the connection then this
15682 ** routine is a harmless no-op.
15683 */
15684 static int unixShmUnmap(
15685 sqlite3_file *fd, /* The underlying database file */
15686 int deleteFlag /* Delete shared-memory if true */
15687 ){
15688 unixShm *p; /* The connection to be closed */
15689 unixShmNode *pShmNode; /* The underlying shared-memory file */
15690 unixShm **pp; /* For looping over sibling connections */
15691 unixFile *pDbFd; /* The underlying database file */
15692
15693 pDbFd = (unixFile*)fd;
15694 p = pDbFd->pShm;
15695 if( p==0 ) return SQLITE_OK;
15696 pShmNode = p->pShmNode;
15697
15698 assert( pShmNode==pDbFd->pInode->pShmNode );
15699 assert( pShmNode->pInode==pDbFd->pInode );
15700
15701 /* Remove connection p from the set of connections associated
15702 ** with pShmNode */
15703 sqlite3_mutex_enter(pShmNode->mutex);
15704 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
15705 *pp = p->pNext;
15706
15707 /* Free the connection p */
15708 sqlite3_free(p);
15709 pDbFd->pShm = 0;
15710 sqlite3_mutex_leave(pShmNode->mutex);
15711
15712 /* If pShmNode->nRef has reached 0, then close the underlying
15713 ** shared-memory file, too */
15714 unixEnterMutex();
15715 assert( pShmNode->nRef>0 );
15716 pShmNode->nRef--;
15717 if( pShmNode->nRef==0 ){
15718 if( deleteFlag && pShmNode->h>=0 ){
15719 osUnlink(pShmNode->zFilename);
15720 }
15721 unixShmPurge(pDbFd);
15722 }
15723 unixLeaveMutex();
15724
15725 return SQLITE_OK;
15726 }
15727
15728
15729 #else
15730 # define unixShmMap 0
15731 # define unixShmLock 0
15732 # define unixShmBarrier 0
15733 # define unixShmUnmap 0
15734 #endif /* #ifndef SQLITE_OMIT_WAL */
15735
15736 #if SQLITE_MAX_MMAP_SIZE>0
15737 /*
15738 ** If it is currently memory mapped, unmap file pFd.
15739 */
15740 static void unixUnmapfile(unixFile *pFd){
15741 assert( pFd->nFetchOut==0 );
15742 if( pFd->pMapRegion ){
15743 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
15744 pFd->pMapRegion = 0;
15745 pFd->mmapSize = 0;
15746 pFd->mmapSizeActual = 0;
15747 }
15748 }
15749
15750 /*
15751 ** Attempt to set the size of the memory mapping maintained by file
15752 ** descriptor pFd to nNew bytes. Any existing mapping is discarded.
15753 **
15754 ** If successful, this function sets the following variables:
15755 **
15756 ** unixFile.pMapRegion
15757 ** unixFile.mmapSize
15758 ** unixFile.mmapSizeActual
15759 **
15760 ** If unsuccessful, an error message is logged via sqlite3_log() and
15761 ** the three variables above are zeroed. In this case SQLite should
15762 ** continue accessing the database using the xRead() and xWrite()
15763 ** methods.
15764 */
15765 static void unixRemapfile(
15766 unixFile *pFd, /* File descriptor object */
15767 i64 nNew /* Required mapping size */
15768 ){
15769 const char *zErr = "mmap";
15770 int h = pFd->h; /* File descriptor open on db file */
15771 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
15772 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
15773 u8 *pNew = 0; /* Location of new mapping */
15774 int flags = PROT_READ; /* Flags to pass to mmap() */
15775
15776 assert( pFd->nFetchOut==0 );
15777 assert( nNew>pFd->mmapSize );
15778 assert( nNew<=pFd->mmapSizeMax );
15779 assert( nNew>0 );
15780 assert( pFd->mmapSizeActual>=pFd->mmapSize );
15781 assert( MAP_FAILED!=0 );
15782
15783 #ifdef SQLITE_MMAP_READWRITE
15784 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
15785 #endif
15786
15787 if( pOrig ){
15788 #if HAVE_MREMAP
15789 i64 nReuse = pFd->mmapSize;
15790 #else
15791 const int szSyspage = osGetpagesize();
15792 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
15793 #endif
15794 u8 *pReq = &pOrig[nReuse];
15795
15796 /* Unmap any pages of the existing mapping that cannot be reused. */
15797 if( nReuse!=nOrig ){
15798 osMunmap(pReq, nOrig-nReuse);
15799 }
15800
15801 #if HAVE_MREMAP
15802 pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE);
15803 zErr = "mremap";
15804 #else
15805 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
15806 if( pNew!=MAP_FAILED ){
15807 if( pNew!=pReq ){
15808 osMunmap(pNew, nNew - nReuse);
15809 pNew = 0;
15810 }else{
15811 pNew = pOrig;
15812 }
15813 }
15814 #endif
15815
15816 /* The attempt to extend the existing mapping failed. Free it. */
15817 if( pNew==MAP_FAILED || pNew==0 ){
15818 osMunmap(pOrig, nReuse);
15819 }
15820 }
15821
15822 /* If pNew is still NULL, try to create an entirely new mapping. */
15823 if( pNew==0 ){
15824 pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0);
15825 }
15826
15827 if( pNew==MAP_FAILED ){
15828 pNew = 0;
15829 nNew = 0;
15830 unixLogError(SQLITE_OK, zErr, pFd->zPath);
15831
15832 /* If the mmap() above failed, assume that all subsequent mmap() calls
15833 ** will probably fail too. Fall back to using xRead/xWrite exclusively
15834 ** in this case. */
15835 pFd->mmapSizeMax = 0;
15836 }
15837 pFd->pMapRegion = (void *)pNew;
15838 pFd->mmapSize = pFd->mmapSizeActual = nNew;
15839 }
15840
15841 /*
15842 ** Memory map or remap the file opened by file-descriptor pFd (if the file
15843 ** is already mapped, the existing mapping is replaced by the new). Or, if
15844 ** there already exists a mapping for this file, and there are still
15845 ** outstanding xFetch() references to it, this function is a no-op.
15846 **
15847 ** If parameter nByte is non-negative, then it is the requested size of
15848 ** the mapping to create. Otherwise, if nByte is less than zero, then the
15849 ** requested size is the size of the file on disk. The actual size of the
15850 ** created mapping is either the requested size or the value configured
15851 ** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
15852 **
15853 ** SQLITE_OK is returned if no error occurs (even if the mapping is not
15854 ** recreated as a result of outstanding references) or an SQLite error
15855 ** code otherwise.
15856 */
15857 static int unixMapfile(unixFile *pFd, i64 nMap){
15858 assert( nMap>=0 || pFd->nFetchOut==0 );
15859 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
15860 if( pFd->nFetchOut>0 ) return SQLITE_OK;
15861
15862 if( nMap<0 ){
15863 struct stat statbuf; /* Low-level file information */
15864 if( osFstat(pFd->h, &statbuf) ){
15865 return SQLITE_IOERR_FSTAT;
15866 }
15867 nMap = statbuf.st_size;
15868 }
15869 if( nMap>pFd->mmapSizeMax ){
15870 nMap = pFd->mmapSizeMax;
15871 }
15872
15873 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
15874 if( nMap!=pFd->mmapSize ){
15875 unixRemapfile(pFd, nMap);
15876 }
15877
15878 return SQLITE_OK;
15879 }
15880 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
15881
15882 /*
15883 ** If possible, return a pointer to a mapping of file fd starting at offset
15884 ** iOff. The mapping must be valid for at least nAmt bytes.
15885 **
15886 ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
15887 ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
15888 ** Finally, if an error does occur, return an SQLite error code. The final
15889 ** value of *pp is undefined in this case.
15890 **
15891 ** If this function does return a pointer, the caller must eventually
15892 ** release the reference by calling unixUnfetch().
15893 */
15894 static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
15895 #if SQLITE_MAX_MMAP_SIZE>0
15896 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
15897 #endif
15898 *pp = 0;
15899
15900 #if SQLITE_MAX_MMAP_SIZE>0
15901 if( pFd->mmapSizeMax>0 ){
15902 if( pFd->pMapRegion==0 ){
15903 int rc = unixMapfile(pFd, -1);
15904 if( rc!=SQLITE_OK ) return rc;
15905 }
15906 if( pFd->mmapSize >= iOff+nAmt ){
15907 *pp = &((u8 *)pFd->pMapRegion)[iOff];
15908 pFd->nFetchOut++;
15909 }
15910 }
15911 #endif
15912 return SQLITE_OK;
15913 }
15914
15915 /*
15916 ** If the third argument is non-NULL, then this function releases a
15917 ** reference obtained by an earlier call to unixFetch(). The second
15918 ** argument passed to this function must be the same as the corresponding
15919 ** argument that was passed to the unixFetch() invocation.
15920 **
15921 ** Or, if the third argument is NULL, then this function is being called
15922 ** to inform the VFS layer that, according to POSIX, any existing mapping
15923 ** may now be invalid and should be unmapped.
15924 */
15925 static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
15926 #if SQLITE_MAX_MMAP_SIZE>0
15927 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
15928 UNUSED_PARAMETER(iOff);
15929
15930 /* If p==0 (unmap the entire file) then there must be no outstanding
15931 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
15932 ** then there must be at least one outstanding. */
15933 assert( (p==0)==(pFd->nFetchOut==0) );
15934
15935 /* If p!=0, it must match the iOff value. */
15936 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
15937
15938 if( p ){
15939 pFd->nFetchOut--;
15940 }else{
15941 unixUnmapfile(pFd);
15942 }
15943
15944 assert( pFd->nFetchOut>=0 );
15945 #else
15946 UNUSED_PARAMETER(fd);
15947 UNUSED_PARAMETER(p);
15948 UNUSED_PARAMETER(iOff);
15949 #endif
15950 return SQLITE_OK;
15951 }
15952
15953 /*
15954 ** Here ends the implementation of all sqlite3_file methods.
15955 **
15956 ********************** End sqlite3_file Methods *******************************
15957 ******************************************************************************/
15958
15959 /*
15960 ** This division contains definitions of sqlite3_io_methods objects that
15961 ** implement various file locking strategies. It also contains definitions
15962 ** of "finder" functions. A finder-function is used to locate the appropriate
15963 ** sqlite3_io_methods object for a particular database file. The pAppData
15964 ** field of the sqlite3_vfs VFS objects are initialized to be pointers to
15965 ** the correct finder-function for that VFS.
15966 **
15967 ** Most finder functions return a pointer to a fixed sqlite3_io_methods
15968 ** object. The only interesting finder-function is autolockIoFinder, which
15969 ** looks at the filesystem type and tries to guess the best locking
15970 ** strategy from that.
15971 **
15972 ** For finder-function F, two objects are created:
15973 **
15974 ** (1) The real finder-function named "FImpt()".
15975 **
15976 ** (2) A constant pointer to this function named just "F".
15977 **
15978 **
15979 ** A pointer to the F pointer is used as the pAppData value for VFS
15980 ** objects. We have to do this instead of letting pAppData point
15981 ** directly at the finder-function since C90 rules prevent a void*
15982 ** from be cast into a function pointer.
15983 **
15984 **
15985 ** Each instance of this macro generates two objects:
15986 **
15987 ** * A constant sqlite3_io_methods object call METHOD that has locking
15988 ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
15989 **
15990 ** * An I/O method finder function called FINDER that returns a pointer
15991 ** to the METHOD object in the previous bullet.
15992 */
15993 #define IOMETHODS(FINDER,METHOD,VERSION,CLOSE,LOCK,UNLOCK,CKLOCK,SHMMAP) \
15994 static const sqlite3_io_methods METHOD = { \
15995 VERSION, /* iVersion */ \
15996 CLOSE, /* xClose */ \
15997 unixRead, /* xRead */ \
15998 unixWrite, /* xWrite */ \
15999 unixTruncate, /* xTruncate */ \
16000 unixSync, /* xSync */ \
16001 unixFileSize, /* xFileSize */ \
16002 LOCK, /* xLock */ \
16003 UNLOCK, /* xUnlock */ \
16004 CKLOCK, /* xCheckReservedLock */ \
16005 unixFileControl, /* xFileControl */ \
16006 unixSectorSize, /* xSectorSize */ \
16007 unixDeviceCharacteristics, /* xDeviceCapabilities */ \
16008 SHMMAP, /* xShmMap */ \
16009 unixShmLock, /* xShmLock */ \
16010 unixShmBarrier, /* xShmBarrier */ \
16011 unixShmUnmap, /* xShmUnmap */ \
16012 unixFetch, /* xFetch */ \
16013 unixUnfetch, /* xUnfetch */ \
16014 }; \
16015 static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \
16016 UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \
16017 return &METHOD; \
16018 } \
16019 static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \
16020 = FINDER##Impl;
16021
16022 /*
16023 ** Here are all of the sqlite3_io_methods objects for each of the
16024 ** locking strategies. Functions that return pointers to these methods
16025 ** are also created.
16026 */
16027 IOMETHODS(
16028 posixIoFinder, /* Finder function name */
16029 posixIoMethods, /* sqlite3_io_methods object name */
16030 3, /* shared memory and mmap are enabled */
16031 unixClose, /* xClose method */
16032 unixLock, /* xLock method */
16033 unixUnlock, /* xUnlock method */
16034 unixCheckReservedLock, /* xCheckReservedLock method */
16035 unixShmMap /* xShmMap method */
16036 )
16037 IOMETHODS(
16038 nolockIoFinder, /* Finder function name */
16039 nolockIoMethods, /* sqlite3_io_methods object name */
16040 3, /* shared memory is disabled */
16041 nolockClose, /* xClose method */
16042 nolockLock, /* xLock method */
16043 nolockUnlock, /* xUnlock method */
16044 nolockCheckReservedLock, /* xCheckReservedLock method */
16045 0 /* xShmMap method */
16046 )
16047 IOMETHODS(
16048 dotlockIoFinder, /* Finder function name */
16049 dotlockIoMethods, /* sqlite3_io_methods object name */
16050 1, /* shared memory is disabled */
16051 dotlockClose, /* xClose method */
16052 dotlockLock, /* xLock method */
16053 dotlockUnlock, /* xUnlock method */
16054 dotlockCheckReservedLock, /* xCheckReservedLock method */
16055 0 /* xShmMap method */
16056 )
16057
16058 #if SQLITE_ENABLE_LOCKING_STYLE
16059 IOMETHODS(
16060 flockIoFinder, /* Finder function name */
16061 flockIoMethods, /* sqlite3_io_methods object name */
16062 1, /* shared memory is disabled */
16063 flockClose, /* xClose method */
16064 flockLock, /* xLock method */
16065 flockUnlock, /* xUnlock method */
16066 flockCheckReservedLock, /* xCheckReservedLock method */
16067 0 /* xShmMap method */
16068 )
16069 #endif
16070
16071 #if OS_VXWORKS
16072 IOMETHODS(
16073 semIoFinder, /* Finder function name */
16074 semIoMethods, /* sqlite3_io_methods object name */
16075 1, /* shared memory is disabled */
16076 semXClose, /* xClose method */
16077 semXLock, /* xLock method */
16078 semXUnlock, /* xUnlock method */
16079 semXCheckReservedLock, /* xCheckReservedLock method */
16080 0 /* xShmMap method */
16081 )
16082 #endif
16083
16084 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
16085 IOMETHODS(
16086 afpIoFinder, /* Finder function name */
16087 afpIoMethods, /* sqlite3_io_methods object name */
16088 1, /* shared memory is disabled */
16089 afpClose, /* xClose method */
16090 afpLock, /* xLock method */
16091 afpUnlock, /* xUnlock method */
16092 afpCheckReservedLock, /* xCheckReservedLock method */
16093 0 /* xShmMap method */
16094 )
16095 #endif
16096
16097 /*
16098 ** The proxy locking method is a "super-method" in the sense that it
16099 ** opens secondary file descriptors for the conch and lock files and
16100 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
16101 ** secondary files. For this reason, the division that implements
16102 ** proxy locking is located much further down in the file. But we need
16103 ** to go ahead and define the sqlite3_io_methods and finder function
16104 ** for proxy locking here. So we forward declare the I/O methods.
16105 */
16106 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
16107 static int proxyClose(sqlite3_file*);
16108 static int proxyLock(sqlite3_file*, int);
16109 static int proxyUnlock(sqlite3_file*, int);
16110 static int proxyCheckReservedLock(sqlite3_file*, int*);
16111 IOMETHODS(
16112 proxyIoFinder, /* Finder function name */
16113 proxyIoMethods, /* sqlite3_io_methods object name */
16114 1, /* shared memory is disabled */
16115 proxyClose, /* xClose method */
16116 proxyLock, /* xLock method */
16117 proxyUnlock, /* xUnlock method */
16118 proxyCheckReservedLock, /* xCheckReservedLock method */
16119 0 /* xShmMap method */
16120 )
16121 #endif
16122
16123 /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
16124 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
16125 IOMETHODS(
16126 nfsIoFinder, /* Finder function name */
16127 nfsIoMethods, /* sqlite3_io_methods object name */
16128 1, /* shared memory is disabled */
16129 unixClose, /* xClose method */
16130 unixLock, /* xLock method */
16131 nfsUnlock, /* xUnlock method */
16132 unixCheckReservedLock, /* xCheckReservedLock method */
16133 0 /* xShmMap method */
16134 )
16135 #endif
16136
16137 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
16138 /*
16139 ** This "finder" function attempts to determine the best locking strategy
16140 ** for the database file "filePath". It then returns the sqlite3_io_methods
16141 ** object that implements that strategy.
16142 **
16143 ** This is for MacOSX only.
16144 */
16145 static const sqlite3_io_methods *autolockIoFinderImpl(
16146 const char *filePath, /* name of the database file */
16147 unixFile *pNew /* open file object for the database file */
16148 ){
16149 static const struct Mapping {
16150 const char *zFilesystem; /* Filesystem type name */
16151 const sqlite3_io_methods *pMethods; /* Appropriate locking method */
16152 } aMap[] = {
16153 { "hfs", &posixIoMethods },
16154 { "ufs", &posixIoMethods },
16155 { "afpfs", &afpIoMethods },
16156 { "smbfs", &afpIoMethods },
16157 { "webdav", &nolockIoMethods },
16158 { 0, 0 }
16159 };
16160 int i;
16161 struct statfs fsInfo;
16162 struct flock lockInfo;
16163
16164 if( !filePath ){
16165 /* If filePath==NULL that means we are dealing with a transient file
16166 ** that does not need to be locked. */
16167 return &nolockIoMethods;
16168 }
16169 if( statfs(filePath, &fsInfo) != -1 ){
16170 if( fsInfo.f_flags & MNT_RDONLY ){
16171 return &nolockIoMethods;
16172 }
16173 for(i=0; aMap[i].zFilesystem; i++){
16174 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
16175 return aMap[i].pMethods;
16176 }
16177 }
16178 }
16179
16180 /* Default case. Handles, amongst others, "nfs".
16181 ** Test byte-range lock using fcntl(). If the call succeeds,
16182 ** assume that the file-system supports POSIX style locks.
16183 */
16184 lockInfo.l_len = 1;
16185 lockInfo.l_start = 0;
16186 lockInfo.l_whence = SEEK_SET;
16187 lockInfo.l_type = F_RDLCK;
16188 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
16189 if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
16190 return &nfsIoMethods;
16191 } else {
16192 return &posixIoMethods;
16193 }
16194 }else{
16195 return &dotlockIoMethods;
16196 }
16197 }
16198 static const sqlite3_io_methods
16199 *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
16200
16201 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
16202
16203 #if OS_VXWORKS
16204 /*
16205 ** This "finder" function for VxWorks checks to see if posix advisory
16206 ** locking works. If it does, then that is what is used. If it does not
16207 ** work, then fallback to named semaphore locking.
16208 */
16209 static const sqlite3_io_methods *vxworksIoFinderImpl(
16210 const char *filePath, /* name of the database file */
16211 unixFile *pNew /* the open file object */
16212 ){
16213 struct flock lockInfo;
16214
16215 if( !filePath ){
16216 /* If filePath==NULL that means we are dealing with a transient file
16217 ** that does not need to be locked. */
16218 return &nolockIoMethods;
16219 }
16220
16221 /* Test if fcntl() is supported and use POSIX style locks.
16222 ** Otherwise fall back to the named semaphore method.
16223 */
16224 lockInfo.l_len = 1;
16225 lockInfo.l_start = 0;
16226 lockInfo.l_whence = SEEK_SET;
16227 lockInfo.l_type = F_RDLCK;
16228 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
16229 return &posixIoMethods;
16230 }else{
16231 return &semIoMethods;
16232 }
16233 }
16234 static const sqlite3_io_methods
16235 *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl;
16236
16237 #endif /* OS_VXWORKS */
16238
16239 /*
16240 ** An abstract type for a pointer to an IO method finder function:
16241 */
16242 typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
16243
16244
16245 /****************************************************************************
16246 **************************** sqlite3_vfs methods ****************************
16247 **
16248 ** This division contains the implementation of methods on the
16249 ** sqlite3_vfs object.
16250 */
16251
16252 /*
16253 ** Initialize the contents of the unixFile structure pointed to by pId.
16254 */
16255 static int fillInUnixFile(
16256 sqlite3_vfs *pVfs, /* Pointer to vfs object */
16257 int h, /* Open file descriptor of file being opened */
16258 sqlite3_file *pId, /* Write to the unixFile structure here */
16259 const char *zFilename, /* Name of the file being opened */
16260 int ctrlFlags /* Zero or more UNIXFILE_* values */
16261 ){
16262 const sqlite3_io_methods *pLockingStyle;
16263 unixFile *pNew = (unixFile *)pId;
16264 int rc = SQLITE_OK;
16265
16266 assert( pNew->pInode==NULL );
16267
16268 /* Usually the path zFilename should not be a relative pathname. The
16269 ** exception is when opening the proxy "conch" file in builds that
16270 ** include the special Apple locking styles.
16271 */
16272 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
16273 assert( zFilename==0 || zFilename[0]=='/'
16274 || pVfs->pAppData==(void*)&autolockIoFinder );
16275 #else
16276 assert( zFilename==0 || zFilename[0]=='/' );
16277 #endif
16278
16279 /* No locking occurs in temporary files */
16280 assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
16281
16282 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
16283 pNew->h = h;
16284 pNew->pVfs = pVfs;
16285 pNew->zPath = zFilename;
16286 pNew->ctrlFlags = (u8)ctrlFlags;
16287 #if SQLITE_MAX_MMAP_SIZE>0
16288 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
16289 #endif
16290 if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
16291 "psow", SQLITE_POWERSAFE_OVERWRITE) ){
16292 pNew->ctrlFlags |= UNIXFILE_PSOW;
16293 }
16294 if( strcmp(pVfs->zName,"unix-excl")==0 ){
16295 pNew->ctrlFlags |= UNIXFILE_EXCL;
16296 }
16297
16298 #if OS_VXWORKS
16299 pNew->pId = vxworksFindFileId(zFilename);
16300 if( pNew->pId==0 ){
16301 ctrlFlags |= UNIXFILE_NOLOCK;
16302 rc = SQLITE_NOMEM_BKPT;
16303 }
16304 #endif
16305
16306 if( ctrlFlags & UNIXFILE_NOLOCK ){
16307 pLockingStyle = &nolockIoMethods;
16308 }else{
16309 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
16310 #if SQLITE_ENABLE_LOCKING_STYLE
16311 /* Cache zFilename in the locking context (AFP and dotlock override) for
16312 ** proxyLock activation is possible (remote proxy is based on db name)
16313 ** zFilename remains valid until file is closed, to support */
16314 pNew->lockingContext = (void*)zFilename;
16315 #endif
16316 }
16317
16318 if( pLockingStyle == &posixIoMethods
16319 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
16320 || pLockingStyle == &nfsIoMethods
16321 #endif
16322 ){
16323 unixEnterMutex();
16324 rc = findInodeInfo(pNew, &pNew->pInode);
16325 if( rc!=SQLITE_OK ){
16326 /* If an error occurred in findInodeInfo(), close the file descriptor
16327 ** immediately, before releasing the mutex. findInodeInfo() may fail
16328 ** in two scenarios:
16329 **
16330 ** (a) A call to fstat() failed.
16331 ** (b) A malloc failed.
16332 **
16333 ** Scenario (b) may only occur if the process is holding no other
16334 ** file descriptors open on the same file. If there were other file
16335 ** descriptors on this file, then no malloc would be required by
16336 ** findInodeInfo(). If this is the case, it is quite safe to close
16337 ** handle h - as it is guaranteed that no posix locks will be released
16338 ** by doing so.
16339 **
16340 ** If scenario (a) caused the error then things are not so safe. The
16341 ** implicit assumption here is that if fstat() fails, things are in
16342 ** such bad shape that dropping a lock or two doesn't matter much.
16343 */
16344 robust_close(pNew, h, __LINE__);
16345 h = -1;
16346 }
16347 unixLeaveMutex();
16348 }
16349
16350 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
16351 else if( pLockingStyle == &afpIoMethods ){
16352 /* AFP locking uses the file path so it needs to be included in
16353 ** the afpLockingContext.
16354 */
16355 afpLockingContext *pCtx;
16356 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
16357 if( pCtx==0 ){
16358 rc = SQLITE_NOMEM_BKPT;
16359 }else{
16360 /* NB: zFilename exists and remains valid until the file is closed
16361 ** according to requirement F11141. So we do not need to make a
16362 ** copy of the filename. */
16363 pCtx->dbPath = zFilename;
16364 pCtx->reserved = 0;
16365 srandomdev();
16366 unixEnterMutex();
16367 rc = findInodeInfo(pNew, &pNew->pInode);
16368 if( rc!=SQLITE_OK ){
16369 sqlite3_free(pNew->lockingContext);
16370 robust_close(pNew, h, __LINE__);
16371 h = -1;
16372 }
16373 unixLeaveMutex();
16374 }
16375 }
16376 #endif
16377
16378 else if( pLockingStyle == &dotlockIoMethods ){
16379 /* Dotfile locking uses the file path so it needs to be included in
16380 ** the dotlockLockingContext
16381 */
16382 char *zLockFile;
16383 int nFilename;
16384 assert( zFilename!=0 );
16385 nFilename = (int)strlen(zFilename) + 6;
16386 zLockFile = (char *)sqlite3_malloc64(nFilename);
16387 if( zLockFile==0 ){
16388 rc = SQLITE_NOMEM_BKPT;
16389 }else{
16390 sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
16391 }
16392 pNew->lockingContext = zLockFile;
16393 }
16394
16395 #if OS_VXWORKS
16396 else if( pLockingStyle == &semIoMethods ){
16397 /* Named semaphore locking uses the file path so it needs to be
16398 ** included in the semLockingContext
16399 */
16400 unixEnterMutex();
16401 rc = findInodeInfo(pNew, &pNew->pInode);
16402 if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
16403 char *zSemName = pNew->pInode->aSemName;
16404 int n;
16405 sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
16406 pNew->pId->zCanonicalName);
16407 for( n=1; zSemName[n]; n++ )
16408 if( zSemName[n]=='/' ) zSemName[n] = '_';
16409 pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
16410 if( pNew->pInode->pSem == SEM_FAILED ){
16411 rc = SQLITE_NOMEM_BKPT;
16412 pNew->pInode->aSemName[0] = '\0';
16413 }
16414 }
16415 unixLeaveMutex();
16416 }
16417 #endif
16418
16419 storeLastErrno(pNew, 0);
16420 #if OS_VXWORKS
16421 if( rc!=SQLITE_OK ){
16422 if( h>=0 ) robust_close(pNew, h, __LINE__);
16423 h = -1;
16424 osUnlink(zFilename);
16425 pNew->ctrlFlags |= UNIXFILE_DELETE;
16426 }
16427 #endif
16428 if( rc!=SQLITE_OK ){
16429 if( h>=0 ) robust_close(pNew, h, __LINE__);
16430 }else{
16431 pNew->pMethod = pLockingStyle;
16432 OpenCounter(+1);
16433 verifyDbFile(pNew);
16434 }
16435 return rc;
16436 }
16437
16438 /*
16439 ** Return the name of a directory in which to put temporary files.
16440 ** If no suitable temporary file directory can be found, return NULL.
16441 */
16442 static const char *unixTempFileDir(void){
16443 static const char *azDirs[] = {
16444 0,
16445 0,
16446 "/var/tmp",
16447 "/usr/tmp",
16448 "/tmp",
16449 "."
16450 };
16451 unsigned int i = 0;
16452 struct stat buf;
16453 const char *zDir = sqlite3_temp_directory;
16454
16455 if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
16456 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
16457 while(1){
16458 if( zDir!=0
16459 && osStat(zDir, &buf)==0
16460 && S_ISDIR(buf.st_mode)
16461 && osAccess(zDir, 03)==0
16462 ){
16463 return zDir;
16464 }
16465 if( i>=sizeof(azDirs)/sizeof(azDirs[0]) ) break;
16466 zDir = azDirs[i++];
16467 }
16468 return 0;
16469 }
16470
16471 /*
16472 ** Create a temporary file name in zBuf. zBuf must be allocated
16473 ** by the calling process and must be big enough to hold at least
16474 ** pVfs->mxPathname bytes.
16475 */
16476 static int unixGetTempname(int nBuf, char *zBuf){
16477 const char *zDir;
16478 int iLimit = 0;
16479
16480 /* It's odd to simulate an io-error here, but really this is just
16481 ** using the io-error infrastructure to test that SQLite handles this
16482 ** function failing.
16483 */
16484 zBuf[0] = 0;
16485 SimulateIOError( return SQLITE_IOERR );
16486
16487 zDir = unixTempFileDir();
16488 if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH;
16489 do{
16490 u64 r;
16491 sqlite3_randomness(sizeof(r), &r);
16492 assert( nBuf>2 );
16493 zBuf[nBuf-2] = 0;
16494 sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c",
16495 zDir, r, 0);
16496 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR;
16497 }while( osAccess(zBuf,0)==0 );
16498 return SQLITE_OK;
16499 }
16500
16501 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
16502 /*
16503 ** Routine to transform a unixFile into a proxy-locking unixFile.
16504 ** Implementation in the proxy-lock division, but used by unixOpen()
16505 ** if SQLITE_PREFER_PROXY_LOCKING is defined.
16506 */
16507 static int proxyTransformUnixFile(unixFile*, const char*);
16508 #endif
16509
16510 /*
16511 ** Search for an unused file descriptor that was opened on the database
16512 ** file (not a journal or master-journal file) identified by pathname
16513 ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
16514 ** argument to this function.
16515 **
16516 ** Such a file descriptor may exist if a database connection was closed
16517 ** but the associated file descriptor could not be closed because some
16518 ** other file descriptor open on the same file is holding a file-lock.
16519 ** Refer to comments in the unixClose() function and the lengthy comment
16520 ** describing "Posix Advisory Locking" at the start of this file for
16521 ** further details. Also, ticket #4018.
16522 **
16523 ** If a suitable file descriptor is found, then it is returned. If no
16524 ** such file descriptor is located, -1 is returned.
16525 */
16526 static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
16527 UnixUnusedFd *pUnused = 0;
16528
16529 /* Do not search for an unused file descriptor on vxworks. Not because
16530 ** vxworks would not benefit from the change (it might, we're not sure),
16531 ** but because no way to test it is currently available. It is better
16532 ** not to risk breaking vxworks support for the sake of such an obscure
16533 ** feature. */
16534 #if !OS_VXWORKS
16535 struct stat sStat; /* Results of stat() call */
16536
16537 /* A stat() call may fail for various reasons. If this happens, it is
16538 ** almost certain that an open() call on the same path will also fail.
16539 ** For this reason, if an error occurs in the stat() call here, it is
16540 ** ignored and -1 is returned. The caller will try to open a new file
16541 ** descriptor on the same path, fail, and return an error to SQLite.
16542 **
16543 ** Even if a subsequent open() call does succeed, the consequences of
16544 ** not searching for a reusable file descriptor are not dire. */
16545 if( 0==osStat(zPath, &sStat) ){
16546 unixInodeInfo *pInode;
16547
16548 unixEnterMutex();
16549 pInode = inodeList;
16550 while( pInode && (pInode->fileId.dev!=sStat.st_dev
16551 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
16552 pInode = pInode->pNext;
16553 }
16554 if( pInode ){
16555 UnixUnusedFd **pp;
16556 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
16557 pUnused = *pp;
16558 if( pUnused ){
16559 *pp = pUnused->pNext;
16560 }
16561 }
16562 unixLeaveMutex();
16563 }
16564 #endif /* if !OS_VXWORKS */
16565 return pUnused;
16566 }
16567
16568 /*
16569 ** Find the mode, uid and gid of file zFile.
16570 */
16571 static int getFileMode(
16572 const char *zFile, /* File name */
16573 mode_t *pMode, /* OUT: Permissions of zFile */
16574 uid_t *pUid, /* OUT: uid of zFile. */
16575 gid_t *pGid /* OUT: gid of zFile. */
16576 ){
16577 struct stat sStat; /* Output of stat() on database file */
16578 int rc = SQLITE_OK;
16579 if( 0==osStat(zFile, &sStat) ){
16580 *pMode = sStat.st_mode & 0777;
16581 *pUid = sStat.st_uid;
16582 *pGid = sStat.st_gid;
16583 }else{
16584 rc = SQLITE_IOERR_FSTAT;
16585 }
16586 return rc;
16587 }
16588
16589 /*
16590 ** This function is called by unixOpen() to determine the unix permissions
16591 ** to create new files with. If no error occurs, then SQLITE_OK is returned
16592 ** and a value suitable for passing as the third argument to open(2) is
16593 ** written to *pMode. If an IO error occurs, an SQLite error code is
16594 ** returned and the value of *pMode is not modified.
16595 **
16596 ** In most cases, this routine sets *pMode to 0, which will become
16597 ** an indication to robust_open() to create the file using
16598 ** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask.
16599 ** But if the file being opened is a WAL or regular journal file, then
16600 ** this function queries the file-system for the permissions on the
16601 ** corresponding database file and sets *pMode to this value. Whenever
16602 ** possible, WAL and journal files are created using the same permissions
16603 ** as the associated database file.
16604 **
16605 ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
16606 ** original filename is unavailable. But 8_3_NAMES is only used for
16607 ** FAT filesystems and permissions do not matter there, so just use
16608 ** the default permissions.
16609 */
16610 static int findCreateFileMode(
16611 const char *zPath, /* Path of file (possibly) being created */
16612 int flags, /* Flags passed as 4th argument to xOpen() */
16613 mode_t *pMode, /* OUT: Permissions to open file with */
16614 uid_t *pUid, /* OUT: uid to set on the file */
16615 gid_t *pGid /* OUT: gid to set on the file */
16616 ){
16617 int rc = SQLITE_OK; /* Return Code */
16618 *pMode = 0;
16619 *pUid = 0;
16620 *pGid = 0;
16621 if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
16622 char zDb[MAX_PATHNAME+1]; /* Database file path */
16623 int nDb; /* Number of valid bytes in zDb */
16624
16625 /* zPath is a path to a WAL or journal file. The following block derives
16626 ** the path to the associated database file from zPath. This block handles
16627 ** the following naming conventions:
16628 **
16629 ** "<path to db>-journal"
16630 ** "<path to db>-wal"
16631 ** "<path to db>-journalNN"
16632 ** "<path to db>-walNN"
16633 **
16634 ** where NN is a decimal number. The NN naming schemes are
16635 ** used by the test_multiplex.c module.
16636 */
16637 nDb = sqlite3Strlen30(zPath) - 1;
16638 while( zPath[nDb]!='-' ){
16639 #ifndef SQLITE_ENABLE_8_3_NAMES
16640 /* In the normal case (8+3 filenames disabled) the journal filename
16641 ** is guaranteed to contain a '-' character. */
16642 assert( nDb>0 );
16643 assert( sqlite3Isalnum(zPath[nDb]) );
16644 #else
16645 /* If 8+3 names are possible, then the journal file might not contain
16646 ** a '-' character. So check for that case and return early. */
16647 if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK;
16648 #endif
16649 nDb--;
16650 }
16651 memcpy(zDb, zPath, nDb);
16652 zDb[nDb] = '\0';
16653
16654 rc = getFileMode(zDb, pMode, pUid, pGid);
16655 }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
16656 *pMode = 0600;
16657 }else if( flags & SQLITE_OPEN_URI ){
16658 /* If this is a main database file and the file was opened using a URI
16659 ** filename, check for the "modeof" parameter. If present, interpret
16660 ** its value as a filename and try to copy the mode, uid and gid from
16661 ** that file. */
16662 const char *z = sqlite3_uri_parameter(zPath, "modeof");
16663 if( z ){
16664 rc = getFileMode(z, pMode, pUid, pGid);
16665 }
16666 }
16667 return rc;
16668 }
16669
16670 /*
16671 ** Initialize |unixFile| internals of |file| on behalf of chromiumOpen() in
16672 ** WebDatabase SQLiteFileSystemPosix.cpp. Function is a subset of unixOpen(),
16673 ** each duplicated piece is marked by "Duplicated in" comment in unixOpen().
16674 */
16675 CHROMIUM_SQLITE_API
16676 int chromium_sqlite3_fill_in_unix_sqlite3_file(sqlite3_vfs* pVfs,
16677 int fd,
16678 sqlite3_file* pFile,
16679 const char* zPath,
16680 int noLock,
16681 int flags) {
16682 unixFile *p = (unixFile *)pFile;
16683 const int eType = flags&0xFFFFFF00; /* Type of file to open */
16684 const int ctrlFlags = (noLock ? UNIXFILE_NOLOCK : 0);
16685 int rc;
16686
16687 memset(p, 0, sizeof(unixFile));
16688
16689 /* osStat() will not work in the sandbox, so findReusableFd() will always
16690 ** fail, so directly include the failure-case setup then initialize pUnused.
16691 */
16692 if( eType==SQLITE_OPEN_MAIN_DB ){
16693 p->pUnused = sqlite3_malloc(sizeof(*p->pUnused));
16694 if (!p->pUnused) {
16695 return SQLITE_NOMEM_BKPT;
16696 }
16697 p->pUnused->fd = fd;
16698 p->pUnused->flags = flags;
16699 }
16700
16701 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
16702 if( rc!=SQLITE_OK ){
16703 sqlite3_free(p->pUnused);
16704 }
16705 return rc;
16706 }
16707
16708 /*
16709 ** Open the file zPath.
16710 **
16711 ** Previously, the SQLite OS layer used three functions in place of this
16712 ** one:
16713 **
16714 ** sqlite3OsOpenReadWrite();
16715 ** sqlite3OsOpenReadOnly();
16716 ** sqlite3OsOpenExclusive();
16717 **
16718 ** These calls correspond to the following combinations of flags:
16719 **
16720 ** ReadWrite() -> (READWRITE | CREATE)
16721 ** ReadOnly() -> (READONLY)
16722 ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
16723 **
16724 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
16725 ** true, the file was configured to be automatically deleted when the
16726 ** file handle closed. To achieve the same effect using this new
16727 ** interface, add the DELETEONCLOSE flag to those specified above for
16728 ** OpenExclusive().
16729 */
16730 static int unixOpen(
16731 sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */
16732 const char *zPath, /* Pathname of file to be opened */
16733 sqlite3_file *pFile, /* The file descriptor to be filled in */
16734 int flags, /* Input flags to control the opening */
16735 int *pOutFlags /* Output flags returned to SQLite core */
16736 ){
16737 unixFile *p = (unixFile *)pFile;
16738 int fd = -1; /* File descriptor returned by open() */
16739 int openFlags = 0; /* Flags to pass to open() */
16740 int eType = flags&0xFFFFFF00; /* Type of file to open */
16741 int noLock; /* True to omit locking primitives */
16742 int rc = SQLITE_OK; /* Function Return Code */
16743 int ctrlFlags = 0; /* UNIXFILE_* flags */
16744
16745 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
16746 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
16747 int isCreate = (flags & SQLITE_OPEN_CREATE);
16748 int isReadonly = (flags & SQLITE_OPEN_READONLY);
16749 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
16750 #if SQLITE_ENABLE_LOCKING_STYLE
16751 int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY);
16752 #endif
16753 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
16754 struct statfs fsInfo;
16755 #endif
16756
16757 /* If creating a master or main-file journal, this function will open
16758 ** a file-descriptor on the directory too. The first time unixSync()
16759 ** is called the directory file descriptor will be fsync()ed and close()d.
16760 */
16761 int syncDir = (isCreate && (
16762 eType==SQLITE_OPEN_MASTER_JOURNAL
16763 || eType==SQLITE_OPEN_MAIN_JOURNAL
16764 || eType==SQLITE_OPEN_WAL
16765 ));
16766
16767 /* If argument zPath is a NULL pointer, this function is required to open
16768 ** a temporary file. Use this buffer to store the file name in.
16769 */
16770 char zTmpname[MAX_PATHNAME+2];
16771 const char *zName = zPath;
16772
16773 /* Check the following statements are true:
16774 **
16775 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
16776 ** (b) if CREATE is set, then READWRITE must also be set, and
16777 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
16778 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
16779 */
16780 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
16781 assert(isCreate==0 || isReadWrite);
16782 assert(isExclusive==0 || isCreate);
16783 assert(isDelete==0 || isCreate);
16784
16785 /* The main DB, main journal, WAL file and master journal are never
16786 ** automatically deleted. Nor are they ever temporary files. */
16787 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
16788 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
16789 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
16790 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
16791
16792 /* Assert that the upper layer has set one of the "file-type" flags. */
16793 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
16794 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
16795 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
16796 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
16797 );
16798
16799 /* Detect a pid change and reset the PRNG. There is a race condition
16800 ** here such that two or more threads all trying to open databases at
16801 ** the same instant might all reset the PRNG. But multiple resets
16802 ** are harmless.
16803 */
16804 if( randomnessPid!=osGetpid(0) ){
16805 randomnessPid = osGetpid(0);
16806 sqlite3_randomness(0,0);
16807 }
16808
16809 /* Duplicated in chromium_sqlite3_fill_in_unix_sqlite3_file(). */
16810 memset(p, 0, sizeof(unixFile));
16811
16812 if( eType==SQLITE_OPEN_MAIN_DB ){
16813 UnixUnusedFd *pUnused;
16814 pUnused = findReusableFd(zName, flags);
16815 if( pUnused ){
16816 fd = pUnused->fd;
16817 }else{
16818 /* Duplicated in chromium_sqlite3_fill_in_unix_sqlite3_file(). */
16819 pUnused = sqlite3_malloc64(sizeof(*pUnused));
16820 if( !pUnused ){
16821 return SQLITE_NOMEM_BKPT;
16822 }
16823 }
16824 p->pUnused = pUnused;
16825
16826 /* Database filenames are double-zero terminated if they are not
16827 ** URIs with parameters. Hence, they can always be passed into
16828 ** sqlite3_uri_parameter(). */
16829 assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
16830
16831 }else if( !zName ){
16832 /* If zName is NULL, the upper layer is requesting a temp file. */
16833 assert(isDelete && !syncDir);
16834 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
16835 if( rc!=SQLITE_OK ){
16836 return rc;
16837 }
16838 zName = zTmpname;
16839
16840 /* Generated temporary filenames are always double-zero terminated
16841 ** for use by sqlite3_uri_parameter(). */
16842 assert( zName[strlen(zName)+1]==0 );
16843 }
16844
16845 /* Determine the value of the flags parameter passed to POSIX function
16846 ** open(). These must be calculated even if open() is not called, as
16847 ** they may be stored as part of the file handle and used by the
16848 ** 'conch file' locking functions later on. */
16849 if( isReadonly ) openFlags |= O_RDONLY;
16850 if( isReadWrite ) openFlags |= O_RDWR;
16851 if( isCreate ) openFlags |= O_CREAT;
16852 if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
16853 openFlags |= (O_LARGEFILE|O_BINARY);
16854
16855 if( fd<0 ){
16856 mode_t openMode; /* Permissions to create file with */
16857 uid_t uid; /* Userid for the file */
16858 gid_t gid; /* Groupid for the file */
16859 rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
16860 if( rc!=SQLITE_OK ){
16861 assert( !p->pUnused );
16862 assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
16863 return rc;
16864 }
16865 fd = robust_open(zName, openFlags, openMode);
16866 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
16867 assert( !isExclusive || (openFlags & O_CREAT)!=0 );
16868 if( fd<0 && errno!=EISDIR && isReadWrite ){
16869 /* Failed to open the file for read/write access. Try read-only. */
16870 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
16871 openFlags &= ~(O_RDWR|O_CREAT);
16872 flags |= SQLITE_OPEN_READONLY;
16873 openFlags |= O_RDONLY;
16874 isReadonly = 1;
16875 fd = robust_open(zName, openFlags, openMode);
16876 }
16877 if( fd<0 ){
16878 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
16879 goto open_finished;
16880 }
16881
16882 /* If this process is running as root and if creating a new rollback
16883 ** journal or WAL file, set the ownership of the journal or WAL to be
16884 ** the same as the original database.
16885 */
16886 if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
16887 robustFchown(fd, uid, gid);
16888 }
16889 }
16890 assert( fd>=0 );
16891 if( pOutFlags ){
16892 *pOutFlags = flags;
16893 }
16894
16895 if( p->pUnused ){
16896 /* Duplicated in chromium_sqlite3_fill_in_unix_sqlite3_file(). */
16897 p->pUnused->fd = fd;
16898 p->pUnused->flags = flags;
16899 }
16900
16901 if( isDelete ){
16902 #if OS_VXWORKS
16903 zPath = zName;
16904 #elif defined(SQLITE_UNLINK_AFTER_CLOSE)
16905 zPath = sqlite3_mprintf("%s", zName);
16906 if( zPath==0 ){
16907 robust_close(p, fd, __LINE__);
16908 return SQLITE_NOMEM_BKPT;
16909 }
16910 #else
16911 osUnlink(zName);
16912 #endif
16913 }
16914 #if SQLITE_ENABLE_LOCKING_STYLE
16915 else{
16916 p->openFlags = openFlags;
16917 }
16918 #endif
16919
16920 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
16921 if( fstatfs(fd, &fsInfo) == -1 ){
16922 storeLastErrno(p, errno);
16923 robust_close(p, fd, __LINE__);
16924 return SQLITE_IOERR_ACCESS;
16925 }
16926 if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
16927 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
16928 }
16929 if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) {
16930 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
16931 }
16932 #endif
16933
16934 /* Set up appropriate ctrlFlags */
16935 if( isDelete ) ctrlFlags |= UNIXFILE_DELETE;
16936 if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY;
16937 noLock = eType!=SQLITE_OPEN_MAIN_DB;
16938 if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK;
16939 if( syncDir ) ctrlFlags |= UNIXFILE_DIRSYNC;
16940 if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
16941
16942 #if SQLITE_ENABLE_LOCKING_STYLE
16943 #if SQLITE_PREFER_PROXY_LOCKING
16944 isAutoProxy = 1;
16945 #endif
16946 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
16947 char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
16948 int useProxy = 0;
16949
16950 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
16951 ** never use proxy, NULL means use proxy for non-local files only. */
16952 if( envforce!=NULL ){
16953 useProxy = atoi(envforce)>0;
16954 }else{
16955 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
16956 }
16957 if( useProxy ){
16958 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
16959 if( rc==SQLITE_OK ){
16960 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
16961 if( rc!=SQLITE_OK ){
16962 /* Use unixClose to clean up the resources added in fillInUnixFile
16963 ** and clear all the structure's references. Specifically,
16964 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
16965 */
16966 unixClose(pFile);
16967 return rc;
16968 }
16969 }
16970 goto open_finished;
16971 }
16972 }
16973 #endif
16974
16975 /* Duplicated in chromium_sqlite3_fill_in_unix_sqlite3_file(). */
16976 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
16977
16978 open_finished:
16979 if( rc!=SQLITE_OK ){
16980 /* Duplicated in chromium_sqlite3_fill_in_unix_sqlite3_file(). */
16981 sqlite3_free(p->pUnused);
16982 }
16983 return rc;
16984 }
16985
16986
16987 /*
16988 ** Delete the file at zPath. If the dirSync argument is true, fsync()
16989 ** the directory after deleting the file.
16990 */
16991 static int unixDelete(
16992 sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */
16993 const char *zPath, /* Name of file to be deleted */
16994 int dirSync /* If true, fsync() directory after deleting file */
16995 ){
16996 int rc = SQLITE_OK;
16997 UNUSED_PARAMETER(NotUsed);
16998 SimulateIOError(return SQLITE_IOERR_DELETE);
16999 if( osUnlink(zPath)==(-1) ){
17000 if( errno==ENOENT
17001 #if OS_VXWORKS
17002 || osAccess(zPath,0)!=0
17003 #endif
17004 ){
17005 rc = SQLITE_IOERR_DELETE_NOENT;
17006 }else{
17007 rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
17008 }
17009 return rc;
17010 }
17011 #ifndef SQLITE_DISABLE_DIRSYNC
17012 if( (dirSync & 1)!=0 ){
17013 int fd;
17014 rc = osOpenDirectory(zPath, &fd);
17015 if( rc==SQLITE_OK ){
17016 if( full_fsync(fd,0,0) ){
17017 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
17018 }
17019 robust_close(0, fd, __LINE__);
17020 }else{
17021 assert( rc==SQLITE_CANTOPEN );
17022 rc = SQLITE_OK;
17023 }
17024 }
17025 #endif
17026 return rc;
17027 }
17028
17029 /*
17030 ** Test the existence of or access permissions of file zPath. The
17031 ** test performed depends on the value of flags:
17032 **
17033 ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists
17034 ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
17035 ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
17036 **
17037 ** Otherwise return 0.
17038 */
17039 static int unixAccess(
17040 sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */
17041 const char *zPath, /* Path of the file to examine */
17042 int flags, /* What do we want to learn about the zPath file? */
17043 int *pResOut /* Write result boolean here */
17044 ){
17045 UNUSED_PARAMETER(NotUsed);
17046 SimulateIOError( return SQLITE_IOERR_ACCESS; );
17047 assert( pResOut!=0 );
17048
17049 /* The spec says there are three possible values for flags. But only
17050 ** two of them are actually used */
17051 assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE );
17052
17053 if( flags==SQLITE_ACCESS_EXISTS ){
17054 struct stat buf;
17055 *pResOut = (0==osStat(zPath, &buf) && buf.st_size>0);
17056 }else{
17057 *pResOut = osAccess(zPath, W_OK|R_OK)==0;
17058 }
17059 return SQLITE_OK;
17060 }
17061
17062 /*
17063 **
17064 */
17065 static int mkFullPathname(
17066 const char *zPath, /* Input path */
17067 char *zOut, /* Output buffer */
17068 int nOut /* Allocated size of buffer zOut */
17069 ){
17070 int nPath = sqlite3Strlen30(zPath);
17071 int iOff = 0;
17072 if( zPath[0]!='/' ){
17073 if( osGetcwd(zOut, nOut-2)==0 ){
17074 return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
17075 }
17076 iOff = sqlite3Strlen30(zOut);
17077 zOut[iOff++] = '/';
17078 }
17079 if( (iOff+nPath+1)>nOut ){
17080 /* SQLite assumes that xFullPathname() nul-terminates the output buffer
17081 ** even if it returns an error. */
17082 zOut[iOff] = '\0';
17083 return SQLITE_CANTOPEN_BKPT;
17084 }
17085 sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath);
17086 return SQLITE_OK;
17087 }
17088
17089 /*
17090 ** Turn a relative pathname into a full pathname. The relative path
17091 ** is stored as a nul-terminated string in the buffer pointed to by
17092 ** zPath.
17093 **
17094 ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
17095 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
17096 ** this buffer before returning.
17097 */
17098 static int unixFullPathname(
17099 sqlite3_vfs *pVfs, /* Pointer to vfs object */
17100 const char *zPath, /* Possibly relative input path */
17101 int nOut, /* Size of output buffer in bytes */
17102 char *zOut /* Output buffer */
17103 ){
17104 #if !defined(HAVE_READLINK) || !defined(HAVE_LSTAT)
17105 return mkFullPathname(zPath, zOut, nOut);
17106 #else
17107 int rc = SQLITE_OK;
17108 int nByte;
17109 int nLink = 1; /* Number of symbolic links followed so far */
17110 const char *zIn = zPath; /* Input path for each iteration of loop */
17111 char *zDel = 0;
17112
17113 assert( pVfs->mxPathname==MAX_PATHNAME );
17114 UNUSED_PARAMETER(pVfs);
17115
17116 /* It's odd to simulate an io-error here, but really this is just
17117 ** using the io-error infrastructure to test that SQLite handles this
17118 ** function failing. This function could fail if, for example, the
17119 ** current working directory has been unlinked.
17120 */
17121 SimulateIOError( return SQLITE_ERROR );
17122
17123 do {
17124
17125 /* Call stat() on path zIn. Set bLink to true if the path is a symbolic
17126 ** link, or false otherwise. */
17127 int bLink = 0;
17128 struct stat buf;
17129 if( osLstat(zIn, &buf)!=0 ){
17130 if( errno!=ENOENT ){
17131 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn);
17132 }
17133 }else{
17134 bLink = S_ISLNK(buf.st_mode);
17135 }
17136
17137 if( bLink ){
17138 if( zDel==0 ){
17139 zDel = sqlite3_malloc(nOut);
17140 if( zDel==0 ) rc = SQLITE_NOMEM_BKPT;
17141 }else if( ++nLink>SQLITE_MAX_SYMLINKS ){
17142 rc = SQLITE_CANTOPEN_BKPT;
17143 }
17144
17145 if( rc==SQLITE_OK ){
17146 nByte = osReadlink(zIn, zDel, nOut-1);
17147 if( nByte<0 ){
17148 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn);
17149 }else{
17150 if( zDel[0]!='/' ){
17151 int n;
17152 for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--);
17153 if( nByte+n+1>nOut ){
17154 rc = SQLITE_CANTOPEN_BKPT;
17155 }else{
17156 memmove(&zDel[n], zDel, nByte+1);
17157 memcpy(zDel, zIn, n);
17158 nByte += n;
17159 }
17160 }
17161 zDel[nByte] = '\0';
17162 }
17163 }
17164
17165 zIn = zDel;
17166 }
17167
17168 assert( rc!=SQLITE_OK || zIn!=zOut || zIn[0]=='/' );
17169 if( rc==SQLITE_OK && zIn!=zOut ){
17170 rc = mkFullPathname(zIn, zOut, nOut);
17171 }
17172 if( bLink==0 ) break;
17173 zIn = zOut;
17174 }while( rc==SQLITE_OK );
17175
17176 sqlite3_free(zDel);
17177 return rc;
17178 #endif /* HAVE_READLINK && HAVE_LSTAT */
17179 }
17180
17181
17182 #ifndef SQLITE_OMIT_LOAD_EXTENSION
17183 /*
17184 ** Interfaces for opening a shared library, finding entry points
17185 ** within the shared library, and closing the shared library.
17186 */
17187 #include <dlfcn.h>
17188 static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
17189 UNUSED_PARAMETER(NotUsed);
17190 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
17191 }
17192
17193 /*
17194 ** SQLite calls this function immediately after a call to unixDlSym() or
17195 ** unixDlOpen() fails (returns a null pointer). If a more detailed error
17196 ** message is available, it is written to zBufOut. If no error message
17197 ** is available, zBufOut is left unmodified and SQLite uses a default
17198 ** error message.
17199 */
17200 static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
17201 const char *zErr;
17202 UNUSED_PARAMETER(NotUsed);
17203 unixEnterMutex();
17204 zErr = dlerror();
17205 if( zErr ){
17206 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
17207 }
17208 unixLeaveMutex();
17209 }
17210 static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
17211 /*
17212 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
17213 ** cast into a pointer to a function. And yet the library dlsym() routine
17214 ** returns a void* which is really a pointer to a function. So how do we
17215 ** use dlsym() with -pedantic-errors?
17216 **
17217 ** Variable x below is defined to be a pointer to a function taking
17218 ** parameters void* and const char* and returning a pointer to a function.
17219 ** We initialize x by assigning it a pointer to the dlsym() function.
17220 ** (That assignment requires a cast.) Then we call the function that
17221 ** x points to.
17222 **
17223 ** This work-around is unlikely to work correctly on any system where
17224 ** you really cannot cast a function pointer into void*. But then, on the
17225 ** other hand, dlsym() will not work on such a system either, so we have
17226 ** not really lost anything.
17227 */
17228 void (*(*x)(void*,const char*))(void);
17229 UNUSED_PARAMETER(NotUsed);
17230 x = (void(*(*)(void*,const char*))(void))dlsym;
17231 return (*x)(p, zSym);
17232 }
17233 static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
17234 UNUSED_PARAMETER(NotUsed);
17235 dlclose(pHandle);
17236 }
17237 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
17238 #define unixDlOpen 0
17239 #define unixDlError 0
17240 #define unixDlSym 0
17241 #define unixDlClose 0
17242 #endif
17243
17244 /*
17245 ** Write nBuf bytes of random data to the supplied buffer zBuf.
17246 */
17247 static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
17248 UNUSED_PARAMETER(NotUsed);
17249 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
17250
17251 /* We have to initialize zBuf to prevent valgrind from reporting
17252 ** errors. The reports issued by valgrind are incorrect - we would
17253 ** prefer that the randomness be increased by making use of the
17254 ** uninitialized space in zBuf - but valgrind errors tend to worry
17255 ** some users. Rather than argue, it seems easier just to initialize
17256 ** the whole array and silence valgrind, even if that means less randomness
17257 ** in the random seed.
17258 **
17259 ** When testing, initializing zBuf[] to zero is all we do. That means
17260 ** that we always use the same random number sequence. This makes the
17261 ** tests repeatable.
17262 */
17263 memset(zBuf, 0, nBuf);
17264 randomnessPid = osGetpid(0);
17265 #if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
17266 {
17267 int fd, got;
17268 fd = robust_open("/dev/urandom", O_RDONLY, 0);
17269 if( fd<0 ){
17270 time_t t;
17271 time(&t);
17272 memcpy(zBuf, &t, sizeof(t));
17273 memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid));
17274 assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf );
17275 nBuf = sizeof(t) + sizeof(randomnessPid);
17276 }else{
17277 do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR );
17278 robust_close(0, fd, __LINE__);
17279 }
17280 }
17281 #endif
17282 return nBuf;
17283 }
17284
17285
17286 /*
17287 ** Sleep for a little while. Return the amount of time slept.
17288 ** The argument is the number of microseconds we want to sleep.
17289 ** The return value is the number of microseconds of sleep actually
17290 ** requested from the underlying operating system, a number which
17291 ** might be greater than or equal to the argument, but not less
17292 ** than the argument.
17293 */
17294 static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
17295 #if OS_VXWORKS
17296 struct timespec sp;
17297
17298 sp.tv_sec = microseconds / 1000000;
17299 sp.tv_nsec = (microseconds % 1000000) * 1000;
17300 nanosleep(&sp, NULL);
17301 UNUSED_PARAMETER(NotUsed);
17302 return microseconds;
17303 #elif defined(HAVE_USLEEP) && HAVE_USLEEP
17304 usleep(microseconds);
17305 UNUSED_PARAMETER(NotUsed);
17306 return microseconds;
17307 #else
17308 int seconds = (microseconds+999999)/1000000;
17309 sleep(seconds);
17310 UNUSED_PARAMETER(NotUsed);
17311 return seconds*1000000;
17312 #endif
17313 }
17314
17315 /*
17316 ** The following variable, if set to a non-zero value, is interpreted as
17317 ** the number of seconds since 1970 and is used to set the result of
17318 ** sqlite3OsCurrentTime() during testing.
17319 */
17320 #ifdef SQLITE_TEST
17321 SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1 970. */
17322 #endif
17323
17324 /*
17325 ** Find the current time (in Universal Coordinated Time). Write into *piNow
17326 ** the current time and date as a Julian Day number times 86_400_000. In
17327 ** other words, write into *piNow the number of milliseconds since the Julian
17328 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
17329 ** proleptic Gregorian calendar.
17330 **
17331 ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
17332 ** cannot be found.
17333 */
17334 static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
17335 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
17336 int rc = SQLITE_OK;
17337 #if defined(NO_GETTOD)
17338 time_t t;
17339 time(&t);
17340 *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
17341 #elif OS_VXWORKS
17342 struct timespec sNow;
17343 clock_gettime(CLOCK_REALTIME, &sNow);
17344 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
17345 #else
17346 struct timeval sNow;
17347 (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */
17348 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
17349 #endif
17350
17351 #ifdef SQLITE_TEST
17352 if( sqlite3_current_time ){
17353 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
17354 }
17355 #endif
17356 UNUSED_PARAMETER(NotUsed);
17357 return rc;
17358 }
17359
17360 #ifndef SQLITE_OMIT_DEPRECATED
17361 /*
17362 ** Find the current time (in Universal Coordinated Time). Write the
17363 ** current time and date as a Julian Day number into *prNow and
17364 ** return 0. Return 1 if the time and date cannot be found.
17365 */
17366 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
17367 sqlite3_int64 i = 0;
17368 int rc;
17369 UNUSED_PARAMETER(NotUsed);
17370 rc = unixCurrentTimeInt64(0, &i);
17371 *prNow = i/86400000.0;
17372 return rc;
17373 }
17374 #else
17375 # define unixCurrentTime 0
17376 #endif
17377
17378 /*
17379 ** The xGetLastError() method is designed to return a better
17380 ** low-level error message when operating-system problems come up
17381 ** during SQLite operation. Only the integer return code is currently
17382 ** used.
17383 */
17384 static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
17385 UNUSED_PARAMETER(NotUsed);
17386 UNUSED_PARAMETER(NotUsed2);
17387 UNUSED_PARAMETER(NotUsed3);
17388 return errno;
17389 }
17390
17391
17392 /*
17393 ************************ End of sqlite3_vfs methods ***************************
17394 ******************************************************************************/
17395
17396 /******************************************************************************
17397 ************************** Begin Proxy Locking ********************************
17398 **
17399 ** Proxy locking is a "uber-locking-method" in this sense: It uses the
17400 ** other locking methods on secondary lock files. Proxy locking is a
17401 ** meta-layer over top of the primitive locking implemented above. For
17402 ** this reason, the division that implements of proxy locking is deferred
17403 ** until late in the file (here) after all of the other I/O methods have
17404 ** been defined - so that the primitive locking methods are available
17405 ** as services to help with the implementation of proxy locking.
17406 **
17407 ****
17408 **
17409 ** The default locking schemes in SQLite use byte-range locks on the
17410 ** database file to coordinate safe, concurrent access by multiple readers
17411 ** and writers [http://sqlite.org/lockingv3.html]. The five file locking
17412 ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
17413 ** as POSIX read & write locks over fixed set of locations (via fsctl),
17414 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
17415 ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
17416 ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
17417 ** address in the shared range is taken for a SHARED lock, the entire
17418 ** shared range is taken for an EXCLUSIVE lock):
17419 **
17420 ** PENDING_BYTE 0x40000000
17421 ** RESERVED_BYTE 0x40000001
17422 ** SHARED_RANGE 0x40000002 -> 0x40000200
17423 **
17424 ** This works well on the local file system, but shows a nearly 100x
17425 ** slowdown in read performance on AFP because the AFP client disables
17426 ** the read cache when byte-range locks are present. Enabling the read
17427 ** cache exposes a cache coherency problem that is present on all OS X
17428 ** supported network file systems. NFS and AFP both observe the
17429 ** close-to-open semantics for ensuring cache coherency
17430 ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
17431 ** address the requirements for concurrent database access by multiple
17432 ** readers and writers
17433 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
17434 **
17435 ** To address the performance and cache coherency issues, proxy file locking
17436 ** changes the way database access is controlled by limiting access to a
17437 ** single host at a time and moving file locks off of the database file
17438 ** and onto a proxy file on the local file system.
17439 **
17440 **
17441 ** Using proxy locks
17442 ** -----------------
17443 **
17444 ** C APIs
17445 **
17446 ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE,
17447 ** <proxy_path> | ":auto:");
17448 ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE,
17449 ** &<proxy_path>);
17450 **
17451 **
17452 ** SQL pragmas
17453 **
17454 ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
17455 ** PRAGMA [database.]lock_proxy_file
17456 **
17457 ** Specifying ":auto:" means that if there is a conch file with a matching
17458 ** host ID in it, the proxy path in the conch file will be used, otherwise
17459 ** a proxy path based on the user's temp dir
17460 ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
17461 ** actual proxy file name is generated from the name and path of the
17462 ** database file. For example:
17463 **
17464 ** For database path "/Users/me/foo.db"
17465 ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
17466 **
17467 ** Once a lock proxy is configured for a database connection, it can not
17468 ** be removed, however it may be switched to a different proxy path via
17469 ** the above APIs (assuming the conch file is not being held by another
17470 ** connection or process).
17471 **
17472 **
17473 ** How proxy locking works
17474 ** -----------------------
17475 **
17476 ** Proxy file locking relies primarily on two new supporting files:
17477 **
17478 ** * conch file to limit access to the database file to a single host
17479 ** at a time
17480 **
17481 ** * proxy file to act as a proxy for the advisory locks normally
17482 ** taken on the database
17483 **
17484 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
17485 ** by taking an sqlite-style shared lock on the conch file, reading the
17486 ** contents and comparing the host's unique host ID (see below) and lock
17487 ** proxy path against the values stored in the conch. The conch file is
17488 ** stored in the same directory as the database file and the file name
17489 ** is patterned after the database file name as ".<databasename>-conch".
17490 ** If the conch file does not exist, or its contents do not match the
17491 ** host ID and/or proxy path, then the lock is escalated to an exclusive
17492 ** lock and the conch file contents is updated with the host ID and proxy
17493 ** path and the lock is downgraded to a shared lock again. If the conch
17494 ** is held by another process (with a shared lock), the exclusive lock
17495 ** will fail and SQLITE_BUSY is returned.
17496 **
17497 ** The proxy file - a single-byte file used for all advisory file locks
17498 ** normally taken on the database file. This allows for safe sharing
17499 ** of the database file for multiple readers and writers on the same
17500 ** host (the conch ensures that they all use the same local lock file).
17501 **
17502 ** Requesting the lock proxy does not immediately take the conch, it is
17503 ** only taken when the first request to lock database file is made.
17504 ** This matches the semantics of the traditional locking behavior, where
17505 ** opening a connection to a database file does not take a lock on it.
17506 ** The shared lock and an open file descriptor are maintained until
17507 ** the connection to the database is closed.
17508 **
17509 ** The proxy file and the lock file are never deleted so they only need
17510 ** to be created the first time they are used.
17511 **
17512 ** Configuration options
17513 ** ---------------------
17514 **
17515 ** SQLITE_PREFER_PROXY_LOCKING
17516 **
17517 ** Database files accessed on non-local file systems are
17518 ** automatically configured for proxy locking, lock files are
17519 ** named automatically using the same logic as
17520 ** PRAGMA lock_proxy_file=":auto:"
17521 **
17522 ** SQLITE_PROXY_DEBUG
17523 **
17524 ** Enables the logging of error messages during host id file
17525 ** retrieval and creation
17526 **
17527 ** LOCKPROXYDIR
17528 **
17529 ** Overrides the default directory used for lock proxy files that
17530 ** are named automatically via the ":auto:" setting
17531 **
17532 ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
17533 **
17534 ** Permissions to use when creating a directory for storing the
17535 ** lock proxy files, only used when LOCKPROXYDIR is not set.
17536 **
17537 **
17538 ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
17539 ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
17540 ** force proxy locking to be used for every database file opened, and 0
17541 ** will force automatic proxy locking to be disabled for all database
17542 ** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or
17543 ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
17544 */
17545
17546 /*
17547 ** Proxy locking is only available on MacOSX
17548 */
17549 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
17550
17551 /*
17552 ** The proxyLockingContext has the path and file structures for the remote
17553 ** and local proxy files in it
17554 */
17555 typedef struct proxyLockingContext proxyLockingContext;
17556 struct proxyLockingContext {
17557 unixFile *conchFile; /* Open conch file */
17558 char *conchFilePath; /* Name of the conch file */
17559 unixFile *lockProxy; /* Open proxy lock file */
17560 char *lockProxyPath; /* Name of the proxy lock file */
17561 char *dbPath; /* Name of the open file */
17562 int conchHeld; /* 1 if the conch is held, -1 if lockless */
17563 int nFails; /* Number of conch taking failures */
17564 void *oldLockingContext; /* Original lockingcontext to restore on close */
17565 sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */
17566 };
17567
17568 /*
17569 ** The proxy lock file path for the database at dbPath is written into lPath,
17570 ** which must point to valid, writable memory large enough for a maxLen length
17571 ** file path.
17572 */
17573 static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
17574 int len;
17575 int dbLen;
17576 int i;
17577
17578 #ifdef LOCKPROXYDIR
17579 len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
17580 #else
17581 # ifdef _CS_DARWIN_USER_TEMP_DIR
17582 {
17583 if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
17584 OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n",
17585 lPath, errno, osGetpid(0)));
17586 return SQLITE_IOERR_LOCK;
17587 }
17588 len = strlcat(lPath, "sqliteplocks", maxLen);
17589 }
17590 # else
17591 len = strlcpy(lPath, "/tmp/", maxLen);
17592 # endif
17593 #endif
17594
17595 if( lPath[len-1]!='/' ){
17596 len = strlcat(lPath, "/", maxLen);
17597 }
17598
17599 /* transform the db path to a unique cache name */
17600 dbLen = (int)strlen(dbPath);
17601 for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
17602 char c = dbPath[i];
17603 lPath[i+len] = (c=='/')?'_':c;
17604 }
17605 lPath[i+len]='\0';
17606 strlcat(lPath, ":auto:", maxLen);
17607 OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0)));
17608 return SQLITE_OK;
17609 }
17610
17611 /*
17612 ** Creates the lock file and any missing directories in lockPath
17613 */
17614 static int proxyCreateLockPath(const char *lockPath){
17615 int i, len;
17616 char buf[MAXPATHLEN];
17617 int start = 0;
17618
17619 assert(lockPath!=NULL);
17620 /* try to create all the intermediate directories */
17621 len = (int)strlen(lockPath);
17622 buf[0] = lockPath[0];
17623 for( i=1; i<len; i++ ){
17624 if( lockPath[i] == '/' && (i - start > 0) ){
17625 /* only mkdir if leaf dir != "." or "/" or ".." */
17626 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
17627 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
17628 buf[i]='\0';
17629 if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
17630 int err=errno;
17631 if( err!=EEXIST ) {
17632 OSTRACE(("CREATELOCKPATH FAILED creating %s, "
17633 "'%s' proxy lock path=%s pid=%d\n",
17634 buf, strerror(err), lockPath, osGetpid(0)));
17635 return err;
17636 }
17637 }
17638 }
17639 start=i+1;
17640 }
17641 buf[i] = lockPath[i];
17642 }
17643 OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n",lockPath,osGetpid(0)));
17644 return 0;
17645 }
17646
17647 /*
17648 ** Create a new VFS file descriptor (stored in memory obtained from
17649 ** sqlite3_malloc) and open the file named "path" in the file descriptor.
17650 **
17651 ** The caller is responsible not only for closing the file descriptor
17652 ** but also for freeing the memory associated with the file descriptor.
17653 */
17654 static int proxyCreateUnixFile(
17655 const char *path, /* path for the new unixFile */
17656 unixFile **ppFile, /* unixFile created and returned by ref */
17657 int islockfile /* if non zero missing dirs will be created */
17658 ) {
17659 int fd = -1;
17660 unixFile *pNew;
17661 int rc = SQLITE_OK;
17662 int openFlags = O_RDWR | O_CREAT;
17663 sqlite3_vfs dummyVfs;
17664 int terrno = 0;
17665 UnixUnusedFd *pUnused = NULL;
17666
17667 /* 1. first try to open/create the file
17668 ** 2. if that fails, and this is a lock file (not-conch), try creating
17669 ** the parent directories and then try again.
17670 ** 3. if that fails, try to open the file read-only
17671 ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
17672 */
17673 pUnused = findReusableFd(path, openFlags);
17674 if( pUnused ){
17675 fd = pUnused->fd;
17676 }else{
17677 pUnused = sqlite3_malloc64(sizeof(*pUnused));
17678 if( !pUnused ){
17679 return SQLITE_NOMEM_BKPT;
17680 }
17681 }
17682 if( fd<0 ){
17683 fd = robust_open(path, openFlags, 0);
17684 terrno = errno;
17685 if( fd<0 && errno==ENOENT && islockfile ){
17686 if( proxyCreateLockPath(path) == SQLITE_OK ){
17687 fd = robust_open(path, openFlags, 0);
17688 }
17689 }
17690 }
17691 if( fd<0 ){
17692 openFlags = O_RDONLY;
17693 fd = robust_open(path, openFlags, 0);
17694 terrno = errno;
17695 }
17696 if( fd<0 ){
17697 if( islockfile ){
17698 return SQLITE_BUSY;
17699 }
17700 switch (terrno) {
17701 case EACCES:
17702 return SQLITE_PERM;
17703 case EIO:
17704 return SQLITE_IOERR_LOCK; /* even though it is the conch */
17705 default:
17706 return SQLITE_CANTOPEN_BKPT;
17707 }
17708 }
17709
17710 pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew));
17711 if( pNew==NULL ){
17712 rc = SQLITE_NOMEM_BKPT;
17713 goto end_create_proxy;
17714 }
17715 memset(pNew, 0, sizeof(unixFile));
17716 pNew->openFlags = openFlags;
17717 memset(&dummyVfs, 0, sizeof(dummyVfs));
17718 dummyVfs.pAppData = (void*)&autolockIoFinder;
17719 dummyVfs.zName = "dummy";
17720 pUnused->fd = fd;
17721 pUnused->flags = openFlags;
17722 pNew->pUnused = pUnused;
17723
17724 rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
17725 if( rc==SQLITE_OK ){
17726 *ppFile = pNew;
17727 return SQLITE_OK;
17728 }
17729 end_create_proxy:
17730 robust_close(pNew, fd, __LINE__);
17731 sqlite3_free(pNew);
17732 sqlite3_free(pUnused);
17733 return rc;
17734 }
17735
17736 #ifdef SQLITE_TEST
17737 /* simulate multiple hosts by creating unique hostid file paths */
17738 SQLITE_API int sqlite3_hostid_num = 0;
17739 #endif
17740
17741 #define PROXY_HOSTIDLEN 16 /* conch file host id length */
17742
17743 #ifdef HAVE_GETHOSTUUID
17744 /* Not always defined in the headers as it ought to be */
17745 extern int gethostuuid(uuid_t id, const struct timespec *wait);
17746 #endif
17747
17748 /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN
17749 ** bytes of writable memory.
17750 */
17751 static int proxyGetHostID(unsigned char *pHostID, int *pError){
17752 assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
17753 memset(pHostID, 0, PROXY_HOSTIDLEN);
17754 #ifdef HAVE_GETHOSTUUID
17755 {
17756 struct timespec timeout = {1, 0}; /* 1 sec timeout */
17757 if( gethostuuid(pHostID, &timeout) ){
17758 int err = errno;
17759 if( pError ){
17760 *pError = err;
17761 }
17762 return SQLITE_IOERR;
17763 }
17764 }
17765 #else
17766 UNUSED_PARAMETER(pError);
17767 #endif
17768 #ifdef SQLITE_TEST
17769 /* simulate multiple hosts by creating unique hostid file paths */
17770 if( sqlite3_hostid_num != 0){
17771 pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
17772 }
17773 #endif
17774
17775 return SQLITE_OK;
17776 }
17777
17778 /* The conch file contains the header, host id and lock file path
17779 */
17780 #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
17781 #define PROXY_HEADERLEN 1 /* conch file header length */
17782 #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
17783 #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
17784
17785 /*
17786 ** Takes an open conch file, copies the contents to a new path and then moves
17787 ** it back. The newly created file's file descriptor is assigned to the
17788 ** conch file structure and finally the original conch file descriptor is
17789 ** closed. Returns zero if successful.
17790 */
17791 static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
17792 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
17793 unixFile *conchFile = pCtx->conchFile;
17794 char tPath[MAXPATHLEN];
17795 char buf[PROXY_MAXCONCHLEN];
17796 char *cPath = pCtx->conchFilePath;
17797 size_t readLen = 0;
17798 size_t pathLen = 0;
17799 char errmsg[64] = "";
17800 int fd = -1;
17801 int rc = -1;
17802 UNUSED_PARAMETER(myHostID);
17803
17804 /* create a new path by replace the trailing '-conch' with '-break' */
17805 pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
17806 if( pathLen>MAXPATHLEN || pathLen<6 ||
17807 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
17808 sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
17809 goto end_breaklock;
17810 }
17811 /* read the conch content */
17812 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
17813 if( readLen<PROXY_PATHINDEX ){
17814 sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
17815 goto end_breaklock;
17816 }
17817 /* write it out to the temporary break file */
17818 fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0);
17819 if( fd<0 ){
17820 sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
17821 goto end_breaklock;
17822 }
17823 if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
17824 sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
17825 goto end_breaklock;
17826 }
17827 if( rename(tPath, cPath) ){
17828 sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
17829 goto end_breaklock;
17830 }
17831 rc = 0;
17832 fprintf(stderr, "broke stale lock on %s\n", cPath);
17833 robust_close(pFile, conchFile->h, __LINE__);
17834 conchFile->h = fd;
17835 conchFile->openFlags = O_RDWR | O_CREAT;
17836
17837 end_breaklock:
17838 if( rc ){
17839 if( fd>=0 ){
17840 osUnlink(tPath);
17841 robust_close(pFile, fd, __LINE__);
17842 }
17843 fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
17844 }
17845 return rc;
17846 }
17847
17848 /* Take the requested lock on the conch file and break a stale lock if the
17849 ** host id matches.
17850 */
17851 static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
17852 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
17853 unixFile *conchFile = pCtx->conchFile;
17854 int rc = SQLITE_OK;
17855 int nTries = 0;
17856 struct timespec conchModTime;
17857
17858 memset(&conchModTime, 0, sizeof(conchModTime));
17859 do {
17860 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
17861 nTries ++;
17862 if( rc==SQLITE_BUSY ){
17863 /* If the lock failed (busy):
17864 * 1st try: get the mod time of the conch, wait 0.5s and try again.
17865 * 2nd try: fail if the mod time changed or host id is different, wait
17866 * 10 sec and try again
17867 * 3rd try: break the lock unless the mod time has changed.
17868 */
17869 struct stat buf;
17870 if( osFstat(conchFile->h, &buf) ){
17871 storeLastErrno(pFile, errno);
17872 return SQLITE_IOERR_LOCK;
17873 }
17874
17875 if( nTries==1 ){
17876 conchModTime = buf.st_mtimespec;
17877 usleep(500000); /* wait 0.5 sec and try the lock again*/
17878 continue;
17879 }
17880
17881 assert( nTries>1 );
17882 if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec ||
17883 conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
17884 return SQLITE_BUSY;
17885 }
17886
17887 if( nTries==2 ){
17888 char tBuf[PROXY_MAXCONCHLEN];
17889 int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
17890 if( len<0 ){
17891 storeLastErrno(pFile, errno);
17892 return SQLITE_IOERR_LOCK;
17893 }
17894 if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
17895 /* don't break the lock if the host id doesn't match */
17896 if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
17897 return SQLITE_BUSY;
17898 }
17899 }else{
17900 /* don't break the lock on short read or a version mismatch */
17901 return SQLITE_BUSY;
17902 }
17903 usleep(10000000); /* wait 10 sec and try the lock again */
17904 continue;
17905 }
17906
17907 assert( nTries==3 );
17908 if( 0==proxyBreakConchLock(pFile, myHostID) ){
17909 rc = SQLITE_OK;
17910 if( lockType==EXCLUSIVE_LOCK ){
17911 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
17912 }
17913 if( !rc ){
17914 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
17915 }
17916 }
17917 }
17918 } while( rc==SQLITE_BUSY && nTries<3 );
17919
17920 return rc;
17921 }
17922
17923 /* Takes the conch by taking a shared lock and read the contents conch, if
17924 ** lockPath is non-NULL, the host ID and lock file path must match. A NULL
17925 ** lockPath means that the lockPath in the conch file will be used if the
17926 ** host IDs match, or a new lock path will be generated automatically
17927 ** and written to the conch file.
17928 */
17929 static int proxyTakeConch(unixFile *pFile){
17930 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
17931
17932 if( pCtx->conchHeld!=0 ){
17933 return SQLITE_OK;
17934 }else{
17935 unixFile *conchFile = pCtx->conchFile;
17936 uuid_t myHostID;
17937 int pError = 0;
17938 char readBuf[PROXY_MAXCONCHLEN];
17939 char lockPath[MAXPATHLEN];
17940 char *tempLockPath = NULL;
17941 int rc = SQLITE_OK;
17942 int createConch = 0;
17943 int hostIdMatch = 0;
17944 int readLen = 0;
17945 int tryOldLockPath = 0;
17946 int forceNewLockPath = 0;
17947
17948 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
17949 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
17950 osGetpid(0)));
17951
17952 rc = proxyGetHostID(myHostID, &pError);
17953 if( (rc&0xff)==SQLITE_IOERR ){
17954 storeLastErrno(pFile, pError);
17955 goto end_takeconch;
17956 }
17957 rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
17958 if( rc!=SQLITE_OK ){
17959 goto end_takeconch;
17960 }
17961 /* read the existing conch file */
17962 readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
17963 if( readLen<0 ){
17964 /* I/O error: lastErrno set by seekAndRead */
17965 storeLastErrno(pFile, conchFile->lastErrno);
17966 rc = SQLITE_IOERR_READ;
17967 goto end_takeconch;
17968 }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) ||
17969 readBuf[0]!=(char)PROXY_CONCHVERSION ){
17970 /* a short read or version format mismatch means we need to create a new
17971 ** conch file.
17972 */
17973 createConch = 1;
17974 }
17975 /* if the host id matches and the lock path already exists in the conch
17976 ** we'll try to use the path there, if we can't open that path, we'll
17977 ** retry with a new auto-generated path
17978 */
17979 do { /* in case we need to try again for an :auto: named lock file */
17980
17981 if( !createConch && !forceNewLockPath ){
17982 hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID,
17983 PROXY_HOSTIDLEN);
17984 /* if the conch has data compare the contents */
17985 if( !pCtx->lockProxyPath ){
17986 /* for auto-named local lock file, just check the host ID and we'll
17987 ** use the local lock file path that's already in there
17988 */
17989 if( hostIdMatch ){
17990 size_t pathLen = (readLen - PROXY_PATHINDEX);
17991
17992 if( pathLen>=MAXPATHLEN ){
17993 pathLen=MAXPATHLEN-1;
17994 }
17995 memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
17996 lockPath[pathLen] = 0;
17997 tempLockPath = lockPath;
17998 tryOldLockPath = 1;
17999 /* create a copy of the lock path if the conch is taken */
18000 goto end_takeconch;
18001 }
18002 }else if( hostIdMatch
18003 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
18004 readLen-PROXY_PATHINDEX)
18005 ){
18006 /* conch host and lock path match */
18007 goto end_takeconch;
18008 }
18009 }
18010
18011 /* if the conch isn't writable and doesn't match, we can't take it */
18012 if( (conchFile->openFlags&O_RDWR) == 0 ){
18013 rc = SQLITE_BUSY;
18014 goto end_takeconch;
18015 }
18016
18017 /* either the conch didn't match or we need to create a new one */
18018 if( !pCtx->lockProxyPath ){
18019 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
18020 tempLockPath = lockPath;
18021 /* create a copy of the lock path _only_ if the conch is taken */
18022 }
18023
18024 /* update conch with host and path (this will fail if other process
18025 ** has a shared lock already), if the host id matches, use the big
18026 ** stick.
18027 */
18028 futimes(conchFile->h, NULL);
18029 if( hostIdMatch && !createConch ){
18030 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
18031 /* We are trying for an exclusive lock but another thread in this
18032 ** same process is still holding a shared lock. */
18033 rc = SQLITE_BUSY;
18034 } else {
18035 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
18036 }
18037 }else{
18038 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
18039 }
18040 if( rc==SQLITE_OK ){
18041 char writeBuffer[PROXY_MAXCONCHLEN];
18042 int writeSize = 0;
18043
18044 writeBuffer[0] = (char)PROXY_CONCHVERSION;
18045 memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
18046 if( pCtx->lockProxyPath!=NULL ){
18047 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
18048 MAXPATHLEN);
18049 }else{
18050 strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
18051 }
18052 writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
18053 robust_ftruncate(conchFile->h, writeSize);
18054 rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
18055 full_fsync(conchFile->h,0,0);
18056 /* If we created a new conch file (not just updated the contents of a
18057 ** valid conch file), try to match the permissions of the database
18058 */
18059 if( rc==SQLITE_OK && createConch ){
18060 struct stat buf;
18061 int err = osFstat(pFile->h, &buf);
18062 if( err==0 ){
18063 mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
18064 S_IROTH|S_IWOTH);
18065 /* try to match the database file R/W permissions, ignore failure */
18066 #ifndef SQLITE_PROXY_DEBUG
18067 osFchmod(conchFile->h, cmode);
18068 #else
18069 do{
18070 rc = osFchmod(conchFile->h, cmode);
18071 }while( rc==(-1) && errno==EINTR );
18072 if( rc!=0 ){
18073 int code = errno;
18074 fprintf(stderr, "fchmod %o FAILED with %d %s\n",
18075 cmode, code, strerror(code));
18076 } else {
18077 fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
18078 }
18079 }else{
18080 int code = errno;
18081 fprintf(stderr, "STAT FAILED[%d] with %d %s\n",
18082 err, code, strerror(code));
18083 #endif
18084 }
18085 }
18086 }
18087 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
18088
18089 end_takeconch:
18090 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
18091 if( rc==SQLITE_OK && pFile->openFlags ){
18092 int fd;
18093 if( pFile->h>=0 ){
18094 robust_close(pFile, pFile->h, __LINE__);
18095 }
18096 pFile->h = -1;
18097 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
18098 OSTRACE(("TRANSPROXY: OPEN %d\n", fd));
18099 if( fd>=0 ){
18100 pFile->h = fd;
18101 }else{
18102 rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
18103 during locking */
18104 }
18105 }
18106 if( rc==SQLITE_OK && !pCtx->lockProxy ){
18107 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
18108 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
18109 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){
18110 /* we couldn't create the proxy lock file with the old lock file path
18111 ** so try again via auto-naming
18112 */
18113 forceNewLockPath = 1;
18114 tryOldLockPath = 0;
18115 continue; /* go back to the do {} while start point, try again */
18116 }
18117 }
18118 if( rc==SQLITE_OK ){
18119 /* Need to make a copy of path if we extracted the value
18120 ** from the conch file or the path was allocated on the stack
18121 */
18122 if( tempLockPath ){
18123 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
18124 if( !pCtx->lockProxyPath ){
18125 rc = SQLITE_NOMEM_BKPT;
18126 }
18127 }
18128 }
18129 if( rc==SQLITE_OK ){
18130 pCtx->conchHeld = 1;
18131
18132 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
18133 afpLockingContext *afpCtx;
18134 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
18135 afpCtx->dbPath = pCtx->lockProxyPath;
18136 }
18137 } else {
18138 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
18139 }
18140 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
18141 rc==SQLITE_OK?"ok":"failed"));
18142 return rc;
18143 } while (1); /* in case we need to retry the :auto: lock file -
18144 ** we should never get here except via the 'continue' call. */
18145 }
18146 }
18147
18148 /*
18149 ** If pFile holds a lock on a conch file, then release that lock.
18150 */
18151 static int proxyReleaseConch(unixFile *pFile){
18152 int rc = SQLITE_OK; /* Subroutine return code */
18153 proxyLockingContext *pCtx; /* The locking context for the proxy lock */
18154 unixFile *conchFile; /* Name of the conch file */
18155
18156 pCtx = (proxyLockingContext *)pFile->lockingContext;
18157 conchFile = pCtx->conchFile;
18158 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
18159 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
18160 osGetpid(0)));
18161 if( pCtx->conchHeld>0 ){
18162 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
18163 }
18164 pCtx->conchHeld = 0;
18165 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
18166 (rc==SQLITE_OK ? "ok" : "failed")));
18167 return rc;
18168 }
18169
18170 /*
18171 ** Given the name of a database file, compute the name of its conch file.
18172 ** Store the conch filename in memory obtained from sqlite3_malloc64().
18173 ** Make *pConchPath point to the new name. Return SQLITE_OK on success
18174 ** or SQLITE_NOMEM if unable to obtain memory.
18175 **
18176 ** The caller is responsible for ensuring that the allocated memory
18177 ** space is eventually freed.
18178 **
18179 ** *pConchPath is set to NULL if a memory allocation error occurs.
18180 */
18181 static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
18182 int i; /* Loop counter */
18183 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
18184 char *conchPath; /* buffer in which to construct conch name */
18185
18186 /* Allocate space for the conch filename and initialize the name to
18187 ** the name of the original database file. */
18188 *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8);
18189 if( conchPath==0 ){
18190 return SQLITE_NOMEM_BKPT;
18191 }
18192 memcpy(conchPath, dbPath, len+1);
18193
18194 /* now insert a "." before the last / character */
18195 for( i=(len-1); i>=0; i-- ){
18196 if( conchPath[i]=='/' ){
18197 i++;
18198 break;
18199 }
18200 }
18201 conchPath[i]='.';
18202 while ( i<len ){
18203 conchPath[i+1]=dbPath[i];
18204 i++;
18205 }
18206
18207 /* append the "-conch" suffix to the file */
18208 memcpy(&conchPath[i+1], "-conch", 7);
18209 assert( (int)strlen(conchPath) == len+7 );
18210
18211 return SQLITE_OK;
18212 }
18213
18214
18215 /* Takes a fully configured proxy locking-style unix file and switches
18216 ** the local lock file path
18217 */
18218 static int switchLockProxyPath(unixFile *pFile, const char *path) {
18219 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
18220 char *oldPath = pCtx->lockProxyPath;
18221 int rc = SQLITE_OK;
18222
18223 if( pFile->eFileLock!=NO_LOCK ){
18224 return SQLITE_BUSY;
18225 }
18226
18227 /* nothing to do if the path is NULL, :auto: or matches the existing path */
18228 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
18229 (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
18230 return SQLITE_OK;
18231 }else{
18232 unixFile *lockProxy = pCtx->lockProxy;
18233 pCtx->lockProxy=NULL;
18234 pCtx->conchHeld = 0;
18235 if( lockProxy!=NULL ){
18236 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
18237 if( rc ) return rc;
18238 sqlite3_free(lockProxy);
18239 }
18240 sqlite3_free(oldPath);
18241 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
18242 }
18243
18244 return rc;
18245 }
18246
18247 /*
18248 ** pFile is a file that has been opened by a prior xOpen call. dbPath
18249 ** is a string buffer at least MAXPATHLEN+1 characters in size.
18250 **
18251 ** This routine find the filename associated with pFile and writes it
18252 ** int dbPath.
18253 */
18254 static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
18255 #if defined(__APPLE__)
18256 if( pFile->pMethod == &afpIoMethods ){
18257 /* afp style keeps a reference to the db path in the filePath field
18258 ** of the struct */
18259 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
18260 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
18261 MAXPATHLEN);
18262 } else
18263 #endif
18264 if( pFile->pMethod == &dotlockIoMethods ){
18265 /* dot lock style uses the locking context to store the dot lock
18266 ** file path */
18267 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
18268 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
18269 }else{
18270 /* all other styles use the locking context to store the db file path */
18271 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
18272 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
18273 }
18274 return SQLITE_OK;
18275 }
18276
18277 /*
18278 ** Takes an already filled in unix file and alters it so all file locking
18279 ** will be performed on the local proxy lock file. The following fields
18280 ** are preserved in the locking context so that they can be restored and
18281 ** the unix structure properly cleaned up at close time:
18282 ** ->lockingContext
18283 ** ->pMethod
18284 */
18285 static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
18286 proxyLockingContext *pCtx;
18287 char dbPath[MAXPATHLEN+1]; /* Name of the database file */
18288 char *lockPath=NULL;
18289 int rc = SQLITE_OK;
18290
18291 if( pFile->eFileLock!=NO_LOCK ){
18292 return SQLITE_BUSY;
18293 }
18294 proxyGetDbPathForUnixFile(pFile, dbPath);
18295 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
18296 lockPath=NULL;
18297 }else{
18298 lockPath=(char *)path;
18299 }
18300
18301 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
18302 (lockPath ? lockPath : ":auto:"), osGetpid(0)));
18303
18304 pCtx = sqlite3_malloc64( sizeof(*pCtx) );
18305 if( pCtx==0 ){
18306 return SQLITE_NOMEM_BKPT;
18307 }
18308 memset(pCtx, 0, sizeof(*pCtx));
18309
18310 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
18311 if( rc==SQLITE_OK ){
18312 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
18313 if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
18314 /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
18315 ** (c) the file system is read-only, then enable no-locking access.
18316 ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
18317 ** that openFlags will have only one of O_RDONLY or O_RDWR.
18318 */
18319 struct statfs fsInfo;
18320 struct stat conchInfo;
18321 int goLockless = 0;
18322
18323 if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
18324 int err = errno;
18325 if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
18326 goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
18327 }
18328 }
18329 if( goLockless ){
18330 pCtx->conchHeld = -1; /* read only FS/ lockless */
18331 rc = SQLITE_OK;
18332 }
18333 }
18334 }
18335 if( rc==SQLITE_OK && lockPath ){
18336 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
18337 }
18338
18339 if( rc==SQLITE_OK ){
18340 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
18341 if( pCtx->dbPath==NULL ){
18342 rc = SQLITE_NOMEM_BKPT;
18343 }
18344 }
18345 if( rc==SQLITE_OK ){
18346 /* all memory is allocated, proxys are created and assigned,
18347 ** switch the locking context and pMethod then return.
18348 */
18349 pCtx->oldLockingContext = pFile->lockingContext;
18350 pFile->lockingContext = pCtx;
18351 pCtx->pOldMethod = pFile->pMethod;
18352 pFile->pMethod = &proxyIoMethods;
18353 }else{
18354 if( pCtx->conchFile ){
18355 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
18356 sqlite3_free(pCtx->conchFile);
18357 }
18358 sqlite3DbFree(0, pCtx->lockProxyPath);
18359 sqlite3_free(pCtx->conchFilePath);
18360 sqlite3_free(pCtx);
18361 }
18362 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
18363 (rc==SQLITE_OK ? "ok" : "failed")));
18364 return rc;
18365 }
18366
18367
18368 /*
18369 ** This routine handles sqlite3_file_control() calls that are specific
18370 ** to proxy locking.
18371 */
18372 static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
18373 switch( op ){
18374 case SQLITE_FCNTL_GET_LOCKPROXYFILE: {
18375 unixFile *pFile = (unixFile*)id;
18376 if( pFile->pMethod == &proxyIoMethods ){
18377 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
18378 proxyTakeConch(pFile);
18379 if( pCtx->lockProxyPath ){
18380 *(const char **)pArg = pCtx->lockProxyPath;
18381 }else{
18382 *(const char **)pArg = ":auto: (not held)";
18383 }
18384 } else {
18385 *(const char **)pArg = NULL;
18386 }
18387 return SQLITE_OK;
18388 }
18389 case SQLITE_FCNTL_SET_LOCKPROXYFILE: {
18390 unixFile *pFile = (unixFile*)id;
18391 int rc = SQLITE_OK;
18392 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
18393 if( pArg==NULL || (const char *)pArg==0 ){
18394 if( isProxyStyle ){
18395 /* turn off proxy locking - not supported. If support is added for
18396 ** switching proxy locking mode off then it will need to fail if
18397 ** the journal mode is WAL mode.
18398 */
18399 rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
18400 }else{
18401 /* turn off proxy locking - already off - NOOP */
18402 rc = SQLITE_OK;
18403 }
18404 }else{
18405 const char *proxyPath = (const char *)pArg;
18406 if( isProxyStyle ){
18407 proxyLockingContext *pCtx =
18408 (proxyLockingContext*)pFile->lockingContext;
18409 if( !strcmp(pArg, ":auto:")
18410 || (pCtx->lockProxyPath &&
18411 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
18412 ){
18413 rc = SQLITE_OK;
18414 }else{
18415 rc = switchLockProxyPath(pFile, proxyPath);
18416 }
18417 }else{
18418 /* turn on proxy file locking */
18419 rc = proxyTransformUnixFile(pFile, proxyPath);
18420 }
18421 }
18422 return rc;
18423 }
18424 default: {
18425 assert( 0 ); /* The call assures that only valid opcodes are sent */
18426 }
18427 }
18428 /*NOTREACHED*/
18429 return SQLITE_ERROR;
18430 }
18431
18432 /*
18433 ** Within this division (the proxying locking implementation) the procedures
18434 ** above this point are all utilities. The lock-related methods of the
18435 ** proxy-locking sqlite3_io_method object follow.
18436 */
18437
18438
18439 /*
18440 ** This routine checks if there is a RESERVED lock held on the specified
18441 ** file by this or any other process. If such a lock is held, set *pResOut
18442 ** to a non-zero value otherwise *pResOut is set to zero. The return value
18443 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
18444 */
18445 static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
18446 unixFile *pFile = (unixFile*)id;
18447 int rc = proxyTakeConch(pFile);
18448 if( rc==SQLITE_OK ){
18449 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
18450 if( pCtx->conchHeld>0 ){
18451 unixFile *proxy = pCtx->lockProxy;
18452 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
18453 }else{ /* conchHeld < 0 is lockless */
18454 pResOut=0;
18455 }
18456 }
18457 return rc;
18458 }
18459
18460 /*
18461 ** Lock the file with the lock specified by parameter eFileLock - one
18462 ** of the following:
18463 **
18464 ** (1) SHARED_LOCK
18465 ** (2) RESERVED_LOCK
18466 ** (3) PENDING_LOCK
18467 ** (4) EXCLUSIVE_LOCK
18468 **
18469 ** Sometimes when requesting one lock state, additional lock states
18470 ** are inserted in between. The locking might fail on one of the later
18471 ** transitions leaving the lock state different from what it started but
18472 ** still short of its goal. The following chart shows the allowed
18473 ** transitions and the inserted intermediate states:
18474 **
18475 ** UNLOCKED -> SHARED
18476 ** SHARED -> RESERVED
18477 ** SHARED -> (PENDING) -> EXCLUSIVE
18478 ** RESERVED -> (PENDING) -> EXCLUSIVE
18479 ** PENDING -> EXCLUSIVE
18480 **
18481 ** This routine will only increase a lock. Use the sqlite3OsUnlock()
18482 ** routine to lower a locking level.
18483 */
18484 static int proxyLock(sqlite3_file *id, int eFileLock) {
18485 unixFile *pFile = (unixFile*)id;
18486 int rc = proxyTakeConch(pFile);
18487 if( rc==SQLITE_OK ){
18488 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
18489 if( pCtx->conchHeld>0 ){
18490 unixFile *proxy = pCtx->lockProxy;
18491 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
18492 pFile->eFileLock = proxy->eFileLock;
18493 }else{
18494 /* conchHeld < 0 is lockless */
18495 }
18496 }
18497 return rc;
18498 }
18499
18500
18501 /*
18502 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock
18503 ** must be either NO_LOCK or SHARED_LOCK.
18504 **
18505 ** If the locking level of the file descriptor is already at or below
18506 ** the requested locking level, this routine is a no-op.
18507 */
18508 static int proxyUnlock(sqlite3_file *id, int eFileLock) {
18509 unixFile *pFile = (unixFile*)id;
18510 int rc = proxyTakeConch(pFile);
18511 if( rc==SQLITE_OK ){
18512 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
18513 if( pCtx->conchHeld>0 ){
18514 unixFile *proxy = pCtx->lockProxy;
18515 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
18516 pFile->eFileLock = proxy->eFileLock;
18517 }else{
18518 /* conchHeld < 0 is lockless */
18519 }
18520 }
18521 return rc;
18522 }
18523
18524 /*
18525 ** Close a file that uses proxy locks.
18526 */
18527 static int proxyClose(sqlite3_file *id) {
18528 if( ALWAYS(id) ){
18529 unixFile *pFile = (unixFile*)id;
18530 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
18531 unixFile *lockProxy = pCtx->lockProxy;
18532 unixFile *conchFile = pCtx->conchFile;
18533 int rc = SQLITE_OK;
18534
18535 if( lockProxy ){
18536 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
18537 if( rc ) return rc;
18538 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
18539 if( rc ) return rc;
18540 sqlite3_free(lockProxy);
18541 pCtx->lockProxy = 0;
18542 }
18543 if( conchFile ){
18544 if( pCtx->conchHeld ){
18545 rc = proxyReleaseConch(pFile);
18546 if( rc ) return rc;
18547 }
18548 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
18549 if( rc ) return rc;
18550 sqlite3_free(conchFile);
18551 }
18552 sqlite3DbFree(0, pCtx->lockProxyPath);
18553 sqlite3_free(pCtx->conchFilePath);
18554 sqlite3DbFree(0, pCtx->dbPath);
18555 /* restore the original locking context and pMethod then close it */
18556 pFile->lockingContext = pCtx->oldLockingContext;
18557 pFile->pMethod = pCtx->pOldMethod;
18558 sqlite3_free(pCtx);
18559 return pFile->pMethod->xClose(id);
18560 }
18561 return SQLITE_OK;
18562 }
18563
18564
18565
18566 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
18567 /*
18568 ** The proxy locking style is intended for use with AFP filesystems.
18569 ** And since AFP is only supported on MacOSX, the proxy locking is also
18570 ** restricted to MacOSX.
18571 **
18572 **
18573 ******************* End of the proxy lock implementation **********************
18574 ******************************************************************************/
18575
18576 /*
18577 ** Initialize the operating system interface.
18578 **
18579 ** This routine registers all VFS implementations for unix-like operating
18580 ** systems. This routine, and the sqlite3_os_end() routine that follows,
18581 ** should be the only routines in this file that are visible from other
18582 ** files.
18583 **
18584 ** This routine is called once during SQLite initialization and by a
18585 ** single thread. The memory allocation and mutex subsystems have not
18586 ** necessarily been initialized when this routine is called, and so they
18587 ** should not be used.
18588 */
18589 SQLITE_API int sqlite3_os_init(void){
18590 /*
18591 ** The following macro defines an initializer for an sqlite3_vfs object.
18592 ** The name of the VFS is NAME. The pAppData is a pointer to a pointer
18593 ** to the "finder" function. (pAppData is a pointer to a pointer because
18594 ** silly C90 rules prohibit a void* from being cast to a function pointer
18595 ** and so we have to go through the intermediate pointer to avoid problems
18596 ** when compiling with -pedantic-errors on GCC.)
18597 **
18598 ** The FINDER parameter to this macro is the name of the pointer to the
18599 ** finder-function. The finder-function returns a pointer to the
18600 ** sqlite_io_methods object that implements the desired locking
18601 ** behaviors. See the division above that contains the IOMETHODS
18602 ** macro for addition information on finder-functions.
18603 **
18604 ** Most finders simply return a pointer to a fixed sqlite3_io_methods
18605 ** object. But the "autolockIoFinder" available on MacOSX does a little
18606 ** more than that; it looks at the filesystem type that hosts the
18607 ** database file and tries to choose an locking method appropriate for
18608 ** that filesystem time.
18609 */
18610 #define UNIXVFS(VFSNAME, FINDER) { \
18611 3, /* iVersion */ \
18612 sizeof(unixFile), /* szOsFile */ \
18613 MAX_PATHNAME, /* mxPathname */ \
18614 0, /* pNext */ \
18615 VFSNAME, /* zName */ \
18616 (void*)&FINDER, /* pAppData */ \
18617 unixOpen, /* xOpen */ \
18618 unixDelete, /* xDelete */ \
18619 unixAccess, /* xAccess */ \
18620 unixFullPathname, /* xFullPathname */ \
18621 unixDlOpen, /* xDlOpen */ \
18622 unixDlError, /* xDlError */ \
18623 unixDlSym, /* xDlSym */ \
18624 unixDlClose, /* xDlClose */ \
18625 unixRandomness, /* xRandomness */ \
18626 unixSleep, /* xSleep */ \
18627 unixCurrentTime, /* xCurrentTime */ \
18628 unixGetLastError, /* xGetLastError */ \
18629 unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \
18630 unixSetSystemCall, /* xSetSystemCall */ \
18631 unixGetSystemCall, /* xGetSystemCall */ \
18632 unixNextSystemCall, /* xNextSystemCall */ \
18633 }
18634
18635 /*
18636 ** All default VFSes for unix are contained in the following array.
18637 **
18638 ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
18639 ** by the SQLite core when the VFS is registered. So the following
18640 ** array cannot be const.
18641 */
18642 static sqlite3_vfs aVfs[] = {
18643 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
18644 UNIXVFS("unix", autolockIoFinder ),
18645 #elif OS_VXWORKS
18646 UNIXVFS("unix", vxworksIoFinder ),
18647 #else
18648 UNIXVFS("unix", posixIoFinder ),
18649 #endif
18650 UNIXVFS("unix-none", nolockIoFinder ),
18651 UNIXVFS("unix-dotfile", dotlockIoFinder ),
18652 UNIXVFS("unix-excl", posixIoFinder ),
18653 #if OS_VXWORKS
18654 UNIXVFS("unix-namedsem", semIoFinder ),
18655 #endif
18656 #if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS
18657 UNIXVFS("unix-posix", posixIoFinder ),
18658 #endif
18659 #if SQLITE_ENABLE_LOCKING_STYLE
18660 UNIXVFS("unix-flock", flockIoFinder ),
18661 #endif
18662 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
18663 UNIXVFS("unix-afp", afpIoFinder ),
18664 UNIXVFS("unix-nfs", nfsIoFinder ),
18665 UNIXVFS("unix-proxy", proxyIoFinder ),
18666 #endif
18667 };
18668 unsigned int i; /* Loop counter */
18669
18670 /* Double-check that the aSyscall[] array has been constructed
18671 ** correctly. See ticket [bb3a86e890c8e96ab] */
18672 assert( ArraySize(aSyscall)==28 );
18673
18674 /* Register all VFSes defined in the aVfs[] array */
18675 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
18676 sqlite3_vfs_register(&aVfs[i], i==0);
18677 }
18678 return SQLITE_OK;
18679 }
18680
18681 /*
18682 ** Shutdown the operating system interface.
18683 **
18684 ** Some operating systems might need to do some cleanup in this routine,
18685 ** to release dynamically allocated objects. But not on unix.
18686 ** This routine is a no-op for unix.
18687 */
18688 SQLITE_API int sqlite3_os_end(void){
18689 return SQLITE_OK;
18690 }
18691
18692 #endif /* SQLITE_OS_UNIX */
18693
18694 /************** End of os_unix.c *********************************************/
18695 /************** Begin file os_win.c ******************************************/
18696 /*
18697 ** 2004 May 22
18698 **
18699 ** The author disclaims copyright to this source code. In place of
18700 ** a legal notice, here is a blessing:
18701 **
18702 ** May you do good and not evil.
18703 ** May you find forgiveness for yourself and forgive others.
18704 ** May you share freely, never taking more than you give.
18705 **
18706 ******************************************************************************
18707 **
18708 ** This file contains code that is specific to Windows.
18709 */
18710 /* #include "sqliteInt.h" */
18711 #if SQLITE_OS_WIN /* This file is used for Windows only */
18712
18713 /*
18714 ** Include code that is common to all os_*.c files
18715 */
18716 /************** Include os_common.h in the middle of os_win.c ****************/
18717 /************** Begin file os_common.h ***************************************/
18718 /*
18719 ** 2004 May 22
18720 **
18721 ** The author disclaims copyright to this source code. In place of
18722 ** a legal notice, here is a blessing:
18723 **
18724 ** May you do good and not evil.
18725 ** May you find forgiveness for yourself and forgive others.
18726 ** May you share freely, never taking more than you give.
18727 **
18728 ******************************************************************************
18729 **
18730 ** This file contains macros and a little bit of code that is common to
18731 ** all of the platform-specific files (os_*.c) and is #included into those
18732 ** files.
18733 **
18734 ** This file should be #included by the os_*.c files only. It is not a
18735 ** general purpose header file.
18736 */
18737 #ifndef _OS_COMMON_H_
18738 #define _OS_COMMON_H_
18739
18740 /*
18741 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
18742 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
18743 ** switch. The following code should catch this problem at compile-time.
18744 */
18745 #ifdef MEMORY_DEBUG
18746 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
18747 #endif
18748
18749 /*
18750 ** Macros for performance tracing. Normally turned off. Only works
18751 ** on i486 hardware.
18752 */
18753 #ifdef SQLITE_PERFORMANCE_TRACE
18754
18755 /*
18756 ** hwtime.h contains inline assembler code for implementing
18757 ** high-performance timing routines.
18758 */
18759 /************** Include hwtime.h in the middle of os_common.h ****************/
18760 /************** Begin file hwtime.h ******************************************/
18761 /*
18762 ** 2008 May 27
18763 **
18764 ** The author disclaims copyright to this source code. In place of
18765 ** a legal notice, here is a blessing:
18766 **
18767 ** May you do good and not evil.
18768 ** May you find forgiveness for yourself and forgive others.
18769 ** May you share freely, never taking more than you give.
18770 **
18771 ******************************************************************************
18772 **
18773 ** This file contains inline asm code for retrieving "high-performance"
18774 ** counters for x86 class CPUs.
18775 */
18776 #ifndef SQLITE_HWTIME_H
18777 #define SQLITE_HWTIME_H
18778
18779 /*
18780 ** The following routine only works on pentium-class (or newer) processors.
18781 ** It uses the RDTSC opcode to read the cycle count value out of the
18782 ** processor and returns that value. This can be used for high-res
18783 ** profiling.
18784 */
18785 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
18786 (defined(i386) || defined(__i386__) || defined(_M_IX86))
18787
18788 #if defined(__GNUC__)
18789
18790 __inline__ sqlite_uint64 sqlite3Hwtime(void){
18791 unsigned int lo, hi;
18792 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
18793 return (sqlite_uint64)hi << 32 | lo;
18794 }
18795
18796 #elif defined(_MSC_VER)
18797
18798 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
18799 __asm {
18800 rdtsc
18801 ret ; return value at EDX:EAX
18802 }
18803 }
18804
18805 #endif
18806
18807 #elif (defined(__GNUC__) && defined(__x86_64__))
18808
18809 __inline__ sqlite_uint64 sqlite3Hwtime(void){
18810 unsigned long val;
18811 __asm__ __volatile__ ("rdtsc" : "=A" (val));
18812 return val;
18813 }
18814
18815 #elif (defined(__GNUC__) && defined(__ppc__))
18816
18817 __inline__ sqlite_uint64 sqlite3Hwtime(void){
18818 unsigned long long retval;
18819 unsigned long junk;
18820 __asm__ __volatile__ ("\n\
18821 1: mftbu %1\n\
18822 mftb %L0\n\
18823 mftbu %0\n\
18824 cmpw %0,%1\n\
18825 bne 1b"
18826 : "=r" (retval), "=r" (junk));
18827 return retval;
18828 }
18829
18830 #else
18831
18832 #error Need implementation of sqlite3Hwtime() for your platform.
18833
18834 /*
18835 ** To compile without implementing sqlite3Hwtime() for your platform,
18836 ** you can remove the above #error and use the following
18837 ** stub function. You will lose timing support for many
18838 ** of the debugging and testing utilities, but it should at
18839 ** least compile and run.
18840 */
18841 SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
18842
18843 #endif
18844
18845 #endif /* !defined(SQLITE_HWTIME_H) */
18846
18847 /************** End of hwtime.h **********************************************/
18848 /************** Continuing where we left off in os_common.h ******************/
18849
18850 static sqlite_uint64 g_start;
18851 static sqlite_uint64 g_elapsed;
18852 #define TIMER_START g_start=sqlite3Hwtime()
18853 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
18854 #define TIMER_ELAPSED g_elapsed
18855 #else
18856 #define TIMER_START
18857 #define TIMER_END
18858 #define TIMER_ELAPSED ((sqlite_uint64)0)
18859 #endif
18860
18861 /*
18862 ** If we compile with the SQLITE_TEST macro set, then the following block
18863 ** of code will give us the ability to simulate a disk I/O error. This
18864 ** is used for testing the I/O recovery logic.
18865 */
18866 #if defined(SQLITE_TEST)
18867 SQLITE_API extern int sqlite3_io_error_hit;
18868 SQLITE_API extern int sqlite3_io_error_hardhit;
18869 SQLITE_API extern int sqlite3_io_error_pending;
18870 SQLITE_API extern int sqlite3_io_error_persist;
18871 SQLITE_API extern int sqlite3_io_error_benign;
18872 SQLITE_API extern int sqlite3_diskfull_pending;
18873 SQLITE_API extern int sqlite3_diskfull;
18874 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
18875 #define SimulateIOError(CODE) \
18876 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
18877 || sqlite3_io_error_pending-- == 1 ) \
18878 { local_ioerr(); CODE; }
18879 static void local_ioerr(){
18880 IOTRACE(("IOERR\n"));
18881 sqlite3_io_error_hit++;
18882 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
18883 }
18884 #define SimulateDiskfullError(CODE) \
18885 if( sqlite3_diskfull_pending ){ \
18886 if( sqlite3_diskfull_pending == 1 ){ \
18887 local_ioerr(); \
18888 sqlite3_diskfull = 1; \
18889 sqlite3_io_error_hit = 1; \
18890 CODE; \
18891 }else{ \
18892 sqlite3_diskfull_pending--; \
18893 } \
18894 }
18895 #else
18896 #define SimulateIOErrorBenign(X)
18897 #define SimulateIOError(A)
18898 #define SimulateDiskfullError(A)
18899 #endif /* defined(SQLITE_TEST) */
18900
18901 /*
18902 ** When testing, keep a count of the number of open files.
18903 */
18904 #if defined(SQLITE_TEST)
18905 SQLITE_API extern int sqlite3_open_file_count;
18906 #define OpenCounter(X) sqlite3_open_file_count+=(X)
18907 #else
18908 #define OpenCounter(X)
18909 #endif /* defined(SQLITE_TEST) */
18910
18911 #endif /* !defined(_OS_COMMON_H_) */
18912
18913 /************** End of os_common.h *******************************************/
18914 /************** Continuing where we left off in os_win.c *********************/
18915
18916 /*
18917 ** Include the header file for the Windows VFS.
18918 */
18919 /* #include "os_win.h" */
18920
18921 /*
18922 ** Compiling and using WAL mode requires several APIs that are only
18923 ** available in Windows platforms based on the NT kernel.
18924 */
18925 #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
18926 # error "WAL mode requires support from the Windows NT kernel, compile\
18927 with SQLITE_OMIT_WAL."
18928 #endif
18929
18930 #if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE>0
18931 # error "Memory mapped files require support from the Windows NT kernel,\
18932 compile with SQLITE_MAX_MMAP_SIZE=0."
18933 #endif
18934
18935 /*
18936 ** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
18937 ** based on the sub-platform)?
18938 */
18939 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
18940 # define SQLITE_WIN32_HAS_ANSI
18941 #endif
18942
18943 /*
18944 ** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
18945 ** based on the sub-platform)?
18946 */
18947 #if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \
18948 !defined(SQLITE_WIN32_NO_WIDE)
18949 # define SQLITE_WIN32_HAS_WIDE
18950 #endif
18951
18952 /*
18953 ** Make sure at least one set of Win32 APIs is available.
18954 */
18955 #if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE)
18956 # error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\
18957 must be defined."
18958 #endif
18959
18960 /*
18961 ** Define the required Windows SDK version constants if they are not
18962 ** already available.
18963 */
18964 #ifndef NTDDI_WIN8
18965 # define NTDDI_WIN8 0x06020000
18966 #endif
18967
18968 #ifndef NTDDI_WINBLUE
18969 # define NTDDI_WINBLUE 0x06030000
18970 #endif
18971
18972 #ifndef NTDDI_WINTHRESHOLD
18973 # define NTDDI_WINTHRESHOLD 0x06040000
18974 #endif
18975
18976 /*
18977 ** Check to see if the GetVersionEx[AW] functions are deprecated on the
18978 ** target system. GetVersionEx was first deprecated in Win8.1.
18979 */
18980 #ifndef SQLITE_WIN32_GETVERSIONEX
18981 # if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
18982 # define SQLITE_WIN32_GETVERSIONEX 0 /* GetVersionEx() is deprecated */
18983 # else
18984 # define SQLITE_WIN32_GETVERSIONEX 1 /* GetVersionEx() is current */
18985 # endif
18986 #endif
18987
18988 /*
18989 ** Check to see if the CreateFileMappingA function is supported on the
18990 ** target system. It is unavailable when using "mincore.lib" on Win10.
18991 ** When compiling for Windows 10, always assume "mincore.lib" is in use.
18992 */
18993 #ifndef SQLITE_WIN32_CREATEFILEMAPPINGA
18994 # if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINTHRESHOLD
18995 # define SQLITE_WIN32_CREATEFILEMAPPINGA 0
18996 # else
18997 # define SQLITE_WIN32_CREATEFILEMAPPINGA 1
18998 # endif
18999 #endif
19000
19001 /*
19002 ** This constant should already be defined (in the "WinDef.h" SDK file).
19003 */
19004 #ifndef MAX_PATH
19005 # define MAX_PATH (260)
19006 #endif
19007
19008 /*
19009 ** Maximum pathname length (in chars) for Win32. This should normally be
19010 ** MAX_PATH.
19011 */
19012 #ifndef SQLITE_WIN32_MAX_PATH_CHARS
19013 # define SQLITE_WIN32_MAX_PATH_CHARS (MAX_PATH)
19014 #endif
19015
19016 /*
19017 ** This constant should already be defined (in the "WinNT.h" SDK file).
19018 */
19019 #ifndef UNICODE_STRING_MAX_CHARS
19020 # define UNICODE_STRING_MAX_CHARS (32767)
19021 #endif
19022
19023 /*
19024 ** Maximum pathname length (in chars) for WinNT. This should normally be
19025 ** UNICODE_STRING_MAX_CHARS.
19026 */
19027 #ifndef SQLITE_WINNT_MAX_PATH_CHARS
19028 # define SQLITE_WINNT_MAX_PATH_CHARS (UNICODE_STRING_MAX_CHARS)
19029 #endif
19030
19031 /*
19032 ** Maximum pathname length (in bytes) for Win32. The MAX_PATH macro is in
19033 ** characters, so we allocate 4 bytes per character assuming worst-case of
19034 ** 4-bytes-per-character for UTF8.
19035 */
19036 #ifndef SQLITE_WIN32_MAX_PATH_BYTES
19037 # define SQLITE_WIN32_MAX_PATH_BYTES (SQLITE_WIN32_MAX_PATH_CHARS*4)
19038 #endif
19039
19040 /*
19041 ** Maximum pathname length (in bytes) for WinNT. This should normally be
19042 ** UNICODE_STRING_MAX_CHARS * sizeof(WCHAR).
19043 */
19044 #ifndef SQLITE_WINNT_MAX_PATH_BYTES
19045 # define SQLITE_WINNT_MAX_PATH_BYTES \
19046 (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
19047 #endif
19048
19049 /*
19050 ** Maximum error message length (in chars) for WinRT.
19051 */
19052 #ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
19053 # define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
19054 #endif
19055
19056 /*
19057 ** Returns non-zero if the character should be treated as a directory
19058 ** separator.
19059 */
19060 #ifndef winIsDirSep
19061 # define winIsDirSep(a) (((a) == '/') || ((a) == '\\'))
19062 #endif
19063
19064 /*
19065 ** This macro is used when a local variable is set to a value that is
19066 ** [sometimes] not used by the code (e.g. via conditional compilation).
19067 */
19068 #ifndef UNUSED_VARIABLE_VALUE
19069 # define UNUSED_VARIABLE_VALUE(x) (void)(x)
19070 #endif
19071
19072 /*
19073 ** Returns the character that should be used as the directory separator.
19074 */
19075 #ifndef winGetDirSep
19076 # define winGetDirSep() '\\'
19077 #endif
19078
19079 /*
19080 ** Do we need to manually define the Win32 file mapping APIs for use with WAL
19081 ** mode or memory mapped files (e.g. these APIs are available in the Windows
19082 ** CE SDK; however, they are not present in the header file)?
19083 */
19084 #if SQLITE_WIN32_FILEMAPPING_API && \
19085 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
19086 /*
19087 ** Two of the file mapping APIs are different under WinRT. Figure out which
19088 ** set we need.
19089 */
19090 #if SQLITE_OS_WINRT
19091 WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \
19092 LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR);
19093
19094 WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T);
19095 #else
19096 #if defined(SQLITE_WIN32_HAS_ANSI)
19097 WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \
19098 DWORD, DWORD, DWORD, LPCSTR);
19099 #endif /* defined(SQLITE_WIN32_HAS_ANSI) */
19100
19101 #if defined(SQLITE_WIN32_HAS_WIDE)
19102 WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \
19103 DWORD, DWORD, DWORD, LPCWSTR);
19104 #endif /* defined(SQLITE_WIN32_HAS_WIDE) */
19105
19106 WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
19107 #endif /* SQLITE_OS_WINRT */
19108
19109 /*
19110 ** These file mapping APIs are common to both Win32 and WinRT.
19111 */
19112
19113 WINBASEAPI BOOL WINAPI FlushViewOfFile(LPCVOID, SIZE_T);
19114 WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
19115 #endif /* SQLITE_WIN32_FILEMAPPING_API */
19116
19117 /*
19118 ** Some Microsoft compilers lack this definition.
19119 */
19120 #ifndef INVALID_FILE_ATTRIBUTES
19121 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
19122 #endif
19123
19124 #ifndef FILE_FLAG_MASK
19125 # define FILE_FLAG_MASK (0xFF3C0000)
19126 #endif
19127
19128 #ifndef FILE_ATTRIBUTE_MASK
19129 # define FILE_ATTRIBUTE_MASK (0x0003FFF7)
19130 #endif
19131
19132 #ifndef SQLITE_OMIT_WAL
19133 /* Forward references to structures used for WAL */
19134 typedef struct winShm winShm; /* A connection to shared-memory */
19135 typedef struct winShmNode winShmNode; /* A region of shared-memory */
19136 #endif
19137
19138 /*
19139 ** WinCE lacks native support for file locking so we have to fake it
19140 ** with some code of our own.
19141 */
19142 #if SQLITE_OS_WINCE
19143 typedef struct winceLock {
19144 int nReaders; /* Number of reader locks obtained */
19145 BOOL bPending; /* Indicates a pending lock has been obtained */
19146 BOOL bReserved; /* Indicates a reserved lock has been obtained */
19147 BOOL bExclusive; /* Indicates an exclusive lock has been obtained */
19148 } winceLock;
19149 #endif
19150
19151 /*
19152 ** The winFile structure is a subclass of sqlite3_file* specific to the win32
19153 ** portability layer.
19154 */
19155 typedef struct winFile winFile;
19156 struct winFile {
19157 const sqlite3_io_methods *pMethod; /*** Must be first ***/
19158 sqlite3_vfs *pVfs; /* The VFS used to open this file */
19159 HANDLE h; /* Handle for accessing the file */
19160 u8 locktype; /* Type of lock currently held on this file */
19161 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
19162 u8 ctrlFlags; /* Flags. See WINFILE_* below */
19163 DWORD lastErrno; /* The Windows errno from the last I/O error */
19164 #ifndef SQLITE_OMIT_WAL
19165 winShm *pShm; /* Instance of shared memory on this file */
19166 #endif
19167 const char *zPath; /* Full pathname of this file */
19168 int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
19169 #if SQLITE_OS_WINCE
19170 LPWSTR zDeleteOnClose; /* Name of file to delete when closing */
19171 HANDLE hMutex; /* Mutex used to control access to shared lock */
19172 HANDLE hShared; /* Shared memory segment used for locking */
19173 winceLock local; /* Locks obtained by this instance of winFile */
19174 winceLock *shared; /* Global shared lock memory for the file */
19175 #endif
19176 #if SQLITE_MAX_MMAP_SIZE>0
19177 int nFetchOut; /* Number of outstanding xFetch references */
19178 HANDLE hMap; /* Handle for accessing memory mapping */
19179 void *pMapRegion; /* Area memory mapped */
19180 sqlite3_int64 mmapSize; /* Usable size of mapped region */
19181 sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */
19182 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
19183 #endif
19184 };
19185
19186 /*
19187 ** The winVfsAppData structure is used for the pAppData member for all of the
19188 ** Win32 VFS variants.
19189 */
19190 typedef struct winVfsAppData winVfsAppData;
19191 struct winVfsAppData {
19192 const sqlite3_io_methods *pMethod; /* The file I/O methods to use. */
19193 void *pAppData; /* The extra pAppData, if any. */
19194 BOOL bNoLock; /* Non-zero if locking is disabled. */
19195 };
19196
19197 /*
19198 ** Allowed values for winFile.ctrlFlags
19199 */
19200 #define WINFILE_RDONLY 0x02 /* Connection is read only */
19201 #define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
19202 #define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
19203
19204 /*
19205 * The size of the buffer used by sqlite3_win32_write_debug().
19206 */
19207 #ifndef SQLITE_WIN32_DBG_BUF_SIZE
19208 # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
19209 #endif
19210
19211 /*
19212 * The value used with sqlite3_win32_set_directory() to specify that
19213 * the data directory should be changed.
19214 */
19215 #ifndef SQLITE_WIN32_DATA_DIRECTORY_TYPE
19216 # define SQLITE_WIN32_DATA_DIRECTORY_TYPE (1)
19217 #endif
19218
19219 /*
19220 * The value used with sqlite3_win32_set_directory() to specify that
19221 * the temporary directory should be changed.
19222 */
19223 #ifndef SQLITE_WIN32_TEMP_DIRECTORY_TYPE
19224 # define SQLITE_WIN32_TEMP_DIRECTORY_TYPE (2)
19225 #endif
19226
19227 /*
19228 * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
19229 * various Win32 API heap functions instead of our own.
19230 */
19231 #ifdef SQLITE_WIN32_MALLOC
19232
19233 /*
19234 * If this is non-zero, an isolated heap will be created by the native Win32
19235 * allocator subsystem; otherwise, the default process heap will be used. This
19236 * setting has no effect when compiling for WinRT. By default, this is enabled
19237 * and an isolated heap will be created to store all allocated data.
19238 *
19239 ******************************************************************************
19240 * WARNING: It is important to note that when this setting is non-zero and the
19241 * winMemShutdown function is called (e.g. by the sqlite3_shutdown
19242 * function), all data that was allocated using the isolated heap will
19243 * be freed immediately and any attempt to access any of that freed
19244 * data will almost certainly result in an immediate access violation.
19245 ******************************************************************************
19246 */
19247 #ifndef SQLITE_WIN32_HEAP_CREATE
19248 # define SQLITE_WIN32_HEAP_CREATE (TRUE)
19249 #endif
19250
19251 /*
19252 * This is cache size used in the calculation of the initial size of the
19253 * Win32-specific heap. It cannot be negative.
19254 */
19255 #ifndef SQLITE_WIN32_CACHE_SIZE
19256 # if SQLITE_DEFAULT_CACHE_SIZE>=0
19257 # define SQLITE_WIN32_CACHE_SIZE (SQLITE_DEFAULT_CACHE_SIZE)
19258 # else
19259 # define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
19260 # endif
19261 #endif
19262
19263 /*
19264 * The initial size of the Win32-specific heap. This value may be zero.
19265 */
19266 #ifndef SQLITE_WIN32_HEAP_INIT_SIZE
19267 # define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_WIN32_CACHE_SIZE) * \
19268 (SQLITE_DEFAULT_PAGE_SIZE) + 4194304)
19269 #endif
19270
19271 /*
19272 * The maximum size of the Win32-specific heap. This value may be zero.
19273 */
19274 #ifndef SQLITE_WIN32_HEAP_MAX_SIZE
19275 # define SQLITE_WIN32_HEAP_MAX_SIZE (0)
19276 #endif
19277
19278 /*
19279 * The extra flags to use in calls to the Win32 heap APIs. This value may be
19280 * zero for the default behavior.
19281 */
19282 #ifndef SQLITE_WIN32_HEAP_FLAGS
19283 # define SQLITE_WIN32_HEAP_FLAGS (0)
19284 #endif
19285
19286
19287 /*
19288 ** The winMemData structure stores information required by the Win32-specific
19289 ** sqlite3_mem_methods implementation.
19290 */
19291 typedef struct winMemData winMemData;
19292 struct winMemData {
19293 #ifndef NDEBUG
19294 u32 magic1; /* Magic number to detect structure corruption. */
19295 #endif
19296 HANDLE hHeap; /* The handle to our heap. */
19297 BOOL bOwned; /* Do we own the heap (i.e. destroy it on shutdown)? */
19298 #ifndef NDEBUG
19299 u32 magic2; /* Magic number to detect structure corruption. */
19300 #endif
19301 };
19302
19303 #ifndef NDEBUG
19304 #define WINMEM_MAGIC1 0x42b2830b
19305 #define WINMEM_MAGIC2 0xbd4d7cf4
19306 #endif
19307
19308 static struct winMemData win_mem_data = {
19309 #ifndef NDEBUG
19310 WINMEM_MAGIC1,
19311 #endif
19312 NULL, FALSE
19313 #ifndef NDEBUG
19314 ,WINMEM_MAGIC2
19315 #endif
19316 };
19317
19318 #ifndef NDEBUG
19319 #define winMemAssertMagic1() assert( win_mem_data.magic1==WINMEM_MAGIC1 )
19320 #define winMemAssertMagic2() assert( win_mem_data.magic2==WINMEM_MAGIC2 )
19321 #define winMemAssertMagic() winMemAssertMagic1(); winMemAssertMagic2();
19322 #else
19323 #define winMemAssertMagic()
19324 #endif
19325
19326 #define winMemGetDataPtr() &win_mem_data
19327 #define winMemGetHeap() win_mem_data.hHeap
19328 #define winMemGetOwned() win_mem_data.bOwned
19329
19330 static void *winMemMalloc(int nBytes);
19331 static void winMemFree(void *pPrior);
19332 static void *winMemRealloc(void *pPrior, int nBytes);
19333 static int winMemSize(void *p);
19334 static int winMemRoundup(int n);
19335 static int winMemInit(void *pAppData);
19336 static void winMemShutdown(void *pAppData);
19337
19338 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
19339 #endif /* SQLITE_WIN32_MALLOC */
19340
19341 /*
19342 ** The following variable is (normally) set once and never changes
19343 ** thereafter. It records whether the operating system is Win9x
19344 ** or WinNT.
19345 **
19346 ** 0: Operating system unknown.
19347 ** 1: Operating system is Win9x.
19348 ** 2: Operating system is WinNT.
19349 **
19350 ** In order to facilitate testing on a WinNT system, the test fixture
19351 ** can manually set this value to 1 to emulate Win98 behavior.
19352 */
19353 #ifdef SQLITE_TEST
19354 SQLITE_API LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
19355 #else
19356 static LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0;
19357 #endif
19358
19359 #ifndef SYSCALL
19360 # define SYSCALL sqlite3_syscall_ptr
19361 #endif
19362
19363 /*
19364 ** This function is not available on Windows CE or WinRT.
19365 */
19366
19367 #if SQLITE_OS_WINCE || SQLITE_OS_WINRT
19368 # define osAreFileApisANSI() 1
19369 #endif
19370
19371 /*
19372 ** Many system calls are accessed through pointer-to-functions so that
19373 ** they may be overridden at runtime to facilitate fault injection during
19374 ** testing and sandboxing. The following array holds the names and pointers
19375 ** to all overrideable system calls.
19376 */
19377 static struct win_syscall {
19378 const char *zName; /* Name of the system call */
19379 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
19380 sqlite3_syscall_ptr pDefault; /* Default value */
19381 } aSyscall[] = {
19382 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
19383 { "AreFileApisANSI", (SYSCALL)AreFileApisANSI, 0 },
19384 #else
19385 { "AreFileApisANSI", (SYSCALL)0, 0 },
19386 #endif
19387
19388 #ifndef osAreFileApisANSI
19389 #define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
19390 #endif
19391
19392 #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
19393 { "CharLowerW", (SYSCALL)CharLowerW, 0 },
19394 #else
19395 { "CharLowerW", (SYSCALL)0, 0 },
19396 #endif
19397
19398 #define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
19399
19400 #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
19401 { "CharUpperW", (SYSCALL)CharUpperW, 0 },
19402 #else
19403 { "CharUpperW", (SYSCALL)0, 0 },
19404 #endif
19405
19406 #define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
19407
19408 { "CloseHandle", (SYSCALL)CloseHandle, 0 },
19409
19410 #define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
19411
19412 #if defined(SQLITE_WIN32_HAS_ANSI)
19413 { "CreateFileA", (SYSCALL)CreateFileA, 0 },
19414 #else
19415 { "CreateFileA", (SYSCALL)0, 0 },
19416 #endif
19417
19418 #define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
19419 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
19420
19421 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
19422 { "CreateFileW", (SYSCALL)CreateFileW, 0 },
19423 #else
19424 { "CreateFileW", (SYSCALL)0, 0 },
19425 #endif
19426
19427 #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
19428 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
19429
19430 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
19431 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) && \
19432 SQLITE_WIN32_CREATEFILEMAPPINGA
19433 { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 },
19434 #else
19435 { "CreateFileMappingA", (SYSCALL)0, 0 },
19436 #endif
19437
19438 #define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
19439 DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
19440
19441 #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
19442 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
19443 { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 },
19444 #else
19445 { "CreateFileMappingW", (SYSCALL)0, 0 },
19446 #endif
19447
19448 #define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
19449 DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
19450
19451 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
19452 { "CreateMutexW", (SYSCALL)CreateMutexW, 0 },
19453 #else
19454 { "CreateMutexW", (SYSCALL)0, 0 },
19455 #endif
19456
19457 #define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
19458 LPCWSTR))aSyscall[8].pCurrent)
19459
19460 #if defined(SQLITE_WIN32_HAS_ANSI)
19461 { "DeleteFileA", (SYSCALL)DeleteFileA, 0 },
19462 #else
19463 { "DeleteFileA", (SYSCALL)0, 0 },
19464 #endif
19465
19466 #define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
19467
19468 #if defined(SQLITE_WIN32_HAS_WIDE)
19469 { "DeleteFileW", (SYSCALL)DeleteFileW, 0 },
19470 #else
19471 { "DeleteFileW", (SYSCALL)0, 0 },
19472 #endif
19473
19474 #define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
19475
19476 #if SQLITE_OS_WINCE
19477 { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
19478 #else
19479 { "FileTimeToLocalFileTime", (SYSCALL)0, 0 },
19480 #endif
19481
19482 #define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
19483 LPFILETIME))aSyscall[11].pCurrent)
19484
19485 #if SQLITE_OS_WINCE
19486 { "FileTimeToSystemTime", (SYSCALL)FileTimeToSystemTime, 0 },
19487 #else
19488 { "FileTimeToSystemTime", (SYSCALL)0, 0 },
19489 #endif
19490
19491 #define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
19492 LPSYSTEMTIME))aSyscall[12].pCurrent)
19493
19494 { "FlushFileBuffers", (SYSCALL)FlushFileBuffers, 0 },
19495
19496 #define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
19497
19498 #if defined(SQLITE_WIN32_HAS_ANSI)
19499 { "FormatMessageA", (SYSCALL)FormatMessageA, 0 },
19500 #else
19501 { "FormatMessageA", (SYSCALL)0, 0 },
19502 #endif
19503
19504 #define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
19505 DWORD,va_list*))aSyscall[14].pCurrent)
19506
19507 #if defined(SQLITE_WIN32_HAS_WIDE)
19508 { "FormatMessageW", (SYSCALL)FormatMessageW, 0 },
19509 #else
19510 { "FormatMessageW", (SYSCALL)0, 0 },
19511 #endif
19512
19513 #define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
19514 DWORD,va_list*))aSyscall[15].pCurrent)
19515
19516 #if !defined(SQLITE_OMIT_LOAD_EXTENSION)
19517 { "FreeLibrary", (SYSCALL)FreeLibrary, 0 },
19518 #else
19519 { "FreeLibrary", (SYSCALL)0, 0 },
19520 #endif
19521
19522 #define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
19523
19524 { "GetCurrentProcessId", (SYSCALL)GetCurrentProcessId, 0 },
19525
19526 #define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
19527
19528 #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
19529 { "GetDiskFreeSpaceA", (SYSCALL)GetDiskFreeSpaceA, 0 },
19530 #else
19531 { "GetDiskFreeSpaceA", (SYSCALL)0, 0 },
19532 #endif
19533
19534 #define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
19535 LPDWORD))aSyscall[18].pCurrent)
19536
19537 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
19538 { "GetDiskFreeSpaceW", (SYSCALL)GetDiskFreeSpaceW, 0 },
19539 #else
19540 { "GetDiskFreeSpaceW", (SYSCALL)0, 0 },
19541 #endif
19542
19543 #define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
19544 LPDWORD))aSyscall[19].pCurrent)
19545
19546 #if defined(SQLITE_WIN32_HAS_ANSI)
19547 { "GetFileAttributesA", (SYSCALL)GetFileAttributesA, 0 },
19548 #else
19549 { "GetFileAttributesA", (SYSCALL)0, 0 },
19550 #endif
19551
19552 #define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
19553
19554 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
19555 { "GetFileAttributesW", (SYSCALL)GetFileAttributesW, 0 },
19556 #else
19557 { "GetFileAttributesW", (SYSCALL)0, 0 },
19558 #endif
19559
19560 #define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
19561
19562 #if defined(SQLITE_WIN32_HAS_WIDE)
19563 { "GetFileAttributesExW", (SYSCALL)GetFileAttributesExW, 0 },
19564 #else
19565 { "GetFileAttributesExW", (SYSCALL)0, 0 },
19566 #endif
19567
19568 #define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
19569 LPVOID))aSyscall[22].pCurrent)
19570
19571 #if !SQLITE_OS_WINRT
19572 { "GetFileSize", (SYSCALL)GetFileSize, 0 },
19573 #else
19574 { "GetFileSize", (SYSCALL)0, 0 },
19575 #endif
19576
19577 #define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
19578
19579 #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
19580 { "GetFullPathNameA", (SYSCALL)GetFullPathNameA, 0 },
19581 #else
19582 { "GetFullPathNameA", (SYSCALL)0, 0 },
19583 #endif
19584
19585 #define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
19586 LPSTR*))aSyscall[24].pCurrent)
19587
19588 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
19589 { "GetFullPathNameW", (SYSCALL)GetFullPathNameW, 0 },
19590 #else
19591 { "GetFullPathNameW", (SYSCALL)0, 0 },
19592 #endif
19593
19594 #define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
19595 LPWSTR*))aSyscall[25].pCurrent)
19596
19597 { "GetLastError", (SYSCALL)GetLastError, 0 },
19598
19599 #define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
19600
19601 #if !defined(SQLITE_OMIT_LOAD_EXTENSION)
19602 #if SQLITE_OS_WINCE
19603 /* The GetProcAddressA() routine is only available on Windows CE. */
19604 { "GetProcAddressA", (SYSCALL)GetProcAddressA, 0 },
19605 #else
19606 /* All other Windows platforms expect GetProcAddress() to take
19607 ** an ANSI string regardless of the _UNICODE setting */
19608 { "GetProcAddressA", (SYSCALL)GetProcAddress, 0 },
19609 #endif
19610 #else
19611 { "GetProcAddressA", (SYSCALL)0, 0 },
19612 #endif
19613
19614 #define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
19615 LPCSTR))aSyscall[27].pCurrent)
19616
19617 #if !SQLITE_OS_WINRT
19618 { "GetSystemInfo", (SYSCALL)GetSystemInfo, 0 },
19619 #else
19620 { "GetSystemInfo", (SYSCALL)0, 0 },
19621 #endif
19622
19623 #define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
19624
19625 { "GetSystemTime", (SYSCALL)GetSystemTime, 0 },
19626
19627 #define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
19628
19629 #if !SQLITE_OS_WINCE
19630 { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
19631 #else
19632 { "GetSystemTimeAsFileTime", (SYSCALL)0, 0 },
19633 #endif
19634
19635 #define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
19636 LPFILETIME))aSyscall[30].pCurrent)
19637
19638 #if defined(SQLITE_WIN32_HAS_ANSI)
19639 { "GetTempPathA", (SYSCALL)GetTempPathA, 0 },
19640 #else
19641 { "GetTempPathA", (SYSCALL)0, 0 },
19642 #endif
19643
19644 #define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
19645
19646 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
19647 { "GetTempPathW", (SYSCALL)GetTempPathW, 0 },
19648 #else
19649 { "GetTempPathW", (SYSCALL)0, 0 },
19650 #endif
19651
19652 #define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
19653
19654 #if !SQLITE_OS_WINRT
19655 { "GetTickCount", (SYSCALL)GetTickCount, 0 },
19656 #else
19657 { "GetTickCount", (SYSCALL)0, 0 },
19658 #endif
19659
19660 #define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
19661
19662 #if defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_GETVERSIONEX
19663 { "GetVersionExA", (SYSCALL)GetVersionExA, 0 },
19664 #else
19665 { "GetVersionExA", (SYSCALL)0, 0 },
19666 #endif
19667
19668 #define osGetVersionExA ((BOOL(WINAPI*)( \
19669 LPOSVERSIONINFOA))aSyscall[34].pCurrent)
19670
19671 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
19672 SQLITE_WIN32_GETVERSIONEX
19673 { "GetVersionExW", (SYSCALL)GetVersionExW, 0 },
19674 #else
19675 { "GetVersionExW", (SYSCALL)0, 0 },
19676 #endif
19677
19678 #define osGetVersionExW ((BOOL(WINAPI*)( \
19679 LPOSVERSIONINFOW))aSyscall[35].pCurrent)
19680
19681 { "HeapAlloc", (SYSCALL)HeapAlloc, 0 },
19682
19683 #define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
19684 SIZE_T))aSyscall[36].pCurrent)
19685
19686 #if !SQLITE_OS_WINRT
19687 { "HeapCreate", (SYSCALL)HeapCreate, 0 },
19688 #else
19689 { "HeapCreate", (SYSCALL)0, 0 },
19690 #endif
19691
19692 #define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
19693 SIZE_T))aSyscall[37].pCurrent)
19694
19695 #if !SQLITE_OS_WINRT
19696 { "HeapDestroy", (SYSCALL)HeapDestroy, 0 },
19697 #else
19698 { "HeapDestroy", (SYSCALL)0, 0 },
19699 #endif
19700
19701 #define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[38].pCurrent)
19702
19703 { "HeapFree", (SYSCALL)HeapFree, 0 },
19704
19705 #define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[39].pCurrent)
19706
19707 { "HeapReAlloc", (SYSCALL)HeapReAlloc, 0 },
19708
19709 #define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
19710 SIZE_T))aSyscall[40].pCurrent)
19711
19712 { "HeapSize", (SYSCALL)HeapSize, 0 },
19713
19714 #define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
19715 LPCVOID))aSyscall[41].pCurrent)
19716
19717 #if !SQLITE_OS_WINRT
19718 { "HeapValidate", (SYSCALL)HeapValidate, 0 },
19719 #else
19720 { "HeapValidate", (SYSCALL)0, 0 },
19721 #endif
19722
19723 #define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
19724 LPCVOID))aSyscall[42].pCurrent)
19725
19726 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
19727 { "HeapCompact", (SYSCALL)HeapCompact, 0 },
19728 #else
19729 { "HeapCompact", (SYSCALL)0, 0 },
19730 #endif
19731
19732 #define osHeapCompact ((UINT(WINAPI*)(HANDLE,DWORD))aSyscall[43].pCurrent)
19733
19734 #if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
19735 { "LoadLibraryA", (SYSCALL)LoadLibraryA, 0 },
19736 #else
19737 { "LoadLibraryA", (SYSCALL)0, 0 },
19738 #endif
19739
19740 #define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[44].pCurrent)
19741
19742 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
19743 !defined(SQLITE_OMIT_LOAD_EXTENSION)
19744 { "LoadLibraryW", (SYSCALL)LoadLibraryW, 0 },
19745 #else
19746 { "LoadLibraryW", (SYSCALL)0, 0 },
19747 #endif
19748
19749 #define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[45].pCurrent)
19750
19751 #if !SQLITE_OS_WINRT
19752 { "LocalFree", (SYSCALL)LocalFree, 0 },
19753 #else
19754 { "LocalFree", (SYSCALL)0, 0 },
19755 #endif
19756
19757 #define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[46].pCurrent)
19758
19759 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
19760 { "LockFile", (SYSCALL)LockFile, 0 },
19761 #else
19762 { "LockFile", (SYSCALL)0, 0 },
19763 #endif
19764
19765 #ifndef osLockFile
19766 #define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
19767 DWORD))aSyscall[47].pCurrent)
19768 #endif
19769
19770 #if !SQLITE_OS_WINCE
19771 { "LockFileEx", (SYSCALL)LockFileEx, 0 },
19772 #else
19773 { "LockFileEx", (SYSCALL)0, 0 },
19774 #endif
19775
19776 #ifndef osLockFileEx
19777 #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
19778 LPOVERLAPPED))aSyscall[48].pCurrent)
19779 #endif
19780
19781 #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \
19782 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
19783 { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 },
19784 #else
19785 { "MapViewOfFile", (SYSCALL)0, 0 },
19786 #endif
19787
19788 #define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
19789 SIZE_T))aSyscall[49].pCurrent)
19790
19791 { "MultiByteToWideChar", (SYSCALL)MultiByteToWideChar, 0 },
19792
19793 #define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
19794 int))aSyscall[50].pCurrent)
19795
19796 { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
19797
19798 #define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
19799 LARGE_INTEGER*))aSyscall[51].pCurrent)
19800
19801 { "ReadFile", (SYSCALL)ReadFile, 0 },
19802
19803 #define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
19804 LPOVERLAPPED))aSyscall[52].pCurrent)
19805
19806 { "SetEndOfFile", (SYSCALL)SetEndOfFile, 0 },
19807
19808 #define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[53].pCurrent)
19809
19810 #if !SQLITE_OS_WINRT
19811 { "SetFilePointer", (SYSCALL)SetFilePointer, 0 },
19812 #else
19813 { "SetFilePointer", (SYSCALL)0, 0 },
19814 #endif
19815
19816 #define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
19817 DWORD))aSyscall[54].pCurrent)
19818
19819 #if !SQLITE_OS_WINRT
19820 { "Sleep", (SYSCALL)Sleep, 0 },
19821 #else
19822 { "Sleep", (SYSCALL)0, 0 },
19823 #endif
19824
19825 #define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[55].pCurrent)
19826
19827 { "SystemTimeToFileTime", (SYSCALL)SystemTimeToFileTime, 0 },
19828
19829 #define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
19830 LPFILETIME))aSyscall[56].pCurrent)
19831
19832 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
19833 { "UnlockFile", (SYSCALL)UnlockFile, 0 },
19834 #else
19835 { "UnlockFile", (SYSCALL)0, 0 },
19836 #endif
19837
19838 #ifndef osUnlockFile
19839 #define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
19840 DWORD))aSyscall[57].pCurrent)
19841 #endif
19842
19843 #if !SQLITE_OS_WINCE
19844 { "UnlockFileEx", (SYSCALL)UnlockFileEx, 0 },
19845 #else
19846 { "UnlockFileEx", (SYSCALL)0, 0 },
19847 #endif
19848
19849 #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
19850 LPOVERLAPPED))aSyscall[58].pCurrent)
19851
19852 #if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
19853 { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 },
19854 #else
19855 { "UnmapViewOfFile", (SYSCALL)0, 0 },
19856 #endif
19857
19858 #define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[59].pCurrent)
19859
19860 { "WideCharToMultiByte", (SYSCALL)WideCharToMultiByte, 0 },
19861
19862 #define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
19863 LPCSTR,LPBOOL))aSyscall[60].pCurrent)
19864
19865 { "WriteFile", (SYSCALL)WriteFile, 0 },
19866
19867 #define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
19868 LPOVERLAPPED))aSyscall[61].pCurrent)
19869
19870 #if SQLITE_OS_WINRT
19871 { "CreateEventExW", (SYSCALL)CreateEventExW, 0 },
19872 #else
19873 { "CreateEventExW", (SYSCALL)0, 0 },
19874 #endif
19875
19876 #define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
19877 DWORD,DWORD))aSyscall[62].pCurrent)
19878
19879 #if !SQLITE_OS_WINRT
19880 { "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 },
19881 #else
19882 { "WaitForSingleObject", (SYSCALL)0, 0 },
19883 #endif
19884
19885 #define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
19886 DWORD))aSyscall[63].pCurrent)
19887
19888 #if !SQLITE_OS_WINCE
19889 { "WaitForSingleObjectEx", (SYSCALL)WaitForSingleObjectEx, 0 },
19890 #else
19891 { "WaitForSingleObjectEx", (SYSCALL)0, 0 },
19892 #endif
19893
19894 #define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
19895 BOOL))aSyscall[64].pCurrent)
19896
19897 #if SQLITE_OS_WINRT
19898 { "SetFilePointerEx", (SYSCALL)SetFilePointerEx, 0 },
19899 #else
19900 { "SetFilePointerEx", (SYSCALL)0, 0 },
19901 #endif
19902
19903 #define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
19904 PLARGE_INTEGER,DWORD))aSyscall[65].pCurrent)
19905
19906 #if SQLITE_OS_WINRT
19907 { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
19908 #else
19909 { "GetFileInformationByHandleEx", (SYSCALL)0, 0 },
19910 #endif
19911
19912 #define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
19913 FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)
19914
19915 #if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
19916 { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 },
19917 #else
19918 { "MapViewOfFileFromApp", (SYSCALL)0, 0 },
19919 #endif
19920
19921 #define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
19922 SIZE_T))aSyscall[67].pCurrent)
19923
19924 #if SQLITE_OS_WINRT
19925 { "CreateFile2", (SYSCALL)CreateFile2, 0 },
19926 #else
19927 { "CreateFile2", (SYSCALL)0, 0 },
19928 #endif
19929
19930 #define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
19931 LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[68].pCurrent)
19932
19933 #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION)
19934 { "LoadPackagedLibrary", (SYSCALL)LoadPackagedLibrary, 0 },
19935 #else
19936 { "LoadPackagedLibrary", (SYSCALL)0, 0 },
19937 #endif
19938
19939 #define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
19940 DWORD))aSyscall[69].pCurrent)
19941
19942 #if SQLITE_OS_WINRT
19943 { "GetTickCount64", (SYSCALL)GetTickCount64, 0 },
19944 #else
19945 { "GetTickCount64", (SYSCALL)0, 0 },
19946 #endif
19947
19948 #define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[70].pCurrent)
19949
19950 #if SQLITE_OS_WINRT
19951 { "GetNativeSystemInfo", (SYSCALL)GetNativeSystemInfo, 0 },
19952 #else
19953 { "GetNativeSystemInfo", (SYSCALL)0, 0 },
19954 #endif
19955
19956 #define osGetNativeSystemInfo ((VOID(WINAPI*)( \
19957 LPSYSTEM_INFO))aSyscall[71].pCurrent)
19958
19959 #if defined(SQLITE_WIN32_HAS_ANSI)
19960 { "OutputDebugStringA", (SYSCALL)OutputDebugStringA, 0 },
19961 #else
19962 { "OutputDebugStringA", (SYSCALL)0, 0 },
19963 #endif
19964
19965 #define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[72].pCurrent)
19966
19967 #if defined(SQLITE_WIN32_HAS_WIDE)
19968 { "OutputDebugStringW", (SYSCALL)OutputDebugStringW, 0 },
19969 #else
19970 { "OutputDebugStringW", (SYSCALL)0, 0 },
19971 #endif
19972
19973 #define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[73].pCurrent)
19974
19975 { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 },
19976
19977 #define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)
19978
19979 #if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)
19980 { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
19981 #else
19982 { "CreateFileMappingFromApp", (SYSCALL)0, 0 },
19983 #endif
19984
19985 #define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
19986 LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
19987
19988 /*
19989 ** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
19990 ** is really just a macro that uses a compiler intrinsic (e.g. x64).
19991 ** So do not try to make this is into a redefinable interface.
19992 */
19993 #if defined(InterlockedCompareExchange)
19994 { "InterlockedCompareExchange", (SYSCALL)0, 0 },
19995
19996 #define osInterlockedCompareExchange InterlockedCompareExchange
19997 #else
19998 { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 },
19999
20000 #define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG \
20001 SQLITE_WIN32_VOLATILE*, LONG,LONG))aSyscall[76].pCurrent)
20002 #endif /* defined(InterlockedCompareExchange) */
20003
20004 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
20005 { "UuidCreate", (SYSCALL)UuidCreate, 0 },
20006 #else
20007 { "UuidCreate", (SYSCALL)0, 0 },
20008 #endif
20009
20010 #define osUuidCreate ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[77].pCurrent)
20011
20012 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
20013 { "UuidCreateSequential", (SYSCALL)UuidCreateSequential, 0 },
20014 #else
20015 { "UuidCreateSequential", (SYSCALL)0, 0 },
20016 #endif
20017
20018 #define osUuidCreateSequential \
20019 ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[78].pCurrent)
20020
20021 #if !defined(SQLITE_NO_SYNC) && SQLITE_MAX_MMAP_SIZE>0
20022 { "FlushViewOfFile", (SYSCALL)FlushViewOfFile, 0 },
20023 #else
20024 { "FlushViewOfFile", (SYSCALL)0, 0 },
20025 #endif
20026
20027 #define osFlushViewOfFile \
20028 ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent)
20029
20030 }; /* End of the overrideable system calls */
20031
20032 /*
20033 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
20034 ** "win32" VFSes. Return SQLITE_OK opon successfully updating the
20035 ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
20036 ** system call named zName.
20037 */
20038 static int winSetSystemCall(
20039 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
20040 const char *zName, /* Name of system call to override */
20041 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
20042 ){
20043 unsigned int i;
20044 int rc = SQLITE_NOTFOUND;
20045
20046 UNUSED_PARAMETER(pNotUsed);
20047 if( zName==0 ){
20048 /* If no zName is given, restore all system calls to their default
20049 ** settings and return NULL
20050 */
20051 rc = SQLITE_OK;
20052 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
20053 if( aSyscall[i].pDefault ){
20054 aSyscall[i].pCurrent = aSyscall[i].pDefault;
20055 }
20056 }
20057 }else{
20058 /* If zName is specified, operate on only the one system call
20059 ** specified.
20060 */
20061 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
20062 if( strcmp(zName, aSyscall[i].zName)==0 ){
20063 if( aSyscall[i].pDefault==0 ){
20064 aSyscall[i].pDefault = aSyscall[i].pCurrent;
20065 }
20066 rc = SQLITE_OK;
20067 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
20068 aSyscall[i].pCurrent = pNewFunc;
20069 break;
20070 }
20071 }
20072 }
20073 return rc;
20074 }
20075
20076 /*
20077 ** Return the value of a system call. Return NULL if zName is not a
20078 ** recognized system call name. NULL is also returned if the system call
20079 ** is currently undefined.
20080 */
20081 static sqlite3_syscall_ptr winGetSystemCall(
20082 sqlite3_vfs *pNotUsed,
20083 const char *zName
20084 ){
20085 unsigned int i;
20086
20087 UNUSED_PARAMETER(pNotUsed);
20088 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
20089 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
20090 }
20091 return 0;
20092 }
20093
20094 /*
20095 ** Return the name of the first system call after zName. If zName==NULL
20096 ** then return the name of the first system call. Return NULL if zName
20097 ** is the last system call or if zName is not the name of a valid
20098 ** system call.
20099 */
20100 static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
20101 int i = -1;
20102
20103 UNUSED_PARAMETER(p);
20104 if( zName ){
20105 for(i=0; i<ArraySize(aSyscall)-1; i++){
20106 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
20107 }
20108 }
20109 for(i++; i<ArraySize(aSyscall); i++){
20110 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
20111 }
20112 return 0;
20113 }
20114
20115 #ifdef SQLITE_WIN32_MALLOC
20116 /*
20117 ** If a Win32 native heap has been configured, this function will attempt to
20118 ** compact it. Upon success, SQLITE_OK will be returned. Upon failure, one
20119 ** of SQLITE_NOMEM, SQLITE_ERROR, or SQLITE_NOTFOUND will be returned. The
20120 ** "pnLargest" argument, if non-zero, will be used to return the size of the
20121 ** largest committed free block in the heap, in bytes.
20122 */
20123 SQLITE_API int sqlite3_win32_compact_heap(LPUINT pnLargest){
20124 int rc = SQLITE_OK;
20125 UINT nLargest = 0;
20126 HANDLE hHeap;
20127
20128 winMemAssertMagic();
20129 hHeap = winMemGetHeap();
20130 assert( hHeap!=0 );
20131 assert( hHeap!=INVALID_HANDLE_VALUE );
20132 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
20133 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
20134 #endif
20135 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
20136 if( (nLargest=osHeapCompact(hHeap, SQLITE_WIN32_HEAP_FLAGS))==0 ){
20137 DWORD lastErrno = osGetLastError();
20138 if( lastErrno==NO_ERROR ){
20139 sqlite3_log(SQLITE_NOMEM, "failed to HeapCompact (no space), heap=%p",
20140 (void*)hHeap);
20141 rc = SQLITE_NOMEM_BKPT;
20142 }else{
20143 sqlite3_log(SQLITE_ERROR, "failed to HeapCompact (%lu), heap=%p",
20144 osGetLastError(), (void*)hHeap);
20145 rc = SQLITE_ERROR;
20146 }
20147 }
20148 #else
20149 sqlite3_log(SQLITE_NOTFOUND, "failed to HeapCompact, heap=%p",
20150 (void*)hHeap);
20151 rc = SQLITE_NOTFOUND;
20152 #endif
20153 if( pnLargest ) *pnLargest = nLargest;
20154 return rc;
20155 }
20156
20157 /*
20158 ** If a Win32 native heap has been configured, this function will attempt to
20159 ** destroy and recreate it. If the Win32 native heap is not isolated and/or
20160 ** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will
20161 ** be returned and no changes will be made to the Win32 native heap.
20162 */
20163 SQLITE_API int sqlite3_win32_reset_heap(){
20164 int rc;
20165 MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */
20166 MUTEX_LOGIC( sqlite3_mutex *pMem; ) /* The memsys static mutex */
20167 MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
20168 MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); )
20169 sqlite3_mutex_enter(pMaster);
20170 sqlite3_mutex_enter(pMem);
20171 winMemAssertMagic();
20172 if( winMemGetHeap()!=NULL && winMemGetOwned() && sqlite3_memory_used()==0 ){
20173 /*
20174 ** At this point, there should be no outstanding memory allocations on
20175 ** the heap. Also, since both the master and memsys locks are currently
20176 ** being held by us, no other function (i.e. from another thread) should
20177 ** be able to even access the heap. Attempt to destroy and recreate our
20178 ** isolated Win32 native heap now.
20179 */
20180 assert( winMemGetHeap()!=NULL );
20181 assert( winMemGetOwned() );
20182 assert( sqlite3_memory_used()==0 );
20183 winMemShutdown(winMemGetDataPtr());
20184 assert( winMemGetHeap()==NULL );
20185 assert( !winMemGetOwned() );
20186 assert( sqlite3_memory_used()==0 );
20187 rc = winMemInit(winMemGetDataPtr());
20188 assert( rc!=SQLITE_OK || winMemGetHeap()!=NULL );
20189 assert( rc!=SQLITE_OK || winMemGetOwned() );
20190 assert( rc!=SQLITE_OK || sqlite3_memory_used()==0 );
20191 }else{
20192 /*
20193 ** The Win32 native heap cannot be modified because it may be in use.
20194 */
20195 rc = SQLITE_BUSY;
20196 }
20197 sqlite3_mutex_leave(pMem);
20198 sqlite3_mutex_leave(pMaster);
20199 return rc;
20200 }
20201 #endif /* SQLITE_WIN32_MALLOC */
20202
20203 /*
20204 ** This function outputs the specified (ANSI) string to the Win32 debugger
20205 ** (if available).
20206 */
20207
20208 SQLITE_API void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
20209 char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
20210 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
20211 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
20212 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
20213 #ifdef SQLITE_ENABLE_API_ARMOR
20214 if( !zBuf ){
20215 (void)SQLITE_MISUSE_BKPT;
20216 return;
20217 }
20218 #endif
20219 #if defined(SQLITE_WIN32_HAS_ANSI)
20220 if( nMin>0 ){
20221 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
20222 memcpy(zDbgBuf, zBuf, nMin);
20223 osOutputDebugStringA(zDbgBuf);
20224 }else{
20225 osOutputDebugStringA(zBuf);
20226 }
20227 #elif defined(SQLITE_WIN32_HAS_WIDE)
20228 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
20229 if ( osMultiByteToWideChar(
20230 osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf,
20231 nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){
20232 return;
20233 }
20234 osOutputDebugStringW((LPCWSTR)zDbgBuf);
20235 #else
20236 if( nMin>0 ){
20237 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
20238 memcpy(zDbgBuf, zBuf, nMin);
20239 fprintf(stderr, "%s", zDbgBuf);
20240 }else{
20241 fprintf(stderr, "%s", zBuf);
20242 }
20243 #endif
20244 }
20245
20246 /*
20247 ** The following routine suspends the current thread for at least ms
20248 ** milliseconds. This is equivalent to the Win32 Sleep() interface.
20249 */
20250 #if SQLITE_OS_WINRT
20251 static HANDLE sleepObj = NULL;
20252 #endif
20253
20254 SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds){
20255 #if SQLITE_OS_WINRT
20256 if ( sleepObj==NULL ){
20257 sleepObj = osCreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET,
20258 SYNCHRONIZE);
20259 }
20260 assert( sleepObj!=NULL );
20261 osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE);
20262 #else
20263 osSleep(milliseconds);
20264 #endif
20265 }
20266
20267 #if SQLITE_MAX_WORKER_THREADS>0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
20268 SQLITE_THREADSAFE>0
20269 SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject){
20270 DWORD rc;
20271 while( (rc = osWaitForSingleObjectEx(hObject, INFINITE,
20272 TRUE))==WAIT_IO_COMPLETION ){}
20273 return rc;
20274 }
20275 #endif
20276
20277 /*
20278 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
20279 ** or WinCE. Return false (zero) for Win95, Win98, or WinME.
20280 **
20281 ** Here is an interesting observation: Win95, Win98, and WinME lack
20282 ** the LockFileEx() API. But we can still statically link against that
20283 ** API as long as we don't call it when running Win95/98/ME. A call to
20284 ** this routine is used to determine if the host is Win95/98/ME or
20285 ** WinNT/2K/XP so that we will know whether or not we can safely call
20286 ** the LockFileEx() API.
20287 */
20288
20289 #if !SQLITE_WIN32_GETVERSIONEX
20290 # define osIsNT() (1)
20291 #elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
20292 # define osIsNT() (1)
20293 #elif !defined(SQLITE_WIN32_HAS_WIDE)
20294 # define osIsNT() (0)
20295 #else
20296 # define osIsNT() ((sqlite3_os_type==2) || sqlite3_win32_is_nt())
20297 #endif
20298
20299 /*
20300 ** This function determines if the machine is running a version of Windows
20301 ** based on the NT kernel.
20302 */
20303 SQLITE_API int sqlite3_win32_is_nt(void){
20304 #if SQLITE_OS_WINRT
20305 /*
20306 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
20307 ** kernel.
20308 */
20309 return 1;
20310 #elif SQLITE_WIN32_GETVERSIONEX
20311 if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
20312 #if defined(SQLITE_WIN32_HAS_ANSI)
20313 OSVERSIONINFOA sInfo;
20314 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
20315 osGetVersionExA(&sInfo);
20316 osInterlockedCompareExchange(&sqlite3_os_type,
20317 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
20318 #elif defined(SQLITE_WIN32_HAS_WIDE)
20319 OSVERSIONINFOW sInfo;
20320 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
20321 osGetVersionExW(&sInfo);
20322 osInterlockedCompareExchange(&sqlite3_os_type,
20323 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
20324 #endif
20325 }
20326 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
20327 #elif SQLITE_TEST
20328 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
20329 #else
20330 /*
20331 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
20332 ** deprecated are always assumed to be based on the NT kernel.
20333 */
20334 return 1;
20335 #endif
20336 }
20337
20338 #ifdef SQLITE_WIN32_MALLOC
20339 /*
20340 ** Allocate nBytes of memory.
20341 */
20342 static void *winMemMalloc(int nBytes){
20343 HANDLE hHeap;
20344 void *p;
20345
20346 winMemAssertMagic();
20347 hHeap = winMemGetHeap();
20348 assert( hHeap!=0 );
20349 assert( hHeap!=INVALID_HANDLE_VALUE );
20350 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
20351 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
20352 #endif
20353 assert( nBytes>=0 );
20354 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
20355 if( !p ){
20356 sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%lu), heap=%p",
20357 nBytes, osGetLastError(), (void*)hHeap);
20358 }
20359 return p;
20360 }
20361
20362 /*
20363 ** Free memory.
20364 */
20365 static void winMemFree(void *pPrior){
20366 HANDLE hHeap;
20367
20368 winMemAssertMagic();
20369 hHeap = winMemGetHeap();
20370 assert( hHeap!=0 );
20371 assert( hHeap!=INVALID_HANDLE_VALUE );
20372 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
20373 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
20374 #endif
20375 if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
20376 if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
20377 sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%lu), heap=%p",
20378 pPrior, osGetLastError(), (void*)hHeap);
20379 }
20380 }
20381
20382 /*
20383 ** Change the size of an existing memory allocation
20384 */
20385 static void *winMemRealloc(void *pPrior, int nBytes){
20386 HANDLE hHeap;
20387 void *p;
20388
20389 winMemAssertMagic();
20390 hHeap = winMemGetHeap();
20391 assert( hHeap!=0 );
20392 assert( hHeap!=INVALID_HANDLE_VALUE );
20393 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
20394 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
20395 #endif
20396 assert( nBytes>=0 );
20397 if( !pPrior ){
20398 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
20399 }else{
20400 p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
20401 }
20402 if( !p ){
20403 sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%lu), heap=%p",
20404 pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
20405 (void*)hHeap);
20406 }
20407 return p;
20408 }
20409
20410 /*
20411 ** Return the size of an outstanding allocation, in bytes.
20412 */
20413 static int winMemSize(void *p){
20414 HANDLE hHeap;
20415 SIZE_T n;
20416
20417 winMemAssertMagic();
20418 hHeap = winMemGetHeap();
20419 assert( hHeap!=0 );
20420 assert( hHeap!=INVALID_HANDLE_VALUE );
20421 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
20422 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, p) );
20423 #endif
20424 if( !p ) return 0;
20425 n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
20426 if( n==(SIZE_T)-1 ){
20427 sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%lu), heap=%p",
20428 p, osGetLastError(), (void*)hHeap);
20429 return 0;
20430 }
20431 return (int)n;
20432 }
20433
20434 /*
20435 ** Round up a request size to the next valid allocation size.
20436 */
20437 static int winMemRoundup(int n){
20438 return n;
20439 }
20440
20441 /*
20442 ** Initialize this module.
20443 */
20444 static int winMemInit(void *pAppData){
20445 winMemData *pWinMemData = (winMemData *)pAppData;
20446
20447 if( !pWinMemData ) return SQLITE_ERROR;
20448 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
20449 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
20450
20451 #if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE
20452 if( !pWinMemData->hHeap ){
20453 DWORD dwInitialSize = SQLITE_WIN32_HEAP_INIT_SIZE;
20454 DWORD dwMaximumSize = (DWORD)sqlite3GlobalConfig.nHeap;
20455 if( dwMaximumSize==0 ){
20456 dwMaximumSize = SQLITE_WIN32_HEAP_MAX_SIZE;
20457 }else if( dwInitialSize>dwMaximumSize ){
20458 dwInitialSize = dwMaximumSize;
20459 }
20460 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
20461 dwInitialSize, dwMaximumSize);
20462 if( !pWinMemData->hHeap ){
20463 sqlite3_log(SQLITE_NOMEM,
20464 "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu",
20465 osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize,
20466 dwMaximumSize);
20467 return SQLITE_NOMEM_BKPT;
20468 }
20469 pWinMemData->bOwned = TRUE;
20470 assert( pWinMemData->bOwned );
20471 }
20472 #else
20473 pWinMemData->hHeap = osGetProcessHeap();
20474 if( !pWinMemData->hHeap ){
20475 sqlite3_log(SQLITE_NOMEM,
20476 "failed to GetProcessHeap (%lu)", osGetLastError());
20477 return SQLITE_NOMEM_BKPT;
20478 }
20479 pWinMemData->bOwned = FALSE;
20480 assert( !pWinMemData->bOwned );
20481 #endif
20482 assert( pWinMemData->hHeap!=0 );
20483 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
20484 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
20485 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
20486 #endif
20487 return SQLITE_OK;
20488 }
20489
20490 /*
20491 ** Deinitialize this module.
20492 */
20493 static void winMemShutdown(void *pAppData){
20494 winMemData *pWinMemData = (winMemData *)pAppData;
20495
20496 if( !pWinMemData ) return;
20497 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
20498 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
20499
20500 if( pWinMemData->hHeap ){
20501 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
20502 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
20503 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
20504 #endif
20505 if( pWinMemData->bOwned ){
20506 if( !osHeapDestroy(pWinMemData->hHeap) ){
20507 sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%lu), heap=%p",
20508 osGetLastError(), (void*)pWinMemData->hHeap);
20509 }
20510 pWinMemData->bOwned = FALSE;
20511 }
20512 pWinMemData->hHeap = NULL;
20513 }
20514 }
20515
20516 /*
20517 ** Populate the low-level memory allocation function pointers in
20518 ** sqlite3GlobalConfig.m with pointers to the routines in this file. The
20519 ** arguments specify the block of memory to manage.
20520 **
20521 ** This routine is only called by sqlite3_config(), and therefore
20522 ** is not required to be threadsafe (it is not).
20523 */
20524 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){
20525 static const sqlite3_mem_methods winMemMethods = {
20526 winMemMalloc,
20527 winMemFree,
20528 winMemRealloc,
20529 winMemSize,
20530 winMemRoundup,
20531 winMemInit,
20532 winMemShutdown,
20533 &win_mem_data
20534 };
20535 return &winMemMethods;
20536 }
20537
20538 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
20539 sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
20540 }
20541 #endif /* SQLITE_WIN32_MALLOC */
20542
20543 /*
20544 ** Convert a UTF-8 string to Microsoft Unicode.
20545 **
20546 ** Space to hold the returned string is obtained from sqlite3_malloc().
20547 */
20548 static LPWSTR winUtf8ToUnicode(const char *zText){
20549 int nChar;
20550 LPWSTR zWideText;
20551
20552 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
20553 if( nChar==0 ){
20554 return 0;
20555 }
20556 zWideText = sqlite3MallocZero( nChar*sizeof(WCHAR) );
20557 if( zWideText==0 ){
20558 return 0;
20559 }
20560 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
20561 nChar);
20562 if( nChar==0 ){
20563 sqlite3_free(zWideText);
20564 zWideText = 0;
20565 }
20566 return zWideText;
20567 }
20568
20569 /*
20570 ** Convert a Microsoft Unicode string to UTF-8.
20571 **
20572 ** Space to hold the returned string is obtained from sqlite3_malloc().
20573 */
20574 static char *winUnicodeToUtf8(LPCWSTR zWideText){
20575 int nByte;
20576 char *zText;
20577
20578 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
20579 if( nByte == 0 ){
20580 return 0;
20581 }
20582 zText = sqlite3MallocZero( nByte );
20583 if( zText==0 ){
20584 return 0;
20585 }
20586 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
20587 0, 0);
20588 if( nByte == 0 ){
20589 sqlite3_free(zText);
20590 zText = 0;
20591 }
20592 return zText;
20593 }
20594
20595 /*
20596 ** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM
20597 ** code page.
20598 **
20599 ** Space to hold the returned string is obtained from sqlite3_malloc().
20600 */
20601 static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){
20602 int nByte;
20603 LPWSTR zMbcsText;
20604 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
20605
20606 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
20607 0)*sizeof(WCHAR);
20608 if( nByte==0 ){
20609 return 0;
20610 }
20611 zMbcsText = sqlite3MallocZero( nByte*sizeof(WCHAR) );
20612 if( zMbcsText==0 ){
20613 return 0;
20614 }
20615 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
20616 nByte);
20617 if( nByte==0 ){
20618 sqlite3_free(zMbcsText);
20619 zMbcsText = 0;
20620 }
20621 return zMbcsText;
20622 }
20623
20624 /*
20625 ** Convert a Microsoft Unicode string to a multi-byte character string,
20626 ** using the ANSI or OEM code page.
20627 **
20628 ** Space to hold the returned string is obtained from sqlite3_malloc().
20629 */
20630 static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){
20631 int nByte;
20632 char *zText;
20633 int codepage = useAnsi ? CP_ACP : CP_OEMCP;
20634
20635 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
20636 if( nByte == 0 ){
20637 return 0;
20638 }
20639 zText = sqlite3MallocZero( nByte );
20640 if( zText==0 ){
20641 return 0;
20642 }
20643 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
20644 nByte, 0, 0);
20645 if( nByte == 0 ){
20646 sqlite3_free(zText);
20647 zText = 0;
20648 }
20649 return zText;
20650 }
20651
20652 /*
20653 ** Convert a multi-byte character string to UTF-8.
20654 **
20655 ** Space to hold the returned string is obtained from sqlite3_malloc().
20656 */
20657 static char *winMbcsToUtf8(const char *zText, int useAnsi){
20658 char *zTextUtf8;
20659 LPWSTR zTmpWide;
20660
20661 zTmpWide = winMbcsToUnicode(zText, useAnsi);
20662 if( zTmpWide==0 ){
20663 return 0;
20664 }
20665 zTextUtf8 = winUnicodeToUtf8(zTmpWide);
20666 sqlite3_free(zTmpWide);
20667 return zTextUtf8;
20668 }
20669
20670 /*
20671 ** Convert a UTF-8 string to a multi-byte character string.
20672 **
20673 ** Space to hold the returned string is obtained from sqlite3_malloc().
20674 */
20675 static char *winUtf8ToMbcs(const char *zText, int useAnsi){
20676 char *zTextMbcs;
20677 LPWSTR zTmpWide;
20678
20679 zTmpWide = winUtf8ToUnicode(zText);
20680 if( zTmpWide==0 ){
20681 return 0;
20682 }
20683 zTextMbcs = winUnicodeToMbcs(zTmpWide, useAnsi);
20684 sqlite3_free(zTmpWide);
20685 return zTextMbcs;
20686 }
20687
20688 /*
20689 ** This is a public wrapper for the winUtf8ToUnicode() function.
20690 */
20691 SQLITE_API LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText){
20692 #ifdef SQLITE_ENABLE_API_ARMOR
20693 if( !zText ){
20694 (void)SQLITE_MISUSE_BKPT;
20695 return 0;
20696 }
20697 #endif
20698 #ifndef SQLITE_OMIT_AUTOINIT
20699 if( sqlite3_initialize() ) return 0;
20700 #endif
20701 return winUtf8ToUnicode(zText);
20702 }
20703
20704 /*
20705 ** This is a public wrapper for the winUnicodeToUtf8() function.
20706 */
20707 SQLITE_API char *sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){
20708 #ifdef SQLITE_ENABLE_API_ARMOR
20709 if( !zWideText ){
20710 (void)SQLITE_MISUSE_BKPT;
20711 return 0;
20712 }
20713 #endif
20714 #ifndef SQLITE_OMIT_AUTOINIT
20715 if( sqlite3_initialize() ) return 0;
20716 #endif
20717 return winUnicodeToUtf8(zWideText);
20718 }
20719
20720 /*
20721 ** This is a public wrapper for the winMbcsToUtf8() function.
20722 */
20723 SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zText){
20724 #ifdef SQLITE_ENABLE_API_ARMOR
20725 if( !zText ){
20726 (void)SQLITE_MISUSE_BKPT;
20727 return 0;
20728 }
20729 #endif
20730 #ifndef SQLITE_OMIT_AUTOINIT
20731 if( sqlite3_initialize() ) return 0;
20732 #endif
20733 return winMbcsToUtf8(zText, osAreFileApisANSI());
20734 }
20735
20736 /*
20737 ** This is a public wrapper for the winMbcsToUtf8() function.
20738 */
20739 SQLITE_API char *sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){
20740 #ifdef SQLITE_ENABLE_API_ARMOR
20741 if( !zText ){
20742 (void)SQLITE_MISUSE_BKPT;
20743 return 0;
20744 }
20745 #endif
20746 #ifndef SQLITE_OMIT_AUTOINIT
20747 if( sqlite3_initialize() ) return 0;
20748 #endif
20749 return winMbcsToUtf8(zText, useAnsi);
20750 }
20751
20752 /*
20753 ** This is a public wrapper for the winUtf8ToMbcs() function.
20754 */
20755 SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zText){
20756 #ifdef SQLITE_ENABLE_API_ARMOR
20757 if( !zText ){
20758 (void)SQLITE_MISUSE_BKPT;
20759 return 0;
20760 }
20761 #endif
20762 #ifndef SQLITE_OMIT_AUTOINIT
20763 if( sqlite3_initialize() ) return 0;
20764 #endif
20765 return winUtf8ToMbcs(zText, osAreFileApisANSI());
20766 }
20767
20768 /*
20769 ** This is a public wrapper for the winUtf8ToMbcs() function.
20770 */
20771 SQLITE_API char *sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){
20772 #ifdef SQLITE_ENABLE_API_ARMOR
20773 if( !zText ){
20774 (void)SQLITE_MISUSE_BKPT;
20775 return 0;
20776 }
20777 #endif
20778 #ifndef SQLITE_OMIT_AUTOINIT
20779 if( sqlite3_initialize() ) return 0;
20780 #endif
20781 return winUtf8ToMbcs(zText, useAnsi);
20782 }
20783
20784 /*
20785 ** This function sets the data directory or the temporary directory based on
20786 ** the provided arguments. The type argument must be 1 in order to set the
20787 ** data directory or 2 in order to set the temporary directory. The zValue
20788 ** argument is the name of the directory to use. The return value will be
20789 ** SQLITE_OK if successful.
20790 */
20791 SQLITE_API int sqlite3_win32_set_directory(DWORD type, LPCWSTR zValue){
20792 char **ppDirectory = 0;
20793 #ifndef SQLITE_OMIT_AUTOINIT
20794 int rc = sqlite3_initialize();
20795 if( rc ) return rc;
20796 #endif
20797 if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
20798 ppDirectory = &sqlite3_data_directory;
20799 }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
20800 ppDirectory = &sqlite3_temp_directory;
20801 }
20802 assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
20803 || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
20804 );
20805 assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
20806 if( ppDirectory ){
20807 char *zValueUtf8 = 0;
20808 if( zValue && zValue[0] ){
20809 zValueUtf8 = winUnicodeToUtf8(zValue);
20810 if ( zValueUtf8==0 ){
20811 return SQLITE_NOMEM_BKPT;
20812 }
20813 }
20814 sqlite3_free(*ppDirectory);
20815 *ppDirectory = zValueUtf8;
20816 return SQLITE_OK;
20817 }
20818 return SQLITE_ERROR;
20819 }
20820
20821 /*
20822 ** The return value of winGetLastErrorMsg
20823 ** is zero if the error message fits in the buffer, or non-zero
20824 ** otherwise (if the message was truncated).
20825 */
20826 static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
20827 /* FormatMessage returns 0 on failure. Otherwise it
20828 ** returns the number of TCHARs written to the output
20829 ** buffer, excluding the terminating null char.
20830 */
20831 DWORD dwLen = 0;
20832 char *zOut = 0;
20833
20834 if( osIsNT() ){
20835 #if SQLITE_OS_WINRT
20836 WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
20837 dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
20838 FORMAT_MESSAGE_IGNORE_INSERTS,
20839 NULL,
20840 lastErrno,
20841 0,
20842 zTempWide,
20843 SQLITE_WIN32_MAX_ERRMSG_CHARS,
20844 0);
20845 #else
20846 LPWSTR zTempWide = NULL;
20847 dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
20848 FORMAT_MESSAGE_FROM_SYSTEM |
20849 FORMAT_MESSAGE_IGNORE_INSERTS,
20850 NULL,
20851 lastErrno,
20852 0,
20853 (LPWSTR) &zTempWide,
20854 0,
20855 0);
20856 #endif
20857 if( dwLen > 0 ){
20858 /* allocate a buffer and convert to UTF8 */
20859 sqlite3BeginBenignMalloc();
20860 zOut = winUnicodeToUtf8(zTempWide);
20861 sqlite3EndBenignMalloc();
20862 #if !SQLITE_OS_WINRT
20863 /* free the system buffer allocated by FormatMessage */
20864 osLocalFree(zTempWide);
20865 #endif
20866 }
20867 }
20868 #ifdef SQLITE_WIN32_HAS_ANSI
20869 else{
20870 char *zTemp = NULL;
20871 dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
20872 FORMAT_MESSAGE_FROM_SYSTEM |
20873 FORMAT_MESSAGE_IGNORE_INSERTS,
20874 NULL,
20875 lastErrno,
20876 0,
20877 (LPSTR) &zTemp,
20878 0,
20879 0);
20880 if( dwLen > 0 ){
20881 /* allocate a buffer and convert to UTF8 */
20882 sqlite3BeginBenignMalloc();
20883 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
20884 sqlite3EndBenignMalloc();
20885 /* free the system buffer allocated by FormatMessage */
20886 osLocalFree(zTemp);
20887 }
20888 }
20889 #endif
20890 if( 0 == dwLen ){
20891 sqlite3_snprintf(nBuf, zBuf, "OsError 0x%lx (%lu)", lastErrno, lastErrno);
20892 }else{
20893 /* copy a maximum of nBuf chars to output buffer */
20894 sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
20895 /* free the UTF8 buffer */
20896 sqlite3_free(zOut);
20897 }
20898 return 0;
20899 }
20900
20901 /*
20902 **
20903 ** This function - winLogErrorAtLine() - is only ever called via the macro
20904 ** winLogError().
20905 **
20906 ** This routine is invoked after an error occurs in an OS function.
20907 ** It logs a message using sqlite3_log() containing the current value of
20908 ** error code and, if possible, the human-readable equivalent from
20909 ** FormatMessage.
20910 **
20911 ** The first argument passed to the macro should be the error code that
20912 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
20913 ** The two subsequent arguments should be the name of the OS function that
20914 ** failed and the associated file-system path, if any.
20915 */
20916 #define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__)
20917 static int winLogErrorAtLine(
20918 int errcode, /* SQLite error code */
20919 DWORD lastErrno, /* Win32 last error */
20920 const char *zFunc, /* Name of OS function that failed */
20921 const char *zPath, /* File path associated with error */
20922 int iLine /* Source line number where error occurred */
20923 ){
20924 char zMsg[500]; /* Human readable error text */
20925 int i; /* Loop counter */
20926
20927 zMsg[0] = 0;
20928 winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
20929 assert( errcode!=SQLITE_OK );
20930 if( zPath==0 ) zPath = "";
20931 for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
20932 zMsg[i] = 0;
20933 sqlite3_log(errcode,
20934 "os_win.c:%d: (%lu) %s(%s) - %s",
20935 iLine, lastErrno, zFunc, zPath, zMsg
20936 );
20937
20938 return errcode;
20939 }
20940
20941 /*
20942 ** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
20943 ** will be retried following a locking error - probably caused by
20944 ** antivirus software. Also the initial delay before the first retry.
20945 ** The delay increases linearly with each retry.
20946 */
20947 #ifndef SQLITE_WIN32_IOERR_RETRY
20948 # define SQLITE_WIN32_IOERR_RETRY 10
20949 #endif
20950 #ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
20951 # define SQLITE_WIN32_IOERR_RETRY_DELAY 25
20952 #endif
20953 static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
20954 static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
20955
20956 /*
20957 ** The "winIoerrCanRetry1" macro is used to determine if a particular I/O
20958 ** error code obtained via GetLastError() is eligible to be retried. It
20959 ** must accept the error code DWORD as its only argument and should return
20960 ** non-zero if the error code is transient in nature and the operation
20961 ** responsible for generating the original error might succeed upon being
20962 ** retried. The argument to this macro should be a variable.
20963 **
20964 ** Additionally, a macro named "winIoerrCanRetry2" may be defined. If it
20965 ** is defined, it will be consulted only when the macro "winIoerrCanRetry1"
20966 ** returns zero. The "winIoerrCanRetry2" macro is completely optional and
20967 ** may be used to include additional error codes in the set that should
20968 ** result in the failing I/O operation being retried by the caller. If
20969 ** defined, the "winIoerrCanRetry2" macro must exhibit external semantics
20970 ** identical to those of the "winIoerrCanRetry1" macro.
20971 */
20972 #if !defined(winIoerrCanRetry1)
20973 #define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED) || \
20974 ((a)==ERROR_SHARING_VIOLATION) || \
20975 ((a)==ERROR_LOCK_VIOLATION) || \
20976 ((a)==ERROR_DEV_NOT_EXIST) || \
20977 ((a)==ERROR_NETNAME_DELETED) || \
20978 ((a)==ERROR_SEM_TIMEOUT) || \
20979 ((a)==ERROR_NETWORK_UNREACHABLE))
20980 #endif
20981
20982 /*
20983 ** If a ReadFile() or WriteFile() error occurs, invoke this routine
20984 ** to see if it should be retried. Return TRUE to retry. Return FALSE
20985 ** to give up with an error.
20986 */
20987 static int winRetryIoerr(int *pnRetry, DWORD *pError){
20988 DWORD e = osGetLastError();
20989 if( *pnRetry>=winIoerrRetry ){
20990 if( pError ){
20991 *pError = e;
20992 }
20993 return 0;
20994 }
20995 if( winIoerrCanRetry1(e) ){
20996 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
20997 ++*pnRetry;
20998 return 1;
20999 }
21000 #if defined(winIoerrCanRetry2)
21001 else if( winIoerrCanRetry2(e) ){
21002 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
21003 ++*pnRetry;
21004 return 1;
21005 }
21006 #endif
21007 if( pError ){
21008 *pError = e;
21009 }
21010 return 0;
21011 }
21012
21013 /*
21014 ** Log a I/O error retry episode.
21015 */
21016 static void winLogIoerr(int nRetry, int lineno){
21017 if( nRetry ){
21018 sqlite3_log(SQLITE_NOTICE,
21019 "delayed %dms for lock/sharing conflict at line %d",
21020 winIoerrRetryDelay*nRetry*(nRetry+1)/2, lineno
21021 );
21022 }
21023 }
21024
21025 /*
21026 ** This #if does not rely on the SQLITE_OS_WINCE define because the
21027 ** corresponding section in "date.c" cannot use it.
21028 */
21029 #if !defined(SQLITE_OMIT_LOCALTIME) && defined(_WIN32_WCE) && \
21030 (!defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API)
21031 /*
21032 ** The MSVC CRT on Windows CE may not have a localtime() function.
21033 ** So define a substitute.
21034 */
21035 /* # include <time.h> */
21036 struct tm *__cdecl localtime(const time_t *t)
21037 {
21038 static struct tm y;
21039 FILETIME uTm, lTm;
21040 SYSTEMTIME pTm;
21041 sqlite3_int64 t64;
21042 t64 = *t;
21043 t64 = (t64 + 11644473600)*10000000;
21044 uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
21045 uTm.dwHighDateTime= (DWORD)(t64 >> 32);
21046 osFileTimeToLocalFileTime(&uTm,&lTm);
21047 osFileTimeToSystemTime(&lTm,&pTm);
21048 y.tm_year = pTm.wYear - 1900;
21049 y.tm_mon = pTm.wMonth - 1;
21050 y.tm_wday = pTm.wDayOfWeek;
21051 y.tm_mday = pTm.wDay;
21052 y.tm_hour = pTm.wHour;
21053 y.tm_min = pTm.wMinute;
21054 y.tm_sec = pTm.wSecond;
21055 return &y;
21056 }
21057 #endif
21058
21059 #if SQLITE_OS_WINCE
21060 /*************************************************************************
21061 ** This section contains code for WinCE only.
21062 */
21063 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
21064
21065 /*
21066 ** Acquire a lock on the handle h
21067 */
21068 static void winceMutexAcquire(HANDLE h){
21069 DWORD dwErr;
21070 do {
21071 dwErr = osWaitForSingleObject(h, INFINITE);
21072 } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
21073 }
21074 /*
21075 ** Release a lock acquired by winceMutexAcquire()
21076 */
21077 #define winceMutexRelease(h) ReleaseMutex(h)
21078
21079 /*
21080 ** Create the mutex and shared memory used for locking in the file
21081 ** descriptor pFile
21082 */
21083 static int winceCreateLock(const char *zFilename, winFile *pFile){
21084 LPWSTR zTok;
21085 LPWSTR zName;
21086 DWORD lastErrno;
21087 BOOL bLogged = FALSE;
21088 BOOL bInit = TRUE;
21089
21090 zName = winUtf8ToUnicode(zFilename);
21091 if( zName==0 ){
21092 /* out of memory */
21093 return SQLITE_IOERR_NOMEM_BKPT;
21094 }
21095
21096 /* Initialize the local lockdata */
21097 memset(&pFile->local, 0, sizeof(pFile->local));
21098
21099 /* Replace the backslashes from the filename and lowercase it
21100 ** to derive a mutex name. */
21101 zTok = osCharLowerW(zName);
21102 for (;*zTok;zTok++){
21103 if (*zTok == '\\') *zTok = '_';
21104 }
21105
21106 /* Create/open the named mutex */
21107 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
21108 if (!pFile->hMutex){
21109 pFile->lastErrno = osGetLastError();
21110 sqlite3_free(zName);
21111 return winLogError(SQLITE_IOERR, pFile->lastErrno,
21112 "winceCreateLock1", zFilename);
21113 }
21114
21115 /* Acquire the mutex before continuing */
21116 winceMutexAcquire(pFile->hMutex);
21117
21118 /* Since the names of named mutexes, semaphores, file mappings etc are
21119 ** case-sensitive, take advantage of that by uppercasing the mutex name
21120 ** and using that as the shared filemapping name.
21121 */
21122 osCharUpperW(zName);
21123 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
21124 PAGE_READWRITE, 0, sizeof(winceLock),
21125 zName);
21126
21127 /* Set a flag that indicates we're the first to create the memory so it
21128 ** must be zero-initialized */
21129 lastErrno = osGetLastError();
21130 if (lastErrno == ERROR_ALREADY_EXISTS){
21131 bInit = FALSE;
21132 }
21133
21134 sqlite3_free(zName);
21135
21136 /* If we succeeded in making the shared memory handle, map it. */
21137 if( pFile->hShared ){
21138 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
21139 FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
21140 /* If mapping failed, close the shared memory handle and erase it */
21141 if( !pFile->shared ){
21142 pFile->lastErrno = osGetLastError();
21143 winLogError(SQLITE_IOERR, pFile->lastErrno,
21144 "winceCreateLock2", zFilename);
21145 bLogged = TRUE;
21146 osCloseHandle(pFile->hShared);
21147 pFile->hShared = NULL;
21148 }
21149 }
21150
21151 /* If shared memory could not be created, then close the mutex and fail */
21152 if( pFile->hShared==NULL ){
21153 if( !bLogged ){
21154 pFile->lastErrno = lastErrno;
21155 winLogError(SQLITE_IOERR, pFile->lastErrno,
21156 "winceCreateLock3", zFilename);
21157 bLogged = TRUE;
21158 }
21159 winceMutexRelease(pFile->hMutex);
21160 osCloseHandle(pFile->hMutex);
21161 pFile->hMutex = NULL;
21162 return SQLITE_IOERR;
21163 }
21164
21165 /* Initialize the shared memory if we're supposed to */
21166 if( bInit ){
21167 memset(pFile->shared, 0, sizeof(winceLock));
21168 }
21169
21170 winceMutexRelease(pFile->hMutex);
21171 return SQLITE_OK;
21172 }
21173
21174 /*
21175 ** Destroy the part of winFile that deals with wince locks
21176 */
21177 static void winceDestroyLock(winFile *pFile){
21178 if (pFile->hMutex){
21179 /* Acquire the mutex */
21180 winceMutexAcquire(pFile->hMutex);
21181
21182 /* The following blocks should probably assert in debug mode, but they
21183 are to cleanup in case any locks remained open */
21184 if (pFile->local.nReaders){
21185 pFile->shared->nReaders --;
21186 }
21187 if (pFile->local.bReserved){
21188 pFile->shared->bReserved = FALSE;
21189 }
21190 if (pFile->local.bPending){
21191 pFile->shared->bPending = FALSE;
21192 }
21193 if (pFile->local.bExclusive){
21194 pFile->shared->bExclusive = FALSE;
21195 }
21196
21197 /* De-reference and close our copy of the shared memory handle */
21198 osUnmapViewOfFile(pFile->shared);
21199 osCloseHandle(pFile->hShared);
21200
21201 /* Done with the mutex */
21202 winceMutexRelease(pFile->hMutex);
21203 osCloseHandle(pFile->hMutex);
21204 pFile->hMutex = NULL;
21205 }
21206 }
21207
21208 /*
21209 ** An implementation of the LockFile() API of Windows for CE
21210 */
21211 static BOOL winceLockFile(
21212 LPHANDLE phFile,
21213 DWORD dwFileOffsetLow,
21214 DWORD dwFileOffsetHigh,
21215 DWORD nNumberOfBytesToLockLow,
21216 DWORD nNumberOfBytesToLockHigh
21217 ){
21218 winFile *pFile = HANDLE_TO_WINFILE(phFile);
21219 BOOL bReturn = FALSE;
21220
21221 UNUSED_PARAMETER(dwFileOffsetHigh);
21222 UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
21223
21224 if (!pFile->hMutex) return TRUE;
21225 winceMutexAcquire(pFile->hMutex);
21226
21227 /* Wanting an exclusive lock? */
21228 if (dwFileOffsetLow == (DWORD)SHARED_FIRST
21229 && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
21230 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
21231 pFile->shared->bExclusive = TRUE;
21232 pFile->local.bExclusive = TRUE;
21233 bReturn = TRUE;
21234 }
21235 }
21236
21237 /* Want a read-only lock? */
21238 else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
21239 nNumberOfBytesToLockLow == 1){
21240 if (pFile->shared->bExclusive == 0){
21241 pFile->local.nReaders ++;
21242 if (pFile->local.nReaders == 1){
21243 pFile->shared->nReaders ++;
21244 }
21245 bReturn = TRUE;
21246 }
21247 }
21248
21249 /* Want a pending lock? */
21250 else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
21251 && nNumberOfBytesToLockLow == 1){
21252 /* If no pending lock has been acquired, then acquire it */
21253 if (pFile->shared->bPending == 0) {
21254 pFile->shared->bPending = TRUE;
21255 pFile->local.bPending = TRUE;
21256 bReturn = TRUE;
21257 }
21258 }
21259
21260 /* Want a reserved lock? */
21261 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
21262 && nNumberOfBytesToLockLow == 1){
21263 if (pFile->shared->bReserved == 0) {
21264 pFile->shared->bReserved = TRUE;
21265 pFile->local.bReserved = TRUE;
21266 bReturn = TRUE;
21267 }
21268 }
21269
21270 winceMutexRelease(pFile->hMutex);
21271 return bReturn;
21272 }
21273
21274 /*
21275 ** An implementation of the UnlockFile API of Windows for CE
21276 */
21277 static BOOL winceUnlockFile(
21278 LPHANDLE phFile,
21279 DWORD dwFileOffsetLow,
21280 DWORD dwFileOffsetHigh,
21281 DWORD nNumberOfBytesToUnlockLow,
21282 DWORD nNumberOfBytesToUnlockHigh
21283 ){
21284 winFile *pFile = HANDLE_TO_WINFILE(phFile);
21285 BOOL bReturn = FALSE;
21286
21287 UNUSED_PARAMETER(dwFileOffsetHigh);
21288 UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
21289
21290 if (!pFile->hMutex) return TRUE;
21291 winceMutexAcquire(pFile->hMutex);
21292
21293 /* Releasing a reader lock or an exclusive lock */
21294 if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
21295 /* Did we have an exclusive lock? */
21296 if (pFile->local.bExclusive){
21297 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
21298 pFile->local.bExclusive = FALSE;
21299 pFile->shared->bExclusive = FALSE;
21300 bReturn = TRUE;
21301 }
21302
21303 /* Did we just have a reader lock? */
21304 else if (pFile->local.nReaders){
21305 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE
21306 || nNumberOfBytesToUnlockLow == 1);
21307 pFile->local.nReaders --;
21308 if (pFile->local.nReaders == 0)
21309 {
21310 pFile->shared->nReaders --;
21311 }
21312 bReturn = TRUE;
21313 }
21314 }
21315
21316 /* Releasing a pending lock */
21317 else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
21318 && nNumberOfBytesToUnlockLow == 1){
21319 if (pFile->local.bPending){
21320 pFile->local.bPending = FALSE;
21321 pFile->shared->bPending = FALSE;
21322 bReturn = TRUE;
21323 }
21324 }
21325 /* Releasing a reserved lock */
21326 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
21327 && nNumberOfBytesToUnlockLow == 1){
21328 if (pFile->local.bReserved) {
21329 pFile->local.bReserved = FALSE;
21330 pFile->shared->bReserved = FALSE;
21331 bReturn = TRUE;
21332 }
21333 }
21334
21335 winceMutexRelease(pFile->hMutex);
21336 return bReturn;
21337 }
21338 /*
21339 ** End of the special code for wince
21340 *****************************************************************************/
21341 #endif /* SQLITE_OS_WINCE */
21342
21343 /*
21344 ** Lock a file region.
21345 */
21346 static BOOL winLockFile(
21347 LPHANDLE phFile,
21348 DWORD flags,
21349 DWORD offsetLow,
21350 DWORD offsetHigh,
21351 DWORD numBytesLow,
21352 DWORD numBytesHigh
21353 ){
21354 #if SQLITE_OS_WINCE
21355 /*
21356 ** NOTE: Windows CE is handled differently here due its lack of the Win32
21357 ** API LockFile.
21358 */
21359 return winceLockFile(phFile, offsetLow, offsetHigh,
21360 numBytesLow, numBytesHigh);
21361 #else
21362 if( osIsNT() ){
21363 OVERLAPPED ovlp;
21364 memset(&ovlp, 0, sizeof(OVERLAPPED));
21365 ovlp.Offset = offsetLow;
21366 ovlp.OffsetHigh = offsetHigh;
21367 return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
21368 }else{
21369 return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
21370 numBytesHigh);
21371 }
21372 #endif
21373 }
21374
21375 /*
21376 ** Unlock a file region.
21377 */
21378 static BOOL winUnlockFile(
21379 LPHANDLE phFile,
21380 DWORD offsetLow,
21381 DWORD offsetHigh,
21382 DWORD numBytesLow,
21383 DWORD numBytesHigh
21384 ){
21385 #if SQLITE_OS_WINCE
21386 /*
21387 ** NOTE: Windows CE is handled differently here due its lack of the Win32
21388 ** API UnlockFile.
21389 */
21390 return winceUnlockFile(phFile, offsetLow, offsetHigh,
21391 numBytesLow, numBytesHigh);
21392 #else
21393 if( osIsNT() ){
21394 OVERLAPPED ovlp;
21395 memset(&ovlp, 0, sizeof(OVERLAPPED));
21396 ovlp.Offset = offsetLow;
21397 ovlp.OffsetHigh = offsetHigh;
21398 return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
21399 }else{
21400 return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
21401 numBytesHigh);
21402 }
21403 #endif
21404 }
21405
21406 /*****************************************************************************
21407 ** The next group of routines implement the I/O methods specified
21408 ** by the sqlite3_io_methods object.
21409 ******************************************************************************/
21410
21411 /*
21412 ** Some Microsoft compilers lack this definition.
21413 */
21414 #ifndef INVALID_SET_FILE_POINTER
21415 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
21416 #endif
21417
21418 /*
21419 ** Move the current position of the file handle passed as the first
21420 ** argument to offset iOffset within the file. If successful, return 0.
21421 ** Otherwise, set pFile->lastErrno and return non-zero.
21422 */
21423 static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
21424 #if !SQLITE_OS_WINRT
21425 LONG upperBits; /* Most sig. 32 bits of new offset */
21426 LONG lowerBits; /* Least sig. 32 bits of new offset */
21427 DWORD dwRet; /* Value returned by SetFilePointer() */
21428 DWORD lastErrno; /* Value returned by GetLastError() */
21429
21430 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
21431
21432 upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
21433 lowerBits = (LONG)(iOffset & 0xffffffff);
21434
21435 /* API oddity: If successful, SetFilePointer() returns a dword
21436 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
21437 ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
21438 ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
21439 ** whether an error has actually occurred, it is also necessary to call
21440 ** GetLastError().
21441 */
21442 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
21443
21444 if( (dwRet==INVALID_SET_FILE_POINTER
21445 && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
21446 pFile->lastErrno = lastErrno;
21447 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
21448 "winSeekFile", pFile->zPath);
21449 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
21450 return 1;
21451 }
21452
21453 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
21454 return 0;
21455 #else
21456 /*
21457 ** Same as above, except that this implementation works for WinRT.
21458 */
21459
21460 LARGE_INTEGER x; /* The new offset */
21461 BOOL bRet; /* Value returned by SetFilePointerEx() */
21462
21463 x.QuadPart = iOffset;
21464 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
21465
21466 if(!bRet){
21467 pFile->lastErrno = osGetLastError();
21468 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
21469 "winSeekFile", pFile->zPath);
21470 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
21471 return 1;
21472 }
21473
21474 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
21475 return 0;
21476 #endif
21477 }
21478
21479 #if SQLITE_MAX_MMAP_SIZE>0
21480 /* Forward references to VFS helper methods used for memory mapped files */
21481 static int winMapfile(winFile*, sqlite3_int64);
21482 static int winUnmapfile(winFile*);
21483 #endif
21484
21485 /*
21486 ** Close a file.
21487 **
21488 ** It is reported that an attempt to close a handle might sometimes
21489 ** fail. This is a very unreasonable result, but Windows is notorious
21490 ** for being unreasonable so I do not doubt that it might happen. If
21491 ** the close fails, we pause for 100 milliseconds and try again. As
21492 ** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
21493 ** giving up and returning an error.
21494 */
21495 #define MX_CLOSE_ATTEMPT 3
21496 static int winClose(sqlite3_file *id){
21497 int rc, cnt = 0;
21498 winFile *pFile = (winFile*)id;
21499
21500 assert( id!=0 );
21501 #ifndef SQLITE_OMIT_WAL
21502 assert( pFile->pShm==0 );
21503 #endif
21504 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
21505 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p\n",
21506 osGetCurrentProcessId(), pFile, pFile->h));
21507
21508 #if SQLITE_MAX_MMAP_SIZE>0
21509 winUnmapfile(pFile);
21510 #endif
21511
21512 do{
21513 rc = osCloseHandle(pFile->h);
21514 /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
21515 }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
21516 #if SQLITE_OS_WINCE
21517 #define WINCE_DELETION_ATTEMPTS 3
21518 {
21519 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
21520 if( pAppData==NULL || !pAppData->bNoLock ){
21521 winceDestroyLock(pFile);
21522 }
21523 }
21524 if( pFile->zDeleteOnClose ){
21525 int cnt = 0;
21526 while(
21527 osDeleteFileW(pFile->zDeleteOnClose)==0
21528 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
21529 && cnt++ < WINCE_DELETION_ATTEMPTS
21530 ){
21531 sqlite3_win32_sleep(100); /* Wait a little before trying again */
21532 }
21533 sqlite3_free(pFile->zDeleteOnClose);
21534 }
21535 #endif
21536 if( rc ){
21537 pFile->h = NULL;
21538 }
21539 OpenCounter(-1);
21540 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p, rc=%s\n",
21541 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
21542 return rc ? SQLITE_OK
21543 : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
21544 "winClose", pFile->zPath);
21545 }
21546
21547 /*
21548 ** Read data from a file into a buffer. Return SQLITE_OK if all
21549 ** bytes were read successfully and SQLITE_IOERR if anything goes
21550 ** wrong.
21551 */
21552 static int winRead(
21553 sqlite3_file *id, /* File to read from */
21554 void *pBuf, /* Write content into this buffer */
21555 int amt, /* Number of bytes to read */
21556 sqlite3_int64 offset /* Begin reading at this offset */
21557 ){
21558 #if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
21559 OVERLAPPED overlapped; /* The offset for ReadFile. */
21560 #endif
21561 winFile *pFile = (winFile*)id; /* file handle */
21562 DWORD nRead; /* Number of bytes actually read from file */
21563 int nRetry = 0; /* Number of retrys */
21564
21565 assert( id!=0 );
21566 assert( amt>0 );
21567 assert( offset>=0 );
21568 SimulateIOError(return SQLITE_IOERR_READ);
21569 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
21570 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
21571 pFile->h, pBuf, amt, offset, pFile->locktype));
21572
21573 #if SQLITE_MAX_MMAP_SIZE>0
21574 /* Deal with as much of this read request as possible by transfering
21575 ** data from the memory mapping using memcpy(). */
21576 if( offset<pFile->mmapSize ){
21577 if( offset+amt <= pFile->mmapSize ){
21578 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
21579 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
21580 osGetCurrentProcessId(), pFile, pFile->h));
21581 return SQLITE_OK;
21582 }else{
21583 int nCopy = (int)(pFile->mmapSize - offset);
21584 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
21585 pBuf = &((u8 *)pBuf)[nCopy];
21586 amt -= nCopy;
21587 offset += nCopy;
21588 }
21589 }
21590 #endif
21591
21592 #if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
21593 if( winSeekFile(pFile, offset) ){
21594 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
21595 osGetCurrentProcessId(), pFile, pFile->h));
21596 return SQLITE_FULL;
21597 }
21598 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
21599 #else
21600 memset(&overlapped, 0, sizeof(OVERLAPPED));
21601 overlapped.Offset = (LONG)(offset & 0xffffffff);
21602 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
21603 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
21604 osGetLastError()!=ERROR_HANDLE_EOF ){
21605 #endif
21606 DWORD lastErrno;
21607 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
21608 pFile->lastErrno = lastErrno;
21609 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_READ\n",
21610 osGetCurrentProcessId(), pFile, pFile->h));
21611 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
21612 "winRead", pFile->zPath);
21613 }
21614 winLogIoerr(nRetry, __LINE__);
21615 if( nRead<(DWORD)amt ){
21616 /* Unread parts of the buffer must be zero-filled */
21617 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
21618 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_SHORT_READ\n",
21619 osGetCurrentProcessId(), pFile, pFile->h));
21620 return SQLITE_IOERR_SHORT_READ;
21621 }
21622
21623 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
21624 osGetCurrentProcessId(), pFile, pFile->h));
21625 return SQLITE_OK;
21626 }
21627
21628 /*
21629 ** Write data from a buffer into a file. Return SQLITE_OK on success
21630 ** or some other error code on failure.
21631 */
21632 static int winWrite(
21633 sqlite3_file *id, /* File to write into */
21634 const void *pBuf, /* The bytes to be written */
21635 int amt, /* Number of bytes to write */
21636 sqlite3_int64 offset /* Offset into the file to begin writing at */
21637 ){
21638 int rc = 0; /* True if error has occurred, else false */
21639 winFile *pFile = (winFile*)id; /* File handle */
21640 int nRetry = 0; /* Number of retries */
21641
21642 assert( amt>0 );
21643 assert( pFile );
21644 SimulateIOError(return SQLITE_IOERR_WRITE);
21645 SimulateDiskfullError(return SQLITE_FULL);
21646
21647 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, "
21648 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile,
21649 pFile->h, pBuf, amt, offset, pFile->locktype));
21650
21651 #if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
21652 /* Deal with as much of this write request as possible by transfering
21653 ** data from the memory mapping using memcpy(). */
21654 if( offset<pFile->mmapSize ){
21655 if( offset+amt <= pFile->mmapSize ){
21656 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
21657 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
21658 osGetCurrentProcessId(), pFile, pFile->h));
21659 return SQLITE_OK;
21660 }else{
21661 int nCopy = (int)(pFile->mmapSize - offset);
21662 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
21663 pBuf = &((u8 *)pBuf)[nCopy];
21664 amt -= nCopy;
21665 offset += nCopy;
21666 }
21667 }
21668 #endif
21669
21670 #if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
21671 rc = winSeekFile(pFile, offset);
21672 if( rc==0 ){
21673 #else
21674 {
21675 #endif
21676 #if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
21677 OVERLAPPED overlapped; /* The offset for WriteFile. */
21678 #endif
21679 u8 *aRem = (u8 *)pBuf; /* Data yet to be written */
21680 int nRem = amt; /* Number of bytes yet to be written */
21681 DWORD nWrite; /* Bytes written by each WriteFile() call */
21682 DWORD lastErrno = NO_ERROR; /* Value returned by GetLastError() */
21683
21684 #if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
21685 memset(&overlapped, 0, sizeof(OVERLAPPED));
21686 overlapped.Offset = (LONG)(offset & 0xffffffff);
21687 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
21688 #endif
21689
21690 while( nRem>0 ){
21691 #if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED)
21692 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
21693 #else
21694 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
21695 #endif
21696 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
21697 break;
21698 }
21699 assert( nWrite==0 || nWrite<=(DWORD)nRem );
21700 if( nWrite==0 || nWrite>(DWORD)nRem ){
21701 lastErrno = osGetLastError();
21702 break;
21703 }
21704 #if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED)
21705 offset += nWrite;
21706 overlapped.Offset = (LONG)(offset & 0xffffffff);
21707 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
21708 #endif
21709 aRem += nWrite;
21710 nRem -= nWrite;
21711 }
21712 if( nRem>0 ){
21713 pFile->lastErrno = lastErrno;
21714 rc = 1;
21715 }
21716 }
21717
21718 if( rc ){
21719 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
21720 || ( pFile->lastErrno==ERROR_DISK_FULL )){
21721 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n",
21722 osGetCurrentProcessId(), pFile, pFile->h));
21723 return winLogError(SQLITE_FULL, pFile->lastErrno,
21724 "winWrite1", pFile->zPath);
21725 }
21726 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_WRITE\n",
21727 osGetCurrentProcessId(), pFile, pFile->h));
21728 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
21729 "winWrite2", pFile->zPath);
21730 }else{
21731 winLogIoerr(nRetry, __LINE__);
21732 }
21733 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
21734 osGetCurrentProcessId(), pFile, pFile->h));
21735 return SQLITE_OK;
21736 }
21737
21738 /*
21739 ** Truncate an open file to a specified size
21740 */
21741 static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
21742 winFile *pFile = (winFile*)id; /* File handle object */
21743 int rc = SQLITE_OK; /* Return code for this function */
21744 DWORD lastErrno;
21745
21746 assert( pFile );
21747 SimulateIOError(return SQLITE_IOERR_TRUNCATE);
21748 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n",
21749 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
21750
21751 /* If the user has configured a chunk-size for this file, truncate the
21752 ** file so that it consists of an integer number of chunks (i.e. the
21753 ** actual file size after the operation may be larger than the requested
21754 ** size).
21755 */
21756 if( pFile->szChunk>0 ){
21757 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
21758 }
21759
21760 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
21761 if( winSeekFile(pFile, nByte) ){
21762 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
21763 "winTruncate1", pFile->zPath);
21764 }else if( 0==osSetEndOfFile(pFile->h) &&
21765 ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
21766 pFile->lastErrno = lastErrno;
21767 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
21768 "winTruncate2", pFile->zPath);
21769 }
21770
21771 #if SQLITE_MAX_MMAP_SIZE>0
21772 /* If the file was truncated to a size smaller than the currently
21773 ** mapped region, reduce the effective mapping size as well. SQLite will
21774 ** use read() and write() to access data beyond this point from now on.
21775 */
21776 if( pFile->pMapRegion && nByte<pFile->mmapSize ){
21777 pFile->mmapSize = nByte;
21778 }
21779 #endif
21780
21781 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, rc=%s\n",
21782 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
21783 return rc;
21784 }
21785
21786 #ifdef SQLITE_TEST
21787 /*
21788 ** Count the number of fullsyncs and normal syncs. This is used to test
21789 ** that syncs and fullsyncs are occuring at the right times.
21790 */
21791 SQLITE_API int sqlite3_sync_count = 0;
21792 SQLITE_API int sqlite3_fullsync_count = 0;
21793 #endif
21794
21795 /*
21796 ** Make sure all writes to a particular file are committed to disk.
21797 */
21798 static int winSync(sqlite3_file *id, int flags){
21799 #ifndef SQLITE_NO_SYNC
21800 /*
21801 ** Used only when SQLITE_NO_SYNC is not defined.
21802 */
21803 BOOL rc;
21804 #endif
21805 #if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \
21806 defined(SQLITE_HAVE_OS_TRACE)
21807 /*
21808 ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
21809 ** OSTRACE() macros.
21810 */
21811 winFile *pFile = (winFile*)id;
21812 #else
21813 UNUSED_PARAMETER(id);
21814 #endif
21815
21816 assert( pFile );
21817 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
21818 assert((flags&0x0F)==SQLITE_SYNC_NORMAL
21819 || (flags&0x0F)==SQLITE_SYNC_FULL
21820 );
21821
21822 /* Unix cannot, but some systems may return SQLITE_FULL from here. This
21823 ** line is to test that doing so does not cause any problems.
21824 */
21825 SimulateDiskfullError( return SQLITE_FULL );
21826
21827 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, flags=%x, lock=%d\n",
21828 osGetCurrentProcessId(), pFile, pFile->h, flags,
21829 pFile->locktype));
21830
21831 #ifndef SQLITE_TEST
21832 UNUSED_PARAMETER(flags);
21833 #else
21834 if( (flags&0x0F)==SQLITE_SYNC_FULL ){
21835 sqlite3_fullsync_count++;
21836 }
21837 sqlite3_sync_count++;
21838 #endif
21839
21840 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
21841 ** no-op
21842 */
21843 #ifdef SQLITE_NO_SYNC
21844 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
21845 osGetCurrentProcessId(), pFile, pFile->h));
21846 return SQLITE_OK;
21847 #else
21848 #if SQLITE_MAX_MMAP_SIZE>0
21849 if( pFile->pMapRegion ){
21850 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
21851 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
21852 "rc=SQLITE_OK\n", osGetCurrentProcessId(),
21853 pFile, pFile->pMapRegion));
21854 }else{
21855 pFile->lastErrno = osGetLastError();
21856 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
21857 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(),
21858 pFile, pFile->pMapRegion));
21859 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
21860 "winSync1", pFile->zPath);
21861 }
21862 }
21863 #endif
21864 rc = osFlushFileBuffers(pFile->h);
21865 SimulateIOError( rc=FALSE );
21866 if( rc ){
21867 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
21868 osGetCurrentProcessId(), pFile, pFile->h));
21869 return SQLITE_OK;
21870 }else{
21871 pFile->lastErrno = osGetLastError();
21872 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_FSYNC\n",
21873 osGetCurrentProcessId(), pFile, pFile->h));
21874 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
21875 "winSync2", pFile->zPath);
21876 }
21877 #endif
21878 }
21879
21880 /*
21881 ** Determine the current size of a file in bytes
21882 */
21883 static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
21884 winFile *pFile = (winFile*)id;
21885 int rc = SQLITE_OK;
21886
21887 assert( id!=0 );
21888 assert( pSize!=0 );
21889 SimulateIOError(return SQLITE_IOERR_FSTAT);
21890 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
21891
21892 #if SQLITE_OS_WINRT
21893 {
21894 FILE_STANDARD_INFO info;
21895 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
21896 &info, sizeof(info)) ){
21897 *pSize = info.EndOfFile.QuadPart;
21898 }else{
21899 pFile->lastErrno = osGetLastError();
21900 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
21901 "winFileSize", pFile->zPath);
21902 }
21903 }
21904 #else
21905 {
21906 DWORD upperBits;
21907 DWORD lowerBits;
21908 DWORD lastErrno;
21909
21910 lowerBits = osGetFileSize(pFile->h, &upperBits);
21911 *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
21912 if( (lowerBits == INVALID_FILE_SIZE)
21913 && ((lastErrno = osGetLastError())!=NO_ERROR) ){
21914 pFile->lastErrno = lastErrno;
21915 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
21916 "winFileSize", pFile->zPath);
21917 }
21918 }
21919 #endif
21920 OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n",
21921 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
21922 return rc;
21923 }
21924
21925 /*
21926 ** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
21927 */
21928 #ifndef LOCKFILE_FAIL_IMMEDIATELY
21929 # define LOCKFILE_FAIL_IMMEDIATELY 1
21930 #endif
21931
21932 #ifndef LOCKFILE_EXCLUSIVE_LOCK
21933 # define LOCKFILE_EXCLUSIVE_LOCK 2
21934 #endif
21935
21936 /*
21937 ** Historically, SQLite has used both the LockFile and LockFileEx functions.
21938 ** When the LockFile function was used, it was always expected to fail
21939 ** immediately if the lock could not be obtained. Also, it always expected to
21940 ** obtain an exclusive lock. These flags are used with the LockFileEx function
21941 ** and reflect those expectations; therefore, they should not be changed.
21942 */
21943 #ifndef SQLITE_LOCKFILE_FLAGS
21944 # define SQLITE_LOCKFILE_FLAGS (LOCKFILE_FAIL_IMMEDIATELY | \
21945 LOCKFILE_EXCLUSIVE_LOCK)
21946 #endif
21947
21948 /*
21949 ** Currently, SQLite never calls the LockFileEx function without wanting the
21950 ** call to fail immediately if the lock cannot be obtained.
21951 */
21952 #ifndef SQLITE_LOCKFILEEX_FLAGS
21953 # define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY)
21954 #endif
21955
21956 /*
21957 ** Acquire a reader lock.
21958 ** Different API routines are called depending on whether or not this
21959 ** is Win9x or WinNT.
21960 */
21961 static int winGetReadLock(winFile *pFile){
21962 int res;
21963 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
21964 if( osIsNT() ){
21965 #if SQLITE_OS_WINCE
21966 /*
21967 ** NOTE: Windows CE is handled differently here due its lack of the Win32
21968 ** API LockFileEx.
21969 */
21970 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
21971 #else
21972 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
21973 SHARED_SIZE, 0);
21974 #endif
21975 }
21976 #ifdef SQLITE_WIN32_HAS_ANSI
21977 else{
21978 int lk;
21979 sqlite3_randomness(sizeof(lk), &lk);
21980 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
21981 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
21982 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
21983 }
21984 #endif
21985 if( res == 0 ){
21986 pFile->lastErrno = osGetLastError();
21987 /* No need to log a failure to lock */
21988 }
21989 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
21990 return res;
21991 }
21992
21993 /*
21994 ** Undo a readlock
21995 */
21996 static int winUnlockReadLock(winFile *pFile){
21997 int res;
21998 DWORD lastErrno;
21999 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
22000 if( osIsNT() ){
22001 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
22002 }
22003 #ifdef SQLITE_WIN32_HAS_ANSI
22004 else{
22005 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
22006 }
22007 #endif
22008 if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
22009 pFile->lastErrno = lastErrno;
22010 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
22011 "winUnlockReadLock", pFile->zPath);
22012 }
22013 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
22014 return res;
22015 }
22016
22017 /*
22018 ** Lock the file with the lock specified by parameter locktype - one
22019 ** of the following:
22020 **
22021 ** (1) SHARED_LOCK
22022 ** (2) RESERVED_LOCK
22023 ** (3) PENDING_LOCK
22024 ** (4) EXCLUSIVE_LOCK
22025 **
22026 ** Sometimes when requesting one lock state, additional lock states
22027 ** are inserted in between. The locking might fail on one of the later
22028 ** transitions leaving the lock state different from what it started but
22029 ** still short of its goal. The following chart shows the allowed
22030 ** transitions and the inserted intermediate states:
22031 **
22032 ** UNLOCKED -> SHARED
22033 ** SHARED -> RESERVED
22034 ** SHARED -> (PENDING) -> EXCLUSIVE
22035 ** RESERVED -> (PENDING) -> EXCLUSIVE
22036 ** PENDING -> EXCLUSIVE
22037 **
22038 ** This routine will only increase a lock. The winUnlock() routine
22039 ** erases all locks at once and returns us immediately to locking level 0.
22040 ** It is not possible to lower the locking level one step at a time. You
22041 ** must go straight to locking level 0.
22042 */
22043 static int winLock(sqlite3_file *id, int locktype){
22044 int rc = SQLITE_OK; /* Return code from subroutines */
22045 int res = 1; /* Result of a Windows lock call */
22046 int newLocktype; /* Set pFile->locktype to this value before exiting */
22047 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
22048 winFile *pFile = (winFile*)id;
22049 DWORD lastErrno = NO_ERROR;
22050
22051 assert( id!=0 );
22052 OSTRACE(("LOCK file=%p, oldLock=%d(%d), newLock=%d\n",
22053 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
22054
22055 /* If there is already a lock of this type or more restrictive on the
22056 ** OsFile, do nothing. Don't use the end_lock: exit path, as
22057 ** sqlite3OsEnterMutex() hasn't been called yet.
22058 */
22059 if( pFile->locktype>=locktype ){
22060 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
22061 return SQLITE_OK;
22062 }
22063
22064 /* Do not allow any kind of write-lock on a read-only database
22065 */
22066 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
22067 return SQLITE_IOERR_LOCK;
22068 }
22069
22070 /* Make sure the locking sequence is correct
22071 */
22072 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
22073 assert( locktype!=PENDING_LOCK );
22074 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
22075
22076 /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
22077 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
22078 ** the PENDING_LOCK byte is temporary.
22079 */
22080 newLocktype = pFile->locktype;
22081 if( pFile->locktype==NO_LOCK
22082 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
22083 ){
22084 int cnt = 3;
22085 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
22086 PENDING_BYTE, 0, 1, 0))==0 ){
22087 /* Try 3 times to get the pending lock. This is needed to work
22088 ** around problems caused by indexing and/or anti-virus software on
22089 ** Windows systems.
22090 ** If you are using this code as a model for alternative VFSes, do not
22091 ** copy this retry logic. It is a hack intended for Windows only.
22092 */
22093 lastErrno = osGetLastError();
22094 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
22095 pFile->h, cnt, res));
22096 if( lastErrno==ERROR_INVALID_HANDLE ){
22097 pFile->lastErrno = lastErrno;
22098 rc = SQLITE_IOERR_LOCK;
22099 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
22100 pFile->h, cnt, sqlite3ErrName(rc)));
22101 return rc;
22102 }
22103 if( cnt ) sqlite3_win32_sleep(1);
22104 }
22105 gotPendingLock = res;
22106 if( !res ){
22107 lastErrno = osGetLastError();
22108 }
22109 }
22110
22111 /* Acquire a shared lock
22112 */
22113 if( locktype==SHARED_LOCK && res ){
22114 assert( pFile->locktype==NO_LOCK );
22115 res = winGetReadLock(pFile);
22116 if( res ){
22117 newLocktype = SHARED_LOCK;
22118 }else{
22119 lastErrno = osGetLastError();
22120 }
22121 }
22122
22123 /* Acquire a RESERVED lock
22124 */
22125 if( locktype==RESERVED_LOCK && res ){
22126 assert( pFile->locktype==SHARED_LOCK );
22127 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
22128 if( res ){
22129 newLocktype = RESERVED_LOCK;
22130 }else{
22131 lastErrno = osGetLastError();
22132 }
22133 }
22134
22135 /* Acquire a PENDING lock
22136 */
22137 if( locktype==EXCLUSIVE_LOCK && res ){
22138 newLocktype = PENDING_LOCK;
22139 gotPendingLock = 0;
22140 }
22141
22142 /* Acquire an EXCLUSIVE lock
22143 */
22144 if( locktype==EXCLUSIVE_LOCK && res ){
22145 assert( pFile->locktype>=SHARED_LOCK );
22146 res = winUnlockReadLock(pFile);
22147 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
22148 SHARED_SIZE, 0);
22149 if( res ){
22150 newLocktype = EXCLUSIVE_LOCK;
22151 }else{
22152 lastErrno = osGetLastError();
22153 winGetReadLock(pFile);
22154 }
22155 }
22156
22157 /* If we are holding a PENDING lock that ought to be released, then
22158 ** release it now.
22159 */
22160 if( gotPendingLock && locktype==SHARED_LOCK ){
22161 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
22162 }
22163
22164 /* Update the state of the lock has held in the file descriptor then
22165 ** return the appropriate result code.
22166 */
22167 if( res ){
22168 rc = SQLITE_OK;
22169 }else{
22170 pFile->lastErrno = lastErrno;
22171 rc = SQLITE_BUSY;
22172 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
22173 pFile->h, locktype, newLocktype));
22174 }
22175 pFile->locktype = (u8)newLocktype;
22176 OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
22177 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
22178 return rc;
22179 }
22180
22181 /*
22182 ** This routine checks if there is a RESERVED lock held on the specified
22183 ** file by this or any other process. If such a lock is held, return
22184 ** non-zero, otherwise zero.
22185 */
22186 static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
22187 int res;
22188 winFile *pFile = (winFile*)id;
22189
22190 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
22191 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
22192
22193 assert( id!=0 );
22194 if( pFile->locktype>=RESERVED_LOCK ){
22195 res = 1;
22196 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
22197 }else{
22198 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
22199 if( res ){
22200 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
22201 }
22202 res = !res;
22203 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
22204 }
22205 *pResOut = res;
22206 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
22207 pFile->h, pResOut, *pResOut));
22208 return SQLITE_OK;
22209 }
22210
22211 /*
22212 ** Lower the locking level on file descriptor id to locktype. locktype
22213 ** must be either NO_LOCK or SHARED_LOCK.
22214 **
22215 ** If the locking level of the file descriptor is already at or below
22216 ** the requested locking level, this routine is a no-op.
22217 **
22218 ** It is not possible for this routine to fail if the second argument
22219 ** is NO_LOCK. If the second argument is SHARED_LOCK then this routine
22220 ** might return SQLITE_IOERR;
22221 */
22222 static int winUnlock(sqlite3_file *id, int locktype){
22223 int type;
22224 winFile *pFile = (winFile*)id;
22225 int rc = SQLITE_OK;
22226 assert( pFile!=0 );
22227 assert( locktype<=SHARED_LOCK );
22228 OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
22229 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
22230 type = pFile->locktype;
22231 if( type>=EXCLUSIVE_LOCK ){
22232 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
22233 if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){
22234 /* This should never happen. We should always be able to
22235 ** reacquire the read lock */
22236 rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
22237 "winUnlock", pFile->zPath);
22238 }
22239 }
22240 if( type>=RESERVED_LOCK ){
22241 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
22242 }
22243 if( locktype==NO_LOCK && type>=SHARED_LOCK ){
22244 winUnlockReadLock(pFile);
22245 }
22246 if( type>=PENDING_LOCK ){
22247 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
22248 }
22249 pFile->locktype = (u8)locktype;
22250 OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
22251 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
22252 return rc;
22253 }
22254
22255 /******************************************************************************
22256 ****************************** No-op Locking **********************************
22257 **
22258 ** Of the various locking implementations available, this is by far the
22259 ** simplest: locking is ignored. No attempt is made to lock the database
22260 ** file for reading or writing.
22261 **
22262 ** This locking mode is appropriate for use on read-only databases
22263 ** (ex: databases that are burned into CD-ROM, for example.) It can
22264 ** also be used if the application employs some external mechanism to
22265 ** prevent simultaneous access of the same database by two or more
22266 ** database connections. But there is a serious risk of database
22267 ** corruption if this locking mode is used in situations where multiple
22268 ** database connections are accessing the same database file at the same
22269 ** time and one or more of those connections are writing.
22270 */
22271
22272 static int winNolockLock(sqlite3_file *id, int locktype){
22273 UNUSED_PARAMETER(id);
22274 UNUSED_PARAMETER(locktype);
22275 return SQLITE_OK;
22276 }
22277
22278 static int winNolockCheckReservedLock(sqlite3_file *id, int *pResOut){
22279 UNUSED_PARAMETER(id);
22280 UNUSED_PARAMETER(pResOut);
22281 return SQLITE_OK;
22282 }
22283
22284 static int winNolockUnlock(sqlite3_file *id, int locktype){
22285 UNUSED_PARAMETER(id);
22286 UNUSED_PARAMETER(locktype);
22287 return SQLITE_OK;
22288 }
22289
22290 /******************* End of the no-op lock implementation *********************
22291 ******************************************************************************/
22292
22293 /*
22294 ** If *pArg is initially negative then this is a query. Set *pArg to
22295 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
22296 **
22297 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
22298 */
22299 static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
22300 if( *pArg<0 ){
22301 *pArg = (pFile->ctrlFlags & mask)!=0;
22302 }else if( (*pArg)==0 ){
22303 pFile->ctrlFlags &= ~mask;
22304 }else{
22305 pFile->ctrlFlags |= mask;
22306 }
22307 }
22308
22309 /* Forward references to VFS helper methods used for temporary files */
22310 static int winGetTempname(sqlite3_vfs *, char **);
22311 static int winIsDir(const void *);
22312 static BOOL winIsDriveLetterAndColon(const char *);
22313
22314 /*
22315 ** Control and query of the open file handle.
22316 */
22317 static int winFileControl(sqlite3_file *id, int op, void *pArg){
22318 winFile *pFile = (winFile*)id;
22319 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
22320 switch( op ){
22321 case SQLITE_FCNTL_LOCKSTATE: {
22322 *(int*)pArg = pFile->locktype;
22323 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
22324 return SQLITE_OK;
22325 }
22326 case SQLITE_FCNTL_LAST_ERRNO: {
22327 *(int*)pArg = (int)pFile->lastErrno;
22328 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
22329 return SQLITE_OK;
22330 }
22331 case SQLITE_FCNTL_CHUNK_SIZE: {
22332 pFile->szChunk = *(int *)pArg;
22333 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
22334 return SQLITE_OK;
22335 }
22336 case SQLITE_FCNTL_SIZE_HINT: {
22337 if( pFile->szChunk>0 ){
22338 sqlite3_int64 oldSz;
22339 int rc = winFileSize(id, &oldSz);
22340 if( rc==SQLITE_OK ){
22341 sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
22342 if( newSz>oldSz ){
22343 SimulateIOErrorBenign(1);
22344 rc = winTruncate(id, newSz);
22345 SimulateIOErrorBenign(0);
22346 }
22347 }
22348 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
22349 return rc;
22350 }
22351 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
22352 return SQLITE_OK;
22353 }
22354 case SQLITE_FCNTL_PERSIST_WAL: {
22355 winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
22356 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
22357 return SQLITE_OK;
22358 }
22359 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
22360 winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
22361 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
22362 return SQLITE_OK;
22363 }
22364 case SQLITE_FCNTL_VFSNAME: {
22365 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
22366 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
22367 return SQLITE_OK;
22368 }
22369 case SQLITE_FCNTL_WIN32_AV_RETRY: {
22370 int *a = (int*)pArg;
22371 if( a[0]>0 ){
22372 winIoerrRetry = a[0];
22373 }else{
22374 a[0] = winIoerrRetry;
22375 }
22376 if( a[1]>0 ){
22377 winIoerrRetryDelay = a[1];
22378 }else{
22379 a[1] = winIoerrRetryDelay;
22380 }
22381 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
22382 return SQLITE_OK;
22383 }
22384 case SQLITE_FCNTL_WIN32_GET_HANDLE: {
22385 LPHANDLE phFile = (LPHANDLE)pArg;
22386 *phFile = pFile->h;
22387 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
22388 return SQLITE_OK;
22389 }
22390 #ifdef SQLITE_TEST
22391 case SQLITE_FCNTL_WIN32_SET_HANDLE: {
22392 LPHANDLE phFile = (LPHANDLE)pArg;
22393 HANDLE hOldFile = pFile->h;
22394 pFile->h = *phFile;
22395 *phFile = hOldFile;
22396 OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n",
22397 hOldFile, pFile->h));
22398 return SQLITE_OK;
22399 }
22400 #endif
22401 case SQLITE_FCNTL_TEMPFILENAME: {
22402 char *zTFile = 0;
22403 int rc = winGetTempname(pFile->pVfs, &zTFile);
22404 if( rc==SQLITE_OK ){
22405 *(char**)pArg = zTFile;
22406 }
22407 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
22408 return rc;
22409 }
22410 #if SQLITE_MAX_MMAP_SIZE>0
22411 case SQLITE_FCNTL_MMAP_SIZE: {
22412 i64 newLimit = *(i64*)pArg;
22413 int rc = SQLITE_OK;
22414 if( newLimit>sqlite3GlobalConfig.mxMmap ){
22415 newLimit = sqlite3GlobalConfig.mxMmap;
22416 }
22417 *(i64*)pArg = pFile->mmapSizeMax;
22418 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
22419 pFile->mmapSizeMax = newLimit;
22420 if( pFile->mmapSize>0 ){
22421 winUnmapfile(pFile);
22422 rc = winMapfile(pFile, -1);
22423 }
22424 }
22425 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
22426 return rc;
22427 }
22428 #endif
22429 }
22430 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
22431 return SQLITE_NOTFOUND;
22432 }
22433
22434 /*
22435 ** Return the sector size in bytes of the underlying block device for
22436 ** the specified file. This is almost always 512 bytes, but may be
22437 ** larger for some devices.
22438 **
22439 ** SQLite code assumes this function cannot fail. It also assumes that
22440 ** if two files are created in the same file-system directory (i.e.
22441 ** a database and its journal file) that the sector size will be the
22442 ** same for both.
22443 */
22444 static int winSectorSize(sqlite3_file *id){
22445 (void)id;
22446 return SQLITE_DEFAULT_SECTOR_SIZE;
22447 }
22448
22449 /*
22450 ** Return a vector of device characteristics.
22451 */
22452 static int winDeviceCharacteristics(sqlite3_file *id){
22453 winFile *p = (winFile*)id;
22454 return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
22455 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
22456 }
22457
22458 /*
22459 ** Windows will only let you create file view mappings
22460 ** on allocation size granularity boundaries.
22461 ** During sqlite3_os_init() we do a GetSystemInfo()
22462 ** to get the granularity size.
22463 */
22464 static SYSTEM_INFO winSysInfo;
22465
22466 #ifndef SQLITE_OMIT_WAL
22467
22468 /*
22469 ** Helper functions to obtain and relinquish the global mutex. The
22470 ** global mutex is used to protect the winLockInfo objects used by
22471 ** this file, all of which may be shared by multiple threads.
22472 **
22473 ** Function winShmMutexHeld() is used to assert() that the global mutex
22474 ** is held when required. This function is only used as part of assert()
22475 ** statements. e.g.
22476 **
22477 ** winShmEnterMutex()
22478 ** assert( winShmMutexHeld() );
22479 ** winShmLeaveMutex()
22480 */
22481 static void winShmEnterMutex(void){
22482 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
22483 }
22484 static void winShmLeaveMutex(void){
22485 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
22486 }
22487 #ifndef NDEBUG
22488 static int winShmMutexHeld(void) {
22489 return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
22490 }
22491 #endif
22492
22493 /*
22494 ** Object used to represent a single file opened and mmapped to provide
22495 ** shared memory. When multiple threads all reference the same
22496 ** log-summary, each thread has its own winFile object, but they all
22497 ** point to a single instance of this object. In other words, each
22498 ** log-summary is opened only once per process.
22499 **
22500 ** winShmMutexHeld() must be true when creating or destroying
22501 ** this object or while reading or writing the following fields:
22502 **
22503 ** nRef
22504 ** pNext
22505 **
22506 ** The following fields are read-only after the object is created:
22507 **
22508 ** fid
22509 ** zFilename
22510 **
22511 ** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
22512 ** winShmMutexHeld() is true when reading or writing any other field
22513 ** in this structure.
22514 **
22515 */
22516 struct winShmNode {
22517 sqlite3_mutex *mutex; /* Mutex to access this object */
22518 char *zFilename; /* Name of the file */
22519 winFile hFile; /* File handle from winOpen */
22520
22521 int szRegion; /* Size of shared-memory regions */
22522 int nRegion; /* Size of array apRegion */
22523 struct ShmRegion {
22524 HANDLE hMap; /* File handle from CreateFileMapping */
22525 void *pMap;
22526 } *aRegion;
22527 DWORD lastErrno; /* The Windows errno from the last I/O error */
22528
22529 int nRef; /* Number of winShm objects pointing to this */
22530 winShm *pFirst; /* All winShm objects pointing to this */
22531 winShmNode *pNext; /* Next in list of all winShmNode objects */
22532 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
22533 u8 nextShmId; /* Next available winShm.id value */
22534 #endif
22535 };
22536
22537 /*
22538 ** A global array of all winShmNode objects.
22539 **
22540 ** The winShmMutexHeld() must be true while reading or writing this list.
22541 */
22542 static winShmNode *winShmNodeList = 0;
22543
22544 /*
22545 ** Structure used internally by this VFS to record the state of an
22546 ** open shared memory connection.
22547 **
22548 ** The following fields are initialized when this object is created and
22549 ** are read-only thereafter:
22550 **
22551 ** winShm.pShmNode
22552 ** winShm.id
22553 **
22554 ** All other fields are read/write. The winShm.pShmNode->mutex must be held
22555 ** while accessing any read/write fields.
22556 */
22557 struct winShm {
22558 winShmNode *pShmNode; /* The underlying winShmNode object */
22559 winShm *pNext; /* Next winShm with the same winShmNode */
22560 u8 hasMutex; /* True if holding the winShmNode mutex */
22561 u16 sharedMask; /* Mask of shared locks held */
22562 u16 exclMask; /* Mask of exclusive locks held */
22563 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
22564 u8 id; /* Id of this connection with its winShmNode */
22565 #endif
22566 };
22567
22568 /*
22569 ** Constants used for locking
22570 */
22571 #define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
22572 #define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
22573
22574 /*
22575 ** Apply advisory locks for all n bytes beginning at ofst.
22576 */
22577 #define WINSHM_UNLCK 1
22578 #define WINSHM_RDLCK 2
22579 #define WINSHM_WRLCK 3
22580 static int winShmSystemLock(
22581 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
22582 int lockType, /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */
22583 int ofst, /* Offset to first byte to be locked/unlocked */
22584 int nByte /* Number of bytes to lock or unlock */
22585 ){
22586 int rc = 0; /* Result code form Lock/UnlockFileEx() */
22587
22588 /* Access to the winShmNode object is serialized by the caller */
22589 assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
22590
22591 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
22592 pFile->hFile.h, lockType, ofst, nByte));
22593
22594 /* Release/Acquire the system-level lock */
22595 if( lockType==WINSHM_UNLCK ){
22596 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
22597 }else{
22598 /* Initialize the locking parameters */
22599 DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
22600 if( lockType == WINSHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
22601 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
22602 }
22603
22604 if( rc!= 0 ){
22605 rc = SQLITE_OK;
22606 }else{
22607 pFile->lastErrno = osGetLastError();
22608 rc = SQLITE_BUSY;
22609 }
22610
22611 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
22612 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
22613 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
22614
22615 return rc;
22616 }
22617
22618 /* Forward references to VFS methods */
22619 static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
22620 static int winDelete(sqlite3_vfs *,const char*,int);
22621
22622 /*
22623 ** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
22624 **
22625 ** This is not a VFS shared-memory method; it is a utility function called
22626 ** by VFS shared-memory methods.
22627 */
22628 static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
22629 winShmNode **pp;
22630 winShmNode *p;
22631 assert( winShmMutexHeld() );
22632 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
22633 osGetCurrentProcessId(), deleteFlag));
22634 pp = &winShmNodeList;
22635 while( (p = *pp)!=0 ){
22636 if( p->nRef==0 ){
22637 int i;
22638 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
22639 for(i=0; i<p->nRegion; i++){
22640 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
22641 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
22642 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
22643 UNUSED_VARIABLE_VALUE(bRc);
22644 bRc = osCloseHandle(p->aRegion[i].hMap);
22645 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
22646 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
22647 UNUSED_VARIABLE_VALUE(bRc);
22648 }
22649 if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
22650 SimulateIOErrorBenign(1);
22651 winClose((sqlite3_file *)&p->hFile);
22652 SimulateIOErrorBenign(0);
22653 }
22654 if( deleteFlag ){
22655 SimulateIOErrorBenign(1);
22656 sqlite3BeginBenignMalloc();
22657 winDelete(pVfs, p->zFilename, 0);
22658 sqlite3EndBenignMalloc();
22659 SimulateIOErrorBenign(0);
22660 }
22661 *pp = p->pNext;
22662 sqlite3_free(p->aRegion);
22663 sqlite3_free(p);
22664 }else{
22665 pp = &p->pNext;
22666 }
22667 }
22668 }
22669
22670 /*
22671 ** Open the shared-memory area associated with database file pDbFd.
22672 **
22673 ** When opening a new shared-memory file, if no other instances of that
22674 ** file are currently open, in this process or in other processes, then
22675 ** the file must be truncated to zero length or have its header cleared.
22676 */
22677 static int winOpenSharedMemory(winFile *pDbFd){
22678 struct winShm *p; /* The connection to be opened */
22679 struct winShmNode *pShmNode = 0; /* The underlying mmapped file */
22680 int rc; /* Result code */
22681 struct winShmNode *pNew; /* Newly allocated winShmNode */
22682 int nName; /* Size of zName in bytes */
22683
22684 assert( pDbFd->pShm==0 ); /* Not previously opened */
22685
22686 /* Allocate space for the new sqlite3_shm object. Also speculatively
22687 ** allocate space for a new winShmNode and filename.
22688 */
22689 p = sqlite3MallocZero( sizeof(*p) );
22690 if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT;
22691 nName = sqlite3Strlen30(pDbFd->zPath);
22692 pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
22693 if( pNew==0 ){
22694 sqlite3_free(p);
22695 return SQLITE_IOERR_NOMEM_BKPT;
22696 }
22697 pNew->zFilename = (char*)&pNew[1];
22698 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
22699 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
22700
22701 /* Look to see if there is an existing winShmNode that can be used.
22702 ** If no matching winShmNode currently exists, create a new one.
22703 */
22704 winShmEnterMutex();
22705 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
22706 /* TBD need to come up with better match here. Perhaps
22707 ** use FILE_ID_BOTH_DIR_INFO Structure.
22708 */
22709 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
22710 }
22711 if( pShmNode ){
22712 sqlite3_free(pNew);
22713 }else{
22714 pShmNode = pNew;
22715 pNew = 0;
22716 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
22717 pShmNode->pNext = winShmNodeList;
22718 winShmNodeList = pShmNode;
22719
22720 if( sqlite3GlobalConfig.bCoreMutex ){
22721 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
22722 if( pShmNode->mutex==0 ){
22723 rc = SQLITE_IOERR_NOMEM_BKPT;
22724 goto shm_open_err;
22725 }
22726 }
22727
22728 rc = winOpen(pDbFd->pVfs,
22729 pShmNode->zFilename, /* Name of the file (UTF-8) */
22730 (sqlite3_file*)&pShmNode->hFile, /* File handle here */
22731 SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
22732 0);
22733 if( SQLITE_OK!=rc ){
22734 goto shm_open_err;
22735 }
22736
22737 /* Check to see if another process is holding the dead-man switch.
22738 ** If not, truncate the file to zero length.
22739 */
22740 if( winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
22741 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
22742 if( rc!=SQLITE_OK ){
22743 rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
22744 "winOpenShm", pDbFd->zPath);
22745 }
22746 }
22747 if( rc==SQLITE_OK ){
22748 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
22749 rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1);
22750 }
22751 if( rc ) goto shm_open_err;
22752 }
22753
22754 /* Make the new connection a child of the winShmNode */
22755 p->pShmNode = pShmNode;
22756 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
22757 p->id = pShmNode->nextShmId++;
22758 #endif
22759 pShmNode->nRef++;
22760 pDbFd->pShm = p;
22761 winShmLeaveMutex();
22762
22763 /* The reference count on pShmNode has already been incremented under
22764 ** the cover of the winShmEnterMutex() mutex and the pointer from the
22765 ** new (struct winShm) object to the pShmNode has been set. All that is
22766 ** left to do is to link the new object into the linked list starting
22767 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
22768 ** mutex.
22769 */
22770 sqlite3_mutex_enter(pShmNode->mutex);
22771 p->pNext = pShmNode->pFirst;
22772 pShmNode->pFirst = p;
22773 sqlite3_mutex_leave(pShmNode->mutex);
22774 return SQLITE_OK;
22775
22776 /* Jump here on any error */
22777 shm_open_err:
22778 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
22779 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
22780 sqlite3_free(p);
22781 sqlite3_free(pNew);
22782 winShmLeaveMutex();
22783 return rc;
22784 }
22785
22786 /*
22787 ** Close a connection to shared-memory. Delete the underlying
22788 ** storage if deleteFlag is true.
22789 */
22790 static int winShmUnmap(
22791 sqlite3_file *fd, /* Database holding shared memory */
22792 int deleteFlag /* Delete after closing if true */
22793 ){
22794 winFile *pDbFd; /* Database holding shared-memory */
22795 winShm *p; /* The connection to be closed */
22796 winShmNode *pShmNode; /* The underlying shared-memory file */
22797 winShm **pp; /* For looping over sibling connections */
22798
22799 pDbFd = (winFile*)fd;
22800 p = pDbFd->pShm;
22801 if( p==0 ) return SQLITE_OK;
22802 pShmNode = p->pShmNode;
22803
22804 /* Remove connection p from the set of connections associated
22805 ** with pShmNode */
22806 sqlite3_mutex_enter(pShmNode->mutex);
22807 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
22808 *pp = p->pNext;
22809
22810 /* Free the connection p */
22811 sqlite3_free(p);
22812 pDbFd->pShm = 0;
22813 sqlite3_mutex_leave(pShmNode->mutex);
22814
22815 /* If pShmNode->nRef has reached 0, then close the underlying
22816 ** shared-memory file, too */
22817 winShmEnterMutex();
22818 assert( pShmNode->nRef>0 );
22819 pShmNode->nRef--;
22820 if( pShmNode->nRef==0 ){
22821 winShmPurge(pDbFd->pVfs, deleteFlag);
22822 }
22823 winShmLeaveMutex();
22824
22825 return SQLITE_OK;
22826 }
22827
22828 /*
22829 ** Change the lock state for a shared-memory segment.
22830 */
22831 static int winShmLock(
22832 sqlite3_file *fd, /* Database file holding the shared memory */
22833 int ofst, /* First lock to acquire or release */
22834 int n, /* Number of locks to acquire or release */
22835 int flags /* What to do with the lock */
22836 ){
22837 winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */
22838 winShm *p = pDbFd->pShm; /* The shared memory being locked */
22839 winShm *pX; /* For looping over all siblings */
22840 winShmNode *pShmNode = p->pShmNode;
22841 int rc = SQLITE_OK; /* Result code */
22842 u16 mask; /* Mask of locks to take or release */
22843
22844 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
22845 assert( n>=1 );
22846 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
22847 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
22848 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
22849 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
22850 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
22851
22852 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
22853 assert( n>1 || mask==(1<<ofst) );
22854 sqlite3_mutex_enter(pShmNode->mutex);
22855 if( flags & SQLITE_SHM_UNLOCK ){
22856 u16 allMask = 0; /* Mask of locks held by siblings */
22857
22858 /* See if any siblings hold this same lock */
22859 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
22860 if( pX==p ) continue;
22861 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
22862 allMask |= pX->sharedMask;
22863 }
22864
22865 /* Unlock the system-level locks */
22866 if( (mask & allMask)==0 ){
22867 rc = winShmSystemLock(pShmNode, WINSHM_UNLCK, ofst+WIN_SHM_BASE, n);
22868 }else{
22869 rc = SQLITE_OK;
22870 }
22871
22872 /* Undo the local locks */
22873 if( rc==SQLITE_OK ){
22874 p->exclMask &= ~mask;
22875 p->sharedMask &= ~mask;
22876 }
22877 }else if( flags & SQLITE_SHM_SHARED ){
22878 u16 allShared = 0; /* Union of locks held by connections other than "p" */
22879
22880 /* Find out which shared locks are already held by sibling connections.
22881 ** If any sibling already holds an exclusive lock, go ahead and return
22882 ** SQLITE_BUSY.
22883 */
22884 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
22885 if( (pX->exclMask & mask)!=0 ){
22886 rc = SQLITE_BUSY;
22887 break;
22888 }
22889 allShared |= pX->sharedMask;
22890 }
22891
22892 /* Get shared locks at the system level, if necessary */
22893 if( rc==SQLITE_OK ){
22894 if( (allShared & mask)==0 ){
22895 rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, ofst+WIN_SHM_BASE, n);
22896 }else{
22897 rc = SQLITE_OK;
22898 }
22899 }
22900
22901 /* Get the local shared locks */
22902 if( rc==SQLITE_OK ){
22903 p->sharedMask |= mask;
22904 }
22905 }else{
22906 /* Make sure no sibling connections hold locks that will block this
22907 ** lock. If any do, return SQLITE_BUSY right away.
22908 */
22909 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
22910 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
22911 rc = SQLITE_BUSY;
22912 break;
22913 }
22914 }
22915
22916 /* Get the exclusive locks at the system level. Then if successful
22917 ** also mark the local connection as being locked.
22918 */
22919 if( rc==SQLITE_OK ){
22920 rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, ofst+WIN_SHM_BASE, n);
22921 if( rc==SQLITE_OK ){
22922 assert( (p->sharedMask & mask)==0 );
22923 p->exclMask |= mask;
22924 }
22925 }
22926 }
22927 sqlite3_mutex_leave(pShmNode->mutex);
22928 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
22929 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
22930 sqlite3ErrName(rc)));
22931 return rc;
22932 }
22933
22934 /*
22935 ** Implement a memory barrier or memory fence on shared memory.
22936 **
22937 ** All loads and stores begun before the barrier must complete before
22938 ** any load or store begun after the barrier.
22939 */
22940 static void winShmBarrier(
22941 sqlite3_file *fd /* Database holding the shared memory */
22942 ){
22943 UNUSED_PARAMETER(fd);
22944 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
22945 winShmEnterMutex(); /* Also mutex, for redundancy */
22946 winShmLeaveMutex();
22947 }
22948
22949 /*
22950 ** This function is called to obtain a pointer to region iRegion of the
22951 ** shared-memory associated with the database file fd. Shared-memory regions
22952 ** are numbered starting from zero. Each shared-memory region is szRegion
22953 ** bytes in size.
22954 **
22955 ** If an error occurs, an error code is returned and *pp is set to NULL.
22956 **
22957 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
22958 ** region has not been allocated (by any client, including one running in a
22959 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
22960 ** isWrite is non-zero and the requested shared-memory region has not yet
22961 ** been allocated, it is allocated by this function.
22962 **
22963 ** If the shared-memory region has already been allocated or is allocated by
22964 ** this call as described above, then it is mapped into this processes
22965 ** address space (if it is not already), *pp is set to point to the mapped
22966 ** memory and SQLITE_OK returned.
22967 */
22968 static int winShmMap(
22969 sqlite3_file *fd, /* Handle open on database file */
22970 int iRegion, /* Region to retrieve */
22971 int szRegion, /* Size of regions */
22972 int isWrite, /* True to extend file if necessary */
22973 void volatile **pp /* OUT: Mapped memory */
22974 ){
22975 winFile *pDbFd = (winFile*)fd;
22976 winShm *pShm = pDbFd->pShm;
22977 winShmNode *pShmNode;
22978 int rc = SQLITE_OK;
22979
22980 if( !pShm ){
22981 rc = winOpenSharedMemory(pDbFd);
22982 if( rc!=SQLITE_OK ) return rc;
22983 pShm = pDbFd->pShm;
22984 }
22985 pShmNode = pShm->pShmNode;
22986
22987 sqlite3_mutex_enter(pShmNode->mutex);
22988 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
22989
22990 if( pShmNode->nRegion<=iRegion ){
22991 struct ShmRegion *apNew; /* New aRegion[] array */
22992 int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
22993 sqlite3_int64 sz; /* Current size of wal-index file */
22994
22995 pShmNode->szRegion = szRegion;
22996
22997 /* The requested region is not mapped into this processes address space.
22998 ** Check to see if it has been allocated (i.e. if the wal-index file is
22999 ** large enough to contain the requested region).
23000 */
23001 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
23002 if( rc!=SQLITE_OK ){
23003 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
23004 "winShmMap1", pDbFd->zPath);
23005 goto shmpage_out;
23006 }
23007
23008 if( sz<nByte ){
23009 /* The requested memory region does not exist. If isWrite is set to
23010 ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
23011 **
23012 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
23013 ** the requested memory region.
23014 */
23015 if( !isWrite ) goto shmpage_out;
23016 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
23017 if( rc!=SQLITE_OK ){
23018 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
23019 "winShmMap2", pDbFd->zPath);
23020 goto shmpage_out;
23021 }
23022 }
23023
23024 /* Map the requested memory region into this processes address space. */
23025 apNew = (struct ShmRegion *)sqlite3_realloc64(
23026 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
23027 );
23028 if( !apNew ){
23029 rc = SQLITE_IOERR_NOMEM_BKPT;
23030 goto shmpage_out;
23031 }
23032 pShmNode->aRegion = apNew;
23033
23034 while( pShmNode->nRegion<=iRegion ){
23035 HANDLE hMap = NULL; /* file-mapping handle */
23036 void *pMap = 0; /* Mapped memory region */
23037
23038 #if SQLITE_OS_WINRT
23039 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
23040 NULL, PAGE_READWRITE, nByte, NULL
23041 );
23042 #elif defined(SQLITE_WIN32_HAS_WIDE)
23043 hMap = osCreateFileMappingW(pShmNode->hFile.h,
23044 NULL, PAGE_READWRITE, 0, nByte, NULL
23045 );
23046 #elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
23047 hMap = osCreateFileMappingA(pShmNode->hFile.h,
23048 NULL, PAGE_READWRITE, 0, nByte, NULL
23049 );
23050 #endif
23051 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
23052 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
23053 hMap ? "ok" : "failed"));
23054 if( hMap ){
23055 int iOffset = pShmNode->nRegion*szRegion;
23056 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
23057 #if SQLITE_OS_WINRT
23058 pMap = osMapViewOfFileFromApp(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
23059 iOffset - iOffsetShift, szRegion + iOffsetShift
23060 );
23061 #else
23062 pMap = osMapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
23063 0, iOffset - iOffsetShift, szRegion + iOffsetShift
23064 );
23065 #endif
23066 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
23067 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
23068 szRegion, pMap ? "ok" : "failed"));
23069 }
23070 if( !pMap ){
23071 pShmNode->lastErrno = osGetLastError();
23072 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
23073 "winShmMap3", pDbFd->zPath);
23074 if( hMap ) osCloseHandle(hMap);
23075 goto shmpage_out;
23076 }
23077
23078 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
23079 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
23080 pShmNode->nRegion++;
23081 }
23082 }
23083
23084 shmpage_out:
23085 if( pShmNode->nRegion>iRegion ){
23086 int iOffset = iRegion*szRegion;
23087 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
23088 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
23089 *pp = (void *)&p[iOffsetShift];
23090 }else{
23091 *pp = 0;
23092 }
23093 sqlite3_mutex_leave(pShmNode->mutex);
23094 return rc;
23095 }
23096
23097 #else
23098 # define winShmMap 0
23099 # define winShmLock 0
23100 # define winShmBarrier 0
23101 # define winShmUnmap 0
23102 #endif /* #ifndef SQLITE_OMIT_WAL */
23103
23104 /*
23105 ** Cleans up the mapped region of the specified file, if any.
23106 */
23107 #if SQLITE_MAX_MMAP_SIZE>0
23108 static int winUnmapfile(winFile *pFile){
23109 assert( pFile!=0 );
23110 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
23111 "mmapSize=%lld, mmapSizeActual=%lld, mmapSizeMax=%lld\n",
23112 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
23113 pFile->mmapSize, pFile->mmapSizeActual, pFile->mmapSizeMax));
23114 if( pFile->pMapRegion ){
23115 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
23116 pFile->lastErrno = osGetLastError();
23117 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
23118 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
23119 pFile->pMapRegion));
23120 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
23121 "winUnmapfile1", pFile->zPath);
23122 }
23123 pFile->pMapRegion = 0;
23124 pFile->mmapSize = 0;
23125 pFile->mmapSizeActual = 0;
23126 }
23127 if( pFile->hMap!=NULL ){
23128 if( !osCloseHandle(pFile->hMap) ){
23129 pFile->lastErrno = osGetLastError();
23130 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
23131 osGetCurrentProcessId(), pFile, pFile->hMap));
23132 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
23133 "winUnmapfile2", pFile->zPath);
23134 }
23135 pFile->hMap = NULL;
23136 }
23137 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
23138 osGetCurrentProcessId(), pFile));
23139 return SQLITE_OK;
23140 }
23141
23142 /*
23143 ** Memory map or remap the file opened by file-descriptor pFd (if the file
23144 ** is already mapped, the existing mapping is replaced by the new). Or, if
23145 ** there already exists a mapping for this file, and there are still
23146 ** outstanding xFetch() references to it, this function is a no-op.
23147 **
23148 ** If parameter nByte is non-negative, then it is the requested size of
23149 ** the mapping to create. Otherwise, if nByte is less than zero, then the
23150 ** requested size is the size of the file on disk. The actual size of the
23151 ** created mapping is either the requested size or the value configured
23152 ** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
23153 **
23154 ** SQLITE_OK is returned if no error occurs (even if the mapping is not
23155 ** recreated as a result of outstanding references) or an SQLite error
23156 ** code otherwise.
23157 */
23158 static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
23159 sqlite3_int64 nMap = nByte;
23160 int rc;
23161
23162 assert( nMap>=0 || pFd->nFetchOut==0 );
23163 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
23164 osGetCurrentProcessId(), pFd, nByte));
23165
23166 if( pFd->nFetchOut>0 ) return SQLITE_OK;
23167
23168 if( nMap<0 ){
23169 rc = winFileSize((sqlite3_file*)pFd, &nMap);
23170 if( rc ){
23171 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
23172 osGetCurrentProcessId(), pFd));
23173 return SQLITE_IOERR_FSTAT;
23174 }
23175 }
23176 if( nMap>pFd->mmapSizeMax ){
23177 nMap = pFd->mmapSizeMax;
23178 }
23179 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
23180
23181 if( nMap==0 && pFd->mmapSize>0 ){
23182 winUnmapfile(pFd);
23183 }
23184 if( nMap!=pFd->mmapSize ){
23185 void *pNew = 0;
23186 DWORD protect = PAGE_READONLY;
23187 DWORD flags = FILE_MAP_READ;
23188
23189 winUnmapfile(pFd);
23190 #ifdef SQLITE_MMAP_READWRITE
23191 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
23192 protect = PAGE_READWRITE;
23193 flags |= FILE_MAP_WRITE;
23194 }
23195 #endif
23196 #if SQLITE_OS_WINRT
23197 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
23198 #elif defined(SQLITE_WIN32_HAS_WIDE)
23199 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
23200 (DWORD)((nMap>>32) & 0xffffffff),
23201 (DWORD)(nMap & 0xffffffff), NULL);
23202 #elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
23203 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
23204 (DWORD)((nMap>>32) & 0xffffffff),
23205 (DWORD)(nMap & 0xffffffff), NULL);
23206 #endif
23207 if( pFd->hMap==NULL ){
23208 pFd->lastErrno = osGetLastError();
23209 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
23210 "winMapfile1", pFd->zPath);
23211 /* Log the error, but continue normal operation using xRead/xWrite */
23212 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
23213 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
23214 return SQLITE_OK;
23215 }
23216 assert( (nMap % winSysInfo.dwPageSize)==0 );
23217 assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff );
23218 #if SQLITE_OS_WINRT
23219 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
23220 #else
23221 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
23222 #endif
23223 if( pNew==NULL ){
23224 osCloseHandle(pFd->hMap);
23225 pFd->hMap = NULL;
23226 pFd->lastErrno = osGetLastError();
23227 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
23228 "winMapfile2", pFd->zPath);
23229 /* Log the error, but continue normal operation using xRead/xWrite */
23230 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
23231 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
23232 return SQLITE_OK;
23233 }
23234 pFd->pMapRegion = pNew;
23235 pFd->mmapSize = nMap;
23236 pFd->mmapSizeActual = nMap;
23237 }
23238
23239 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
23240 osGetCurrentProcessId(), pFd));
23241 return SQLITE_OK;
23242 }
23243 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
23244
23245 /*
23246 ** If possible, return a pointer to a mapping of file fd starting at offset
23247 ** iOff. The mapping must be valid for at least nAmt bytes.
23248 **
23249 ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
23250 ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
23251 ** Finally, if an error does occur, return an SQLite error code. The final
23252 ** value of *pp is undefined in this case.
23253 **
23254 ** If this function does return a pointer, the caller must eventually
23255 ** release the reference by calling winUnfetch().
23256 */
23257 static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
23258 #if SQLITE_MAX_MMAP_SIZE>0
23259 winFile *pFd = (winFile*)fd; /* The underlying database file */
23260 #endif
23261 *pp = 0;
23262
23263 OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n",
23264 osGetCurrentProcessId(), fd, iOff, nAmt, pp));
23265
23266 #if SQLITE_MAX_MMAP_SIZE>0
23267 if( pFd->mmapSizeMax>0 ){
23268 if( pFd->pMapRegion==0 ){
23269 int rc = winMapfile(pFd, -1);
23270 if( rc!=SQLITE_OK ){
23271 OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n",
23272 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
23273 return rc;
23274 }
23275 }
23276 if( pFd->mmapSize >= iOff+nAmt ){
23277 *pp = &((u8 *)pFd->pMapRegion)[iOff];
23278 pFd->nFetchOut++;
23279 }
23280 }
23281 #endif
23282
23283 OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n",
23284 osGetCurrentProcessId(), fd, pp, *pp));
23285 return SQLITE_OK;
23286 }
23287
23288 /*
23289 ** If the third argument is non-NULL, then this function releases a
23290 ** reference obtained by an earlier call to winFetch(). The second
23291 ** argument passed to this function must be the same as the corresponding
23292 ** argument that was passed to the winFetch() invocation.
23293 **
23294 ** Or, if the third argument is NULL, then this function is being called
23295 ** to inform the VFS layer that, according to POSIX, any existing mapping
23296 ** may now be invalid and should be unmapped.
23297 */
23298 static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
23299 #if SQLITE_MAX_MMAP_SIZE>0
23300 winFile *pFd = (winFile*)fd; /* The underlying database file */
23301
23302 /* If p==0 (unmap the entire file) then there must be no outstanding
23303 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
23304 ** then there must be at least one outstanding. */
23305 assert( (p==0)==(pFd->nFetchOut==0) );
23306
23307 /* If p!=0, it must match the iOff value. */
23308 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
23309
23310 OSTRACE(("UNFETCH pid=%lu, pFile=%p, offset=%lld, p=%p\n",
23311 osGetCurrentProcessId(), pFd, iOff, p));
23312
23313 if( p ){
23314 pFd->nFetchOut--;
23315 }else{
23316 /* FIXME: If Windows truly always prevents truncating or deleting a
23317 ** file while a mapping is held, then the following winUnmapfile() call
23318 ** is unnecessary can be omitted - potentially improving
23319 ** performance. */
23320 winUnmapfile(pFd);
23321 }
23322
23323 assert( pFd->nFetchOut>=0 );
23324 #endif
23325
23326 OSTRACE(("UNFETCH pid=%lu, pFile=%p, rc=SQLITE_OK\n",
23327 osGetCurrentProcessId(), fd));
23328 return SQLITE_OK;
23329 }
23330
23331 /*
23332 ** Here ends the implementation of all sqlite3_file methods.
23333 **
23334 ********************** End sqlite3_file Methods *******************************
23335 ******************************************************************************/
23336
23337 /*
23338 ** This vector defines all the methods that can operate on an
23339 ** sqlite3_file for win32.
23340 */
23341 static const sqlite3_io_methods winIoMethod = {
23342 3, /* iVersion */
23343 winClose, /* xClose */
23344 winRead, /* xRead */
23345 winWrite, /* xWrite */
23346 winTruncate, /* xTruncate */
23347 winSync, /* xSync */
23348 winFileSize, /* xFileSize */
23349 winLock, /* xLock */
23350 winUnlock, /* xUnlock */
23351 winCheckReservedLock, /* xCheckReservedLock */
23352 winFileControl, /* xFileControl */
23353 winSectorSize, /* xSectorSize */
23354 winDeviceCharacteristics, /* xDeviceCharacteristics */
23355 winShmMap, /* xShmMap */
23356 winShmLock, /* xShmLock */
23357 winShmBarrier, /* xShmBarrier */
23358 winShmUnmap, /* xShmUnmap */
23359 winFetch, /* xFetch */
23360 winUnfetch /* xUnfetch */
23361 };
23362
23363 /*
23364 ** This vector defines all the methods that can operate on an
23365 ** sqlite3_file for win32 without performing any locking.
23366 */
23367 static const sqlite3_io_methods winIoNolockMethod = {
23368 3, /* iVersion */
23369 winClose, /* xClose */
23370 winRead, /* xRead */
23371 winWrite, /* xWrite */
23372 winTruncate, /* xTruncate */
23373 winSync, /* xSync */
23374 winFileSize, /* xFileSize */
23375 winNolockLock, /* xLock */
23376 winNolockUnlock, /* xUnlock */
23377 winNolockCheckReservedLock, /* xCheckReservedLock */
23378 winFileControl, /* xFileControl */
23379 winSectorSize, /* xSectorSize */
23380 winDeviceCharacteristics, /* xDeviceCharacteristics */
23381 winShmMap, /* xShmMap */
23382 winShmLock, /* xShmLock */
23383 winShmBarrier, /* xShmBarrier */
23384 winShmUnmap, /* xShmUnmap */
23385 winFetch, /* xFetch */
23386 winUnfetch /* xUnfetch */
23387 };
23388
23389 static winVfsAppData winAppData = {
23390 &winIoMethod, /* pMethod */
23391 0, /* pAppData */
23392 0 /* bNoLock */
23393 };
23394
23395 static winVfsAppData winNolockAppData = {
23396 &winIoNolockMethod, /* pMethod */
23397 0, /* pAppData */
23398 1 /* bNoLock */
23399 };
23400
23401 /****************************************************************************
23402 **************************** sqlite3_vfs methods ****************************
23403 **
23404 ** This division contains the implementation of methods on the
23405 ** sqlite3_vfs object.
23406 */
23407
23408 #if defined(__CYGWIN__)
23409 /*
23410 ** Convert a filename from whatever the underlying operating system
23411 ** supports for filenames into UTF-8. Space to hold the result is
23412 ** obtained from malloc and must be freed by the calling function.
23413 */
23414 static char *winConvertToUtf8Filename(const void *zFilename){
23415 char *zConverted = 0;
23416 if( osIsNT() ){
23417 zConverted = winUnicodeToUtf8(zFilename);
23418 }
23419 #ifdef SQLITE_WIN32_HAS_ANSI
23420 else{
23421 zConverted = winMbcsToUtf8(zFilename, osAreFileApisANSI());
23422 }
23423 #endif
23424 /* caller will handle out of memory */
23425 return zConverted;
23426 }
23427 #endif
23428
23429 /*
23430 ** Convert a UTF-8 filename into whatever form the underlying
23431 ** operating system wants filenames in. Space to hold the result
23432 ** is obtained from malloc and must be freed by the calling
23433 ** function.
23434 */
23435 static void *winConvertFromUtf8Filename(const char *zFilename){
23436 void *zConverted = 0;
23437 if( osIsNT() ){
23438 zConverted = winUtf8ToUnicode(zFilename);
23439 }
23440 #ifdef SQLITE_WIN32_HAS_ANSI
23441 else{
23442 zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
23443 }
23444 #endif
23445 /* caller will handle out of memory */
23446 return zConverted;
23447 }
23448
23449 /*
23450 ** This function returns non-zero if the specified UTF-8 string buffer
23451 ** ends with a directory separator character or one was successfully
23452 ** added to it.
23453 */
23454 static int winMakeEndInDirSep(int nBuf, char *zBuf){
23455 if( zBuf ){
23456 int nLen = sqlite3Strlen30(zBuf);
23457 if( nLen>0 ){
23458 if( winIsDirSep(zBuf[nLen-1]) ){
23459 return 1;
23460 }else if( nLen+1<nBuf ){
23461 zBuf[nLen] = winGetDirSep();
23462 zBuf[nLen+1] = '\0';
23463 return 1;
23464 }
23465 }
23466 }
23467 return 0;
23468 }
23469
23470 /*
23471 ** Create a temporary file name and store the resulting pointer into pzBuf.
23472 ** The pointer returned in pzBuf must be freed via sqlite3_free().
23473 */
23474 static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
23475 static char zChars[] =
23476 "abcdefghijklmnopqrstuvwxyz"
23477 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
23478 "0123456789";
23479 size_t i, j;
23480 int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX);
23481 int nMax, nBuf, nDir, nLen;
23482 char *zBuf;
23483
23484 /* It's odd to simulate an io-error here, but really this is just
23485 ** using the io-error infrastructure to test that SQLite handles this
23486 ** function failing.
23487 */
23488 SimulateIOError( return SQLITE_IOERR );
23489
23490 /* Allocate a temporary buffer to store the fully qualified file
23491 ** name for the temporary file. If this fails, we cannot continue.
23492 */
23493 nMax = pVfs->mxPathname; nBuf = nMax + 2;
23494 zBuf = sqlite3MallocZero( nBuf );
23495 if( !zBuf ){
23496 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
23497 return SQLITE_IOERR_NOMEM_BKPT;
23498 }
23499
23500 /* Figure out the effective temporary directory. First, check if one
23501 ** has been explicitly set by the application; otherwise, use the one
23502 ** configured by the operating system.
23503 */
23504 nDir = nMax - (nPre + 15);
23505 assert( nDir>0 );
23506 if( sqlite3_temp_directory ){
23507 int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
23508 if( nDirLen>0 ){
23509 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
23510 nDirLen++;
23511 }
23512 if( nDirLen>nDir ){
23513 sqlite3_free(zBuf);
23514 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
23515 return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0);
23516 }
23517 sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
23518 }
23519 }
23520 #if defined(__CYGWIN__)
23521 else{
23522 static const char *azDirs[] = {
23523 0, /* getenv("SQLITE_TMPDIR") */
23524 0, /* getenv("TMPDIR") */
23525 0, /* getenv("TMP") */
23526 0, /* getenv("TEMP") */
23527 0, /* getenv("USERPROFILE") */
23528 "/var/tmp",
23529 "/usr/tmp",
23530 "/tmp",
23531 ".",
23532 0 /* List terminator */
23533 };
23534 unsigned int i;
23535 const char *zDir = 0;
23536
23537 if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
23538 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
23539 if( !azDirs[2] ) azDirs[2] = getenv("TMP");
23540 if( !azDirs[3] ) azDirs[3] = getenv("TEMP");
23541 if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE");
23542 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
23543 void *zConverted;
23544 if( zDir==0 ) continue;
23545 /* If the path starts with a drive letter followed by the colon
23546 ** character, assume it is already a native Win32 path; otherwise,
23547 ** it must be converted to a native Win32 path via the Cygwin API
23548 ** prior to using it.
23549 */
23550 if( winIsDriveLetterAndColon(zDir) ){
23551 zConverted = winConvertFromUtf8Filename(zDir);
23552 if( !zConverted ){
23553 sqlite3_free(zBuf);
23554 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
23555 return SQLITE_IOERR_NOMEM_BKPT;
23556 }
23557 if( winIsDir(zConverted) ){
23558 sqlite3_snprintf(nMax, zBuf, "%s", zDir);
23559 sqlite3_free(zConverted);
23560 break;
23561 }
23562 sqlite3_free(zConverted);
23563 }else{
23564 zConverted = sqlite3MallocZero( nMax+1 );
23565 if( !zConverted ){
23566 sqlite3_free(zBuf);
23567 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
23568 return SQLITE_IOERR_NOMEM_BKPT;
23569 }
23570 if( cygwin_conv_path(
23571 osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
23572 zConverted, nMax+1)<0 ){
23573 sqlite3_free(zConverted);
23574 sqlite3_free(zBuf);
23575 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
23576 return winLogError(SQLITE_IOERR_CONVPATH, (DWORD)errno,
23577 "winGetTempname2", zDir);
23578 }
23579 if( winIsDir(zConverted) ){
23580 /* At this point, we know the candidate directory exists and should
23581 ** be used. However, we may need to convert the string containing
23582 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
23583 */
23584 char *zUtf8 = winConvertToUtf8Filename(zConverted);
23585 if( !zUtf8 ){
23586 sqlite3_free(zConverted);
23587 sqlite3_free(zBuf);
23588 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
23589 return SQLITE_IOERR_NOMEM_BKPT;
23590 }
23591 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
23592 sqlite3_free(zUtf8);
23593 sqlite3_free(zConverted);
23594 break;
23595 }
23596 sqlite3_free(zConverted);
23597 }
23598 }
23599 }
23600 #elif !SQLITE_OS_WINRT && !defined(__CYGWIN__)
23601 else if( osIsNT() ){
23602 char *zMulti;
23603 LPWSTR zWidePath = sqlite3MallocZero( nMax*sizeof(WCHAR) );
23604 if( !zWidePath ){
23605 sqlite3_free(zBuf);
23606 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
23607 return SQLITE_IOERR_NOMEM_BKPT;
23608 }
23609 if( osGetTempPathW(nMax, zWidePath)==0 ){
23610 sqlite3_free(zWidePath);
23611 sqlite3_free(zBuf);
23612 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
23613 return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
23614 "winGetTempname2", 0);
23615 }
23616 zMulti = winUnicodeToUtf8(zWidePath);
23617 if( zMulti ){
23618 sqlite3_snprintf(nMax, zBuf, "%s", zMulti);
23619 sqlite3_free(zMulti);
23620 sqlite3_free(zWidePath);
23621 }else{
23622 sqlite3_free(zWidePath);
23623 sqlite3_free(zBuf);
23624 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
23625 return SQLITE_IOERR_NOMEM_BKPT;
23626 }
23627 }
23628 #ifdef SQLITE_WIN32_HAS_ANSI
23629 else{
23630 char *zUtf8;
23631 char *zMbcsPath = sqlite3MallocZero( nMax );
23632 if( !zMbcsPath ){
23633 sqlite3_free(zBuf);
23634 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
23635 return SQLITE_IOERR_NOMEM_BKPT;
23636 }
23637 if( osGetTempPathA(nMax, zMbcsPath)==0 ){
23638 sqlite3_free(zBuf);
23639 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
23640 return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
23641 "winGetTempname3", 0);
23642 }
23643 zUtf8 = winMbcsToUtf8(zMbcsPath, osAreFileApisANSI());
23644 if( zUtf8 ){
23645 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
23646 sqlite3_free(zUtf8);
23647 }else{
23648 sqlite3_free(zBuf);
23649 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
23650 return SQLITE_IOERR_NOMEM_BKPT;
23651 }
23652 }
23653 #endif /* SQLITE_WIN32_HAS_ANSI */
23654 #endif /* !SQLITE_OS_WINRT */
23655
23656 /*
23657 ** Check to make sure the temporary directory ends with an appropriate
23658 ** separator. If it does not and there is not enough space left to add
23659 ** one, fail.
23660 */
23661 if( !winMakeEndInDirSep(nDir+1, zBuf) ){
23662 sqlite3_free(zBuf);
23663 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
23664 return winLogError(SQLITE_ERROR, 0, "winGetTempname4", 0);
23665 }
23666
23667 /*
23668 ** Check that the output buffer is large enough for the temporary file
23669 ** name in the following format:
23670 **
23671 ** "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
23672 **
23673 ** If not, return SQLITE_ERROR. The number 17 is used here in order to
23674 ** account for the space used by the 15 character random suffix and the
23675 ** two trailing NUL characters. The final directory separator character
23676 ** has already added if it was not already present.
23677 */
23678 nLen = sqlite3Strlen30(zBuf);
23679 if( (nLen + nPre + 17) > nBuf ){
23680 sqlite3_free(zBuf);
23681 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
23682 return winLogError(SQLITE_ERROR, 0, "winGetTempname5", 0);
23683 }
23684
23685 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
23686
23687 j = sqlite3Strlen30(zBuf);
23688 sqlite3_randomness(15, &zBuf[j]);
23689 for(i=0; i<15; i++, j++){
23690 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
23691 }
23692 zBuf[j] = 0;
23693 zBuf[j+1] = 0;
23694 *pzBuf = zBuf;
23695
23696 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
23697 return SQLITE_OK;
23698 }
23699
23700 /*
23701 ** Return TRUE if the named file is really a directory. Return false if
23702 ** it is something other than a directory, or if there is any kind of memory
23703 ** allocation failure.
23704 */
23705 static int winIsDir(const void *zConverted){
23706 DWORD attr;
23707 int rc = 0;
23708 DWORD lastErrno;
23709
23710 if( osIsNT() ){
23711 int cnt = 0;
23712 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
23713 memset(&sAttrData, 0, sizeof(sAttrData));
23714 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
23715 GetFileExInfoStandard,
23716 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
23717 if( !rc ){
23718 return 0; /* Invalid name? */
23719 }
23720 attr = sAttrData.dwFileAttributes;
23721 #if SQLITE_OS_WINCE==0
23722 }else{
23723 attr = osGetFileAttributesA((char*)zConverted);
23724 #endif
23725 }
23726 return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
23727 }
23728
23729 /*
23730 ** Open a file.
23731 */
23732 static int winOpen(
23733 sqlite3_vfs *pVfs, /* Used to get maximum path length and AppData */
23734 const char *zName, /* Name of the file (UTF-8) */
23735 sqlite3_file *id, /* Write the SQLite file handle here */
23736 int flags, /* Open mode flags */
23737 int *pOutFlags /* Status return flags */
23738 ){
23739 HANDLE h;
23740 DWORD lastErrno = 0;
23741 DWORD dwDesiredAccess;
23742 DWORD dwShareMode;
23743 DWORD dwCreationDisposition;
23744 DWORD dwFlagsAndAttributes = 0;
23745 #if SQLITE_OS_WINCE
23746 int isTemp = 0;
23747 #endif
23748 winVfsAppData *pAppData;
23749 winFile *pFile = (winFile*)id;
23750 void *zConverted; /* Filename in OS encoding */
23751 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
23752 int cnt = 0;
23753
23754 /* If argument zPath is a NULL pointer, this function is required to open
23755 ** a temporary file. Use this buffer to store the file name in.
23756 */
23757 char *zTmpname = 0; /* For temporary filename, if necessary. */
23758
23759 int rc = SQLITE_OK; /* Function Return Code */
23760 #if !defined(NDEBUG) || SQLITE_OS_WINCE
23761 int eType = flags&0xFFFFFF00; /* Type of file to open */
23762 #endif
23763
23764 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
23765 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
23766 int isCreate = (flags & SQLITE_OPEN_CREATE);
23767 int isReadonly = (flags & SQLITE_OPEN_READONLY);
23768 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
23769
23770 #ifndef NDEBUG
23771 int isOpenJournal = (isCreate && (
23772 eType==SQLITE_OPEN_MASTER_JOURNAL
23773 || eType==SQLITE_OPEN_MAIN_JOURNAL
23774 || eType==SQLITE_OPEN_WAL
23775 ));
23776 #endif
23777
23778 OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
23779 zUtf8Name, id, flags, pOutFlags));
23780
23781 /* Check the following statements are true:
23782 **
23783 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
23784 ** (b) if CREATE is set, then READWRITE must also be set, and
23785 ** (c) if EXCLUSIVE is set, then CREATE must also be set.
23786 ** (d) if DELETEONCLOSE is set, then CREATE must also be set.
23787 */
23788 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
23789 assert(isCreate==0 || isReadWrite);
23790 assert(isExclusive==0 || isCreate);
23791 assert(isDelete==0 || isCreate);
23792
23793 /* The main DB, main journal, WAL file and master journal are never
23794 ** automatically deleted. Nor are they ever temporary files. */
23795 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
23796 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
23797 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
23798 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
23799
23800 /* Assert that the upper layer has set one of the "file-type" flags. */
23801 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB
23802 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
23803 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL
23804 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
23805 );
23806
23807 assert( pFile!=0 );
23808 memset(pFile, 0, sizeof(winFile));
23809 pFile->h = INVALID_HANDLE_VALUE;
23810
23811 #if SQLITE_OS_WINRT
23812 if( !zUtf8Name && !sqlite3_temp_directory ){
23813 sqlite3_log(SQLITE_ERROR,
23814 "sqlite3_temp_directory variable should be set for WinRT");
23815 }
23816 #endif
23817
23818 /* If the second argument to this function is NULL, generate a
23819 ** temporary file name to use
23820 */
23821 if( !zUtf8Name ){
23822 assert( isDelete && !isOpenJournal );
23823 rc = winGetTempname(pVfs, &zTmpname);
23824 if( rc!=SQLITE_OK ){
23825 OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
23826 return rc;
23827 }
23828 zUtf8Name = zTmpname;
23829 }
23830
23831 /* Database filenames are double-zero terminated if they are not
23832 ** URIs with parameters. Hence, they can always be passed into
23833 ** sqlite3_uri_parameter().
23834 */
23835 assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
23836 zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 );
23837
23838 /* Convert the filename to the system encoding. */
23839 zConverted = winConvertFromUtf8Filename(zUtf8Name);
23840 if( zConverted==0 ){
23841 sqlite3_free(zTmpname);
23842 OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
23843 return SQLITE_IOERR_NOMEM_BKPT;
23844 }
23845
23846 if( winIsDir(zConverted) ){
23847 sqlite3_free(zConverted);
23848 sqlite3_free(zTmpname);
23849 OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
23850 return SQLITE_CANTOPEN_ISDIR;
23851 }
23852
23853 if( isReadWrite ){
23854 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
23855 }else{
23856 dwDesiredAccess = GENERIC_READ;
23857 }
23858
23859 /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
23860 ** created. SQLite doesn't use it to indicate "exclusive access"
23861 ** as it is usually understood.
23862 */
23863 if( isExclusive ){
23864 /* Creates a new file, only if it does not already exist. */
23865 /* If the file exists, it fails. */
23866 dwCreationDisposition = CREATE_NEW;
23867 }else if( isCreate ){
23868 /* Open existing file, or create if it doesn't exist */
23869 dwCreationDisposition = OPEN_ALWAYS;
23870 }else{
23871 /* Opens a file, only if it exists. */
23872 dwCreationDisposition = OPEN_EXISTING;
23873 }
23874
23875 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
23876
23877 if( isDelete ){
23878 #if SQLITE_OS_WINCE
23879 dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
23880 isTemp = 1;
23881 #else
23882 dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
23883 | FILE_ATTRIBUTE_HIDDEN
23884 | FILE_FLAG_DELETE_ON_CLOSE;
23885 #endif
23886 }else{
23887 dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
23888 }
23889 /* Reports from the internet are that performance is always
23890 ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */
23891 #if SQLITE_OS_WINCE
23892 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
23893 #endif
23894
23895 if( osIsNT() ){
23896 #if SQLITE_OS_WINRT
23897 CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
23898 extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
23899 extendedParameters.dwFileAttributes =
23900 dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
23901 extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
23902 extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
23903 extendedParameters.lpSecurityAttributes = NULL;
23904 extendedParameters.hTemplateFile = NULL;
23905 while( (h = osCreateFile2((LPCWSTR)zConverted,
23906 dwDesiredAccess,
23907 dwShareMode,
23908 dwCreationDisposition,
23909 &extendedParameters))==INVALID_HANDLE_VALUE &&
23910 winRetryIoerr(&cnt, &lastErrno) ){
23911 /* Noop */
23912 }
23913 #else
23914 while( (h = osCreateFileW((LPCWSTR)zConverted,
23915 dwDesiredAccess,
23916 dwShareMode, NULL,
23917 dwCreationDisposition,
23918 dwFlagsAndAttributes,
23919 NULL))==INVALID_HANDLE_VALUE &&
23920 winRetryIoerr(&cnt, &lastErrno) ){
23921 /* Noop */
23922 }
23923 #endif
23924 }
23925 #ifdef SQLITE_WIN32_HAS_ANSI
23926 else{
23927 while( (h = osCreateFileA((LPCSTR)zConverted,
23928 dwDesiredAccess,
23929 dwShareMode, NULL,
23930 dwCreationDisposition,
23931 dwFlagsAndAttributes,
23932 NULL))==INVALID_HANDLE_VALUE &&
23933 winRetryIoerr(&cnt, &lastErrno) ){
23934 /* Noop */
23935 }
23936 }
23937 #endif
23938 winLogIoerr(cnt, __LINE__);
23939
23940 OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
23941 dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
23942
23943 if( h==INVALID_HANDLE_VALUE ){
23944 pFile->lastErrno = lastErrno;
23945 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
23946 sqlite3_free(zConverted);
23947 sqlite3_free(zTmpname);
23948 if( isReadWrite && !isExclusive ){
23949 return winOpen(pVfs, zName, id,
23950 ((flags|SQLITE_OPEN_READONLY) &
23951 ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
23952 pOutFlags);
23953 }else{
23954 return SQLITE_CANTOPEN_BKPT;
23955 }
23956 }
23957
23958 if( pOutFlags ){
23959 if( isReadWrite ){
23960 *pOutFlags = SQLITE_OPEN_READWRITE;
23961 }else{
23962 *pOutFlags = SQLITE_OPEN_READONLY;
23963 }
23964 }
23965
23966 OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, "
23967 "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ?
23968 *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
23969
23970 pAppData = (winVfsAppData*)pVfs->pAppData;
23971
23972 #if SQLITE_OS_WINCE
23973 {
23974 if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
23975 && ((pAppData==NULL) || !pAppData->bNoLock)
23976 && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
23977 ){
23978 osCloseHandle(h);
23979 sqlite3_free(zConverted);
23980 sqlite3_free(zTmpname);
23981 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
23982 return rc;
23983 }
23984 }
23985 if( isTemp ){
23986 pFile->zDeleteOnClose = zConverted;
23987 }else
23988 #endif
23989 {
23990 sqlite3_free(zConverted);
23991 }
23992
23993 sqlite3_free(zTmpname);
23994 pFile->pMethod = pAppData ? pAppData->pMethod : &winIoMethod;
23995 pFile->pVfs = pVfs;
23996 pFile->h = h;
23997 if( isReadonly ){
23998 pFile->ctrlFlags |= WINFILE_RDONLY;
23999 }
24000 if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
24001 pFile->ctrlFlags |= WINFILE_PSOW;
24002 }
24003 pFile->lastErrno = NO_ERROR;
24004 pFile->zPath = zName;
24005 #if SQLITE_MAX_MMAP_SIZE>0
24006 pFile->hMap = NULL;
24007 pFile->pMapRegion = 0;
24008 pFile->mmapSize = 0;
24009 pFile->mmapSizeActual = 0;
24010 pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
24011 #endif
24012
24013 OpenCounter(+1);
24014 return rc;
24015 }
24016
24017 /*
24018 ** Delete the named file.
24019 **
24020 ** Note that Windows does not allow a file to be deleted if some other
24021 ** process has it open. Sometimes a virus scanner or indexing program
24022 ** will open a journal file shortly after it is created in order to do
24023 ** whatever it does. While this other process is holding the
24024 ** file open, we will be unable to delete it. To work around this
24025 ** problem, we delay 100 milliseconds and try to delete again. Up
24026 ** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
24027 ** up and returning an error.
24028 */
24029 static int winDelete(
24030 sqlite3_vfs *pVfs, /* Not used on win32 */
24031 const char *zFilename, /* Name of file to delete */
24032 int syncDir /* Not used on win32 */
24033 ){
24034 int cnt = 0;
24035 int rc;
24036 DWORD attr;
24037 DWORD lastErrno = 0;
24038 void *zConverted;
24039 UNUSED_PARAMETER(pVfs);
24040 UNUSED_PARAMETER(syncDir);
24041
24042 SimulateIOError(return SQLITE_IOERR_DELETE);
24043 OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
24044
24045 zConverted = winConvertFromUtf8Filename(zFilename);
24046 if( zConverted==0 ){
24047 OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
24048 return SQLITE_IOERR_NOMEM_BKPT;
24049 }
24050 if( osIsNT() ){
24051 do {
24052 #if SQLITE_OS_WINRT
24053 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
24054 memset(&sAttrData, 0, sizeof(sAttrData));
24055 if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
24056 &sAttrData) ){
24057 attr = sAttrData.dwFileAttributes;
24058 }else{
24059 lastErrno = osGetLastError();
24060 if( lastErrno==ERROR_FILE_NOT_FOUND
24061 || lastErrno==ERROR_PATH_NOT_FOUND ){
24062 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
24063 }else{
24064 rc = SQLITE_ERROR;
24065 }
24066 break;
24067 }
24068 #else
24069 attr = osGetFileAttributesW(zConverted);
24070 #endif
24071 if ( attr==INVALID_FILE_ATTRIBUTES ){
24072 lastErrno = osGetLastError();
24073 if( lastErrno==ERROR_FILE_NOT_FOUND
24074 || lastErrno==ERROR_PATH_NOT_FOUND ){
24075 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
24076 }else{
24077 rc = SQLITE_ERROR;
24078 }
24079 break;
24080 }
24081 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
24082 rc = SQLITE_ERROR; /* Files only. */
24083 break;
24084 }
24085 if ( osDeleteFileW(zConverted) ){
24086 rc = SQLITE_OK; /* Deleted OK. */
24087 break;
24088 }
24089 if ( !winRetryIoerr(&cnt, &lastErrno) ){
24090 rc = SQLITE_ERROR; /* No more retries. */
24091 break;
24092 }
24093 } while(1);
24094 }
24095 #ifdef SQLITE_WIN32_HAS_ANSI
24096 else{
24097 do {
24098 attr = osGetFileAttributesA(zConverted);
24099 if ( attr==INVALID_FILE_ATTRIBUTES ){
24100 lastErrno = osGetLastError();
24101 if( lastErrno==ERROR_FILE_NOT_FOUND
24102 || lastErrno==ERROR_PATH_NOT_FOUND ){
24103 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
24104 }else{
24105 rc = SQLITE_ERROR;
24106 }
24107 break;
24108 }
24109 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
24110 rc = SQLITE_ERROR; /* Files only. */
24111 break;
24112 }
24113 if ( osDeleteFileA(zConverted) ){
24114 rc = SQLITE_OK; /* Deleted OK. */
24115 break;
24116 }
24117 if ( !winRetryIoerr(&cnt, &lastErrno) ){
24118 rc = SQLITE_ERROR; /* No more retries. */
24119 break;
24120 }
24121 } while(1);
24122 }
24123 #endif
24124 if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
24125 rc = winLogError(SQLITE_IOERR_DELETE, lastErrno, "winDelete", zFilename);
24126 }else{
24127 winLogIoerr(cnt, __LINE__);
24128 }
24129 sqlite3_free(zConverted);
24130 OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
24131 return rc;
24132 }
24133
24134 /*
24135 ** Check the existence and status of a file.
24136 */
24137 static int winAccess(
24138 sqlite3_vfs *pVfs, /* Not used on win32 */
24139 const char *zFilename, /* Name of file to check */
24140 int flags, /* Type of test to make on this file */
24141 int *pResOut /* OUT: Result */
24142 ){
24143 DWORD attr;
24144 int rc = 0;
24145 DWORD lastErrno = 0;
24146 void *zConverted;
24147 UNUSED_PARAMETER(pVfs);
24148
24149 SimulateIOError( return SQLITE_IOERR_ACCESS; );
24150 OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
24151 zFilename, flags, pResOut));
24152
24153 zConverted = winConvertFromUtf8Filename(zFilename);
24154 if( zConverted==0 ){
24155 OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
24156 return SQLITE_IOERR_NOMEM_BKPT;
24157 }
24158 if( osIsNT() ){
24159 int cnt = 0;
24160 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
24161 memset(&sAttrData, 0, sizeof(sAttrData));
24162 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
24163 GetFileExInfoStandard,
24164 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
24165 if( rc ){
24166 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
24167 ** as if it does not exist.
24168 */
24169 if( flags==SQLITE_ACCESS_EXISTS
24170 && sAttrData.nFileSizeHigh==0
24171 && sAttrData.nFileSizeLow==0 ){
24172 attr = INVALID_FILE_ATTRIBUTES;
24173 }else{
24174 attr = sAttrData.dwFileAttributes;
24175 }
24176 }else{
24177 winLogIoerr(cnt, __LINE__);
24178 if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
24179 sqlite3_free(zConverted);
24180 return winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess",
24181 zFilename);
24182 }else{
24183 attr = INVALID_FILE_ATTRIBUTES;
24184 }
24185 }
24186 }
24187 #ifdef SQLITE_WIN32_HAS_ANSI
24188 else{
24189 attr = osGetFileAttributesA((char*)zConverted);
24190 }
24191 #endif
24192 sqlite3_free(zConverted);
24193 switch( flags ){
24194 case SQLITE_ACCESS_READ:
24195 case SQLITE_ACCESS_EXISTS:
24196 rc = attr!=INVALID_FILE_ATTRIBUTES;
24197 break;
24198 case SQLITE_ACCESS_READWRITE:
24199 rc = attr!=INVALID_FILE_ATTRIBUTES &&
24200 (attr & FILE_ATTRIBUTE_READONLY)==0;
24201 break;
24202 default:
24203 assert(!"Invalid flags argument");
24204 }
24205 *pResOut = rc;
24206 OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
24207 zFilename, pResOut, *pResOut));
24208 return SQLITE_OK;
24209 }
24210
24211 /*
24212 ** Returns non-zero if the specified path name starts with a drive letter
24213 ** followed by a colon character.
24214 */
24215 static BOOL winIsDriveLetterAndColon(
24216 const char *zPathname
24217 ){
24218 return ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' );
24219 }
24220
24221 /*
24222 ** Returns non-zero if the specified path name should be used verbatim. If
24223 ** non-zero is returned from this function, the calling function must simply
24224 ** use the provided path name verbatim -OR- resolve it into a full path name
24225 ** using the GetFullPathName Win32 API function (if available).
24226 */
24227 static BOOL winIsVerbatimPathname(
24228 const char *zPathname
24229 ){
24230 /*
24231 ** If the path name starts with a forward slash or a backslash, it is either
24232 ** a legal UNC name, a volume relative path, or an absolute path name in the
24233 ** "Unix" format on Windows. There is no easy way to differentiate between
24234 ** the final two cases; therefore, we return the safer return value of TRUE
24235 ** so that callers of this function will simply use it verbatim.
24236 */
24237 if ( winIsDirSep(zPathname[0]) ){
24238 return TRUE;
24239 }
24240
24241 /*
24242 ** If the path name starts with a letter and a colon it is either a volume
24243 ** relative path or an absolute path. Callers of this function must not
24244 ** attempt to treat it as a relative path name (i.e. they should simply use
24245 ** it verbatim).
24246 */
24247 if ( winIsDriveLetterAndColon(zPathname) ){
24248 return TRUE;
24249 }
24250
24251 /*
24252 ** If we get to this point, the path name should almost certainly be a purely
24253 ** relative one (i.e. not a UNC name, not absolute, and not volume relative).
24254 */
24255 return FALSE;
24256 }
24257
24258 /*
24259 ** Turn a relative pathname into a full pathname. Write the full
24260 ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
24261 ** bytes in size.
24262 */
24263 static int winFullPathname(
24264 sqlite3_vfs *pVfs, /* Pointer to vfs object */
24265 const char *zRelative, /* Possibly relative input path */
24266 int nFull, /* Size of output buffer in bytes */
24267 char *zFull /* Output buffer */
24268 ){
24269 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
24270 DWORD nByte;
24271 void *zConverted;
24272 char *zOut;
24273 #endif
24274
24275 /* If this path name begins with "/X:", where "X" is any alphabetic
24276 ** character, discard the initial "/" from the pathname.
24277 */
24278 if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){
24279 zRelative++;
24280 }
24281
24282 #if defined(__CYGWIN__)
24283 SimulateIOError( return SQLITE_ERROR );
24284 UNUSED_PARAMETER(nFull);
24285 assert( nFull>=pVfs->mxPathname );
24286 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
24287 /*
24288 ** NOTE: We are dealing with a relative path name and the data
24289 ** directory has been set. Therefore, use it as the basis
24290 ** for converting the relative path name to an absolute
24291 ** one by prepending the data directory and a slash.
24292 */
24293 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
24294 if( !zOut ){
24295 return SQLITE_IOERR_NOMEM_BKPT;
24296 }
24297 if( cygwin_conv_path(
24298 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) |
24299 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
24300 sqlite3_free(zOut);
24301 return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
24302 "winFullPathname1", zRelative);
24303 }else{
24304 char *zUtf8 = winConvertToUtf8Filename(zOut);
24305 if( !zUtf8 ){
24306 sqlite3_free(zOut);
24307 return SQLITE_IOERR_NOMEM_BKPT;
24308 }
24309 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
24310 sqlite3_data_directory, winGetDirSep(), zUtf8);
24311 sqlite3_free(zUtf8);
24312 sqlite3_free(zOut);
24313 }
24314 }else{
24315 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
24316 if( !zOut ){
24317 return SQLITE_IOERR_NOMEM_BKPT;
24318 }
24319 if( cygwin_conv_path(
24320 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A),
24321 zRelative, zOut, pVfs->mxPathname+1)<0 ){
24322 sqlite3_free(zOut);
24323 return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
24324 "winFullPathname2", zRelative);
24325 }else{
24326 char *zUtf8 = winConvertToUtf8Filename(zOut);
24327 if( !zUtf8 ){
24328 sqlite3_free(zOut);
24329 return SQLITE_IOERR_NOMEM_BKPT;
24330 }
24331 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
24332 sqlite3_free(zUtf8);
24333 sqlite3_free(zOut);
24334 }
24335 }
24336 return SQLITE_OK;
24337 #endif
24338
24339 #if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
24340 SimulateIOError( return SQLITE_ERROR );
24341 /* WinCE has no concept of a relative pathname, or so I am told. */
24342 /* WinRT has no way to convert a relative path to an absolute one. */
24343 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
24344 /*
24345 ** NOTE: We are dealing with a relative path name and the data
24346 ** directory has been set. Therefore, use it as the basis
24347 ** for converting the relative path name to an absolute
24348 ** one by prepending the data directory and a backslash.
24349 */
24350 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
24351 sqlite3_data_directory, winGetDirSep(), zRelative);
24352 }else{
24353 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
24354 }
24355 return SQLITE_OK;
24356 #endif
24357
24358 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
24359 /* It's odd to simulate an io-error here, but really this is just
24360 ** using the io-error infrastructure to test that SQLite handles this
24361 ** function failing. This function could fail if, for example, the
24362 ** current working directory has been unlinked.
24363 */
24364 SimulateIOError( return SQLITE_ERROR );
24365 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
24366 /*
24367 ** NOTE: We are dealing with a relative path name and the data
24368 ** directory has been set. Therefore, use it as the basis
24369 ** for converting the relative path name to an absolute
24370 ** one by prepending the data directory and a backslash.
24371 */
24372 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
24373 sqlite3_data_directory, winGetDirSep(), zRelative);
24374 return SQLITE_OK;
24375 }
24376 zConverted = winConvertFromUtf8Filename(zRelative);
24377 if( zConverted==0 ){
24378 return SQLITE_IOERR_NOMEM_BKPT;
24379 }
24380 if( osIsNT() ){
24381 LPWSTR zTemp;
24382 nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
24383 if( nByte==0 ){
24384 sqlite3_free(zConverted);
24385 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
24386 "winFullPathname1", zRelative);
24387 }
24388 nByte += 3;
24389 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
24390 if( zTemp==0 ){
24391 sqlite3_free(zConverted);
24392 return SQLITE_IOERR_NOMEM_BKPT;
24393 }
24394 nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
24395 if( nByte==0 ){
24396 sqlite3_free(zConverted);
24397 sqlite3_free(zTemp);
24398 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
24399 "winFullPathname2", zRelative);
24400 }
24401 sqlite3_free(zConverted);
24402 zOut = winUnicodeToUtf8(zTemp);
24403 sqlite3_free(zTemp);
24404 }
24405 #ifdef SQLITE_WIN32_HAS_ANSI
24406 else{
24407 char *zTemp;
24408 nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
24409 if( nByte==0 ){
24410 sqlite3_free(zConverted);
24411 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
24412 "winFullPathname3", zRelative);
24413 }
24414 nByte += 3;
24415 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
24416 if( zTemp==0 ){
24417 sqlite3_free(zConverted);
24418 return SQLITE_IOERR_NOMEM_BKPT;
24419 }
24420 nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
24421 if( nByte==0 ){
24422 sqlite3_free(zConverted);
24423 sqlite3_free(zTemp);
24424 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
24425 "winFullPathname4", zRelative);
24426 }
24427 sqlite3_free(zConverted);
24428 zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
24429 sqlite3_free(zTemp);
24430 }
24431 #endif
24432 if( zOut ){
24433 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
24434 sqlite3_free(zOut);
24435 return SQLITE_OK;
24436 }else{
24437 return SQLITE_IOERR_NOMEM_BKPT;
24438 }
24439 #endif
24440 }
24441
24442 #ifndef SQLITE_OMIT_LOAD_EXTENSION
24443 /*
24444 ** Interfaces for opening a shared library, finding entry points
24445 ** within the shared library, and closing the shared library.
24446 */
24447 static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
24448 HANDLE h;
24449 #if defined(__CYGWIN__)
24450 int nFull = pVfs->mxPathname+1;
24451 char *zFull = sqlite3MallocZero( nFull );
24452 void *zConverted = 0;
24453 if( zFull==0 ){
24454 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
24455 return 0;
24456 }
24457 if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK ){
24458 sqlite3_free(zFull);
24459 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
24460 return 0;
24461 }
24462 zConverted = winConvertFromUtf8Filename(zFull);
24463 sqlite3_free(zFull);
24464 #else
24465 void *zConverted = winConvertFromUtf8Filename(zFilename);
24466 UNUSED_PARAMETER(pVfs);
24467 #endif
24468 if( zConverted==0 ){
24469 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
24470 return 0;
24471 }
24472 if( osIsNT() ){
24473 #if SQLITE_OS_WINRT
24474 h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
24475 #else
24476 h = osLoadLibraryW((LPCWSTR)zConverted);
24477 #endif
24478 }
24479 #ifdef SQLITE_WIN32_HAS_ANSI
24480 else{
24481 h = osLoadLibraryA((char*)zConverted);
24482 }
24483 #endif
24484 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h));
24485 sqlite3_free(zConverted);
24486 return (void*)h;
24487 }
24488 static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
24489 UNUSED_PARAMETER(pVfs);
24490 winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
24491 }
24492 static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
24493 FARPROC proc;
24494 UNUSED_PARAMETER(pVfs);
24495 proc = osGetProcAddressA((HANDLE)pH, zSym);
24496 OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n",
24497 (void*)pH, zSym, (void*)proc));
24498 return (void(*)(void))proc;
24499 }
24500 static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
24501 UNUSED_PARAMETER(pVfs);
24502 osFreeLibrary((HANDLE)pHandle);
24503 OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle));
24504 }
24505 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
24506 #define winDlOpen 0
24507 #define winDlError 0
24508 #define winDlSym 0
24509 #define winDlClose 0
24510 #endif
24511
24512 /* State information for the randomness gatherer. */
24513 typedef struct EntropyGatherer EntropyGatherer;
24514 struct EntropyGatherer {
24515 unsigned char *a; /* Gather entropy into this buffer */
24516 int na; /* Size of a[] in bytes */
24517 int i; /* XOR next input into a[i] */
24518 int nXor; /* Number of XOR operations done */
24519 };
24520
24521 #if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS)
24522 /* Mix sz bytes of entropy into p. */
24523 static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){
24524 int j, k;
24525 for(j=0, k=p->i; j<sz; j++){
24526 p->a[k++] ^= x[j];
24527 if( k>=p->na ) k = 0;
24528 }
24529 p->i = k;
24530 p->nXor += sz;
24531 }
24532 #endif /* !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) */
24533
24534 /*
24535 ** Write up to nBuf bytes of randomness into zBuf.
24536 */
24537 static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
24538 #if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS)
24539 UNUSED_PARAMETER(pVfs);
24540 memset(zBuf, 0, nBuf);
24541 return nBuf;
24542 #else
24543 EntropyGatherer e;
24544 UNUSED_PARAMETER(pVfs);
24545 memset(zBuf, 0, nBuf);
24546 #if defined(_MSC_VER) && _MSC_VER>=1400 && !SQLITE_OS_WINCE
24547 rand_s((unsigned int*)zBuf); /* rand_s() is not available with MinGW */
24548 #endif /* defined(_MSC_VER) && _MSC_VER>=1400 */
24549 e.a = (unsigned char*)zBuf;
24550 e.na = nBuf;
24551 e.nXor = 0;
24552 e.i = 0;
24553 {
24554 SYSTEMTIME x;
24555 osGetSystemTime(&x);
24556 xorMemory(&e, (unsigned char*)&x, sizeof(SYSTEMTIME));
24557 }
24558 {
24559 DWORD pid = osGetCurrentProcessId();
24560 xorMemory(&e, (unsigned char*)&pid, sizeof(DWORD));
24561 }
24562 #if SQLITE_OS_WINRT
24563 {
24564 ULONGLONG cnt = osGetTickCount64();
24565 xorMemory(&e, (unsigned char*)&cnt, sizeof(ULONGLONG));
24566 }
24567 #else
24568 {
24569 DWORD cnt = osGetTickCount();
24570 xorMemory(&e, (unsigned char*)&cnt, sizeof(DWORD));
24571 }
24572 #endif /* SQLITE_OS_WINRT */
24573 {
24574 LARGE_INTEGER i;
24575 osQueryPerformanceCounter(&i);
24576 xorMemory(&e, (unsigned char*)&i, sizeof(LARGE_INTEGER));
24577 }
24578 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID
24579 {
24580 UUID id;
24581 memset(&id, 0, sizeof(UUID));
24582 osUuidCreate(&id);
24583 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
24584 memset(&id, 0, sizeof(UUID));
24585 osUuidCreateSequential(&id);
24586 xorMemory(&e, (unsigned char*)&id, sizeof(UUID));
24587 }
24588 #endif /* !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID */
24589 return e.nXor>nBuf ? nBuf : e.nXor;
24590 #endif /* defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) */
24591 }
24592
24593
24594 /*
24595 ** Sleep for a little while. Return the amount of time slept.
24596 */
24597 static int winSleep(sqlite3_vfs *pVfs, int microsec){
24598 sqlite3_win32_sleep((microsec+999)/1000);
24599 UNUSED_PARAMETER(pVfs);
24600 return ((microsec+999)/1000)*1000;
24601 }
24602
24603 /*
24604 ** The following variable, if set to a non-zero value, is interpreted as
24605 ** the number of seconds since 1970 and is used to set the result of
24606 ** sqlite3OsCurrentTime() during testing.
24607 */
24608 #ifdef SQLITE_TEST
24609 SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1 970. */
24610 #endif
24611
24612 /*
24613 ** Find the current time (in Universal Coordinated Time). Write into *piNow
24614 ** the current time and date as a Julian Day number times 86_400_000. In
24615 ** other words, write into *piNow the number of milliseconds since the Julian
24616 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
24617 ** proleptic Gregorian calendar.
24618 **
24619 ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
24620 ** cannot be found.
24621 */
24622 static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
24623 /* FILETIME structure is a 64-bit value representing the number of
24624 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
24625 */
24626 FILETIME ft;
24627 static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
24628 #ifdef SQLITE_TEST
24629 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
24630 #endif
24631 /* 2^32 - to avoid use of LL and warnings in gcc */
24632 static const sqlite3_int64 max32BitValue =
24633 (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
24634 (sqlite3_int64)294967296;
24635
24636 #if SQLITE_OS_WINCE
24637 SYSTEMTIME time;
24638 osGetSystemTime(&time);
24639 /* if SystemTimeToFileTime() fails, it returns zero. */
24640 if (!osSystemTimeToFileTime(&time,&ft)){
24641 return SQLITE_ERROR;
24642 }
24643 #else
24644 osGetSystemTimeAsFileTime( &ft );
24645 #endif
24646
24647 *piNow = winFiletimeEpoch +
24648 ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
24649 (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
24650
24651 #ifdef SQLITE_TEST
24652 if( sqlite3_current_time ){
24653 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
24654 }
24655 #endif
24656 UNUSED_PARAMETER(pVfs);
24657 return SQLITE_OK;
24658 }
24659
24660 /*
24661 ** Find the current time (in Universal Coordinated Time). Write the
24662 ** current time and date as a Julian Day number into *prNow and
24663 ** return 0. Return 1 if the time and date cannot be found.
24664 */
24665 static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
24666 int rc;
24667 sqlite3_int64 i;
24668 rc = winCurrentTimeInt64(pVfs, &i);
24669 if( !rc ){
24670 *prNow = i/86400000.0;
24671 }
24672 return rc;
24673 }
24674
24675 /*
24676 ** The idea is that this function works like a combination of
24677 ** GetLastError() and FormatMessage() on Windows (or errno and
24678 ** strerror_r() on Unix). After an error is returned by an OS
24679 ** function, SQLite calls this function with zBuf pointing to
24680 ** a buffer of nBuf bytes. The OS layer should populate the
24681 ** buffer with a nul-terminated UTF-8 encoded error message
24682 ** describing the last IO error to have occurred within the calling
24683 ** thread.
24684 **
24685 ** If the error message is too large for the supplied buffer,
24686 ** it should be truncated. The return value of xGetLastError
24687 ** is zero if the error message fits in the buffer, or non-zero
24688 ** otherwise (if the message was truncated). If non-zero is returned,
24689 ** then it is not necessary to include the nul-terminator character
24690 ** in the output buffer.
24691 **
24692 ** Not supplying an error message will have no adverse effect
24693 ** on SQLite. It is fine to have an implementation that never
24694 ** returns an error message:
24695 **
24696 ** int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
24697 ** assert(zBuf[0]=='\0');
24698 ** return 0;
24699 ** }
24700 **
24701 ** However if an error message is supplied, it will be incorporated
24702 ** by sqlite into the error message available to the user using
24703 ** sqlite3_errmsg(), possibly making IO errors easier to debug.
24704 */
24705 static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
24706 DWORD e = osGetLastError();
24707 UNUSED_PARAMETER(pVfs);
24708 if( nBuf>0 ) winGetLastErrorMsg(e, nBuf, zBuf);
24709 return e;
24710 }
24711
24712 /*
24713 ** Initialize and deinitialize the operating system interface.
24714 */
24715 SQLITE_API int sqlite3_os_init(void){
24716 static sqlite3_vfs winVfs = {
24717 3, /* iVersion */
24718 sizeof(winFile), /* szOsFile */
24719 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
24720 0, /* pNext */
24721 "win32", /* zName */
24722 &winAppData, /* pAppData */
24723 winOpen, /* xOpen */
24724 winDelete, /* xDelete */
24725 winAccess, /* xAccess */
24726 winFullPathname, /* xFullPathname */
24727 winDlOpen, /* xDlOpen */
24728 winDlError, /* xDlError */
24729 winDlSym, /* xDlSym */
24730 winDlClose, /* xDlClose */
24731 winRandomness, /* xRandomness */
24732 winSleep, /* xSleep */
24733 winCurrentTime, /* xCurrentTime */
24734 winGetLastError, /* xGetLastError */
24735 winCurrentTimeInt64, /* xCurrentTimeInt64 */
24736 winSetSystemCall, /* xSetSystemCall */
24737 winGetSystemCall, /* xGetSystemCall */
24738 winNextSystemCall, /* xNextSystemCall */
24739 };
24740 #if defined(SQLITE_WIN32_HAS_WIDE)
24741 static sqlite3_vfs winLongPathVfs = {
24742 3, /* iVersion */
24743 sizeof(winFile), /* szOsFile */
24744 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
24745 0, /* pNext */
24746 "win32-longpath", /* zName */
24747 &winAppData, /* pAppData */
24748 winOpen, /* xOpen */
24749 winDelete, /* xDelete */
24750 winAccess, /* xAccess */
24751 winFullPathname, /* xFullPathname */
24752 winDlOpen, /* xDlOpen */
24753 winDlError, /* xDlError */
24754 winDlSym, /* xDlSym */
24755 winDlClose, /* xDlClose */
24756 winRandomness, /* xRandomness */
24757 winSleep, /* xSleep */
24758 winCurrentTime, /* xCurrentTime */
24759 winGetLastError, /* xGetLastError */
24760 winCurrentTimeInt64, /* xCurrentTimeInt64 */
24761 winSetSystemCall, /* xSetSystemCall */
24762 winGetSystemCall, /* xGetSystemCall */
24763 winNextSystemCall, /* xNextSystemCall */
24764 };
24765 #endif
24766 static sqlite3_vfs winNolockVfs = {
24767 3, /* iVersion */
24768 sizeof(winFile), /* szOsFile */
24769 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
24770 0, /* pNext */
24771 "win32-none", /* zName */
24772 &winNolockAppData, /* pAppData */
24773 winOpen, /* xOpen */
24774 winDelete, /* xDelete */
24775 winAccess, /* xAccess */
24776 winFullPathname, /* xFullPathname */
24777 winDlOpen, /* xDlOpen */
24778 winDlError, /* xDlError */
24779 winDlSym, /* xDlSym */
24780 winDlClose, /* xDlClose */
24781 winRandomness, /* xRandomness */
24782 winSleep, /* xSleep */
24783 winCurrentTime, /* xCurrentTime */
24784 winGetLastError, /* xGetLastError */
24785 winCurrentTimeInt64, /* xCurrentTimeInt64 */
24786 winSetSystemCall, /* xSetSystemCall */
24787 winGetSystemCall, /* xGetSystemCall */
24788 winNextSystemCall, /* xNextSystemCall */
24789 };
24790 #if defined(SQLITE_WIN32_HAS_WIDE)
24791 static sqlite3_vfs winLongPathNolockVfs = {
24792 3, /* iVersion */
24793 sizeof(winFile), /* szOsFile */
24794 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
24795 0, /* pNext */
24796 "win32-longpath-none", /* zName */
24797 &winNolockAppData, /* pAppData */
24798 winOpen, /* xOpen */
24799 winDelete, /* xDelete */
24800 winAccess, /* xAccess */
24801 winFullPathname, /* xFullPathname */
24802 winDlOpen, /* xDlOpen */
24803 winDlError, /* xDlError */
24804 winDlSym, /* xDlSym */
24805 winDlClose, /* xDlClose */
24806 winRandomness, /* xRandomness */
24807 winSleep, /* xSleep */
24808 winCurrentTime, /* xCurrentTime */
24809 winGetLastError, /* xGetLastError */
24810 winCurrentTimeInt64, /* xCurrentTimeInt64 */
24811 winSetSystemCall, /* xSetSystemCall */
24812 winGetSystemCall, /* xGetSystemCall */
24813 winNextSystemCall, /* xNextSystemCall */
24814 };
24815 #endif
24816
24817 /* Double-check that the aSyscall[] array has been constructed
24818 ** correctly. See ticket [bb3a86e890c8e96ab] */
24819 assert( ArraySize(aSyscall)==80 );
24820
24821 /* get memory map allocation granularity */
24822 memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
24823 #if SQLITE_OS_WINRT
24824 osGetNativeSystemInfo(&winSysInfo);
24825 #else
24826 osGetSystemInfo(&winSysInfo);
24827 #endif
24828 assert( winSysInfo.dwAllocationGranularity>0 );
24829 assert( winSysInfo.dwPageSize>0 );
24830
24831 sqlite3_vfs_register(&winVfs, 1);
24832
24833 #if defined(SQLITE_WIN32_HAS_WIDE)
24834 sqlite3_vfs_register(&winLongPathVfs, 0);
24835 #endif
24836
24837 sqlite3_vfs_register(&winNolockVfs, 0);
24838
24839 #if defined(SQLITE_WIN32_HAS_WIDE)
24840 sqlite3_vfs_register(&winLongPathNolockVfs, 0);
24841 #endif
24842
24843 return SQLITE_OK;
24844 }
24845
24846 SQLITE_API int sqlite3_os_end(void){
24847 #if SQLITE_OS_WINRT
24848 if( sleepObj!=NULL ){
24849 osCloseHandle(sleepObj);
24850 sleepObj = NULL;
24851 }
24852 #endif
24853 return SQLITE_OK;
24854 }
24855
24856 CHROMIUM_SQLITE_API
24857 void chromium_sqlite3_initialize_win_sqlite3_file(sqlite3_file* file, HANDLE han dle) {
24858 winFile* winSQLite3File = (winFile*)file;
24859 memset(file, 0, sizeof(*file));
24860 winSQLite3File->pMethod = &winIoMethod;
24861 winSQLite3File->h = handle;
24862 }
24863
24864 #endif /* SQLITE_OS_WIN */
24865
24866 /************** End of os_win.c **********************************************/
24867 /************** Begin file bitvec.c ******************************************/
24868 /*
24869 ** 2008 February 16
24870 **
24871 ** The author disclaims copyright to this source code. In place of
24872 ** a legal notice, here is a blessing:
24873 **
24874 ** May you do good and not evil.
24875 ** May you find forgiveness for yourself and forgive others.
24876 ** May you share freely, never taking more than you give.
24877 **
24878 *************************************************************************
24879 ** This file implements an object that represents a fixed-length
24880 ** bitmap. Bits are numbered starting with 1.
24881 **
24882 ** A bitmap is used to record which pages of a database file have been
24883 ** journalled during a transaction, or which pages have the "dont-write"
24884 ** property. Usually only a few pages are meet either condition.
24885 ** So the bitmap is usually sparse and has low cardinality.
24886 ** But sometimes (for example when during a DROP of a large table) most
24887 ** or all of the pages in a database can get journalled. In those cases,
24888 ** the bitmap becomes dense with high cardinality. The algorithm needs
24889 ** to handle both cases well.
24890 **
24891 ** The size of the bitmap is fixed when the object is created.
24892 **
24893 ** All bits are clear when the bitmap is created. Individual bits
24894 ** may be set or cleared one at a time.
24895 **
24896 ** Test operations are about 100 times more common that set operations.
24897 ** Clear operations are exceedingly rare. There are usually between
24898 ** 5 and 500 set operations per Bitvec object, though the number of sets can
24899 ** sometimes grow into tens of thousands or larger. The size of the
24900 ** Bitvec object is the number of pages in the database file at the
24901 ** start of a transaction, and is thus usually less than a few thousand,
24902 ** but can be as large as 2 billion for a really big database.
24903 */
24904 /* #include "sqliteInt.h" */
24905
24906 /* Size of the Bitvec structure in bytes. */
24907 #define BITVEC_SZ 512
24908
24909 /* Round the union size down to the nearest pointer boundary, since that's how
24910 ** it will be aligned within the Bitvec struct. */
24911 #define BITVEC_USIZE \
24912 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
24913
24914 /* Type of the array "element" for the bitmap representation.
24915 ** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
24916 ** Setting this to the "natural word" size of your CPU may improve
24917 ** performance. */
24918 #define BITVEC_TELEM u8
24919 /* Size, in bits, of the bitmap element. */
24920 #define BITVEC_SZELEM 8
24921 /* Number of elements in a bitmap array. */
24922 #define BITVEC_NELEM (BITVEC_USIZE/sizeof(BITVEC_TELEM))
24923 /* Number of bits in the bitmap array. */
24924 #define BITVEC_NBIT (BITVEC_NELEM*BITVEC_SZELEM)
24925
24926 /* Number of u32 values in hash table. */
24927 #define BITVEC_NINT (BITVEC_USIZE/sizeof(u32))
24928 /* Maximum number of entries in hash table before
24929 ** sub-dividing and re-hashing. */
24930 #define BITVEC_MXHASH (BITVEC_NINT/2)
24931 /* Hashing function for the aHash representation.
24932 ** Empirical testing showed that the *37 multiplier
24933 ** (an arbitrary prime)in the hash function provided
24934 ** no fewer collisions than the no-op *1. */
24935 #define BITVEC_HASH(X) (((X)*1)%BITVEC_NINT)
24936
24937 #define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *))
24938
24939
24940 /*
24941 ** A bitmap is an instance of the following structure.
24942 **
24943 ** This bitmap records the existence of zero or more bits
24944 ** with values between 1 and iSize, inclusive.
24945 **
24946 ** There are three possible representations of the bitmap.
24947 ** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
24948 ** bitmap. The least significant bit is bit 1.
24949 **
24950 ** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
24951 ** a hash table that will hold up to BITVEC_MXHASH distinct values.
24952 **
24953 ** Otherwise, the value i is redirected into one of BITVEC_NPTR
24954 ** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
24955 ** handles up to iDivisor separate values of i. apSub[0] holds
24956 ** values between 1 and iDivisor. apSub[1] holds values between
24957 ** iDivisor+1 and 2*iDivisor. apSub[N] holds values between
24958 ** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized
24959 ** to hold deal with values between 1 and iDivisor.
24960 */
24961 struct Bitvec {
24962 u32 iSize; /* Maximum bit index. Max iSize is 4,294,967,296. */
24963 u32 nSet; /* Number of bits that are set - only valid for aHash
24964 ** element. Max is BITVEC_NINT. For BITVEC_SZ of 512,
24965 ** this would be 125. */
24966 u32 iDivisor; /* Number of bits handled by each apSub[] entry. */
24967 /* Should >=0 for apSub element. */
24968 /* Max iDivisor is max(u32) / BITVEC_NPTR + 1. */
24969 /* For a BITVEC_SZ of 512, this would be 34,359,739. */
24970 union {
24971 BITVEC_TELEM aBitmap[BITVEC_NELEM]; /* Bitmap representation */
24972 u32 aHash[BITVEC_NINT]; /* Hash table representation */
24973 Bitvec *apSub[BITVEC_NPTR]; /* Recursive representation */
24974 } u;
24975 };
24976
24977 /*
24978 ** Create a new bitmap object able to handle bits between 0 and iSize,
24979 ** inclusive. Return a pointer to the new object. Return NULL if
24980 ** malloc fails.
24981 */
24982 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
24983 Bitvec *p;
24984 assert( sizeof(*p)==BITVEC_SZ );
24985 p = sqlite3MallocZero( sizeof(*p) );
24986 if( p ){
24987 p->iSize = iSize;
24988 }
24989 return p;
24990 }
24991
24992 /*
24993 ** Check to see if the i-th bit is set. Return true or false.
24994 ** If p is NULL (if the bitmap has not been created) or if
24995 ** i is out of range, then return false.
24996 */
24997 SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){
24998 assert( p!=0 );
24999 i--;
25000 if( i>=p->iSize ) return 0;
25001 while( p->iDivisor ){
25002 u32 bin = i/p->iDivisor;
25003 i = i%p->iDivisor;
25004 p = p->u.apSub[bin];
25005 if (!p) {
25006 return 0;
25007 }
25008 }
25009 if( p->iSize<=BITVEC_NBIT ){
25010 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
25011 } else{
25012 u32 h = BITVEC_HASH(i++);
25013 while( p->u.aHash[h] ){
25014 if( p->u.aHash[h]==i ) return 1;
25015 h = (h+1) % BITVEC_NINT;
25016 }
25017 return 0;
25018 }
25019 }
25020 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
25021 return p!=0 && sqlite3BitvecTestNotNull(p,i);
25022 }
25023
25024 /*
25025 ** Set the i-th bit. Return 0 on success and an error code if
25026 ** anything goes wrong.
25027 **
25028 ** This routine might cause sub-bitmaps to be allocated. Failing
25029 ** to get the memory needed to hold the sub-bitmap is the only
25030 ** that can go wrong with an insert, assuming p and i are valid.
25031 **
25032 ** The calling function must ensure that p is a valid Bitvec object
25033 ** and that the value for "i" is within range of the Bitvec object.
25034 ** Otherwise the behavior is undefined.
25035 */
25036 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
25037 u32 h;
25038 if( p==0 ) return SQLITE_OK;
25039 assert( i>0 );
25040 assert( i<=p->iSize );
25041 i--;
25042 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
25043 u32 bin = i/p->iDivisor;
25044 i = i%p->iDivisor;
25045 if( p->u.apSub[bin]==0 ){
25046 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
25047 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
25048 }
25049 p = p->u.apSub[bin];
25050 }
25051 if( p->iSize<=BITVEC_NBIT ){
25052 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
25053 return SQLITE_OK;
25054 }
25055 h = BITVEC_HASH(i++);
25056 /* if there wasn't a hash collision, and this doesn't */
25057 /* completely fill the hash, then just add it without */
25058 /* worring about sub-dividing and re-hashing. */
25059 if( !p->u.aHash[h] ){
25060 if (p->nSet<(BITVEC_NINT-1)) {
25061 goto bitvec_set_end;
25062 } else {
25063 goto bitvec_set_rehash;
25064 }
25065 }
25066 /* there was a collision, check to see if it's already */
25067 /* in hash, if not, try to find a spot for it */
25068 do {
25069 if( p->u.aHash[h]==i ) return SQLITE_OK;
25070 h++;
25071 if( h>=BITVEC_NINT ) h = 0;
25072 } while( p->u.aHash[h] );
25073 /* we didn't find it in the hash. h points to the first */
25074 /* available free spot. check to see if this is going to */
25075 /* make our hash too "full". */
25076 bitvec_set_rehash:
25077 if( p->nSet>=BITVEC_MXHASH ){
25078 unsigned int j;
25079 int rc;
25080 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
25081 if( aiValues==0 ){
25082 return SQLITE_NOMEM_BKPT;
25083 }else{
25084 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
25085 memset(p->u.apSub, 0, sizeof(p->u.apSub));
25086 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
25087 rc = sqlite3BitvecSet(p, i);
25088 for(j=0; j<BITVEC_NINT; j++){
25089 if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
25090 }
25091 sqlite3StackFree(0, aiValues);
25092 return rc;
25093 }
25094 }
25095 bitvec_set_end:
25096 p->nSet++;
25097 p->u.aHash[h] = i;
25098 return SQLITE_OK;
25099 }
25100
25101 /*
25102 ** Clear the i-th bit.
25103 **
25104 ** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
25105 ** that BitvecClear can use to rebuilt its hash table.
25106 */
25107 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
25108 if( p==0 ) return;
25109 assert( i>0 );
25110 i--;
25111 while( p->iDivisor ){
25112 u32 bin = i/p->iDivisor;
25113 i = i%p->iDivisor;
25114 p = p->u.apSub[bin];
25115 if (!p) {
25116 return;
25117 }
25118 }
25119 if( p->iSize<=BITVEC_NBIT ){
25120 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
25121 }else{
25122 unsigned int j;
25123 u32 *aiValues = pBuf;
25124 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
25125 memset(p->u.aHash, 0, sizeof(p->u.aHash));
25126 p->nSet = 0;
25127 for(j=0; j<BITVEC_NINT; j++){
25128 if( aiValues[j] && aiValues[j]!=(i+1) ){
25129 u32 h = BITVEC_HASH(aiValues[j]-1);
25130 p->nSet++;
25131 while( p->u.aHash[h] ){
25132 h++;
25133 if( h>=BITVEC_NINT ) h = 0;
25134 }
25135 p->u.aHash[h] = aiValues[j];
25136 }
25137 }
25138 }
25139 }
25140
25141 /*
25142 ** Destroy a bitmap object. Reclaim all memory used.
25143 */
25144 SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
25145 if( p==0 ) return;
25146 if( p->iDivisor ){
25147 unsigned int i;
25148 for(i=0; i<BITVEC_NPTR; i++){
25149 sqlite3BitvecDestroy(p->u.apSub[i]);
25150 }
25151 }
25152 sqlite3_free(p);
25153 }
25154
25155 /*
25156 ** Return the value of the iSize parameter specified when Bitvec *p
25157 ** was created.
25158 */
25159 SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
25160 return p->iSize;
25161 }
25162
25163 #ifndef SQLITE_UNTESTABLE
25164 /*
25165 ** Let V[] be an array of unsigned characters sufficient to hold
25166 ** up to N bits. Let I be an integer between 0 and N. 0<=I<N.
25167 ** Then the following macros can be used to set, clear, or test
25168 ** individual bits within V.
25169 */
25170 #define SETBIT(V,I) V[I>>3] |= (1<<(I&7))
25171 #define CLEARBIT(V,I) V[I>>3] &= ~(1<<(I&7))
25172 #define TESTBIT(V,I) (V[I>>3]&(1<<(I&7)))!=0
25173
25174 /*
25175 ** This routine runs an extensive test of the Bitvec code.
25176 **
25177 ** The input is an array of integers that acts as a program
25178 ** to test the Bitvec. The integers are opcodes followed
25179 ** by 0, 1, or 3 operands, depending on the opcode. Another
25180 ** opcode follows immediately after the last operand.
25181 **
25182 ** There are 6 opcodes numbered from 0 through 5. 0 is the
25183 ** "halt" opcode and causes the test to end.
25184 **
25185 ** 0 Halt and return the number of errors
25186 ** 1 N S X Set N bits beginning with S and incrementing by X
25187 ** 2 N S X Clear N bits beginning with S and incrementing by X
25188 ** 3 N Set N randomly chosen bits
25189 ** 4 N Clear N randomly chosen bits
25190 ** 5 N S X Set N bits from S increment X in array only, not in bitvec
25191 **
25192 ** The opcodes 1 through 4 perform set and clear operations are performed
25193 ** on both a Bitvec object and on a linear array of bits obtained from malloc.
25194 ** Opcode 5 works on the linear array only, not on the Bitvec.
25195 ** Opcode 5 is used to deliberately induce a fault in order to
25196 ** confirm that error detection works.
25197 **
25198 ** At the conclusion of the test the linear array is compared
25199 ** against the Bitvec object. If there are any differences,
25200 ** an error is returned. If they are the same, zero is returned.
25201 **
25202 ** If a memory allocation error occurs, return -1.
25203 */
25204 SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
25205 Bitvec *pBitvec = 0;
25206 unsigned char *pV = 0;
25207 int rc = -1;
25208 int i, nx, pc, op;
25209 void *pTmpSpace;
25210
25211 /* Allocate the Bitvec to be tested and a linear array of
25212 ** bits to act as the reference */
25213 pBitvec = sqlite3BitvecCreate( sz );
25214 pV = sqlite3MallocZero( (sz+7)/8 + 1 );
25215 pTmpSpace = sqlite3_malloc64(BITVEC_SZ);
25216 if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end;
25217
25218 /* NULL pBitvec tests */
25219 sqlite3BitvecSet(0, 1);
25220 sqlite3BitvecClear(0, 1, pTmpSpace);
25221
25222 /* Run the program */
25223 pc = 0;
25224 while( (op = aOp[pc])!=0 ){
25225 switch( op ){
25226 case 1:
25227 case 2:
25228 case 5: {
25229 nx = 4;
25230 i = aOp[pc+2] - 1;
25231 aOp[pc+2] += aOp[pc+3];
25232 break;
25233 }
25234 case 3:
25235 case 4:
25236 default: {
25237 nx = 2;
25238 sqlite3_randomness(sizeof(i), &i);
25239 break;
25240 }
25241 }
25242 if( (--aOp[pc+1]) > 0 ) nx = 0;
25243 pc += nx;
25244 i = (i & 0x7fffffff)%sz;
25245 if( (op & 1)!=0 ){
25246 SETBIT(pV, (i+1));
25247 if( op!=5 ){
25248 if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
25249 }
25250 }else{
25251 CLEARBIT(pV, (i+1));
25252 sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
25253 }
25254 }
25255
25256 /* Test to make sure the linear array exactly matches the
25257 ** Bitvec object. Start with the assumption that they do
25258 ** match (rc==0). Change rc to non-zero if a discrepancy
25259 ** is found.
25260 */
25261 rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
25262 + sqlite3BitvecTest(pBitvec, 0)
25263 + (sqlite3BitvecSize(pBitvec) - sz);
25264 for(i=1; i<=sz; i++){
25265 if( (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
25266 rc = i;
25267 break;
25268 }
25269 }
25270
25271 /* Free allocated structure */
25272 bitvec_end:
25273 sqlite3_free(pTmpSpace);
25274 sqlite3_free(pV);
25275 sqlite3BitvecDestroy(pBitvec);
25276 return rc;
25277 }
25278 #endif /* SQLITE_UNTESTABLE */
25279
25280 /************** End of bitvec.c **********************************************/
25281 /************** Begin file pcache.c ******************************************/
25282 /*
25283 ** 2008 August 05
25284 **
25285 ** The author disclaims copyright to this source code. In place of
25286 ** a legal notice, here is a blessing:
25287 **
25288 ** May you do good and not evil.
25289 ** May you find forgiveness for yourself and forgive others.
25290 ** May you share freely, never taking more than you give.
25291 **
25292 *************************************************************************
25293 ** This file implements that page cache.
25294 */
25295 /* #include "sqliteInt.h" */
25296
25297 /*
25298 ** A complete page cache is an instance of this structure. Every
25299 ** entry in the cache holds a single page of the database file. The
25300 ** btree layer only operates on the cached copy of the database pages.
25301 **
25302 ** A page cache entry is "clean" if it exactly matches what is currently
25303 ** on disk. A page is "dirty" if it has been modified and needs to be
25304 ** persisted to disk.
25305 **
25306 ** pDirty, pDirtyTail, pSynced:
25307 ** All dirty pages are linked into the doubly linked list using
25308 ** PgHdr.pDirtyNext and pDirtyPrev. The list is maintained in LRU order
25309 ** such that p was added to the list more recently than p->pDirtyNext.
25310 ** PCache.pDirty points to the first (newest) element in the list and
25311 ** pDirtyTail to the last (oldest).
25312 **
25313 ** The PCache.pSynced variable is used to optimize searching for a dirty
25314 ** page to eject from the cache mid-transaction. It is better to eject
25315 ** a page that does not require a journal sync than one that does.
25316 ** Therefore, pSynced is maintained to that it *almost* always points
25317 ** to either the oldest page in the pDirty/pDirtyTail list that has a
25318 ** clear PGHDR_NEED_SYNC flag or to a page that is older than this one
25319 ** (so that the right page to eject can be found by following pDirtyPrev
25320 ** pointers).
25321 */
25322 struct PCache {
25323 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
25324 PgHdr *pSynced; /* Last synced page in dirty page list */
25325 int nRefSum; /* Sum of ref counts over all pages */
25326 int szCache; /* Configured cache size */
25327 int szSpill; /* Size before spilling occurs */
25328 int szPage; /* Size of every page in this cache */
25329 int szExtra; /* Size of extra space for each page */
25330 u8 bPurgeable; /* True if pages are on backing store */
25331 u8 eCreate; /* eCreate value for for xFetch() */
25332 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
25333 void *pStress; /* Argument to xStress */
25334 sqlite3_pcache *pCache; /* Pluggable cache module */
25335 };
25336
25337 /********************************** Test and Debug Logic **********************/
25338 /*
25339 ** Debug tracing macros. Enable by by changing the "0" to "1" and
25340 ** recompiling.
25341 **
25342 ** When sqlite3PcacheTrace is 1, single line trace messages are issued.
25343 ** When sqlite3PcacheTrace is 2, a dump of the pcache showing all cache entries
25344 ** is displayed for many operations, resulting in a lot of output.
25345 */
25346 #if defined(SQLITE_DEBUG) && 0
25347 int sqlite3PcacheTrace = 2; /* 0: off 1: simple 2: cache dumps */
25348 int sqlite3PcacheMxDump = 9999; /* Max cache entries for pcacheDump() */
25349 # define pcacheTrace(X) if(sqlite3PcacheTrace){sqlite3DebugPrintf X;}
25350 void pcacheDump(PCache *pCache){
25351 int N;
25352 int i, j;
25353 sqlite3_pcache_page *pLower;
25354 PgHdr *pPg;
25355 unsigned char *a;
25356
25357 if( sqlite3PcacheTrace<2 ) return;
25358 if( pCache->pCache==0 ) return;
25359 N = sqlite3PcachePagecount(pCache);
25360 if( N>sqlite3PcacheMxDump ) N = sqlite3PcacheMxDump;
25361 for(i=1; i<=N; i++){
25362 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
25363 if( pLower==0 ) continue;
25364 pPg = (PgHdr*)pLower->pExtra;
25365 printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
25366 a = (unsigned char *)pLower->pBuf;
25367 for(j=0; j<12; j++) printf("%02x", a[j]);
25368 printf("\n");
25369 if( pPg->pPage==0 ){
25370 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
25371 }
25372 }
25373 }
25374 #else
25375 # define pcacheTrace(X)
25376 # define pcacheDump(X)
25377 #endif
25378
25379 /*
25380 ** Check invariants on a PgHdr entry. Return true if everything is OK.
25381 ** Return false if any invariant is violated.
25382 **
25383 ** This routine is for use inside of assert() statements only. For
25384 ** example:
25385 **
25386 ** assert( sqlite3PcachePageSanity(pPg) );
25387 */
25388 #if SQLITE_DEBUG
25389 SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr *pPg){
25390 PCache *pCache;
25391 assert( pPg!=0 );
25392 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
25393 pCache = pPg->pCache;
25394 assert( pCache!=0 ); /* Every page has an associated PCache */
25395 if( pPg->flags & PGHDR_CLEAN ){
25396 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
25397 assert( pCache->pDirty!=pPg ); /* CLEAN pages not on dirty list */
25398 assert( pCache->pDirtyTail!=pPg );
25399 }
25400 /* WRITEABLE pages must also be DIRTY */
25401 if( pPg->flags & PGHDR_WRITEABLE ){
25402 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
25403 }
25404 /* NEED_SYNC can be set independently of WRITEABLE. This can happen,
25405 ** for example, when using the sqlite3PagerDontWrite() optimization:
25406 ** (1) Page X is journalled, and gets WRITEABLE and NEED_SEEK.
25407 ** (2) Page X moved to freelist, WRITEABLE is cleared
25408 ** (3) Page X reused, WRITEABLE is set again
25409 ** If NEED_SYNC had been cleared in step 2, then it would not be reset
25410 ** in step 3, and page might be written into the database without first
25411 ** syncing the rollback journal, which might cause corruption on a power
25412 ** loss.
25413 **
25414 ** Another example is when the database page size is smaller than the
25415 ** disk sector size. When any page of a sector is journalled, all pages
25416 ** in that sector are marked NEED_SYNC even if they are still CLEAN, just
25417 ** in case they are later modified, since all pages in the same sector
25418 ** must be journalled and synced before any of those pages can be safely
25419 ** written.
25420 */
25421 return 1;
25422 }
25423 #endif /* SQLITE_DEBUG */
25424
25425
25426 /********************************** Linked List Management ********************/
25427
25428 /* Allowed values for second argument to pcacheManageDirtyList() */
25429 #define PCACHE_DIRTYLIST_REMOVE 1 /* Remove pPage from dirty list */
25430 #define PCACHE_DIRTYLIST_ADD 2 /* Add pPage to the dirty list */
25431 #define PCACHE_DIRTYLIST_FRONT 3 /* Move pPage to the front of the list */
25432
25433 /*
25434 ** Manage pPage's participation on the dirty list. Bits of the addRemove
25435 ** argument determines what operation to do. The 0x01 bit means first
25436 ** remove pPage from the dirty list. The 0x02 means add pPage back to
25437 ** the dirty list. Doing both moves pPage to the front of the dirty list.
25438 */
25439 static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){
25440 PCache *p = pPage->pCache;
25441
25442 pcacheTrace(("%p.DIRTYLIST.%s %d\n", p,
25443 addRemove==1 ? "REMOVE" : addRemove==2 ? "ADD" : "FRONT",
25444 pPage->pgno));
25445 if( addRemove & PCACHE_DIRTYLIST_REMOVE ){
25446 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
25447 assert( pPage->pDirtyPrev || pPage==p->pDirty );
25448
25449 /* Update the PCache1.pSynced variable if necessary. */
25450 if( p->pSynced==pPage ){
25451 p->pSynced = pPage->pDirtyPrev;
25452 }
25453
25454 if( pPage->pDirtyNext ){
25455 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
25456 }else{
25457 assert( pPage==p->pDirtyTail );
25458 p->pDirtyTail = pPage->pDirtyPrev;
25459 }
25460 if( pPage->pDirtyPrev ){
25461 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
25462 }else{
25463 /* If there are now no dirty pages in the cache, set eCreate to 2.
25464 ** This is an optimization that allows sqlite3PcacheFetch() to skip
25465 ** searching for a dirty page to eject from the cache when it might
25466 ** otherwise have to. */
25467 assert( pPage==p->pDirty );
25468 p->pDirty = pPage->pDirtyNext;
25469 assert( p->bPurgeable || p->eCreate==2 );
25470 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
25471 assert( p->bPurgeable==0 || p->eCreate==1 );
25472 p->eCreate = 2;
25473 }
25474 }
25475 pPage->pDirtyNext = 0;
25476 pPage->pDirtyPrev = 0;
25477 }
25478 if( addRemove & PCACHE_DIRTYLIST_ADD ){
25479 assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
25480
25481 pPage->pDirtyNext = p->pDirty;
25482 if( pPage->pDirtyNext ){
25483 assert( pPage->pDirtyNext->pDirtyPrev==0 );
25484 pPage->pDirtyNext->pDirtyPrev = pPage;
25485 }else{
25486 p->pDirtyTail = pPage;
25487 if( p->bPurgeable ){
25488 assert( p->eCreate==2 );
25489 p->eCreate = 1;
25490 }
25491 }
25492 p->pDirty = pPage;
25493
25494 /* If pSynced is NULL and this page has a clear NEED_SYNC flag, set
25495 ** pSynced to point to it. Checking the NEED_SYNC flag is an
25496 ** optimization, as if pSynced points to a page with the NEED_SYNC
25497 ** flag set sqlite3PcacheFetchStress() searches through all newer
25498 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
25499 if( !p->pSynced
25500 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
25501 ){
25502 p->pSynced = pPage;
25503 }
25504 }
25505 pcacheDump(p);
25506 }
25507
25508 /*
25509 ** Wrapper around the pluggable caches xUnpin method. If the cache is
25510 ** being used for an in-memory database, this function is a no-op.
25511 */
25512 static void pcacheUnpin(PgHdr *p){
25513 if( p->pCache->bPurgeable ){
25514 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
25515 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
25516 pcacheDump(p->pCache);
25517 }
25518 }
25519
25520 /*
25521 ** Compute the number of pages of cache requested. p->szCache is the
25522 ** cache size requested by the "PRAGMA cache_size" statement.
25523 */
25524 static int numberOfCachePages(PCache *p){
25525 if( p->szCache>=0 ){
25526 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
25527 ** suggested cache size is set to N. */
25528 return p->szCache;
25529 }else{
25530 /* IMPLEMENTATION-OF: R-61436-13639 If the argument N is negative, then
25531 ** the number of cache pages is adjusted to use approximately abs(N*1024)
25532 ** bytes of memory. */
25533 return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
25534 }
25535 }
25536
25537 /*************************************************** General Interfaces ******
25538 **
25539 ** Initialize and shutdown the page cache subsystem. Neither of these
25540 ** functions are threadsafe.
25541 */
25542 SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
25543 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
25544 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
25545 ** built-in default page cache is used instead of the application defined
25546 ** page cache. */
25547 sqlite3PCacheSetDefault();
25548 }
25549 return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
25550 }
25551 SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
25552 if( sqlite3GlobalConfig.pcache2.xShutdown ){
25553 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
25554 sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
25555 }
25556 }
25557
25558 /*
25559 ** Return the size in bytes of a PCache object.
25560 */
25561 SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
25562
25563 /*
25564 ** Create a new PCache object. Storage space to hold the object
25565 ** has already been allocated and is passed in as the p pointer.
25566 ** The caller discovers how much space needs to be allocated by
25567 ** calling sqlite3PcacheSize().
25568 **
25569 ** szExtra is some extra space allocated for each page. The first
25570 ** 8 bytes of the extra space will be zeroed as the page is allocated,
25571 ** but remaining content will be uninitialized. Though it is opaque
25572 ** to this module, the extra space really ends up being the MemPage
25573 ** structure in the pager.
25574 */
25575 SQLITE_PRIVATE int sqlite3PcacheOpen(
25576 int szPage, /* Size of every page */
25577 int szExtra, /* Extra space associated with each page */
25578 int bPurgeable, /* True if pages are on backing store */
25579 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
25580 void *pStress, /* Argument to xStress */
25581 PCache *p /* Preallocated space for the PCache */
25582 ){
25583 memset(p, 0, sizeof(PCache));
25584 p->szPage = 1;
25585 p->szExtra = szExtra;
25586 assert( szExtra>=8 ); /* First 8 bytes will be zeroed */
25587 p->bPurgeable = bPurgeable;
25588 p->eCreate = 2;
25589 p->xStress = xStress;
25590 p->pStress = pStress;
25591 p->szCache = 100;
25592 p->szSpill = 1;
25593 pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
25594 return sqlite3PcacheSetPageSize(p, szPage);
25595 }
25596
25597 /*
25598 ** Change the page size for PCache object. The caller must ensure that there
25599 ** are no outstanding page references when this function is called.
25600 */
25601 SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
25602 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
25603 if( pCache->szPage ){
25604 sqlite3_pcache *pNew;
25605 pNew = sqlite3GlobalConfig.pcache2.xCreate(
25606 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
25607 pCache->bPurgeable
25608 );
25609 if( pNew==0 ) return SQLITE_NOMEM_BKPT;
25610 sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
25611 if( pCache->pCache ){
25612 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
25613 }
25614 pCache->pCache = pNew;
25615 pCache->szPage = szPage;
25616 pcacheTrace(("%p.PAGESIZE %d\n",pCache,szPage));
25617 }
25618 return SQLITE_OK;
25619 }
25620
25621 /*
25622 ** Try to obtain a page from the cache.
25623 **
25624 ** This routine returns a pointer to an sqlite3_pcache_page object if
25625 ** such an object is already in cache, or if a new one is created.
25626 ** This routine returns a NULL pointer if the object was not in cache
25627 ** and could not be created.
25628 **
25629 ** The createFlags should be 0 to check for existing pages and should
25630 ** be 3 (not 1, but 3) to try to create a new page.
25631 **
25632 ** If the createFlag is 0, then NULL is always returned if the page
25633 ** is not already in the cache. If createFlag is 1, then a new page
25634 ** is created only if that can be done without spilling dirty pages
25635 ** and without exceeding the cache size limit.
25636 **
25637 ** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
25638 ** initialize the sqlite3_pcache_page object and convert it into a
25639 ** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
25640 ** routines are split this way for performance reasons. When separated
25641 ** they can both (usually) operate without having to push values to
25642 ** the stack on entry and pop them back off on exit, which saves a
25643 ** lot of pushing and popping.
25644 */
25645 SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch(
25646 PCache *pCache, /* Obtain the page from this cache */
25647 Pgno pgno, /* Page number to obtain */
25648 int createFlag /* If true, create page if it does not exist already */
25649 ){
25650 int eCreate;
25651 sqlite3_pcache_page *pRes;
25652
25653 assert( pCache!=0 );
25654 assert( pCache->pCache!=0 );
25655 assert( createFlag==3 || createFlag==0 );
25656 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
25657
25658 /* eCreate defines what to do if the page does not exist.
25659 ** 0 Do not allocate a new page. (createFlag==0)
25660 ** 1 Allocate a new page if doing so is inexpensive.
25661 ** (createFlag==1 AND bPurgeable AND pDirty)
25662 ** 2 Allocate a new page even it doing so is difficult.
25663 ** (createFlag==1 AND !(bPurgeable AND pDirty)
25664 */
25665 eCreate = createFlag & pCache->eCreate;
25666 assert( eCreate==0 || eCreate==1 || eCreate==2 );
25667 assert( createFlag==0 || pCache->eCreate==eCreate );
25668 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
25669 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
25670 pcacheTrace(("%p.FETCH %d%s (result: %p)\n",pCache,pgno,
25671 createFlag?" create":"",pRes));
25672 return pRes;
25673 }
25674
25675 /*
25676 ** If the sqlite3PcacheFetch() routine is unable to allocate a new
25677 ** page because no clean pages are available for reuse and the cache
25678 ** size limit has been reached, then this routine can be invoked to
25679 ** try harder to allocate a page. This routine might invoke the stress
25680 ** callback to spill dirty pages to the journal. It will then try to
25681 ** allocate the new page and will only fail to allocate a new page on
25682 ** an OOM error.
25683 **
25684 ** This routine should be invoked only after sqlite3PcacheFetch() fails.
25685 */
25686 SQLITE_PRIVATE int sqlite3PcacheFetchStress(
25687 PCache *pCache, /* Obtain the page from this cache */
25688 Pgno pgno, /* Page number to obtain */
25689 sqlite3_pcache_page **ppPage /* Write result here */
25690 ){
25691 PgHdr *pPg;
25692 if( pCache->eCreate==2 ) return 0;
25693
25694 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
25695 /* Find a dirty page to write-out and recycle. First try to find a
25696 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
25697 ** cleared), but if that is not possible settle for any other
25698 ** unreferenced dirty page.
25699 **
25700 ** If the LRU page in the dirty list that has a clear PGHDR_NEED_SYNC
25701 ** flag is currently referenced, then the following may leave pSynced
25702 ** set incorrectly (pointing to other than the LRU page with NEED_SYNC
25703 ** cleared). This is Ok, as pSynced is just an optimization. */
25704 for(pPg=pCache->pSynced;
25705 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
25706 pPg=pPg->pDirtyPrev
25707 );
25708 pCache->pSynced = pPg;
25709 if( !pPg ){
25710 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
25711 }
25712 if( pPg ){
25713 int rc;
25714 #ifdef SQLITE_LOG_CACHE_SPILL
25715 sqlite3_log(SQLITE_FULL,
25716 "spill page %d making room for %d - cache used: %d/%d",
25717 pPg->pgno, pgno,
25718 sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
25719 numberOfCachePages(pCache));
25720 #endif
25721 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
25722 rc = pCache->xStress(pCache->pStress, pPg);
25723 pcacheDump(pCache);
25724 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
25725 return rc;
25726 }
25727 }
25728 }
25729 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
25730 return *ppPage==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
25731 }
25732
25733 /*
25734 ** This is a helper routine for sqlite3PcacheFetchFinish()
25735 **
25736 ** In the uncommon case where the page being fetched has not been
25737 ** initialized, this routine is invoked to do the initialization.
25738 ** This routine is broken out into a separate function since it
25739 ** requires extra stack manipulation that can be avoided in the common
25740 ** case.
25741 */
25742 static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
25743 PCache *pCache, /* Obtain the page from this cache */
25744 Pgno pgno, /* Page number obtained */
25745 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
25746 ){
25747 PgHdr *pPgHdr;
25748 assert( pPage!=0 );
25749 pPgHdr = (PgHdr*)pPage->pExtra;
25750 assert( pPgHdr->pPage==0 );
25751 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
25752 pPgHdr->pPage = pPage;
25753 pPgHdr->pData = pPage->pBuf;
25754 pPgHdr->pExtra = (void *)&pPgHdr[1];
25755 memset(pPgHdr->pExtra, 0, 8);
25756 pPgHdr->pCache = pCache;
25757 pPgHdr->pgno = pgno;
25758 pPgHdr->flags = PGHDR_CLEAN;
25759 return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
25760 }
25761
25762 /*
25763 ** This routine converts the sqlite3_pcache_page object returned by
25764 ** sqlite3PcacheFetch() into an initialized PgHdr object. This routine
25765 ** must be called after sqlite3PcacheFetch() in order to get a usable
25766 ** result.
25767 */
25768 SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish(
25769 PCache *pCache, /* Obtain the page from this cache */
25770 Pgno pgno, /* Page number obtained */
25771 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */
25772 ){
25773 PgHdr *pPgHdr;
25774
25775 assert( pPage!=0 );
25776 pPgHdr = (PgHdr *)pPage->pExtra;
25777
25778 if( !pPgHdr->pPage ){
25779 return pcacheFetchFinishWithInit(pCache, pgno, pPage);
25780 }
25781 pCache->nRefSum++;
25782 pPgHdr->nRef++;
25783 assert( sqlite3PcachePageSanity(pPgHdr) );
25784 return pPgHdr;
25785 }
25786
25787 /*
25788 ** Decrement the reference count on a page. If the page is clean and the
25789 ** reference count drops to 0, then it is made eligible for recycling.
25790 */
25791 SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
25792 assert( p->nRef>0 );
25793 p->pCache->nRefSum--;
25794 if( (--p->nRef)==0 ){
25795 if( p->flags&PGHDR_CLEAN ){
25796 pcacheUnpin(p);
25797 }else if( p->pDirtyPrev!=0 ){ /*OPTIMIZATION-IF-FALSE*/
25798 /* Move the page to the head of the dirty list. If p->pDirtyPrev==0,
25799 ** then page p is already at the head of the dirty list and the
25800 ** following call would be a no-op. Hence the OPTIMIZATION-IF-FALSE
25801 ** tag above. */
25802 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
25803 }
25804 }
25805 }
25806
25807 /*
25808 ** Increase the reference count of a supplied page by 1.
25809 */
25810 SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
25811 assert(p->nRef>0);
25812 assert( sqlite3PcachePageSanity(p) );
25813 p->nRef++;
25814 p->pCache->nRefSum++;
25815 }
25816
25817 /*
25818 ** Drop a page from the cache. There must be exactly one reference to the
25819 ** page. This function deletes that reference, so after it returns the
25820 ** page pointed to by p is invalid.
25821 */
25822 SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
25823 assert( p->nRef==1 );
25824 assert( sqlite3PcachePageSanity(p) );
25825 if( p->flags&PGHDR_DIRTY ){
25826 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
25827 }
25828 p->pCache->nRefSum--;
25829 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
25830 }
25831
25832 /*
25833 ** Make sure the page is marked as dirty. If it isn't dirty already,
25834 ** make it so.
25835 */
25836 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
25837 assert( p->nRef>0 );
25838 assert( sqlite3PcachePageSanity(p) );
25839 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
25840 p->flags &= ~PGHDR_DONT_WRITE;
25841 if( p->flags & PGHDR_CLEAN ){
25842 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
25843 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
25844 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
25845 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD);
25846 }
25847 assert( sqlite3PcachePageSanity(p) );
25848 }
25849 }
25850
25851 /*
25852 ** Make sure the page is marked as clean. If it isn't clean already,
25853 ** make it so.
25854 */
25855 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
25856 assert( sqlite3PcachePageSanity(p) );
25857 if( ALWAYS((p->flags & PGHDR_DIRTY)!=0) ){
25858 assert( (p->flags & PGHDR_CLEAN)==0 );
25859 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
25860 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
25861 p->flags |= PGHDR_CLEAN;
25862 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
25863 assert( sqlite3PcachePageSanity(p) );
25864 if( p->nRef==0 ){
25865 pcacheUnpin(p);
25866 }
25867 }
25868 }
25869
25870 /*
25871 ** Make every page in the cache clean.
25872 */
25873 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
25874 PgHdr *p;
25875 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
25876 while( (p = pCache->pDirty)!=0 ){
25877 sqlite3PcacheMakeClean(p);
25878 }
25879 }
25880
25881 /*
25882 ** Clear the PGHDR_NEED_SYNC and PGHDR_WRITEABLE flag from all dirty pages.
25883 */
25884 SQLITE_PRIVATE void sqlite3PcacheClearWritable(PCache *pCache){
25885 PgHdr *p;
25886 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
25887 for(p=pCache->pDirty; p; p=p->pDirtyNext){
25888 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
25889 }
25890 pCache->pSynced = pCache->pDirtyTail;
25891 }
25892
25893 /*
25894 ** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
25895 */
25896 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
25897 PgHdr *p;
25898 for(p=pCache->pDirty; p; p=p->pDirtyNext){
25899 p->flags &= ~PGHDR_NEED_SYNC;
25900 }
25901 pCache->pSynced = pCache->pDirtyTail;
25902 }
25903
25904 /*
25905 ** Change the page number of page p to newPgno.
25906 */
25907 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
25908 PCache *pCache = p->pCache;
25909 assert( p->nRef>0 );
25910 assert( newPgno>0 );
25911 assert( sqlite3PcachePageSanity(p) );
25912 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
25913 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
25914 p->pgno = newPgno;
25915 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
25916 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT);
25917 }
25918 }
25919
25920 /*
25921 ** Drop every cache entry whose page number is greater than "pgno". The
25922 ** caller must ensure that there are no outstanding references to any pages
25923 ** other than page 1 with a page number greater than pgno.
25924 **
25925 ** If there is a reference to page 1 and the pgno parameter passed to this
25926 ** function is 0, then the data area associated with page 1 is zeroed, but
25927 ** the page object is not dropped.
25928 */
25929 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
25930 if( pCache->pCache ){
25931 PgHdr *p;
25932 PgHdr *pNext;
25933 pcacheTrace(("%p.TRUNCATE %d\n",pCache,pgno));
25934 for(p=pCache->pDirty; p; p=pNext){
25935 pNext = p->pDirtyNext;
25936 /* This routine never gets call with a positive pgno except right
25937 ** after sqlite3PcacheCleanAll(). So if there are dirty pages,
25938 ** it must be that pgno==0.
25939 */
25940 assert( p->pgno>0 );
25941 if( p->pgno>pgno ){
25942 assert( p->flags&PGHDR_DIRTY );
25943 sqlite3PcacheMakeClean(p);
25944 }
25945 }
25946 if( pgno==0 && pCache->nRefSum ){
25947 sqlite3_pcache_page *pPage1;
25948 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
25949 if( ALWAYS(pPage1) ){ /* Page 1 is always available in cache, because
25950 ** pCache->nRefSum>0 */
25951 memset(pPage1->pBuf, 0, pCache->szPage);
25952 pgno = 1;
25953 }
25954 }
25955 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
25956 }
25957 }
25958
25959 /*
25960 ** Close a cache.
25961 */
25962 SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
25963 assert( pCache->pCache!=0 );
25964 pcacheTrace(("%p.CLOSE\n",pCache));
25965 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
25966 }
25967
25968 /*
25969 ** Discard the contents of the cache.
25970 */
25971 SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
25972 sqlite3PcacheTruncate(pCache, 0);
25973 }
25974
25975 /*
25976 ** Merge two lists of pages connected by pDirty and in pgno order.
25977 ** Do not bother fixing the pDirtyPrev pointers.
25978 */
25979 static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
25980 PgHdr result, *pTail;
25981 pTail = &result;
25982 assert( pA!=0 && pB!=0 );
25983 for(;;){
25984 if( pA->pgno<pB->pgno ){
25985 pTail->pDirty = pA;
25986 pTail = pA;
25987 pA = pA->pDirty;
25988 if( pA==0 ){
25989 pTail->pDirty = pB;
25990 break;
25991 }
25992 }else{
25993 pTail->pDirty = pB;
25994 pTail = pB;
25995 pB = pB->pDirty;
25996 if( pB==0 ){
25997 pTail->pDirty = pA;
25998 break;
25999 }
26000 }
26001 }
26002 return result.pDirty;
26003 }
26004
26005 /*
26006 ** Sort the list of pages in accending order by pgno. Pages are
26007 ** connected by pDirty pointers. The pDirtyPrev pointers are
26008 ** corrupted by this sort.
26009 **
26010 ** Since there cannot be more than 2^31 distinct pages in a database,
26011 ** there cannot be more than 31 buckets required by the merge sorter.
26012 ** One extra bucket is added to catch overflow in case something
26013 ** ever changes to make the previous sentence incorrect.
26014 */
26015 #define N_SORT_BUCKET 32
26016 static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
26017 PgHdr *a[N_SORT_BUCKET], *p;
26018 int i;
26019 memset(a, 0, sizeof(a));
26020 while( pIn ){
26021 p = pIn;
26022 pIn = p->pDirty;
26023 p->pDirty = 0;
26024 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
26025 if( a[i]==0 ){
26026 a[i] = p;
26027 break;
26028 }else{
26029 p = pcacheMergeDirtyList(a[i], p);
26030 a[i] = 0;
26031 }
26032 }
26033 if( NEVER(i==N_SORT_BUCKET-1) ){
26034 /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
26035 ** the input list. But that is impossible.
26036 */
26037 a[i] = pcacheMergeDirtyList(a[i], p);
26038 }
26039 }
26040 p = a[0];
26041 for(i=1; i<N_SORT_BUCKET; i++){
26042 if( a[i]==0 ) continue;
26043 p = p ? pcacheMergeDirtyList(p, a[i]) : a[i];
26044 }
26045 return p;
26046 }
26047
26048 /*
26049 ** Return a list of all dirty pages in the cache, sorted by page number.
26050 */
26051 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
26052 PgHdr *p;
26053 for(p=pCache->pDirty; p; p=p->pDirtyNext){
26054 p->pDirty = p->pDirtyNext;
26055 }
26056 return pcacheSortDirtyList(pCache->pDirty);
26057 }
26058
26059 /*
26060 ** Return the total number of references to all pages held by the cache.
26061 **
26062 ** This is not the total number of pages referenced, but the sum of the
26063 ** reference count for all pages.
26064 */
26065 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
26066 return pCache->nRefSum;
26067 }
26068
26069 /*
26070 ** Return the number of references to the page supplied as an argument.
26071 */
26072 SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
26073 return p->nRef;
26074 }
26075
26076 /*
26077 ** Return the total number of pages in the cache.
26078 */
26079 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
26080 assert( pCache->pCache!=0 );
26081 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
26082 }
26083
26084 #ifdef SQLITE_TEST
26085 /*
26086 ** Get the suggested cache-size value.
26087 */
26088 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
26089 return numberOfCachePages(pCache);
26090 }
26091 #endif
26092
26093 /*
26094 ** Set the suggested cache-size value.
26095 */
26096 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
26097 assert( pCache->pCache!=0 );
26098 pCache->szCache = mxPage;
26099 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
26100 numberOfCachePages(pCache));
26101 }
26102
26103 /*
26104 ** Set the suggested cache-spill value. Make no changes if if the
26105 ** argument is zero. Return the effective cache-spill size, which will
26106 ** be the larger of the szSpill and szCache.
26107 */
26108 SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){
26109 int res;
26110 assert( p->pCache!=0 );
26111 if( mxPage ){
26112 if( mxPage<0 ){
26113 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
26114 }
26115 p->szSpill = mxPage;
26116 }
26117 res = numberOfCachePages(p);
26118 if( res<p->szSpill ) res = p->szSpill;
26119 return res;
26120 }
26121
26122 /*
26123 ** Free up as much memory as possible from the page cache.
26124 */
26125 SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
26126 assert( pCache->pCache!=0 );
26127 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
26128 }
26129
26130 /*
26131 ** Return the size of the header added by this middleware layer
26132 ** in the page-cache hierarchy.
26133 */
26134 SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr)); }
26135
26136 /*
26137 ** Return the number of dirty pages currently in the cache, as a percentage
26138 ** of the configured cache size.
26139 */
26140 SQLITE_PRIVATE int sqlite3PCachePercentDirty(PCache *pCache){
26141 PgHdr *pDirty;
26142 int nDirty = 0;
26143 int nCache = numberOfCachePages(pCache);
26144 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
26145 return nCache ? (int)(((i64)nDirty * 100) / nCache) : 0;
26146 }
26147
26148 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
26149 /*
26150 ** For all dirty pages currently in the cache, invoke the specified
26151 ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
26152 ** defined.
26153 */
26154 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHd r *)){
26155 PgHdr *pDirty;
26156 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
26157 xIter(pDirty);
26158 }
26159 }
26160 #endif
26161
26162 /************** End of pcache.c **********************************************/
26163
26164 /* Chain include. */
26165 #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