/ Changes On Branch obsolete
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Changes In Branch obsolete Excluding Merge-Ins

This is equivalent to a diff from 2dbdfa5170 to c8ade949d4

2015-04-06
22:05
Detect and suppress an endless loops in clearDatabasePage() that might result from a corrupt database file. This is an edited cherry-pick from [30011ad2f55c] and [395bb3e677a]. (Leaf check-in: e5f5ef008d user: drh tags: apple-osx-385)
2014-08-15
22:37
Aborted merge. (Closed-Leaf check-in: c8ade949d4 user: numist tags: obsolete)
2014-06-30
19:57
Merge the latest trunk changes into the apple-osx branch. (check-in: f426876fe1 user: drh tags: apple-osx)
2014-06-05
12:53
Merge in the 3.8.5 release changes and the FTS integrity-check fix. (check-in: 2dbdfa5170 user: drh tags: apple-osx)
11:15
Fix a problem that was causing the FTS4 integrity-check command to fail if the table had one or more "notindexed" columns. (check-in: 7123bb2605 user: dan tags: trunk)
2014-05-29
20:36
Merge trunk changes into the apple-osx branch. (check-in: b9fad4490c user: drh tags: apple-osx)

Changes to Makefile.in.

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
         fts3_unicode.lo fts3_unicode2.lo fts3_write.lo \
         func.lo global.lo hash.lo \
         icu.lo insert.lo journal.lo legacy.lo loadext.lo \
         main.lo malloc.lo mem0.lo mem1.lo mem2.lo mem3.lo mem5.lo \
         memjournal.lo \
         mutex.lo mutex_noop.lo mutex_unix.lo mutex_w32.lo \
         notify.lo opcodes.lo os.lo os_unix.lo os_win.lo \
         pager.lo parse.lo pcache.lo pcache1.lo pragma.lo prepare.lo printf.lo \
         random.lo resolve.lo rowset.lo rtree.lo select.lo status.lo \
         table.lo tokenize.lo trigger.lo \
         update.lo util.lo vacuum.lo \
         vdbe.lo vdbeapi.lo vdbeaux.lo vdbeblob.lo vdbemem.lo vdbesort.lo \
         vdbetrace.lo wal.lo walker.lo where.lo utf.lo vtab.lo

# Object files for the amalgamation.







|







171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
         fts3_unicode.lo fts3_unicode2.lo fts3_write.lo \
         func.lo global.lo hash.lo \
         icu.lo insert.lo journal.lo legacy.lo loadext.lo \
         main.lo malloc.lo mem0.lo mem1.lo mem2.lo mem3.lo mem5.lo \
         memjournal.lo \
         mutex.lo mutex_noop.lo mutex_unix.lo mutex_w32.lo \
         notify.lo opcodes.lo os.lo os_unix.lo os_win.lo \
         pager.lo parse.lo pcache.lo pcache1.lo pcache_purgeable.lo pragma.lo prepare.lo printf.lo \
         random.lo resolve.lo rowset.lo rtree.lo select.lo status.lo \
         table.lo tokenize.lo trigger.lo \
         update.lo util.lo vacuum.lo \
         vdbe.lo vdbeapi.lo vdbeaux.lo vdbeblob.lo vdbemem.lo vdbesort.lo \
         vdbetrace.lo wal.lo walker.lo where.lo utf.lo vtab.lo

# Object files for the amalgamation.
245
246
247
248
249
250
251

252
253
254
255
256
257
258
259
260
261
262
263
264
265
266

267
268
269
270
271
272
273
  $(TOP)/src/os_win.h \
  $(TOP)/src/pager.c \
  $(TOP)/src/pager.h \
  $(TOP)/src/parse.y \
  $(TOP)/src/pcache.c \
  $(TOP)/src/pcache.h \
  $(TOP)/src/pcache1.c \

  $(TOP)/src/pragma.c \
  $(TOP)/src/prepare.c \
  $(TOP)/src/printf.c \
  $(TOP)/src/random.c \
  $(TOP)/src/resolve.c \
  $(TOP)/src/rowset.c \
  $(TOP)/src/select.c \
  $(TOP)/src/status.c \
  $(TOP)/src/shell.c \
  $(TOP)/src/sqlite.h.in \
  $(TOP)/src/sqlite3ext.h \
  $(TOP)/src/sqliteInt.h \
  $(TOP)/src/sqliteLimit.h \
  $(TOP)/src/table.c \
  $(TOP)/src/tclsqlite.c \

  $(TOP)/src/tokenize.c \
  $(TOP)/src/trigger.c \
  $(TOP)/src/utf.c \
  $(TOP)/src/update.c \
  $(TOP)/src/util.c \
  $(TOP)/src/vacuum.c \
  $(TOP)/src/vdbe.c \







>















>







245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
  $(TOP)/src/os_win.h \
  $(TOP)/src/pager.c \
  $(TOP)/src/pager.h \
  $(TOP)/src/parse.y \
  $(TOP)/src/pcache.c \
  $(TOP)/src/pcache.h \
  $(TOP)/src/pcache1.c \
  $(TOP)/src/pcache_purgeable.c \
  $(TOP)/src/pragma.c \
  $(TOP)/src/prepare.c \
  $(TOP)/src/printf.c \
  $(TOP)/src/random.c \
  $(TOP)/src/resolve.c \
  $(TOP)/src/rowset.c \
  $(TOP)/src/select.c \
  $(TOP)/src/status.c \
  $(TOP)/src/shell.c \
  $(TOP)/src/sqlite.h.in \
  $(TOP)/src/sqlite3ext.h \
  $(TOP)/src/sqliteInt.h \
  $(TOP)/src/sqliteLimit.h \
  $(TOP)/src/table.c \
  $(TOP)/src/tclsqlite.c \
  $(TOP)/src/test_intarray.h \
  $(TOP)/src/tokenize.c \
  $(TOP)/src/trigger.c \
  $(TOP)/src/utf.c \
  $(TOP)/src/update.c \
  $(TOP)/src/util.c \
  $(TOP)/src/vacuum.c \
  $(TOP)/src/vdbe.c \
329
330
331
332
333
334
335




336
337
338
339
340
341
342
  $(TOP)/ext/icu/icu.c
SRC += \
  $(TOP)/ext/rtree/rtree.h \
  $(TOP)/ext/rtree/rtree.c
SRC += \
  $(TOP)/ext/sqlrr/sqlrr.h \
  $(TOP)/ext/sqlrr/sqlrr.c






# Generated source code files
#
SRC += \
  keywordhash.h \
  opcodes.c \







>
>
>
>







331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
  $(TOP)/ext/icu/icu.c
SRC += \
  $(TOP)/ext/rtree/rtree.h \
  $(TOP)/ext/rtree/rtree.c
SRC += \
  $(TOP)/ext/sqlrr/sqlrr.h \
  $(TOP)/ext/sqlrr/sqlrr.c
SRC += \
  $(TOP)/src/test_intarray.c
SRC += \
  $(TOP)/src/sqlite3_private.h


# Generated source code files
#
SRC += \
  keywordhash.h \
  opcodes.c \
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
  $(TOP)/src/test_config.c \
  $(TOP)/src/test_demovfs.c \
  $(TOP)/src/test_devsym.c \
  $(TOP)/src/test_fs.c \
  $(TOP)/src/test_func.c \
  $(TOP)/src/test_hexio.c \
  $(TOP)/src/test_init.c \
  $(TOP)/src/test_intarray.c \
  $(TOP)/src/test_journal.c \
  $(TOP)/src/test_malloc.c \
  $(TOP)/src/test_multiplex.c \
  $(TOP)/src/test_mutex.c \
  $(TOP)/src/test_onefile.c \
  $(TOP)/src/test_osinst.c \
  $(TOP)/src/test_pcache.c \







<







371
372
373
374
375
376
377

378
379
380
381
382
383
384
  $(TOP)/src/test_config.c \
  $(TOP)/src/test_demovfs.c \
  $(TOP)/src/test_devsym.c \
  $(TOP)/src/test_fs.c \
  $(TOP)/src/test_func.c \
  $(TOP)/src/test_hexio.c \
  $(TOP)/src/test_init.c \

  $(TOP)/src/test_journal.c \
  $(TOP)/src/test_malloc.c \
  $(TOP)/src/test_multiplex.c \
  $(TOP)/src/test_mutex.c \
  $(TOP)/src/test_onefile.c \
  $(TOP)/src/test_osinst.c \
  $(TOP)/src/test_pcache.c \
467
468
469
470
471
472
473

474
475
476
477
478
479
480
   $(TOP)/src/pager.h \
   $(TOP)/src/pcache.h \
   parse.h  \
   sqlite3.h  \
   $(TOP)/src/sqlite3ext.h \
   $(TOP)/src/sqliteInt.h  \
   $(TOP)/src/sqliteLimit.h \

   $(TOP)/src/vdbe.h \
   $(TOP)/src/vdbeInt.h \
   $(TOP)/src/whereInt.h \
   config.h

# Header files used by extensions
#







>







472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
   $(TOP)/src/pager.h \
   $(TOP)/src/pcache.h \
   parse.h  \
   sqlite3.h  \
   $(TOP)/src/sqlite3ext.h \
   $(TOP)/src/sqliteInt.h  \
   $(TOP)/src/sqliteLimit.h \
   $(TOP)/src/test_intarray.h \
   $(TOP)/src/vdbe.h \
   $(TOP)/src/vdbeInt.h \
   $(TOP)/src/whereInt.h \
   config.h

# Header files used by extensions
#
708
709
710
711
712
713
714



715
716
717
718
719
720
721
	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/pager.c

pcache.lo:	$(TOP)/src/pcache.c $(HDR) $(TOP)/src/pcache.h
	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/pcache.c

pcache1.lo:	$(TOP)/src/pcache1.c $(HDR) $(TOP)/src/pcache.h
	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/pcache1.c




os.lo:	$(TOP)/src/os.c $(HDR)
	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/os.c

os_unix.lo:	$(TOP)/src/os_unix.c $(HDR)
	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/os_unix.c








>
>
>







714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/pager.c

pcache.lo:	$(TOP)/src/pcache.c $(HDR) $(TOP)/src/pcache.h
	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/pcache.c

pcache1.lo:	$(TOP)/src/pcache1.c $(HDR) $(TOP)/src/pcache.h
	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/pcache1.c

pcache_purgeable.lo:	$(TOP)/src/pcache_purgeable.c $(HDR) $(TOP)/src/pcache.h
	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/pcache_purgeable.c

os.lo:	$(TOP)/src/os.c $(HDR)
	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/os.c

os_unix.lo:	$(TOP)/src/os_unix.c $(HDR)
	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/os_unix.c

Changes to config.h.in.

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* Define to 1 if you have the `localtime_r' function. */
#undef HAVE_LOCALTIME_R

/* Define to 1 if you have the `localtime_s' function. */
#undef HAVE_LOCALTIME_S

/* Define to 1 if you have the <malloc.h> header file. */
#undef HAVE_MALLOC_H

/* Define to 1 if you have the `malloc_usable_size' function. */
#undef HAVE_MALLOC_USABLE_SIZE

/* Define to 1 if you have the <memory.h> header file. */
#undef HAVE_MEMORY_H

/* Define to 1 if you have the <stdint.h> header file. */
#undef HAVE_STDINT_H








|


|







30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* Define to 1 if you have the `localtime_r' function. */
#undef HAVE_LOCALTIME_R

/* Define to 1 if you have the `localtime_s' function. */
#undef HAVE_LOCALTIME_S

/* Define to 1 if you have the <malloc.h> header file. */
/* #undef HAVE_MALLOC_H */

/* Define to 1 if you have the `malloc_usable_size' function. */
/* #undef HAVE_MALLOC_USABLE_SIZE */

/* Define to 1 if you have the <memory.h> header file. */
#undef HAVE_MEMORY_H

/* Define to 1 if you have the <stdint.h> header file. */
#undef HAVE_STDINT_H

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* Define to 1 if you have the <unistd.h> header file. */
#undef HAVE_UNISTD_H

/* Define to 1 if you have the `usleep' function. */
#undef HAVE_USLEEP

/* Define to 1 if you have the utime() library function. */
#undef HAVE_UTIME

/* Define to the sub-directory in which libtool stores uninstalled libraries.
   */
#undef LT_OBJDIR

/* Define to the address where bug reports for this package should be sent. */
#undef PACKAGE_BUGREPORT







|







78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* Define to 1 if you have the <unistd.h> header file. */
#undef HAVE_UNISTD_H

/* Define to 1 if you have the `usleep' function. */
#undef HAVE_USLEEP

/* Define to 1 if you have the utime() library function. */
/* #undef HAVE_UTIME */

/* Define to the sub-directory in which libtool stores uninstalled libraries.
   */
#undef LT_OBJDIR

/* Define to the address where bug reports for this package should be sent. */
#undef PACKAGE_BUGREPORT

Changes to src/btree.c.

10
11
12
13
14
15
16

17
18
19
20
21
22
















23
24
25
26
27
28
29
**
*************************************************************************
** This file implements a external (disk-based) database using BTrees.
** See the header comment on "btreeInt.h" for additional information.
** Including a description of file format and an overview of operation.
*/
#include "btreeInt.h"


/*
** The header string that appears at the beginning of every
** SQLite database.
*/
static const char zMagicHeader[] = SQLITE_FILE_HEADER;

















/*
** Set this global variable to 1 to enable tracing using the TRACE
** macro.
*/
#if 0
int sqlite3BtreeTrace=1;  /* True to enable tracing */







>






>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
**
*************************************************************************
** This file implements a external (disk-based) database using BTrees.
** See the header comment on "btreeInt.h" for additional information.
** Including a description of file format and an overview of operation.
*/
#include "btreeInt.h"
#include <AvailabilityMacros.h>

/*
** The header string that appears at the beginning of every
** SQLite database.
*/
static const char zMagicHeader[] = SQLITE_FILE_HEADER;

#if defined(SQLITE_ENABLE_FLOCKTIMEOUT)&&(SQLITE_ENABLE_FLOCKTIMEOUT>0)
# if (defined(__APPLE__) && (!defined(TARGET_IPHONE_SIMULATOR)||(TARGET_IPHONE_SIMULATOR==0)) && ((TARGET_OS_EMBEDDED>0)||(MAC_OS_X_VERSION_10_9>0)))
#  define SQLITE_USE_FLOCKTIMEOUT 1
# endif
#endif /* SQLITE_ENABLE_FLOCKTIMEOUT */

/*
 ** Operator used with sqlite3_file_control to set the timeout for the next call
 ** to lock the database file via fcntl with F_SETLKWTIMEOUT.  The timeout should
 ** be passed as a pointer to a long representing the timeout in milliseconds
 */
#ifndef SQLITE_FCNTL_SET_NEXTFLOCKTIMEOUT
#define SQLITE_FCNTL_SET_NEXTFLOCKTIMEOUT      104
#define SQLITE_FCNTL_GET_NEXTFLOCKTIMEOUT      105
#endif

/*
** Set this global variable to 1 to enable tracing using the TRACE
** macro.
*/
#if 0
int sqlite3BtreeTrace=1;  /* True to enable tracing */
2764
2765
2766
2767
2768
2769
2770







2771
2772







2773
2774
2775
2776
2777
2778
2779
    /* Call lockBtree() until either pBt->pPage1 is populated or
    ** lockBtree() returns something other than SQLITE_OK. lockBtree()
    ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
    ** reading page 1 it discovers that the page-size of the database 
    ** file is not pBt->pageSize. In this case lockBtree() will update
    ** pBt->pageSize to the page-size of the file on disk.
    */







    while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );








    if( rc==SQLITE_OK && wrflag ){
      if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
        rc = SQLITE_READONLY;
      }else{
        rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
        if( rc==SQLITE_OK ){
          rc = newDatabase(pBt);







>
>
>
>
>
>
>


>
>
>
>
>
>
>







2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
    /* Call lockBtree() until either pBt->pPage1 is populated or
    ** lockBtree() returns something other than SQLITE_OK. lockBtree()
    ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
    ** reading page 1 it discovers that the page-size of the database 
    ** file is not pBt->pageSize. In this case lockBtree() will update
    ** pBt->pageSize to the page-size of the file on disk.
    */
    
#ifdef SQLITE_USE_FLOCKTIMEOUT
    int existingNextTimeout = 0;
    if (wrflag) {
      sqlite3_file_control(p->db, NULL, SQLITE_FCNTL_GET_NEXTFLOCKTIMEOUT, &existingNextTimeout);
    }
    
    while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );

    if (wrflag) {
      sqlite3_file_control(p->db, NULL, SQLITE_FCNTL_SET_NEXTFLOCKTIMEOUT, &existingNextTimeout);
    }
#else
    while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
#endif
    
    if( rc==SQLITE_OK && wrflag ){
      if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
        rc = SQLITE_READONLY;
      }else{
        rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
        if( rc==SQLITE_OK ){
          rc = newDatabase(pBt);

Changes to src/ctime.c.

43
44
45
46
47
48
49



50
51
52
53
54
55
56
  "CASE_SENSITIVE_LIKE",
#endif
#ifdef SQLITE_CHECK_PAGES
  "CHECK_PAGES",
#endif
#ifdef SQLITE_COVERAGE_TEST
  "COVERAGE_TEST",



#endif
#ifdef SQLITE_DEBUG
  "DEBUG",
#endif
#ifdef SQLITE_DEFAULT_LOCKING_MODE
  "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
#endif







>
>
>







43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  "CASE_SENSITIVE_LIKE",
#endif
#ifdef SQLITE_CHECK_PAGES
  "CHECK_PAGES",
#endif
#ifdef SQLITE_COVERAGE_TEST
  "COVERAGE_TEST",
#endif
#ifdef SQLITE_CURDIR
  "CURDIR",
#endif
#ifdef SQLITE_DEBUG
  "DEBUG",
#endif
#ifdef SQLITE_DEFAULT_LOCKING_MODE
  "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
#endif

Changes to src/loadext.c.

745
746
747
748
749
750
751
752
753
754
755
756
757
758
    }else{
      xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
              wsdAutoext.aExt[i];
    }
    sqlite3_mutex_leave(mutex);
    zErrmsg = 0;
    if( xInit && (rc = xInit(db, &zErrmsg, &sqlite3Apis))!=0 ){
      sqlite3Error(db, rc,
            "automatic extension loading failed: %s", zErrmsg);
      go = 0;
    }
    sqlite3_free(zErrmsg);
  }
}







<
|
|




745
746
747
748
749
750
751

752
753
754
755
756
757
    }else{
      xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
              wsdAutoext.aExt[i];
    }
    sqlite3_mutex_leave(mutex);
    zErrmsg = 0;
    if( xInit && (rc = xInit(db, &zErrmsg, &sqlite3Apis))!=0 ){

//      sqlite3Error(db, rc, "automatic extension loading failed: %s", zErrmsg);
//      go = 0;
    }
    sqlite3_free(zErrmsg);
  }
}

Changes to src/main.c.

11
12
13
14
15
16
17

18
19
20
21
22
23
24
25
26
27
28
29
30
31
































32
33
34
35
36
37
38
*************************************************************************
** Main file for the SQLite library.  The routines in this file
** implement the programmer interface to the library.  Routines in
** other files are for internal use by SQLite and should not be
** accessed by users of the library.
*/
#include "sqliteInt.h"


#ifdef SQLITE_ENABLE_SQLRR
# include "sqlrr.h"
#endif 
#ifdef SQLITE_ENABLE_FTS3
# include "fts3.h"
#endif
#ifdef SQLITE_ENABLE_RTREE
# include "rtree.h"
#endif
#ifdef SQLITE_ENABLE_ICU
# include "sqliteicu.h"
#endif

































#ifndef SQLITE_AMALGAMATION
/* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
** contains the text of SQLITE_VERSION macro. 
*/
const char sqlite3_version[] = SQLITE_VERSION;
#endif








>














>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
*************************************************************************
** Main file for the SQLite library.  The routines in this file
** implement the programmer interface to the library.  Routines in
** other files are for internal use by SQLite and should not be
** accessed by users of the library.
*/
#include "sqliteInt.h"
#include <AvailabilityMacros.h>

#ifdef SQLITE_ENABLE_SQLRR
# include "sqlrr.h"
#endif 
#ifdef SQLITE_ENABLE_FTS3
# include "fts3.h"
#endif
#ifdef SQLITE_ENABLE_RTREE
# include "rtree.h"
#endif
#ifdef SQLITE_ENABLE_ICU
# include "sqliteicu.h"
#endif

#if defined(SQLITE_ENABLE_FLOCKTIMEOUT)&&(SQLITE_ENABLE_FLOCKTIMEOUT>0)
# if (defined(__APPLE__) && (!defined(TARGET_IPHONE_SIMULATOR)||(TARGET_IPHONE_SIMULATOR==0)) && ((TARGET_OS_EMBEDDED>0)||(MAC_OS_X_VERSION_10_9>0)))
# include "sqlite3_private.h"
#  define SQLITE_USE_FLOCKTIMEOUT 1
# endif
#endif /* SQLITE_ENABLE_FLOCKTIMEOUT */

#if defined(__APPLE__)
# include <unistd.h>
# include <dispatch/dispatch.h>
# include <dispatch/private.h>
extern void _sqlite3_purgeEligiblePagerCacheMemory();
# if defined(DISPATCH_API_VERSION)&&(DISPATCH_API_VERSION>=20100226) && \
  defined(SQLITE_ENABLE_PURGEABLE_PCACHE)&&(SQLITE_ENABLE_PURGEABLE_PCACHE>0)
#  include <dlfcn.h> /* amalgamator: keep */ 
static dispatch_source_t _cache_event_source = NULL;
# endif
#endif

/*
 ** Operator used with sqlite3_file_control to set the timeout for the next call
 ** to lock the database file via fcntl with F_SETLKWTIMEOUT.  The timeout should
 ** be passed as a pointer to a long representing the timeout in milliseconds
 */
#ifndef SQLITE_FCNTL_SET_NEXTFLOCKTIMEOUT
#define SQLITE_FCNTL_SET_NEXTFLOCKTIMEOUT      104
#define SQLITE_FCNTL_GET_NEXTFLOCKTIMEOUT      105
#endif

/*
** The version of the library
*/
#ifndef SQLITE_AMALGAMATION
/* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
** contains the text of SQLITE_VERSION macro. 
*/
const char sqlite3_version[] = SQLITE_VERSION;
#endif

213
214
215
216
217
218
219








































220
221
222
223
224
225
226
      sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, 
          sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
      sqlite3GlobalConfig.isInit = 1;
#ifdef SQLITE_EXTRA_INIT
      bRunExtraInit = 1;
#endif
    }








































    sqlite3GlobalConfig.inProgress = 0;
  }
  sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);

  /* Go back under the static mutex and clean up the recursive
  ** mutex to prevent a resource leak.
  */







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
      sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, 
          sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
      sqlite3GlobalConfig.isInit = 1;
#ifdef SQLITE_EXTRA_INIT
      bRunExtraInit = 1;
#endif
    }
    
#if defined(__APPLE__) && defined(USETHREADASSERTS)
    if ((0 != access("/var/db/disableAppleInternal", R_OK))) {
//      fprintf(stderr, "SQLite multi-threading assertions ENABLED.\n");
      
      if (sqlite3GlobalConfig.bCoreMutex != 0) {
        sqlite3GlobalConfig.bFullMutex = 1;
      }
    } else {
      sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
      pTo->xMutexHeld = NULL;
      pTo->xMutexNotheld = NULL;
//      fprintf(stderr, "SQLite multi-threading assertions DISABLED.\n");
    }
#endif    
    
  
#if defined(DISPATCH_API_VERSION)&&(DISPATCH_API_VERSION>=20100226) && \
  defined(SQLITE_ENABLE_PURGEABLE_PCACHE)&&(SQLITE_ENABLE_PURGEABLE_PCACHE>0)
    
		dispatch_queue_t mq = dispatch_get_main_queue();
		if (mq) {
      if( NULL != dlopen("/usr/lib/libsqlite3.dylib", RTLD_LAZY) ){ 
        /* never dlclose to avoid dylib unloading */
        dispatch_async(mq, ^{
          dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
          if (queue) {
            _cache_event_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VM, 0, DISPATCH_VM_PRESSURE, queue);
            if (_cache_event_source != NULL) {
              dispatch_source_set_event_handler(_cache_event_source, ^{ 
                _sqlite3_purgeEligiblePagerCacheMemory();
              });
              dispatch_resume(_cache_event_source);
            }
          }
        });
      }
		}
#endif 
  
    sqlite3GlobalConfig.inProgress = 0;
  }
  sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);

  /* Go back under the static mutex and clean up the recursive
  ** mutex to prevent a resource leak.
  */
1227
1228
1229
1230
1231
1232
1233






1234
1235
1236
1237


1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253





1254




1255

1256
1257
1258
1259
1260
1261
1262
** argument.
*/
static int sqliteDefaultBusyCallback(
 void *ptr,               /* Database connection */
 int count                /* Number of times table has been busy */
){
#if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP)






  static const u8 delays[] =
     { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };
  static const u8 totals[] =
     { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };


# define NDELAY ArraySize(delays)
  sqlite3 *db = (sqlite3 *)ptr;
  int timeout = db->busyTimeout;
  int delay, prior;

  assert( count>=0 );
  if( count < NDELAY ){
    delay = delays[count];
    prior = totals[count];
  }else{
    delay = delays[NDELAY-1];
    prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
  }
  if( prior + delay > timeout ){
    delay = timeout - prior;
    if( delay<=0 ) return 0;





  }




  sqlite3OsSleep(db->pVfs, delay*1000);

  return 1;
#else
  sqlite3 *db = (sqlite3 *)ptr;
  int timeout = ((sqlite3 *)ptr)->busyTimeout;
  if( (count+1)*1000 > timeout ){
    return 0;
  }







>
>
>
>
>
>




>
>















|
>
>
>
>
>
|
>
>
>
>

>







1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
** argument.
*/
static int sqliteDefaultBusyCallback(
 void *ptr,               /* Database connection */
 int count                /* Number of times table has been busy */
){
#if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP)
# ifdef SQLITE_USE_FLOCKTIMEOUT
  static const u16 delays[] =
  { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100, 100, 100, 100, 100, 100, 100, 100,  100, 100, 100,  100,  100,  100,  100,  100,  100,  100,  100,  200,  200,  200,  200,  200,  250,  250,  250,  250, 333 }; // 333, 333, 500, 500, 500, 500, 1000, 1000, 1000, 10000 };
  static const u16 totals[] =
     { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228, 328, 428, 528, 628, 728, 828, 928, 1028, 1128, 1228, 1328, 1428, 1528, 1628, 1728, 1828, 1928, 2128, 2228, 2428, 2628, 2828, 3028, 3228, 3478, 3728, 3978, 4228, 4561, 4894, 5227, 5727, 6227, 6727, 7227, 8227, 9227 };
#else
  static const u8 delays[] =
     { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };
  static const u8 totals[] =
     { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };
#endif
  
# define NDELAY ArraySize(delays)
  sqlite3 *db = (sqlite3 *)ptr;
  int timeout = db->busyTimeout;
  int delay, prior;

  assert( count>=0 );
  if( count < NDELAY ){
    delay = delays[count];
    prior = totals[count];
  }else{
    delay = delays[NDELAY-1];
    prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
  }
  if( prior + delay > timeout ){
    delay = timeout - prior;
    if( delay<=0 ){
# ifdef SQLITE_USE_FLOCKTIMEOUT
      delay = 0; /* reset the delay */
      sqlite3_file_control(db, NULL, SQLITE_FCNTL_SET_NEXTFLOCKTIMEOUT, &delay);
# endif
      return 0;
    }
  }
# ifdef SQLITE_USE_FLOCKTIMEOUT
  sqlite3_file_control(db, NULL, SQLITE_FCNTL_SET_NEXTFLOCKTIMEOUT, &delay);
# else
  sqlite3OsSleep(db->pVfs, delay*1000);
# endif
  return 1;
#else
  sqlite3 *db = (sqlite3 *)ptr;
  int timeout = ((sqlite3 *)ptr)->busyTimeout;
  if( (count+1)*1000 > timeout ){
    return 0;
  }
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
  if( NULL!=autolog_client ){
    asl_close(autolog_client);
    autolog_client = NULL;
  }
}
static void _open_asl_log() {
  if( NULL==autolog_client ){
    autolog_client = asl_open("SQLite", NULL, 0);
    atexit(_close_asl_log);
  }
}

void _sqlite_auto_profile_syslog(void *aux, const char *sql, u64 ns);
void _sqlite_auto_trace_syslog(void *aux, const char *sql);
void _sqlite_auto_profile_syslog(void *aux, const char *sql, u64 ns) {
#pragma unused(aux)
	asl_log(autolog_client, NULL, ASL_LEVEL_NOTICE, "Query: %s\n Execution Time: %llu ms\n", sql, ns / 1000000);
}
void _sqlite_auto_trace_syslog(void *aux, const char *sql) {
	asl_log(autolog_client, NULL, ASL_LEVEL_NOTICE, "TraceSQL(%p): %s\n", aux, sql);
}
#endif

/*
** This function is used to parse both URIs and non-URI filenames passed by the
** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
** URIs specified as part of ATTACH statements.







|








|


|







2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
  if( NULL!=autolog_client ){
    asl_close(autolog_client);
    autolog_client = NULL;
  }
}
static void _open_asl_log() {
  if( NULL==autolog_client ){
    autolog_client = asl_open(NULL, NULL, 0);
    atexit(_close_asl_log);
  }
}

void _sqlite_auto_profile_syslog(void *aux, const char *sql, u64 ns);
void _sqlite_auto_trace_syslog(void *aux, const char *sql);
void _sqlite_auto_profile_syslog(void *aux, const char *sql, u64 ns) {
#pragma unused(aux)
	asl_log(autolog_client, NULL, ASL_LEVEL_NOTICE, "Query: %s\n Execution Time: %llu ms\n", sql ? sql : "", ns / 1000000);
}
void _sqlite_auto_trace_syslog(void *aux, const char *sql) {
	asl_log(autolog_client, NULL, ASL_LEVEL_NOTICE, "TraceSQL(%p): %s\n", aux, sql ? sql : "");
}
#endif

/*
** This function is used to parse both URIs and non-URI filenames passed by the
** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
** URIs specified as part of ATTACH statements.
2503
2504
2505
2506
2507
2508
2509



2510
2511
2512
2513
2514
2515
2516
  const char *zVfs       /* Name of the VFS to use */
){
  sqlite3 *db;                    /* Store allocated handle here */
  int rc;                         /* Return code */
  int isThreadsafe;               /* True for threadsafe connections */
  char *zOpen = 0;                /* Filename argument to pass to BtreeOpen() */
  char *zErrMsg = 0;              /* Error message from sqlite3ParseUri() */




  *ppDb = 0;
#ifndef SQLITE_OMIT_AUTOINIT
  rc = sqlite3_initialize();
  if( rc ) return rc;
#endif








>
>
>







2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
  const char *zVfs       /* Name of the VFS to use */
){
  sqlite3 *db;                    /* Store allocated handle here */
  int rc;                         /* Return code */
  int isThreadsafe;               /* True for threadsafe connections */
  char *zOpen = 0;                /* Filename argument to pass to BtreeOpen() */
  char *zErrMsg = 0;              /* Error message from sqlite3ParseUri() */
#if SQLITE_ENABLE_DATA_PROTECTION
  int protFlags = flags & SQLITE_OPEN_FILEPROTECTION_MASK;
#endif

  *ppDb = 0;
#ifndef SQLITE_OMIT_AUTOINIT
  rc = sqlite3_initialize();
  if( rc ) return rc;
#endif

2565
2566
2567
2568
2569
2570
2571



2572
2573
2574
2575
2576
2577
2578
               SQLITE_OPEN_TEMP_JOURNAL | 
               SQLITE_OPEN_SUBJOURNAL | 
               SQLITE_OPEN_MASTER_JOURNAL |
               SQLITE_OPEN_NOMUTEX |
               SQLITE_OPEN_FULLMUTEX |
               SQLITE_OPEN_WAL
             );




  /* Allocate the sqlite data structure */
  db = sqlite3MallocZero( sizeof(sqlite3) );
  if( db==0 ) goto opendb_out;
  if( isThreadsafe ){
    db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
    if( db->mutex==0 ){







>
>
>







2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
               SQLITE_OPEN_TEMP_JOURNAL | 
               SQLITE_OPEN_SUBJOURNAL | 
               SQLITE_OPEN_MASTER_JOURNAL |
               SQLITE_OPEN_NOMUTEX |
               SQLITE_OPEN_FULLMUTEX |
               SQLITE_OPEN_WAL
             );
#if SQLITE_ENABLE_DATA_PROTECTION
  flags |= protFlags;
#endif

  /* Allocate the sqlite data structure */
  db = sqlite3MallocZero( sizeof(sqlite3) );
  if( db==0 ) goto opendb_out;
  if( isThreadsafe ){
    db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
    if( db->mutex==0 ){
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
  /* Register all built-in functions, but do not attempt to read the
  ** database schema yet. This is delayed until the first time the database
  ** is accessed.
  */
  sqlite3Error(db, SQLITE_OK, 0);
  sqlite3RegisterBuiltinFunctions(db);

  /* Load automatic extensions - extensions that have been registered
  ** using the sqlite3_automatic_extension() API.
  */
  rc = sqlite3_errcode(db);
  if( rc==SQLITE_OK ){
    sqlite3AutoLoadExtensions(db);
    rc = sqlite3_errcode(db);
    if( rc!=SQLITE_OK ){
      goto opendb_out;
    }
  }

#ifdef SQLITE_ENABLE_FTS1
  if( !db->mallocFailed ){
    extern int sqlite3Fts1Init(sqlite3*);
    rc = sqlite3Fts1Init(db);
  }
#endif







<
<
<
<
<
<
<
<
<
<
<







2772
2773
2774
2775
2776
2777
2778











2779
2780
2781
2782
2783
2784
2785
  /* Register all built-in functions, but do not attempt to read the
  ** database schema yet. This is delayed until the first time the database
  ** is accessed.
  */
  sqlite3Error(db, SQLITE_OK, 0);
  sqlite3RegisterBuiltinFunctions(db);













#ifdef SQLITE_ENABLE_FTS1
  if( !db->mallocFailed ){
    extern int sqlite3Fts1Init(sqlite3*);
    rc = sqlite3Fts1Init(db);
  }
#endif
2718
2719
2720
2721
2722
2723
2724












2725
2726
2727
2728
2729
2730
2731
#endif

#ifdef SQLITE_ENABLE_RTREE
  if( !db->mallocFailed && rc==SQLITE_OK){
    rc = sqlite3RtreeInit(db);
  }
#endif













  /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
  ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
  ** mode.  Doing nothing at all also makes NORMAL the default.
  */
#ifdef SQLITE_DEFAULT_LOCKING_MODE
  db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;







>
>
>
>
>
>
>
>
>
>
>
>







2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
#endif

#ifdef SQLITE_ENABLE_RTREE
  if( !db->mallocFailed && rc==SQLITE_OK){
    rc = sqlite3RtreeInit(db);
  }
#endif

  /* Load automatic extensions - extensions that have been registered
  ** using the sqlite3_automatic_extension() API.
  */
  rc = sqlite3_errcode(db);
  if( rc==SQLITE_OK ){
    sqlite3AutoLoadExtensions(db);
    rc = sqlite3_errcode(db);
    if( rc!=SQLITE_OK ){
      goto opendb_out;
    }
  }

  /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
  ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
  ** mode.  Doing nothing at all also makes NORMAL the default.
  */
#ifdef SQLITE_DEFAULT_LOCKING_MODE
  db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
3599
3600
3601
3602
3603
3604
3605
3606








3607
    return state;
  }
  if( NULL!=db ){ 
    sqlite3_close(db); /* need to close even if open returns an error */
  }
  return SQLITE_LOCKSTATE_ERROR;
}









#endif /* SQLITE_ENABLE_APPLE_SPI */








>
>
>
>
>
>
>
>

3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
    return state;
  }
  if( NULL!=db ){ 
    sqlite3_close(db); /* need to close even if open returns an error */
  }
  return SQLITE_LOCKSTATE_ERROR;
}

