Fossil SCM

When db_open_config() is called with the useAttach parameter set to non-zero, it may need to close and reopen the database using ATTACH if that was not done previously.

drh 2012-11-23 01:50 trunk merge
Commit fe453a4893ca5e6be46d265b7e6fdfd2ae5f0262
+35 -10
--- src/db.c
+++ src/db.c
@@ -740,17 +740,25 @@
740740
/*
741741
** zDbName is the name of a database file. If no other database
742742
** file is open, then open this one. If another database file is
743743
** already open, then attach zDbName using the name zLabel.
744744
*/
745
-void db_open_or_attach(const char *zDbName, const char *zLabel){
745
+void db_open_or_attach(
746
+ const char *zDbName,
747
+ const char *zLabel,
748
+ int *pWasAttached
749
+){
746750
if( !g.db ){
751
+ assert( g.zMainDbType==0 );
747752
g.db = openDatabase(zDbName);
748753
g.zMainDbType = zLabel;
749754
db_connection_init();
755
+ if ( pWasAttached ) *pWasAttached = 0;
750756
}else{
757
+ assert( g.zMainDbType!=0 );
751758
db_attach(zDbName, zLabel);
759
+ if ( pWasAttached ) *pWasAttached = 1;
752760
}
753761
}
754762
755763
/*
756764
** Open the user database in "~/.fossil". Create the database anew if
@@ -806,16 +814,18 @@
806814
zDbName = mprintf("%s/.fossil", zHome);
807815
#endif
808816
if( file_size(zDbName)<1024*3 ){
809817
db_init_database(zDbName, zConfigSchema, (char*)0);
810818
}
811
- g.useAttach = useAttach;
812819
if( useAttach ){
813
- db_open_or_attach(zDbName, "configdb");
820
+ db_open_or_attach(zDbName, "configdb", &g.useAttach);
814821
g.dbConfig = 0;
822
+ g.zConfigDbType = 0;
815823
}else{
824
+ g.useAttach = 0;
816825
g.dbConfig = openDatabase(zDbName);
826
+ g.zConfigDbType = "configdb";
817827
}
818828
g.configOpen = 1;
819829
free(zDbName);
820830
}
821831
@@ -850,11 +860,11 @@
850860
char *zVFileDef;
851861
852862
if( file_access(zDbName, F_OK) ) return 0;
853863
lsize = file_size(zDbName);
854864
if( lsize%1024!=0 || lsize<4096 ) return 0;
855
- db_open_or_attach(zDbName, "localdb");
865
+ db_open_or_attach(zDbName, "localdb", 0);
856866
zVFileDef = db_text(0, "SELECT sql FROM %s.sqlite_master"
857867
" WHERE name=='vfile'", db_name("localdb"));
858868
859869
/* If the "isexe" column is missing from the vfile table, then
860870
** add it now. This code added on 2010-03-06. After all users have
@@ -986,11 +996,11 @@
986996
g.json.resultCode = FSL_JSON_E_DB_NOT_VALID;
987997
#endif
988998
fossil_panic("not a valid repository: %s", zDbName);
989999
}
9901000
}
991
- db_open_or_attach(zDbName, "repository");
1001
+ db_open_or_attach(zDbName, "repository", 0);
9921002
g.repositoryOpen = 1;
9931003
g.zRepositoryName = mprintf("%s", zDbName);
9941004
/* Cache "allow-symlinks" option, because we'll need it on every stat call */
9951005
g.allowSymlinks = db_get_boolean("allow-symlinks", 0);
9961006
}
@@ -1109,11 +1119,11 @@
11091119
file_canonical_name(g.argv[2], &repo, 0);
11101120
zRepo = blob_str(&repo);
11111121
if( file_access(zRepo, 0) ){
11121122
fossil_fatal("no such file: %s", zRepo);
11131123
}
1114
- db_open_or_attach(zRepo, "test_repo");
1124
+ db_open_or_attach(zRepo, "test_repo", 0);
11151125
db_lset("repository", blob_str(&repo));
11161126
db_close(1);
11171127
}
11181128
11191129
@@ -1177,13 +1187,15 @@
11771187
g.localOpen = 0;
11781188
g.configOpen = 0;
11791189
sqlite3_wal_checkpoint(g.db, 0);
11801190
sqlite3_close(g.db);
11811191
g.db = 0;
1192
+ g.zMainDbType = 0;
11821193
if( g.dbConfig ){
11831194
sqlite3_close(g.dbConfig);
11841195
g.dbConfig = 0;
1196
+ g.zConfigDbType = 0;
11851197
}
11861198
}
11871199
11881200
11891201
/*
@@ -1630,19 +1642,32 @@
16301642
/*
16311643
** Swap the g.db and g.dbConfig connections so that the various db_* routines
16321644
** work on the ~/.fossil database instead of on the repository database.
16331645
** Be sure to swap them back after doing the operation.
16341646
**
1635
-** If g.useAttach that means the ~/.fossil database was opened with
1636
-** the useAttach flag set to 1. In that case no connection swap is required
1637
-** so this routine is a no-op.
1647
+** If the ~/.fossil database has already been opened as the main database or
1648
+** is attached to the main database, no connection swaps are required so this
1649
+** routine is a no-op.
16381650
*/
16391651
void db_swap_connections(void){
1640
- if( !g.useAttach ){
1652
+ /*
1653
+ ** When swapping the main database connection with the config database
1654
+ ** connection, the config database connection must be open (not simply
1655
+ ** attached); otherwise, the swap would end up leaving the main database
1656
+ ** connection invalid, defeating the very purpose of this routine. This
1657
+ ** same constraint also holds true when restoring the previously swapped
1658
+ ** database connection; otherwise, it means that no swap was performed
1659
+ ** because the main database connection was already pointing to the config
1660
+ ** database.
1661
+ */
1662
+ if( g.dbConfig ){
16411663
sqlite3 *dbTemp = g.db;
1664
+ const char *zTempDbType = g.zMainDbType;
16421665
g.db = g.dbConfig;
1666
+ g.zMainDbType = g.zConfigDbType;
16431667
g.dbConfig = dbTemp;
1668
+ g.zConfigDbType = zTempDbType;
16441669
}
16451670
}
16461671
16471672
/*
16481673
** Logic for reading potentially versioned settings from
16491674
--- src/db.c
+++ src/db.c
@@ -740,17 +740,25 @@
740 /*
741 ** zDbName is the name of a database file. If no other database
742 ** file is open, then open this one. If another database file is
743 ** already open, then attach zDbName using the name zLabel.
744 */
745 void db_open_or_attach(const char *zDbName, const char *zLabel){
 
 
 
 
746 if( !g.db ){
 
747 g.db = openDatabase(zDbName);
748 g.zMainDbType = zLabel;
749 db_connection_init();
 
750 }else{
 
751 db_attach(zDbName, zLabel);
 
752 }
753 }
754
755 /*
756 ** Open the user database in "~/.fossil". Create the database anew if
@@ -806,16 +814,18 @@
806 zDbName = mprintf("%s/.fossil", zHome);
807 #endif
808 if( file_size(zDbName)<1024*3 ){
809 db_init_database(zDbName, zConfigSchema, (char*)0);
810 }
811 g.useAttach = useAttach;
812 if( useAttach ){
813 db_open_or_attach(zDbName, "configdb");
814 g.dbConfig = 0;
 
815 }else{
 
816 g.dbConfig = openDatabase(zDbName);
 
817 }
818 g.configOpen = 1;
819 free(zDbName);
820 }
821
@@ -850,11 +860,11 @@
850 char *zVFileDef;
851
852 if( file_access(zDbName, F_OK) ) return 0;
853 lsize = file_size(zDbName);
854 if( lsize%1024!=0 || lsize<4096 ) return 0;
855 db_open_or_attach(zDbName, "localdb");
856 zVFileDef = db_text(0, "SELECT sql FROM %s.sqlite_master"
857 " WHERE name=='vfile'", db_name("localdb"));
858
859 /* If the "isexe" column is missing from the vfile table, then
860 ** add it now. This code added on 2010-03-06. After all users have
@@ -986,11 +996,11 @@
986 g.json.resultCode = FSL_JSON_E_DB_NOT_VALID;
987 #endif
988 fossil_panic("not a valid repository: %s", zDbName);
989 }
990 }
991 db_open_or_attach(zDbName, "repository");
992 g.repositoryOpen = 1;
993 g.zRepositoryName = mprintf("%s", zDbName);
994 /* Cache "allow-symlinks" option, because we'll need it on every stat call */
995 g.allowSymlinks = db_get_boolean("allow-symlinks", 0);
996 }
@@ -1109,11 +1119,11 @@
1109 file_canonical_name(g.argv[2], &repo, 0);
1110 zRepo = blob_str(&repo);
1111 if( file_access(zRepo, 0) ){
1112 fossil_fatal("no such file: %s", zRepo);
1113 }
1114 db_open_or_attach(zRepo, "test_repo");
1115 db_lset("repository", blob_str(&repo));
1116 db_close(1);
1117 }
1118
1119
@@ -1177,13 +1187,15 @@
1177 g.localOpen = 0;
1178 g.configOpen = 0;
1179 sqlite3_wal_checkpoint(g.db, 0);
1180 sqlite3_close(g.db);
1181 g.db = 0;
 
