SQLite

Check-in [f70d552bcd]
Login

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

Overview
Comment:Rearrange some ENABLE_LOCKING_STYLE related code in os_unix.c. (CVS 5324)
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: f70d552bcd0df884eea2e2272bae558d35fc8845
User & Date: danielk1977 2008-06-28 11:23:00.000
Context
2008-06-28
11:29
Added reminder message to terminate SQL statements with a semicolon on shell startup. This closes #3099. (CVS 5325) (check-in: 0ab0b030de user: mihailim tags: trunk)
11:23
Rearrange some ENABLE_LOCKING_STYLE related code in os_unix.c. (CVS 5324) (check-in: f70d552bcd user: danielk1977 tags: trunk)
2008-06-27
18:59
Changed copy-paste error in comment. Fixes #3193. (CVS 5323) (check-in: 00eee53e86 user: mihailim tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/os_unix.c.
8
9
10
11
12
13
14
15
16
17
18
19










20
21
22
23
24
25
26
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
******************************************************************************
**
** This file contains code that is specific to Unix systems.
**
** $Id: os_unix.c,v 1.190 2008/06/26 10:41:19 danielk1977 Exp $
*/
#include "sqliteInt.h"
#if SQLITE_OS_UNIX              /* This file is used on unix only */











/* #define SQLITE_ENABLE_LOCKING_STYLE 0 */

/*
** These #defines should enable >2GB file support on Posix if the
** underlying operating system supports it.  If the OS lacks
** large file support, these should be no-ops.
**







|




>
>
>
>
>
>
>
>
>
>







8
9
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
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
******************************************************************************
**
** This file contains code that is specific to Unix systems.
**
** $Id: os_unix.c,v 1.191 2008/06/28 11:23:00 danielk1977 Exp $
*/
#include "sqliteInt.h"
#if SQLITE_OS_UNIX              /* This file is used on unix only */

/*
** If SQLITE_ENABLE_LOCKING_STYLE is defined, then several different 
** locking implementations are provided:
**
**   * POSIX locking (the default),
**   * No locking,
**   * Dot-file locking,
**   * flock() locking,
**   * AFP locking (OSX only).
*/
/* #define SQLITE_ENABLE_LOCKING_STYLE 0 */

/*
** These #defines should enable >2GB file support on Posix if the
** underlying operating system supports it.  If the OS lacks
** large file support, these should be no-ops.
**
46
47
48
49
50
51
52

53
54
55
56
57
58
59
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <errno.h>

#ifdef SQLITE_ENABLE_LOCKING_STYLE
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/mount.h>
#endif /* SQLITE_ENABLE_LOCKING_STYLE */

/*







>







56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <errno.h>

#ifdef SQLITE_ENABLE_LOCKING_STYLE
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/mount.h>
#endif /* SQLITE_ENABLE_LOCKING_STYLE */

/*
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  */
  char aPadding[32];
#endif
  struct openCnt *pOpen;    /* Info about all open fd's on this inode */
  struct lockInfo *pLock;   /* Info about locks on this inode */
#ifdef SQLITE_ENABLE_LOCKING_STYLE
  void *lockingContext;     /* Locking style specific state */
#endif /* SQLITE_ENABLE_LOCKING_STYLE */
  int h;                    /* The file descriptor */
  unsigned char locktype;   /* The type of lock held on this fd */
  int dirfd;                /* File descriptor for the directory */
#if SQLITE_THREADSAFE
  pthread_t tid;            /* The thread that "owns" this unixFile */
#endif
};







|







102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  */
  char aPadding[32];
#endif
  struct openCnt *pOpen;    /* Info about all open fd's on this inode */
  struct lockInfo *pLock;   /* Info about locks on this inode */
#ifdef SQLITE_ENABLE_LOCKING_STYLE
  void *lockingContext;     /* Locking style specific state */
#endif
  int h;                    /* The file descriptor */
  unsigned char locktype;   /* The type of lock held on this fd */
  int dirfd;                /* File descriptor for the directory */
#if SQLITE_THREADSAFE
  pthread_t tid;            /* The thread that "owns" this unixFile */
#endif
};
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
** Here is the dirt on POSIX advisory locks:  ANSI STD 1003.1 (1996)
** section 6.5.2.2 lines 483 through 490 specify that when a process
** sets or clears a lock, that operation overrides any prior locks set
** by the same process.  It does not explicitly say so, but this implies
** that it overrides locks set by the same process using a different
** file descriptor.  Consider this test case:
**
**       int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
**       int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
**
** Suppose ./file1 and ./file2 are really the same file (because
** one is a hard or symbolic link to the other) then if you set
** an exclusive lock on fd1, then try to get an exclusive lock
** on fd2, it works.  I would have expected the second lock to
** fail since there was already a lock on the file due to fd1.







<
<







185
186
187
188
189
190
191


192
193
194
195
196
197
198
/*
** Here is the dirt on POSIX advisory locks:  ANSI STD 1003.1 (1996)
** section 6.5.2.2 lines 483 through 490 specify that when a process
** sets or clears a lock, that operation overrides any prior locks set
** by the same process.  It does not explicitly say so, but this implies
** that it overrides locks set by the same process using a different
** file descriptor.  Consider this test case:


**       int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
**
** Suppose ./file1 and ./file2 are really the same file (because
** one is a hard or symbolic link to the other) then if you set
** an exclusive lock on fd1, then try to get an exclusive lock
** on fd2, it works.  I would have expected the second lock to
** fail since there was already a lock on the file due to fd1.
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
** These hash tables map inodes and file descriptors (really, lockKey and
** openKey structures) into lockInfo and openCnt structures.  Access to 
** these hash tables must be protected by a mutex.
*/
static Hash lockHash = {SQLITE_HASH_BINARY, 0, 0, 0, 0, 0};
static Hash openHash = {SQLITE_HASH_BINARY, 0, 0, 0, 0, 0};

#ifdef SQLITE_ENABLE_LOCKING_STYLE
/*
** The locking styles are associated with the different file locking
** capabilities supported by different file systems.  
**
** POSIX locking style fully supports shared and exclusive byte-range locks 
** ADP locking only supports exclusive byte-range locks
** FLOCK only supports a single file-global exclusive lock
** DOTLOCK isn't a true locking style, it refers to the use of a special
**   file named the same as the database file with a '.lock' extension, this
**   can be used on file systems that do not offer any reliable file locking
** NO locking means that no locking will be attempted, this is only used for
**   read-only file systems currently
** UNSUPPORTED means that no locking will be attempted, this is only used for
**   file systems that are known to be unsupported
*/
typedef enum {
  posixLockingStyle = 0,       /* standard posix-advisory locks */
  afpLockingStyle,             /* use afp locks */
  flockLockingStyle,           /* use flock() */
  dotlockLockingStyle,         /* use <file>.lock files */
  noLockingStyle,              /* useful for read-only file system */
  unsupportedLockingStyle      /* indicates unsupported file system */
} sqlite3LockingStyle;
#endif /* SQLITE_ENABLE_LOCKING_STYLE */

/*
** Helper functions to obtain and relinquish the global mutex.
*/
static void enterMutex(){
  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
}







<





|









|
|
|
|
<
<
<
<
|







342
343
344
345
346
347
348

349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367




368
369
370
371
372
373
374
375
** These hash tables map inodes and file descriptors (really, lockKey and
** openKey structures) into lockInfo and openCnt structures.  Access to 
** these hash tables must be protected by a mutex.
*/
static Hash lockHash = {SQLITE_HASH_BINARY, 0, 0, 0, 0, 0};
static Hash openHash = {SQLITE_HASH_BINARY, 0, 0, 0, 0, 0};


/*
** The locking styles are associated with the different file locking
** capabilities supported by different file systems.  
**
** POSIX locking style fully supports shared and exclusive byte-range locks 
** AFP locking only supports exclusive byte-range locks
** FLOCK only supports a single file-global exclusive lock
** DOTLOCK isn't a true locking style, it refers to the use of a special
**   file named the same as the database file with a '.lock' extension, this
**   can be used on file systems that do not offer any reliable file locking
** NO locking means that no locking will be attempted, this is only used for
**   read-only file systems currently
** UNSUPPORTED means that no locking will be attempted, this is only used for
**   file systems that are known to be unsupported
*/
#define LOCKING_STYLE_POSIX        1
#define LOCKING_STYLE_FLOCK        2
#define LOCKING_STYLE_DOTFILE      3
#define LOCKING_STYLE_NONE         4




#define LOCKING_STYLE_AFP          5

/*
** Helper functions to obtain and relinquish the global mutex.
*/
static void enterMutex(){
  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
}
512
513
514
515
516
517
518
519
520
521
522
523
524

525
526
527
528
529
530
531
532
533
534
535
536
537
538

539
540
541
542
543
544
545
546
547
548
549
550
551
552
553



554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569

570
571
572
573
574



575
576

577
578
579
580






581
582
583





584
585
586
587
588
589
590
591
592
593
594
595

596
597
598

599
600
601
602
603
604
605
606
607
608
609
610

611
612

613
614
615
616
617
618
619
620
621
622
}
#endif /* SQLITE_THREADSAFE */

/*
** Release a lockInfo structure previously allocated by findLockInfo().
*/
static void releaseLockInfo(struct lockInfo *pLock){
  if (pLock == NULL)
    return;
  pLock->nRef--;
  if( pLock->nRef==0 ){
    sqlite3HashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0);
    sqlite3_free(pLock);

  }
}

/*
** Release a openCnt structure previously allocated by findLockInfo().
*/
static void releaseOpenCnt(struct openCnt *pOpen){
  if (pOpen == NULL)
    return;
  pOpen->nRef--;
  if( pOpen->nRef==0 ){
    sqlite3HashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0);
    free(pOpen->aPending);
    sqlite3_free(pOpen);

  }
}

