/ Check-in [2b1884dc]
Login

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

Overview
Comment:Actually look at i-node numbers to determine whether or not the database file has moved.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | detect-moved-db
Files: files | file ages | folders
SHA1:2b1884dc14f9a04a04eebb3245fbe0daaff399eb
User & Date: drh 2013-12-07 12:29:22
Context
2013-12-07
16:45
Back out the new device capability. The determination of whether or not a file has moved is now done strictly using a file-control. Closed-Leaf check-in: 9c59f5af user: drh tags: detect-moved-db
12:29
Actually look at i-node numbers to determine whether or not the database file has moved. check-in: 2b1884dc user: drh tags: detect-moved-db
2013-12-06
19:58
Add the SQLITE_READONLY_DBMOVED error code to the sqlite3ErrName() function. check-in: 7789f801 user: mistachkin tags: detect-moved-db
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/os_unix.c.

1311
1312
1313
1314
1315
1316
1317









1318
1319
1320
1321
1322
1323
1324
....
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
....
3796
3797
3798
3799
3800
3801
3802




3803
3804
3805
3806
3807
3808
3809
  }else{
    pInode->nRef++;
  }
  *ppInode = pInode;
  return SQLITE_OK;
}











/*
** Check a unixFile that is a database.  Verify the following:
**
** (1) There is exactly one hard link on the file
** (2) The file is not a symbolic link
** (3) The file has not been renamed or unlinked
................................................................................
    return;
  }
  if( buf.st_nlink>1 ){
    sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
    pFile->ctrlFlags |= UNIXFILE_WARNED;
    return;
  }
  if( pFile->pInode!=0
   && ((rc = osStat(pFile->zPath, &buf))!=0
       || buf.st_ino!=pFile->pInode->fileId.ino)
  ){
    sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
    pFile->ctrlFlags |= UNIXFILE_WARNED;
    return;
  }
}


................................................................................
    case SQLITE_FCNTL_TEMPFILENAME: {
      char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname );
      if( zTFile ){
        unixGetTempname(pFile->pVfs->mxPathname, zTFile);
        *(char**)pArg = zTFile;
      }
      return SQLITE_OK;




    }
#if SQLITE_MAX_MMAP_SIZE>0
    case SQLITE_FCNTL_MMAP_SIZE: {
      i64 newLimit = *(i64*)pArg;
      int rc = SQLITE_OK;
      if( newLimit>sqlite3GlobalConfig.mxMmap ){
        newLimit = sqlite3GlobalConfig.mxMmap;







>
>
>
>
>
>
>
>
>







 







|
<
<
<







 







>
>
>
>







1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
....
1354
1355
1356
1357
1358
1359
1360
1361



1362
1363
1364
1365
1366
1367
1368
....
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
  }else{
    pInode->nRef++;
  }
  *ppInode = pInode;
  return SQLITE_OK;
}

/*
** Return TRUE if pFile has been renamed or unlinked since it was first opened.
*/
static int fileHasMoved(unixFile *pFile){
  struct stat buf;
  return pFile->pInode!=0 &&
         (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino);
}


/*
** Check a unixFile that is a database.  Verify the following:
**
** (1) There is exactly one hard link on the file
** (2) The file is not a symbolic link
** (3) The file has not been renamed or unlinked
................................................................................
    return;
  }
  if( buf.st_nlink>1 ){
    sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
    pFile->ctrlFlags |= UNIXFILE_WARNED;
    return;
  }
  if( fileHasMoved(pFile) ){



    sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
    pFile->ctrlFlags |= UNIXFILE_WARNED;
    return;
  }
}


................................................................................
    case SQLITE_FCNTL_TEMPFILENAME: {
      char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname );
      if( zTFile ){
        unixGetTempname(pFile->pVfs->mxPathname, zTFile);
        *(char**)pArg = zTFile;
      }
      return SQLITE_OK;
    }
    case SQLITE_FCNTL_HAS_MOVED: {
      *(int*)pArg = fileHasMoved(pFile);
      return SQLITE_OK;
    }
#if SQLITE_MAX_MMAP_SIZE>0
    case SQLITE_FCNTL_MMAP_SIZE: {
      i64 newLimit = *(i64*)pArg;
      int rc = SQLITE_OK;
      if( newLimit>sqlite3GlobalConfig.mxMmap ){
        newLimit = sqlite3GlobalConfig.mxMmap;

Changes to src/pager.c.

4802
4803
4804
4805
4806
4807
4808

4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
** code from sqlite3OsAccess()) if the database has gone missing.
*/
static int databaseIsUnmoved(Pager *pPager){
  const int fixedFlags = SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
                         SQLITE_IOCAP_UNMOVABLE_WHEN_OPEN;
  int dc;

  int x = 0, rc;

  if( pPager->tempFile ) return SQLITE_OK;
  if( pPager->dbSize==0 ) return SQLITE_OK;
  assert( pPager->zFilename && pPager->zFilename[0] );
  dc = sqlite3OsDeviceCharacteristics(pPager->fd);
  if( (dc&fixedFlags)==fixedFlags ) return SQLITE_OK;
  rc = sqlite3OsAccess(pPager->pVfs, pPager->zFilename, SQLITE_ACCESS_EXISTS, &x);
  if( rc==SQLITE_OK && !x ) rc = SQLITE_READONLY_DBMOVED;
  return rc;
}