/*
** Returns the system defined default sqlite3_busy_handler function
**
*/
int (*_sqlite3_system_busy_handler(void))(void*,int) {
  return &sqliteDefaultBusyCallback;
}

#endif /* SQLITE_ENABLE_APPLE_SPI */

Changes to src/mutex.c.

133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
*/
void sqlite3_mutex_leave(sqlite3_mutex *p){
  if( p ){
    sqlite3GlobalConfig.mutex.xMutexLeave(p);
  }
}

#ifndef NDEBUG
/*
** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
** intended for use inside assert() statements.
*/
int sqlite3_mutex_held(sqlite3_mutex *p){
  return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
}
int sqlite3_mutex_notheld(sqlite3_mutex *p){
  return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
}
#endif

#endif /* !defined(SQLITE_MUTEX_OMIT) */







|





|


|




133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
*/
void sqlite3_mutex_leave(sqlite3_mutex *p){
  if( p ){
    sqlite3GlobalConfig.mutex.xMutexLeave(p);
  }
}

#if defined(USETHREADASSERTS) || defined(SQLITE_DEBUG)
/*
** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
** intended for use inside assert() statements.
*/
int sqlite3_mutex_held(sqlite3_mutex *p){
  return p==0 || (sqlite3GlobalConfig.mutex.xMutexHeld && sqlite3GlobalConfig.mutex.xMutexHeld(p));
}
int sqlite3_mutex_notheld(sqlite3_mutex *p){
  return p==0 || (sqlite3GlobalConfig.mutex.xMutexNotheld && sqlite3GlobalConfig.mutex.xMutexNotheld(p));
}
#endif

#endif /* !defined(SQLITE_MUTEX_OMIT) */

Changes to src/mutex_unix.c.

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <pthread.h>

/*
** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
** are necessary under two condidtions:  (1) Debug builds and (2) using
** home-grown mutexes.  Encapsulate these conditions into a single #define.
*/
#if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
# define SQLITE_MUTEX_NREF 1
#else
# define SQLITE_MUTEX_NREF 0
#endif

/*
** Each recursive mutex is an instance of the following structure.







|







25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <pthread.h>

/*
** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
** are necessary under two condidtions:  (1) Debug builds and (2) using
** home-grown mutexes.  Encapsulate these conditions into a single #define.
*/
#if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX) || defined(USETHREADASSERTS)
# define SQLITE_MUTEX_NREF 1
#else
# define SQLITE_MUTEX_NREF 0
#endif

/*
** Each recursive mutex is an instance of the following structure.
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
** will not always work correctly on HPUX.
**
** On those platforms where pthread_equal() is not atomic, SQLite
** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
** make sure no assert() statements are evaluated and hence these
** routines are never called.
*/
#if !defined(NDEBUG) || defined(SQLITE_DEBUG)
static int pthreadMutexHeld(sqlite3_mutex *p){
  return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
}
static int pthreadMutexNotheld(sqlite3_mutex *p){
  return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
}
#endif







|







65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
** will not always work correctly on HPUX.
**
** On those platforms where pthread_equal() is not atomic, SQLite
** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
** make sure no assert() statements are evaluated and hence these
** routines are never called.
*/
#if defined(USETHREADASSERTS) || defined(SQLITE_DEBUG)
static int pthreadMutexHeld(sqlite3_mutex *p){
  return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
}
static int pthreadMutexNotheld(sqlite3_mutex *p){
  return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
}
#endif
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
    pthreadMutexInit,
    pthreadMutexEnd,
    pthreadMutexAlloc,
    pthreadMutexFree,
    pthreadMutexEnter,
    pthreadMutexTry,
    pthreadMutexLeave,
#ifdef SQLITE_DEBUG
    pthreadMutexHeld,
    pthreadMutexNotheld
#else
    0,
    0
#endif
  };

  return &sMutex;
}

#endif /* SQLITE_MUTEX_PTHREADS */







|












332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
    pthreadMutexInit,
    pthreadMutexEnd,
    pthreadMutexAlloc,
    pthreadMutexFree,
    pthreadMutexEnter,
    pthreadMutexTry,
    pthreadMutexLeave,
#if defined(USETHREADASSERTS) || defined(SQLITE_DEBUG)
    pthreadMutexHeld,
    pthreadMutexNotheld
#else
    0,
    0
#endif
  };

  return &sMutex;
}

#endif /* SQLITE_MUTEX_PTHREADS */

Changes to src/os_unix.c.

43
44
45
46
47
48
49

50
51
52
53
54
55
56
**   *  Definitions of sqlite3_vfs objects for all locking methods
**      plus implementations of sqlite3_os_init() and sqlite3_os_end().
*/
#include "sqliteInt.h"
#if SQLITE_OS_UNIX              /* This file is used on unix only */

#include "btreeInt.h"           /* Use by Apple extensions only */



/*
** There are various methods for file locking used for concurrency
** control:
**
**   1. POSIX locking (the default),







>







43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
**   *  Definitions of sqlite3_vfs objects for all locking methods
**      plus implementations of sqlite3_os_init() and sqlite3_os_end().
*/
#include "sqliteInt.h"
#if SQLITE_OS_UNIX              /* This file is used on unix only */

#include "btreeInt.h"           /* Use by Apple extensions only */
#include <AvailabilityMacros.h>


/*
** There are various methods for file locking used for concurrency
** control:
**
**   1. POSIX locking (the default),
91
92
93
94
95
96
97

98
99
100
101
102
103
104
105
106
107
108

109




110
111
112
113
114
115
116
117
118
119
120
121
122



123
124
125
126






127
128
129
130
131

132
133
134
135
136
137
138
139
140
141


































































































































142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>

#include <errno.h>
#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
#include <sys/mman.h>
#endif


#if SQLITE_ENABLE_LOCKING_STYLE
# include <sys/ioctl.h>
# include <uuid/uuid.h>
# if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
                            (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))

#  define HAVE_GETHOSTUUID 1




# endif
# if OS_VXWORKS
#  include <semaphore.h>
#  include <limits.h>
# else
#  include <sys/file.h>
#  include <sys/param.h>
# endif
#endif /* SQLITE_ENABLE_LOCKING_STYLE */

#if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
# include <sys/mount.h>
#endif




#ifdef HAVE_UTIME
# include <utime.h>
#endif







/*
** Allowed values of unixFile.fsFlags
*/
#define SQLITE_FSFLAGS_IS_MSDOS     0x1


/*
** If we are to be thread-safe, include the pthreads header and define
** the SQLITE_UNIX_THREADS macro.
*/
#if SQLITE_THREADSAFE
# include <pthread.h>
# define SQLITE_UNIX_THREADS 1
#endif



































































































































/*
** Default permissions when creating a new file
*/
#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
#endif

/*
** Default permissions when creating auto proxy dir
*/
#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
#endif

/*
** Maximum supported path-length.
*/
#define MAX_PATHNAME 512

/*
** Only set the lastErrno if the error code is a real error and not 
** a normal expected return code of SQLITE_BUSY or SQLITE_OK
*/
#define IS_LOCK_ERROR(x)  ((x != SQLITE_OK) && (x != SQLITE_BUSY))








>











>

>
>
>
>













>
>
>




>
>
>
>
>
>





>










>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

















|







92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <sys/syslimits.h>
#include <errno.h>
#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
#include <sys/mman.h>
#endif


#if SQLITE_ENABLE_LOCKING_STYLE
# include <sys/ioctl.h>
# include <uuid/uuid.h>
# if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \
                            (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000))
#if (!defined(TARGET_OS_EMBEDDED)||(TARGET_OS_EMBEDDED==0))&&(!defined(TARGET_IPHONE_SIMULATOR)||(TARGET_IPHONE_SIMULATOR==0))
#  define HAVE_GETHOSTUUID 1
#warning "gethostuuid() is enabled."
#else
#warning "gethostuuid() is disabled."
#endif
# endif
# if OS_VXWORKS
#  include <semaphore.h>
#  include <limits.h>
# else
#  include <sys/file.h>
#  include <sys/param.h>
# endif
#endif /* SQLITE_ENABLE_LOCKING_STYLE */

#if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
# include <sys/mount.h>
#endif
#if defined(__APPLE__) && defined(SQLITE_ENABLE_LOCKING_STYLE)
# include <CommonCrypto/CommonDigest.h>
#endif

#ifdef HAVE_UTIME
# include <utime.h>
#endif

#if defined(__APPLE__)
#include <mach/mach.h>
#include <mach/mach_vm.h>
#include <pwd.h>
#endif

/*
** Allowed values of unixFile.fsFlags
*/
#define SQLITE_FSFLAGS_IS_MSDOS     0x1
#define SQLITE_FSFLAGS_IS_MMAP_SAFE 0x2

/*
** If we are to be thread-safe, include the pthreads header and define
** the SQLITE_UNIX_THREADS macro.
*/
#if SQLITE_THREADSAFE
# include <pthread.h>
# define SQLITE_UNIX_THREADS 1
#endif

/*
 ** Operator used with sqlite3_file_control to set the timeout for the next call
 ** to lock the database file via fcntl with F_SETLKWTIMEOUT.  The timeout should
 ** be passed as a pointer to a long representing the timeout in milliseconds
 */
#ifndef SQLITE_FCNTL_SET_NEXTFLOCKTIMEOUT
#define SQLITE_FCNTL_SET_NEXTFLOCKTIMEOUT      104
#define SQLITE_FCNTL_GET_NEXTFLOCKTIMEOUT      105
#endif

#if defined(__APPLE__)
#include <asl.h>
#include <libkern/OSAtomic.h>
static aslclient errorlog_client = NULL;

static void _close_error_asl_log() {
  if( NULL!=errorlog_client ){
    asl_close(errorlog_client);
    errorlog_client = NULL;
  }
}
static void _open_error_asl_log() {
  OSMemoryBarrier();
  if( NULL==errorlog_client ){
    aslclient aclient = asl_open(NULL, NULL, ASL_OPT_STDERR);
    
    if (OSAtomicCompareAndSwapPtrBarrier(NULL, (void*)aclient, (void* volatile*)&errorlog_client)) {
      atexit(_close_error_asl_log);
    } else {
      asl_close(aclient);
    }    
  }
}

#endif

#if defined(USE_GUARDED_FD)&&defined(__APPLE__)
# include <dispatch/dispatch.h>
# include <dlfcn.h>

/* taken from <sys/guarded.h> */
# ifndef _GUARDID_T
typedef __uint64_t guardid_t;
#  define GUARD_CLOSE             (1u << 0)
#  define GUARD_DUP               (1u << 1)
#  define GUARD_SOCKET_IPC        (1u << 2)
#  define GUARD_FILEPORT          (1u << 3)
# endif /* _GUARDID_T */

#ifndef GUARD_WRITE
#define GUARD_WRITE		(1u << 4)
#endif

#ifndef GUARD_MMAP
#define GUARD_MMAP		(1u << 5)
#endif

#ifndef PROTECTION_CLASS_A
#define PROTECTION_CLASS_A 1
#define PROTECTION_CLASS_B 2
#define PROTECTION_CLASS_C 3
#define PROTECTION_CLASS_D 4
#endif

#ifndef PROTECTION_CLASS_SYSTEM_DEFAULT
#define PROTECTION_CLASS_SYSTEM_DEFAULT 0
#endif

static int unixProtectionClassForVFSProtectionFlags(int protFlags) {
  protFlags &= SQLITE_OPEN_FILEPROTECTION_MASK;
  
  switch (protFlags) {
    case SQLITE_OPEN_FILEPROTECTION_COMPLETE:
      return PROTECTION_CLASS_A;
    case SQLITE_OPEN_FILEPROTECTION_COMPLETEUNLESSOPEN:
      return PROTECTION_CLASS_B;
    case SQLITE_OPEN_FILEPROTECTION_COMPLETEUNTILFIRSTUSERAUTHENTICATION:
      return PROTECTION_CLASS_C;
    case SQLITE_OPEN_FILEPROTECTION_NONE:
      return PROTECTION_CLASS_D;
    default:
    return PROTECTION_CLASS_SYSTEM_DEFAULT;
  }
}

static int shmUnixProtectionClassForVFSProtectionFlags(int protFlags) {
  protFlags &= SQLITE_OPEN_FILEPROTECTION_MASK;
  
  switch (protFlags) {
    case SQLITE_OPEN_FILEPROTECTION_COMPLETE:
      return PROTECTION_CLASS_C;
    case SQLITE_OPEN_FILEPROTECTION_COMPLETEUNLESSOPEN:
      return PROTECTION_CLASS_C;
    case SQLITE_OPEN_FILEPROTECTION_COMPLETEUNTILFIRSTUSERAUTHENTICATION:
      return PROTECTION_CLASS_C;
    case SQLITE_OPEN_FILEPROTECTION_NONE:
      return PROTECTION_CLASS_D;
    default:
      return PROTECTION_CLASS_D;
  }
}

static guardid_t guard = 0x8fd4dbfade2deadull;
static dispatch_once_t guard_once_token;

// extern user_ssize_t guarded_pwrite_np(int fd, const guardid_t *guard, user_addr_t buf, user_size_t nbyte, off_t offset);
// extern user_addr_t guarded_mmap_np(caddr_t addr, const guardid_t *guard, size_t len, int prot, int flags, int fd, off_t pos);
// extern user_ssize_t guarded_write_np(int fd, const guardid_t *guard, user_addr_t cbuf, user_size_t nbyte);


static int (*dl_guarded_open_dprotected_np)(const char *path, const guardid_t *guard, u_int guardflags, int flags, int dpclass, int dpflags, ...);

static int (*dl_guarded_open_np)(const char *path, const guardid_t *guard, u_int guardflags, int flags, ...);

static int (*dl_guarded_close_np)(int fd, const guardid_t *guard);

static int (*dl_guarded_pwrite_np)(int fd, const guardid_t *guard, user_addr_t buf, user_size_t nbyte, off_t offset);

static int (*dl_guarded_write_np)(int fd, const guardid_t *guard, user_addr_t cbuf, user_size_t nbyte);

static int (*dl_guarded_mmap_np)(caddr_t addr, const guardid_t *guard, size_t len, int prot, int flags, int fd, off_t pos);

#endif /* USE_GUARDED_FD */

#if defined(SQLITE_ENABLE_FLOCKTIMEOUT)&&(SQLITE_ENABLE_FLOCKTIMEOUT>0)
# if (defined(__APPLE__) && (!defined(TARGET_IPHONE_SIMULATOR)||(TARGET_IPHONE_SIMULATOR==0)) && ((TARGET_OS_EMBEDDED>0)||(MAC_OS_X_VERSION_10_9>0)))
#  define SQLITE_USE_FLOCKTIMEOUT 1
# endif
#endif /* SQLITE_ENABLE_FLOCKTIMEOUT */

/*
** Default permissions when creating a new file
*/
#ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
# define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
#endif

/*
** Default permissions when creating auto proxy dir
*/
#ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
# define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
#endif

/*
** Maximum supported path-length.
*/
#define MAX_PATHNAME PATH_MAX

/*
** Only set the lastErrno if the error code is a real error and not 
** a normal expected return code of SQLITE_BUSY or SQLITE_OK
*/
#define IS_LOCK_ERROR(x)  ((x != SQLITE_OK) && (x != SQLITE_BUSY))

216
217
218
219
220
221
222



223
224
225
226
227
228
229
#endif
#if SQLITE_ENABLE_DATA_PROTECTION
  int protFlags;                      /* Data protection flags from unixOpen */
#endif
#if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
  unsigned fsFlags;                   /* cached details from statfs() */
#endif



#if OS_VXWORKS
  struct vxworksFileId *pId;          /* Unique file ID */
#endif
#ifdef SQLITE_DEBUG
  /* The next group of variables are used to track whether or not the
  ** transaction counter in bytes 24-27 of database files are updated
  ** whenever any part of the database changes.  An assertion fault will







>
>
>







363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#endif
#if SQLITE_ENABLE_DATA_PROTECTION
  int protFlags;                      /* Data protection flags from unixOpen */
#endif
#if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
  unsigned fsFlags;                   /* cached details from statfs() */
#endif
#ifdef SQLITE_USE_FLOCKTIMEOUT
  int nextFlockTimeout;
#endif
#if OS_VXWORKS
  struct vxworksFileId *pId;          /* Unique file ID */
#endif
#ifdef SQLITE_DEBUG
  /* The next group of variables are used to track whether or not the
  ** transaction counter in bytes 24-27 of database files are updated
  ** whenever any part of the database changes.  An assertion fault will
238
239
240
241
242
243
244








245
246
247
248
249
250
251
#endif

#ifdef SQLITE_TEST
  /* In test mode, increase the size of this structure a bit so that 
  ** it is larger than the struct CrashFile defined in test6.c.
  */
  char aPadding[32];








#endif
};

/* This variable holds the process id (pid) from when the xRandomness()
** method was called.  If xOpen() is called from a different process id,
** indicating that a fork() has occurred, the PRNG will be reset.
*/







>
>
>
>
>
>
>
>







388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#endif

#ifdef SQLITE_TEST
  /* In test mode, increase the size of this structure a bit so that 
  ** it is larger than the struct CrashFile defined in test6.c.
  */
  char aPadding[32];
#endif
#if defined(__APPLE__)
    /* <rdar://problem/17742712> */
    /* This variable is used to sanity check reads and writes against
    ** corrupt offsets or lengths. No reads or writes should ever exceed
    ** the total capacity of the volume.
    */
    i64 nFsBytes;
#endif
};

/* This variable holds the process id (pid) from when the xRandomness()
** method was called.  If xOpen() is called from a different process id,
** indicating that a fork() has occurred, the PRNG will be reset.
*/
599
600
601
602
603
604
605
606

























































































607
608
609
610
611
612
613
614
615
616
617



































618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
** Different Unix systems declare open() in different ways.  Same use
** open(const char*,int,mode_t).  Others use open(const char*,int,...).
** The difference is important when using a pointer to the function.
**
** The safest way to deal with the problem is to always use this wrapper
** which always has the same well-defined interface.
*/
static int posixOpen(const char *zFile, int flags, int mode){

























































































  return open(zFile, flags, mode);
}

/*
** On some systems, calls to fchown() will trigger a message in a security
** log if they come from non-root processes.  So avoid calling fchown() if
** we are not running as root.
*/
static int posixFchown(int fd, uid_t uid, gid_t gid){
  return geteuid() ? 0 : fchown(fd,uid,gid);
}




































/* Forward reference */
static int openDirectory(const char*, int*);
static int unixGetpagesize(void);

/*
** Many system calls are accessed through pointer-to-functions so that
** they may be overridden at runtime to facilitate fault injection during
** testing and sandboxing.  The following array holds the names and pointers
** to all overrideable system calls.
*/
static struct unix_syscall {
  const char *zName;            /* Name of the system call */
  sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
  sqlite3_syscall_ptr pDefault; /* Default value */
} aSyscall[] = {
  { "open",         (sqlite3_syscall_ptr)posixOpen,  0  },
#define osOpen      ((int(*)(const char*,int,int))aSyscall[0].pCurrent)

  { "close",        (sqlite3_syscall_ptr)close,      0  },
#define osClose     ((int(*)(int))aSyscall[1].pCurrent)

  { "access",       (sqlite3_syscall_ptr)access,     0  },
#define osAccess    ((int(*)(const char*,int))aSyscall[2].pCurrent)

  { "getcwd",       (sqlite3_syscall_ptr)getcwd,     0  },
#define osGetcwd    ((char*(*)(char*,size_t))aSyscall[3].pCurrent)







|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>











>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
















|


|







757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
** Different Unix systems declare open() in different ways.  Same use
** open(const char*,int,mode_t).  Others use open(const char*,int,...).
** The difference is important when using a pointer to the function.
**
** The safest way to deal with the problem is to always use this wrapper
** which always has the same well-defined interface.
*/
static int posixOpen(const char *zFile, int flags, int mode, int protectionFlags, int allowmmap){
#if defined(USE_GUARDED_FD)&&defined(__APPLE__)
  dispatch_once(&guard_once_token, ^{
    void *kernellib = (void *)dlopen("/usr/lib/libSystem.dylib", RTLD_GLOBAL);
    assert(kernellib);
    if (kernellib) {
#if SQLITE_ENABLE_DATA_PROTECTION
      dl_guarded_open_dprotected_np = dlsym(kernellib, "guarded_open_dprotected_np");
#else
      dl_guarded_open_dprotected_np = NULL;
#endif
      dl_guarded_open_np = dlsym(kernellib, "guarded_open_np");
      
      if (dl_guarded_open_dprotected_np || dl_guarded_open_np) {
        dl_guarded_close_np = dlsym(kernellib, "guarded_close_np");
        dl_guarded_pwrite_np = dlsym(kernellib, "guarded_pwrite_np");
        dl_guarded_write_np = dlsym(kernellib, "guarded_write_np");
        dl_guarded_mmap_np = NULL; // dlsym(kernellib, "guarded_mmap_np");
        
#if ((!defined(TARGET_IPHONE_SIMULATOR)||(TARGET_IPHONE_SIMULATOR==0)) && (!defined(__i386__)))
        char* isbandi = getenv("RC_XBS");
        if (isbandi && (strlen(isbandi) > 0)) {
          dl_guarded_pwrite_np = NULL;
          dl_guarded_write_np = NULL;
          dl_guarded_mmap_np = NULL;
        }
#else
        dl_guarded_open_dprotected_np = NULL;
        dl_guarded_pwrite_np = NULL;
        dl_guarded_write_np = NULL;
        dl_guarded_mmap_np = NULL;
#endif
        
#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST)
        _open_error_asl_log();
        if (dl_guarded_open_dprotected_np) {
          asl_log(errorlog_client, NULL, ASL_LEVEL_NOTICE, "SQLite: Found guarded_open_dprotected_np\n");
        } else if (dl_guarded_open_np) {
          asl_log(errorlog_client, NULL, ASL_LEVEL_NOTICE, "SQLite: Using guarded_open_np\n");
        }
        if (dl_guarded_pwrite_np) {
          asl_log(errorlog_client, NULL, ASL_LEVEL_NOTICE, "SQLite: Using guarded_pwrite_np\n");
        } else {
          asl_log(errorlog_client, NULL, ASL_LEVEL_WARNING, "SQLite: Failed to find guarded_pwrite_np!\n");
        }
        if (dl_guarded_write_np) {
          asl_log(errorlog_client, NULL, ASL_LEVEL_NOTICE, "SQLite: Using guarded_write_np\n");
          if (!dl_guarded_pwrite_np) {
            dl_guarded_write_np = NULL;
          }
        } else {
          asl_log(errorlog_client, NULL, ASL_LEVEL_WARNING, "SQLite: Failed to find guarded_write_np!\n");
          dl_guarded_pwrite_np = NULL;
        }

        if( dl_guarded_close_np==NULL|| (dl_guarded_open_np == NULL && dl_guarded_open_dprotected_np == NULL) ){
          _open_error_asl_log();
          asl_log(errorlog_client, NULL, ASL_LEVEL_ERR, "SQLite: Failed to find guarded open/close\n");
        }
#else
        if (dl_guarded_pwrite_np == NULL || dl_guarded_write_np == NULL) {
          dl_guarded_pwrite_np = NULL;
          dl_guarded_write_np = NULL;
        }
#endif
      }
    }
  });
  
  if(dl_guarded_open_np || dl_guarded_open_dprotected_np){
    int guardflags = GUARD_CLOSE | GUARD_DUP | GUARD_SOCKET_IPC | GUARD_FILEPORT;
    if (dl_guarded_pwrite_np && dl_guarded_write_np) {
      if (!allowmmap) {
        guardflags |= (GUARD_WRITE );
      }
    }
    if (dl_guarded_mmap_np) {
      guardflags |= ( GUARD_MMAP );
    }
    int dpc = 0;
    if (flags & O_CREAT) {
      dpc = protectionFlags ? protectionFlags : 0;
    }
    if (dl_guarded_open_dprotected_np && dpc) {
      return dl_guarded_open_dprotected_np(zFile, &guard, guardflags, flags, dpc, 0, mode);
    } else {
      return dl_guarded_open_np(zFile, &guard, guardflags, flags, mode);
    }
  }
#endif
  return open(zFile, flags, mode);
}

/*
** On some systems, calls to fchown() will trigger a message in a security
** log if they come from non-root processes.  So avoid calling fchown() if
** we are not running as root.
*/
static int posixFchown(int fd, uid_t uid, gid_t gid){
  return geteuid() ? 0 : fchown(fd,uid,gid);
}

static ssize_t apple_write(int fildes, const void *buf, size_t nbyte, off_t offset) {
#if defined(USE_GUARDED_FD)&&defined(__APPLE__)
  if( dl_guarded_write_np ){
      return dl_guarded_write_np(fildes, &guard, buf, nbyte);
  }
#endif
  return write(fildes, buf, nbyte);
}

static ssize_t apple_Pwrite(int fildes, const void *buf, size_t nbyte, off_t offset) {
#if defined(USE_GUARDED_FD)&&defined(__APPLE__)
  if( dl_guarded_pwrite_np ){
      return dl_guarded_pwrite_np(fildes, &guard, buf, nbyte, offset);
  }
#endif
  return pwrite(fildes, buf, nbyte, offset);;
}

static ssize_t os_rawPwrite(int fildes, const void *buf, size_t nbyte, off_t offset) {
  return pwrite(fildes, buf, nbyte, offset);
}

static int posixOpen2(const char *zFile, int flags, int mode){
  return posixOpen(zFile, flags, mode, 0, 1);
}

static int posixClose(int fd){
#if defined(USE_GUARDED_FD)&&defined(__APPLE__)
  if( dl_guarded_close_np ){
    return dl_guarded_close_np(fd, &guard);
  }
#endif
  return close(fd);
}

/* Forward reference */
static int openDirectory(const char*, int*);
static int unixGetpagesize(void);

/*
** Many system calls are accessed through pointer-to-functions so that
** they may be overridden at runtime to facilitate fault injection during
** testing and sandboxing.  The following array holds the names and pointers
** to all overrideable system calls.
*/
static struct unix_syscall {
  const char *zName;            /* Name of the system call */
  sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
  sqlite3_syscall_ptr pDefault; /* Default value */
} aSyscall[] = {
  { "open",         (sqlite3_syscall_ptr)posixOpen2,  0  },
#define osOpen      ((int(*)(const char*,int,int))aSyscall[0].pCurrent)

  { "close",        (sqlite3_syscall_ptr)posixClose, 0  },
#define osClose     ((int(*)(int))aSyscall[1].pCurrent)

  { "access",       (sqlite3_syscall_ptr)access,     0  },
#define osAccess    ((int(*)(const char*,int))aSyscall[2].pCurrent)

  { "getcwd",       (sqlite3_syscall_ptr)getcwd,     0  },
#define osGetcwd    ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
#if defined(USE_PREAD64)
  { "pread64",      (sqlite3_syscall_ptr)pread64,    0  },
#else
  { "pread64",      (sqlite3_syscall_ptr)0,          0  },
#endif
#define osPread64   ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)

  { "write",        (sqlite3_syscall_ptr)write,      0  },
#define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)

#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
  { "pwrite",       (sqlite3_syscall_ptr)pwrite,     0  },
#else
  { "pwrite",       (sqlite3_syscall_ptr)0,          0  },
#endif
#define osPwrite    ((ssize_t(*)(int,const void*,size_t,off_t))\
                    aSyscall[12].pCurrent)

#if defined(USE_PREAD64)







|



|







961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
#if defined(USE_PREAD64)
  { "pread64",      (sqlite3_syscall_ptr)pread64,    0  },
#else
  { "pread64",      (sqlite3_syscall_ptr)0,          0  },
#endif
#define osPread64   ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)

  { "write",        (sqlite3_syscall_ptr)apple_write,      0  },
#define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)

#if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
  { "pwrite",       (sqlite3_syscall_ptr)apple_Pwrite,     0  },
#else
  { "pwrite",       (sqlite3_syscall_ptr)0,          0  },
#endif
#define osPwrite    ((ssize_t(*)(int,const void*,size_t,off_t))\
                    aSyscall[12].pCurrent)

#if defined(USE_PREAD64)
729
730
731
732
733
734
735


736
737
738
739
740
741
742

#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
  { "mmap",       (sqlite3_syscall_ptr)mmap,     0 },
#define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[21].pCurrent)

  { "munmap",       (sqlite3_syscall_ptr)munmap,          0 },
#define osMunmap ((void*(*)(void*,size_t))aSyscall[22].pCurrent)



#if HAVE_MREMAP
  { "mremap",       (sqlite3_syscall_ptr)mremap,          0 },
#else
  { "mremap",       (sqlite3_syscall_ptr)0,               0 },
#endif
#define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)







>
>







1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026

#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
  { "mmap",       (sqlite3_syscall_ptr)mmap,     0 },
#define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[21].pCurrent)

  { "munmap",       (sqlite3_syscall_ptr)munmap,          0 },
#define osMunmap ((void*(*)(void*,size_t))aSyscall[22].pCurrent)

{ "umask",        (sqlite3_syscall_ptr)0,           0 },

#if HAVE_MREMAP
  { "mremap",       (sqlite3_syscall_ptr)mremap,          0 },
#else
  { "mremap",       (sqlite3_syscall_ptr)0,               0 },
#endif
#define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
852
853
854
855
856
857
858

859
860
861
862
863
864





865
866
867
868
869
870
871
872
873
874
875
876

877

878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894




895
896
897
898
899
900
901
** and -shm files.  We want those files to have *exactly* the same
** permissions as their original database, unadulterated by the umask.
** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
** transaction crashes and leaves behind hot journals, then any
** process that is able to write to the database will also be able to
** recover the hot journals.
*/

static int robust_open(const char *z, int f, mode_t m){
  int fd;
  mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
  while(1){
#if defined(O_CLOEXEC)
    fd = osOpen(z,f|O_CLOEXEC,m2);





#else
    fd = osOpen(z,f,m2);
#endif
    if( fd<0 ){
      if( errno==EINTR ) continue;
      break;
    }
    if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break;
    osClose(fd);
    sqlite3_log(SQLITE_WARNING, 
                "attempt to open \"%s\" as file descriptor %d", z, fd);
    fd = -1;

    if( osOpen("/dev/null", f, m)<0 ) break;

  }
  if( fd>=0 ){
    if( m!=0 ){
      struct stat statbuf;
      if( osFstat(fd, &statbuf)==0 
       && statbuf.st_size==0
       && (statbuf.st_mode&0777)!=m 
      ){
        osFchmod(fd, m);
      }
    }
#if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
    osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
#endif
  }
  return fd;
}





/*
** Helper functions to obtain and relinquish the global mutex. The
** global mutex is used to protect the unixInodeInfo and
** vxworksFileId objects used by this file, all of which may be 
** shared by multiple threads.
**







>
|
|

|

|
>
>
>
>
>
|
|
<
<
<
<






>

>
|
|
|
|
|












>
>
>
>







1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156




1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
** and -shm files.  We want those files to have *exactly* the same
** permissions as their original database, unadulterated by the umask.
** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
** transaction crashes and leaves behind hot journals, then any
** process that is able to write to the database will also be able to
** recover the hot journals.
*/

static int robust_open2(const char *z, int f, mode_t m, int protectionFlags, int allowmmap){
  int fd = -1;
  mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;

#if defined(O_CLOEXEC)
  f = f|O_CLOEXEC;
#endif

  do{
    if (osOpen == posixOpen2) {
      fd = posixOpen(z,f,m2, protectionFlags, allowmmap);
    } else {
      fd = osOpen(z,f,m2);




    }
    if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break;
    osClose(fd);
    sqlite3_log(SQLITE_WARNING, 
                "attempt to open \"%s\" as file descriptor %d", z, fd);
    fd = -1;
    // numist: is this ok with sandboxing?
    if( osOpen("/dev/null", f, m)<0 ) break;
  }while( fd<0 && errno==EINTR );

  if( fd>=0 ) {
  if( m!=0 ){
    struct stat statbuf;
    if( osFstat(fd, &statbuf)==0 
       && statbuf.st_size==0
       && (statbuf.st_mode&0777)!=m 
      ){
        osFchmod(fd, m);
      }
    }
#if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
    osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
#endif
  }
  return fd;
}

static int robust_open(const char *z, int f, mode_t m, int protectionFlags) {
  return robust_open2(z, f, m, protectionFlags, 0);
}