1182 if( g.dbConfig ){
1183 sqlite3_close(g.dbConfig);
1184 g.dbConfig = 0;
 
1185 }
1186 }
1187
1188
1189 /*
@@ -1630,19 +1642,32 @@
1630 /*
1631 ** Swap the g.db and g.dbConfig connections so that the various db_* routines
1632 ** work on the ~/.fossil database instead of on the repository database.
1633 ** Be sure to swap them back after doing the operation.
1634 **
1635 ** If g.useAttach that means the ~/.fossil database was opened with
1636 ** the useAttach flag set to 1. In that case no connection swap is required
1637 ** so this routine is a no-op.
1638 */
1639 void db_swap_connections(void){
1640 if( !g.useAttach ){
 
 
 
 
 
 
 
 
 
 
1641 sqlite3 *dbTemp = g.db;
 
1642 g.db = g.dbConfig;
 
1643 g.dbConfig = dbTemp;
 
1644 }
1645 }
1646
1647 /*
1648 ** Logic for reading potentially versioned settings from
1649
--- src/db.c
+++ src/db.c
@@ -740,17 +740,25 @@
740 /*
741 ** zDbName is the name of a database file. If no other database
742 ** file is open, then open this one. If another database file is
743 ** already open, then attach zDbName using the name zLabel.
744 */
745 void db_open_or_attach(
746 const char *zDbName,
747 const char *zLabel,
748 int *pWasAttached
749 ){
750 if( !g.db ){
751 assert( g.zMainDbType==0 );
752 g.db = openDatabase(zDbName);
753 g.zMainDbType = zLabel;
754 db_connection_init();
755 if ( pWasAttached ) *pWasAttached = 0;
756 }else{
757 assert( g.zMainDbType!=0 );
758 db_attach(zDbName, zLabel);
759 if ( pWasAttached ) *pWasAttached = 1;
760 }
761 }
762
763 /*
764 ** Open the user database in "~/.fossil". Create the database anew if
@@ -806,16 +814,18 @@
814 zDbName = mprintf("%s/.fossil", zHome);
815 #endif
816 if( file_size(zDbName)<1024*3 ){
817 db_init_database(zDbName, zConfigSchema, (char*)0);
818 }
 
819 if( useAttach ){
820 db_open_or_attach(zDbName, "configdb", &g.useAttach);
821 g.dbConfig = 0;
822 g.zConfigDbType = 0;
823 }else{
824 g.useAttach = 0;
825 g.dbConfig = openDatabase(zDbName);
826 g.zConfigDbType = "configdb";
827 }
828 g.configOpen = 1;
829 free(zDbName);
830 }
831
@@ -850,11 +860,11 @@
860 char *zVFileDef;
861
862 if( file_access(zDbName, F_OK) ) return 0;
863 lsize = file_size(zDbName);
864 if( lsize%1024!=0 || lsize<4096 ) return 0;
865 db_open_or_attach(zDbName, "localdb", 0);
866 zVFileDef = db_text(0, "SELECT sql FROM %s.sqlite_master"
867 " WHERE name=='vfile'", db_name("localdb"));
868
869 /* If the "isexe" column is missing from the vfile table, then
870 ** add it now. This code added on 2010-03-06. After all users have
@@ -986,11 +996,11 @@
996 g.json.resultCode = FSL_JSON_E_DB_NOT_VALID;
997 #endif
998 fossil_panic("not a valid repository: %s", zDbName);
999 }
1000 }
1001 db_open_or_attach(zDbName, "repository", 0);
1002 g.repositoryOpen = 1;
1003 g.zRepositoryName = mprintf("%s", zDbName);
1004 /* Cache "allow-symlinks" option, because we'll need it on every stat call */
1005 g.allowSymlinks = db_get_boolean("allow-symlinks", 0);
1006 }
@@ -1109,11 +1119,11 @@
1119 file_canonical_name(g.argv[2], &repo, 0);
1120 zRepo = blob_str(&repo);
1121 if( file_access(zRepo, 0) ){
1122 fossil_fatal("no such file: %s", zRepo);
1123 }
1124 db_open_or_attach(zRepo, "test_repo", 0);
1125 db_lset("repository", blob_str(&repo));
1126 db_close(1);
1127 }
1128
1129
@@ -1177,13 +1187,15 @@
1187 g.localOpen = 0;
1188 g.configOpen = 0;
1189 sqlite3_wal_checkpoint(g.db, 0);
1190 sqlite3_close(g.db);
1191 g.db = 0;
1192 g.zMainDbType = 0;
1193 if( g.dbConfig ){
1194 sqlite3_close(g.dbConfig);
1195 g.dbConfig = 0;
1196 g.zConfigDbType = 0;
1197 }
1198 }
1199
1200
1201 /*
@@ -1630,19 +1642,32 @@
1642 /*
1643 ** Swap the g.db and g.dbConfig connections so that the various db_* routines
1644 ** work on the ~/.fossil database instead of on the repository database.
1645 ** Be sure to swap them back after doing the operation.
1646 **
1647 ** If the ~/.fossil database has already been opened as the main database or
1648 ** is attached to the main database, no connection swaps are required so this
1649 ** routine is a no-op.
1650 */
1651 void db_swap_connections(void){
1652 /*
1653 ** When swapping the main database connection with the config database
1654 ** connection, the config database connection must be open (not simply
1655 ** attached); otherwise, the swap would end up leaving the main database
1656 ** connection invalid, defeating the very purpose of this routine. This
1657 ** same constraint also holds true when restoring the previously swapped
1658 ** database connection; otherwise, it means that no swap was performed
1659 ** because the main database connection was already pointing to the config
1660 ** database.
1661 */
1662 if( g.dbConfig ){
1663 sqlite3 *dbTemp = g.db;
1664 const char *zTempDbType = g.zMainDbType;
1665 g.db = g.dbConfig;
1666 g.zMainDbType = g.zConfigDbType;
1667 g.dbConfig = dbTemp;
1668 g.zConfigDbType = zTempDbType;
1669 }
1670 }
1671
1672 /*
1673 ** Logic for reading potentially versioned settings from
1674
+1 -1
--- src/file.c
+++ src/file.c
@@ -425,11 +425,11 @@
425425
char *zDate;
426426
i64 iMTime;
427427
if( g.argc!=4 ){
428428
usage("test-set-mtime FILENAME DATE/TIME");
429429
}
430
- db_open_or_attach(":memory:", "mem");
430
+ db_open_or_attach(":memory:", "mem", 0);
431431
iMTime = db_int64(0, "SELECT strftime('%%s',%Q)", g.argv[3]);
432432
zFile = g.argv[2];
433433
file_set_mtime(zFile, iMTime);
434434
iMTime = file_wd_mtime(zFile);
435435
zDate = db_text(0, "SELECT datetime(%lld, 'unixepoch')", iMTime);
436436
--- src/file.c
+++ src/file.c
@@ -425,11 +425,11 @@
425 char *zDate;
426 i64 iMTime;
427 if( g.argc!=4 ){
428 usage("test-set-mtime FILENAME DATE/TIME");
429 }
430 db_open_or_attach(":memory:", "mem");
431 iMTime = db_int64(0, "SELECT strftime('%%s',%Q)", g.argv[3]);
432 zFile = g.argv[2];
433 file_set_mtime(zFile, iMTime);
434 iMTime = file_wd_mtime(zFile);
435 zDate = db_text(0, "SELECT datetime(%lld, 'unixepoch')", iMTime);
436
--- src/file.c
+++ src/file.c
@@ -425,11 +425,11 @@
425 char *zDate;
426 i64 iMTime;
427 if( g.argc!=4 ){
428 usage("test-set-mtime FILENAME DATE/TIME");
429 }
430 db_open_or_attach(":memory:", "mem", 0);
431 iMTime = db_int64(0, "SELECT strftime('%%s',%Q)", g.argv[3]);
432 zFile = g.argv[2];
433 file_set_mtime(zFile, iMTime);
434 iMTime = file_wd_mtime(zFile);
435 zDate = db_text(0, "SELECT datetime(%lld, 'unixepoch')", iMTime);
436
+1
--- src/json.c
+++ src/json.c
@@ -1409,10 +1409,11 @@
14091409
INT(g, isHome);
14101410
INT(g, nAux);
14111411
INT(g, allowSymlinks);
14121412
14131413
CSTR(g, zMainDbType);
1414
+ CSTR(g, zConfigDbType);
14141415
CSTR(g, zHome);
14151416
CSTR(g, zLocalRoot);
14161417
CSTR(g, zPath);
14171418
CSTR(g, zExtra);
14181419
CSTR(g, zBaseURL);
14191420
--- src/json.c
+++ src/json.c
@@ -1409,10 +1409,11 @@
1409 INT(g, isHome);
1410 INT(g, nAux);
1411 INT(g, allowSymlinks);
1412
1413 CSTR(g, zMainDbType);
 
1414 CSTR(g, zHome);
1415 CSTR(g, zLocalRoot);
1416 CSTR(g, zPath);
1417 CSTR(g, zExtra);
1418 CSTR(g, zBaseURL);
1419
--- src/json.c
+++ src/json.c
@@ -1409,10 +1409,11 @@
1409 INT(g, isHome);
1410 INT(g, nAux);
1411 INT(g, allowSymlinks);
1412
1413 CSTR(g, zMainDbType);
1414 CSTR(g, zConfigDbType);
1415 CSTR(g, zHome);
1416 CSTR(g, zLocalRoot);
1417 CSTR(g, zPath);
1418 CSTR(g, zExtra);
1419 CSTR(g, zBaseURL);
1420
+1
--- src/main.c
+++ src/main.c
@@ -119,10 +119,11 @@
119119
int configOpen; /* True if the config database is open */
120120
sqlite3_int64 now; /* Seconds since 1970 */
121121
int repositoryOpen; /* True if the main repository database is open */
122122
char *zRepositoryName; /* Name of the repository database */
123123
const char *zMainDbType;/* "configdb", "localdb", or "repository" */
124
+ const char *zConfigDbType; /* "configdb", "localdb", or "repository" */
124125
const char *zHome; /* Name of user home directory */
125126
int localOpen; /* True if the local database is open */
126127
char *zLocalRoot; /* The directory holding the local database */
127128
int minPrefix; /* Number of digits needed for a distinct UUID */
128129
int fSqlTrace; /* True if --sqltrace flag is present */
129130
--- src/main.c
+++ src/main.c
@@ -119,10 +119,11 @@
119 int configOpen; /* True if the config database is open */
120 sqlite3_int64 now; /* Seconds since 1970 */
121 int repositoryOpen; /* True if the main repository database is open */
122 char *zRepositoryName; /* Name of the repository database */
123 const char *zMainDbType;/* "configdb", "localdb", or "repository" */
 