#ifdef SQLITE_ENABLE_LOCKING_STYLE
/*
** Tests a byte-range locking query to see if byte range locks are 
** supported, if not we fall back to dotlockLockingStyle.
*/
static sqlite3LockingStyle sqlite3TestLockingStyle(
  const char *filePath, 
  int fd
){
  /* test byte-range lock using fcntl */
  struct flock lockInfo;
  



  lockInfo.l_len = 1;
  lockInfo.l_start = 0;
  lockInfo.l_whence = SEEK_SET;
  lockInfo.l_type = F_RDLCK;
  
  if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
    return posixLockingStyle;
  } 
  
  /* testing for flock can give false positives.  So if if the above test
  ** fails, then we fall back to using dot-lock style locking.
  */  
  return dotlockLockingStyle;
}

/* 

** Examines the f_fstypename entry in the statfs structure as returned by 
** stat() for the file system hosting the database file, assigns the 
** appropriate locking style based on its value.  These values and 
** assignments are based on Darwin/OSX behavior and have not been tested on 
** other systems.



*/
static sqlite3LockingStyle sqlite3DetectLockingStyle(

  const char *filePath, 
  int fd
){







#ifdef SQLITE_FIXED_LOCKING_STYLE
  return (sqlite3LockingStyle)SQLITE_FIXED_LOCKING_STYLE;
#else





  struct statfs fsInfo;

  if( statfs(filePath, &fsInfo) == -1 ){
    return sqlite3TestLockingStyle(filePath, fd);
  }
  if( fsInfo.f_flags & MNT_RDONLY ){
    return noLockingStyle;
  }
  if( strcmp(fsInfo.f_fstypename, "hfs")==0 ||
      strcmp(fsInfo.f_fstypename, "ufs")==0 ){
    return posixLockingStyle;
  }

  if( strcmp(fsInfo.f_fstypename, "afpfs")==0 ){
    return afpLockingStyle;
  }

  if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
    return sqlite3TestLockingStyle(filePath, fd);
  }
  if( strcmp(fsInfo.f_fstypename, "smbfs")==0 ){
    return flockLockingStyle;
  }
  if( strcmp(fsInfo.f_fstypename, "msdos")==0 ){
    return dotlockLockingStyle;
  }
  if( strcmp(fsInfo.f_fstypename, "webdav")==0 ){
    return unsupportedLockingStyle;
  }

  return sqlite3TestLockingStyle(filePath, fd);  
#endif /* SQLITE_FIXED_LOCKING_STYLE */

}

#endif /* SQLITE_ENABLE_LOCKING_STYLE */

/*
** Given a file descriptor, locate lockInfo and openCnt structures that
** describes that file descriptor.  Create new ones if necessary.  The
** return values might be uninitialized if an error occurs.
**
** Return an appropriate error code.







|
<
|
|
|
|
>







|
<
|
|
|
|
|
>



<




|
<
<
<
<

|
>
>
>




<

|
|

|
|

|



>
|
|
|
|

>
>
>

|
>



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


|
|

|
|

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

<
<
|
>
|
|
>

<
<







516
517
518
519
520
521
522
523

524
525
526
527
528
529
530
531
532
533
534
535
536

537
538
539
540
541
542
543
544
545

546
547
548
549
550




551
552
553
554
555
556
557
558
559

560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594

595
596
597
598
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
}
#endif /* SQLITE_THREADSAFE */

/*
** Release a lockInfo structure previously allocated by findLockInfo().
*/
static void releaseLockInfo(struct lockInfo *pLock){
  if( pLock ){

    pLock->nRef--;
    if( pLock->nRef==0 ){
      sqlite3HashInsert(&lockHash, &pLock->key, sizeof(pLock->key), 0);
      sqlite3_free(pLock);
    }
  }
}

/*
** Release a openCnt structure previously allocated by findLockInfo().
*/
static void releaseOpenCnt(struct openCnt *pOpen){
  if( pOpen ){

    pOpen->nRef--;
    if( pOpen->nRef==0 ){
      sqlite3HashInsert(&openHash, &pOpen->key, sizeof(pOpen->key), 0);
      free(pOpen->aPending);
      sqlite3_free(pOpen);
    }
  }
}


/*
** Tests a byte-range locking query to see if byte range locks are 
** supported, if not we fall back to dotlockLockingStyle.
*/
static int testLockingStyle(int fd){




  struct flock lockInfo;

  /* Test byte-range lock using fcntl(). If the call succeeds, 
  ** assume that the file-system supports POSIX style locks. 
  */
  lockInfo.l_len = 1;
  lockInfo.l_start = 0;
  lockInfo.l_whence = SEEK_SET;
  lockInfo.l_type = F_RDLCK;

  if( fcntl(fd, F_GETLK, &lockInfo)!=-1 ) {
    return LOCKING_STYLE_POSIX;
  }
  
  /* Testing for flock() can give false positives.  So if if the above 
  ** test fails, then we fall back to using dot-file style locking.
  */  
  return LOCKING_STYLE_DOTFILE;
}

/* 
** If SQLITE_ENABLE_LOCKING_STYLE is defined, this function Examines the 
** f_fstypename entry in the statfs structure as returned by stat() for 
** the file system hosting the database file and selects  the appropriate
** locking style based on its value.  These values and assignments are 
** based on Darwin/OSX behavior and have not been thoroughly tested on 
** other systems.
**
** If SQLITE_ENABLE_LOCKING_STYLE is not defined, this function always
** returns LOCKING_STYLE_POSIX.
*/
static int detectLockingStyle(
  sqlite3_vfs *pVfs,
  const char *filePath, 
  int fd
){
#ifdef SQLITE_ENABLE_LOCKING_STYLE
  struct Mapping {
    const char *zFilesystem;
    int eLockingStyle;
  } aMap[] = {
    { "hfs",    LOCKING_STYLE_POSIX },
    { "ufs",    LOCKING_STYLE_POSIX },
    { "afpfs",  LOCKING_STYLE_AFP },
    { "smbfs",  LOCKING_STYLE_FLOCK },

    { "msdos",  LOCKING_STYLE_DOTFILE },
    { "webdav", LOCKING_STYLE_NONE },
    { 0, 0 }
  };
  int i;
  struct statfs fsInfo;

  if( !filePath ){
    return LOCKING_STYLE_NONE;
  }
  if( pVfs->pAppData ){
    return (int)pVfs->pAppData;
  }




  if( statfs(filePath, &fsInfo) != -1 ){
    if( fsInfo.f_flags & MNT_RDONLY ){
      return LOCKING_STYLE_NONE;
    }
    for(i=0; aMap[i].zFilesystem; i++){
      if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
        return aMap[i].eLockingStyle;
      }


    }


  }



  /* Default case. Handles, amongst others, "nfs". */
  return testLockingStyle(fd);  
#endif
  return LOCKING_STYLE_POSIX;
}



/*
** Given a file descriptor, locate lockInfo and openCnt structures that
** describes that file descriptor.  Create new ones if necessary.  The
** return values might be uninitialized if an error occurs.
**
** Return an appropriate error code.
1419
1420
1421
1422
1423
1424
1425






















1426
1427
1428
1429
1430

1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452

1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464

1465
1466
1467
1468
1469
1470
1471
      }
    }
  }
  leaveMutex();
  if( rc==SQLITE_OK ) pFile->locktype = locktype;
  return rc;
}























/*
** Close a file.
*/
static int unixClose(sqlite3_file *id){

  unixFile *pFile = (unixFile *)id;
  if( !pFile ) return SQLITE_OK;
  unixUnlock(id, NO_LOCK);
  if( pFile->dirfd>=0 ) close(pFile->dirfd);
  pFile->dirfd = -1;
  enterMutex();

  if( pFile->pOpen->nLock ){
    /* If there are outstanding locks, do not actually close the file just
    ** yet because that would clear those locks.  Instead, add the file
    ** descriptor to pOpen->aPending.  It will be automatically closed when
    ** the last lock is cleared.
    */
    int *aNew;
    struct openCnt *pOpen = pFile->pOpen;
    aNew = realloc( pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
    if( aNew==0 ){
      /* If a malloc fails, just leak the file descriptor */
    }else{
      pOpen->aPending = aNew;
      pOpen->aPending[pOpen->nPending] = pFile->h;
      pOpen->nPending++;

    }
  }else{
    /* There are no outstanding locks so we can close the file immediately */
    close(pFile->h);
  }
  releaseLockInfo(pFile->pLock);
  releaseOpenCnt(pFile->pOpen);

  leaveMutex();
  OSTRACE2("CLOSE   %-3d\n", pFile->h);
  OpenCounter(-1);
  memset(pFile, 0, sizeof(unixFile));

  return SQLITE_OK;
}


#ifdef SQLITE_ENABLE_LOCKING_STYLE
#pragma mark AFP Support








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





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







1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463

1464


1465

1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482



1483
1484
1485
1486
1487



1488
1489
1490
1491
1492
1493
1494
1495
      }
    }
  }
  leaveMutex();
  if( rc==SQLITE_OK ) pFile->locktype = locktype;
  return rc;
}

/*
** This function performs the parts of the "close file" operation 
** common to all locking schemes. It closes the directory and file
** handles, if they are valid, and sets all fields of the unixFile
** structure to 0.
*/
static int closeUnixFile(sqlite3_file *id){
  unixFile *pFile = (unixFile*)id;
  if( pFile ){
    if( pFile->dirfd>=0 ){
      close(pFile->dirfd);
    }
    if( pFile->h>=0 ){
      close(pFile->h);
    }
    OSTRACE2("CLOSE   %-3d\n", pFile->h);
    OpenCounter(-1);
    memset(pFile, 0, sizeof(unixFile));
  }
  return SQLITE_OK;
}