/*
** Helper functions to obtain and relinquish the global mutex. The
** global mutex is used to protect the unixInodeInfo and
** vxworksFileId objects used by this file, all of which may be 
** shared by multiple threads.
**
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
static void closePendingFds(unixFile *pFile){
  unixInodeInfo *pInode = pFile->pInode;
  UnixUnusedFd *p;
  UnixUnusedFd *pNext;
  for(p=pInode->pUnused; p; p=pNext){
    pNext = p->pNext;
#if OSCLOSE_CHECK_CLOSE_IOERR
    if( close(p->fd) ){
      storeLastErrno(pFile, errno);
      rc = SQLITE_IOERR_CLOSE;
      p->pNext = pError;
      pError = p;
    }else{
      sqlite3_free(p);
    }







|







1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
static void closePendingFds(unixFile *pFile){
  unixInodeInfo *pInode = pFile->pInode;
  UnixUnusedFd *p;
  UnixUnusedFd *pNext;
  for(p=pInode->pUnused; p; p=pNext){
    pNext = p->pNext;
#if OSCLOSE_CHECK_CLOSE_IOERR
    if( osClose(p->fd) ){
      storeLastErrno(pFile, errno);
      rc = SQLITE_IOERR_CLOSE;
      p->pNext = pError;
      pError = p;
    }else{
      sqlite3_free(p);
    }
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
  ** prior to accessing the inode number.  The one byte written is
  ** an ASCII 'S' character which also happens to be the first byte
  ** in the header of every SQLite database.  In this way, if there
  ** is a race condition such that another thread has already populated
  ** the first page of the database, no damage is done.
  */
  if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
    do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
    if( rc!=1 ){
      storeLastErrno(pFile, errno);
      return SQLITE_IOERR;
    }
    rc = osFstat(fd, &statbuf);
    if( rc!=0 ){
      storeLastErrno(pFile, errno);







|







1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
  ** prior to accessing the inode number.  The one byte written is
  ** an ASCII 'S' character which also happens to be the first byte
  ** in the header of every SQLite database.  In this way, if there
  ** is a race condition such that another thread has already populated
  ** the first page of the database, no damage is done.
  */
  if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
    do{ rc = (int)osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
    if( rc!=1 ){
      storeLastErrno(pFile, errno);
      return SQLITE_IOERR;
    }
    rc = osFstat(fd, &statbuf);
    if( rc!=0 ){
      storeLastErrno(pFile, errno);
1742
1743
1744
1745
1746
1747
1748
















1749




1750
1751
1752
1753
1754
1755
1756
1757
1758



























1759
1760
1761
1762

1763
1764
1765
1766
1767
1768
1769
    if( pInode->bProcessLock==0 ){
      struct flock lock;
      assert( pInode->nLock==0 );
      lock.l_whence = SEEK_SET;
      lock.l_start = SHARED_FIRST;
      lock.l_len = SHARED_SIZE;
      lock.l_type = F_WRLCK;
















      rc = osFcntl(pFile->h, F_SETLK, &lock);




      if( rc<0 ) return rc;
      pInode->bProcessLock = 1;
      pInode->nLock++;
    }else{
      rc = 0;
    }
  }else{
    int i = 0;                      
    do {



























      rc = osFcntl(pFile->h, F_SETLK, pLock);
      if( rc && nRetry ){
         usleep(100 * (++i));
      }

    }while( !rc && nRetry-- );
  }
  return rc;
}

/*
** Lock the file with the lock specified by parameter eFileLock - one







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>









>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




>







2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
    if( pInode->bProcessLock==0 ){
      struct flock lock;
      assert( pInode->nLock==0 );
      lock.l_whence = SEEK_SET;
      lock.l_start = SHARED_FIRST;
      lock.l_len = SHARED_SIZE;
      lock.l_type = F_WRLCK;
#ifdef SQLITE_USE_FLOCKTIMEOUT
      if( pFile->nextFlockTimeout>0 ){
        struct timespec ts;
        struct flocktimeout fltimeout;
        
        ts.tv_sec = pFile->nextFlockTimeout / 1000;
        ts.tv_nsec = (((long)pFile->nextFlockTimeout) % 1000L) * 1000000L;
        fltimeout.fl = lock;
        fltimeout.timeout = ts;
        
        rc = osFcntl(pFile->h, F_SETLKWTIMEOUT, &fltimeout);
        if( !rc ){
          /* reset the timeout on success */
          pFile->nextFlockTimeout = 0;
        }
      }else{
        rc = osFcntl(pFile->h, F_SETLK, &lock);
      }
#else
      rc = osFcntl(pFile->h, F_SETLK, &lock);
#endif
      if( rc<0 ) return rc;
      pInode->bProcessLock = 1;
      pInode->nLock++;
    }else{
      rc = 0;
    }
  }else{
    int i = 0;                      
    do {
#ifdef SQLITE_USE_FLOCKTIMEOUT
      if( (i>0)||(pFile->nextFlockTimeout>0) ){
        struct timespec ts;
        struct flocktimeout fltimeout;
        
        if( i==0 ){
          ts.tv_sec = pFile->nextFlockTimeout / 1000;
          ts.tv_nsec = (((long)pFile->nextFlockTimeout) % 1000L) * 1000000L;
        }else{
          long usTimeout = (i*100);
          assert(i<10000);
          ts.tv_sec = 0;
          ts.tv_nsec = usTimeout * 1000;
        }
        fltimeout.fl = *pLock;
        fltimeout.timeout = ts;
        
        rc = osFcntl(pFile->h, F_SETLKWTIMEOUT, &fltimeout);
        if( !rc ){
          /* reset the timeout on success */
          pFile->nextFlockTimeout = 0;
        }
      }else{
        rc = osFcntl(pFile->h, F_SETLK, pLock);
      }
      i++;
#else
      rc = osFcntl(pFile->h, F_SETLK, pLock);
      if( rc && nRetry ){
         usleep(100 * (++i));
      }
#endif
    }while( !rc && nRetry-- );
  }
  return rc;
}

/*
** Lock the file with the lock specified by parameter eFileLock - one
1866
1867
1868
1869
1870
1871
1872





1873
1874
1875
1876
1877
1878
1879
  /* If some thread using this PID has a lock via a different unixFile*
  ** handle that precludes the requested lock, return BUSY.
  */
  if( (pFile->eFileLock!=pInode->eFileLock && 
          (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
  ){
    rc = SQLITE_BUSY;





    goto end_lock;
  }

  /* If a SHARED lock is requested, and some thread using this PID already
  ** has a SHARED or RESERVED lock, then increment reference counts and
  ** return SQLITE_OK.
  */







>
>
>
>
>







2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
  /* If some thread using this PID has a lock via a different unixFile*
  ** handle that precludes the requested lock, return BUSY.
  */
  if( (pFile->eFileLock!=pInode->eFileLock && 
          (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
  ){
    rc = SQLITE_BUSY;
#ifdef SQLITE_USE_FLOCKTIMEOUT
    if( pFile->nextFlockTimeout>0 ){
      usleep(((int)pFile->nextFlockTimeout) * 1000L);
    }
#endif
    goto end_lock;
  }

  /* If a SHARED lock is requested, and some thread using this PID already
  ** has a SHARED or RESERVED lock, then increment reference counts and
  ** return SQLITE_OK.
  */
1951
1952
1953
1954
1955
1956
1957





1958
1959
1960
1961
1962
1963
1964
      pInode->nLock++;
      pInode->nShared = 1;
    }
  }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
    /* We are trying for an exclusive lock but another thread in this
    ** same process is still holding a shared lock. */
    rc = SQLITE_BUSY;





  }else{
    /* The request was for a RESERVED or EXCLUSIVE lock.  It is
    ** assumed that there is a SHARED or greater lock on the file
    ** already.
    */
    assert( 0!=pFile->eFileLock );
    lock.l_type = F_WRLCK;







>
>
>
>
>







2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
      pInode->nLock++;
      pInode->nShared = 1;
    }
  }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
    /* We are trying for an exclusive lock but another thread in this
    ** same process is still holding a shared lock. */
    rc = SQLITE_BUSY;
#ifdef SQLITE_USE_FLOCKTIMEOUT
    if( pFile->nextFlockTimeout>0 ){
      usleep(((int)pFile->nextFlockTimeout) * 1000L);
    }
#endif
  }else{
    /* The request was for a RESERVED or EXCLUSIVE lock.  It is
    ** assumed that there is a SHARED or greater lock on the file
    ** already.
    */
    assert( 0!=pFile->eFileLock );
    lock.l_type = F_WRLCK;
2264
2265
2266
2267
2268
2269
2270











2271
2272
2273
2274

2275
2276
2277
2278
2279
2280
2281
** vxworksReleaseFileId() routine.
*/
static int closeUnixFile(sqlite3_file *id){
  unixFile *pFile = (unixFile*)id;
#if SQLITE_MAX_MMAP_SIZE>0
  unixUnmapfile(pFile);
#endif











  if( pFile->h>=0 ){
    robust_close(pFile, pFile->h, __LINE__);
    pFile->h = -1;
  }

#if OS_VXWORKS
  if( pFile->pId ){
    if( pFile->ctrlFlags & UNIXFILE_DELETE ){
      osUnlink(pFile->pId->zCanonicalName);
    }
    vxworksReleaseFileId(pFile->pId);
    pFile->pId = 0;







>
>
>
>
>
>
>
>
>
>
>




>







2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
** vxworksReleaseFileId() routine.
*/
static int closeUnixFile(sqlite3_file *id){
  unixFile *pFile = (unixFile*)id;
#if SQLITE_MAX_MMAP_SIZE>0
  unixUnmapfile(pFile);
#endif
#if OSCLOSE_CHECK_CLOSE_IOERR
  if( pFile->h>=0 ){
    int err = osClose(pFile->h);
    if( err ){
      storeLastErrno(pFile, errno);
      return SQLITE_IOERR_CLOSE;
    }else{
      pFile->h=-1;
    }
  }
#else
  if( pFile->h>=0 ){
    robust_close(pFile, pFile->h, __LINE__);
    pFile->h = -1;
  }
#endif
#if OS_VXWORKS
  if( pFile->pId ){
    if( pFile->ctrlFlags & UNIXFILE_DELETE ){
      osUnlink(pFile->pId->zCanonicalName);
    }
    vxworksReleaseFileId(pFile->pId);
    pFile->pId = 0;
2491
2492
2493
2494
2495
2496
2497





2498
2499
2500
2501
2502
2503
2504
  /* grab an exclusive lock */
  rc = osMkdir(zLockFile, 0777);
  if( rc<0 ){
    /* failed to open/create the lock directory */
    int tErrno = errno;
    if( EEXIST == tErrno ){
      rc = SQLITE_BUSY;





    } else {
      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
      if( IS_LOCK_ERROR(rc) ){
        storeLastErrno(pFile, tErrno);
      }
    }
    return rc;







>
>
>
>
>







2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
  /* grab an exclusive lock */
  rc = osMkdir(zLockFile, 0777);
  if( rc<0 ){
    /* failed to open/create the lock directory */
    int tErrno = errno;
    if( EEXIST == tErrno ){
      rc = SQLITE_BUSY;
#ifdef SQLITE_USE_FLOCKTIMEOUT
      if( pFile->nextFlockTimeout>0 ){
        usleep(((int)pFile->nextFlockTimeout) * 1000L);
      }
#endif
    } else {
      rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
      if( IS_LOCK_ERROR(rc) ){
        storeLastErrno(pFile, tErrno);
      }
    }
    return rc;
2730
2731
2732
2733
2734
2735
2736





2737
2738
2739
2740
2741
2742
2743
    pFile->eFileLock = eFileLock;
  }
  OSTRACE(("LOCK    %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), 
           rc==SQLITE_OK ? "ok" : "failed"));
#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
  if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
    rc = SQLITE_BUSY;





  }
#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
  return rc;
}


/*







>
>
>
>
>







3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
    pFile->eFileLock = eFileLock;
  }
  OSTRACE(("LOCK    %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), 
           rc==SQLITE_OK ? "ok" : "failed"));
#ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
  if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
    rc = SQLITE_BUSY;
#ifdef SQLITE_USE_FLOCKTIMEOUT
    if( pFile->nextFlockTimeout>0 ){
      usleep(((long)pFile->nextFlockTimeout) * 1000L);
    }
#endif
  }
#endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
  return rc;
}


/*
2894
2895
2896
2897
2898
2899
2900





2901
2902
2903
2904
2905
2906
2907
    rc = SQLITE_OK;
    goto sem_end_lock;
  }
  
  /* lock semaphore now but bail out when already locked. */
  if( sem_trywait(pSem)==-1 ){
    rc = SQLITE_BUSY;





    goto sem_end_lock;
  }

  /* got it, set the type and return ok */
  pFile->eFileLock = eFileLock;

 sem_end_lock:







>
>
>
>
>







3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
    rc = SQLITE_OK;
    goto sem_end_lock;
  }
  
  /* lock semaphore now but bail out when already locked. */
  if( sem_trywait(pSem)==-1 ){
    rc = SQLITE_BUSY;
#ifdef SQLITE_USE_FLOCKTIMEOUT
    if( pFile->nextFlockTimeout>0 ){
      usleep(((long)pFile->nextFlockTimeout) * 1000L);
    }
#endif
    goto sem_end_lock;
  }

  /* got it, set the type and return ok */
  pFile->eFileLock = eFileLock;

 sem_end_lock:
3041
3042
3043
3044
3045
3046
3047





3048
3049
3050
3051
3052
3053
3054
#else
    rc = sqliteErrorFromPosixError(tErrno,
                    setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
    if( IS_LOCK_ERROR(rc) ){
      storeLastErrno(pFile, tErrno);
    }





    return rc;
  } else {
    return SQLITE_OK;
  }
}

/*







>
>
>
>
>







3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
#else
    rc = sqliteErrorFromPosixError(tErrno,
                    setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
#endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
    if( IS_LOCK_ERROR(rc) ){
      storeLastErrno(pFile, tErrno);
    }
#ifdef SQLITE_USE_FLOCKTIMEOUT
    if( (rc==SQLITE_BUSY)&&(pFile->nextFlockTimeout>0) ){
      usleep(((int)pFile->nextFlockTimeout) * 1000L);
    }
#endif
    return rc;
  } else {
    return SQLITE_OK;
  }
}

/*
3165
3166
3167
3168
3169
3170
3171





3172
3173
3174
3175
3176
3177
3178
  /* If some thread using this PID has a lock via a different unixFile*
  ** handle that precludes the requested lock, return BUSY.
  */
  if( (pFile->eFileLock!=pInode->eFileLock && 
       (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
     ){
    rc = SQLITE_BUSY;





    goto afp_end_lock;
  }
  
  /* If a SHARED lock is requested, and some thread using this PID already
  ** has a SHARED or RESERVED lock, then increment reference counts and
  ** return SQLITE_OK.
  */







>
>
>
>
>







3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
  /* If some thread using this PID has a lock via a different unixFile*
  ** handle that precludes the requested lock, return BUSY.
  */
  if( (pFile->eFileLock!=pInode->eFileLock && 
       (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
     ){
    rc = SQLITE_BUSY;
#ifdef SQLITE_USE_FLOCKTIMEOUT
    if( pFile->nextFlockTimeout>0 ){
      usleep(((int)pFile->nextFlockTimeout) * 1000L);
    }
#endif
    goto afp_end_lock;
  }
  
  /* If a SHARED lock is requested, and some thread using this PID already
  ** has a SHARED or RESERVED lock, then increment reference counts and
  ** return SQLITE_OK.
  */
3239
3240
3241
3242
3243
3244
3245





3246
3247
3248
3249
3250
3251
3252
      pInode->nLock++;
      pInode->nShared = 1;
    }
  }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
    /* We are trying for an exclusive lock but another thread in this
     ** same process is still holding a shared lock. */
    rc = SQLITE_BUSY;





  }else{
    /* The request was for a RESERVED or EXCLUSIVE lock.  It is
    ** assumed that there is a SHARED or greater lock on the file
    ** already.
    */
    int failed = 0;
    assert( 0!=pFile->eFileLock );







>
>
>
>
>







3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
      pInode->nLock++;
      pInode->nShared = 1;
    }
  }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
    /* We are trying for an exclusive lock but another thread in this
     ** same process is still holding a shared lock. */
    rc = SQLITE_BUSY;
#ifdef SQLITE_USE_FLOCKTIMEOUT
    if( pFile->nextFlockTimeout>0 ){
      usleep(((int)pFile->nextFlockTimeout) * 1000L);
    }
#endif
  }else{
    /* The request was for a RESERVED or EXCLUSIVE lock.  It is
    ** assumed that there is a SHARED or greater lock on the file
    ** already.
    */
    int failed = 0;
    assert( 0!=pFile->eFileLock );
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
    pFile->inNormalWrite = 0;
#endif
    
    if( pFile->eFileLock==EXCLUSIVE_LOCK ){
      rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
      if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
        /* only re-establish the shared lock if necessary */
        int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
        rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
      } else {
        skipShared = 1;
      }
    }
    if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
      rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);







|







3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
    pFile->inNormalWrite = 0;
#endif
    
    if( pFile->eFileLock==EXCLUSIVE_LOCK ){
      rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
      if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
        /* only re-establish the shared lock if necessary */
        int sharedLockByte = (int) (SHARED_FIRST+pInode->sharedByte);
        rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
      } else {
        skipShared = 1;
      }
    }
    if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
      rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
3493
3494
3495
3496
3497
3498
3499







3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
*/
static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
  int got;
  int prior = 0;
#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
  i64 newOffset;
#endif







  TIMER_START;
  assert( cnt==(cnt&0x1ffff) );
  assert( id->h>2 );
  cnt &= 0x1ffff;
  do{
#if defined(USE_PREAD)
    got = osPread(id->h, pBuf, cnt, offset);
    SimulateIOError( got = -1 );
#elif defined(USE_PREAD64)
    got = osPread64(id->h, pBuf, cnt, offset);
    SimulateIOError( got = -1 );
#else
    newOffset = lseek(id->h, offset, SEEK_SET);
    SimulateIOError( newOffset-- );







>
>
>
>
>
>
>






|







3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
*/
static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
  int got;
  int prior = 0;
#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
  i64 newOffset;
#endif

#if defined(__APPLE__)
  /* <rdar://problem/17742712> */
  if( id->nFsBytes > 0 && offset + cnt > id->nFsBytes ){
    __builtin_trap();
  }
#endif
  TIMER_START;
  assert( cnt==(cnt&0x1ffff) );
  assert( id->h>2 );
  cnt &= 0x1ffff;
  do{
#if defined(USE_PREAD)
    got = (int)osPread(id->h, pBuf, cnt, offset);
    SimulateIOError( got = -1 );
#elif defined(USE_PREAD64)
    got = osPread64(id->h, pBuf, cnt, offset);
    SimulateIOError( got = -1 );
#else
    newOffset = lseek(id->h, offset, SEEK_SET);
    SimulateIOError( newOffset-- );
3607
3608
3609
3610
3611
3612
3613



3614
3615
3616
3617
3618
3619
3620
  int fd,                         /* File descriptor to write to */
  i64 iOff,                       /* File offset to begin writing at */
  const void *pBuf,               /* Copy data from this buffer to the file */
  int nBuf,                       /* Size of buffer pBuf in bytes */
  int *piErrno                    /* OUT: Error number if error occurs */
){
  int rc = 0;                     /* Value returned by system call */




  assert( nBuf==(nBuf&0x1ffff) );
  assert( fd>2 );
  nBuf &= 0x1ffff;
  TIMER_START;

#if defined(USE_PREAD)







>
>
>







4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
  int fd,                         /* File descriptor to write to */
  i64 iOff,                       /* File offset to begin writing at */
  const void *pBuf,               /* Copy data from this buffer to the file */
  int nBuf,                       /* Size of buffer pBuf in bytes */
  int *piErrno                    /* OUT: Error number if error occurs */
){
  int rc = 0;                     /* Value returned by system call */
#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
  i64 newOffset;
#endif

  assert( nBuf==(nBuf&0x1ffff) );
  assert( fd>2 );
  nBuf &= 0x1ffff;
  TIMER_START;

#if defined(USE_PREAD)
3646
3647
3648
3649
3650
3651
3652






3653
3654
3655
3656
3657
3658
3659
** Seek to the offset in id->offset then read cnt bytes into pBuf.
** Return the number of bytes actually read.  Update the offset.
**
** To avoid stomping the errno value on a failed write the lastErrno value
** is set before returning.
*/
static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){






  return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
}


/*
** Write data from a buffer into a file.  Return SQLITE_OK on success
** or some other error code on failure.







>
>
>
>
>
>







4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
** Seek to the offset in id->offset then read cnt bytes into pBuf.
** Return the number of bytes actually read.  Update the offset.
**
** To avoid stomping the errno value on a failed write the lastErrno value
** is set before returning.
*/
static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
#if defined(__APPLE__)
  /* <rdar://problem/17742712> */
  if( id->nFsBytes > 0 && offset + cnt > id->nFsBytes ){
    __builtin_trap();
  }
#endif
  return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
}