/*
** This function is called after transitioning from PAGER_UNLOCK to
** PAGER_SHARED state. It tests if there is a hot journal present in







>
|






|
|







4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
** code from sqlite3OsAccess()) if the database has gone missing.
*/
static int databaseIsUnmoved(Pager *pPager){
  const int fixedFlags = SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
                         SQLITE_IOCAP_UNMOVABLE_WHEN_OPEN;
  int dc;
  int bHasMoved = 0;
  int rc;

  if( pPager->tempFile ) return SQLITE_OK;
  if( pPager->dbSize==0 ) return SQLITE_OK;
  assert( pPager->zFilename && pPager->zFilename[0] );
  dc = sqlite3OsDeviceCharacteristics(pPager->fd);
  if( (dc&fixedFlags)==fixedFlags ) return SQLITE_OK;
  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
  if( rc==SQLITE_OK && bHasMoved ) rc = SQLITE_READONLY_DBMOVED;
  return rc;
}


/*
** This function is called after transitioning from PAGER_UNLOCK to
** PAGER_SHARED state. It tests if there is a hot journal present in

Changes to src/sqlite.h.in.

916
917
918
919
920
921
922






923
924
925
926
927
928
929
...
936
937
938
939
940
941
942

943
944
945
946
947
948
949
** The [SQLITE_FCNTL_TRACE] file control provides advisory information
** to the VFS about what the higher layers of the SQLite stack are doing.
** This file control is used by some VFS activity tracing [shims].
** The argument is a zero-terminated string.  Higher layers in the
** SQLite stack may generate instances of this file control if
** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
**






** </ul>
*/
#define SQLITE_FCNTL_LOCKSTATE               1
#define SQLITE_GET_LOCKPROXYFILE             2
#define SQLITE_SET_LOCKPROXYFILE             3
#define SQLITE_LAST_ERRNO                    4
#define SQLITE_FCNTL_SIZE_HINT               5
................................................................................
#define SQLITE_FCNTL_VFSNAME                12
#define SQLITE_FCNTL_POWERSAFE_OVERWRITE    13
#define SQLITE_FCNTL_PRAGMA                 14
#define SQLITE_FCNTL_BUSYHANDLER            15
#define SQLITE_FCNTL_TEMPFILENAME           16
#define SQLITE_FCNTL_MMAP_SIZE              18
#define SQLITE_FCNTL_TRACE                  19


/*
** CAPI3REF: Mutex Handle
**
** The mutex module within SQLite defines [sqlite3_mutex] to be an
** abstract type for a mutex object.  The SQLite core never looks
** at the internal representation of an [sqlite3_mutex].  It only







>
>
>
>
>
>







 







>







916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
...
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
** The [SQLITE_FCNTL_TRACE] file control provides advisory information
** to the VFS about what the higher layers of the SQLite stack are doing.
** This file control is used by some VFS activity tracing [shims].
** The argument is a zero-terminated string.  Higher layers in the
** SQLite stack may generate instances of this file control if
** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
**
** <li>[[SQLITE_FCNTL_HAS_MOVED]]
** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a
** pointer to an integer and it writes a boolean into that integer depending
** on whether or not the file has been renamed, moved, or deleted since it
** was first opened.
**
** </ul>
*/
#define SQLITE_FCNTL_LOCKSTATE               1
#define SQLITE_GET_LOCKPROXYFILE             2
#define SQLITE_SET_LOCKPROXYFILE             3
#define SQLITE_LAST_ERRNO                    4
#define SQLITE_FCNTL_SIZE_HINT               5
................................................................................
#define SQLITE_FCNTL_VFSNAME                12
#define SQLITE_FCNTL_POWERSAFE_OVERWRITE    13
#define SQLITE_FCNTL_PRAGMA                 14
#define SQLITE_FCNTL_BUSYHANDLER            15
#define SQLITE_FCNTL_TEMPFILENAME           16
#define SQLITE_FCNTL_MMAP_SIZE              18
#define SQLITE_FCNTL_TRACE                  19
#define SQLITE_FCNTL_HAS_MOVED              20