/*
** Close a file.
*/
static int unixClose(sqlite3_file *id){
  if( id ){
    unixFile *pFile = (unixFile *)id;

    unixUnlock(id, NO_LOCK);


    enterMutex();

    if( pFile->pOpen->nLock ){
      /* If there are outstanding locks, do not actually close the file just
      ** yet because that would clear those locks.  Instead, add the file
      ** descriptor to pOpen->aPending.  It will be automatically closed when
      ** the last lock is cleared.
      */
      int *aNew;
      struct openCnt *pOpen = pFile->pOpen;
      aNew = realloc( pOpen->aPending, (pOpen->nPending+1)*sizeof(int) );
      if( aNew==0 ){
        /* If a malloc fails, just leak the file descriptor */
      }else{
        pOpen->aPending = aNew;
        pOpen->aPending[pOpen->nPending] = pFile->h;
        pOpen->nPending++;
        pFile->h = -1;
      }



    }
    releaseLockInfo(pFile->pLock);
    releaseOpenCnt(pFile->pOpen);
    closeUnixFile(id);
    leaveMutex();



  }
  return SQLITE_OK;
}


#ifdef SQLITE_ENABLE_LOCKING_STYLE
#pragma mark AFP Support

1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539

/*
 ** This routine checks if there is a RESERVED lock held on the specified
 ** file by this or any other process. If such a lock is held, return
 ** non-zero.  If the file is unlocked or holds only SHARED locks, then
 ** return zero.
 */
static int afpUnixCheckReservedLock(sqlite3_file *id, int *pResOut){
  int r = 0;
  unixFile *pFile = (unixFile*)id;
  
  assert( pFile ); 
  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
  
  /* Check if a thread in this process holds such a lock */







|







1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563

/*
 ** This routine checks if there is a RESERVED lock held on the specified
 ** file by this or any other process. If such a lock is held, return
 ** non-zero.  If the file is unlocked or holds only SHARED locks, then
 ** return zero.
 */
static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
  int r = 0;
  unixFile *pFile = (unixFile*)id;
  
  assert( pFile ); 
  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
  
  /* Check if a thread in this process holds such a lock */
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
  
  *pResOut = r;
  return SQLITE_OK;
}