/*
** Write data from a buffer into a file.  Return SQLITE_OK on success
** or some other error code on failure.
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
  int fd = -1;
  char zDirname[MAX_PATHNAME+1];

  sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
  for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
  if( ii>0 ){
    zDirname[ii] = '\0';
    fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
    if( fd>=0 ){
      OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
    }
  }
  *pFd = fd;
  return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
}







|







4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
  int fd = -1;
  char zDirname[MAX_PATHNAME+1];

  sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
  for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
  if( ii>0 ){
    zDirname[ii] = '\0';
    fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0, 0);
    if( fd>=0 ){
      OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
    }
  }
  *pFd = fd;
  return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
}
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
    int dirfd;
    OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
            HAVE_FULLFSYNC, isFullsync));
    rc = osOpenDirectory(pFile->zPath, &dirfd);
    if( rc==SQLITE_OK && dirfd>=0 ){
      full_fsync(dirfd, 0, 0);
#if OSCLOSE_CHECK_CLOSE_IOERR
      if( close(pFile->dirfd) ){
        storeLastErrno(pFile, errno);
        rc = SQLITE_IOERR_DIR_CLOSE;
      }
#else
      robust_close(pFile, dirfd, __LINE__);
#endif
    }else if( rc==SQLITE_CANTOPEN ){







|







4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
    int dirfd;
    OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
            HAVE_FULLFSYNC, isFullsync));
    rc = osOpenDirectory(pFile->zPath, &dirfd);
    if( rc==SQLITE_OK && dirfd>=0 ){
      full_fsync(dirfd, 0, 0);
#if OSCLOSE_CHECK_CLOSE_IOERR
      if( osClose(pFile->dirfd) ){
        storeLastErrno(pFile, errno);
        rc = SQLITE_IOERR_DIR_CLOSE;
      }
#else
      robust_close(pFile, dirfd, __LINE__);
#endif
    }else if( rc==SQLITE_CANTOPEN ){
4077
4078
4079
4080
4081
4082
4083
4084



4085
4086
4087
4088
4089
4090
4091
** nBytes or larger, this routine is a no-op.
*/
static int fcntlSizeHint(unixFile *pFile, i64 nByte){
  if( pFile->szChunk>0 ){
    i64 nSize;                    /* Required file size */
    struct stat buf;              /* Used to hold return values of fstat() */
   
    if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;




    nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
    if( nSize>(i64)buf.st_size ){

#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
      /* The code below is handling the return value of osFallocate() 
      ** correctly. posix_fallocate() is defined to "returns zero on success, 







|
>
>
>







4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
** nBytes or larger, this routine is a no-op.
*/
static int fcntlSizeHint(unixFile *pFile, i64 nByte){
  if( pFile->szChunk>0 ){
    i64 nSize;                    /* Required file size */
    struct stat buf;              /* Used to hold return values of fstat() */
   
    if( osFstat(pFile->h, &buf) ) {
      storeLastErrno(pFile, errno);
      return SQLITE_IOERR_FSTAT;
    }

    nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
    if( nSize>(i64)buf.st_size ){

#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
      /* The code below is handling the return value of osFallocate() 
      ** correctly. posix_fallocate() is defined to "returns zero on success, 
4133
4134
4135
4136
4137
4138
4139








































4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152

4153
4154
4155
4156
4157
4158
4159
    return rc;
  }
#endif

  return SQLITE_OK;
}










































#if (SQLITE_ENABLE_APPLE_SPI>0) && defined(__APPLE__)
#include "sqlite3_private.h"
#include <copyfile.h>
static int getDbPathForUnixFile(unixFile *pFile, char *dbPath);
#endif

#if SQLITE_ENABLE_LOCKING_STYLE
static int isProxyLockingMode(unixFile *);
#endif

#if (SQLITE_ENABLE_APPLE_SPI>0) && defined(__APPLE__)
static int unixTruncateDatabase(unixFile *, int);


static int unixInvalidateSupportFiles(unixFile *, int);

static int findCreateFileMode(const char *, int, mode_t*, uid_t *,gid_t *);

/* opens a read/write connection to a file zName inheriting the appropriate
** user/perms from the database file if running as root.  Returns the file 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>













>







4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
    return rc;
  }
#endif

  return SQLITE_OK;
}

#if SQLITE_ENABLE_DATA_PROTECTION
static int unixSetFileProtection(int fd, int protFlags, int isShm){
  int protClass;
  int currClass;
  
  if( 0==protFlags ){
    return SQLITE_OK;
  }
  assert(fd>=0);
  assert((protFlags&SQLITE_OPEN_FILEPROTECTION_MASK)==protFlags);
  
  protClass = (!isShm) ? unixProtectionClassForVFSProtectionFlags(protFlags) : shmUnixProtectionClassForVFSProtectionFlags(protFlags);
  
# if defined(__APPLE__)&&(TARGET_OS_EMBEDDED!=0)&&\
(!defined(TARGET_IPHONE_SIMULATOR)||(TARGET_IPHONE_SIMULATOR==0))
  
  currClass = fcntl(fd, F_GETPROTECTIONCLASS);
  if( currClass<1 && protClass==4 ){
    /* F_GETPROTECTIONCLASS will fail if the file system doesn't have content protection enabled, 
     * if the protClass is 4/NONE that's OK */
    return SQLITE_OK;
  }
  if( protClass!=currClass ){
    if (0 != fcntl(fd, F_SETPROTECTIONCLASS, protClass)) {
      if( errno == EPERM ){
        /* <rdar://problem/9385939> Test device locked state when error reporting for content protection issues */        
        return SQLITE_AUTH;
      }
      return SQLITE_IOERR;
    }

    rc = unixMapfile(pFile, nByte);
    return rc;
  }
# else
# warning SQLITE_ENABLE_DATA_PROTECTION is defined but could not be enabled for this deployment target
# endif
  return SQLITE_OK;
}
#endif /* SQLITE_ENABLE_DATA_PROTECTION */

#if (SQLITE_ENABLE_APPLE_SPI>0) && defined(__APPLE__)
#include "sqlite3_private.h"
#include <copyfile.h>
static int getDbPathForUnixFile(unixFile *pFile, char *dbPath);
#endif

#if SQLITE_ENABLE_LOCKING_STYLE
static int isProxyLockingMode(unixFile *);
#endif

#if (SQLITE_ENABLE_APPLE_SPI>0) && defined(__APPLE__)
static int unixTruncateDatabase(unixFile *, int);
static const char *unixTempFileDir(void);

static int unixInvalidateSupportFiles(unixFile *, int);

static int findCreateFileMode(const char *, int, mode_t*, uid_t *,gid_t *);

/* opens a read/write connection to a file zName inheriting the appropriate
** user/perms from the database file if running as root.  Returns the file 
4167
4168
4169
4170
4171
4172
4173

4174







4175
4176
4177
4178
4179


4180
4181
4182





4183




4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195





4196
4197
4198
4199
4200
4201
4202
4203


























































































































4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220











4221
4222
4223
4224
4225




















4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244


4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279

4280
4281










4282
4283
4284



























4285
4286
4287
4288
4289
4290
4291
4292
4293





































































































































































4294
4295
4296
4297
4298
4299
4300
4301
4302
4303

4304

4305
4306
4307
4308
4309
4310
4311



4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328

4329

4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373

4374
4375
4376





4377












4378



4379
4380
4381
4382
4383
4384
4385
4386


























4387
4388
4389
4390
4391
4392
4393
  int *pFd
){
  int fd = -1;
  mode_t openMode;              /* Permissions to create file with */
  uid_t uid;                    /* Userid for the file */
  gid_t gid;                    /* Groupid for the file */
  int rc;

  







  assert(pFd!=NULL);
  rc = findCreateFileMode(zName, dbOpenFlags, &openMode, &uid, &gid);
  if( rc!=SQLITE_OK ){
    return rc;
  }


  fd = robust_open(zName, openFlags, openMode);
  OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
  if( fd<0 ){





    rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);




    return rc;
  }
  /* if we're opening the wal or journal and running as root, set
  ** the journal uid/gid */
  if( dbOpenFlags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
    uid_t euid = geteuid();
    if( euid==0 && (euid!=uid || getegid()!=gid) ){
      if( fchown(fd, uid, gid) ){
        rc = SQLITE_CANTOPEN_BKPT;
      }
    }
  }





  if( rc==SQLITE_OK ){
    *pFd = fd;
  } else {
    *pFd = -1;
    close(fd);
  }
  return rc;
}



























































































































static int unixReplaceDatabase(unixFile *pFile, sqlite3 *srcdb) {
  sqlite3_file *id = (sqlite3_file *)pFile;
  Btree *pSrcBtree = NULL;
  sqlite3_file *src_file = NULL;
  unixFile *pSrcFile = NULL;
  char srcWalPath[MAXPATHLEN+5];
  int srcWalFD = -1;
  int rc = SQLITE_OK;
  void *pLock = NULL;
  int flags = 0;
  sqlite3 *srcdb2 = NULL;
  copyfile_state_t s;
  int corruptSrcFileLock = 0;
  int corruptDstFileLock = 0;
  int isSrcCorrupt = 0;
  int isDstCorrupt = 0;











  
  if( !sqlite3SafetyCheckOk(srcdb) ){
    return SQLITE_MISUSE;
  }
    




















#if SQLITE_ENABLE_DATA_PROTECTION
  flags |= pFile->protFlags;
#endif
#if SQLITE_ENABLE_LOCKING_STYLE
  if( isProxyLockingMode(pFile) ){
    flags |= SQLITE_OPEN_AUTOPROXY;
  }
#endif
  
  rc = sqlite3demo_superlock(pFile->zPath, 0, flags, 0, 0, &pLock);
  if( rc ){
    if( rc==SQLITE_CORRUPT || rc==SQLITE_NOTADB ){
      isDstCorrupt = 1;
      rc = sqlite3demo_superlock_corrupt(id, SQLITE_LOCK_EXCLUSIVE,
                                         &corruptDstFileLock);
    }
    if( rc ){
      return rc;
    }


  }
  /* get the src file descriptor adhering to the db struct access rules 
   ** this code is modeled after sqlite3_file_control() in main.c
   */ 
  sqlite3_mutex_enter(srcdb->mutex);
  if( srcdb->nDb>0 ){
    pSrcBtree = srcdb->aDb[0].pBt;
  }
  if( pSrcBtree ){
    Pager *pSrcPager;
    sqlite3BtreeEnter(pSrcBtree);
    pSrcPager = sqlite3BtreePager(pSrcBtree);
    assert( pSrcPager!=0 );
    src_file = sqlite3PagerFile(pSrcPager);
    assert( src_file!=0 );
    if( src_file->pMethods ){
      int srcFlags = 0;
      pSrcFile = (unixFile *)src_file;
#if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
      if ((pSrcFile->openFlags & O_RDWR) == O_RDWR) {
        srcFlags = SQLITE_OPEN_READWRITE;
      } else {
        srcFlags = SQLITE_OPEN_READONLY;
      }
#else
      srcFlags = SQLITE_OPEN_READWRITE;
#endif
#if SQLITE_ENABLE_DATA_PROTECTION
      srcFlags |= pSrcFile->protFlags;
#endif
#if SQLITE_ENABLE_LOCKING_STYLE
      if( isProxyLockingMode(pSrcFile) ){
        srcFlags |= SQLITE_OPEN_AUTOPROXY;
      }
#endif

      rc = sqlite3_open_v2(pSrcFile->zPath, &srcdb2, srcFlags, 0);
      if( rc==SQLITE_OK ){










        /* start a deferred transaction and read to establish a read lock */
        rc = sqlite3_exec(srcdb2, "BEGIN DEFERRED; PRAGMA schema_version",
                          0, 0, 0);



























        if( rc==SQLITE_CORRUPT || rc==SQLITE_NOTADB ){
          isSrcCorrupt = 1;
          rc = sqlite3demo_superlock_corrupt(src_file, SQLITE_LOCK_SHARED,
                                             &corruptSrcFileLock);
        }
      }
    }
  }
  if( !srcdb2 || pSrcFile==NULL || pSrcFile->h<0){





































































































































































    rc = SQLITE_INTERNAL;
  }
  if( rc!=SQLITE_OK ){
    goto end_replace_database;
  }
  /* both databases are locked appropriately, copy the src wal journal if 
   ** one exists and then the actual database file
   */
  strlcpy(srcWalPath, pSrcFile->zPath, MAXPATHLEN+5);
  strlcat(srcWalPath, "-wal", MAXPATHLEN+5);

  srcWalFD = open(srcWalPath, O_RDONLY);

  if( !(srcWalFD<0) ){
    char dstWalPath[MAXPATHLEN+5];
    int dstWalFD = -1;
    int protFlags = 0;
    strlcpy(dstWalPath, pFile->zPath, MAXPATHLEN+5);
    strlcat(dstWalPath, "-wal", MAXPATHLEN+5);




    rc = unixOpenChildFile(dstWalPath, O_RDWR|O_CREAT, SQLITE_OPEN_WAL,
                           protFlags, &dstWalFD);
    if( rc==SQLITE_OK ){
      s = copyfile_state_alloc();
      lseek(srcWalFD, 0, SEEK_SET);
      lseek(dstWalFD, 0, SEEK_SET);
      if( fcopyfile(srcWalFD, dstWalFD, s, COPYFILE_DATA) ){
        int err=errno;
        switch(err) {
          case ENOMEM:
            rc = SQLITE_NOMEM;
            break;
          default:
            storeLastErrno(pFile, err);
            rc = SQLITE_IOERR;
        }
      }

      copyfile_state_free(s);

      close(dstWalFD);
    }
    close(srcWalFD);
  }
  if( rc==SQLITE_OK ){
    /* before we copy, ensure that the file change counter will be modified */
    uint32_t srcChange = 0;
    uint32_t dstChange = 0;
    pread(pSrcFile->h, &srcChange, 4, 24);
    pread(pFile->h, &dstChange, 4, 24);
    
    /* copy the actual database */
    s = copyfile_state_alloc();
    lseek(pSrcFile->h, 0, SEEK_SET);
    lseek(pFile->h, 0, SEEK_SET);
    if( fcopyfile(pSrcFile->h, pFile->h, s, COPYFILE_DATA) ){
      int err=errno;
      switch(err) {
        case ENOMEM:
          rc = SQLITE_NOMEM;
          break;
        default:
          storeLastErrno(pFile, err);
          rc = SQLITE_IOERR;
      }
    }
    copyfile_state_free(s);
    
    if (srcChange == dstChange) {
      /* modify the change counter to force page zero to be reloaded */
      dstChange ++;
      pwrite(pFile->h, &dstChange, 4, 24);
    }
  }
  if( isSrcCorrupt ){
    sqlite3demo_superunlock_corrupt(src_file, corruptSrcFileLock);
  }else{
    /* done with the source db so end the transaction */
    sqlite3_exec(srcdb2, "COMMIT", 0, 0, 0);
  }
  /* zero out any old journal clutter */
  if( rc==SQLITE_OK ){
    int skipWAL = (srcWalFD<0)?0:1;
    unixInvalidateSupportFiles(pFile, skipWAL);

  }
  
end_replace_database:





  if( pSrcBtree ){












    sqlite3_close(srcdb2);



    sqlite3BtreeLeave(pSrcBtree);
  }
  sqlite3_mutex_leave(srcdb->mutex);
  if( isDstCorrupt ){
    sqlite3demo_superunlock_corrupt(id, corruptDstFileLock);
  }else{
    sqlite3demo_superunlock(pLock);
  }


























  return rc;
}
#define SQLITE_FILE_HEADER_LEN 16
/* Check for a conflicting lock.  If one is found, print an this
 ** on standard output using the format string given and return 1.
 ** If there are no conflicting locks, return 0.
 */







>

>
>
>
>
>
>
>





>
>
|


>
>
>
>
>
|
>
>
>
>












>
>
>
>
>




|



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>









|







>
>
>
>
>
>
>
>
>
>
>





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>









|









>
>
















<


















>


>
>
>
>
>
>
>
>
>
>

|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>








|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>








|

>
|
>







>
>
>






|










>

>
|

|





|




|

|















|












>



>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>

>
>
>








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891

4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
  int *pFd
){
  int fd = -1;
  mode_t openMode;              /* Permissions to create file with */
  uid_t uid;                    /* Userid for the file */
  gid_t gid;                    /* Groupid for the file */
  int rc;
  int isShm = 0;
  
  int zNameLen = zName ? (int)strlen(zName) : 0;
  if (zNameLen > 4) {
    const char* suffix = &(zName[zNameLen-4]);
    if (strncmp("-shm", suffix, 4) == 0) {
      isShm = 1;
    }
  }
  assert(pFd!=NULL);
  rc = findCreateFileMode(zName, dbOpenFlags, &openMode, &uid, &gid);
  if( rc!=SQLITE_OK ){
    return rc;
  }
  int unixProtFlags = (!isShm) ? unixProtectionClassForVFSProtectionFlags(protFlags) : shmUnixProtectionClassForVFSProtectionFlags(protFlags);
  
  fd = robust_open2(zName, openFlags, openMode, unixProtFlags, isShm);
  OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
  if( fd<0 ){
#if SQLITE_ENABLE_DATA_PROTECTION
    if( errno==EPERM ){
      /* <rdar://problem/9385939> Test device locked state when error reporting for content protection issues */
      rc = unixLogError(SQLITE_AUTH, "open", zName);
    }else{
      rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
    }
#else
    rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
#endif
    return rc;
  }
  /* if we're opening the wal or journal and running as root, set
  ** the journal uid/gid */
  if( dbOpenFlags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
    uid_t euid = geteuid();
    if( euid==0 && (euid!=uid || getegid()!=gid) ){
      if( fchown(fd, uid, gid) ){
        rc = SQLITE_CANTOPEN_BKPT;
      }
    }
  }
#if SQLITE_ENABLE_DATA_PROTECTION
  if( rc==SQLITE_OK ){
    rc = unixSetFileProtection(fd, protFlags, isShm);
  }
#endif
  if( rc==SQLITE_OK ){
    *pFd = fd;
  } else {
    *pFd = -1;
    osClose(fd);
  }
  return rc;
}

static int readWALSettingFromFile(unixFile *pFile) {
  char magicword[20];
  int result = -1;
  sqlite3_file *id = (sqlite3_file *)pFile;
  if (id != NULL) {
    int sqlitecode = id->pMethods->xRead(id, magicword, sizeof(magicword), 0l);
    
    if (SQLITE_OK == sqlitecode) {
      if (0 == memcmp(magicword, "SQLite format 3", 15)) {
        result = 0;
        if ((magicword[18] == 2) && (magicword[19] == 2)) {
          result = 1;
        }
      }
    }
  }
  return result;
}

static int sqlite_guarded_fcopyfile(int src_fd, int dst_fd) {
  size_t blen;
  char *bp = 0;
  ssize_t nread;
  int ret = 0;
  size_t iBlocksize = 0;
  size_t oBlocksize = 0;
  size_t totalCopied = 0;
  const size_t twomeg = (1024 * 1024 * 2);
  struct statfs sfs;
  bzero(&sfs, sizeof(struct statfs));
  struct stat sbuf;
  bzero(&sbuf, sizeof(struct stat));
  int err = -1;
        
  fstat(src_fd, &sbuf);
  
  if (fstatfs(src_fd, &sfs) == -1) {
    iBlocksize = sbuf.st_blksize;
  } else {
    iBlocksize = sfs.f_iosize;
  }
  
  if (iBlocksize > twomeg) {
    iBlocksize = twomeg;
  }
  
  if ((bp = malloc(iBlocksize)) == NULL)
    return -1;
  
  oBlocksize = iBlocksize;
  
  blen = iBlocksize;
  
  /* If supported, do preallocation for Xsan / HFS volumes */
#ifdef F_PREALLOCATE
  {
    fstore_t fst;
    
    fst.fst_flags = 0;
    fst.fst_posmode = F_PEOFPOSMODE;
    fst.fst_offset = 0;
    fst.fst_length = sbuf.st_size;
    /* Ignore errors; this is merely advisory. */
    (void)fcntl(dst_fd, F_PREALLOCATE, &fst);
  }
#endif
  
  while ((nread = osRead(src_fd, bp, blen)) > 0)
  {
    ssize_t nwritten;
    size_t left = nread;
    void *ptr = bp;
    int loop = 0;
    
    while (left > 0) {
      nwritten = osWrite(dst_fd, ptr, MIN(left, oBlocksize));
      switch (nwritten) {
        case 0:
          if (++loop > 5) {
            ret = -1;
            err = EAGAIN;
            goto exit;
          }
          break;
        case -1:
          ret = -1;
          goto exit;
        default:
          left -= nwritten;
          ptr = ((char*)ptr) + nwritten;
          loop = 0;
          break;
      }
      totalCopied += nwritten;
    }
  }
  if (nread < 0)
  {
    ret = -1;
    goto exit;
  }
  
  if (ftruncate(dst_fd, totalCopied) < 0)
  {
    ret = -1;
    goto exit;
  }
  
exit:
  if (ret == -1)
  {
    if (err != -1) {
      errno = err;
    } else {
      err = errno;
    }
  }
  free(bp);
  return (ret != -1) ? 0 : err;
}


static int unixReplaceDatabase(unixFile *pFile, sqlite3 *srcdb) {
  sqlite3_file *id = (sqlite3_file *)pFile;
  Btree *pSrcBtree = NULL;
  sqlite3_file *src_file = NULL;
  unixFile *pSrcFile = NULL;
  char srcWalPath[MAXPATHLEN+5];
  int srcWalFD = -1;
  int rc = SQLITE_OK;
  Superlock *pLock = NULL;
  int flags = 0;
  sqlite3 *srcdb2 = NULL;
  copyfile_state_t s;
  int corruptSrcFileLock = 0;
  int corruptDstFileLock = 0;
  int isSrcCorrupt = 0;
  int isDstCorrupt = 0;
  int shouldCopySrc = 0;
  int isDstWAL = -1;
  int isSrcWAL = -1;
  int extraFDtoClose = -1;
  char* replacementSrcPath = NULL;
  char* replacementSrcWALPath = NULL;
  char* replacementSrcShmPath = NULL;
  int notGuardedFileDescriptors = 0;
  int dstPreExists = 1;
  struct stat checkBuffer;
  int srcFlags = 0;
  
  if( !sqlite3SafetyCheckOk(srcdb) ){
    return SQLITE_MISUSE;
  }
    
  const char* realSrcPath = sqlite3_db_filename(srcdb, NULL);
  
  const char *tDir = unixTempFileDir();
  int tDirLen = tDir ? (int)strlen(tDir) : 0;
  if (tDirLen < 1) {
    _open_error_asl_log();
    asl_log(errorlog_client, NULL, ASL_LEVEL_CRIT, "SQLite: replace database failed because TMPDIR is not set correctly\n");

    return SQLITE_PERM;
  }

  if (0 == stat(pFile->zPath, &checkBuffer)) {
    if (checkBuffer.st_size == 0l) {
      dstPreExists = 0;
    }
  } else {
    if (errno == ENOENT) {
      dstPreExists = 0;
    }
  }
#if SQLITE_ENABLE_DATA_PROTECTION
  flags |= pFile->protFlags;
#endif
#if SQLITE_ENABLE_LOCKING_STYLE
  if( isProxyLockingMode(pFile) ){
    flags |= SQLITE_OPEN_AUTOPROXY;
  }
#endif
  
  rc = sqlite3demo_superlock(pFile->zPath, 0, flags, 0, 0, (void**)&pLock);
  if( rc ){
    if( rc==SQLITE_CORRUPT || rc==SQLITE_NOTADB ){
      isDstCorrupt = 1;
      rc = sqlite3demo_superlock_corrupt(id, SQLITE_LOCK_EXCLUSIVE,
                                         &corruptDstFileLock);
    }
    if( rc ){
      return rc;
    }
  } else {
    isDstWAL = ((pLock->bWal) ? 1 : 0);
  }
  /* get the src file descriptor adhering to the db struct access rules 
   ** this code is modeled after sqlite3_file_control() in main.c
   */ 
  sqlite3_mutex_enter(srcdb->mutex);
  if( srcdb->nDb>0 ){
    pSrcBtree = srcdb->aDb[0].pBt;
  }
  if( pSrcBtree ){
    Pager *pSrcPager;
    sqlite3BtreeEnter(pSrcBtree);
    pSrcPager = sqlite3BtreePager(pSrcBtree);
    assert( pSrcPager!=0 );
    src_file = sqlite3PagerFile(pSrcPager);
    assert( src_file!=0 );
    if( src_file->pMethods ){

      pSrcFile = (unixFile *)src_file;
#if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
      if ((pSrcFile->openFlags & O_RDWR) == O_RDWR) {
        srcFlags = SQLITE_OPEN_READWRITE;
      } else {
        srcFlags = SQLITE_OPEN_READONLY;
      }
#else
      srcFlags = SQLITE_OPEN_READWRITE;
#endif
#if SQLITE_ENABLE_DATA_PROTECTION
      srcFlags |= pSrcFile->protFlags;
#endif
#if SQLITE_ENABLE_LOCKING_STYLE
      if( isProxyLockingMode(pSrcFile) ){
        srcFlags |= SQLITE_OPEN_AUTOPROXY;
      }
#endif
      
      rc = sqlite3_open_v2(pSrcFile->zPath, &srcdb2, srcFlags, 0);
      if( rc==SQLITE_OK ){
        sqlite3_file* tempSrcFile = NULL;
        int anotherError = sqlite3_file_control(srcdb2, NULL, SQLITE_FCNTL_FILE_POINTER, &tempSrcFile);

        if (anotherError == SQLITE_OK && tempSrcFile) {
          isSrcWAL = (1== readWALSettingFromFile((unixFile*)tempSrcFile)) ? 1 : 0;
        } else {
          isSrcWAL = (1== readWALSettingFromFile(pSrcFile)) ? 1 : 0;
        }

        sqlite3_busy_timeout(srcdb2, srcdb->busyTimeout);
        /* start a deferred transaction and read to establish a read lock */
        rc = sqlite3_exec(srcdb2, "BEGIN DEFERRED; PRAGMA schema_version", 0, 0, 0);
        
        if (rc == SQLITE_CANTOPEN) {
          if (isSrcWAL) {
            // try opening writeable and again
            sqlite3_close(srcdb2);
            srcdb2 = NULL;
            rc = sqlite3_open_v2(pSrcFile->zPath, &srcdb2, ((srcFlags & (~SQLITE_OPEN_READONLY))| SQLITE_OPEN_READWRITE), 0);
            if( rc==SQLITE_OK ){
              sqlite3_busy_timeout(srcdb2, srcdb->busyTimeout);
              /* start a deferred transaction and read to establish a read lock */
              rc = sqlite3_exec(srcdb2, "BEGIN DEFERRED; PRAGMA schema_version", 0, 0, 0);
            }
          }
        }
        
        if (isSrcWAL && (rc == SQLITE_CANTOPEN)) {
          // try copying to temp and opening writeable
          if (srcdb2) {
            sqlite3_close(srcdb2);
            srcdb2 = NULL;
          }
          rc = SQLITE_OK;  // well, maybe
          shouldCopySrc = 1;
        }
        if (dstPreExists && (isSrcWAL != isDstWAL) && (!isDstCorrupt)) {
          shouldCopySrc = 1;
        }

        if( rc==SQLITE_CORRUPT || rc==SQLITE_NOTADB ){
          isSrcCorrupt = 1;
          rc = sqlite3demo_superlock_corrupt(src_file, SQLITE_LOCK_SHARED,
                                             &corruptSrcFileLock);
        }
      }
    }
  }
  if( pSrcFile==NULL || pSrcFile->h<0){
    rc = SQLITE_INTERNAL;
  }
  if( rc!=SQLITE_OK ){
    goto end_replace_database;
  }
  
  int srcFD = pSrcFile->h;
  const char* srcPath = pSrcFile->zPath;
  
  if (shouldCopySrc) {
    sqlite3* newSrcDB = NULL;
    int shouldUnlinkWAL = 0;

    /* initialize a new database in TMPDIR and copy the contents over */
    int tLen = sizeof(char) * (tDirLen + 25);
    replacementSrcPath = (char *)calloc(1, tLen);
    
    int tsrcFd = -1;
    
    strlcpy(replacementSrcPath, tDir, tLen);
    /* create a temporary database with the prefix tmpsqlitereplacedb */
    if( replacementSrcPath[(tDirLen-1)] != '/' ){
      strlcat(replacementSrcPath, "/", tLen);
    }
    strlcat(replacementSrcPath, "tmpsqlitereplacedbXXXXXX", tLen);
    tsrcFd = mkstemp(replacementSrcPath);
    if( tsrcFd==-1 ){
      storeLastErrno(pFile, errno);
      rc = SQLITE_IOERR;
    } else{
      extraFDtoClose = tsrcFd;
      
      replacementSrcWALPath = (char*)calloc(1,tLen+5);
      strlcpy(replacementSrcWALPath, replacementSrcPath, tLen+5);
      strlcat(replacementSrcWALPath, "-wal", tLen+5);

      replacementSrcShmPath = (char*)calloc(1,tLen+5);
      strlcpy(replacementSrcShmPath, replacementSrcPath, tLen+5);
      strlcat(replacementSrcShmPath, "-shm", tLen+5);
      
      // copy src wal & db
      s = copyfile_state_alloc();
      lseek(srcFD, 0, SEEK_SET);
      lseek(tsrcFd, 0, SEEK_SET);
      
      if( fcopyfile(srcFD, tsrcFd, s, COPYFILE_DATA)){
        int err=errno;
        switch(err) {
          case ENOMEM:
            rc = SQLITE_NOMEM;
            break;
          default:
            storeLastErrno(pFile, err);
            rc = SQLITE_IOERR;
        }
      }
      copyfile_state_free(s);
      fsync(tsrcFd);
            
      if (rc == SQLITE_OK) {
        int tSrcWALFD = -1;
        strlcpy(srcWalPath, srcPath, MAXPATHLEN+5);
        strlcat(srcWalPath, "-wal", MAXPATHLEN+5);
        tSrcWALFD = robust_open2(srcWalPath, O_RDONLY, 0, 0, 0);
        if (tSrcWALFD >=0) {
          srcWalFD = robust_open2(replacementSrcWALPath, O_RDWR | O_CREAT | O_TRUNC, 0, 0, 0);
          if ((srcWalFD < 0) && (errno != ENOENT)) {
            storeLastErrno(pFile, errno);
            rc = SQLITE_IOERR;
            osClose(tSrcWALFD);
            tSrcWALFD = -1;
          }
        } else {
          if (errno != ENOENT) {
            storeLastErrno(pFile, errno);
            rc = SQLITE_IOERR;
          }
        }
        
        if (rc == SQLITE_OK) {
          if (srcWalFD >= 0) {
            s = copyfile_state_alloc();
            lseek(srcWalFD, 0, SEEK_SET);
            lseek(tSrcWALFD, 0, SEEK_SET);
            if( sqlite_guarded_fcopyfile(tSrcWALFD, srcWalFD) != 0){
              int err=errno;
              switch(err) {
                case ENOMEM:
                  rc = SQLITE_NOMEM;
                  break;
                default:
                  storeLastErrno(pFile, err);
                  rc = SQLITE_IOERR;
              }
            }
            copyfile_state_free(s);
            fsync(srcWalFD);
          }
          if (tSrcWALFD >= 0) {
            osClose(tSrcWALFD);
          }
          
          // open src as db
          rc = sqlite3_open_v2(replacementSrcPath, &newSrcDB, ((srcFlags & (~SQLITE_OPEN_READONLY))| SQLITE_OPEN_READWRITE), 0);
          if (rc != SQLITE_OK) {
          } else {
            sqlite3_busy_timeout(newSrcDB, srcdb->busyTimeout);
          }


        }
      }

      if (rc != SQLITE_OK) {
        if (newSrcDB) {
          sqlite3_close(newSrcDB);
          newSrcDB = NULL;
        }
      }
      
      if (rc == SQLITE_OK) {
        rc = sqlite3_exec(newSrcDB, "BEGIN DEFERRED; PRAGMA schema_version; COMMIT;", 0, 0, 0);
 
        if (isSrcWAL != isDstWAL) {
          if (isDstWAL) {
            rc = sqlite3_exec(newSrcDB, "PRAGMA journal_mode=WAL", 0, 0, 0);
          } else {
            rc = sqlite3_exec(newSrcDB, "PRAGMA journal_mode=DELETE", 0, 0, 0);
            shouldUnlinkWAL = 1;
          }
        }

        if (srcdb2 == NULL) {
          srcdb2 = newSrcDB;
          sqlite3_exec(newSrcDB, "BEGIN DEFERRED; PRAGMA schema_version;", 0, 0, 0);
        } else {
          sqlite3_close(newSrcDB);
          newSrcDB = NULL;
        }
      }
    
    if (shouldUnlinkWAL) {
      if (srcWalFD >= 0) {
        osClose(srcWalFD);
        srcWalFD = -1;
      }
      unlink(replacementSrcWALPath);
      free(replacementSrcWALPath);
      replacementSrcWALPath = NULL;

      unlink(replacementSrcShmPath);
      free(replacementSrcShmPath);
      replacementSrcShmPath = NULL;
    }

      if (rc == SQLITE_OK) {
        notGuardedFileDescriptors = 1;
        srcFD = tsrcFd;
        srcPath = replacementSrcPath;
      }
    }
    
  }
  
  if (!srcdb2) {
    rc = SQLITE_INTERNAL;
  }
  if( rc!=SQLITE_OK ){
    goto end_replace_database;
  }
  /* both databases are locked appropriately, copy the src wal journal if 
   ** one exists and then the actual database file
   */
  strlcpy(srcWalPath, srcPath, MAXPATHLEN+5);
  strlcat(srcWalPath, "-wal", MAXPATHLEN+5);
  if (srcWalFD < 0) {
    srcWalFD = robust_open2(srcWalPath, O_RDONLY, 0, 0, 0);
  }
  if( !(srcWalFD<0) ){
    char dstWalPath[MAXPATHLEN+5];
    int dstWalFD = -1;
    int protFlags = 0;
    strlcpy(dstWalPath, pFile->zPath, MAXPATHLEN+5);
    strlcat(dstWalPath, "-wal", MAXPATHLEN+5);

#if SQLITE_ENABLE_DATA_PROTECTION
    protFlags = pFile->protFlags;
#endif
    rc = unixOpenChildFile(dstWalPath, O_RDWR|O_CREAT, SQLITE_OPEN_WAL,
                           protFlags, &dstWalFD);
    if( rc==SQLITE_OK ){
      s = copyfile_state_alloc();
      lseek(srcWalFD, 0, SEEK_SET);
      lseek(dstWalFD, 0, SEEK_SET);
      if( sqlite_guarded_fcopyfile(srcWalFD, dstWalFD) != 0){
        int err=errno;
        switch(err) {
          case ENOMEM:
            rc = SQLITE_NOMEM;
            break;
          default:
            storeLastErrno(pFile, err);
            rc = SQLITE_IOERR;
        }
      }
      
      copyfile_state_free(s);
      fsync(dstWalFD);
      osClose(dstWalFD);
    }
    osClose(srcWalFD);
  }
  if( rc==SQLITE_OK ){
    /* before we copy, ensure that the file change counter will be modified */
    uint32_t srcChange = 0;
    uint32_t dstChange = 0;
    pread(srcFD, &srcChange, 4, 24);
    pread(pFile->h, &dstChange, 4, 24);
    
    /* copy the actual database */
    s = copyfile_state_alloc();
    lseek(srcFD, 0, SEEK_SET);
    lseek(pFile->h, 0, SEEK_SET);
    if( sqlite_guarded_fcopyfile(srcFD, pFile->h) != 0 ){
      int err=errno;
      switch(err) {
        case ENOMEM:
          rc = SQLITE_NOMEM;
          break;
        default:
          storeLastErrno(pFile, err);
          rc = SQLITE_IOERR;
      }
    }
    copyfile_state_free(s);
    
    if (srcChange == dstChange) {
      /* modify the change counter to force page zero to be reloaded */
      dstChange ++;
      osPwrite(pFile->h, &dstChange, 4, 24);
    }
  }
  if( isSrcCorrupt ){
    sqlite3demo_superunlock_corrupt(src_file, corruptSrcFileLock);
  }else{
    /* done with the source db so end the transaction */
    sqlite3_exec(srcdb2, "COMMIT", 0, 0, 0);
  }
  /* zero out any old journal clutter */
  if( rc==SQLITE_OK ){
    int skipWAL = (srcWalFD<0)?0:1;
    unixInvalidateSupportFiles(pFile, skipWAL);
    pFile->pMethod->xSync((sqlite3_file*)pFile, SQLITE_SYNC_FULL);
  }
  
end_replace_database:
  if (extraFDtoClose >= 0) {
    /* extraFDtoClose is created with mkstemp, can't use guarded close() */
    close(extraFDtoClose);
    extraFDtoClose = -1;
  }
  if (replacementSrcPath) {
    unlink(replacementSrcPath);
    free(replacementSrcPath);
  }
  if (replacementSrcWALPath) {
    unlink(replacementSrcWALPath);
    free(replacementSrcWALPath);
  }
  if (replacementSrcShmPath) {
    unlink(replacementSrcShmPath);
    free(replacementSrcShmPath);
  }
  if (srcdb2) {
    sqlite3_close(srcdb2);
    srcdb2 = NULL;
  }
  if( pSrcBtree ){
    sqlite3BtreeLeave(pSrcBtree);
  }
  sqlite3_mutex_leave(srcdb->mutex);
  if( isDstCorrupt ){
    sqlite3demo_superunlock_corrupt(id, corruptDstFileLock);
  }else{
    sqlite3demo_superunlock(pLock);
  }
  if( rc==SQLITE_OK && (srcWalFD>=0) && !isSrcCorrupt ){
    /* sync up the -shm file */
    sqlite3 *tdb = NULL;
    int trc = sqlite3_open_v2(pFile->zPath, &tdb, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|flags, 0);
    if( trc==SQLITE_OK ){
      sqlite3_busy_timeout(tdb, srcdb->busyTimeout);
      sqlite3_exec(tdb, "PRAGMA schema_version", 0, 0, 0);
    }
    if( tdb!=NULL ){
      sqlite3_close(tdb);
    }
  }
  
  if (rc) {
    _open_error_asl_log();
    const char* dstPath = pFile ? pFile->zPath : "<dst db null>";

    if (dstPath == NULL) {
      dstPath = "<dst path null>";
    }
    if (realSrcPath == NULL) {
      realSrcPath = "<src path null>";
    }

    asl_log(errorlog_client, NULL, ASL_LEVEL_ERR, "SQLite: unixReplaceDatabase() failed with error %d for replacing '%s' with '%s' \n", (int)rc, dstPath, realSrcPath);
  }
  return rc;
}
#define SQLITE_FILE_HEADER_LEN 16
/* Check for a conflicting lock.  If one is found, print an this
 ** on standard output using the format string given and return 1.
 ** If there are no conflicting locks, return 0.
 */
4425
4426
4427
4428
4429
4430
4431


4432
4433


4434
4435
4436
4437











































4438
4439
4440
4441
4442
4443
4444
    return -1;
  }
  
  if( lk.l_type!=F_UNLCK && (pid==SQLITE_LOCKSTATE_ANYPID || lk.l_pid==pid) ){
#ifdef SQLITE_DEBUG
    fprintf(stderr, "%s lock held by %d\n", zType, (int)lk.l_pid);
#endif


    return 1;
  } 


  return 0;
}

static int unixLockstatePid(unixFile *, pid_t, int *);












































#endif /* (SQLITE_ENABLE_APPLE_SPI>0) && defined(__APPLE__) */


/*
** If *pArg is inititially negative then this is a query.  Set *pArg to
** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.







>
>


>
>




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
    return -1;
  }
  
  if( lk.l_type!=F_UNLCK && (pid==SQLITE_LOCKSTATE_ANYPID || lk.l_pid==pid) ){
#ifdef SQLITE_DEBUG
    fprintf(stderr, "%s lock held by %d\n", zType, (int)lk.l_pid);
#endif
    OSTRACE(("GETLOCKSTATE fd-%d type-%s, loc-0x%x len-%d pid-(%d) state=ON\n",
             h, zType, iOfst, iCnt, pid));
    return 1;
  } 
  OSTRACE(("GETLOCKSTATE fd-%d type-%s, loc-0x%x len-%d pid-(%d) state=OFF\n",
           h, zType, iOfst, iCnt, pid));
  return 0;
}

static int unixLockstatePid(unixFile *, pid_t, int *);

/*
  ** Copy data from shared-memory into a buffer.
  **
  ** Returns an error if the mapping has disappeared, or if the memory cannot
  ** be accessed safely.
*/
static int unixShmRead(sqlite3_file *fd,               /* The underlying database file */
                       void *dst,                      /* The destination memory */
                       volatile void *src,             /* The source (mapped) memory */
                       sqlite3_int64 nByte             /* Size of the destination */
){
#if defined(__APPLE__)
    unixFile *pDbFd;                /* The underlying database file */

  pDbFd = (unixFile*)fd;

  if ( pDbFd->fsFlags & SQLITE_FSFLAGS_IS_MMAP_SAFE ) {
      memcpy(dst, (void *)src, nByte);
    } else {
        mach_vm_size_t aSz;
        mach_vm_read_overwrite(mach_task_self(), src, nByte, dst, &aSz);
        if ( aSz!=nByte ) {
            return SQLITE_IOERR;
          }
      }
#else
    memcpy(dst, (void *)src, nByte);
#endif

  return SQLITE_OK;
}

int sqlite30sShmRead(sqlite3_file *id, void *dst, volatile void *src, sqlite3_int64 size);

int sqlite30sShmRead(sqlite3_file *id, void *dst, volatile void *src, sqlite3_int64 size){
#if (defined(__APPLE__) && ((!defined(TARGET_OS_EMBEDDED)||(TARGET_OS_EMBEDDED==0))))
        return unixShmRead(id, dst, src, size);
#else
    memcpy(dst, (void *)src, size);
    return SQLITE_OK;
#endif
}

#endif /* (SQLITE_ENABLE_APPLE_SPI>0) && defined(__APPLE__) */


/*
** If *pArg is inititially negative then this is a query.  Set *pArg to
** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
4558
4559
4560
4561
4562
4563
4564














4565
4566
4567
4568
4569
4570
4571
      }
      pLockstate = (LockstatePID *)pArg;
      rc = unixLockstatePid(pFile, pLockstate->pid, &(pLockstate->state));
      return rc;
    }
      
#endif /* (SQLITE_ENABLE_APPLE_SPI>0) && defined(__APPLE__) */














  }
  return SQLITE_NOTFOUND;
}

/*
** Return the sector size in bytes of the underlying block device for
** the specified file. This is almost always 512 bytes, but may be







>
>
>
>
>
>
>
>
>
>
>
>
>
>







5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
      }
      pLockstate = (LockstatePID *)pArg;
      rc = unixLockstatePid(pFile, pLockstate->pid, &(pLockstate->state));
      return rc;
    }
      
#endif /* (SQLITE_ENABLE_APPLE_SPI>0) && defined(__APPLE__) */

    case SQLITE_FCNTL_SET_NEXTFLOCKTIMEOUT: {
#ifdef SQLITE_USE_FLOCKTIMEOUT
      pFile->nextFlockTimeout = *(int*)pArg;
#endif
      return SQLITE_OK;
    }
    case SQLITE_FCNTL_GET_NEXTFLOCKTIMEOUT: {
#ifdef SQLITE_USE_FLOCKTIMEOUT
      *(int*)pArg = pFile->nextFlockTimeout;
#endif
      return SQLITE_OK;
    }

  }
  return SQLITE_NOTFOUND;
}

/*
** Return the sector size in bytes of the underlying block device for
** the specified file. This is almost always 512 bytes, but may be
4774
4775
4776
4777
4778
4779
4780



4781
4782
4783
4784
4785
4786
4787
** otherwise.
*/
static int unixShmSystemLock(
  unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */
  int lockType,          /* F_UNLCK, F_RDLCK, or F_WRLCK */
  int ofst,              /* First byte of the locking range */
  int n                  /* Number of bytes to lock */



){
  struct flock f;       /* The posix advisory locking structure */
  int rc = SQLITE_OK;   /* Result code form fcntl() */

  /* Access to the unixShmNode object is serialized by the caller */
  assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );








>
>
>







5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
** otherwise.
*/
static int unixShmSystemLock(
  unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */
  int lockType,          /* F_UNLCK, F_RDLCK, or F_WRLCK */
  int ofst,              /* First byte of the locking range */
  int n                  /* Number of bytes to lock */
#ifdef SQLITE_USE_FLOCKTIMEOUT
  , int timeout          /* Lock timeout in milliseconds */
#endif
){
  struct flock f;       /* The posix advisory locking structure */
  int rc = SQLITE_OK;   /* Result code form fcntl() */

  /* Access to the unixShmNode object is serialized by the caller */
  assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );

4795
4796
4797
4798
4799
4800
4801












4802




4803
4804
4805
4806
4807
4808
4809
    /* Initialize the locking parameters */
    memset(&f, 0, sizeof(f));
    f.l_type = lockType;
    f.l_whence = SEEK_SET;
    f.l_start = ofst;
    f.l_len = n;













    rc = osFcntl(pShmNode->h, F_SETLK, &f);




    rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
  }

  /* Update the global lock state and do debug tracing */
#ifdef SQLITE_DEBUG
  { u16 mask;
  OSTRACE(("SHM-LOCK "));







>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>







5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
    /* Initialize the locking parameters */
    memset(&f, 0, sizeof(f));
    f.l_type = lockType;
    f.l_whence = SEEK_SET;
    f.l_start = ofst;
    f.l_len = n;

#ifdef SQLITE_USE_FLOCKTIMEOUT
    if( timeout>0 ){
      struct timespec ts;
      struct flocktimeout fltimeout;

      ts.tv_sec = timeout / 1000;
      ts.tv_nsec = (((long)timeout) % 1000L) * 1000000L;
      fltimeout.fl = f;
      fltimeout.timeout = ts;

      rc = osFcntl(pShmNode->h, F_SETLKWTIMEOUT, &fltimeout);
    }else{
      rc = osFcntl(pShmNode->h, F_SETLK, &f);
    }
#else
    rc = osFcntl(pShmNode->h, F_SETLK, &f);
#endif
    rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
  }

  /* Update the global lock state and do debug tracing */
#ifdef SQLITE_DEBUG
  { u16 mask;
  OSTRACE(("SHM-LOCK "));
4967
4968
4969
4970
4971
4972
4973

4974
4975
4976
4977
4978
4979
4980
    const char *zBasePath = pDbFd->zPath;

    /* Call fstat() to figure out the permissions on the database file. If
    ** a new *-shm file is created, an attempt will be made to create it
    ** with the same permissions.
    */
    if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){

      rc = SQLITE_IOERR_FSTAT;
      goto shm_open_err;
    }

#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
    /* If pDbFd is configured with proxy locking mode, use the local 
     ** lock file path to determine the -shm file path







>







5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
    const char *zBasePath = pDbFd->zPath;

    /* Call fstat() to figure out the permissions on the database file. If
    ** a new *-shm file is created, an attempt will be made to create it
    ** with the same permissions.
    */
    if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){
      storeLastErrno(pDbFd, errno);
      rc = SQLITE_IOERR_FSTAT;
      goto shm_open_err;
    }

#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
    /* If pDbFd is configured with proxy locking mode, use the local 
     ** lock file path to determine the -shm file path
5024
5025
5026
5027
5028
5029
5030




5031

5032




5033




5034
5035














5036
5037
5038
5039
5040




5041

5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055






5056

5057
5058
5059
5060
5061
5062
5063
    || defined(__APPLE__))
         || (pDbFd->openFlags & O_RDWR) != O_RDWR
#endif
         ){
        openFlags = O_RDONLY;
        pShmNode->isReadonly = 1;
      }




      pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));

      if( pShmNode->h<0 ){




        rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);




        goto shm_open_err;
      }















      /* Check to see if another process is holding the dead-man switch.
      ** If not, truncate the file to zero length. 
      */
      rc = SQLITE_OK;




      if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){

        if( robust_ftruncate(pShmNode->h, 0) ){
          rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
        }else{
          /* If running as root set the uid/gid of the shm file to match
          ** the database */
          uid_t euid = geteuid();
          if( (!pShmNode->isReadonly) && euid==0 && (euid!=sStat.st_uid || getegid()!=sStat.st_gid) ){
            if( osFchown(pShmNode->h, sStat.st_uid, sStat.st_gid) ){
              rc = SQLITE_IOERR_SHMOPEN;
            }
          }
        }
      }
      if( rc==SQLITE_OK ){






        rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1);

      }
      if( rc ) goto shm_open_err;
    }
  }

  /* Make the new connection a child of the unixShmNode */
  p->pShmNode = pShmNode;







>
>
>
>
|
>

>
>
>
>
|
>
>
>
>


>
>
>
>
>
>
>
>
>
>
>
>
>
>





>
>
>
>

>














>
>
>
>
>
>

>