/*
** CAPI3REF: Mutex Handle
**
** The mutex module within SQLite defines [sqlite3_mutex] to be an
** abstract type for a mutex object.  The SQLite core never looks
** at the internal representation of an [sqlite3_mutex].  It only

Changes to test/pager4.test.

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
file rename test.db test-xyz.db
do_catchsql_test pager4-1.2 {
  SELECT * FROM t1;
} {0 {673 stone philips}}
do_catchsql_test pager4-1.3 {
  UPDATE t1 SET a=537;
} {1 {attempt to write a readonly database}}










# Changing the name back clears the READONLY error
#


file rename test-xyz.db test.db
do_catchsql_test pager4-1.4 {
  SELECT * FROM t1;
} {0 {673 stone philips}}
do_catchsql_test pager4-1.5 {
  UPDATE t1 SET a=537;
  SELECT * FROM t1;
} {0 {537 stone philips}}

# We can write to a renamed database if journal_mode=OFF or
# journal_mode=MEMORY.
#
file rename test.db test-xyz.db
do_catchsql_test pager4-1.6 {
  PRAGMA journal_mode=OFF;
  UPDATE t1 SET a=107;
  SELECT * FROM t1;
} {0 {off 107 stone philips}}
do_catchsql_test pager4-1.7 {
  PRAGMA journal_mode=MEMORY;
  UPDATE t1 SET b='magpie';
  SELECT * FROM t1;
} {0 {memory 107 magpie philips}}

# Any other journal mode gives a READONLY error
#
do_catchsql_test pager4-1.8 {
  PRAGMA journal_mode=DELETE;
  UPDATE t1 SET c='jaguar';
} {1 {attempt to write a readonly database}}
do_catchsql_test pager4-1.9 {
  PRAGMA journal_mode=TRUNCATE;
  UPDATE t1 SET c='jaguar';
} {1 {attempt to write a readonly database}}
do_catchsql_test pager4-1.10 {
  PRAGMA journal_mode=PERSIST;
  UPDATE t1 SET c='jaguar';
} {1 {attempt to write a readonly database}}


finish_test







>
>
>
>
>
>
>
>
>



>
>

|


|








|




|







|



|



|






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
file rename test.db test-xyz.db
do_catchsql_test pager4-1.2 {
  SELECT * FROM t1;
} {0 {673 stone philips}}
do_catchsql_test pager4-1.3 {
  UPDATE t1 SET a=537;
} {1 {attempt to write a readonly database}}

# Creating a different database file with the same name of the original
# is detected and still leaves the database read-only.
#
sqlite3 db2 test.db
db2 eval {CREATE TABLE t2(x,y,z)}
do_catchsql_test pager4-1.4 {
  UPDATE t1 SET a=948;
} {1 {attempt to write a readonly database}}

# Changing the name back clears the READONLY error
#
db2 close
file delete -force test.db
file rename test-xyz.db test.db
do_catchsql_test pager4-1.5 {
  SELECT * FROM t1;
} {0 {673 stone philips}}
do_catchsql_test pager4-1.6 {
  UPDATE t1 SET a=537;
  SELECT * FROM t1;
} {0 {537 stone philips}}

# We can write to a renamed database if journal_mode=OFF or
# journal_mode=MEMORY.
#
file rename test.db test-xyz.db
do_catchsql_test pager4-1.7 {
  PRAGMA journal_mode=OFF;
  UPDATE t1 SET a=107;
  SELECT * FROM t1;
} {0 {off 107 stone philips}}
do_catchsql_test pager4-1.8 {
  PRAGMA journal_mode=MEMORY;
  UPDATE t1 SET b='magpie';
  SELECT * FROM t1;
} {0 {memory 107 magpie philips}}

# Any other journal mode gives a READONLY error
#
do_catchsql_test pager4-1.9 {
  PRAGMA journal_mode=DELETE;
  UPDATE t1 SET c='jaguar';
} {1 {attempt to write a readonly database}}
do_catchsql_test pager4-1.10 {
  PRAGMA journal_mode=TRUNCATE;
  UPDATE t1 SET c='jaguar';
} {1 {attempt to write a readonly database}}
do_catchsql_test pager4-1.11 {
  PRAGMA journal_mode=PERSIST;
  UPDATE t1 SET c='jaguar';
} {1 {attempt to write a readonly database}}


finish_test