/* AFP-style locking following the behavior of unixLock, see the unixLock 
** function comments for details of lock management. */
static int afpUnixLock(sqlite3_file *id, int locktype){
  int rc = SQLITE_OK;
  unixFile *pFile = (unixFile*)id;
  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
  int gotPendingLock = 0;
  
  assert( pFile );
  OSTRACE5("LOCK    %d %s was %s pid=%d\n", pFile->h,
         locktypeName(locktype), locktypeName(pFile->locktype), getpid());

  /* If there is already a lock of this type or more restrictive on the
  ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as







|



<







1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593

1594
1595
1596
1597
1598
1599
1600
  
  *pResOut = r;
  return SQLITE_OK;
}

/* AFP-style locking following the behavior of unixLock, see the unixLock 
** function comments for details of lock management. */
static int afpLock(sqlite3_file *id, int locktype){
  int rc = SQLITE_OK;
  unixFile *pFile = (unixFile*)id;
  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;

  
  assert( pFile );
  OSTRACE5("LOCK    %d %s was %s pid=%d\n", pFile->h,
         locktypeName(locktype), locktypeName(pFile->locktype), getpid());

  /* If there is already a lock of this type or more restrictive on the
  ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
  }
  
  /* If control gets to this point, then actually go ahead and make
  ** operating system calls for the specified lock.
  */
  if( locktype==SHARED_LOCK ){
    int lk, failed;
    int tries = 0;
    
    /* Now get the read-lock */
    /* note that the quality of the randomness doesn't matter that much */
    lk = random(); 
    context->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
    failed = _AFPFSSetLock(context->filePath, pFile->h, 
      SHARED_FIRST+context->sharedLockByte, 1, 1);







<







1640
1641
1642
1643
1644
1645
1646

1647
1648
1649
1650
1651
1652
1653
  }
  
  /* If control gets to this point, then actually go ahead and make
  ** operating system calls for the specified lock.
  */
  if( locktype==SHARED_LOCK ){
    int lk, failed;

    
    /* Now get the read-lock */
    /* note that the quality of the randomness doesn't matter that much */
    lk = random(); 
    context->sharedLockByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1);
    failed = _AFPFSSetLock(context->filePath, pFile->h, 
      SHARED_FIRST+context->sharedLockByte, 1, 1);
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
        /* Acquire a RESERVED lock */
        failed = _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1,1);
    }
    if (!failed && locktype == EXCLUSIVE_LOCK) {
      /* Acquire an EXCLUSIVE lock */
        
      /* Remove the shared lock before trying the range.  we'll need to 
      ** reestablish the shared lock if we can't get the  afpUnixUnlock
      */
      if (!_AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST +
                         context->sharedLockByte, 1, 0)) {
        /* now attemmpt to get the exclusive lock range */
        failed = _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST, 
                               SHARED_SIZE, 1);
        if (failed && _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST +







|







1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
        /* Acquire a RESERVED lock */
        failed = _AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1,1);
    }
    if (!failed && locktype == EXCLUSIVE_LOCK) {
      /* Acquire an EXCLUSIVE lock */
        
      /* Remove the shared lock before trying the range.  we'll need to 
      ** reestablish the shared lock if we can't get the  afpUnlock
      */
      if (!_AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST +
                         context->sharedLockByte, 1, 0)) {
        /* now attemmpt to get the exclusive lock range */
        failed = _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST, 
                               SHARED_SIZE, 1);
        if (failed && _AFPFSSetLock(context->filePath, pFile->h, SHARED_FIRST +
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
/*
** Lower the locking level on file descriptor pFile to locktype.  locktype
** must be either NO_LOCK or SHARED_LOCK.
**
** If the locking level of the file descriptor is already at or below
** the requested locking level, this routine is a no-op.
*/
static int afpUnixUnlock(sqlite3_file *id, int locktype) {
  struct flock lock;
  int rc = SQLITE_OK;
  unixFile *pFile = (unixFile*)id;
  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;

  assert( pFile );
  OSTRACE5("UNLOCK  %d %d was %d pid=%d\n", pFile->h, locktype,
         pFile->locktype, getpid());







|
<







1715
1716
1717
1718
1719
1720
1721
1722

1723
1724
1725
1726
1727
1728
1729
/*
** Lower the locking level on file descriptor pFile to locktype.  locktype
** must be either NO_LOCK or SHARED_LOCK.
**
** If the locking level of the file descriptor is already at or below
** the requested locking level, this routine is a no-op.
*/
static int afpUnlock(sqlite3_file *id, int locktype) {

  int rc = SQLITE_OK;
  unixFile *pFile = (unixFile*)id;
  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;

  assert( pFile );
  OSTRACE5("UNLOCK  %d %d was %d pid=%d\n", pFile->h, locktype,
         pFile->locktype, getpid());
1761
1762
1763
1764
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
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
  leaveMutex();
  return rc;
}

/*
** Close a file & cleanup AFP specific locking context 
*/
static int afpUnixClose(sqlite3_file *id) {

  unixFile *pFile = (unixFile*)id;

  if( !pFile ) return SQLITE_OK;
  afpUnixUnlock(id, NO_LOCK);
  sqlite3_free(pFile->lockingContext);
  if( pFile->dirfd>=0 ) close(pFile->dirfd);
  pFile->dirfd = -1;
  enterMutex();
  close(pFile->h);
  leaveMutex();
  OSTRACE2("CLOSE   %-3d\n", pFile->h);
  OpenCounter(-1);
  memset(pFile, 0, sizeof(unixFile));

  return SQLITE_OK;
}


#pragma mark flock() style locking

/*
** The flockLockingContext is not used
*/
typedef void flockLockingContext;

static int flockUnixCheckReservedLock(sqlite3_file *id, int *pResOut){
  int r = 1;
  unixFile *pFile = (unixFile*)id;
  
  if (pFile->locktype != RESERVED_LOCK) {
    /* attempt to get the lock */
    int rc = flock(pFile->h, LOCK_EX | LOCK_NB);
    if (!rc) {
      /* got the lock, unlock it */
      flock(pFile->h, LOCK_UN);
      r = 0;  /* no one has it reserved */
    }
  }

  *pResOut = r;
  return SQLITE_OK;
}

static int flockUnixLock(sqlite3_file *id, int locktype) {
  unixFile *pFile = (unixFile*)id;
  
  /* if we already have a lock, it is exclusive.  
  ** Just adjust level and punt on outta here. */
  if (pFile->locktype > NO_LOCK) {
    pFile->locktype = locktype;
    return SQLITE_OK;







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










|

















|







1782
1783
1784
1785
1786
1787
1788
1789
1790
1791


1792
1793








1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
  leaveMutex();
  return rc;
}

/*
** Close a file & cleanup AFP specific locking context 
*/
static int afpClose(sqlite3_file *id) {
  if( id ){
    unixFile *pFile = (unixFile*)id;


    afpUnlock(id, NO_LOCK);
    sqlite3_free(pFile->lockingContext);








  }
  return closeUnixFile(id);
}


#pragma mark flock() style locking

/*
** The flockLockingContext is not used
*/
typedef void flockLockingContext;

static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
  int r = 1;
  unixFile *pFile = (unixFile*)id;
  
  if (pFile->locktype != RESERVED_LOCK) {
    /* attempt to get the lock */
    int rc = flock(pFile->h, LOCK_EX | LOCK_NB);
    if (!rc) {
      /* got the lock, unlock it */
      flock(pFile->h, LOCK_UN);
      r = 0;  /* no one has it reserved */
    }
  }

  *pResOut = r;
  return SQLITE_OK;
}

static int flockLock(sqlite3_file *id, int locktype) {
  unixFile *pFile = (unixFile*)id;
  
  /* if we already have a lock, it is exclusive.  
  ** Just adjust level and punt on outta here. */
  if (pFile->locktype > NO_LOCK) {
    pFile->locktype = locktype;
    return SQLITE_OK;
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
  } else {
    /* got it, set the type and return ok */
    pFile->locktype = locktype;
    return SQLITE_OK;
  }
}

static int flockUnixUnlock(sqlite3_file *id, int locktype) {
  unixFile *pFile = (unixFile*)id;
  
  assert( locktype<=SHARED_LOCK );
  
  /* no-op if possible */
  if( pFile->locktype==locktype ){
    return SQLITE_OK;







|







1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
  } else {
    /* got it, set the type and return ok */
    pFile->locktype = locktype;
    return SQLITE_OK;
  }
}

static int flockUnlock(sqlite3_file *id, int locktype) {
  unixFile *pFile = (unixFile*)id;
  
  assert( locktype<=SHARED_LOCK );
  
  /* no-op if possible */
  if( pFile->locktype==locktype ){
    return SQLITE_OK;
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
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
1938
1939
1940
1941
1942
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

1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
    return SQLITE_OK;
  }
}

/*
** Close a file.
*/
static int flockUnixClose(sqlite3_file *id) {
  unixFile *pFile = (unixFile*)id;
  
  if( !pFile ) return SQLITE_OK;
  flockUnixUnlock(id, NO_LOCK);
  
  if( pFile->dirfd>=0 ) close(pFile->dirfd);
  pFile->dirfd = -1;

  enterMutex();
  close(pFile->h);  
  leaveMutex();
  OSTRACE2("CLOSE   %-3d\n", pFile->h);
  OpenCounter(-1);
  memset(pFile, 0, sizeof(unixFile));
  return SQLITE_OK;
}

#pragma mark Old-School .lock file based locking

/*
** The dotlockLockingContext structure contains all dotlock (.lock) lock
** specific state
*/
typedef struct dotlockLockingContext dotlockLockingContext;
struct dotlockLockingContext {
  char *lockPath;
};


static int dotlockUnixCheckReservedLock(sqlite3_file *id, int *pResOut) {
  int r = 1;
  unixFile *pFile = (unixFile*)id;
  dotlockLockingContext *context;

  context = (dotlockLockingContext*)pFile->lockingContext;
  if (pFile->locktype != RESERVED_LOCK) {
    struct stat statBuf;
    if (lstat(context->lockPath,&statBuf) != 0){
      /* file does not exist, we could have it if we want it */
      r = 0;
    }
  }

  *pResOut = r;
  return SQLITE_OK;
}

static int dotlockUnixLock(sqlite3_file *id, int locktype) {
  unixFile *pFile = (unixFile*)id;
  dotlockLockingContext *context;
  int fd;

  context = (dotlockLockingContext*)pFile->lockingContext;
  
  /* if we already have a lock, it is exclusive.  
  ** Just adjust level and punt on outta here. */
  if (pFile->locktype > NO_LOCK) {
    pFile->locktype = locktype;
    
    /* Always update the timestamp on the old file */
    utimes(context->lockPath,NULL);
    return SQLITE_OK;
  }
  
  /* check to see if lock file already exists */
  struct stat statBuf;
  if (lstat(context->lockPath,&statBuf) == 0){
    return SQLITE_BUSY; /* it does, busy */
  }
  
  /* grab an exclusive lock */
  fd = open(context->lockPath,O_RDONLY|O_CREAT|O_EXCL,0600);
  if( fd<0 ){
    /* failed to open/create the file, someone else may have stolen the lock */
    return SQLITE_BUSY; 
  }
  close(fd);
  
  /* got it, set the type and return ok */
  pFile->locktype = locktype;
  return SQLITE_OK;
}

static int dotlockUnixUnlock(sqlite3_file *id, int locktype) {
  unixFile *pFile = (unixFile*)id;
  dotlockLockingContext *context;

  context = (dotlockLockingContext*)pFile->lockingContext;
  
  assert( locktype<=SHARED_LOCK );
  
  /* no-op if possible */
  if( pFile->locktype==locktype ){
    return SQLITE_OK;
  }
  
  /* shared can just be set because we always have an exclusive */
  if (locktype==SHARED_LOCK) {
    pFile->locktype = locktype;
    return SQLITE_OK;
  }
  
  /* no, really, unlock. */
  unlink(context->lockPath);
  pFile->locktype = NO_LOCK;
  return SQLITE_OK;
}

/*
 ** Close a file.
 */
static int dotlockUnixClose(sqlite3_file *id) {

  unixFile *pFile = (unixFile*)id;
  
  if( !pFile ) return SQLITE_OK;
  dotlockUnixUnlock(id, NO_LOCK);
  sqlite3_free(pFile->lockingContext);
  if( pFile->dirfd>=0 ) close(pFile->dirfd);
  pFile->dirfd = -1;
  enterMutex();  
  close(pFile->h);
  leaveMutex();
  OSTRACE2("CLOSE   %-3d\n", pFile->h);
  OpenCounter(-1);
  memset(pFile, 0, sizeof(unixFile));

  return SQLITE_OK;
}


#pragma mark No locking

/*
** The nolockLockingContext is void
*/
typedef void nolockLockingContext;

static int nolockUnixCheckReservedLock(sqlite3_file *id, int *pResOut) {
  *pResOut = 0;
  return SQLITE_OK;
}

static int nolockUnixLock(sqlite3_file *id, int locktype) {
  return SQLITE_OK;
}

static int nolockUnixUnlock(sqlite3_file *id, int locktype) {
  return SQLITE_OK;
}

/*
** Close a file.
*/
static int nolockUnixClose(sqlite3_file *id) {
  unixFile *pFile = (unixFile*)id;
  
  if( !pFile ) return SQLITE_OK;
  if( pFile->dirfd>=0 ) close(pFile->dirfd);
  pFile->dirfd = -1;
  enterMutex();
  close(pFile->h);
  leaveMutex();
  OSTRACE2("CLOSE   %-3d\n", pFile->h);
  OpenCounter(-1);
  memset(pFile, 0, sizeof(unixFile));
  return SQLITE_OK;
}

#endif /* SQLITE_ENABLE_LOCKING_STYLE */


/*
** Information and control of an open file handle.







|
<
|
<
|
|
<
<
|
<
<
<
<
<
<
<




<
<
<
<
<
<
<
<
<
<
|


|

<


|









|

<

<
|
|






|





|




|











|

<
<
|
|














|







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










|




|



|






|
<
|
<
<
<
<
<
<
<
<
<
<







1868
1869
1870
1871
1872
1873
1874
1875

1876

1877
1878


1879







1880
1881
1882
1883










1884
1885
1886
1887
1888

1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902

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
1938
1939
1940
1941
1942
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
1988
1989
1990
1991
1992
1993
1994

1995










1996
1997
1998
1999
2000
2001
2002
    return SQLITE_OK;
  }
}

/*
** Close a file.
*/
static int flockClose(sqlite3_file *id) {

  if( id ){

    flockUnlock(id, NO_LOCK);
  }


  return closeUnixFile(id);







}

#pragma mark Old-School .lock file based locking











static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
  int r = 1;
  unixFile *pFile = (unixFile*)id;
  char *zLockFile = (char *)pFile->lockingContext;


  if (pFile->locktype != RESERVED_LOCK) {
    struct stat statBuf;
    if (lstat(zLockFile, &statBuf) != 0){
      /* file does not exist, we could have it if we want it */
      r = 0;
    }
  }

  *pResOut = r;
  return SQLITE_OK;
}

static int dotlockLock(sqlite3_file *id, int locktype) {
  unixFile *pFile = (unixFile*)id;

  int fd;

  char *zLockFile = (char *)pFile->lockingContext;

  /* if we already have a lock, it is exclusive.  
  ** Just adjust level and punt on outta here. */
  if (pFile->locktype > NO_LOCK) {
    pFile->locktype = locktype;
    
    /* Always update the timestamp on the old file */
    utimes(zLockFile, NULL);
    return SQLITE_OK;
  }
  
  /* check to see if lock file already exists */
  struct stat statBuf;
  if (lstat(zLockFile,&statBuf) == 0){
    return SQLITE_BUSY; /* it does, busy */
  }
  
  /* grab an exclusive lock */
  fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600);
  if( fd<0 ){
    /* failed to open/create the file, someone else may have stolen the lock */
    return SQLITE_BUSY; 
  }
  close(fd);
  
  /* got it, set the type and return ok */
  pFile->locktype = locktype;
  return SQLITE_OK;
}

static int dotlockUnlock(sqlite3_file *id, int locktype) {
  unixFile *pFile = (unixFile*)id;


  char *zLockFile = (char *)pFile->lockingContext;

  assert( locktype<=SHARED_LOCK );
  
  /* no-op if possible */
  if( pFile->locktype==locktype ){
    return SQLITE_OK;
  }
  
  /* shared can just be set because we always have an exclusive */
  if (locktype==SHARED_LOCK) {
    pFile->locktype = locktype;
    return SQLITE_OK;
  }
  
  /* no, really, unlock. */
  unlink(zLockFile);
  pFile->locktype = NO_LOCK;
  return SQLITE_OK;
}

/*
 ** Close a file.
 */
static int dotlockClose(sqlite3_file *id) {
  if( id ){
    unixFile *pFile = (unixFile*)id;


    dotlockUnlock(id, NO_LOCK);
    sqlite3_free(pFile->lockingContext);








  }
  return closeUnixFile(id);
}


#pragma mark No locking

/*
** The nolockLockingContext is void
*/
typedef void nolockLockingContext;

static int nolockCheckReservedLock(sqlite3_file *id, int *pResOut) {
  *pResOut = 0;
  return SQLITE_OK;
}

static int nolockLock(sqlite3_file *id, int locktype) {
  return SQLITE_OK;
}

static int nolockUnlock(sqlite3_file *id, int locktype) {
  return SQLITE_OK;
}

/*
** Close a file.
*/
static int nolockClose(sqlite3_file *id) {

  return closeUnixFile(id);










}

#endif /* SQLITE_ENABLE_LOCKING_STYLE */


/*
** Information and control of an open file handle.
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
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182

2183
2184
2185
2186
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
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225

2226
2227
2228
2229
2230
2231
2232
2233

2234
2235
2236
2237

2238

2239
2240
2241
2242
2243
2244
2245
2246
2247
2248

2249
2250
2251
2252
2253
2254
2255
2256
2257
2258






2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273





2274



2275


2276
2277
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
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
** Return the device characteristics for the file. This is always 0.
*/
static int unixDeviceCharacteristics(sqlite3_file *id){
  return 0;
}

/*
** This vector defines all the methods that can operate on an sqlite3_file
** for unix.
*/
static const sqlite3_io_methods sqlite3UnixIoMethod = {
  1,                        /* iVersion */
  unixClose,
  unixRead,
  unixWrite,
  unixTruncate,
  unixSync,
  unixFileSize,
  unixLock,
  unixUnlock,
  unixCheckReservedLock,
  unixFileControl,
  unixSectorSize,
  unixDeviceCharacteristics
};

#ifdef SQLITE_ENABLE_LOCKING_STYLE
/*
** This vector defines all the methods that can operate on an sqlite3_file
** for unix with AFP style file locking.
*/
static const sqlite3_io_methods sqlite3AFPLockingUnixIoMethod = {
  1,                        /* iVersion */
  afpUnixClose,
  unixRead,
  unixWrite,
  unixTruncate,
  unixSync,
  unixFileSize,
  afpUnixLock,
  afpUnixUnlock,
  afpUnixCheckReservedLock,
  unixFileControl,
  unixSectorSize,
  unixDeviceCharacteristics
};

/*
** This vector defines all the methods that can operate on an sqlite3_file
** for unix with flock() style file locking.
*/
static const sqlite3_io_methods sqlite3FlockLockingUnixIoMethod = {
  1,                        /* iVersion */
  flockUnixClose,
  unixRead,
  unixWrite,
  unixTruncate,
  unixSync,
  unixFileSize,
  flockUnixLock,
  flockUnixUnlock,
  flockUnixCheckReservedLock,
  unixFileControl,
  unixSectorSize,
  unixDeviceCharacteristics
};

/*
** This vector defines all the methods that can operate on an sqlite3_file
** for unix with dotlock style file locking.
*/
static const sqlite3_io_methods sqlite3DotlockLockingUnixIoMethod = {
  1,                        /* iVersion */
  dotlockUnixClose,
  unixRead,
  unixWrite,
  unixTruncate,
  unixSync,
  unixFileSize,
  dotlockUnixLock,
  dotlockUnixUnlock,
  dotlockUnixCheckReservedLock,
  unixFileControl,
  unixSectorSize,
  unixDeviceCharacteristics
};

/*
** This vector defines all the methods that can operate on an sqlite3_file
** for unix with nolock style file locking.
*/
static const sqlite3_io_methods sqlite3NolockLockingUnixIoMethod = {
  1,                        /* iVersion */
  nolockUnixClose,
  unixRead,
  unixWrite,
  unixTruncate,
  unixSync,
  unixFileSize,
  nolockUnixLock,
  nolockUnixUnlock,
  nolockUnixCheckReservedLock,
  unixFileControl,
  unixSectorSize,
  unixDeviceCharacteristics
};

#endif /* SQLITE_ENABLE_LOCKING_STYLE */

/*
** Allocate memory for a new unixFile and initialize that unixFile.
** Write a pointer to the new unixFile into *pId.
** If we run out of memory, close the file and return an error.
*/
#ifdef SQLITE_ENABLE_LOCKING_STYLE
/* 
** When locking extensions are enabled, the filepath and locking style 
** are needed to determine the unixFile pMethod to use for locking operations.
** The locking-style specific lockingContext data structure is created 
** and assigned here also.
*/
static int fillInUnixFile(

  int h,                  /* Open file descriptor of file being opened */
  int dirfd,              /* Directory file descriptor */
  sqlite3_file *pId,      /* Write to the unixFile structure here */
  const char *zFilename   /* Name of the file being opened */
){
  sqlite3LockingStyle lockingStyle = noLockingStyle;
  unixFile *pNew = (unixFile *)pId;
  int rc;



















#ifdef FD_CLOEXEC
  fcntl(h, F_SETFD, fcntl(h, F_GETFD, 0) | FD_CLOEXEC);




#endif






  assert( pNew->pLock==NULL );
  assert( pNew->pOpen==NULL );
  if( zFilename ){
    /* If zFilename is NULL then this is a temporary file. Temporary files
    ** are never locked or unlocked, so noLockingStyle is used for these.
    ** The locking style used by other files is determined by 
    ** sqlite3DetectLockingStyle().
    */
    lockingStyle = sqlite3DetectLockingStyle(zFilename, h);
    if ( lockingStyle==posixLockingStyle ){
      enterMutex();
      rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
      leaveMutex();
      if( rc ){
        if( dirfd>=0 ) close(dirfd);
        close(h);
        return rc;
      }
    }
  }

  OSTRACE3("OPEN    %-3d %s\n", h, zFilename);    
  pNew->h = h;
  pNew->dirfd = dirfd;
  SET_THREADID(pNew);
    
  switch(lockingStyle) {
    case afpLockingStyle: {
      /* afp locking uses the file path so it needs to be included in
      ** the afpLockingContext */

      afpLockingContext *context;
      pNew->pMethod = &sqlite3AFPLockingUnixIoMethod;
      pNew->lockingContext = context = sqlite3_malloc( sizeof(*context) );
      if( context==0 ){
        close(h);
        if( dirfd>=0 ) close(dirfd);
        return SQLITE_NOMEM;
      }


      /* NB: zFilename exists and remains valid until the file is closed
      ** according to requirement F11141.  So we do not need to make a
      ** copy of the filename. */

      context->filePath = zFilename;

      srandomdev();
      break;
    }
    case flockLockingStyle:
      /* flock locking doesn't need additional lockingContext information */
      pNew->pMethod = &sqlite3FlockLockingUnixIoMethod;
      break;
    case dotlockLockingStyle: {
      /* dotlock locking uses the file path so it needs to be included in
      ** the dotlockLockingContext */

      dotlockLockingContext *context;
      int nFilename;
      nFilename = strlen(zFilename);
      pNew->pMethod = &sqlite3DotlockLockingUnixIoMethod;
      pNew->lockingContext = context = 
         sqlite3_malloc( sizeof(*context) + nFilename + 6 );
      if( context==0 ){
        close(h);
        if( dirfd>=0 ) close(dirfd);
        return SQLITE_NOMEM;






      }
      context->lockPath = (char*)&context[1];
      sqlite3_snprintf(nFilename, context->lockPath,
                       "%s.lock", zFilename);
      break;
    }
    case posixLockingStyle:
      /* posix locking doesn't need additional lockingContext information */
      pNew->pMethod = &sqlite3UnixIoMethod;
      break;
    case noLockingStyle:
    case unsupportedLockingStyle:
    default: 
      pNew->pMethod = &sqlite3NolockLockingUnixIoMethod;
  }





  OpenCounter(+1);



  return SQLITE_OK;


}
#else /* SQLITE_ENABLE_LOCKING_STYLE */
static int fillInUnixFile(
  int h,                 /* Open file descriptor on file being opened */
  int dirfd,
  sqlite3_file *pId,     /* Write to the unixFile structure here */
  const char *zFilename  /* Name of the file being opened */
){
  unixFile *pNew = (unixFile *)pId;
  int rc;

#ifdef FD_CLOEXEC
  fcntl(h, F_SETFD, fcntl(h, F_GETFD, 0) | FD_CLOEXEC);



#endif

  enterMutex();
  rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
  leaveMutex();
  if( rc ){
    if( dirfd>=0 ) close(dirfd);
    close(h);
    return rc;
  }

  OSTRACE3("OPEN    %-3d %s\n", h, zFilename);
  pNew->dirfd = -1;
  pNew->h = h;
  pNew->dirfd = dirfd;
  SET_THREADID(pNew);

  pNew->pMethod = &sqlite3UnixIoMethod;
  OpenCounter(+1);
  return SQLITE_OK;
}
#endif /* SQLITE_ENABLE_LOCKING_STYLE */

/*
** Open a file descriptor to the directory containing file zFilename.
** If successful, *pFd is set to the opened file descriptor and
** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
** value.







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<






>





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

>

>
>
>
>


<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<





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

<
<
<
>
|
>
|


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

<
<
<


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

|
|
<
<
|


<
|
|
<
<
<
<
<
|
<
<
|

<







2029
2030
2031
2032
2033
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
2110
2111
2112
2113
2114



2115

2116


2117
2118
2119
2120
2121
2122
2123
2124



2125
2126








2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140

2141






2142
2143

2144
2145
2146
2147
2148
2149
2150


2151
2152
2153

2154
2155





2156


2157
2158

2159
2160
2161
2162
2163
2164
2165
** Return the device characteristics for the file. This is always 0.
*/
static int unixDeviceCharacteristics(sqlite3_file *id){
  return 0;
}

/*


















** Initialize the contents of the unixFile structure pointed to by pId.

**
























































































** When locking extensions are enabled, the filepath and locking style 
** are needed to determine the unixFile pMethod to use for locking operations.
** The locking-style specific lockingContext data structure is created 
** and assigned here also.
*/
static int fillInUnixFile(
  sqlite3_vfs *pVfs,      /* Pointer to vfs object */
  int h,                  /* Open file descriptor of file being opened */
  int dirfd,              /* Directory file descriptor */
  sqlite3_file *pId,      /* Write to the unixFile structure here */
  const char *zFilename   /* Name of the file being opened */
){
  /* Macro to define the static contents of an sqlite3_io_methods 
  ** structure for a unix backend file. Different locking methods
  ** require different functions for the xClose, xLock, xUnlock and
  ** xCheckReservedLock methods.
  */
  #define IOMETHODS(xClose, xLock, xUnlock, xCheckReservedLock) {    \
    1,                          /* iVersion */                           \
    xClose,                     /* xClose */                             \
    unixRead,                   /* xRead */                              \
    unixWrite,                  /* xWrite */                             \
    unixTruncate,               /* xTruncate */                          \
    unixSync,                   /* xSync */                              \
    unixFileSize,               /* xFileSize */                          \
    xLock,                      /* xLock */                              \
    xUnlock,                    /* xUnlock */                            \
    xCheckReservedLock,         /* xCheckReservedLock */                 \
    unixFileControl,            /* xFileControl */                       \
    unixSectorSize,             /* xSectorSize */                        \
    unixDeviceCharacteristics   /* xDeviceCapabilities */                \
  }
  static sqlite3_io_methods aIoMethod[] = {
    IOMETHODS(unixClose, unixLock, unixUnlock, unixCheckReservedLock) 
#ifdef SQLITE_ENABLE_LOCKING_STYLE

   ,IOMETHODS(flockClose, flockLock, flockUnlock, flockCheckReservedLock)
   ,IOMETHODS(dotlockClose, dotlockLock, dotlockUnlock,dotlockCheckReservedLock)
   ,IOMETHODS(nolockClose, nolockLock, nolockUnlock, nolockCheckReservedLock)
   ,IOMETHODS(afpClose, afpLock, afpUnlock, afpCheckReservedLock)
#endif
  };

  int eLockingStyle;
  unixFile *pNew = (unixFile *)pId;
  int rc = SQLITE_OK;

  assert( pNew->pLock==NULL );
  assert( pNew->pOpen==NULL );



















  OSTRACE3("OPEN    %-3d %s\n", h, zFilename);    
  pNew->h = h;
  pNew->dirfd = dirfd;
  SET_THREADID(pNew);

  assert(LOCKING_STYLE_POSIX==1);
  assert(LOCKING_STYLE_FLOCK==2);
  assert(LOCKING_STYLE_DOTFILE==3);
  assert(LOCKING_STYLE_NONE==4);
  assert(LOCKING_STYLE_AFP==5);
  eLockingStyle = detectLockingStyle(pVfs, zFilename, h);
  pNew->pMethod = &aIoMethod[eLockingStyle-1];






  switch( eLockingStyle ){




    case LOCKING_STYLE_POSIX: {
      enterMutex();
      rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen);
      leaveMutex();
      break;
    }



#ifdef SQLITE_ENABLE_LOCKING_STYLE
    case LOCKING_STYLE_AFP: {
      /* AFP locking uses the file path so it needs to be included in
      ** the afpLockingContext.
      */
      afpLockingContext *pCtx;



      pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );

      if( pCtx==0 ){


        rc = SQLITE_NOMEM;
      }else{
        /* NB: zFilename exists and remains valid until the file is closed
        ** according to requirement F11141.  So we do not need to make a
        ** copy of the filename. */
        pCtx->filePath = zFilename;
        srandomdev();
      }



      break;
    }









    case LOCKING_STYLE_DOTFILE: {
      /* Dotfile locking uses the file path so it needs to be included in
      ** the dotlockLockingContext 
      */
      char *zLockFile;
      int nFilename;
      nFilename = strlen(zFilename) + 6;
      zLockFile = (char *)sqlite3_malloc(nFilename);
      if( zLockFile==0 ){
        rc = SQLITE_NOMEM;
      }else{
        sqlite3_snprintf(nFilename, zLockFile, "%s.lock", zFilename);
      }

      pNew->lockingContext = zLockFile;






      break;
    }


    case LOCKING_STYLE_FLOCK: 
    case LOCKING_STYLE_NONE: 
      break;