5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
    || defined(__APPLE__))
         || (pDbFd->openFlags & O_RDWR) != O_RDWR
#endif
         ){
        openFlags = O_RDONLY;
        pShmNode->isReadonly = 1;
      }
      int protFlags = 0;
#if SQLITE_ENABLE_DATA_PROTECTION
      protFlags = shmUnixProtectionClassForVFSProtectionFlags(pDbFd->protFlags);
#endif
      pShmNode->h = robust_open2(zShmFilename, openFlags, (sStat.st_mode&0777), protFlags, 1);

      if( pShmNode->h<0 ){
#if SQLITE_ENABLE_DATA_PROTECTION
        if( errno==EPERM ) {
          rc = unixLogError(SQLITE_AUTH, "open", zShmFilename);
        }else{
          rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
        }
#else
        rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
#endif      
        goto shm_open_err;
      }
#if SQLITE_ENABLE_DATA_PROTECTION
      if( (pDbFd->ctrlFlags&UNIXFILE_RDONLY) == 0 ){
        int shmProtFlags = (pDbFd->protFlags)&SQLITE_OPEN_FILEPROTECTION_MASK;
        
        rc = unixSetFileProtection(pShmNode->h, shmProtFlags, 1);
        if( rc ){
          if( rc==SQLITE_IOERR ){
            storeLastErrno(pDbFd, errno);
          }
          osClose(pShmNode->h);
          goto shm_open_err;
        }
      }
#endif      

      /* Check to see if another process is holding the dead-man switch.
      ** If not, truncate the file to zero length. 
      */
      rc = SQLITE_OK;
#ifdef SQLITE_USE_FLOCKTIMEOUT
      if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1, pDbFd->nextFlockTimeout)==SQLITE_OK ){
        pDbFd->nextFlockTimeout = 0;
#else
      if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
#endif
        if( robust_ftruncate(pShmNode->h, 0) ){
          rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
        }else{
          /* If running as root set the uid/gid of the shm file to match
          ** the database */
          uid_t euid = geteuid();
          if( (!pShmNode->isReadonly) && euid==0 && (euid!=sStat.st_uid || getegid()!=sStat.st_gid) ){
            if( osFchown(pShmNode->h, sStat.st_uid, sStat.st_gid) ){
              rc = SQLITE_IOERR_SHMOPEN;
            }
          }
        }
      }
      if( rc==SQLITE_OK ){
#ifdef SQLITE_USE_FLOCKTIMEOUT
        rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1, pDbFd->nextFlockTimeout);
        if( rc==SQLITE_OK ){
          pDbFd->nextFlockTimeout = 0;
        }
#else
        rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1);
#endif
      }
      if( rc ) goto shm_open_err;
    }
  }

  /* Make the new connection a child of the unixShmNode */
  p->pShmNode = pShmNode;
5111
5112
5113
5114
5115
5116
5117


5118
5119
5120
5121
5122
5123
5124
static int unixShmMap(
  sqlite3_file *fd,               /* Handle open on database file */
  int iRegion,                    /* Region to retrieve */
  int szRegion,                   /* Size of regions */
  int bExtend,                    /* True to extend file if necessary */
  void volatile **pp              /* OUT: Mapped memory */
){


  unixFile *pDbFd = (unixFile*)fd;
  unixShm *p;
  unixShmNode *pShmNode;
  int rc = SQLITE_OK;
  int nShmPerMap = unixShmRegionPerMap();
  int nReqRegion;








>
>







6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
static int unixShmMap(
  sqlite3_file *fd,               /* Handle open on database file */
  int iRegion,                    /* Region to retrieve */
  int szRegion,                   /* Size of regions */
  int bExtend,                    /* True to extend file if necessary */
  void volatile **pp              /* OUT: Mapped memory */
){
  static int oncethingie = 0;
  
  unixFile *pDbFd = (unixFile*)fd;
  unixShm *p;
  unixShmNode *pShmNode;
  int rc = SQLITE_OK;
  int nShmPerMap = unixShmRegionPerMap();
  int nReqRegion;

5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208

5209
5210
5211
5212
5213
5214
5215
      rc = SQLITE_IOERR_NOMEM;
      goto shmpage_out;
    }
    pShmNode->apRegion = apNew;
    while( pShmNode->nRegion<nReqRegion ){
      int nMap = szRegion*nShmPerMap;
      int i;
      void *pMem;
      if( pShmNode->h>=0 ){
        pMem = osMmap(0, nMap,
            pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, 
            MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
        );

        if( pMem==MAP_FAILED ){
          rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
          goto shmpage_out;
        }
      }else{
        pMem = sqlite3_malloc(szRegion);
        if( pMem==0 ){







|





>







6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
      rc = SQLITE_IOERR_NOMEM;
      goto shmpage_out;
    }
    pShmNode->apRegion = apNew;
    while( pShmNode->nRegion<nReqRegion ){
      int nMap = szRegion*nShmPerMap;
      int i;
      void *pMem = NULL;
      if( pShmNode->h>=0 ){
        pMem = osMmap(0, nMap,
            pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, 
            MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
        );
        
        if( pMem==MAP_FAILED ){
          rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
          goto shmpage_out;
        }
      }else{
        pMem = sqlite3_malloc(szRegion);
        if( pMem==0 ){
5281
5282
5283
5284
5285
5286
5287






5288

5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307





5308
5309
5310
5311
5312
5313
5314
5315






5316

5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332





5333
5334
5335
5336
5337
5338
5339
5340





5341
5342

5343
5344
5345
5346
5347
5348
5349
      if( pX==p ) continue;
      assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
      allMask |= pX->sharedMask;
    }

    /* Unlock the system-level locks */
    if( (mask & allMask)==0 ){






      rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n);

    }else{
      rc = SQLITE_OK;
    }

    /* Undo the local locks */
    if( rc==SQLITE_OK ){
      p->exclMask &= ~mask;
      p->sharedMask &= ~mask;
    } 
  }else if( flags & SQLITE_SHM_SHARED ){
    u16 allShared = 0;  /* Union of locks held by connections other than "p" */

    /* Find out which shared locks are already held by sibling connections.
    ** If any sibling already holds an exclusive lock, go ahead and return
    ** SQLITE_BUSY.
    */
    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
      if( (pX->exclMask & mask)!=0 ){
        rc = SQLITE_BUSY;





        break;
      }
      allShared |= pX->sharedMask;
    }

    /* Get shared locks at the system level, if necessary */
    if( rc==SQLITE_OK ){
      if( (allShared & mask)==0 ){






        rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n);

      }else{
        rc = SQLITE_OK;
      }
    }

    /* Get the local shared locks */
    if( rc==SQLITE_OK ){
      p->sharedMask |= mask;
    }
  }else{
    /* Make sure no sibling connections hold locks that will block this
    ** lock.  If any do, return SQLITE_BUSY right away.
    */
    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
      if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
        rc = SQLITE_BUSY;





        break;
      }
    }
  
    /* Get the exclusive locks at the system level.  Then if successful
    ** also mark the local connection as being locked.
    */
    if( rc==SQLITE_OK ){





      rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n);
      if( rc==SQLITE_OK ){

        assert( (p->sharedMask & mask)==0 );
        p->exclMask |= mask;
      }
    }
  }
  sqlite3_mutex_leave(pShmNode->mutex);
  OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",







>
>
>
>
>
>

>



















>
>
>
>
>








>
>
>
>
>
>

>
















>
>
>
>
>








>
>
>
>
>


>







6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
      if( pX==p ) continue;
      assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
      allMask |= pX->sharedMask;
    }

    /* Unlock the system-level locks */
    if( (mask & allMask)==0 ){
#ifdef SQLITE_USE_FLOCKTIMEOUT
      rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n, pDbFd->nextFlockTimeout);
      if( rc==SQLITE_OK ){
        pDbFd->nextFlockTimeout = 0;
      }
#else
      rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n);
#endif
    }else{
      rc = SQLITE_OK;
    }

    /* Undo the local locks */
    if( rc==SQLITE_OK ){
      p->exclMask &= ~mask;
      p->sharedMask &= ~mask;
    } 
  }else if( flags & SQLITE_SHM_SHARED ){
    u16 allShared = 0;  /* Union of locks held by connections other than "p" */

    /* Find out which shared locks are already held by sibling connections.
    ** If any sibling already holds an exclusive lock, go ahead and return
    ** SQLITE_BUSY.
    */
    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
      if( (pX->exclMask & mask)!=0 ){
        rc = SQLITE_BUSY;
#ifdef SQLITE_USE_FLOCKTIMEOUT
        if( pDbFd->nextFlockTimeout>0 ){
          usleep(((int)pDbFd->nextFlockTimeout) * 1000L);
        }
#endif
        break;
      }
      allShared |= pX->sharedMask;
    }

    /* Get shared locks at the system level, if necessary */
    if( rc==SQLITE_OK ){
      if( (allShared & mask)==0 ){
#ifdef SQLITE_USE_FLOCKTIMEOUT
        rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n, pDbFd->nextFlockTimeout);
        if( rc==SQLITE_OK ){
          pDbFd->nextFlockTimeout = 0;
        }
#else
        rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n);
#endif
      }else{
        rc = SQLITE_OK;
      }
    }

    /* Get the local shared locks */
    if( rc==SQLITE_OK ){
      p->sharedMask |= mask;
    }
  }else{
    /* Make sure no sibling connections hold locks that will block this
    ** lock.  If any do, return SQLITE_BUSY right away.
    */
    for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
      if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
        rc = SQLITE_BUSY;
#ifdef SQLITE_USE_FLOCKTIMEOUT
        if( pDbFd->nextFlockTimeout>0 ){
          usleep(((int)pDbFd->nextFlockTimeout) * 1000L);
        }
#endif
        break;
      }
    }
  
    /* Get the exclusive locks at the system level.  Then if successful
    ** also mark the local connection as being locked.
    */
    if( rc==SQLITE_OK ){
#ifdef SQLITE_USE_FLOCKTIMEOUT
      rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n, pDbFd->nextFlockTimeout);
      if( rc==SQLITE_OK ){
        pDbFd->nextFlockTimeout = 0;
#else
      rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n);
      if( rc==SQLITE_OK ){
#endif
        assert( (p->sharedMask & mask)==0 );
        p->exclMask |= mask;
      }
    }
  }
  sqlite3_mutex_leave(pShmNode->mutex);
  OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
5357
5358
5359
5360
5361
5362
5363



5364
5365

5366
5367
5368
5369
5370
5371
5372
** All loads and stores begun before the barrier must complete before
** any load or store begun after the barrier.
*/
static void unixShmBarrier(
  sqlite3_file *fd                /* Database file holding the shared memory */
){
  UNUSED_PARAMETER(fd);



  unixEnterMutex();
  unixLeaveMutex();

}

/*
** Close a connection to shared-memory.  Delete the underlying 
** storage if deleteFlag is true.
**
** If there is no shared memory associated with the connection then this







>
>
>


>







6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
** All loads and stores begun before the barrier must complete before
** any load or store begun after the barrier.
*/
static void unixShmBarrier(
  sqlite3_file *fd                /* Database file holding the shared memory */
){
  UNUSED_PARAMETER(fd);
#if defined(__APPLE__)
  OSMemoryBarrier();
#else
  unixEnterMutex();
  unixLeaveMutex();
#endif
}

/*
** Close a connection to shared-memory.  Delete the underlying 
** storage if deleteFlag is true.
**
** If there is no shared memory associated with the connection then this
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442

5443
5444
5445


5446
5447
5448
5449
5450
5451
5452
#endif /* #ifndef SQLITE_OMIT_WAL */

#if (SQLITE_ENABLE_APPLE_SPI>0) && defined(__APPLE__)
static const char *unixTempFileDir(void);

static int unixInvalidateSupportFiles(unixFile *pFile, int skipWAL) {
  char jPath[MAXPATHLEN+9];
  int zLen = strlcpy(jPath, pFile->zPath, MAXPATHLEN+9);
  if( zLen<MAXPATHLEN ){
    size_t jLen;
    const char extensions[3][9] = { "-wal", "-journal", "-shm" };
    int j = (skipWAL ? 1 : 0);
    for( ; j<3; j++ ){

      
      /* Check to see if the shm file is already opened for this pFile */
      if( j==2 ){


        unixEnterMutex(); /* Because pFile->pInode is shared across threads */
        unixShmNode *pShmNode = pFile->pInode->pShmNode;
        if( pShmNode && !pShmNode->isReadonly ){
          struct stat sStat;
          sqlite3_mutex_enter(pShmNode->mutex);
          
          if( pShmNode->h>=0 && !osFstat(pShmNode->h, &sStat) ){







|





>



>
>







6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
#endif /* #ifndef SQLITE_OMIT_WAL */

#if (SQLITE_ENABLE_APPLE_SPI>0) && defined(__APPLE__)
static const char *unixTempFileDir(void);

static int unixInvalidateSupportFiles(unixFile *pFile, int skipWAL) {
  char jPath[MAXPATHLEN+9];
  int zLen = (int)strlcpy(jPath, pFile->zPath, MAXPATHLEN+9);
  if( zLen<MAXPATHLEN ){
    size_t jLen;
    const char extensions[3][9] = { "-wal", "-journal", "-shm" };
    int j = (skipWAL ? 1 : 0);
    for( ; j<3; j++ ){
      int isShm = 0;
      
      /* Check to see if the shm file is already opened for this pFile */
      if( j==2 ){
        isShm = 1;
        
        unixEnterMutex(); /* Because pFile->pInode is shared across threads */
        unixShmNode *pShmNode = pFile->pInode->pShmNode;
        if( pShmNode && !pShmNode->isReadonly ){
          struct stat sStat;
          sqlite3_mutex_enter(pShmNode->mutex);
          
          if( pShmNode->h>=0 && !osFstat(pShmNode->h, &sStat) ){
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479

5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
          sqlite3_mutex_leave(pShmNode->mutex);
        }
        unixLeaveMutex();
      }
      jLen = strlcpy(&jPath[zLen], extensions[j], 9);
      if( jLen < 9 ){
        int jflags = (j<2) ? O_TRUNC : O_RDWR;
        int jfd = open(jPath, jflags);
        if( jfd==(-1) ){
          if( errno!=ENOENT ){
            perror(jPath);
          }
        } else {
          if( j==2 ){
            struct stat sStat;
            if( !osFstat(jfd, &sStat) ){
              unsigned long size = (sStat.st_size<4) ? sStat.st_size : 4;
              if( size>0 ){
                uint32_t zero = 0;

                pwrite(jfd, &zero, (size_t)size, 0);
              }
            }
          }
          fsync(jfd);
          close(jfd);
        }
      }
    }
  }
  return SQLITE_OK;
}








|











>
|




|







6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
          sqlite3_mutex_leave(pShmNode->mutex);
        }
        unixLeaveMutex();
      }
      jLen = strlcpy(&jPath[zLen], extensions[j], 9);
      if( jLen < 9 ){
        int jflags = (j<2) ? O_TRUNC : O_RDWR;
        int jfd = robust_open2(jPath, jflags, 0, 0, isShm);
        if( jfd==(-1) ){
          if( errno!=ENOENT ){
            perror(jPath);
          }
        } else {
          if( j==2 ){
            struct stat sStat;
            if( !osFstat(jfd, &sStat) ){
              unsigned long size = (sStat.st_size<4) ? sStat.st_size : 4;
              if( size>0 ){
                uint32_t zero = 0;
                
                os_rawPwrite(jfd, &zero, (size_t)size, 0);
              }
            }
          }
          fsync(jfd);
          osClose(jfd);
        }
      }
    }
  }
  return SQLITE_OK;
}

5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566

5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577

5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589














5590
5591
5592
5593
5594
5595
5596
5597
5598
5599



5600


5601

5602
5603
5604
5605
5606
5607
5608
5609
5610

5611
5612
5613
5614
5615

5616
5617
5618
5619
5620
5621
5622
    result = errno;
  }
  if (result) {
    rc = SQLITE_IOERR;
    storeLastErrno(pFile, result);
  }
  
  int fd2 = open(journalPath, O_RDWR);
  int result2 = 0;
  if (fd2 < 0) {
    if (errno != ENOENT) {
      result2 = errno;
    } else {
      result2 = 0;
    }
  } else {
    result2 = ftruncate(fd2, 0ll);
    if (result2) {
      result2 = errno;
    }
  }
  if (result2 && !result) {
    rc = SQLITE_IOERR;
    storeLastErrno(pFile, result2);
  }
  
  int fd3 = open(walPath, O_RDWR);
  int result3 = 0;
  if (fd3 < 0) {
    if (errno != ENOENT) {
      result3 = errno;
    } else {
      result3 = 0;
    }
  } else {
    result3 = ftruncate(fd3, 0ll);
    if (result3) {
      result3 = errno;
    }
  }
  if (result3 && !(result || result2)) {
    rc = SQLITE_IOERR;
    storeLastErrno(pFile, result2);
  }
  
  if (fd3 >= 0) {
    fsync(fd3);
    close(fd3);
  }
  if (fd2 >= 0) {
    fsync(fd2);
    close(fd2);
  }
  fsync(fd1);

	
  return rc;
}

static int unixTruncateDatabase(unixFile *pFile, int bFlags) {
  sqlite3_file *id = (sqlite3_file *)pFile;
  int rc = SQLITE_OK;
  void *pLock = NULL;
  int flags = 0;
  int corruptFileLock = 0;
  int isCorrupt = 0;

  int force = (bFlags & SQLITE_TRUNCATE_FORCE);
  int safeFailed = 0;

#if SQLITE_ENABLE_DATA_PROTECTION
  flags |= pFile->protFlags;
#endif
#if SQLITE_ENABLE_LOCKING_STYLE
  if( isProxyLockingMode(pFile) ){
    flags |= SQLITE_OPEN_AUTOPROXY;
  }
#endif
  














  rc = sqlite3demo_superlock(pFile->zPath, 0, flags, 0, 0, &pLock);
  if( rc ){
    if( rc==SQLITE_CORRUPT || rc==SQLITE_NOTADB ){
      isCorrupt = 1;
      rc = sqlite3demo_superlock_corrupt(id, SQLITE_LOCK_EXCLUSIVE,
                                         &corruptFileLock);
    }
    if( rc && !force ){
      return rc;
    }



    rc = SQLITE_OK; /* Ignore the locking failure if force is true */


  }

  if( (bFlags&SQLITE_TRUNCATE_INITIALIZE_HEADER_MASK)!=0 ){
    /* initialize a new database in TMPDIR and copy the contents over */
    const char *tDir = unixTempFileDir();
    int tDirLen = strlen(tDir);
    int tLen = sizeof(char) * (tDirLen + 12);
    char *tDbPath = (char *)malloc(tLen);
    int tFd = -1;
    
    strlcpy(tDbPath, tDir, tLen);

    if( tDbPath[(tDirLen-1)] != '/' ){
      strlcat(tDbPath, "/tmpdbXXXXX", tLen);
    } else {
      strlcat(tDbPath, "tmpdbXXXXX", tLen);
    }

    tFd = mkstemp(tDbPath);
    if( tFd==-1 ){
      storeLastErrno(pFile, errno);
      rc = SQLITE_IOERR;
      safeFailed = 1;
    }else{
      sqlite3 *tDb = NULL;







|


















|




















|



|

|
>







|



>











|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|









>
>
>

>
>

>


<
<
|
|



>

|
<
<

>







6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673


6674
6675
6676
6677
6678
6679
6680
6681


6682
6683
6684
6685
6686
6687
6688
6689
6690
    result = errno;
  }
  if (result) {
    rc = SQLITE_IOERR;
    storeLastErrno(pFile, result);
  }
  
  int fd2 = robust_open2(journalPath, O_RDWR, 0, 0, 0);
  int result2 = 0;
  if (fd2 < 0) {
    if (errno != ENOENT) {
      result2 = errno;
    } else {
      result2 = 0;
    }
  } else {
    result2 = ftruncate(fd2, 0ll);
    if (result2) {
      result2 = errno;
    }
  }
  if (result2 && !result) {
    rc = SQLITE_IOERR;
    storeLastErrno(pFile, result2);
  }
  
  int fd3 = robust_open2(walPath, O_RDWR, 0, 0, 0);
  int result3 = 0;
  if (fd3 < 0) {
    if (errno != ENOENT) {
      result3 = errno;
    } else {
      result3 = 0;
    }
  } else {
    result3 = ftruncate(fd3, 0ll);
    if (result3) {
      result3 = errno;
    }
  }
  if (result3 && !(result || result2)) {
    rc = SQLITE_IOERR;
    storeLastErrno(pFile, result2);
  }
  
  if (fd3 >= 0) {
    fsync(fd3);
    osClose(fd3);
  }
  if (fd2 >= 0) {
    fsync(fd2);
    osClose(fd2);
  }
  
  pFile->pMethod->xSync((sqlite3_file*)pFile, SQLITE_SYNC_FULL);
	
  return rc;
}

static int unixTruncateDatabase(unixFile *pFile, int bFlags) {
  sqlite3_file *id = (sqlite3_file *)pFile;
  int rc = SQLITE_OK;
  Superlock *pLock = NULL;
  int flags = 0;
  int corruptFileLock = 0;
  int isCorrupt = 0;
  int isWal = (bFlags & SQLITE_TRUNCATE_JOURNALMODE_WAL);
  int force = (bFlags & SQLITE_TRUNCATE_FORCE);
  int safeFailed = 0;

#if SQLITE_ENABLE_DATA_PROTECTION
  flags |= pFile->protFlags;
#endif
#if SQLITE_ENABLE_LOCKING_STYLE
  if( isProxyLockingMode(pFile) ){
    flags |= SQLITE_OPEN_AUTOPROXY;
  }
#endif

  const char *tDir = NULL;
  int tDirLen = 0;

  if( (bFlags&SQLITE_TRUNCATE_INITIALIZE_HEADER_MASK)!=0 ){
    tDir = unixTempFileDir();
    tDirLen = tDir ? (int)strlen(tDir) : 0;
    if (tDirLen < 1) {
      _open_error_asl_log();
      asl_log(errorlog_client, NULL, ASL_LEVEL_CRIT, "SQLite: truncate database failed because TMPDIR is not set correctly\n");

      return SQLITE_PERM;
    }
  }
  
  rc = sqlite3demo_superlock(pFile->zPath, 0, flags, 0, 0, (void**)&pLock);
  if( rc ){
    if( rc==SQLITE_CORRUPT || rc==SQLITE_NOTADB ){
      isCorrupt = 1;
      rc = sqlite3demo_superlock_corrupt(id, SQLITE_LOCK_EXCLUSIVE,
                                         &corruptFileLock);
    }
    if( rc && !force ){
      return rc;
    }
    if (!isWal) {
      isWal = (1 == readWALSettingFromFile(pFile));
    }
    rc = SQLITE_OK; /* Ignore the locking failure if force is true */
  } else if (!isWal && (pLock != NULL)) {
    isWal = pLock->bWal;
  }
  
  if( (bFlags&SQLITE_TRUNCATE_INITIALIZE_HEADER_MASK)!=0 ){
    /* initialize a new database in TMPDIR and copy the contents over */


    int tLen = sizeof(char) * (tDirLen + 26);
    char *tDbPath = (char *)calloc(1, tLen);
    int tFd = -1;
    
    strlcpy(tDbPath, tDir, tLen);
    /* create a temporary database with the prefix tmpsqlitetruncatedb */
    if( tDbPath[(tDirLen-1)] != '/' ){
      strlcat(tDbPath, "/", tLen);


    }
    strlcat(tDbPath, "tmpsqlitetruncatedbXXXXXX", tLen);
    tFd = mkstemp(tDbPath);
    if( tFd==-1 ){
      storeLastErrno(pFile, errno);
      rc = SQLITE_IOERR;
      safeFailed = 1;
    }else{
      sqlite3 *tDb = NULL;
5668
5669
5670
5671
5672
5673
5674

5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690

5691
5692
5693
5694
5695
5696
5697

5698

5699
5700
5701
5702
5703
5704
5705
      }
      if( tDb ){
        int off = 0;
        /* merge the wal into the db */
        sqlite3_file_control(tDb, NULL, SQLITE_FCNTL_PERSIST_WAL, &off);
        sqlite3_close(tDb);
      }

      s = copyfile_state_alloc();
      lseek(tFd, 0, SEEK_SET);
      lseek(pFile->h, 0, SEEK_SET);
      if( fcopyfile(tFd, pFile->h, s, COPYFILE_DATA) ){
        int err=errno;
        switch(err) {
          case ENOMEM:
            trc = SQLITE_NOMEM;
            break;
          default:
            storeLastErrno(pFile, err);
            trc = SQLITE_IOERR;
        }
      }
      copyfile_state_free(s);
      fsync(pFile->h);

      close(tFd);
      unlink(tDbPath);
      if( trc!=SQLITE_OK ){
        safeFailed = 1;
        rc = trc;
      }
    }

    free(tDbPath);

  } else {
    rc = pFile->pMethod->xTruncate(id, 
           ((pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS) != 0) ? 1L : 0L);
    if( rc ){
      safeFailed = 1;
    }
  }







>



|












>







>
|
>







6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
      }
      if( tDb ){
        int off = 0;
        /* merge the wal into the db */
        sqlite3_file_control(tDb, NULL, SQLITE_FCNTL_PERSIST_WAL, &off);
        sqlite3_close(tDb);
      }

      s = copyfile_state_alloc();
      lseek(tFd, 0, SEEK_SET);
      lseek(pFile->h, 0, SEEK_SET);
      if( sqlite_guarded_fcopyfile(tFd, pFile->h) != 0 ){
        int err=errno;
        switch(err) {
          case ENOMEM:
            trc = SQLITE_NOMEM;
            break;
          default:
            storeLastErrno(pFile, err);
            trc = SQLITE_IOERR;
        }
      }
      copyfile_state_free(s);
      fsync(pFile->h);
      /* tFd is created with mkstemp, can't use guarded close() */
      close(tFd);
      unlink(tDbPath);
      if( trc!=SQLITE_OK ){
        safeFailed = 1;
        rc = trc;
      }
    }
    if (tDbPath) {
      free(tDbPath);
    }
  } else {
    rc = pFile->pMethod->xTruncate(id, 
           ((pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS) != 0) ? 1L : 0L);
    if( rc ){
      safeFailed = 1;
    }
  }
5720
5721
5722
5723
5724
5725
5726











5727
5728
5729
5730
5731
5732
5733
    assert(force);
  }
  
  if( force && safeFailed){
    rc = unixUnsafeTruncateDatabase(pFile);
  }
  











  return rc;
}

/*
 ** Lock locations for shared-memory locks used by WAL mode.
 */
#ifndef SHM_BASE







>
>
>
>
>
>
>
>
>
>
>







6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
    assert(force);
  }
  
  if( force && safeFailed){
    rc = unixUnsafeTruncateDatabase(pFile);
  }
  
  if (rc) {
    _open_error_asl_log();
    const char* dstPath = pFile ? pFile->zPath : "<dst db null>";
    
    if (dstPath == NULL) {
      dstPath = "<dst path null>";
    }

    asl_log(errorlog_client, NULL, ASL_LEVEL_ERR, "SQLite: unixTruncateDatabase() failed with %d for replacing '%s' with flags %x\n", rc, dstPath, flags);
  }

  return rc;
}

/*
 ** Lock locations for shared-memory locks used by WAL mode.
 */
#ifndef SHM_BASE
5745
5746
5747
5748
5749
5750
5751

5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762











5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
*/
static int unixLockstatePid(unixFile *pFile, pid_t pid, int *pLockstate){
  int hDb;        /* File descriptor for the open database file */
  int hShm = -1;  /* File descriptor for WAL shared-memory file */
  ssize_t got;    /* Bytes read from header */
  int isWal = 0;             /* True if in WAL mode */
  int nLock = 0;             /* Number of locks held */

  int noHdr = 0;             /* Zero byte DB has no header */
  unsigned char aHdr[100];   /* Database header */
  
  assert(pLockstate);
  
  /* make sure we are dealing with a database file */
  hDb = pFile->h;
  if( hDb<0 ){
    *pLockstate = SQLITE_LOCKSTATE_ERROR;
    return SQLITE_ERROR;
  }











  assert( (strlen(SQLITE_FILE_HEADER)+1)==SQLITE_FILE_HEADER_LEN );
  got = pread(hDb, aHdr, 100, 0);
  if( got<0 ){
    *pLockstate = SQLITE_LOCKSTATE_ERROR;
    return SQLITE_ERROR;
  }
  if( got==0 ){
    noHdr = 1;
  }else if( got!=100
         || memcmp(aHdr, SQLITE_FILE_HEADER, SQLITE_FILE_HEADER_LEN)!=0
  ){
    *pLockstate = SQLITE_LOCKSTATE_NOTADB;
    return SQLITE_NOTADB;
  }
  
  /* First check for an exclusive lock */
  nLock += unixIsLocked(pid, hDb, F_RDLCK, SHARED_FIRST, SHARED_SIZE,
                        "EXCLUSIVE");
  if (!noHdr) {
    isWal = aHdr[18]==2;
  }
  if( nLock==0 && isWal==0 ){
    /* Rollback mode */
    nLock += unixIsLocked(pid, hDb, F_WRLCK, PENDING_BYTE, SHARED_SIZE+2,
                          "PENDING|RESERVED|SHARED");
  }
  if( nLock==0 && isWal!=0 ){
    /* lookup the file descriptor for the shared memory file if we have it open
    ** in this process */
    unixEnterMutex(); /* Because pFile->pInode is shared across threads */
    unixShmNode *pShmNode = pFile->pInode->pShmNode;
    if( pShmNode ){







>











>
>
>
>
>
>
>
>
>
>
>















<
<
<





|
<







6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872



6873
6874
6875
6876
6877
6878

6879
6880
6881
6882
6883
6884
6885
*/
static int unixLockstatePid(unixFile *pFile, pid_t pid, int *pLockstate){
  int hDb;        /* File descriptor for the open database file */
  int hShm = -1;  /* File descriptor for WAL shared-memory file */
  ssize_t got;    /* Bytes read from header */
  int isWal = 0;             /* True if in WAL mode */
  int nLock = 0;             /* Number of locks held */
  int npLock = 0;            /* Number of potential locks held */
  int noHdr = 0;             /* Zero byte DB has no header */
  unsigned char aHdr[100];   /* Database header */
  
  assert(pLockstate);
  
  /* make sure we are dealing with a database file */
  hDb = pFile->h;
  if( hDb<0 ){
    *pLockstate = SQLITE_LOCKSTATE_ERROR;
    return SQLITE_ERROR;
  }
  
  /* First check for an exclusive lock */
  nLock += unixIsLocked(pid, hDb, F_RDLCK, SHARED_FIRST, SHARED_SIZE,
                        "EXCLUSIVE");
  npLock += unixIsLocked(pid, hDb, F_WRLCK, PENDING_BYTE, SHARED_SIZE+2,
                        "PENDING|RESERVED|SHARED");
  if( nLock==0 && npLock==0 ){
    *pLockstate = SQLITE_LOCKSTATE_OFF;
    return SQLITE_OK;
  }
  
  assert( (strlen(SQLITE_FILE_HEADER)+1)==SQLITE_FILE_HEADER_LEN );
  got = pread(hDb, aHdr, 100, 0);
  if( got<0 ){
    *pLockstate = SQLITE_LOCKSTATE_ERROR;
    return SQLITE_ERROR;
  }
  if( got==0 ){
    noHdr = 1;
  }else if( got!=100
         || memcmp(aHdr, SQLITE_FILE_HEADER, SQLITE_FILE_HEADER_LEN)!=0
  ){
    *pLockstate = SQLITE_LOCKSTATE_NOTADB;
    return SQLITE_NOTADB;
  }
  



  if (!noHdr) {
    isWal = aHdr[18]==2;
  }
  if( nLock==0 && isWal==0 ){
    /* Rollback mode */
    nLock += npLock;

  }
  if( nLock==0 && isWal!=0 ){
    /* lookup the file descriptor for the shared memory file if we have it open
    ** in this process */
    unixEnterMutex(); /* Because pFile->pInode is shared across threads */
    unixShmNode *pShmNode = pFile->pInode->pShmNode;
    if( pShmNode ){
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
      /* the shared memory file isn't open in this process space, open our
      ** own FD */
      char zShm[MAXPATHLEN];
      
      /* WAL mode */
      strlcpy(zShm, pFile->zPath, MAXPATHLEN);
      strlcat(zShm, "-shm", MAXPATHLEN);
      hShm = open(zShm, O_RDONLY, 0);
      if( hShm<0 ){
        *pLockstate = SQLITE_LOCKSTATE_OFF;
        unixLeaveMutex();
        return SQLITE_OK;
      }
      if( unixIsLocked(pid, hShm, F_RDLCK, SHM_RECOVER, 1, "WAL-RECOVERY") ||
         unixIsLocked(pid, hShm, F_RDLCK, SHM_WRITE, 1, "WAL-WRITE") ){
        nLock = 1;
      }
      close(hShm);
    }
    unixLeaveMutex();
  }
  if( nLock>0 ){
    *pLockstate = SQLITE_LOCKSTATE_ON;
  } else {
    *pLockstate = SQLITE_LOCKSTATE_OFF;







|









|







6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
      /* the shared memory file isn't open in this process space, open our
      ** own FD */
      char zShm[MAXPATHLEN];
      
      /* WAL mode */
      strlcpy(zShm, pFile->zPath, MAXPATHLEN);
      strlcat(zShm, "-shm", MAXPATHLEN);
      hShm = robust_open2(zShm, O_RDONLY, 0, 0, 1);
      if( hShm<0 ){
        *pLockstate = SQLITE_LOCKSTATE_OFF;
        unixLeaveMutex();
        return SQLITE_OK;
      }
      if( unixIsLocked(pid, hShm, F_RDLCK, SHM_RECOVER, 1, "WAL-RECOVERY") ||
         unixIsLocked(pid, hShm, F_RDLCK, SHM_WRITE, 1, "WAL-WRITE") ){
        nLock = 1;
      }
      osClose(hShm);
    }
    unixLeaveMutex();
  }
  if( nLock>0 ){
    *pLockstate = SQLITE_LOCKSTATE_ON;
  } else {
    *pLockstate = SQLITE_LOCKSTATE_OFF;
6549
6550
6551
6552
6553
6554
6555

6556
6557
6558
6559
6560
6561













6562


6563
6564
6565
6566
6567
6568
6569





6570
6571
6572
6573
6574
6575
6576
     0,
     0,
     "/var/tmp",
     "/usr/tmp",
     "/tmp",
     0        /* List terminator */
  };

  unsigned int i;
  struct stat buf;
  const char *zDir = 0;

  azDirs[0] = sqlite3_temp_directory;
  if( !azDirs[1] ) azDirs[1] = getenv("SQLITE_TMPDIR");













  if( !azDirs[2] ) azDirs[2] = getenv("TMPDIR");


  for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
    if( zDir==0 ) continue;
    if( osStat(zDir, &buf) ) continue;
    if( !S_ISDIR(buf.st_mode) ) continue;
    if( osAccess(zDir, 07) ) continue;
    break;
  }





  return zDir;
}

/*
** Create a temporary file name in zBuf.  zBuf must be allocated
** by the calling process and must be big enough to hold at least
** pVfs->mxPathname bytes.







>






>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>




|


>
>
>
>
>







7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
     0,
     0,
     "/var/tmp",
     "/usr/tmp",
     "/tmp",
     0        /* List terminator */
  };
  
  unsigned int i;
  struct stat buf;
  const char *zDir = 0;

  azDirs[0] = sqlite3_temp_directory;
  if( !azDirs[1] ) azDirs[1] = getenv("SQLITE_TMPDIR");
  if( !azDirs[2] ){
#if defined(__APPLE__)
    char *path = malloc(PATH_MAX);
    size_t pathLen = 0;
    path[0] = '\0';
    pathLen = confstr(_CS_DARWIN_USER_TEMP_DIR, path, PATH_MAX);
    if (pathLen > 0) {
      azDirs[2] = path;
    } else {
      free(path);
      path = NULL;
    }
#else
    azDirs[2] = getenv("TMPDIR");
#endif
  }
  for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
    if( zDir==0 ) continue;
    if( osStat(zDir, &buf) ) continue;
    if( !S_ISDIR(buf.st_mode) ) continue;
    if( osAccess(zDir, R_OK|W_OK|X_OK) ) continue;
    break;
  }
  
  if (!zDir) {
    _open_error_asl_log();
    asl_log(errorlog_client, NULL, ASL_LEVEL_CRIT, "SQLite: No temporary directory is accessible.  This is not a legitimate configuration.  Either the process environment or its sandbox is misconfigured.  Various library routines will error out abruptly.\n");
  }
  return zDir;
}

