Fossil SCM

Automatically delete the _FOSSIL_ file after a failed open. Ticket [d299fb9842d6bc]

drh 2011-05-02 14:29 trunk
Commit 0aee050f320050b1f47487deb9f476681038e816
1 file changed +25 -26
+25 -26
--- src/db.c
+++ src/db.c
@@ -81,19 +81,29 @@
8181
db_force_rollback();
8282
fossil_exit(1);
8383
}
8484
8585
static int nBegin = 0; /* Nesting depth of BEGIN */
86
-static int isNewRepo = 0; /* True if the repository is newly created */
8786
static int doRollback = 0; /* True to force a rollback */
8887
static int nCommitHook = 0; /* Number of commit hooks */
8988
static struct sCommitHook {
9089
int (*xHook)(void); /* Functions to call at db_end_transaction() */
9190
int sequence; /* Call functions in sequence order */
9291
} aHook[5];
9392
static Stmt *pAllStmt = 0; /* List of all unfinalized statements */
9493
static int nPrepare = 0; /* Number of calls to sqlite3_prepare() */
94
+static int nDeleteOnFail = 0; /* Number of entries in azDeleteOnFail[] */
95
+static char *azDeleteOnFail[3]; /* Files to delete on a failure */
96
+
97
+
98
+/*
99
+** Arrange for the given file to be deleted on a failure.
100
+*/
101
+void db_delete_on_failure(const char *zFilename){
102
+ assert( nDeleteOnFail<count(azDeleteOnFail) );
103
+ azDeleteOnFail[nDeleteOnFail++] = fossil_strdup(zFilename);
104
+}
95105
96106
/*
97107
** This routine is called by the SQLite commit-hook mechanism
98108
** just prior to each commit. All this routine does is verify
99109
** that nBegin really is zero. That insures that transactions
@@ -141,10 +151,11 @@
141151
142152
/*
143153
** Force a rollback and shutdown the database
144154
*/
145155
void db_force_rollback(void){
156
+ int i;
146157
static int busy = 0;
147158
if( busy || g.db==0 ) return;
148159
busy = 1;
149160
undo_rollback();
150161
while( pAllStmt ){
@@ -151,17 +162,16 @@
151162
db_finalize(pAllStmt);
152163
}
153164
if( nBegin ){
154165
sqlite3_exec(g.db, "ROLLBACK", 0, 0, 0);
155166
nBegin = 0;
156
- if( isNewRepo ){
157
- db_close(0);
158
- unlink(g.zRepositoryName);
159
- }
160167
}
161168
busy = 0;
162169
db_close(0);
170
+ for(i=0; i<nDeleteOnFail; i++){
171
+ unlink(azDeleteOnFail[i]);
172
+ }
163173
}
164174
165175
/*
166176
** Install a commit hook. Hooks are installed in sequence order.
167177
** It is an error to install the same commit hook more than once.
@@ -565,14 +575,10 @@
565575
}
566576
db_finalize(&s);
567577
return z;
568578
}
569579
570
-#if defined(_WIN32)
571
-extern char *sqlite3_win32_mbcs_to_utf8(const char*);
572
-#endif
573
-
574580
/*
575581
** Initialize a new database file with the given schema. If anything
576582
** goes wrong, call db_err() to exit.
577583
*/
578584
void db_init_database(
@@ -583,13 +589,10 @@
583589
sqlite3 *db;
584590
int rc;
585591
const char *zSql;
586592
va_list ap;
587593
588
-#if defined(_WIN32)
589
- zFileName = sqlite3_win32_mbcs_to_utf8(zFileName);
590
-#endif
591594
rc = sqlite3_open(zFileName, &db);
592595
if( rc!=SQLITE_OK ){
593596
db_err(sqlite3_errmsg(db));
594597
}
595598
sqlite3_busy_timeout(db, 5000);
@@ -631,13 +634,10 @@
631634
int rc;
632635
const char *zVfs;
633636
sqlite3 *db;
634637
635638
zVfs = getenv("FOSSIL_VFS");
636
-#if defined(_WIN32)
637
- zDbName = sqlite3_win32_mbcs_to_utf8(zDbName);
638
-#endif
639639
rc = sqlite3_open_v2(
640640
zDbName, &db,
641641
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
642642
zVfs
643643
);
@@ -660,13 +660,10 @@
660660
if( !g.db ){
661661
g.db = openDatabase(zDbName);
662662
g.zMainDbType = zLabel;
663663
db_connection_init();
664664
}else{
665
-#if defined(_WIN32)
666
- zDbName = sqlite3_win32_mbcs_to_utf8(zDbName);
667
-#endif
668665
db_multi_exec("ATTACH DATABASE %Q AS %s", zDbName, zLabel);
669666
}
670667
}
671668
672669
/*
@@ -1056,11 +1053,11 @@
10561053
zFilename,
10571054
zRepositorySchema1,
10581055
zRepositorySchema2,
10591056
(char*)0
10601057
);
1061
- isNewRepo = 1;
1058
+ db_delete_on_failure(zFilename);
10621059
}
10631060
10641061
/*
10651062
** Create the default user accounts in the USER table.
10661063
*/
@@ -1189,14 +1186,15 @@
11891186
db_open_repository(g.argv[2]);
11901187
db_open_config(0);
11911188
db_begin_transaction();
11921189
db_initial_setup(zDate, zDefaultUser, 1);
11931190
db_end_transaction(0);
1194
- printf("project-id: %s\n", db_get("project-code", 0));
1195
- printf("server-id: %s\n", db_get("server-code", 0));
1191
+ fossil_print("project-id: %s\n", db_get("project-code", 0));
1192
+ fossil_print("server-id: %s\n", db_get("server-code", 0));
11961193
zPassword = db_text(0, "SELECT pw FROM user WHERE login=%Q", g.zLogin);
1197
- printf("admin-user: %s (initial password is \"%s\")\n", g.zLogin, zPassword);
1194
+ fossil_print("admin-user: %s (initial password is \"%s\")\n",
1195
+ g.zLogin, zPassword);
11981196
}
11991197
12001198
/*
12011199
** SQL functions for debugging.
12021200
**
@@ -1210,11 +1208,11 @@
12101208
){
12111209
int i;
12121210
if( g.fSqlPrint ){
12131211
for(i=0; i<argc; i++){
12141212
char c = i==argc-1 ? '\n' : ' ';
1215
- printf("%s%c", sqlite3_value_text(argv[i]), c);
1213
+ fossil_print("%s%c", sqlite3_value_text(argv[i]), c);
12161214
}
12171215
}
12181216
}
12191217
static void db_sql_trace(void *notUsed, const char *zSql){
12201218
int n = strlen(zSql);
@@ -1566,10 +1564,11 @@
15661564
fossil_panic("already within an open tree rooted at %s", g.zLocalRoot);
15671565
}
15681566
file_canonical_name(g.argv[2], &path);
15691567
db_open_repository(blob_str(&path));
15701568
db_init_database("./_FOSSIL_", zLocalSchema, (char*)0);
1569
+ db_delete_on_failure("./_FOSSIL_");
15711570
db_open_local();
15721571
db_lset("repository", blob_str(&path));
15731572
db_record_repository_filename(blob_str(&path));
15741573
vid = db_int(0, "SELECT pid FROM plink y"
15751574
" WHERE NOT EXISTS(SELECT 1 FROM plink x WHERE x.cid=y.pid)");
@@ -1613,14 +1612,14 @@
16131612
"SELECT '(global)', value FROM global_config WHERE name=%Q",
16141613
zName
16151614
);
16161615
}
16171616
if( db_step(&q)==SQLITE_ROW ){
1618
- printf("%-20s %-8s %s\n", zName, db_column_text(&q, 0),
1617
+ fossil_print("%-20s %-8s %s\n", zName, db_column_text(&q, 0),
16191618
db_column_text(&q, 1));
16201619
}else{
1621
- printf("%-20s\n", zName);
1620
+ fossil_print("%-20s\n", zName);
16221621
}
16231622
db_finalize(&q);
16241623
}
16251624
16261625
@@ -1869,9 +1868,9 @@
18691868
void test_timespan_cmd(void){
18701869
double rDiff;
18711870
if( g.argc!=3 ) usage("TIMESTAMP");
18721871
sqlite3_open(":memory:", &g.db);
18731872
rDiff = db_double(0.0, "SELECT julianday('now') - julianday(%Q)", g.argv[2]);
1874
- printf("Time differences: %s\n", db_timespan_name(rDiff));
1873
+ fossil_print("Time differences: %s\n", db_timespan_name(rDiff));
18751874
sqlite3_close(g.db);
18761875
g.db = 0;
18771876
}
18781877
--- src/db.c
+++ src/db.c
@@ -81,19 +81,29 @@
81 db_force_rollback();
82 fossil_exit(1);
83 }
84
85 static int nBegin = 0; /* Nesting depth of BEGIN */
86 static int isNewRepo = 0; /* True if the repository is newly created */
87 static int doRollback = 0; /* True to force a rollback */
88 static int nCommitHook = 0; /* Number of commit hooks */
89 static struct sCommitHook {
90 int (*xHook)(void); /* Functions to call at db_end_transaction() */
91 int sequence; /* Call functions in sequence order */
92 } aHook[5];
93 static Stmt *pAllStmt = 0; /* List of all unfinalized statements */
94 static int nPrepare = 0; /* Number of calls to sqlite3_prepare() */
 
 
 
 
 
 
 
 
 
 
 