#endif
  }



  if( rc!=SQLITE_OK ){
    if( dirfd>=0 ) close(dirfd);
    close(h);

  }else{
    OpenCounter(+1);





  }


  return rc;
}


/*
** Open a file descriptor to the directory containing file zFilename.
** If successful, *pFd is set to the opened file descriptor and
** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
** value.
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479


2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
  **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
  */
  assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
  assert(isCreate==0 || isReadWrite);
  assert(isExclusive==0 || isCreate);
  assert(isDelete==0 || isCreate);


  /* The main DB, main journal, and master journal are never automatically
  ** deleted
  */
  assert( eType!=SQLITE_OPEN_MAIN_DB || !isDelete );
  assert( eType!=SQLITE_OPEN_MAIN_JOURNAL || !isDelete );
  assert( eType!=SQLITE_OPEN_MASTER_JOURNAL || !isDelete );

  /* Assert that the upper layer has set one of the "file-type" flags. */
  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
       || eType==SQLITE_OPEN_TRANSIENT_DB
  );



  if( !zName ){
    int rc;
    assert(isDelete && !isOpenDirectory);
    rc = getTempname(MAX_PATHNAME+1, zTmpname);
    if( rc!=SQLITE_OK ){
      return rc;
    }
    zName = zTmpname;
  }

  if( isReadonly )  oflags |= O_RDONLY;
  if( isReadWrite ) oflags |= O_RDWR;
  if( isCreate )    oflags |= O_CREAT;
  if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW);
  oflags |= (O_LARGEFILE|O_BINARY);

  memset(pFile, 0, sizeof(unixFile));
  fd = open(zName, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
  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);
    flags |= SQLITE_OPEN_READONLY;
    return unixOpen(pVfs, zPath, pFile, flags, pOutFlags);
  }