/*
** Create a temporary file name in zBuf.  zBuf must be allocated
** by the calling process and must be big enough to hold at least
** pVfs->mxPathname bytes.
6751
6752
6753
6754
6755
6756
6757












































































































6758
6759
6760
6761
6762
6763
6764
    }
  }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
    *pMode = 0600;
  }
  return rc;
}













































































































/*
** Open the file zPath.
** 
** Previously, the SQLite OS layer used three functions in place of this
** one:
**
**     sqlite3OsOpenReadWrite();







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
    }
  }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
    *pMode = 0600;
  }
  return rc;
}

  
#if defined(__APPLE__)
  
static int _copyUserHomeDirectory(char* userhomedirectory, int bufferSize) {
  uid_t euid;
  gid_t gid;
  
  if (0 != pthread_getugid_np(&euid, &gid)) {
    euid = 0;
  }

  int bufsize;
  if ((bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)) == -1) {
      return 0;
  }
  char buffer[bufsize];
  struct passwd upwd, *result = NULL;
    
  if (getpwuid_r((euid ? euid : getuid()), &upwd, buffer, bufsize, &result) != 0 || !result) {
      return 0;
  }

  const char *homePath = NULL;
    
  if (upwd.pw_dir && (strlen(upwd.pw_dir)) > 0) {
    homePath = upwd.pw_dir;
  }
  if (!homePath) {
    char* t = getenv("HOME");
    if (t && (strlen(t) > 0)) {
      homePath = t;
    }
  }

  if (homePath) {
    strlcpy(userhomedirectory, homePath, bufferSize);
    return 1;
  }
  
  return 0;
}
  
  
static int _canSafelyMapFileDescriptor(struct statfs *fs) {
#if (!defined(TARGET_OS_EMBEDDED)||(TARGET_OS_EMBEDDED==0))
  static int userhomedirinited = 0;
  static fsid_t *userhomedirid = NULL;
  static fsid_t underlyinghomedirid;
  static OSSpinLock underlyinghomelock = OS_SPINLOCK_INIT;
  
  //Network volumes are never safe to mmap
  int isNetworkVolume = (MNT_LOCAL != (fs->f_flags & MNT_LOCAL));
  if (isNetworkVolume) {
    return 0;
  }
  
  //The root volume shouldn't be removeable
  int isRootVolume = (MNT_ROOTFS == (fs->f_flags & MNT_ROOTFS));
  if (isRootVolume) {
    return 1;
  }

  OSMemoryBarrier();
  if (userhomedirinited == 0) {
    OSSpinLockLock(&underlyinghomelock);
    
    char apath[MAXPATHLEN+1];
    bzero(apath, MAXPATHLEN+1);

    if (getuid() != 0) {
      if (0 != _copyUserHomeDirectory(apath, MAXPATHLEN)) {
        if (strlen(apath) > 0) {
          struct statfs userfs;
          
          if (statfs(apath, &userfs) == 0) {
            if (0 != (userfs.f_flags & MNT_LOCAL)) {
              fsid_t tests;
              bzero(&tests, sizeof(fsid_t));

              if (memcmp(&tests, &(userfs.f_fsid), sizeof(fsid_t)) != 0) {
                memcpy(&underlyinghomedirid, &(userfs.f_fsid), sizeof(fsid_t));
                
                // if the user is not root, their home directory is local and the f_fsid field is accessible
                userhomedirid = &underlyinghomedirid;
              }
            }
          }
        }
      }
    }
    
    userhomedirinited = 1;

    OSSpinLockUnlock(&underlyinghomelock);
  }
  
  if (userhomedirid != NULL) {
    if (memcmp(userhomedirid, &(fs->f_fsid), sizeof(fsid_t)) == 0) {
      return 1;
    }
  }
  return 0;
#else
  return 1;
#endif
  }
#endif
  
/*
** Open the file zPath.
** 
** Previously, the SQLite OS layer used three functions in place of this
** one:
**
**     sqlite3OsOpenReadWrite();
6791
6792
6793
6794
6795
6796
6797

6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810


6811
6812
6813
6814
6815
6816
6817
  int eType = flags&0xFF0FFF00;  /* Type of file to open */
#else
  int eType = flags&0xFFFFFF00;  /* Type of file to open */
#endif
  int noLock;                    /* True to omit locking primitives */
  int rc = SQLITE_OK;            /* Function Return Code */
  int ctrlFlags = 0;             /* UNIXFILE_* flags */


  int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
  int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
  int isCreate     = (flags & SQLITE_OPEN_CREATE);
  int isReadonly   = (flags & SQLITE_OPEN_READONLY);
  int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
#if SQLITE_ENABLE_LOCKING_STYLE
  int isAutoProxy  = (flags & SQLITE_OPEN_AUTOPROXY);
#endif
#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
  struct statfs fsInfo;
#endif



  /* If creating a master or main-file journal, this function will open
  ** a file-descriptor on the directory too. The first time unixSync()
  ** is called the directory file descriptor will be fsync()ed and close()d.
  */
  int syncDir = (isCreate && (
        eType==SQLITE_OPEN_MASTER_JOURNAL 
     || eType==SQLITE_OPEN_MAIN_JOURNAL 







>












|
>
>







8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
  int eType = flags&0xFF0FFF00;  /* Type of file to open */
#else
  int eType = flags&0xFFFFFF00;  /* Type of file to open */
#endif
  int noLock;                    /* True to omit locking primitives */
  int rc = SQLITE_OK;            /* Function Return Code */
  int ctrlFlags = 0;             /* UNIXFILE_* flags */
  int protClass = 0;

  int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
  int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
  int isCreate     = (flags & SQLITE_OPEN_CREATE);
  int isReadonly   = (flags & SQLITE_OPEN_READONLY);
  int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
#if SQLITE_ENABLE_LOCKING_STYLE
  int isAutoProxy  = (flags & SQLITE_OPEN_AUTOPROXY);
#endif
#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
  struct statfs fsInfo;
#endif
  
  int isShm = 0;
  
  /* If creating a master or main-file journal, this function will open
  ** a file-descriptor on the directory too. The first time unixSync()
  ** is called the directory file descriptor will be fsync()ed and close()d.
  */
  int syncDir = (isCreate && (
        eType==SQLITE_OPEN_MASTER_JOURNAL 
     || eType==SQLITE_OPEN_MAIN_JOURNAL 
6902
6903
6904
6905
6906
6907
6908

6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932





6933




6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948












6949
6950
6951
6952
6953
6954
6955
  if( isReadWrite ) openFlags |= O_RDWR;
  if( isCreate )    openFlags |= O_CREAT;
  if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
  openFlags |= (O_LARGEFILE|O_BINARY);

#if SQLITE_ENABLE_DATA_PROTECTION
  p->protFlags = (flags & SQLITE_OPEN_FILEPROTECTION_MASK);

#endif
    
  if( fd<0 ){
    mode_t openMode;              /* Permissions to create file with */
    uid_t uid;                    /* Userid for the file */
    gid_t gid;                    /* Groupid for the file */
    rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
    if( rc!=SQLITE_OK ){
      assert( !p->pUnused );
      assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
      return rc;
    }
    fd = robust_open(zName, openFlags, openMode);
    OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
    if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
      /* Failed to open the file for read/write access. Try read-only. */
      flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
      openFlags &= ~(O_RDWR|O_CREAT);
      flags |= SQLITE_OPEN_READONLY;
      openFlags |= O_RDONLY;
      isReadonly = 1;
      fd = robust_open(zName, openFlags, openMode);
    }
    if( fd<0 ){





      rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);




      goto open_finished;
    }

    /* if we're opening the wal or journal and running as root, set the
    ** journal uid/gid */
    if( !isReadonly && (flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL)) ){
      uid_t euid = geteuid();
      if( euid==0 && (euid!=uid || getegid()!=gid) ){
        if( osFchown(fd, uid, gid) ){
          rc = SQLITE_CANTOPEN_BKPT;
          goto open_finished;
        }
      }
    }
  }












  assert( fd>=0 );
  if( pOutFlags ){
    *pOutFlags = flags;
  }

  if( p->pUnused ){
    p->pUnused->fd = fd;







>












|








|


>
>
>
>
>
|
>
>
>
>















>
>
>
>
>
>
>
>
>
>
>
>







8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
  if( isReadWrite ) openFlags |= O_RDWR;
  if( isCreate )    openFlags |= O_CREAT;
  if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
  openFlags |= (O_LARGEFILE|O_BINARY);

#if SQLITE_ENABLE_DATA_PROTECTION
  p->protFlags = (flags & SQLITE_OPEN_FILEPROTECTION_MASK);
  protClass = (!isShm) ? unixProtectionClassForVFSProtectionFlags((flags & SQLITE_OPEN_FILEPROTECTION_MASK)) : shmUnixProtectionClassForVFSProtectionFlags((flags & SQLITE_OPEN_FILEPROTECTION_MASK));
#endif
    
  if( fd<0 ){
    mode_t openMode;              /* Permissions to create file with */
    uid_t uid;                    /* Userid for the file */
    gid_t gid;                    /* Groupid for the file */
    rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
    if( rc!=SQLITE_OK ){
      assert( !p->pUnused );
      assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
      return rc;
    }
    fd = robust_open2(zName, openFlags, openMode, protClass, isShm);
    OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
    if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
      /* Failed to open the file for read/write access. Try read-only. */
      flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
      openFlags &= ~(O_RDWR|O_CREAT);
      flags |= SQLITE_OPEN_READONLY;
      openFlags |= O_RDONLY;
      isReadonly = 1;
      fd = robust_open2(zName, openFlags, openMode, protClass, isShm);
    }
    if( fd<0 ){
#if SQLITE_ENABLE_DATA_PROTECTION
      if( errno==EPERM ){
        /* <rdar://problem/9385939> Test device locked state when error reporting for content protection issues */
        rc = unixLogError(SQLITE_AUTH, "open", zName);
      }else{
        rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
      }
#else
      rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
#endif
      goto open_finished;
    }

    /* if we're opening the wal or journal and running as root, set the
    ** journal uid/gid */
    if( !isReadonly && (flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL)) ){
      uid_t euid = geteuid();
      if( euid==0 && (euid!=uid || getegid()!=gid) ){
        if( osFchown(fd, uid, gid) ){
          rc = SQLITE_CANTOPEN_BKPT;
          goto open_finished;
        }
      }
    }
  }
#if SQLITE_ENABLE_DATA_PROTECTION
  if( !isReadonly ){
    rc = unixSetFileProtection(fd, p->protFlags, isShm);
    if( rc ){
      if( rc!=SQLITE_AUTH ){
        rc =SQLITE_CANTOPEN_BKPT;
      }
      osClose(fd);
      goto open_finished;
    }
  }
#endif
  assert( fd>=0 );
  if( pOutFlags ){
    *pOutFlags = flags;
  }

  if( p->pUnused ){
    p->pUnused->fd = fd;
6980
6981
6982
6983
6984
6985
6986













6987
6988
6989
6990
6991
6992
6993
  }
  if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
    ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
  }
  if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) {
    ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
  }













#endif

  /* Set up appropriate ctrlFlags */
  if( isDelete )                ctrlFlags |= UNIXFILE_DELETE;
  if( isReadonly )              ctrlFlags |= UNIXFILE_RDONLY;
  if( noLock )                  ctrlFlags |= UNIXFILE_NOLOCK;
  if( syncDir )                 ctrlFlags |= UNIXFILE_DIRSYNC;







>
>
>
>
>
>
>
>
>
>
>
>
>







8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
  }
  if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
    ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
  }
  if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) {
    ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
  }
  if (fsInfo.f_flags & MNT_ROOTFS) {
    ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MMAP_SAFE;
  } else {
#if defined(__APPLE__)
    if (_canSafelyMapFileDescriptor(&fsInfo)) {
      ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MMAP_SAFE;
    }
#endif
  }
#if defined(__APPLE__)
  /* <rdar://problem/17742712> */
  ((unixFile*)pFile)->nFsBytes = (i64)fsInfo.f_blocks * (i64)fsInfo.f_bsize;
#endif
#endif

  /* Set up appropriate ctrlFlags */
  if( isDelete )                ctrlFlags |= UNIXFILE_DELETE;
  if( isReadonly )              ctrlFlags |= UNIXFILE_RDONLY;
  if( noLock )                  ctrlFlags |= UNIXFILE_NOLOCK;
  if( syncDir )                 ctrlFlags |= UNIXFILE_DIRSYNC;
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
    int useProxy = 0;

    /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means 
    ** never use proxy, NULL means use proxy for non-local files only.  */
    if( envforce!=NULL ){
      useProxy = atoi(envforce)>0;
    }else{
      useProxy = !(fsInfo.f_flags&MNT_LOCAL);
    }
    if( useProxy ){
      rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
      if( rc==SQLITE_OK ){
        /* cache the pMethod in case the transform fails */
        const struct sqlite3_io_methods *pMethod = pFile->pMethods;
        rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");







|







8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
    int useProxy = 0;

    /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means 
    ** never use proxy, NULL means use proxy for non-local files only.  */
    if( envforce!=NULL ){
      useProxy = atoi(envforce)>0;
    }else{
      useProxy = !((fsInfo.f_flags&MNT_LOCAL) || (fsInfo.f_flags&MNT_RDONLY));
    }
    if( useProxy ){
      rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
      if( rc==SQLITE_OK ){
        /* cache the pMethod in case the transform fails */
        const struct sqlite3_io_methods *pMethod = pFile->pMethods;
        rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
#else
      if( fsync(fd) )
#endif
      {
        rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
      }
#if OSCLOSE_CHECK_CLOSE_IOERR
      if( close(fd)&&!rc ){
        rc = SQLITE_IOERR_DIR_CLOSE;
      }
#else
      robust_close(0, fd, __LINE__);
#endif
    }else if( rc==SQLITE_CANTOPEN ){
      rc = SQLITE_OK;







|







8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
#else
      if( fsync(fd) )
#endif
      {
        rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
      }
#if OSCLOSE_CHECK_CLOSE_IOERR
      if( osClose(fd)&&!rc ){
        rc = SQLITE_IOERR_DIR_CLOSE;
      }
#else
      robust_close(0, fd, __LINE__);
#endif
    }else if( rc==SQLITE_CANTOPEN ){
      rc = SQLITE_OK;
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195


#ifndef SQLITE_OMIT_LOAD_EXTENSION
/*
** Interfaces for opening a shared library, finding entry points
** within the shared library, and closing the shared library.
*/
#include <dlfcn.h>
static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
  UNUSED_PARAMETER(NotUsed);
  return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
}

/*
** SQLite calls this function immediately after a call to unixDlSym() or







|







8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453


#ifndef SQLITE_OMIT_LOAD_EXTENSION
/*
** Interfaces for opening a shared library, finding entry points
** within the shared library, and closing the shared library.
*/
#include <dlfcn.h> /* amalgamator: keep */
static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
  UNUSED_PARAMETER(NotUsed);
  return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
}

/*
** SQLite calls this function immediately after a call to unixDlSym() or
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
  ** tests repeatable.
  */
  memset(zBuf, 0, nBuf);
  randomnessPid = getpid();  
#if !defined(SQLITE_TEST)
  {
    int fd, got;
    fd = robust_open("/dev/urandom", O_RDONLY, 0);
    if( fd<0 ){
      time_t t;
      time(&t);
      memcpy(zBuf, &t, sizeof(t));
      memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid));
      assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf );
      nBuf = sizeof(t) + sizeof(randomnessPid);
    }else{
      do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR );
      robust_close(0, fd, __LINE__);
    }
  }
#endif
  return nBuf;
}








|








|







8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
  ** tests repeatable.
  */
  memset(zBuf, 0, nBuf);
  randomnessPid = getpid();  
#if !defined(SQLITE_TEST)
  {
    int fd, got;
    fd = robust_open("/dev/urandom", O_RDONLY, 0, 0);
    if( fd<0 ){
      time_t t;
      time(&t);
      memcpy(zBuf, &t, sizeof(t));
      memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid));
      assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf );
      nBuf = sizeof(t) + sizeof(randomnessPid);
    }else{
      do{ got = (int)osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR );
      robust_close(0, fd, __LINE__);
    }
  }
#endif
  return nBuf;
}

7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577

7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598

7599





7600














































7601
7602







7603

7604
7605



7606
7607
7608
7609
7610
7611
7612
7613
7614
};

/* 
** The proxy lock file path for the database at dbPath is written into lPath, 
** which must point to valid, writable memory large enough for a maxLen length
** file path. 
*/
static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
  int len;
  int dbLen;
  int i;


#ifdef LOCKPROXYDIR
  len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
#else
# ifdef _CS_DARWIN_USER_TEMP_DIR
  {
    if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
      OSTRACE(("GETLOCKPATH  failed %s errno=%d pid=%d\n",
               lPath, errno, getpid()));
      return SQLITE_IOERR_LOCK;
    }
    len = strlcat(lPath, "sqliteplocks", maxLen);    
  }
# else
  len = strlcpy(lPath, "/tmp/", maxLen);
# endif
#endif

  if( lPath[len-1]!='/' ){
    len = strlcat(lPath, "/", maxLen);
  }
  

  /* transform the db path to a unique cache name */





  dbLen = (int)strlen(dbPath);














































  for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
    char c = dbPath[i];







    lPath[i+len] = (c=='/')?'_':c;

  }
  lPath[i+len]='\0';



  strlcat(lPath, ":auto:", maxLen);
  OSTRACE(("GETLOCKPATH  proxy lock path=%s pid=%d\n", lPath, getpid()));
  return SQLITE_OK;
}

/* 
 ** Creates the lock file and any missing directories in lockPath
 */
static int proxyCreateLockPath(const char *lockPath){







|



|
>

|



|

|


|


|



|
|

|
>
|
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
>
>
>
>
>
>
>
|
>
|
|
>
>
>
|
|







8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
};

/* 
** The proxy lock file path for the database at dbPath is written into lPath, 
** which must point to valid, writable memory large enough for a maxLen length
** file path. 
*/
static int proxyGetLockPath(const char *dbPath, char *lockPath, size_t maxLen){
  int len;
  int dbLen;
  int i;
  int tooBig;
  int nameMax;
#ifdef LOCKPROXYDIR
  len = strlcpy(lockPath, LOCKPROXYDIR, maxLen);
#else
# ifdef _CS_DARWIN_USER_TEMP_DIR
  {
    if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lockPath, maxLen) ){
      OSTRACE(("GETLOCKPATH  failed %s errno=%d pid=%d\n",
               lockPath, errno, getpid()));
      return SQLITE_IOERR_LOCK;
    }
    len = (int)strlcat(lockPath, "sqliteplocks", maxLen);
  }
# else
  len = strlcpy(lockPath, "/tmp/", maxLen);
# endif
#endif

  if( lockPath[len-1]!='/' ){
    len = (int)strlcat(lockPath, "/", maxLen);
  }
    
  /* 
  ** transform the db path to a unique cache name.
  ** By default this simply turns '/'s into '_'s,
  ** but if the resulting path would exceed maxLen, hash the
  ** path and create a subpath like:
  **   <SHA-256 hash>-<database filename>:auto:
  */
  dbLen = (int)strlen(dbPath);
  tooBig = ( (dbLen+len+7) > maxLen ) ? 1 : 0;
#if defined(NAME_MAX)
  nameMax = NAME_MAX;
#else
  nameMax = 255;
#endif
  
#if defined(__APPLE__)
  if( tooBig || (nameMax<(dbLen+7)) ){
    CC_SHA256_CTX ctx;
    unsigned char shabytes[CC_SHA256_DIGEST_LENGTH];
    char shahex[CC_SHA256_DIGEST_LENGTH * 2 + 2];
    
    CC_SHA256_Init (&ctx);
    CC_SHA256_Update (&ctx, dbPath, dbLen);
    CC_SHA256_Final (shabytes, &ctx);
    
    /* render the hash into the path as chars [0-9][a-f] */
    for( i=0; i<CC_SHA256_DIGEST_LENGTH; i++ ){
      unsigned char byte = shabytes[i];
      shahex[i*2] = '0' + (byte >> 4);
      if (shahex[i*2] > '9'){
        shahex[i*2] += 'a' - '9' - 1;
      }
      shahex[i*2+1] = '0' + (byte & 0xf);
      if (shahex[i*2+1] > '9'){
        shahex[i*2+1] += 'a' - '9' - 1;
      }
    }
    shahex[CC_SHA256_DIGEST_LENGTH * 2] = '-';
    shahex[CC_SHA256_DIGEST_LENGTH * 2 + 1] = 0;
    
    strlcat(lockPath, shahex, maxLen);
    
    /* tack on the actual database name for discoverability */
    const char *dbName = strrchr (dbPath, '/');
    if (!dbName){
      dbName = dbPath;
    }else{
      ++dbName;
    }
    
    strlcat(lockPath, dbName, maxLen);
  }else{
#endif
    int pos = len;
    for( i=0; i<dbLen; i++){
      char c = dbPath[i];
      /* collapse "./" and "/./" patterns */
      if( (dbLen>(i+2)) && (dbPath[i]=='.') && (dbPath[i+1]=='/') &&
         ((i==0) || dbPath[i-1]=='/') ){
        i ++; /* skips 2 */
        continue;
      }
      
      lockPath[pos] = (c=='/')?'_':c;
      pos++;
    }
    lockPath[pos]='\0';
#if defined(__APPLE__)
  }
#endif
  strlcat(lockPath, ":auto:", maxLen);
  OSTRACE(("GETLOCKPATH  proxy lock path=%s pid=%d\n", lockPath, getpid()));
  return SQLITE_OK;
}

/* 
 ** Creates the lock file and any missing directories in lockPath
 */
static int proxyCreateLockPath(const char *lockPath){
7659
7660
7661
7662
7663
7664
7665


7666
7667
7668


7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688

7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718

7719
7720
7721
7722
7723
7724
7725
7726
  proxyLockingContext *pCtx;
  unixFile *pLockFile;
  
  assert(pFile!=NULL && pFile->lockingContext!=NULL);
  assert(pFile->pMethod == &proxyIoMethods);
  pCtx = ((proxyLockingContext *)(pFile->lockingContext));
  pLockFile = pCtx->lockProxy;


  if( pLockFile->pMethod->iVersion>=2 && pLockFile->pMethod->xShmMap!=0 ){
    return pCtx->lockProxyPath;
  }


  return NULL;
}
#endif

/*
** Create a new VFS file descriptor (stored in memory obtained from
** sqlite3_malloc) and open the file named "path" in the file descriptor.
**
** The caller is responsible not only for closing the file descriptor
** but also for freeing the memory associated with the file descriptor.
*/
static int proxyCreateUnixFile(
    const char *path,        /* path for the new unixFile */
    unixFile **ppFile,       /* unixFile created and returned by ref */
    int islockfile           /* if non zero missing dirs will be created */
) {
  int fd = -1;
  unixFile *pNew;
  int rc = SQLITE_OK;
  int openFlags = O_RDWR | O_CREAT;

  sqlite3_vfs dummyVfs;
  int terrno = 0;
  UnixUnusedFd *pUnused = NULL;

  /* 1. first try to open/create the file
  ** 2. if that fails, and this is a lock file (not-conch), try creating
  ** the parent directories and then try again.
  ** 3. if that fails, try to open the file read-only
  ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
  */
  pUnused = findReusableFd(path, openFlags);
  if( pUnused ){
    fd = pUnused->fd;
  }else{
    pUnused = sqlite3_malloc(sizeof(*pUnused));
    if( !pUnused ){
      return SQLITE_NOMEM;
    }
  }
  if( fd<0 ){
    fd = robust_open(path, openFlags, 0);
    terrno = errno;
    if( fd<0 && errno==ENOENT && islockfile ){
      if( proxyCreateLockPath(path) == SQLITE_OK ){
        fd = robust_open(path, openFlags, 0);
      }
    }
  }
  if( fd<0 ){
    openFlags = O_RDONLY;

    fd = robust_open(path, openFlags, 0);
    terrno = errno;
  }
  if( fd<0 ){
    sqlite3_free(pUnused);
    if( islockfile ){
      return SQLITE_BUSY;
    }







>
>
|
|
|
>
>




















>










|









|



|





>
|







8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
  proxyLockingContext *pCtx;
  unixFile *pLockFile;
  
  assert(pFile!=NULL && pFile->lockingContext!=NULL);
  assert(pFile->pMethod == &proxyIoMethods);
  pCtx = ((proxyLockingContext *)(pFile->lockingContext));
  pLockFile = pCtx->lockProxy;
  
  if (pLockFile) {
    if( pLockFile->pMethod->iVersion>=2 && pLockFile->pMethod->xShmMap!=0 ){
      return pCtx->lockProxyPath;
    }
  }

  return NULL;
}
#endif

/*
** Create a new VFS file descriptor (stored in memory obtained from
** sqlite3_malloc) and open the file named "path" in the file descriptor.
**
** The caller is responsible not only for closing the file descriptor
** but also for freeing the memory associated with the file descriptor.
*/
static int proxyCreateUnixFile(
    const char *path,        /* path for the new unixFile */
    unixFile **ppFile,       /* unixFile created and returned by ref */
    int islockfile           /* if non zero missing dirs will be created */
) {
  int fd = -1;
  unixFile *pNew;
  int rc = SQLITE_OK;
  int openFlags = O_RDWR | O_CREAT;
  int sqliteFlags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
  sqlite3_vfs dummyVfs;
  int terrno = 0;
  UnixUnusedFd *pUnused = NULL;

  /* 1. first try to open/create the file
  ** 2. if that fails, and this is a lock file (not-conch), try creating
  ** the parent directories and then try again.
  ** 3. if that fails, try to open the file read-only
  ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
  */
  pUnused = findReusableFd(path, sqliteFlags);
  if( pUnused ){
    fd = pUnused->fd;
  }else{
    pUnused = sqlite3_malloc(sizeof(*pUnused));
    if( !pUnused ){
      return SQLITE_NOMEM;
    }
  }
  if( fd<0 ){
    fd = robust_open2(path, openFlags, 0, 0,0);
    terrno = errno;
    if( fd<0 && errno==ENOENT && islockfile ){
      if( proxyCreateLockPath(path) == SQLITE_OK ){
        fd = robust_open2(path, openFlags, 0, 0, 0);
      }
    }
  }
  if( fd<0 ){
    openFlags = O_RDONLY;
    sqliteFlags = SQLITE_OPEN_READONLY;
    fd = robust_open2(path, openFlags, 0, 0, 0);
    terrno = errno;
  }
  if( fd<0 ){
    sqlite3_free(pUnused);
    if( islockfile ){
      return SQLITE_BUSY;
    }
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
  }
  memset(pNew, 0, sizeof(unixFile));
  pNew->openFlags = openFlags;
  memset(&dummyVfs, 0, sizeof(dummyVfs));
  dummyVfs.pAppData = (void*)&autolockIoFinder;
  dummyVfs.zName = "dummy";
  pUnused->fd = fd;
  pUnused->flags = openFlags;
  pNew->pUnused = pUnused;
  
  rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
  if( rc==SQLITE_OK ){
    *ppFile = pNew;
    return SQLITE_OK;
  }







|







9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
  }
  memset(pNew, 0, sizeof(unixFile));
  pNew->openFlags = openFlags;
  memset(&dummyVfs, 0, sizeof(dummyVfs));
  dummyVfs.pAppData = (void*)&autolockIoFinder;
  dummyVfs.zName = "dummy";
  pUnused->fd = fd;
  pUnused->flags = sqliteFlags;
  pNew->pUnused = pUnused;
  
  rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
  if( rc==SQLITE_OK ){
    *ppFile = pNew;
    return SQLITE_OK;
  }
7763
7764
7765
7766
7767
7768
7769

7770
7771

7772
7773
7774
7775
7776
7777
7778
7779
#ifdef SQLITE_TEST
/* simulate multiple hosts by creating unique hostid file paths */
int sqlite3_hostid_num = 0;
#endif

#define PROXY_HOSTIDLEN    16  /* conch file host id length */


/* Not always defined in the headers as it ought to be */
extern int gethostuuid(uuid_t id, const struct timespec *wait);


/* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN 
** bytes of writable memory.
*/
static int proxyGetHostID(unsigned char *pHostID, int *pError){
  assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
  memset(pHostID, 0, PROXY_HOSTIDLEN);
#if HAVE_GETHOSTUUID







>


>
|







9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
#ifdef SQLITE_TEST
/* simulate multiple hosts by creating unique hostid file paths */
int sqlite3_hostid_num = 0;
#endif

#define PROXY_HOSTIDLEN    16  /* conch file host id length */

#if HAVE_GETHOSTUUID
/* Not always defined in the headers as it ought to be */
extern int gethostuuid(uuid_t id, const struct timespec *wait);
#endif
  
/* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN 
** bytes of writable memory.
*/
static int proxyGetHostID(unsigned char *pHostID, int *pError){
  assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
  memset(pHostID, 0, PROXY_HOSTIDLEN);
#if HAVE_GETHOSTUUID
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
  /* read the conch content */
  readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
  if( readLen<PROXY_PATHINDEX ){
    sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
    goto end_breaklock;
  }
  /* write it out to the temporary break file */
  fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0);
  if( fd<0 ){
    sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
    goto end_breaklock;
  }
  if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
    sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
    goto end_breaklock;







|







9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
  /* read the conch content */
  readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
  if( readLen<PROXY_PATHINDEX ){
    sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
    goto end_breaklock;
  }
  /* write it out to the temporary break file */
  fd = robust_open2(tPath, (O_RDWR|O_CREAT|O_EXCL), 0, 0, 0);
  if( fd<0 ){
    sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
    goto end_breaklock;
  }
  if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
    sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
    goto end_breaklock;
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
      if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || 
         conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
        return SQLITE_BUSY;
      }
      
      if( pCtx->nFails==2 ){  
        char tBuf[PROXY_MAXCONCHLEN];
        int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
        if( len<0 ){
          storeLastErrno(pFile, errno);
          return SQLITE_IOERR_LOCK;
        }
        if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
          /* don't break the lock if the host id doesn't match, but do log
           * an error to console so users can diagnose stale NFS locks more 







|







9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
      if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || 
         conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
        return SQLITE_BUSY;
      }
      
      if( pCtx->nFails==2 ){  
        char tBuf[PROXY_MAXCONCHLEN];
        int len = (int)osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
        if( len<0 ){
          storeLastErrno(pFile, errno);
          return SQLITE_IOERR_LOCK;
        }
        if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
          /* don't break the lock if the host id doesn't match, but do log
           * an error to console so users can diagnose stale NFS locks more 
8066
8067
8068
8069
8070
8071
8072





8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
      ** stick.
      */
      futimes(conchFile->h, NULL);
      if( hostIdMatch && !createConch ){
        if( conchFile->pInode && conchFile->pInode->nShared>1 ){
          /* We are trying for an exclusive lock but another thread in this
           ** same process is still holding a shared lock. */





          rc = SQLITE_BUSY;
        } else {          
          rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
        }
      }else{
        rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
      }
      if( rc==SQLITE_OK ){
        char writeBuffer[PROXY_MAXCONCHLEN];
        int writeSize = 0;
        
        writeBuffer[0] = (char)PROXY_CONCHVERSION;
        memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
        if( pCtx->lockProxyPath!=NULL ){
          strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
                  MAXPATHLEN);
        }else{
          strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
        }
        writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
        robust_ftruncate(conchFile->h, writeSize);
        rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
        fsync(conchFile->h);
        /* If we created a new conch file (not just updated the contents of a 
         ** valid conch file), try to match the permissions of the database 
         */
        if( rc==SQLITE_OK && createConch ){







>
>
>
>
>



















|







9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
      ** stick.
      */
      futimes(conchFile->h, NULL);
      if( hostIdMatch && !createConch ){
        if( conchFile->pInode && conchFile->pInode->nShared>1 ){
          /* We are trying for an exclusive lock but another thread in this
           ** same process is still holding a shared lock. */
#ifdef SQLITE_USE_FLOCKTIMEOUT
          if( pFile->nextFlockTimeout>0 ){
            usleep(((int)pFile->nextFlockTimeout) * 1000L);
          }
#endif
          rc = SQLITE_BUSY;
        } else {          
          rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
        }
      }else{
        rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
      }
      if( rc==SQLITE_OK ){
        char writeBuffer[PROXY_MAXCONCHLEN];
        int writeSize = 0;
        
        writeBuffer[0] = (char)PROXY_CONCHVERSION;
        memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
        if( pCtx->lockProxyPath!=NULL ){
          strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
                  MAXPATHLEN);
        }else{
          strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
        }
        writeSize = PROXY_PATHINDEX + (int)strlen(&writeBuffer[PROXY_PATHINDEX]);
        robust_ftruncate(conchFile->h, writeSize);
        rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
        fsync(conchFile->h);
        /* If we created a new conch file (not just updated the contents of a 
         ** valid conch file), try to match the permissions of the database 
         */
        if( rc==SQLITE_OK && createConch ){
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
      
    end_takeconch:
      OSTRACE(("TRANSPROXY: CLOSE  %d\n", pFile->h));
      if( rc==SQLITE_OK && pFile->openFlags ){
        int fd;
        if( pFile->h>=0 ){
#if defined(STRICT_CLOSE_ERROR) && OSCLOSE_CHECK_CLOSE_IOERR
          if( close(pFile->h) ){
            storeLastErrno(pFile, errno);
            return SQLITE_IOERR_CLOSE;
          }
#else
          robust_close(pFile, pFile->h, __LINE__);
#endif
        }
        pFile->h = -1;
        fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
        OSTRACE(("TRANSPROXY: OPEN  %d\n", fd));
        if( fd>=0 ){
          pFile->h = fd;
        }else{
          rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
           during locking */
        }







|








|







9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
      
    end_takeconch:
      OSTRACE(("TRANSPROXY: CLOSE  %d\n", pFile->h));
      if( rc==SQLITE_OK && pFile->openFlags ){
        int fd;
        if( pFile->h>=0 ){
#if defined(STRICT_CLOSE_ERROR) && OSCLOSE_CHECK_CLOSE_IOERR
          if( osClose(pFile->h) ){
            storeLastErrno(pFile, errno);
            return SQLITE_IOERR_CLOSE;
          }
#else
          robust_close(pFile, pFile->h, __LINE__);
#endif
        }
        pFile->h = -1;
        fd = robust_open2(pCtx->dbPath, pFile->openFlags, 0, 0, 0);
        OSTRACE(("TRANSPROXY: OPEN  %d\n", fd));
        if( fd>=0 ){
          pFile->h = fd;
        }else{
          rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
           during locking */
        }
8264
8265
8266
8267
8268
8269
8270





8271
8272
8273
8274
8275
8276
8277
*/
static int switchLockProxyPath(unixFile *pFile, const char *path) {
  proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
  char *oldPath = pCtx->lockProxyPath;
  int rc = SQLITE_OK;

  if( pFile->eFileLock!=NO_LOCK ){





    return SQLITE_BUSY;
  }  

  /* nothing to do if the path is NULL, :auto: or matches the existing path */
  if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
    (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
    return SQLITE_OK;







>
>
>
>
>







9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
*/
static int switchLockProxyPath(unixFile *pFile, const char *path) {
  proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
  char *oldPath = pCtx->lockProxyPath;
  int rc = SQLITE_OK;

  if( pFile->eFileLock!=NO_LOCK ){
#ifdef SQLITE_USE_FLOCKTIMEOUT
    if( pFile->nextFlockTimeout>0 ){
      usleep(((int)pFile->nextFlockTimeout) * 1000L);
    }
#endif
    return SQLITE_BUSY;
  }  

  /* nothing to do if the path is NULL, :auto: or matches the existing path */
  if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
    (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
    return SQLITE_OK;
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
    strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
            MAXPATHLEN);
  } else
#endif
  if( pFile->pMethod == &dotlockIoMethods ){
    /* dot lock style uses the locking context to store the dot lock
    ** file path */
    int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
    memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
  }else{
    /* all other styles use the locking context to store the db file path */
    assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
    strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
  }
  return SQLITE_OK;







|







9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
    strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
            MAXPATHLEN);
  } else
#endif
  if( pFile->pMethod == &dotlockIoMethods ){
    /* dot lock style uses the locking context to store the dot lock
    ** file path */
    long len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
    memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
  }else{
    /* all other styles use the locking context to store the db file path */
    assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
    strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
  }
  return SQLITE_OK;
8332
8333
8334
8335
8336
8337
8338





8339
8340
8341
8342
8343
8344
8345
static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
  proxyLockingContext *pCtx;
  char dbPath[MAXPATHLEN+1];       /* Name of the database file */
  char *lockPath=NULL;
  int rc = SQLITE_OK;
  
  if( pFile->eFileLock!=NO_LOCK ){





    return SQLITE_BUSY;
  }
  getDbPathForUnixFile(pFile, dbPath);
  if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
    lockPath=NULL;
  }else{
    lockPath=(char *)path;







>
>
>
>
>







9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
  proxyLockingContext *pCtx;
  char dbPath[MAXPATHLEN+1];       /* Name of the database file */
  char *lockPath=NULL;
  int rc = SQLITE_OK;
  
  if( pFile->eFileLock!=NO_LOCK ){
#ifdef SQLITE_USE_FLOCKTIMEOUT
    if( pFile->nextFlockTimeout>0 ){
      usleep(((int)pFile->nextFlockTimeout) * 1000L);
    }
#endif
    return SQLITE_BUSY;
  }
  getDbPathForUnixFile(pFile, dbPath);
  if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
    lockPath=NULL;
  }else{
    lockPath=(char *)path;

Changes to src/pager.c.

18
19
20
21
22
23
24

25
26
27
28
29
30
31
** file simultaneously, or one process from reading the database while
** another is writing.
*/
#ifndef SQLITE_OMIT_DISKIO
#include "sqliteInt.h"
#include "wal.h"



/******************* NOTES ON THE DESIGN OF THE PAGER ************************
**
** This comment block describes invariants that hold when using a rollback
** journal.  These invariants do not apply for journal_mode=WAL,
** journal_mode=MEMORY, or journal_mode=OFF.
**







>







18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
** file simultaneously, or one process from reading the database while
** another is writing.
*/
#ifndef SQLITE_OMIT_DISKIO
#include "sqliteInt.h"
#include "wal.h"

extern void pthread_yield_np(void);

/******************* NOTES ON THE DESIGN OF THE PAGER ************************
**
** This comment block describes invariants that hold when using a rollback
** journal.  These invariants do not apply for journal_mode=WAL,
** journal_mode=MEMORY, or journal_mode=OFF.
**
3788
3789
3790
3791
3792
3793
3794


3795
3796
3797






3798
3799
3800
3801
3802
3803
3804
  ** sqlite3PagerSetBusyhandler().
  */
  assert( (pPager->eLock>=locktype)
       || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
       || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
  );



  do {
    rc = pagerLockDb(pPager, locktype);
  }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );






  return rc;
}

/*
** Function assertTruncateConstraint(pPager) checks that one of the 
** following is true for all dirty pages currently in the page-cache:
**







>
>


|
>
>
>
>
>
>







3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
  ** sqlite3PagerSetBusyhandler().
  */
  assert( (pPager->eLock>=locktype)
       || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
       || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
  );

  int i = 0;
  int c = 0;
  do {
    rc = pagerLockDb(pPager, locktype);
    c = ( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
    if (c && (i > 0)) {
      pthread_yield_np();
    }
    i++;
  }while( c );
  
  return rc;
}

/*
** Function assertTruncateConstraint(pPager) checks that one of the 
** following is true for all dirty pages currently in the page-cache:
**

Changes to src/pcache1.c.

15
16
17
18
19
20
21


22
23
24
25
26
27
28
** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
** If the default page cache implementation is overriden, then neither of
** these two features are available.
*/

#include "sqliteInt.h"



typedef struct PCache1 PCache1;
typedef struct PgHdr1 PgHdr1;
typedef struct PgFreeslot PgFreeslot;
typedef struct PGroup PGroup;

/* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set 
** of one or more PCaches that are able to recycle each others unpinned







>
>







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
** If the default page cache implementation is overriden, then neither of
** these two features are available.
*/

#include "sqliteInt.h"

#if !defined(SQLITE_OMIT_PCACHE1_PCACHE)

typedef struct PCache1 PCache1;
typedef struct PgHdr1 PgHdr1;
typedef struct PgFreeslot PgFreeslot;
typedef struct PGroup PGroup;

/* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set 
** of one or more PCaches that are able to recycle each others unpinned
1025
1026
1027
1028
1029
1030
1031


  }
  *pnCurrent = pcache1.grp.nCurrentPage;
  *pnMax = (int)pcache1.grp.nMaxPage;
  *pnMin = (int)pcache1.grp.nMinPage;
  *pnRecyclable = nRecyclable;
}
#endif









>
>
1027
1028
1029
1030
1031
1032
1033
1034
1035
  }
  *pnCurrent = pcache1.grp.nCurrentPage;
  *pnMax = (int)pcache1.grp.nMaxPage;
  *pnMin = (int)pcache1.grp.nMinPage;
  *pnRecyclable = nRecyclable;
}
#endif

#endif /* SQLITE_OMIT_PCACHE1_PCACHE */

Changes to src/pragma.c.

1321
1322
1323
1324
1325
1326
1327







1328
1329
1330
1331
1332
1333
1334
      }
#endif /* SQLITE_OMIT_WSD */
    }
    break;
  }
#endif








#if SQLITE_ENABLE_LOCKING_STYLE
  /*
  **   PRAGMA [database.]lock_proxy_file
  **   PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
  **
  ** Return or set the value of the lock_proxy_file flag.  Changing
  ** the value sets a specific file to be used for database access locks.







>
>
>
>
>
>
>







1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
      }
#endif /* SQLITE_OMIT_WSD */
    }
    break;
  }