95
96 /*
97 ** This routine is called by the SQLite commit-hook mechanism
98 ** just prior to each commit. All this routine does is verify
99 ** that nBegin really is zero. That insures that transactions
@@ -141,10 +151,11 @@
141
142 /*
143 ** Force a rollback and shutdown the database
144 */
145 void db_force_rollback(void){
 
146 static int busy = 0;
147 if( busy || g.db==0 ) return;
148 busy = 1;
149 undo_rollback();
150 while( pAllStmt ){
@@ -151,17 +162,16 @@
151 db_finalize(pAllStmt);
152 }
153 if( nBegin ){
154 sqlite3_exec(g.db, "ROLLBACK", 0, 0, 0);
155 nBegin = 0;
156 if( isNewRepo ){
157 db_close(0);
158 unlink(g.zRepositoryName);
159 }
160 }
161 busy = 0;
162 db_close(0);
 
 
 
163 }
164
165 /*
166 ** Install a commit hook. Hooks are installed in sequence order.
167 ** It is an error to install the same commit hook more than once.
@@ -565,14 +575,10 @@
565 }
566 db_finalize(&s);
567 return z;
568 }
569
570 #if defined(_WIN32)
571 extern char *sqlite3_win32_mbcs_to_utf8(const char*);
572 #endif
573
574 /*
575 ** Initialize a new database file with the given schema. If anything
576 ** goes wrong, call db_err() to exit.
577 */
578 void db_init_database(
@@ -583,13 +589,10 @@
583 sqlite3 *db;
584 int rc;
585 const char *zSql;
586 va_list ap;
587
588 #if defined(_WIN32)
589 zFileName = sqlite3_win32_mbcs_to_utf8(zFileName);
590 #endif
591 rc = sqlite3_open(zFileName, &db);
592 if( rc!=SQLITE_OK ){
593 db_err(sqlite3_errmsg(db));
594 }
595 sqlite3_busy_timeout(db, 5000);
@@ -631,13 +634,10 @@
631 int rc;
632 const char *zVfs;
633 sqlite3 *db;
634
635 zVfs = getenv("FOSSIL_VFS");
636 #if defined(_WIN32)
637 zDbName = sqlite3_win32_mbcs_to_utf8(zDbName);
638 #endif
639 rc = sqlite3_open_v2(
640 zDbName, &db,
641 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
642 zVfs
643 );
@@ -660,13 +660,10 @@
660 if( !g.db ){
661 g.db = openDatabase(zDbName);
662 g.zMainDbType = zLabel;
663 db_connection_init();
664 }else{
665 #if defined(_WIN32)
666 zDbName = sqlite3_win32_mbcs_to_utf8(zDbName);
667 #endif
668 db_multi_exec("ATTACH DATABASE %Q AS %s", zDbName, zLabel);
669 }
670 }
671
672 /*
@@ -1056,11 +1053,11 @@
1056 zFilename,
1057 zRepositorySchema1,
1058 zRepositorySchema2,
1059 (char*)0
1060 );
1061 isNewRepo = 1;
1062 }
1063
1064 /*
1065 ** Create the default user accounts in the USER table.
1066 */
@@ -1189,14 +1186,15 @@
1189 db_open_repository(g.argv[2]);
1190 db_open_config(0);
1191 db_begin_transaction();
1192 db_initial_setup(zDate, zDefaultUser, 1);
1193 db_end_transaction(0);
1194 printf("project-id: %s\n", db_get("project-code", 0));
1195 printf("server-id: %s\n", db_get("server-code", 0));
1196 zPassword = db_text(0, "SELECT pw FROM user WHERE login=%Q", g.zLogin);
1197 printf("admin-user: %s (initial password is \"%s\")\n", g.zLogin, zPassword);
 