<













>
>

















<







2307
2308
2309
2310
2311
2312
2313

2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345

2346
2347
2348
2349
2350
2351
2352
  **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
  */
  assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
  assert(isCreate==0 || isReadWrite);
  assert(isExclusive==0 || isCreate);
  assert(isDelete==0 || isCreate);


  /* The main DB, main journal, and master journal are never automatically
  ** deleted
  */
  assert( eType!=SQLITE_OPEN_MAIN_DB || !isDelete );
  assert( eType!=SQLITE_OPEN_MAIN_JOURNAL || !isDelete );
  assert( eType!=SQLITE_OPEN_MASTER_JOURNAL || !isDelete );

  /* Assert that the upper layer has set one of the "file-type" flags. */
  assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB 
       || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL 
       || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL 
       || eType==SQLITE_OPEN_TRANSIENT_DB
  );

  memset(pFile, 0, sizeof(unixFile));

  if( !zName ){
    int rc;
    assert(isDelete && !isOpenDirectory);
    rc = getTempname(MAX_PATHNAME+1, zTmpname);
    if( rc!=SQLITE_OK ){
      return rc;
    }
    zName = zTmpname;
  }

  if( isReadonly )  oflags |= O_RDONLY;
  if( isReadWrite ) oflags |= O_RDWR;
  if( isCreate )    oflags |= O_CREAT;
  if( isExclusive ) oflags |= (O_EXCL|O_NOFOLLOW);
  oflags |= (O_LARGEFILE|O_BINARY);


  fd = open(zName, oflags, isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS);
  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);
    flags |= SQLITE_OPEN_READONLY;
    return unixOpen(pVfs, zPath, pFile, flags, pOutFlags);
  }
2516
2517
2518
2519
2520
2521
2522





2523
2524
2525
2526
2527
2528
2529
2530
  if( isOpenDirectory ){
    int rc = openDirectory(zPath, &dirfd);
    if( rc!=SQLITE_OK ){
      close(fd);
      return rc;
    }
  }





  return fillInUnixFile(fd, dirfd, pFile, zPath);
}

/*
** Delete the file at zPath. If the dirSync argument is true, fsync()
** the directory after deleting the file.
*/
static int unixDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){







>
>
>
>
>
|







2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
  if( isOpenDirectory ){
    int rc = openDirectory(zPath, &dirfd);
    if( rc!=SQLITE_OK ){
      close(fd);
      return rc;
    }
  }

#ifdef FD_CLOEXEC
  fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
#endif

  return fillInUnixFile(pVfs, fd, dirfd, pFile, zPath);
}

/*
** Delete the file at zPath. If the dirSync argument is true, fsync()
** the directory after deleting the file.
*/
static int unixDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
2776
2777
2778
2779
2780
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
2811
2812
2813
2814
}

static int unixGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
  return 0;
}

/*
** Initialize and deinitialize the operating system interface.
*/
int sqlite3_os_init(void){ 
  static sqlite3_vfs unixVfs = {





    1,                  /* iVersion */
    sizeof(unixFile),   /* szOsFile */
    MAX_PATHNAME,       /* mxPathname */
    0,                  /* pNext */
    "unix",             /* zName */
    0,                  /* pAppData */
  
    unixOpen,           /* xOpen */
    unixDelete,         /* xDelete */
    unixAccess,         /* xAccess */
    unixFullPathname,   /* xFullPathname */
    unixDlOpen,         /* xDlOpen */
    unixDlError,        /* xDlError */
    unixDlSym,          /* xDlSym */
    unixDlClose,        /* xDlClose */
    unixRandomness,     /* xRandomness */
    unixSleep,          /* xSleep */
    unixCurrentTime,    /* xCurrentTime */
    unixGetLastError    /* xGetLastError */












  };





  sqlite3_vfs_register(&unixVfs, 1);
  return SQLITE_OK; 
}




int sqlite3_os_end(void){ 
  return SQLITE_OK; 
}
 
#endif /* SQLITE_OS_UNIX */







|


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

>
>
>
>
>



>
>
>
>





2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649

2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
}

static int unixGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
  return 0;
}