124 const char *zHome; /* Name of user home directory */
125 int localOpen; /* True if the local database is open */
126 char *zLocalRoot; /* The directory holding the local database */
127 int minPrefix; /* Number of digits needed for a distinct UUID */
128 int fSqlTrace; /* True if --sqltrace flag is present */
129
--- src/main.c
+++ src/main.c
@@ -119,10 +119,11 @@
119 int configOpen; /* True if the config database is open */
120 sqlite3_int64 now; /* Seconds since 1970 */
121 int repositoryOpen; /* True if the main repository database is open */
122 char *zRepositoryName; /* Name of the repository database */
123 const char *zMainDbType;/* "configdb", "localdb", or "repository" */
124 const char *zConfigDbType; /* "configdb", "localdb", or "repository" */
125 const char *zHome; /* Name of user home directory */
126 int localOpen; /* True if the local database is open */
127 char *zLocalRoot; /* The directory holding the local database */
128 int minPrefix; /* Number of digits needed for a distinct UUID */
129 int fSqlTrace; /* True if --sqltrace flag is present */
130
+1
--- src/main.c
+++ src/main.c
@@ -119,10 +119,11 @@
119119
int configOpen; /* True if the config database is open */
120120
sqlite3_int64 now; /* Seconds since 1970 */
121121
int repositoryOpen; /* True if the main repository database is open */
122122
char *zRepositoryName; /* Name of the repository database */
123123
const char *zMainDbType;/* "configdb", "localdb", or "repository" */
124
+ const char *zConfigDbType; /* "configdb", "localdb", or "repository" */
124125
const char *zHome; /* Name of user home directory */
125126
int localOpen; /* True if the local database is open */
126127
char *zLocalRoot; /* The directory holding the local database */
127128
int minPrefix; /* Number of digits needed for a distinct UUID */
128129
int fSqlTrace; /* True if --sqltrace flag is present */
129130
--- src/main.c
+++ src/main.c
@@ -119,10 +119,11 @@
119 int configOpen; /* True if the config database is open */
120 sqlite3_int64 now; /* Seconds since 1970 */
121 int repositoryOpen; /* True if the main repository database is open */
122 char *zRepositoryName; /* Name of the repository database */
123 const char *zMainDbType;/* "configdb", "localdb", or "repository" */
 
124 const char *zHome; /* Name of user home directory */
125 int localOpen; /* True if the local database is open */
126 char *zLocalRoot; /* The directory holding the local database */
127 int minPrefix; /* Number of digits needed for a distinct UUID */
128 int fSqlTrace; /* True if --sqltrace flag is present */
129
--- src/main.c
+++ src/main.c
@@ -119,10 +119,11 @@
119 int configOpen; /* True if the config database is open */
120 sqlite3_int64 now; /* Seconds since 1970 */
121 int repositoryOpen; /* True if the main repository database is open */
122 char *zRepositoryName; /* Name of the repository database */
123 const char *zMainDbType;/* "configdb", "localdb", or "repository" */
124 const char *zConfigDbType; /* "configdb", "localdb", or "repository" */
125 const char *zHome; /* Name of user home directory */
126 int localOpen; /* True if the local database is open */
127 char *zLocalRoot; /* The directory holding the local database */
128 int minPrefix; /* Number of digits needed for a distinct UUID */
129 int fSqlTrace; /* True if --sqltrace flag is present */
130

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button