1198 }
1199
1200 /*
1201 ** SQL functions for debugging.
1202 **
@@ -1210,11 +1208,11 @@
1210 ){
1211 int i;
1212 if( g.fSqlPrint ){
1213 for(i=0; i<argc; i++){
1214 char c = i==argc-1 ? '\n' : ' ';
1215 printf("%s%c", sqlite3_value_text(argv[i]), c);
1216 }
1217 }
1218 }
1219 static void db_sql_trace(void *notUsed, const char *zSql){
1220 int n = strlen(zSql);
@@ -1566,10 +1564,11 @@
1566 fossil_panic("already within an open tree rooted at %s", g.zLocalRoot);
1567 }
1568 file_canonical_name(g.argv[2], &path);
1569 db_open_repository(blob_str(&path));
1570 db_init_database("./_FOSSIL_", zLocalSchema, (char*)0);
 
1571 db_open_local();
1572 db_lset("repository", blob_str(&path));
1573 db_record_repository_filename(blob_str(&path));
1574 vid = db_int(0, "SELECT pid FROM plink y"
1575 " WHERE NOT EXISTS(SELECT 1 FROM plink x WHERE x.cid=y.pid)");
@@ -1613,14 +1612,14 @@
1613 "SELECT '(global)', value FROM global_config WHERE name=%Q",
1614 zName
1615 );
1616 }
1617 if( db_step(&q)==SQLITE_ROW ){
1618 printf("%-20s %-8s %s\n", zName, db_column_text(&q, 0),
1619 db_column_text(&q, 1));
1620 }else{
1621 printf("%-20s\n", zName);
1622 }
1623 db_finalize(&q);
1624 }
1625
1626
@@ -1869,9 +1868,9 @@
1869 void test_timespan_cmd(void){
1870 double rDiff;
1871 if( g.argc!=3 ) usage("TIMESTAMP");
1872 sqlite3_open(":memory:", &g.db);
1873 rDiff = db_double(0.0, "SELECT julianday('now') - julianday(%Q)", g.argv[2]);
1874 printf("Time differences: %s\n", db_timespan_name(rDiff));
1875 sqlite3_close(g.db);
1876 g.db = 0;
1877 }
1878
--- src/db.c
+++ src/db.c
@@ -81,19 +81,29 @@
81 db_force_rollback();
82 fossil_exit(1);
83 }
84
85 static int nBegin = 0; /* Nesting depth of BEGIN */
 