/*
** Initialize the operating system interface.
*/
int sqlite3_os_init(void){ 
  /* Macro to define the static contents of an sqlite3_vfs structure for
  ** the unix backend. The two parameters are the values to use for
  ** the sqlite3_vfs.zName and sqlite3_vfs.pAppData fields, respectively.
  ** 
  */
  #define UNIXVFS(zVfsName, pVfsAppData) {                  \
    1,                    /* iVersion */                    \
    sizeof(unixFile),     /* szOsFile */                    \
    MAX_PATHNAME,         /* mxPathname */                  \
    0,                    /* pNext */                       \
    zVfsName,             /* zName */                       \

    (void *)pVfsAppData,  /* pAppData */                    \
    unixOpen,             /* xOpen */                       \
    unixDelete,           /* xDelete */                     \
    unixAccess,           /* xAccess */                     \
    unixFullPathname,     /* xFullPathname */               \
    unixDlOpen,           /* xDlOpen */                     \
    unixDlError,          /* xDlError */                    \
    unixDlSym,            /* xDlSym */                      \
    unixDlClose,          /* xDlClose */                    \
    unixRandomness,       /* xRandomness */                 \
    unixSleep,            /* xSleep */                      \
    unixCurrentTime,      /* xCurrentTime */                \
    unixGetLastError      /* xGetLastError */               \
  }

  static sqlite3_vfs unixVfs = UNIXVFS("unix", 0);
#ifdef SQLITE_ENABLE_LOCKING_STYLE
#if 0
  int i;
  static sqlite3_vfs aVfs[] = {
    UNIXVFS("unix-posix",   LOCKING_STYLE_POSIX), 
    UNIXVFS("unix-afp",     LOCKING_STYLE_AFP), 
    UNIXVFS("unix-flock",   LOCKING_STYLE_FLOCK), 
    UNIXVFS("unix-dotfile", LOCKING_STYLE_DOTFILE), 
    UNIXVFS("unix-none",    LOCKING_STYLE_NONE)
  };
  for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
    sqlite3_vfs_register(&aVfs[i], 0);
  }
#endif
#endif
  sqlite3_vfs_register(&unixVfs, 1);
  return SQLITE_OK; 
}

/*
** Shutdown the operating system interface. This is a no-op for unix.
*/
int sqlite3_os_end(void){ 
  return SQLITE_OK; 
}
 
#endif /* SQLITE_OS_UNIX */
Changes to src/sqlite.h.in.
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
** on how SQLite interfaces are suppose to operate.
**
** The name of this file under configuration management is "sqlite.h.in".
** The makefile makes some minor changes to this file (such as inserting
** the version number) and changes its name to "sqlite3.h" as
** part of the build process.
**
** @(#) $Id: sqlite.h.in,v 1.361 2008/06/27 14:51:53 drh Exp $
*/
#ifndef _SQLITE3_H_
#define _SQLITE3_H_
#include <stdarg.h>     /* Needed for the definition of va_list */

/*
** Make sure we can call this stuff from C++.







|







26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
** on how SQLite interfaces are suppose to operate.
**
** The name of this file under configuration management is "sqlite.h.in".
** The makefile makes some minor changes to this file (such as inserting
** the version number) and changes its name to "sqlite3.h" as
** part of the build process.
**
** @(#) $Id: sqlite.h.in,v 1.362 2008/06/28 11:23:00 danielk1977 Exp $
*/
#ifndef _SQLITE3_H_
#define _SQLITE3_H_
#include <stdarg.h>     /* Needed for the definition of va_list */

/*
** Make sure we can call this stuff from C++.
1105
1106
1107
1108
1109
1110
1111
1112


1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
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
** <dd>This option specifies a static memory buffer that SQLite will use
** for all of its dynamic memory allocation needs beyond those provided
** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
** There are three arguments: A pointer to the memory, the number of
** bytes in the memory buffer, and the minimum allocation size.  When
** this configuration option is used, SQLite never calls the system
** malloc() implementation but instead uses the supplied memory buffer
** to satisfy all [sqlite3_malloc()] requests.</dd>


**
** <dt>SQLITE_CONFIG_MUTEX</dt>
** <dd>This option takes a single argument which is a pointer to an
** instance of the [sqlite3_mutex_methods] structure.  The argument specifies
** alternative low-level mutex routines to be used in place
** the mutex routines built into SQLite.</dd>
**
** <dt>SQLITE_CONFIG_GETMALLOC</dt>
** <dd>This option takes a single argument which is a pointer to an
** instance of the [sqlite3_mutex_methods] structure.  The
** [sqlite3_mutex_methods]
** structure is filled with the currently defined mutex routines.
** This option can be used to overload the default mutex allocation
** routines with a wrapper used to track mutex usage for performance
** profiling or testing, for example.</dd>
**
** <dt>SQLITE_CONFIG_MEMSYS3</dt>
** <dd>This option is only available if SQLite is compiled with the 
** SQLITE_ENABLE_MEMSYS3 symbol defined. If available, then it is used
** to install an alternative set of built-in memory allocation routines
** known as the "memsys3" allocator (in the same way as SQLITE_CONFIG_MALLOC
** may be used to install an external set of memory allocation routines).
** This options must be passed two arguments, a pointer to a large blob of
** allocated memory (type char*) and the size of the block of memory in bytes
** (type int). The memsys3 allocator manages this block of memory and uses
** it to satisfy all requests for dynamic memory made by the library. The
** caller must ensure that the block of memory remains valid for as long
** as the memsys3 allocator is in use.</dd>
**
** <dt>SQLITE_CONFIG_MEMSYS5</dt>
** <dd>This option is only available if SQLite is compiled with the 
** SQLITE_ENABLE_MEMSYS5 symbol defined. If available, then it is similar

** to the SQLITE_CONFIG_MEMSYS3 option. The "memsys5" allocator differs
** from the "memsys3" allocator in that it rounds all allocations up to
** the next largest power of two. Although this is sometimes more wasteful
** than the procedures used by memsys3, it guarantees an upper limit on
** internal fragmentation.</dd>


** </dl>
*/
#define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
#define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
#define SQLITE_CONFIG_SERIALIZED    3  /* nil */
#define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
#define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
#define SQLITE_CONFIG_SCRATCH       6  /* void*, int sz, int N */
#define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
#define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
#define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
#define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* */
#define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* */
#define SQLITE_CONFIG_MEMSYS3      12  /* u8*, int */
#define SQLITE_CONFIG_MEMSYS5      13  /* u8*, int */

/*
** CAPI3REF: Enable Or Disable Extended Result Codes {F12200}
**
** The sqlite3_extended_result_codes() routine enables or disables the
** [extended result codes] feature of SQLite. The extended result
** codes are disabled by default for historical compatibility considerations.







|
>
>


















|
|
|
<
<
<
<
<
<
|



|
>
|
|
<
<
<
>
>
|












|
|







1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135






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
** <dd>This option specifies a static memory buffer that SQLite will use
** for all of its dynamic memory allocation needs beyond those provided
** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
** There are three arguments: A pointer to the memory, the number of
** bytes in the memory buffer, and the minimum allocation size.  When
** this configuration option is used, SQLite never calls the system
** malloc() implementation but instead uses the supplied memory buffer
** to satisfy all [sqlite3_malloc()] requests. This option is only
** available if either or both of SQLITE_ENABLE_MEMSYS3 and 
** SQLITE_ENABLE_MEMSYS5 are defined during compilation.</dd>
**
** <dt>SQLITE_CONFIG_MUTEX</dt>
** <dd>This option takes a single argument which is a pointer to an
** instance of the [sqlite3_mutex_methods] structure.  The argument specifies
** alternative low-level mutex routines to be used in place
** the mutex routines built into SQLite.</dd>
**
** <dt>SQLITE_CONFIG_GETMALLOC</dt>
** <dd>This option takes a single argument which is a pointer to an
** instance of the [sqlite3_mutex_methods] structure.  The
** [sqlite3_mutex_methods]
** structure is filled with the currently defined mutex routines.
** This option can be used to overload the default mutex allocation
** routines with a wrapper used to track mutex usage for performance
** profiling or testing, for example.</dd>
**
** <dt>SQLITE_CONFIG_MEMSYS3</dt>
** <dd>This option is only available if SQLite is compiled with the 
** SQLITE_ENABLE_MEMSYS3 symbol defined. It selects one of two memory
** allocation systems that use the block of memory supplied to sqlite 
** using the SQLITE_CONFIG_HEAP option.






** </dd>
**
** <dt>SQLITE_CONFIG_MEMSYS5</dt>
** <dd>This option is only available if SQLite is compiled with the 
** SQLITE_ENABLE_MEMSYS5 symbol defined. It selects one of two memory
** allocation systems that use the block of memory supplied to sqlite 
** using the SQLITE_CONFIG_HEAP option. The memory allocation system
** selected by this option, "memsys5", is also installed by default 



** when the SQLITE_CONFIG_HEAP option is set, so it is not usually
** necessary to use this option directly.
** </dd>
*/
#define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
#define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
#define SQLITE_CONFIG_SERIALIZED    3  /* nil */
#define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
#define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
#define SQLITE_CONFIG_SCRATCH       6  /* void*, int sz, int N */
#define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
#define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
#define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
#define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* */
#define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* */
#define SQLITE_CONFIG_MEMSYS3      12  /* nil */
#define SQLITE_CONFIG_MEMSYS5      13  /* nil */

/*
** CAPI3REF: Enable Or Disable Extended Result Codes {F12200}
**
** The sqlite3_extended_result_codes() routine enables or disables the
** [extended result codes] feature of SQLite. The extended result
** codes are disabled by default for historical compatibility considerations.
Changes to src/test1.c.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
**    May you share freely, never taking more than you give.
**
*************************************************************************
** Code for testing all sorts of SQLite interfaces.  This code
** is not included in the SQLite library.  It is used for automated
** testing of the SQLite library.
**
** $Id: test1.c,v 1.307 2008/06/26 10:41:19 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "tcl.h"
#include <stdlib.h>
#include <string.h>

/*







|







9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
**    May you share freely, never taking more than you give.
**
*************************************************************************
** Code for testing all sorts of SQLite interfaces.  This code
** is not included in the SQLite library.  It is used for automated
** testing of the SQLite library.
**
** $Id: test1.c,v 1.308 2008/06/28 11:23:00 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "tcl.h"
#include <stdlib.h>
#include <string.h>

/*
4444
4445
4446
4447
4448
4449
4450
























4451
4452
4453
4454
4455
4456
4457
  assert( rc==SQLITE_ERROR );
  rc = sqlite3_file_control(db, "main", -1, &iArg);
  assert( rc==SQLITE_ERROR );
  rc = sqlite3_file_control(db, "temp", -1, &iArg);
  assert( rc==SQLITE_ERROR );
  return TCL_OK;  
}

























/*
** tclcmd:   sqlite3_limit DB ID VALUE
**
** This TCL command runs the sqlite3_limit interface and
** verifies correct operation of the same.
*/







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







4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
  assert( rc==SQLITE_ERROR );
  rc = sqlite3_file_control(db, "main", -1, &iArg);
  assert( rc==SQLITE_ERROR );
  rc = sqlite3_file_control(db, "temp", -1, &iArg);
  assert( rc==SQLITE_ERROR );
  return TCL_OK;  
}