#endif

#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
#  if defined(__APPLE__)
#    define SQLITE_ENABLE_LOCKING_STYLE 1
#  else
#    define SQLITE_ENABLE_LOCKING_STYLE 0
#  endif
#endif
#if SQLITE_ENABLE_LOCKING_STYLE
  /*
  **   PRAGMA [database.]lock_proxy_file
  **   PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
  **
  ** Return or set the value of the lock_proxy_file flag.  Changing
  ** the value sets a specific file to be used for database access locks.

Changes to src/shell.c.

3716
3717
3718
3719
3720
3721
3722
3723

3724
3725
3726
3727
3728
3729
3730
3731
3732
  char *zFirstCmd = 0;
  int i;
  int rc = 0;
  int warnInmemoryDb = 0;

#if USE_SYSTEM_SQLITE+0!=1
  if( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)!=0 ){
    fprintf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",

            sqlite3_sourceid(), SQLITE_SOURCE_ID);
    exit(1);
  }
#endif
  Argv0 = argv[0];
  main_init(&data);
  stdin_is_interactive = isatty(0);

  /* Make sure we have a valid signal handler early, before anything







|
>

|







3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
  char *zFirstCmd = 0;
  int i;
  int rc = 0;
  int warnInmemoryDb = 0;

#if USE_SYSTEM_SQLITE+0!=1
  if( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)!=0 ){
    fprintf(stderr, "\nWARNING!  SQLite header and source version mismatch"
            "\n  source: %s\n  header: %s\n\n",
            sqlite3_sourceid(), SQLITE_SOURCE_ID);
    /* exit(1); */
  }
#endif
  Argv0 = argv[0];
  main_init(&data);
  stdin_is_interactive = isatty(0);

  /* Make sure we have a valid signal handler early, before anything

Changes to src/sqlite.h.in.

60
61
62
63
64
65
66



67
68
69
70
71
72
73
** would generate warning messages when they were used.  But that
** compiler magic ended up generating such a flurry of bug reports
** that we have taken it all out and gone back to using simple
** noop macros.
*/
#define SQLITE_DEPRECATED
#define SQLITE_EXPERIMENTAL




/*
** Ensure these symbols were not defined by some previous header file.
*/
#ifdef SQLITE_VERSION
# undef SQLITE_VERSION
#endif







>
>
>







60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
** would generate warning messages when they were used.  But that
** compiler magic ended up generating such a flurry of bug reports
** that we have taken it all out and gone back to using simple
** noop macros.
*/
#define SQLITE_DEPRECATED
#define SQLITE_EXPERIMENTAL
#ifndef __OSX_AVAILABLE_BUT_DEPRECATED
#define __OSX_AVAILABLE_BUT_DEPRECATED(MacOSAvailable, MacOSDeprecated, iPhoneOSAvailable, iPhoneOSDeprecated)
#endif

/*
** Ensure these symbols were not defined by some previous header file.
*/
#ifdef SQLITE_VERSION
# undef SQLITE_VERSION
#endif
525
526
527
528
529
530
531




532
533
534
535
536
537
538
#define SQLITE_OPEN_SUBJOURNAL       0x00002000  /* VFS only */
#define SQLITE_OPEN_MASTER_JOURNAL   0x00004000  /* VFS only */
#define SQLITE_OPEN_NOMUTEX          0x00008000  /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_FULLMUTEX        0x00010000  /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_SHAREDCACHE      0x00020000  /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_PRIVATECACHE     0x00040000  /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_WAL              0x00080000  /* VFS only */




#define SQLITE_OPEN_FILEPROTECTION_MASK                                 0x00700000

/* Reserved:                         0x00F00000 */

/*
** CAPI3REF: Device Characteristics
**







>
>
>
>







528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#define SQLITE_OPEN_SUBJOURNAL       0x00002000  /* VFS only */
#define SQLITE_OPEN_MASTER_JOURNAL   0x00004000  /* VFS only */
#define SQLITE_OPEN_NOMUTEX          0x00008000  /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_FULLMUTEX        0x00010000  /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_SHAREDCACHE      0x00020000  /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_PRIVATECACHE     0x00040000  /* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_WAL              0x00080000  /* VFS only */
#define SQLITE_OPEN_FILEPROTECTION_COMPLETE                             0x00100000
#define SQLITE_OPEN_FILEPROTECTION_COMPLETEUNLESSOPEN                   0x00200000
#define SQLITE_OPEN_FILEPROTECTION_COMPLETEUNTILFIRSTUSERAUTHENTICATION 0x00300000
#define SQLITE_OPEN_FILEPROTECTION_NONE                                 0x00400000
#define SQLITE_OPEN_FILEPROTECTION_MASK                                 0x00700000

/* Reserved:                         0x00F00000 */

/*
** CAPI3REF: Device Characteristics
**
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
** sqlite3_open_v2 with SQLITE_OPEN_SHAREDCACHE instead.
**
** This interface is threadsafe on processors where writing a
** 32-bit integer is atomic.
**
** See Also:  [SQLite Shared-Cache Mode]
*/
int sqlite3_enable_shared_cache(int);

/*
** CAPI3REF: Attempt To Free Heap Memory
**
** ^The sqlite3_release_memory() interface attempts to free N bytes
** of heap memory by deallocating non-essential memory allocations
** held by the database library.   Memory used to cache database







|







4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
** sqlite3_open_v2 with SQLITE_OPEN_SHAREDCACHE instead.
**
** This interface is threadsafe on processors where writing a
** 32-bit integer is atomic.
**
** See Also:  [SQLite Shared-Cache Mode]
*/
int sqlite3_enable_shared_cache(int) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_0, __MAC_10_7, __IPHONE_2_0, __IPHONE_5_0);

/*
** CAPI3REF: Attempt To Free Heap Memory
**
** ^The sqlite3_release_memory() interface attempts to free N bytes
** of heap memory by deallocating non-essential memory allocations
** held by the database library.   Memory used to cache database

Changes to src/sqlite3_private.h.

1
2
3
4
5
6
7



8
9
10
11
12
13
14
/*
 *  sqlite3_private.h
 */

#ifndef _SQLITE3_PRIVATE_H
#define _SQLITE3_PRIVATE_H




#define SQLITE_LOCKSTATE_OFF    0
#define SQLITE_LOCKSTATE_ON     1
#define SQLITE_LOCKSTATE_NOTADB 2
#define SQLITE_LOCKSTATE_ERROR  -1

#define SQLITE_LOCKSTATE_ANYPID -1








>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 *  sqlite3_private.h
 */

#ifndef _SQLITE3_PRIVATE_H
#define _SQLITE3_PRIVATE_H

#include <unistd.h>
#include <sqlite3.h>

#define SQLITE_LOCKSTATE_OFF    0
#define SQLITE_LOCKSTATE_ON     1
#define SQLITE_LOCKSTATE_NOTADB 2
#define SQLITE_LOCKSTATE_ERROR  -1

#define SQLITE_LOCKSTATE_ANYPID -1

31
32
33
34
35
36
37












38
39
40
41
42
43
44
** Test an open database connection for sqlite locks held by a process ID,
** if a process has an open database connection this will avoid trashing file
** locks by re-using open file descriptors for the database file and support
** files (-shm)
*/
#define SQLITE_FCNTL_LOCKSTATE_PID          103













/*
** Pass the SQLITE_TRUNCATE_DATABASE operation code to sqlite3_file_control() 
** to truncate a database and its associated journal file to zero length.  The 
** SQLITE_TRUNCATE_* flags represent optional flags to safely initialize an
** empty database in the place of the truncated database, the flags are passed 
** into sqlite3_file_control via the fourth argument using a pointer to an integer
** configured with the ORed flags.  If the fourth argument is NULL, the default 







>
>
>
>
>
>
>
>
>
>
>
>







34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
** Test an open database connection for sqlite locks held by a process ID,
** if a process has an open database connection this will avoid trashing file
** locks by re-using open file descriptors for the database file and support
** files (-shm)
*/
#define SQLITE_FCNTL_LOCKSTATE_PID          103

/*
** Purges eligible purgable memory regions (holding only unpinned pages) from 
** the page cache
*/
extern void _sqlite3_purgeEligiblePagerCacheMemory(void);

/*
** Returns the system defined default sqlite3_busy_handler function
** 
*/
extern int (*_sqlite3_system_busy_handler(void))(void*,int);

/*
** Pass the SQLITE_TRUNCATE_DATABASE operation code to sqlite3_file_control() 
** to truncate a database and its associated journal file to zero length.  The 
** SQLITE_TRUNCATE_* flags represent optional flags to safely initialize an
** empty database in the place of the truncated database, the flags are passed 
** into sqlite3_file_control via the fourth argument using a pointer to an integer
** configured with the ORed flags.  If the fourth argument is NULL, the default 

Changes to src/sqliteInt.h.

86
87
88
89
90
91
92



93

94
95
96
97
98
99
100
#include "sqlite3.h"

/*
** Include the configuration header output by 'configure' if we're using the
** autoconf-based build
*/
#ifdef _HAVE_SQLITE_CONFIG_H



#include "config.h"

#endif

#include "sqliteLimit.h"

/* Disable nuisance warnings on Borland compilers */
#if defined(__BORLANDC__)
#pragma warn -rch /* unreachable code */







>
>
>

>







86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "sqlite3.h"

/*
** Include the configuration header output by 'configure' if we're using the
** autoconf-based build
*/
#ifdef _HAVE_SQLITE_CONFIG_H
# ifdef _MSC_VER
#include "win_config.h" /* hand-mantained windows variant */
# else
#include "config.h"
# endif
#endif

#include "sqliteLimit.h"

/* Disable nuisance warnings on Borland compilers */
#if defined(__BORLANDC__)
#pragma warn -rch /* unreachable code */
186
187
188
189
190
191
192










193
194
195
196
197
198
199
** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can
** be overridden at runtime using the sqlite3_config() API.
*/
#if !defined(SQLITE_DEFAULT_MEMSTATUS)
# define SQLITE_DEFAULT_MEMSTATUS 1
#endif











/*
** Exactly one of the following macros must be defined in order to
** specify which memory allocation subsystem to use.
**
**     SQLITE_SYSTEM_MALLOC          // Use normal system malloc()
**     SQLITE_WIN32_MALLOC           // Use Win32 native heap API
**     SQLITE_ZERO_MALLOC            // Use a stub allocator that always fails







>
>
>
>
>
>
>
>
>
>







190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can
** be overridden at runtime using the sqlite3_config() API.
*/
#if !defined(SQLITE_DEFAULT_MEMSTATUS)
# define SQLITE_DEFAULT_MEMSTATUS 1
#endif

/*
** The SQLITE_ENABLE_PURGEABLE_PCACHE sets the default pcache to 
** pcache_purgeable, otherwise pcache1 is used.
*/
#if !defined(SQLITE_ENABLE_PURGEABLE_PCACHE)
# define SQLITE_OMIT_PURGEABLE_PCACHE
#else
# define SQLITE_OMIT_PCACHE1_PCACHE
#endif

/*
** Exactly one of the following macros must be defined in order to
** specify which memory allocation subsystem to use.
**
**     SQLITE_SYSTEM_MALLOC          // Use normal system malloc()
**     SQLITE_WIN32_MALLOC           // Use Win32 native heap API
**     SQLITE_ZERO_MALLOC            // Use a stub allocator that always fails

Changes to src/test1.c.

5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113









5114
5115
5116
5117
5118
5119
5120
                     Tcl_GetStringFromObj(Tcl_NewIntObj(iArg), 0), " ", 0);
    return TCL_ERROR;
  }
  return TCL_OK;  
}

#ifdef __APPLE__
/* From sqlite3_priavet.h */
# ifndef SQLITE_TRUNCATE_DATABASE
# define SQLITE_TRUNCATE_DATABASE      101
# define SQLITE_TRUNCATE_JOURNALMODE_WAL           (0x1<<0)
# define SQLITE_TRUNCATE_AUTOVACUUM_MASK           (0x3<<2)
# define SQLITE_TRUNCATE_AUTOVACUUM_OFF            (0x1<<2)
# define SQLITE_TRUNCATE_AUTOVACUUM_FULL           (0x2<<2)
# define SQLITE_TRUNCATE_AUTOVACUUM_INCREMENTAL    (0x3<<2)
# define SQLITE_TRUNCATE_PAGESIZE_MASK             (0x7<<4)
# define SQLITE_TRUNCATE_PAGESIZE_1024             (0x1<<4)
# define SQLITE_TRUNCATE_PAGESIZE_2048             (0x2<<4)
# define SQLITE_TRUNCATE_PAGESIZE_4096             (0x3<<4)
# define SQLITE_TRUNCATE_PAGESIZE_8192             (0x4<<4)
# endif
# ifndef SQLITE_REPLACE_DATABASE
# define SQLITE_REPLACE_DATABASE       102
# endif










/*
** tclcmd:   file_control_truncate_test DB
**
** This TCL command runs the sqlite3_file_control interface and
** verifies correct operation of the SQLITE_TRUNCATE_DATABASE verb.
*/







|
















>
>
>
>
>
>
>
>
>







5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
                     Tcl_GetStringFromObj(Tcl_NewIntObj(iArg), 0), " ", 0);
    return TCL_ERROR;
  }
  return TCL_OK;  
}

#ifdef __APPLE__
/* From sqlite3_private.h */
# ifndef SQLITE_TRUNCATE_DATABASE
# define SQLITE_TRUNCATE_DATABASE      101
# define SQLITE_TRUNCATE_JOURNALMODE_WAL           (0x1<<0)
# define SQLITE_TRUNCATE_AUTOVACUUM_MASK           (0x3<<2)
# define SQLITE_TRUNCATE_AUTOVACUUM_OFF            (0x1<<2)
# define SQLITE_TRUNCATE_AUTOVACUUM_FULL           (0x2<<2)
# define SQLITE_TRUNCATE_AUTOVACUUM_INCREMENTAL    (0x3<<2)
# define SQLITE_TRUNCATE_PAGESIZE_MASK             (0x7<<4)
# define SQLITE_TRUNCATE_PAGESIZE_1024             (0x1<<4)
# define SQLITE_TRUNCATE_PAGESIZE_2048             (0x2<<4)
# define SQLITE_TRUNCATE_PAGESIZE_4096             (0x3<<4)
# define SQLITE_TRUNCATE_PAGESIZE_8192             (0x4<<4)
# endif
# ifndef SQLITE_REPLACE_DATABASE
# define SQLITE_REPLACE_DATABASE       102
# endif
# ifndef SQLITE_FCNTL_LOCKSTATE_PID
#  define SQLITE_FCNTL_LOCKSTATE_PID          103
#  define SQLITE_LOCKSTATE_OFF    0
#  define SQLITE_LOCKSTATE_ON     1
#  define SQLITE_LOCKSTATE_NOTADB 2
#  define SQLITE_LOCKSTATE_ERROR  -1
#  define SQLITE_LOCKSTATE_ANYPID -1
extern int _sqlite3_lockstate(const char *path, pid_t pid);
# endif

/*
** tclcmd:   file_control_truncate_test DB
**
** This TCL command runs the sqlite3_file_control interface and
** verifies correct operation of the SQLITE_TRUNCATE_DATABASE verb.
*/
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
*/
static int file_control_replace_test(
  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  int objc,              /* Number of arguments */
  Tcl_Obj *CONST objv[]  /* Command arguments */
){
  int iArg = 0;
  sqlite3 *src_db;
  sqlite3 *dst_db;
  int rc;
  
  if( objc!=3 ){
    Tcl_AppendResult(interp, "wrong # args: should be \"",
                     Tcl_GetStringFromObj(objv[0], 0), " DST_DB SRC_DB", 0);







<







5164
5165
5166
5167
5168
5169
5170

5171
5172
5173
5174
5175
5176
5177
*/
static int file_control_replace_test(
  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  int objc,              /* Number of arguments */
  Tcl_Obj *CONST objv[]  /* Command arguments */
){

  sqlite3 *src_db;
  sqlite3 *dst_db;
  int rc;
  
  if( objc!=3 ){
    Tcl_AppendResult(interp, "wrong # args: should be \"",
                     Tcl_GetStringFromObj(objv[0], 0), " DST_DB SRC_DB", 0);
5178
5179
5180
5181
5182
5183
5184


































5185
5186
5187
5188
5189
5190
5191
  rc = sqlite3_file_control(dst_db, NULL, SQLITE_REPLACE_DATABASE, src_db);
  if( rc ){ 
    Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 
    return TCL_ERROR; 
  }
  return TCL_OK;  
}


































#endif /* __APPLE__ */

/*
** tclcmd:   file_control_chunksize_test DB DBNAME SIZE
**
** This TCL command runs the sqlite3_file_control interface and
** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
  rc = sqlite3_file_control(dst_db, NULL, SQLITE_REPLACE_DATABASE, src_db);
  if( rc ){ 
    Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); 
    return TCL_ERROR; 
  }
  return TCL_OK;  
}

/*
** tclcmd:   file_control_lockstate_test DBNAME PID
**
** This TCL command runs the _sqlite3_lockstate interface and
** verifies correct operation of the SQLITE_FCNTL_LOCKSTATE_PID verb.
*/
static int file_control_lockstate_test(
  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  int objc,              /* Number of arguments */
  Tcl_Obj *CONST objv[]  /* Command arguments */
){
  char *zDb;
  int pid = -1;
  int state;
  
  if( objc!=3 ){
    Tcl_AppendResult(interp, "wrong # args: should be \"",
                     Tcl_GetStringFromObj(objv[0], 0), " DBNAME PID", 0);
    return TCL_ERROR;
  }
  if( Tcl_GetIntFromObj(interp, objv[2], &pid) ){
    return TCL_ERROR;
  }
  zDb = Tcl_GetString(objv[1]);
  if( zDb[0]=='\0' ) zDb = NULL;
  if( !zDb ){
    return TCL_ERROR;
  }
  state = _sqlite3_lockstate(zDb, pid);
  Tcl_SetObjResult(interp, Tcl_NewIntObj(state));
  return TCL_OK;
}
#endif /* __APPLE__ */

/*
** tclcmd:   file_control_chunksize_test DB DBNAME SIZE
**
** This TCL command runs the sqlite3_file_control interface and
** verifies correct operation of the SQLITE_GET_LOCKPROXYFILE and
6709
6710
6711
6712
6713
6714
6715

6716
6717
6718
6719
6720
6721
6722
     { "vfs_reregister_all",         vfs_reregister_all,  0   },
     { "file_control_test",          file_control_test,   0   },
     { "file_control_lasterrno_test", file_control_lasterrno_test,  0   },
     { "file_control_lockproxy_test", file_control_lockproxy_test,  0   },
#ifdef __APPLE__
     { "file_control_truncate_test", file_control_truncate_test,  0   },
     { "file_control_replace_test", file_control_replace_test,  0   },

#endif 
     { "file_control_chunksize_test", file_control_chunksize_test,  0   },
     { "file_control_sizehint_test",  file_control_sizehint_test,   0   },
#if SQLITE_OS_WIN
     { "file_control_win32_av_retry", file_control_win32_av_retry,  0   },
     { "file_control_win32_set_handle", file_control_win32_set_handle, 0  },
#endif







>







6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
     { "vfs_reregister_all",         vfs_reregister_all,  0   },
     { "file_control_test",          file_control_test,   0   },
     { "file_control_lasterrno_test", file_control_lasterrno_test,  0   },
     { "file_control_lockproxy_test", file_control_lockproxy_test,  0   },
#ifdef __APPLE__
     { "file_control_truncate_test", file_control_truncate_test,  0   },
     { "file_control_replace_test", file_control_replace_test,  0   },
     { "file_control_lockstate_test", file_control_lockstate_test,  0   },
#endif 
     { "file_control_chunksize_test", file_control_chunksize_test,  0   },
     { "file_control_sizehint_test",  file_control_sizehint_test,   0   },
#if SQLITE_OS_WIN
     { "file_control_win32_av_retry", file_control_win32_av_retry,  0   },
     { "file_control_win32_set_handle", file_control_win32_set_handle, 0  },
#endif

Changes to src/test_config.c.

537
538
539
540
541
542
543






544
545
546
547
548
549
550
  Tcl_SetVar2(interp, "sqlite_options", "tclvar", "1", TCL_GLOBAL_ONLY);
#endif

  Tcl_SetVar2(interp, "sqlite_options", "threadsafe", 
      STRINGVALUE(SQLITE_THREADSAFE), TCL_GLOBAL_ONLY);
  assert( sqlite3_threadsafe()==SQLITE_THREADSAFE );







#ifdef SQLITE_OMIT_TEMPDB
  Tcl_SetVar2(interp, "sqlite_options", "tempdb", "0", TCL_GLOBAL_ONLY);
#else
  Tcl_SetVar2(interp, "sqlite_options", "tempdb", "1", TCL_GLOBAL_ONLY);
#endif

#ifdef SQLITE_OMIT_TRACE







>
>
>
>
>
>







537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
  Tcl_SetVar2(interp, "sqlite_options", "tclvar", "1", TCL_GLOBAL_ONLY);
#endif

  Tcl_SetVar2(interp, "sqlite_options", "threadsafe", 
      STRINGVALUE(SQLITE_THREADSAFE), TCL_GLOBAL_ONLY);
  assert( sqlite3_threadsafe()==SQLITE_THREADSAFE );

#if defined(USETHREADASSERTS)
  Tcl_SetVar2(interp,"sqlite_options","use_thread_asserts","1",TCL_GLOBAL_ONLY);
#else
  Tcl_SetVar2(interp,"sqlite_options","use_thread_asserts","0",TCL_GLOBAL_ONLY);
#endif
  
#ifdef SQLITE_OMIT_TEMPDB
  Tcl_SetVar2(interp, "sqlite_options", "tempdb", "0", TCL_GLOBAL_ONLY);
#else
  Tcl_SetVar2(interp, "sqlite_options", "tempdb", "1", TCL_GLOBAL_ONLY);
#endif

#ifdef SQLITE_OMIT_TRACE

Changes to src/test_intarray.c.

54
55
56
57
58
59
60
61
62

63
64
65
66
67
68
69
*/
#ifndef SQLITE_OMIT_VIRTUALTABLE

/*
** Free an sqlite3_intarray object.
*/
static void intarrayFree(sqlite3_intarray *p){
  if( p->xFree ){
    p->xFree(p->a);

  }
  sqlite3_free(p);
}

/*
** Table destructor for the intarray module.
*/







|

>







54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
*/
#ifndef SQLITE_OMIT_VIRTUALTABLE

/*
** Free an sqlite3_intarray object.
*/
static void intarrayFree(sqlite3_intarray *p){
  if( p->xFree && p->a){
    p->xFree(p->a);
    p->a = NULL;
  }
  sqlite3_free(p);
}

/*
** Table destructor for the intarray module.
*/
254
255
256
257
258
259
260

261
262
263
264
265
266
267
  sqlite3_intarray *pIntArray,   /* The intarray object to bind to */
  int nElements,                 /* Number of elements in the intarray */
  sqlite3_int64 *aElements,      /* Content of the intarray */
  void (*xFree)(void*)           /* How to dispose of the intarray when done */
){
  if( pIntArray->xFree ){
    pIntArray->xFree(pIntArray->a);

  }
  pIntArray->n = nElements;
  pIntArray->a = aElements;
  pIntArray->xFree = xFree;
  return SQLITE_OK;
}








>







255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
  sqlite3_intarray *pIntArray,   /* The intarray object to bind to */
  int nElements,                 /* Number of elements in the intarray */
  sqlite3_int64 *aElements,      /* Content of the intarray */
  void (*xFree)(void*)           /* How to dispose of the intarray when done */
){
  if( pIntArray->xFree ){
    pIntArray->xFree(pIntArray->a);
    pIntArray->a = NULL;
  }
  pIntArray->n = nElements;
  pIntArray->a = aElements;
  pIntArray->xFree = xFree;
  return SQLITE_OK;
}

Changes to src/util.c.

1066
1067
1068
1069
1070
1071
1072








1073
1074
1075
1076
1077
1078
1079
    }
    zBlob[i/2] = 0;
  }
  return zBlob;
}
#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */









/*
** Log an error that is an API call on a connection pointer that should
** not have been used.  The "type" of connection pointer is given as the
** argument.  The zType is a word like "NULL" or "closed" or "invalid".
*/
static void logBadConnection(const char *zType){
  sqlite3_log(SQLITE_MISUSE, 







>
>
>
>
>
>
>
>







1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
    }
    zBlob[i/2] = 0;
  }
  return zBlob;
}
#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */

#if defined(__APPLE__) && (defined(USETHREADASSERTS) || defined(SQLITE_DEBUG))
__private_extern__ char* __crashreporter_info__ = NULL;
static const char* _defaultMessage_ = "\n SQLite: Fatal multithreading error in caller.  Illegal multi-threaded access to the same database connection.\n";
#define HALT_CATCH_FIRE() { __crashreporter_info__ = (char*)_defaultMessage_; __builtin_trap(); }
#else
#define HALT_CATCH_FIRE()
#endif

/*
** Log an error that is an API call on a connection pointer that should
** not have been used.  The "type" of connection pointer is given as the
** argument.  The zType is a word like "NULL" or "closed" or "invalid".
*/
static void logBadConnection(const char *zType){
  sqlite3_log(SQLITE_MISUSE, 
1104
1105
1106
1107
1108
1109
1110





1111
1112
1113
1114
1115
1116
1117
  }
  magic = db->magic;
  if( magic!=SQLITE_MAGIC_OPEN ){
    if( sqlite3SafetyCheckSickOrOk(db) ){
      testcase( sqlite3GlobalConfig.xLog!=0 );
      logBadConnection("unopened");
    }





    return 0;
  }else{
    return 1;
  }
}
int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
  u32 magic;