86 static int doRollback = 0; /* True to force a rollback */
87 static int nCommitHook = 0; /* Number of commit hooks */
88 static struct sCommitHook {
89 int (*xHook)(void); /* Functions to call at db_end_transaction() */
90 int sequence; /* Call functions in sequence order */
91 } aHook[5];
92 static Stmt *pAllStmt = 0; /* List of all unfinalized statements */
93 static int nPrepare = 0; /* Number of calls to sqlite3_prepare() */
94 static int nDeleteOnFail = 0; /* Number of entries in azDeleteOnFail[] */
95 static char *azDeleteOnFail[3]; /* Files to delete on a failure */
96
97
98 /*
99 ** Arrange for the given file to be deleted on a failure.
100 */
101 void db_delete_on_failure(const char *zFilename){
102 assert( nDeleteOnFail<count(azDeleteOnFail) );
103 azDeleteOnFail[nDeleteOnFail++] = fossil_strdup(zFilename);
104 }
105
106 /*
107 ** This routine is called by the SQLite commit-hook mechanism
108 ** just prior to each commit. All this routine does is verify
109 ** that nBegin really is zero. That insures that transactions
@@ -141,10 +151,11 @@
151
152 /*
153 ** Force a rollback and shutdown the database
154 */
155 void db_force_rollback(void){
156 int i;
157 static int busy = 0;
158 if( busy || g.db==0 ) return;
159 busy = 1;
160 undo_rollback();
161 while( pAllStmt ){
@@ -151,17 +162,16 @@
162 db_finalize(pAllStmt);
163 }
164 if( nBegin ){
165 sqlite3_exec(g.db, "ROLLBACK", 0, 0, 0);
166 nBegin = 0;
 
 
 
 
167 }
168 busy = 0;
169 db_close(0);
170 for(i=0; i<nDeleteOnFail; i++){
171 unlink(azDeleteOnFail[i]);
172 }
173 }
174
175 /*
176 ** Install a commit hook. Hooks are installed in sequence order.
177 ** It is an error to install the same commit hook more than once.
@@ -565,14 +575,10 @@
575 }
576 db_finalize(&s);
577 return z;
578 }
579
 
 
 
 
580 /*
581 ** Initialize a new database file with the given schema. If anything
582 ** goes wrong, call db_err() to exit.
583 */
584 void db_init_database(
@@ -583,13 +589,10 @@
589 sqlite3 *db;
590 int rc;
591 const char *zSql;
592 va_list ap;
593
 
 
 
594 rc = sqlite3_open(zFileName, &db);
595 if( rc!=SQLITE_OK ){
596 db_err(sqlite3_errmsg(db));
597 }
598 sqlite3_busy_timeout(db, 5000);
@@ -631,13 +634,10 @@
634 int rc;
635 const char *zVfs;
636 sqlite3 *db;
637
638 zVfs = getenv("FOSSIL_VFS");
 
 
 
639 rc = sqlite3_open_v2(
640 zDbName, &db,
641 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
642 zVfs
643 );
@@ -660,13 +660,10 @@
660 if( !g.db ){
661 g.db = openDatabase(zDbName);
662 g.zMainDbType = zLabel;
663 db_connection_init();
664 }else{
 
 
 
665 db_multi_exec("ATTACH DATABASE %Q AS %s", zDbName, zLabel);
666 }
667 }
668
669 /*
@@ -1056,11 +1053,11 @@
1053 zFilename,
1054 zRepositorySchema1,
1055 zRepositorySchema2,
1056 (char*)0
1057 );
1058 db_delete_on_failure(zFilename);
1059 }
1060
1061 /*
1062 ** Create the default user accounts in the USER table.
1063 */
@@ -1189,14 +1186,15 @@
1186 db_open_repository(g.argv[2]);
1187 db_open_config(0);
1188 db_begin_transaction();
1189 db_initial_setup(zDate, zDefaultUser, 1);
1190 db_end_transaction(0);
1191 fossil_print("project-id: %s\n", db_get("project-code", 0));
1192 fossil_print("server-id: %s\n", db_get("server-code", 0));
1193 zPassword = db_text(0, "SELECT pw FROM user WHERE login=%Q", g.zLogin);
1194 fossil_print("admin-user: %s (initial password is \"%s\")\n",
1195 g.zLogin, zPassword);
1196 }
1197
1198 /*
1199 ** SQL functions for debugging.
1200 **
@@ -1210,11 +1208,11 @@
1208 ){
1209 int i;
1210 if( g.fSqlPrint ){
1211 for(i=0; i<argc; i++){
1212 char c = i==argc-1 ? '\n' : ' ';
1213 fossil_print("%s%c", sqlite3_value_text(argv[i]), c);
1214 }
1215 }
1216 }
1217 static void db_sql_trace(void *notUsed, const char *zSql){
1218 int n = strlen(zSql);
@@ -1566,10 +1564,11 @@
1564 fossil_panic("already within an open tree rooted at %s", g.zLocalRoot);
1565 }
1566 file_canonical_name(g.argv[2], &path);
1567 db_open_repository(blob_str(&path));
1568 db_init_database("./_FOSSIL_", zLocalSchema, (char*)0);
1569 db_delete_on_failure("./_FOSSIL_");
1570 db_open_local();
1571 db_lset("repository", blob_str(&path));
1572 db_record_repository_filename(blob_str(&path));
1573 vid = db_int(0, "SELECT pid FROM plink y"
1574 " WHERE NOT EXISTS(SELECT 1 FROM plink x WHERE x.cid=y.pid)");
@@ -1613,14 +1612,14 @@
1612 "SELECT '(global)', value FROM global_config WHERE name=%Q",
1613 zName
1614 );
1615 }
1616 if( db_step(&q)==SQLITE_ROW ){
1617 fossil_print("%-20s %-8s %s\n", zName, db_column_text(&q, 0),
1618 db_column_text(&q, 1));
1619 }else{
1620 fossil_print("%-20s\n", zName);
1621 }
1622 db_finalize(&q);
1623 }
1624
1625
@@ -1869,9 +1868,9 @@
1868 void test_timespan_cmd(void){
1869 double rDiff;
1870 if( g.argc!=3 ) usage("TIMESTAMP");
1871 sqlite3_open(":memory:", &g.db);
1872 rDiff = db_double(0.0, "SELECT julianday('now') - julianday(%Q)", g.argv[2]);
1873 fossil_print("Time differences: %s\n", db_timespan_name(rDiff));
1874 sqlite3_close(g.db);
1875 g.db = 0;
1876 }
1877

Keyboard Shortcuts

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