/*
** tclcmd:   sqlite3_vfs_list
**
**   Return a tcl list containing the names of all registered vfs's.
*/
static int vfs_list(
  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_vfs *pVfs;
  Tcl_Obj *pRet = Tcl_NewObj();
  if( objc!=1 ){
    Tcl_WrongNumArgs(interp, 1, objv, "");
    return TCL_ERROR;
  }
  for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
    Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(pVfs->zName, -1));
  }
  Tcl_SetObjResult(interp, pRet);
  return TCL_OK;  
}

/*
** tclcmd:   sqlite3_limit DB ID VALUE
**
** This TCL command runs the sqlite3_limit interface and
** verifies correct operation of the same.
*/
4689
4690
4691
4692
4693
4694
4695

4696
4697
4698
4699
4700
4701
4702
#endif
#endif
     { "sqlite3_create_collation_v2", test_create_collation_v2, 0 },
     { "sqlite3_global_recover",     test_global_recover, 0   },
     { "working_64bit_int",          working_64bit_int,   0   },
     { "vfs_unlink_test",            vfs_unlink_test,     0   },
     { "file_control_test",          file_control_test,   0   },


     /* Functions from os.h */
#ifndef SQLITE_OMIT_DISKIO
#if 0
     { "sqlite3OsOpenReadWrite",test_sqlite3OsOpenReadWrite, 0 },
     { "sqlite3OsClose",        test_sqlite3OsClose, 0 },
     { "sqlite3OsLock",         test_sqlite3OsLock, 0 },







>







4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
#endif
#endif
     { "sqlite3_create_collation_v2", test_create_collation_v2, 0 },
     { "sqlite3_global_recover",     test_global_recover, 0   },
     { "working_64bit_int",          working_64bit_int,   0   },
     { "vfs_unlink_test",            vfs_unlink_test,     0   },
     { "file_control_test",          file_control_test,   0   },
     { "sqlite3_vfs_list",           vfs_list,     0   },

     /* Functions from os.h */
#ifndef SQLITE_OMIT_DISKIO
#if 0
     { "sqlite3OsOpenReadWrite",test_sqlite3OsOpenReadWrite, 0 },
     { "sqlite3OsClose",        test_sqlite3OsClose, 0 },
     { "sqlite3OsLock",         test_sqlite3OsLock, 0 },
Added test/lock5.test.




































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
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
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
72
73
74
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
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
# 2008 June 28
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this script is database locks.
#
# $Id: lock5.test,v 1.1 2008/06/28 11:23:00 danielk1977 Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

# This file is only run if using the unix backend compiled with the
# SQLITE_ENABLE_LOCKING_STYLE macro.
db close
if {[catch {sqlite3 db test.db -vfs unix-none} msg]} {
puts $msg
  finish_test
  return
}
db close

do_test lock5-dotfile.1 {
  sqlite3 db test.db -vfs unix-dotfile
  execsql {
    BEGIN;
    CREATE TABLE t1(a, b);
  }
} {}

do_test lock5-dotfile.2 {
  file exists test.db.lock
} {1}

do_test lock5-dotfile.3 {
  execsql COMMIT
  file exists test.db.lock
} {0}

do_test lock5-dotfile.4 {
  sqlite3 db2 test.db -vfs unix-dotfile
  execsql {
    INSERT INTO t1 VALUES('a', 'b');
    SELECT * FROM t1;
  } db2
} {a b}

do_test lock5-dotfile.5 {
  execsql {
    BEGIN;
    SELECT * FROM t1;
  } db2
} {a b}

do_test lock5-dotfile.6 {
  file exists test.db.lock
} {1}

do_test lock5-dotfile.7 {
  catchsql { SELECT * FROM t1; }
} {1 {database is locked}}

do_test lock5-dotfile.8 {
  execsql {
    SELECT * FROM t1;
    ROLLBACK;
  } db2
} {a b}

do_test lock5-dotfile.9 {
  catchsql { SELECT * FROM t1; }
} {0 {a b}}

do_test lock5-dotfile.10 {
  file exists test.db.lock
} {0}

do_test lock5-dotfile.X {
  db2 close
  execsql {BEGIN EXCLUSIVE}
  db close
  file exists test.db.lock
} {0}

#####################################################################

file delete -force test.db

do_test lock5-flock.1 {
  sqlite3 db test.db -vfs unix-flock
  execsql {
    CREATE TABLE t1(a, b);
    BEGIN;
    INSERT INTO t1 VALUES(1, 2);
  }
} {}

# Make sure we are not accidentally using the dotfile locking scheme.
do_test lock5-flock.2 {
  file exists test.db.lock
} {0}

do_test lock5-flock.3 {
  sqlite3 db2 test.db -vfs unix-flock
  catchsql { SELECT * FROM t1 } db2
} {1 {database is locked}}

do_test lock5-flock.4 {
  execsql COMMIT
  catchsql { SELECT * FROM t1 } db2
} {0 {1 2}}

do_test lock5-flock.5 {
  execsql BEGIN
  catchsql { SELECT * FROM t1 } db2
} {0 {1 2}}

do_test lock5-flock.6 {
  execsql {SELECT * FROM t1}
  catchsql { SELECT * FROM t1 } db2
} {1 {database is locked}}

do_test lock5-flock.7 {
  db close
  catchsql { SELECT * FROM t1 } db2
} {0 {1 2}}

do_test lock5-flock.8 {
  db2 close
} {}

#####################################################################

do_test lock5-none.1 {
  sqlite3 db test.db -vfs unix-none
  sqlite3 db2 test.db -vfs unix-none
  execsql {
    BEGIN;
    INSERT INTO t1 VALUES(3, 4);
  }
} {}
do_test lock5-none.2 {
  execsql { SELECT * FROM t1 }
} {1 2 3 4}
do_test lock5-flock.3 {
  execsql { SELECT * FROM t1 } db2
} {1 2}
do_test lock5-none.4 {
  execsql { 
    BEGIN;
    SELECT * FROM t1;
  } db2
} {1 2}
do_test lock5-none.5 {
  execsql COMMIT
  execsql {SELECT * FROM t1} db2
} {1 2}

ifcapable memorymanage {
  do_test lock5-none.6 {
    sqlite3_release_memory 1000000
    execsql {SELECT * FROM t1} db2
  } {1 2 3 4}
}

do_test lock5-flock.X {
  db close
  db2 close
} {}

finish_test

Changes to test/mutex1.test.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 2008 June 17
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#
# $Id: mutex1.test,v 1.5 2008/06/26 08:29:35 danielk1977 Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl
sqlite3_reset_auto_extension

proc mutex_counters {varname} {
  upvar $varname var











|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 2008 June 17
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#
# $Id: mutex1.test,v 1.6 2008/06/28 11:23:00 danielk1977 Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl
sqlite3_reset_auto_extension

proc mutex_counters {varname} {
  upvar $varname var
60
61
62
63
64
65
66
67

68
69
70
71
72
73
74
75

do_test mutex1-1.6 {
  sqlite3_initialize
} {SQLITE_OK}

do_test mutex1-1.7 {
  mutex_counters counters
  list $counters(total) $counters(static_master)

} {6 3}

do_test mutex1-1.8 {
  clear_mutex_counters
  sqlite3_initialize
} {SQLITE_OK}

do_test mutex1-1.9 {







|
>
|







60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

do_test mutex1-1.6 {
  sqlite3_initialize
} {SQLITE_OK}

do_test mutex1-1.7 {
  mutex_counters counters
  # list $counters(total) $counters(static_master)
  expr {$counters(total)>0}
} {1}

do_test mutex1-1.8 {
  clear_mutex_counters
  sqlite3_initialize
} {SQLITE_OK}

do_test mutex1-1.9 {
88
89
90
91
92
93
94






95
96
97
98
99
100
101

ifcapable threadsafe {
  foreach {mode mutexes} {
    singlethread {}
    multithread  {fast static_master static_mem static_prng}
    serialized   {fast recursive static_master static_mem static_prng}
  } {






    do_test mutex1.2.$mode.1 {
      catch {db close}
      sqlite3_shutdown
      sqlite3_config $mode
    } SQLITE_OK

    do_test mutex1.2.$mode.2 {







>
>
>
>
>
>







89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

ifcapable threadsafe {
  foreach {mode mutexes} {
    singlethread {}
    multithread  {fast static_master static_mem static_prng}
    serialized   {fast recursive static_master static_mem static_prng}
  } {
    ifcapable memorymanage {
      if {$mode ne "singlethread"} {
        lappend mutexes static_lru static_lru2 static_mem2
        set mutexes [lsort $mutexes]
      }
    }
    do_test mutex1.2.$mode.1 {
      catch {db close}
      sqlite3_shutdown
      sqlite3_config $mode
    } SQLITE_OK

    do_test mutex1.2.$mode.2 {