Fossil SCM

Create a single subroutine that determines whether a file is a "reserved" file used by Fossil itself, or is potentially a valid repository file. This processing used to be duplicated at each place where it was needed.

drh 2010-12-10 22:02 trunk
Commit 56d69dbd858083354713052ea7be24cb0e711e06
3 files changed +67 -14 +6 -11 +11 -16
+67 -14
--- src/add.c
+++ src/add.c
@@ -26,30 +26,85 @@
2626
/*
2727
** Set to true if files whose names begin with "." should be
2828
** included when processing a recursive "add" command.
2929
*/
3030
static int includeDotFiles = 0;
31
+
32
+/*
33
+** This routine returns the names of files in a working checkout that
34
+** are created by Fossil itself, and hence should not be added, deleted,
35
+** or merge, and should be omitted from "clean" and "extra" lists.
36
+**
37
+** Return the N-th name. The first name has N==0. When all names have
38
+** been used, return 0.
39
+*/
40
+const char *fossil_reserved_name(int N){
41
+ /* Possible names of the local per-checkout database file and
42
+ ** its associated journals
43
+ */
44
+ static const char *azName[] = {
45
+ "_FOSSIL_",
46
+ "_FOSSIL_-journal",
47
+ "_FOSSIL_-wal",
48
+ "_FOSSIL_-shm",
49
+ ".fos",
50
+ ".fos-journal",
51
+ ".fos-wal",
52
+ ".fos-shm",
53
+ };
54
+
55
+ /* Names of auxiliary files generated by SQLite when the "manifest"
56
+ ** properity is enabled
57
+ */
58
+ static const char *azManifest[] = {
59
+ "manifest",
60
+ "manifest.uuid",
61
+ };
62
+
63
+ if( N>=0 && N<count(azName) ) return azName[N];
64
+ if( N>=count(azName) && N<count(azName)+count(azManifest)
65
+ && db_get_boolean("manifest",0) ){
66
+ return azManifest[N-count(azName)];
67
+ }
68
+ return 0;
69
+}
70
+
71
+/*
72
+** Return a list of all reserved filenames as an SQL list.
73
+*/
74
+const char *fossil_all_reserved_names(void){
75
+ static char *zAll = 0;
76
+ if( zAll==0 ){
77
+ Blob x;
78
+ int i;
79
+ const char *z;
80
+ blob_zero(&x);
81
+ for(i=0; (z = fossil_reserved_name(i))!=0; i++){
82
+ if( i>0 ) blob_append(&x, ",", 1);
83
+ blob_appendf(&x, "'%s'", z);
84
+ }
85
+ zAll = blob_str(&x);
86
+ }
87
+ return zAll;
88
+}
89
+
3190
3291
/*
3392
** Add a single file
3493
*/
3594
static void add_one_file(const char *zName, int vid, Blob *pOmit){
3695
Blob pathname;
3796
const char *zPath;
97
+ int i;
98
+ const char *zReserved;
3899
39100
file_tree_name(zName, &pathname, 1);
40101
zPath = blob_str(&pathname);
41
- if( strcmp(zPath, "_FOSSIL_")==0
42
- || strcmp(zPath, "_FOSSIL_-journal")==0
43
- || strcmp(zPath, "_FOSSIL_-wal")==0
44
- || strcmp(zPath, "_FOSSIL_-shm")==0
45
- || strcmp(zPath, ".fos")==0
46
- || strcmp(zPath, ".fos-journal")==0
47
- || strcmp(zPath, ".fos-wal")==0
48
- || strcmp(zPath, ".fos-shm")==0
49
- || (pOmit && blob_compare(&pathname, pOmit)==0)
50
- ){
102
+ for(i=0; (zReserved = fossil_reserved_name(i))!=0; i++){
103
+ if( strcmp(zPath, zReserved)==0 ) break;
104
+ }
105
+ if( zReserved || (pOmit && blob_compare(&pathname, pOmit)==0) ){
51106
fossil_warning("cannot add %s", zPath);
52107
}else{
53108
if( !file_is_simple_pathname(zPath) ){
54109
fossil_fatal("filename contains illegal characters: %s", zPath);
55110
}
@@ -334,17 +389,15 @@
334389
}
335390
336391
/* step 1: search for extra files */
337392
db_prepare(&q,
338393
"SELECT x, %Q || x FROM sfile"
339
- " WHERE x NOT IN ('manifest','manifest.uuid','_FOSSIL_',"
340
- "'_FOSSIL_-journal','.fos','.fos-journal',"
341
- "'_FOSSIL_-wal','_FOSSIL_-shm','.fos-wal',"
342
- "'.fos-shm')"
394
+ " WHERE x NOT IN (%s)"
343395
" AND NOT %s"
344396
" ORDER BY 1",
345397
g.zLocalRoot,
398
+ fossil_all_reserved_names(),
346399
glob_expr("x", zIgnoreFlag)
347400
);
348401
while( db_step(&q)==SQLITE_ROW ){
349402
add_one_file(db_column_text(&q, 1), vid, 0);
350403
nAdd++;
351404
--- src/add.c
+++ src/add.c
@@ -26,30 +26,85 @@
26 /*
27 ** Set to true if files whose names begin with "." should be
28 ** included when processing a recursive "add" command.
29 */
30 static int includeDotFiles = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
32 /*
33 ** Add a single file
34 */
35 static void add_one_file(const char *zName, int vid, Blob *pOmit){
36 Blob pathname;
37 const char *zPath;
 
 
38
39 file_tree_name(zName, &pathname, 1);
40 zPath = blob_str(&pathname);
41 if( strcmp(zPath, "_FOSSIL_")==0
42 || strcmp(zPath, "_FOSSIL_-journal")==0
43 || strcmp(zPath, "_FOSSIL_-wal")==0
44 || strcmp(zPath, "_FOSSIL_-shm")==0
45 || strcmp(zPath, ".fos")==0
46 || strcmp(zPath, ".fos-journal")==0
47 || strcmp(zPath, ".fos-wal")==0
48 || strcmp(zPath, ".fos-shm")==0
49 || (pOmit && blob_compare(&pathname, pOmit)==0)
50 ){
51 fossil_warning("cannot add %s", zPath);
52 }else{
53 if( !file_is_simple_pathname(zPath) ){
54 fossil_fatal("filename contains illegal characters: %s", zPath);
55 }
@@ -334,17 +389,15 @@
334 }
335
336 /* step 1: search for extra files */
337 db_prepare(&q,
338 "SELECT x, %Q || x FROM sfile"
339 " WHERE x NOT IN ('manifest','manifest.uuid','_FOSSIL_',"
340 "'_FOSSIL_-journal','.fos','.fos-journal',"
341 "'_FOSSIL_-wal','_FOSSIL_-shm','.fos-wal',"
342 "'.fos-shm')"
343 " AND NOT %s"
344 " ORDER BY 1",
345 g.zLocalRoot,
 
346 glob_expr("x", zIgnoreFlag)
347 );
348 while( db_step(&q)==SQLITE_ROW ){
349 add_one_file(db_column_text(&q, 1), vid, 0);
350 nAdd++;
351
--- src/add.c
+++ src/add.c
@@ -26,30 +26,85 @@
26 /*
27 ** Set to true if files whose names begin with "." should be
28 ** included when processing a recursive "add" command.
29 */
30 static int includeDotFiles = 0;
31
32 /*
33 ** This routine returns the names of files in a working checkout that
34 ** are created by Fossil itself, and hence should not be added, deleted,
35 ** or merge, and should be omitted from "clean" and "extra" lists.
36 **
37 ** Return the N-th name. The first name has N==0. When all names have
38 ** been used, return 0.
39 */
40 const char *fossil_reserved_name(int N){
41 /* Possible names of the local per-checkout database file and
42 ** its associated journals
43 */
44 static const char *azName[] = {
45 "_FOSSIL_",
46 "_FOSSIL_-journal",
47 "_FOSSIL_-wal",
48 "_FOSSIL_-shm",
49 ".fos",
50 ".fos-journal",
51 ".fos-wal",
52 ".fos-shm",
53 };
54
55 /* Names of auxiliary files generated by SQLite when the "manifest"
56 ** properity is enabled
57 */
58 static const char *azManifest[] = {
59 "manifest",
60 "manifest.uuid",
61 };
62
63 if( N>=0 && N<count(azName) ) return azName[N];
64 if( N>=count(azName) && N<count(azName)+count(azManifest)
65 && db_get_boolean("manifest",0) ){
66 return azManifest[N-count(azName)];
67 }
68 return 0;
69 }
70
71 /*
72 ** Return a list of all reserved filenames as an SQL list.
73 */
74 const char *fossil_all_reserved_names(void){
75 static char *zAll = 0;
76 if( zAll==0 ){
77 Blob x;
78 int i;
79 const char *z;
80 blob_zero(&x);
81 for(i=0; (z = fossil_reserved_name(i))!=0; i++){
82 if( i>0 ) blob_append(&x, ",", 1);
83 blob_appendf(&x, "'%s'", z);
84 }
85 zAll = blob_str(&x);
86 }
87 return zAll;
88 }
89
90
91 /*
92 ** Add a single file
93 */
94 static void add_one_file(const char *zName, int vid, Blob *pOmit){
95 Blob pathname;
96 const char *zPath;
97 int i;
98 const char *zReserved;
99
100 file_tree_name(zName, &pathname, 1);
101 zPath = blob_str(&pathname);
102 for(i=0; (zReserved = fossil_reserved_name(i))!=0; i++){
103 if( strcmp(zPath, zReserved)==0 ) break;
104 }
105 if( zReserved || (pOmit && blob_compare(&pathname, pOmit)==0) ){
 
 
 
 
 
 
106 fossil_warning("cannot add %s", zPath);
107 }else{
108 if( !file_is_simple_pathname(zPath) ){
109 fossil_fatal("filename contains illegal characters: %s", zPath);
110 }
@@ -334,17 +389,15 @@
389 }
390
391 /* step 1: search for extra files */
392 db_prepare(&q,
393 "SELECT x, %Q || x FROM sfile"
394 " WHERE x NOT IN (%s)"
 
 
 
395 " AND NOT %s"
396 " ORDER BY 1",
397 g.zLocalRoot,
398 fossil_all_reserved_names(),
399 glob_expr("x", zIgnoreFlag)
400 );
401 while( db_step(&q)==SQLITE_ROW ){
402 add_one_file(db_column_text(&q, 1), vid, 0);
403 nAdd++;
404
+6 -11
--- src/checkin.c
+++ src/checkin.c
@@ -283,18 +283,14 @@
283283
zIgnoreFlag = db_get("ignore-glob", 0);
284284
}
285285
vfile_scan(0, &path, blob_size(&path), allFlag);
286286
db_prepare(&q,
287287
"SELECT x FROM sfile"
288
- " WHERE x NOT IN ('%s','%s','_FOSSIL_',"
289
- "'_FOSSIL_-journal','.fos','.fos-journal',"
290
- "'_FOSSIL_-wal','_FOSSIL_-shm','.fos-wal',"
291
- "'.fos-shm')"
288
+ " WHERE x NOT IN (%s)"
292289
" AND NOT %s"
293290
" ORDER BY 1",
294
- outputManifest ? "manifest" : "_FOSSIL_",
295
- outputManifest ? "manifest.uuid" : "_FOSSIL_",
291
+ fossil_all_reserved_names(),
296292
glob_expr("x", zIgnoreFlag)
297293
);
298294
if( file_tree_name(g.zRepositoryName, &repo, 0) ){
299295
db_multi_exec("DELETE FROM sfile WHERE x=%B", &repo);
300296
}
@@ -333,15 +329,14 @@
333329
n = strlen(g.zLocalRoot);
334330
blob_init(&path, g.zLocalRoot, n-1);
335331
vfile_scan(0, &path, blob_size(&path), dotfilesFlag);
336332
db_prepare(&q,
337333
"SELECT %Q || x FROM sfile"
338
- " WHERE x NOT IN ('manifest','manifest.uuid','_FOSSIL_',"
339
- "'_FOSSIL_-journal','.fos','.fos-journal',"
340
- "'_FOSSIL_-wal','_FOSSIL_-shm','.fos-wal',"
341
- "'.fos-shm')"
342
- " ORDER BY 1", g.zLocalRoot);
334
+ " WHERE x NOT IN (%s)"
335
+ " ORDER BY 1",
336
+ g.zLocalRoot, fossil_all_reserved_names()
337
+ );
343338
if( file_tree_name(g.zRepositoryName, &repo, 0) ){
344339
db_multi_exec("DELETE FROM sfile WHERE x=%B", &repo);
345340
}
346341
while( db_step(&q)==SQLITE_ROW ){
347342
if( allFlag ){
348343
--- src/checkin.c
+++ src/checkin.c
@@ -283,18 +283,14 @@
283 zIgnoreFlag = db_get("ignore-glob", 0);
284 }
285 vfile_scan(0, &path, blob_size(&path), allFlag);
286 db_prepare(&q,
287 "SELECT x FROM sfile"
288 " WHERE x NOT IN ('%s','%s','_FOSSIL_',"
289 "'_FOSSIL_-journal','.fos','.fos-journal',"
290 "'_FOSSIL_-wal','_FOSSIL_-shm','.fos-wal',"
291 "'.fos-shm')"
292 " AND NOT %s"
293 " ORDER BY 1",
294 outputManifest ? "manifest" : "_FOSSIL_",
295 outputManifest ? "manifest.uuid" : "_FOSSIL_",
296 glob_expr("x", zIgnoreFlag)
297 );
298 if( file_tree_name(g.zRepositoryName, &repo, 0) ){
299 db_multi_exec("DELETE FROM sfile WHERE x=%B", &repo);
300 }
@@ -333,15 +329,14 @@
333 n = strlen(g.zLocalRoot);
334 blob_init(&path, g.zLocalRoot, n-1);
335 vfile_scan(0, &path, blob_size(&path), dotfilesFlag);
336 db_prepare(&q,
337 "SELECT %Q || x FROM sfile"
338 " WHERE x NOT IN ('manifest','manifest.uuid','_FOSSIL_',"
339 "'_FOSSIL_-journal','.fos','.fos-journal',"
340 "'_FOSSIL_-wal','_FOSSIL_-shm','.fos-wal',"
341 "'.fos-shm')"
342 " ORDER BY 1", g.zLocalRoot);
343 if( file_tree_name(g.zRepositoryName, &repo, 0) ){
344 db_multi_exec("DELETE FROM sfile WHERE x=%B", &repo);
345 }
346 while( db_step(&q)==SQLITE_ROW ){
347 if( allFlag ){
348
--- src/checkin.c
+++ src/checkin.c
@@ -283,18 +283,14 @@
283 zIgnoreFlag = db_get("ignore-glob", 0);
284 }
285 vfile_scan(0, &path, blob_size(&path), allFlag);
286 db_prepare(&q,
287 "SELECT x FROM sfile"
288 " WHERE x NOT IN (%s)"
 
 
 
289 " AND NOT %s"
290 " ORDER BY 1",
291 fossil_all_reserved_names(),
 
292 glob_expr("x", zIgnoreFlag)
293 );
294 if( file_tree_name(g.zRepositoryName, &repo, 0) ){
295 db_multi_exec("DELETE FROM sfile WHERE x=%B", &repo);
296 }
@@ -333,15 +329,14 @@
329 n = strlen(g.zLocalRoot);
330 blob_init(&path, g.zLocalRoot, n-1);
331 vfile_scan(0, &path, blob_size(&path), dotfilesFlag);
332 db_prepare(&q,
333 "SELECT %Q || x FROM sfile"
334 " WHERE x NOT IN (%s)"
335 " ORDER BY 1",
336 g.zLocalRoot, fossil_all_reserved_names()
337 );
 
338 if( file_tree_name(g.zRepositoryName, &repo, 0) ){
339 db_multi_exec("DELETE FROM sfile WHERE x=%B", &repo);
340 }
341 while( db_step(&q)==SQLITE_ROW ){
342 if( allFlag ){
343
+11 -16
--- src/checkout.c
+++ src/checkout.c
@@ -261,26 +261,20 @@
261261
}
262262
263263
/*
264264
** Unlink the local database file
265265
*/
266
-void unlink_local_database(void){
267
- static const char *azFile[] = {
268
- "%s_FOSSIL_",
269
- "%s_FOSSIL_-journal",
270
- "%s_FOSSIL_-wal",
271
- "%s_FOSSIL_-shm",
272
- "%s.fos",
273
- "%s.fos-journal",
274
- "%s.fos-wal",
275
- "%s.fos-shm",
276
- };
266
+static void unlink_local_database(int manifestOnly){
267
+ const char *zReserved;
277268
int i;
278
- for(i=0; i<sizeof(azFile)/sizeof(azFile[0]); i++){
279
- char *z = mprintf(azFile[i], g.zLocalRoot);
280
- unlink(z);
281
- free(z);
269
+ for(i=0; (zReserved = fossil_reserved_name(i))!=0; i++){
270
+ if( manifestOnly==0 || zReserved[0]=='m' ){
271
+ char *z;
272
+ z = mprintf("%s%s", g.zLocalRoot, zReserved);
273
+ unlink(z);
274
+ free(z);
275
+ }
282276
}
283277
}
284278
285279
/*
286280
** COMMAND: close
@@ -295,8 +289,9 @@
295289
int forceFlag = find_option("force","f",0)!=0;
296290
db_must_be_within_tree();
297291
if( !forceFlag && unsaved_changes()==1 ){
298292
fossil_fatal("there are unsaved changes in the current checkout");
299293
}
294
+ unlink_local_database(1);
300295
db_close();
301
- unlink_local_database();
296
+ unlink_local_database(0);
302297
}
303298
--- src/checkout.c
+++ src/checkout.c
@@ -261,26 +261,20 @@
261 }
262
263 /*
264 ** Unlink the local database file
265 */
266 void unlink_local_database(void){
267 static const char *azFile[] = {
268 "%s_FOSSIL_",
269 "%s_FOSSIL_-journal",
270 "%s_FOSSIL_-wal",
271 "%s_FOSSIL_-shm",
272 "%s.fos",
273 "%s.fos-journal",
274 "%s.fos-wal",
275 "%s.fos-shm",
276 };
277 int i;
278 for(i=0; i<sizeof(azFile)/sizeof(azFile[0]); i++){
279 char *z = mprintf(azFile[i], g.zLocalRoot);
280 unlink(z);
281 free(z);
 
 
 
282 }
283 }
284
285 /*
286 ** COMMAND: close
@@ -295,8 +289,9 @@
295 int forceFlag = find_option("force","f",0)!=0;
296 db_must_be_within_tree();
297 if( !forceFlag && unsaved_changes()==1 ){
298 fossil_fatal("there are unsaved changes in the current checkout");
299 }
 
300 db_close();
301 unlink_local_database();
302 }
303
--- src/checkout.c
+++ src/checkout.c
@@ -261,26 +261,20 @@
261 }
262
263 /*
264 ** Unlink the local database file
265 */
266 static void unlink_local_database(int manifestOnly){
267 const char *zReserved;
 
 
 
 
 
 
 
 
 
268 int i;
269 for(i=0; (zReserved = fossil_reserved_name(i))!=0; i++){
270 if( manifestOnly==0 || zReserved[0]=='m' ){
271 char *z;
272 z = mprintf("%s%s", g.zLocalRoot, zReserved);
273 unlink(z);
274 free(z);
275 }
276 }
277 }
278
279 /*
280 ** COMMAND: close
@@ -295,8 +289,9 @@
289 int forceFlag = find_option("force","f",0)!=0;
290 db_must_be_within_tree();
291 if( !forceFlag && unsaved_changes()==1 ){
292 fossil_fatal("there are unsaved changes in the current checkout");
293 }
294 unlink_local_database(1);
295 db_close();
296 unlink_local_database(0);
297 }
298

Keyboard Shortcuts

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