>
>
>
>
>







1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
  }
  magic = db->magic;
  if( magic!=SQLITE_MAGIC_OPEN ){
    if( sqlite3SafetyCheckSickOrOk(db) ){
      testcase( sqlite3GlobalConfig.xLog!=0 );
      logBadConnection("unopened");
    }
#if defined(USETHREADASSERTS) || defined(SQLITE_DEBUG)
    if (magic==SQLITE_MAGIC_BUSY) {
      HALT_CATCH_FIRE();
    }
#endif
    return 0;
  }else{
    return 1;
  }
}
int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
  u32 magic;
1161
1162
1163
1164
1165
1166
1167

1168
1169
1170
1171
1172
1173
1174
  }
}
#define TWOPOWER32 (((i64)1)<<32)
#define TWOPOWER31 (((i64)1)<<31)
int sqlite3MulInt64(i64 *pA, i64 iB){
  i64 iA = *pA;
  i64 iA1, iA0, iB1, iB0, r;


  iA1 = iA/TWOPOWER32;
  iA0 = iA % TWOPOWER32;
  iB1 = iB/TWOPOWER32;
  iB0 = iB % TWOPOWER32;
  if( iA1==0 ){
    if( iB1==0 ){







>







1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
  }
}
#define TWOPOWER32 (((i64)1)<<32)
#define TWOPOWER31 (((i64)1)<<31)
int sqlite3MulInt64(i64 *pA, i64 iB){
  i64 iA = *pA;
  i64 iA1, iA0, iB1, iB0, r;
#undef HALT_CATCH_FIRE

  iA1 = iA/TWOPOWER32;
  iA0 = iA % TWOPOWER32;
  iB1 = iB/TWOPOWER32;
  iB0 = iB % TWOPOWER32;
  if( iA1==0 ){
    if( iB1==0 ){

Changes to src/vtab.c.

41
42
43
44
45
46
47





48
49
50
51
52
53
54
  int rc = SQLITE_OK;
  int nName;

  sqlite3_mutex_enter(db->mutex);
  nName = sqlite3Strlen30(zName);
  if( sqlite3HashFind(&db->aModule, zName, nName) ){
    rc = SQLITE_MISUSE_BKPT;





  }else{
    Module *pMod;
    pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
    if( pMod ){
      Module *pDel;
      char *zCopy = (char *)(&pMod[1]);
      memcpy(zCopy, zName, nName+1);







>
>
>
>
>







41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  int rc = SQLITE_OK;
  int nName;

  sqlite3_mutex_enter(db->mutex);
  nName = sqlite3Strlen30(zName);
  if( sqlite3HashFind(&db->aModule, zName, nName) ){
    rc = SQLITE_MISUSE_BKPT;
    // <rdar://problem/11777852>
    if (strncmp("fts", zName, 3) == 0) {
      xDestroy = NULL;
      rc = SQLITE_OK;
    }
  }else{
    Module *pMod;
    pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
    if( pMod ){
      Module *pDel;
      char *zCopy = (char *)(&pMod[1]);
      memcpy(zCopy, zName, nName+1);
322
323
324
325
326
327
328



329

330
331
332
333
334
335
336
  assert( iDb>=0 );

  pTable->tabFlags |= TF_Virtual;
  pTable->nModuleArg = 0;
  addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
  addModuleArgument(db, pTable, 0);
  addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));



  pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z);


#ifndef SQLITE_OMIT_AUTHORIZATION
  /* Creating a virtual table invokes the authorization callback twice.
  ** The first invocation, to obtain permission to INSERT a row into the
  ** sqlite_master table, has already been made by sqlite3StartTable().
  ** The second call, to obtain permission to create the table, is made now.
  */







>
>
>
|
>







327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
  assert( iDb>=0 );

  pTable->tabFlags |= TF_Virtual;
  pTable->nModuleArg = 0;
  addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
  addModuleArgument(db, pTable, 0);
  addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
  if (pName2 && pName2->z) {
    pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName2->z);
  } else {
    pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z);
  }

#ifndef SQLITE_OMIT_AUTHORIZATION
  /* Creating a virtual table invokes the authorization callback twice.
  ** The first invocation, to obtain permission to INSERT a row into the
  ** sqlite_master table, has already been made by sqlite3StartTable().
  ** The second call, to obtain permission to create the table, is made now.
  */

Changes to src/wal.c.

1673
1674
1675
1676
1677
1678
1679





1680
1681
1682
1683
1684
1685













1686
1687
1688
1689
1690
1691
1692
  u32 iDbpage = 0;                /* Next database page to write */
  u32 iFrame = 0;                 /* Wal frame containing data for iDbpage */
  u32 mxSafeFrame;                /* Max frame that can be backfilled */
  u32 mxPage;                     /* Max database page to write */
  int i;                          /* Loop counter */
  volatile WalCkptInfo *pInfo;    /* The checkpoint status information */
  int (*xBusy)(void*) = 0;        /* Function to call when waiting for locks */






  szPage = walPagesize(pWal);
  testcase( szPage<=32768 );
  testcase( szPage>=65536 );
  pInfo = walCkptInfo(pWal);
  if( pInfo->nBackfill>=pWal->hdr.mxFrame ) return SQLITE_OK;














  /* Allocate the iterator */
  rc = walIteratorInit(pWal, &pIter);
  if( rc!=SQLITE_OK ){
    return rc;
  }
  assert( pIter );







>
>
>
>
>






>
>
>
>
>
>
>
>
>
>
>
>
>







1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
  u32 iDbpage = 0;                /* Next database page to write */
  u32 iFrame = 0;                 /* Wal frame containing data for iDbpage */
  u32 mxSafeFrame;                /* Max frame that can be backfilled */
  u32 mxPage;                     /* Max database page to write */
  int i;                          /* Loop counter */
  volatile WalCkptInfo *pInfo;    /* The checkpoint status information */
  int (*xBusy)(void*) = 0;        /* Function to call when waiting for locks */
#if defined(__APPLE__)
  /* <rdar://problem/17742712> */
  i64 szDbFile;                   /* File size of the database, in bytes */
  i64 szWalFile;                  /* File size of the wal, in bytes */
#endif

  szPage = walPagesize(pWal);
  testcase( szPage<=32768 );
  testcase( szPage>=65536 );
  pInfo = walCkptInfo(pWal);
  if( pInfo->nBackfill>=pWal->hdr.mxFrame ) return SQLITE_OK;

#if defined(__APPLE__)
  /* <rdar://problem/17742712> */
  /* Get the size of the db and wal files to sanity check write offsets. */
  rc = sqlite3OsFileSize(pWal->pDbFd, &szDbFile);
  if( rc!=SQLITE_OK ){
      return rc;
  }
  rc = sqlite3OsFileSize(pWal->pWalFd, &szWalFile);
  if( rc!=SQLITE_OK ){
      return rc;
  }
#endif

  /* Allocate the iterator */
  rc = walIteratorInit(pWal, &pIter);
  if( rc!=SQLITE_OK ){
    return rc;
  }
  assert( pIter );
1747
1748
1749
1750
1751
1752
1753


















1754
1755
1756
1757
1758
1759
1760
      if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ) continue;
      iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
      /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
      rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
      if( rc!=SQLITE_OK ) break;
      iOffset = (iDbpage-1)*(i64)szPage;
      testcase( IS_BIG_INT(iOffset) );


















      rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
      if( rc!=SQLITE_OK ) break;
    }

    /* If work was actually accomplished... */
    if( rc==SQLITE_OK ){
      if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
      if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ) continue;
      iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
      /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
      rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
      if( rc!=SQLITE_OK ) break;
      iOffset = (iDbpage-1)*(i64)szPage;
      testcase( IS_BIG_INT(iOffset) );
      
#if defined(__APPLE__)
      /* <rdar://problem/17742712> */
      /* DEBUG(numist): this is way, way more conservative than the checks in
      ** the seekAndRead/seekAndWrite functions. I'm not even 100% sure that
      ** it's not overconservative, but db storage is more efficient than wal
      ** storage, so I'm pretty confident that writes to the db should never
      ** exceed dbsize +_ walsize.
      */
      if( iOffset>(szDbFile+szWalFile) ){
        __builtin_trap();
      }
      /* Update the db file size if the file will grow due to the write. */
      if( iOffset+szPage > szDbFile ){
        szDbFile = iOffset+szPage;
      }
#endif
      
      rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
      if( rc!=SQLITE_OK ) break;
    }

    /* If work was actually accomplished... */
    if( rc==SQLITE_OK ){
      if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
1882
1883
1884
1885
1886
1887
1888




1889
1890
1891
1892
1893
1894
1895
    WALTRACE(("WAL%p: closed\n", pWal));
    sqlite3_free((void *)pWal->apWiData);
    sqlite3_free(pWal);
  }
  return rc;
}





/*
** Try to read the wal-index header.  Return 0 on success and 1 if
** there is a problem.
**
** The wal-index is in shared memory.  Another thread or process might
** be writing the header at the same time this procedure is trying to
** read it, which might result in inconsistency.  A dirty read is detected







>
>
>
>







1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
    WALTRACE(("WAL%p: closed\n", pWal));
    sqlite3_free((void *)pWal->apWiData);
    sqlite3_free(pWal);
  }
  return rc;
}

#ifndef SQLITE_AMALGAMATION
extern int sqlite30sShmRead(sqlite3_file *id, void *dst, volatile void *src, sqlite3_int64 size);
#endif

/*
** Try to read the wal-index header.  Return 0 on success and 1 if
** there is a problem.
**
** The wal-index is in shared memory.  Another thread or process might
** be writing the header at the same time this procedure is trying to
** read it, which might result in inconsistency.  A dirty read is detected
1903
1904
1905
1906
1907
1908
1909

1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927

1928




1929

1930



1931
1932
1933
1934
1935
1936
1937
** If the checksum cannot be verified return non-zero. If the header
** is read successfully and the checksum verified, return zero.
*/
static int walIndexTryHdr(Wal *pWal, int *pChanged){
  u32 aCksum[2];                  /* Checksum on the header content */
  WalIndexHdr h1, h2;             /* Two copies of the header content */
  WalIndexHdr volatile *aHdr;     /* Header in shared memory */


  /* The first page of the wal-index must be mapped at this point. */
  assert( pWal->nWiData>0 && pWal->apWiData[0] );

  /* Read the header. This might happen concurrently with a write to the
  ** same area of shared memory on a different CPU in a SMP,
  ** meaning it is possible that an inconsistent snapshot is read
  ** from the file. If this happens, return non-zero.
  **
  ** There are two copies of the header at the beginning of the wal-index.
  ** When reading, read [0] first then [1].  Writes are in the reverse order.
  ** Memory barriers are used to prevent the compiler or the hardware from
  ** reordering the reads and writes.
  */
  aHdr = walIndexHdr(pWal);
  if( aHdr==NULL ){
    return 1; /* Shouldn't be getting NULL from walIndexHdr, but we are */
  }

  memcpy(&h1, (void *)&aHdr[0], sizeof(h1));




  walShmBarrier(pWal);

  memcpy(&h2, (void *)&aHdr[1], sizeof(h2));




  if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
    return 1;   /* Dirty read */
  }  
  if( h1.isInit==0 ){
    return 1;   /* Malformed header - probably all zeros */
  }







>


















>
|
>
>
>
>

>
|
>
>
>







1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
** If the checksum cannot be verified return non-zero. If the header
** is read successfully and the checksum verified, return zero.
*/
static int walIndexTryHdr(Wal *pWal, int *pChanged){
  u32 aCksum[2];                  /* Checksum on the header content */
  WalIndexHdr h1, h2;             /* Two copies of the header content */
  WalIndexHdr volatile *aHdr;     /* Header in shared memory */
  int rc = SQLITE_OK;

  /* The first page of the wal-index must be mapped at this point. */
  assert( pWal->nWiData>0 && pWal->apWiData[0] );

  /* Read the header. This might happen concurrently with a write to the
  ** same area of shared memory on a different CPU in a SMP,
  ** meaning it is possible that an inconsistent snapshot is read
  ** from the file. If this happens, return non-zero.
  **
  ** There are two copies of the header at the beginning of the wal-index.
  ** When reading, read [0] first then [1].  Writes are in the reverse order.
  ** Memory barriers are used to prevent the compiler or the hardware from
  ** reordering the reads and writes.
  */
  aHdr = walIndexHdr(pWal);
  if( aHdr==NULL ){
    return 1; /* Shouldn't be getting NULL from walIndexHdr, but we are */
  }
  
  rc = sqlite30sShmRead(pWal->pDbFd, &h1, &aHdr[0], sizeof(h1));
  if ( rc!=SQLITE_OK ) {
    return rc;
  }

  walShmBarrier(pWal);

  rc = sqlite30sShmRead(pWal->pDbFd, &h2, &aHdr[1], sizeof(h2));
  if ( rc!=SQLITE_OK ) {
    return rc;
  }

  if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
    return 1;   /* Dirty read */
  }  
  if( h1.isInit==0 ){
    return 1;   /* Malformed header - probably all zeros */
  }
2850
2851
2852
2853
2854
2855
2856
2857






2858
2859
2860
2861
2862
2863
2864
  /* Write all frames into the log file exactly once */
  for(p=pList; p; p=p->pDirty){
    int nDbSize;   /* 0 normally.  Positive == commit flag */
    iFrame++;
    assert( iOffset==walFrameOffset(iFrame, szPage) );
    nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
    rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
    if( rc ) return rc;






    pLast = p;
    iOffset += szFrame;
  }

  /* If this is the end of a transaction, then we might need to pad
  ** the transaction and/or sync the WAL file.
  **







|
>
>
>
>
>
>







2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
  /* Write all frames into the log file exactly once */
  for(p=pList; p; p=p->pDirty){
    int nDbSize;   /* 0 normally.  Positive == commit flag */
    iFrame++;
    assert( iOffset==walFrameOffset(iFrame, szPage) );
    nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
    rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
    if( rc ) {
#if defined(SQLITE_WRITE_WALFRAME_PREBUFFERED)
      free(w.aFrameBuf);
      w.aFrameBuf = NULL;
#endif
      return rc;
    }
    pLast = p;
    iOffset += szFrame;
  }

  /* If this is the end of a transaction, then we might need to pad
  ** the transaction and/or sync the WAL file.
  **
2875
2876
2877
2878
2879
2880
2881
2882






2883
2884
2885
2886
2887
2888
2889
2890
2891
2892

2893
2894
2895
2896
2897
2898
2899
  */
  if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){
    if( pWal->padToSectorBoundary ){
      int sectorSize = sqlite3SectorSize(pWal->pWalFd);
      w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
      while( iOffset<w.iSyncPoint ){
        rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
        if( rc ) return rc;






        iOffset += szFrame;
        nExtra++;
      }
    }else{
      rc = sqlite3OsSync(w.pFd, sync_flags & SQLITE_SYNC_MASK);
    }
  }

#if defined(SQLITE_WRITE_WALFRAME_PREBUFFERED)
  free(w.aFrameBuf);

#endif
  /* If this frame set completes the first transaction in the WAL and
  ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
  ** journal size limit, if possible.
  */
  if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
    i64 sz = pWal->mxWalSize;







|
>
>
>
>
>
>










>







2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
  */
  if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){
    if( pWal->padToSectorBoundary ){
      int sectorSize = sqlite3SectorSize(pWal->pWalFd);
      w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
      while( iOffset<w.iSyncPoint ){
        rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
        if( rc ) {
#if defined(SQLITE_WRITE_WALFRAME_PREBUFFERED)
          free(w.aFrameBuf);
          w.aFrameBuf = NULL;
#endif
          return rc;
        }
        iOffset += szFrame;
        nExtra++;
      }
    }else{
      rc = sqlite3OsSync(w.pFd, sync_flags & SQLITE_SYNC_MASK);
    }
  }

#if defined(SQLITE_WRITE_WALFRAME_PREBUFFERED)
  free(w.aFrameBuf);
  w.aFrameBuf = NULL;
#endif
  /* If this frame set completes the first transaction in the WAL and
  ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
  ** journal size limit, if possible.
  */
  if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
    i64 sz = pWal->mxWalSize;

Changes to test/fallocate.test.

75
76
77
78
79
80
81





82
83
84
85
86
87
88
89
90
91
92
93
94
set skipwaltests [expr {
  [permutation]=="journaltest" || [permutation]=="inmemory_journal"
}]
ifcapable !wal { set skipwaltests 1 }
if {![wal_is_ok]} { set skipwaltests 1 }

if {!$skipwaltests} {





  db close
  forcedelete test.db
  ifcapable enable_persist_wal {
    forcedelete test.db-journal
    forcedelete test.db-wal
    forcedelete test.db-shm
  }
  if {[forced_proxy_locking]} { 
    forcedelete .test.db-conch
  }
  sqlite3 db test.db
  ifcapable enable_persist_wal {
    file_control_persist_wal db 0







>
>
>
>
>





|







75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
set skipwaltests [expr {
  [permutation]=="journaltest" || [permutation]=="inmemory_journal"
}]
ifcapable !wal { set skipwaltests 1 }
if {![wal_is_ok]} { set skipwaltests 1 }

if {!$skipwaltests} {
  set shmpath test.db-shm
  if {[forced_proxy_locking]} {
    set shmpath [execsql { pragma lock_proxy_file }]-shm
    #puts $shmpath
  }
  db close
  forcedelete test.db
  ifcapable enable_persist_wal {
    forcedelete test.db-journal
    forcedelete test.db-wal
    forcedelete $shmpath
  }
  if {[forced_proxy_locking]} { 
    forcedelete .test.db-conch
  }
  sqlite3 db test.db
  ifcapable enable_persist_wal {
    file_control_persist_wal db 0

Changes to test/lock_proxy.test.

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  } {1}
  catch { db2 close }


  # test proxy locking readonly file system handling
  #

  if {[file exists /usr/bin/hdiutil]} {

    puts "Creating readonly file system for proxy locking tests..."
    if {[file exists /Volumes/readonly]} {
      exec hdiutil detach /Volumes/readonly
    }
    if {[file exists readonly.dmg]} {
      file delete readonly.dmg







|







51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  } {1}
  catch { db2 close }


  # test proxy locking readonly file system handling
  #

  if {[path_is_local "."]&&[file exists /usr/bin/hdiutil]} {

    puts "Creating readonly file system for proxy locking tests..."
    if {[file exists /Volumes/readonly]} {
      exec hdiutil detach /Volumes/readonly
    }
    if {[file exists readonly.dmg]} {
      file delete readonly.dmg

Changes to test/mutex1.test.

133
134
135
136
137
138
139






140
141
142
143
144
145
146
      mutex_counters counters
  
      set res [list]
      foreach {key value} [array get counters] {
        if {$key ne "total" && $value > 0} {
          lappend res $key
        }






      }
      lsort $res
    } [lsort $mutexes]
  }
  sqlite3_enable_shared_cache $enable_shared_cache

  # Open and use a connection in "nomutex" mode. Test that no recursive







>
>
>
>
>
>







133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
      mutex_counters counters
  
      set res [list]
      foreach {key value} [array get counters] {
        if {$key ne "total" && $value > 0} {
          lappend res $key
        }
      }
      ifcapable {use_thread_asserts} {
        if { $mode == "multithread" } {
          # USETHREADASSERTS changes the multithread mutexes to serialized
          set res [lsort $mutexes]
        }
      }
      lsort $res
    } [lsort $mutexes]
  }
  sqlite3_enable_shared_cache $enable_shared_cache

  # Open and use a connection in "nomutex" mode. Test that no recursive

Changes to test/permutations.test.

341
342
343
344
345
346
347

348
349
350
351
352
353
354
355
356
357
} 

test_suite "coverage-pager" -description {
  Coverage tests for file pager.c.
} -files {
  pager1.test    pager2.test  pagerfault.test  pagerfault2.test
  walfault.test  walbak.test  journal2.test    tkt-9d68c883.test

} 

test_suite "coverage-analyze" -description {
  Coverage tests for file analyze.c.
} -files {
  analyze3.test analyze4.test analyze5.test analyze6.test
  analyze7.test analyze8.test analyze9.test analyzeA.test
  analyze.test analyzeB.test mallocA.test
} 








>


<







341
342
343
344
345
346
347
348
349
350

351
352
353
354
355
356
357
} 

test_suite "coverage-pager" -description {
  Coverage tests for file pager.c.
} -files {
  pager1.test    pager2.test  pagerfault.test  pagerfault2.test
  walfault.test  walbak.test  journal2.test    tkt-9d68c883.test
test_suite "coverage-analyze" -description {
} 


  Coverage tests for file analyze.c.
} -files {
  analyze3.test analyze4.test analyze5.test analyze6.test
  analyze7.test analyze8.test analyze9.test analyzeA.test
  analyze.test analyzeB.test mallocA.test
} 

Changes to test/pragma.test.

1516
1517
1518
1519
1520
1521
1522























1523
1524
1525
1526
1527
1528
1529
    execsql "PRAGMA lock_proxy_file='$lpp6'" 
    set lockpath4 [execsql {
      create table if not exists aa(bb);
      PRAGMA lock_proxy_file;
    } db]
    string match "*proxytest/sub/dir/lock" $lockpath4
  } {1}
























  # ensure that if the path can not be created (perm), setting :auto: deals
  db close
  file delete -force $lpp5d
  do_test pragma-16.10.5 {
    sqlite3 db proxytest.db
    execsql "PRAGMA lock_proxy_file='$lpp5'" 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
    execsql "PRAGMA lock_proxy_file='$lpp6'" 
    set lockpath4 [execsql {
      create table if not exists aa(bb);
      PRAGMA lock_proxy_file;
    } db]
    string match "*proxytest/sub/dir/lock" $lockpath4
  } {1}

  # make sure we can deal with long (> NAME_MAX) file paths correctly
  db close
  set db_longroot [exec mktemp -d -t "longdbpath"]
  set db_longpath $db_longroot/123456789/223456789/323456789/423456789/523456789/623456789/723456789/823456789/923456789/a23456789/b23456789/c23456789/d23456789/e23456789/f23456789/g23456789/h23456789/i23456789/j23456789/k23456789/l23456789/m23456789/n23456789/o23456789/p23456789/q23456789
  file mkdir $db_longpath
  do_test pragma-16.10.4.1 {
    sqlite3 db $db_longpath/longtest.db
    execsql "PRAGMA lock_proxy_file=':auto:'"
    set lockpath_long [execsql {
      create table if not exists aa(bb);
      PRAGMA lock_proxy_file;
    } db]
    #puts $db_longpath
    #puts $lockpath_long
    set lockPathLen [string len $lockpath_long]
    set dbPathLen [string len $db_longpath]
    set lockName [string range $lockpath_long [string last / $lockpath_long] $lockPathLen]
    set nameLength [string len $lockName]
    list [expr "$dbPathLen > 256"] [expr "$lockPathLen < 1024"] [expr "$nameLength < 256"] [expr "$lockPathLen > 7"] [expr "$nameLength > 7"]
  } {1 1 1 1 1}
  file delete -force $db_longroot
  file delete -force $lockpath_long

  # ensure that if the path can not be created (perm), setting :auto: deals
  db close
  file delete -force $lpp5d
  do_test pragma-16.10.5 {
    sqlite3 db proxytest.db
    execsql "PRAGMA lock_proxy_file='$lpp5'" 

Changes to test/superlock.test.

209
210
211
212
213
214
215







216
217
218

219
220
221
222
223
224
225
226
227
228
229
230
231
232
233



234
235
236
237
238
239
240
  set wal2 [read_content ${file2}-wal]
  write_content ${file1}-wal $wal2
  write_content ${file2}-wal $wal1

  unlock1
  unlock2
}








forcedelete test.db
sqlite3 db  test.db

do_execsql_test 6.1 {
  ATTACH 'test.db2' AS aux;
  PRAGMA aux.journal_mode = wal;
  CREATE TABLE aux.t2(x, y);
  INSERT INTO aux.t2 VALUES('a', 'b');
  PRAGMA schema_version = 450;
  DETACH aux;

  PRAGMA main.journal_mode = wal;
  CREATE TABLE t1(a, b);
  INSERT INTO t1 VALUES(1, 2);
  INSERT INTO t1 VALUES(3, 4);
  SELECT * FROM t1;
} {wal wal 1 2 3 4}





db_swap test.db2 test.db
do_catchsql_test 6.2 { SELECT * FROM t1 } {1 {no such table: t1}}
do_catchsql_test 6.3 { SELECT * FROM t2 } {0 {a b}}

db_swap test.db2 test.db
do_catchsql_test 6.4 { SELECT * FROM t1 } {0 {1 2 3 4}}







>
>
>
>
>
>
>



>















>
>
>







209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
  set wal2 [read_content ${file2}-wal]
  write_content ${file1}-wal $wal2
  write_content ${file2}-wal $wal1

  unlock1
  unlock2
}

sqlite3 db  test.db
file_control_persist_wal db 0
db close
sqlite3 db2  test.db2
file_control_persist_wal db2 0
db2 close

forcedelete test.db
sqlite3 db  test.db
file_control_persist_wal db 0
do_execsql_test 6.1 {
  ATTACH 'test.db2' AS aux;
  PRAGMA aux.journal_mode = wal;
  CREATE TABLE aux.t2(x, y);
  INSERT INTO aux.t2 VALUES('a', 'b');
  PRAGMA schema_version = 450;
  DETACH aux;

  PRAGMA main.journal_mode = wal;
  CREATE TABLE t1(a, b);
  INSERT INTO t1 VALUES(1, 2);
  INSERT INTO t1 VALUES(3, 4);
  SELECT * FROM t1;
} {wal wal 1 2 3 4}

sqlite3 db2  test.db2
file_control_persist_wal db2 0
db2 close

db_swap test.db2 test.db
do_catchsql_test 6.2 { SELECT * FROM t1 } {1 {no such table: t1}}
do_catchsql_test 6.3 { SELECT * FROM t2 } {0 {a b}}

db_swap test.db2 test.db
do_catchsql_test 6.4 { SELECT * FROM t1 } {0 {1 2 3 4}}

Changes to test/walro.test.

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
do_multiclient_test tn {
  
  # Close all connections and delete the database.
  #
  code1 { db close  }
  code2 { db2 close }
  code3 { db3 close }
  forcedelete test.db
  forcedelete walro
  
  # Do not run tests with the connections in the same process.
  #
  if {$tn==2} continue

  foreach c {code1 code2 code3} {







|







42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
do_multiclient_test tn {
  
  # Close all connections and delete the database.
  #
  code1 { db close  }
  code2 { db2 close }
  code3 { db3 close }
  forcedelete test.db $shmpath
  forcedelete walro
  
  # Do not run tests with the connections in the same process.
  #
  if {$tn==2} continue

  foreach c {code1 code2 code3} {
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  } {SQLITE_READONLY_RECOVERY}

  #-----------------------------------------------------------------------
  # Test cases 1.4.* check that checkpoints and log wraps don't prevent
  # read-only connections from reading the database.
  do_test 1.4.1 {
    code1 { db close }
    forcedelete test.db-shm
    file exists test.db-shm
  } {0}

  # Open one read-only and one read-write connection. Write some data
  # and then run a checkpoint using the read-write connection. Then
  # check the read-only connection can still read.
  do_test 1.4.2 {
    code1 { sqlite3 db file:test.db?readonly_shm=1 }







|
|







179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  } {SQLITE_READONLY_RECOVERY}

  #-----------------------------------------------------------------------
  # Test cases 1.4.* check that checkpoints and log wraps don't prevent
  # read-only connections from reading the database.
  do_test 1.4.1 {
    code1 { db close }
    forcedelete $shmpath
    file exists $shmpath
  } {0}

  # Open one read-only and one read-write connection. Write some data
  # and then run a checkpoint using the read-write connection. Then
  # check the read-only connection can still read.
  do_test 1.4.2 {
    code1 { sqlite3 db file:test.db?readonly_shm=1 }
241
242
243
244
245
246
247



248
249
250
251
252
253
254
forcedelete test.db

#-----------------------------------------------------------------------
# Test cases 2.* check that a read-only connection may read the
# database file while a checkpoint operation is ongoing.
#
do_multiclient_test tn {



  
  # Close all connections and delete the database.
  #
  code1 { db close  }
  code2 { db2 close }
  code3 { db3 close }
  forcedelete test.db







>
>
>







241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
forcedelete test.db

#-----------------------------------------------------------------------
# Test cases 2.* check that a read-only connection may read the
# database file while a checkpoint operation is ongoing.
#
do_multiclient_test tn {
  # Do not run tests with the connections in the same process.
  #
  if {$tn==2} continue
  
  # Close all connections and delete the database.
  #
  code1 { db close  }
  code2 { db2 close }
  code3 { db3 close }
  forcedelete test.db

Changes to tool/mksqlite3c.tcl.

112
113
114
115
116
117
118

119
120
121
122

123
124
125
126
127
128
129
   rtree.h
   sqlite3ext.h
   sqlite3.h
   sqliteicu.h
   sqliteInt.h
   sqliteLimit.h
   sqlrr.h

   vdbe.h
   vdbeInt.h
   wal.h
   whereInt.h

} {
  set available_hdr($hdr) 1
}
set available_hdr(sqliteInt.h) 0

# 78 stars used for comment formatting.
set s78 \







>




>







112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
   rtree.h
   sqlite3ext.h
   sqlite3.h
   sqliteicu.h
   sqliteInt.h
   sqliteLimit.h
   sqlrr.h
   test_intarray.h
   vdbe.h
   vdbeInt.h
   wal.h
   whereInt.h
   sqlite3_private.h
} {
  set available_hdr($hdr) 1
}
set available_hdr(sqliteInt.h) 0

# 78 stars used for comment formatting.
set s78 \
261
262
263
264
265
266
267

268
269
270
271
272
273
274

   os_unix.c
   os_win.c

   bitvec.c
   pcache.c
   pcache1.c

   rowset.c
   pager.c
   wal.c

   btmutex.c
   btree.c
   backup.c







>







263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

   os_unix.c
   os_win.c

   bitvec.c
   pcache.c
   pcache1.c
   pcache_purgeable.c
   rowset.c
   pager.c
   wal.c

   btmutex.c
   btree.c
   backup.c
329
330
331
332
333
334
335

336
337
338
339
340
   fts3_unicode.c
   fts3_unicode2.c

   rtree.c
   icu.c
   fts3_icu.c
   sqlrr.c

} {
  copy_file tsrc/$file
}

close $out







>





332
333
334
335
336
337
338
339
340
341
342
343
344
   fts3_unicode.c
   fts3_unicode2.c

   rtree.c
   icu.c
   fts3_icu.c
   sqlrr.c
   test_intarray.c
} {
  copy_file tsrc/$file
}

close $out

Changes to tool/warnings.sh.

1
2
3
4
5
6
7




8
9
10
11
12
13
14
15
16
17
18
19
#/bin/sh
#
# Run this script in a directory with a working makefile to check for 
# compiler warnings in SQLite.
#
rm -f sqlite3.c
make sqlite3.c




echo '********** No optimizations.  Includes FTS4 and RTREE *********'
gcc -c -Wshadow -Wall -Wextra -pedantic-errors -Wno-long-long -std=c89 \
      -ansi -DHAVE_STDINT_H -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_RTREE \
      sqlite3.c
echo '********** No optimizations. ENABLE_STAT4. THREADSAFE=0 *******'
gcc -c -Wshadow -Wall -Wextra -pedantic-errors -Wno-long-long -std=c89 \
      -ansi -DSQLITE_ENABLE_STAT4 -DSQLITE_THREADSAFE=0 \
      sqlite3.c
echo '********** Optimized -O3.  Includes FTS4 and RTREE ************'
gcc -O3 -c -Wshadow -Wall -Wextra -pedantic-errors -Wno-long-long -std=c89 \
      -ansi -DHAVE_STDINT_H -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_RTREE \
      sqlite3.c







>
>
>
>

|
|


|
|


|
|

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#/bin/sh
#
# Run this script in a directory with a working makefile to check for 
# compiler warnings in SQLite.
#
rm -f sqlite3.c
make sqlite3.c

# Apple-specific defines used for platform specific feature support
DEFINES="-DSQLITE_OMIT_LOAD_EXTENSION=1 -DSQLITE_ENABLE_LOCKING_STYLE=1 -DSQLITE_ENABLE_AUTO_PROFILE=1 -DUSE_PREAD=1 -DSQLITE_THREADSAFE=2 -DSQLITE_MAX_LENGTH=2147483645 -DSQLITE_MAX_VARIABLE_NUMBER=500000 -DSQLITE_ENABLE_APPLE_SPI=1 -DHAVE_USLEEP=1 -DSQLITE_OMIT_AUTORESET=1 -DSQLITE_ENABLE_PURGEABLE_PCACHE=1 -DSQLITE_DEFAULT_CKPTFULLFSYNC=1 -DSQLITE_DEFAULT_WAL_SAFETYLEVEL=2 -DSQLITE_USE_URI=1 -DSQLITE_WRITE_WALFRAME_PREBUFFERED=1 -DSQLITE_ENABLE_PERSIST_WAL=1 -DUSE_GUARDED_FD=1 -DSQLITE_ENABLE_FLOCKTIMEOUT=1 -DSQLITE_OMIT_BUILTIN_TEST=1 -DSQLITE_DEFAULT_MEMSTATUS=0 -DSQLITE_DEFAULT_PAGE_SIZE=4096 -DSQLITE_DEFAULT_JOURNAL_SIZE_LIMIT=32768 -DSQLITE_DEFAULT_CACHE_SIZE=500 -DSQLITE_ENABLE_RTREE=1 -DSQLITE_ENABLE_FTS3=1 -DSQLITE_ENABLE_FTS3_PARENTHESIS=1"

echo '********** No optimizations.  Includes FTS4 and RTREE *********'
gcc -c -Wshadow -Wall -Wextra -Weverything -Wno-long-long -std=c89 \
      -ansi $DEFINES -DHAVE_STDINT_H -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_RTREE \
      sqlite3.c
echo '********** No optimizations. ENABLE_STAT4. THREADSAFE=0 *******'
gcc -c -Wshadow -Wall -Wextra -Weverything -Wno-long-long -std=c89 \
      -ansi $DEFINES -DSQLITE_ENABLE_STAT4 -DSQLITE_THREADSAFE=0 \
      sqlite3.c
echo '********** Optimized -O3.  Includes FTS4 and RTREE ************'
gcc -O3 -c -Wshadow -Wall -Wextra -Weverything -Wno-long-long -std=c89 \
      -ansi $DEFINES -DHAVE_STDINT_H -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_RTREE \
      sqlite3.c