Fossil SCM

Fixes for reserved names case sensitivity, coding style adjustments, more tests.

mistachkin 2020-08-17 22:22 sec2020
Commit fde20bc03c500809f5a544a3b4764096b987d527aa95bac69f1c929f8b4919f9
+45 -49
--- src/file.c
+++ src/file.c
@@ -2408,90 +2408,86 @@
24082408
const char * zExt = zFileName ? strrchr(zFileName, '.') : 0;
24092409
return zExt ? &zExt[1] : 0;
24102410
}
24112411
24122412
/*
2413
-** Returns true if the given filename ends with any of fossil's
2414
-** checkout database filenames: _FOSSIL_ or .fslckout. Specifically,
2415
-** it returns 1 if it's an exact match and 2 if it's the tail match
2416
-** on a longer input.
2417
-**
2418
-** zFilename must, for efficiency's sake, be a
2419
-** canonicalized/normalized name, e.g. using only '/' as directory
2420
-** separators.
2421
-**
2422
-** nFilename must be the strlen of zFilename. If it is negative,
2423
-** strlen() is used to calculate it.
2424
-*/
2425
-int filename_is_ckout_db(const char *zFilename, int nFilename){
2413
+** Returns non-zero if the specified file name ends with any reserved name,
2414
+** e.g.: _FOSSIL_ or .fslckout. Specifically, it returns 1 for exact match
2415
+** or 2 for a tail match on a longer file name.
2416
+**
2417
+** For the sake of efficiency, zFilename must be a canonical name, e.g. an
2418
+** absolute path using only forward slash ('/') as a directory separator.
2419
+**
2420
+** nFilename must be the length of zFilename. When negative, strlen() will
2421
+** be used to calculate it.
2422
+*/
2423
+int file_is_reserved_name(const char *zFilename, int nFilename){
24262424
const char *zEnd; /* one-after-the-end of zFilename */
24272425
int gotSuffix = 0; /* length of suffix (-wal, -shm, -journal) */
24282426
2429
- assert(zFilename && "API misuse");
2430
- if(nFilename<0) nFilename = (int)strlen(zFilename);
2431
- if(nFilename<8/*strlen _FOSSIL_*/) return 0;
2427
+ assert( zFilename && "API misuse" );
2428
+ if( nFilename<0 ) nFilename = (int)strlen(zFilename);
2429
+ if( nFilename<8 ) return 0; /* strlen("_FOSSIL_") */
24322430
zEnd = zFilename + nFilename;
2433
- if(nFilename>=12/*strlen _FOSSIL_-(shm|wal)*/){
2431
+ if( nFilename>=12 ){ /* strlen("_FOSSIL_-(shm|wal)") */
24342432
/* Check for (-wal, -shm, -journal) suffixes, with an eye towards
24352433
** runtime speed. */
2436
- if('-'==zEnd[-4]){
2437
- if(fossil_stricmp("wal", &zEnd[-3])
2438
- && fossil_stricmp("shm", &zEnd[-3])){
2434
+ if( zEnd[-4]=='-' ){
2435
+ if( fossil_strnicmp("wal", &zEnd[-3], 3)
2436
+ && fossil_strnicmp("shm", &zEnd[-3], 3) ){
24392437
return 0;
24402438
}
24412439
gotSuffix = 4;
2442
- }else if(nFilename>=16/*strlen _FOSSIL_-journal*/ && '-'==zEnd[-8]){
2443
- if(fossil_stricmp("journal",&zEnd[-7])){
2444
- return 0;
2445
- }
2440
+ }else if( nFilename>=16 && zEnd[-8]=='-' ){ /*strlen(_FOSSIL_-journal) */
2441
+ if( fossil_strnicmp("journal", &zEnd[-7], 7) ) return 0;
24462442
gotSuffix = 8;
24472443
}
2448
- if(gotSuffix){
2449
- assert(4==gotSuffix || 8==gotSuffix);
2444
+ if( gotSuffix ){
2445
+ assert( 4==gotSuffix || 8==gotSuffix );
24502446
zEnd -= gotSuffix;
24512447
nFilename -= gotSuffix;
24522448
gotSuffix = 1;
24532449
}
2454
- assert(nFilename>=8 && "strlen _FOSSIL_");
2455
- assert(gotSuffix==0 || gotSuffix==1);
2456
- }
2457
- switch(zEnd[-1]){
2458
- case '_': {
2459
- return fossil_strnicmp("_FOSSIL_", &zEnd[-8], 8)
2460
- ? 0 : (8==nFilename
2461
- ? 1
2462
- : ('/'==zEnd[-9] ? 2 : gotSuffix));
2463
- }
2464
- case 't': {
2465
- return (nFilename<9
2466
- || '.'!=zEnd[-9]
2467
- || fossil_strnicmp(".fslckout", &zEnd[-9], 9))
2468
- ? 0 : (9==nFilename
2469
- ? 1
2470
- : ('/'==zEnd[-10] ? 2 : gotSuffix));
2471
- }
2472
- default: {
2450
+ assert( nFilename>=8 && "strlen(_FOSSIL_)" );
2451
+ assert( gotSuffix==0 || gotSuffix==1 );
2452
+ }
2453
+ switch( zEnd[-1] ){
2454
+ case '_':{
2455
+ if( fossil_strnicmp("_FOSSIL_", &zEnd[-8], 8) ) return 0;
2456
+ if( 8==nFilename ) return 1;
2457
+ return zEnd[-9]=='/' ? 2 : gotSuffix;
2458
+ }
2459
+ case 'T':
2460
+ case 't':{
2461
+ if( nFilename<9 || zEnd[-9]!='.'
2462
+ || fossil_strnicmp(".fslckout", &zEnd[-9], 9) ){
2463
+ return 0;
2464
+ }
2465
+ if( 9==nFilename ) return 1;
2466
+ return zEnd[-10]=='/' ? 2 : gotSuffix;
2467
+ }
2468
+ default:{
24732469
return 0;
24742470
}
24752471
}
24762472
}
24772473
24782474
/*
2479
-** COMMAND: test-is-ckout-db
2475
+** COMMAND: test-is-reserved-name
24802476
**
24812477
** Usage: %fossil test-is-ckout-db FILENAMES...
24822478
**
2483
-** Passes each given name to filename_is_ckout_db() and outputs one
2479
+** Passes each given name to file_is_reserved_name() and outputs one
24842480
** line per file: the result value of that function followed by the
24852481
** name.
24862482
*/
2487
-void test_is_ckout_name_cmd(void){
2483
+void test_is_reserved_name_cmd(void){
24882484
int i;
24892485
24902486
if(g.argc<3){
24912487
usage("FILENAME_1 [...FILENAME_N]");
24922488
}
24932489
for( i = 2; i < g.argc; ++i ){
2494
- const int check = filename_is_ckout_db(g.argv[i], -1);
2490
+ const int check = file_is_reserved_name(g.argv[i], -1);
24952491
fossil_print("%d %s\n", check, g.argv[i]);
24962492
}
24972493
}
24982494
--- src/file.c
+++ src/file.c
@@ -2408,90 +2408,86 @@
2408 const char * zExt = zFileName ? strrchr(zFileName, '.') : 0;
2409 return zExt ? &zExt[1] : 0;
2410 }
2411
2412 /*
2413 ** Returns true if the given filename ends with any of fossil's
2414 ** checkout database filenames: _FOSSIL_ or .fslckout. Specifically,
2415 ** it returns 1 if it's an exact match and 2 if it's the tail match
2416 ** on a longer input.
2417 **
2418 ** zFilename must, for efficiency's sake, be a
2419 ** canonicalized/normalized name, e.g. using only '/' as directory
2420 ** separators.
2421 **
2422 ** nFilename must be the strlen of zFilename. If it is negative,
2423 ** strlen() is used to calculate it.
2424 */
2425 int filename_is_ckout_db(const char *zFilename, int nFilename){
2426 const char *zEnd; /* one-after-the-end of zFilename */
2427 int gotSuffix = 0; /* length of suffix (-wal, -shm, -journal) */
2428
2429 assert(zFilename && "API misuse");
2430 if(nFilename<0) nFilename = (int)strlen(zFilename);
2431 if(nFilename<8/*strlen _FOSSIL_*/) return 0;
2432 zEnd = zFilename + nFilename;
2433 if(nFilename>=12/*strlen _FOSSIL_-(shm|wal)*/){
2434 /* Check for (-wal, -shm, -journal) suffixes, with an eye towards
2435 ** runtime speed. */
2436 if('-'==zEnd[-4]){
2437 if(fossil_stricmp("wal", &zEnd[-3])
2438 && fossil_stricmp("shm", &zEnd[-3])){
2439 return 0;
2440 }
2441 gotSuffix = 4;
2442 }else if(nFilename>=16/*strlen _FOSSIL_-journal*/ && '-'==zEnd[-8]){
2443 if(fossil_stricmp("journal",&zEnd[-7])){
2444 return 0;
2445 }
2446 gotSuffix = 8;
2447 }
2448 if(gotSuffix){
2449 assert(4==gotSuffix || 8==gotSuffix);
2450 zEnd -= gotSuffix;
2451 nFilename -= gotSuffix;
2452 gotSuffix = 1;
2453 }
2454 assert(nFilename>=8 && "strlen _FOSSIL_");
2455 assert(gotSuffix==0 || gotSuffix==1);
2456 }
2457 switch(zEnd[-1]){
2458 case '_': {
2459 return fossil_strnicmp("_FOSSIL_", &zEnd[-8], 8)
2460 ? 0 : (8==nFilename
2461 ? 1
2462 : ('/'==zEnd[-9] ? 2 : gotSuffix));
2463 }
2464 case 't': {
2465 return (nFilename<9
2466 || '.'!=zEnd[-9]
2467 || fossil_strnicmp(".fslckout", &zEnd[-9], 9))
2468 ? 0 : (9==nFilename
2469 ? 1
2470 : ('/'==zEnd[-10] ? 2 : gotSuffix));
2471 }
2472 default: {
2473 return 0;
2474 }
2475 }
2476 }
2477
2478 /*
2479 ** COMMAND: test-is-ckout-db
2480 **
2481 ** Usage: %fossil test-is-ckout-db FILENAMES...
2482 **
2483 ** Passes each given name to filename_is_ckout_db() and outputs one
2484 ** line per file: the result value of that function followed by the
2485 ** name.
2486 */
2487 void test_is_ckout_name_cmd(void){
2488 int i;
2489
2490 if(g.argc<3){
2491 usage("FILENAME_1 [...FILENAME_N]");
2492 }
2493 for( i = 2; i < g.argc; ++i ){
2494 const int check = filename_is_ckout_db(g.argv[i], -1);
2495 fossil_print("%d %s\n", check, g.argv[i]);
2496 }
2497 }
2498
--- src/file.c
+++ src/file.c
@@ -2408,90 +2408,86 @@
2408 const char * zExt = zFileName ? strrchr(zFileName, '.') : 0;
2409 return zExt ? &zExt[1] : 0;
2410 }
2411
2412 /*
2413 ** Returns non-zero if the specified file name ends with any reserved name,
2414 ** e.g.: _FOSSIL_ or .fslckout. Specifically, it returns 1 for exact match
2415 ** or 2 for a tail match on a longer file name.
2416 **
2417 ** For the sake of efficiency, zFilename must be a canonical name, e.g. an
2418 ** absolute path using only forward slash ('/') as a directory separator.
2419 **
2420 ** nFilename must be the length of zFilename. When negative, strlen() will
2421 ** be used to calculate it.
2422 */
2423 int file_is_reserved_name(const char *zFilename, int nFilename){
 
 
2424 const char *zEnd; /* one-after-the-end of zFilename */
2425 int gotSuffix = 0; /* length of suffix (-wal, -shm, -journal) */
2426
2427 assert( zFilename && "API misuse" );
2428 if( nFilename<0 ) nFilename = (int)strlen(zFilename);
2429 if( nFilename<8 ) return 0; /* strlen("_FOSSIL_") */
2430 zEnd = zFilename + nFilename;
2431 if( nFilename>=12 ){ /* strlen("_FOSSIL_-(shm|wal)") */
2432 /* Check for (-wal, -shm, -journal) suffixes, with an eye towards
2433 ** runtime speed. */
2434 if( zEnd[-4]=='-' ){
2435 if( fossil_strnicmp("wal", &zEnd[-3], 3)
2436 && fossil_strnicmp("shm", &zEnd[-3], 3) ){
2437 return 0;
2438 }
2439 gotSuffix = 4;
2440 }else if( nFilename>=16 && zEnd[-8]=='-' ){ /*strlen(_FOSSIL_-journal) */
2441 if( fossil_strnicmp("journal", &zEnd[-7], 7) ) return 0;
 
 
2442 gotSuffix = 8;
2443 }
2444 if( gotSuffix ){
2445 assert( 4==gotSuffix || 8==gotSuffix );
2446 zEnd -= gotSuffix;
2447 nFilename -= gotSuffix;
2448 gotSuffix = 1;
2449 }
2450 assert( nFilename>=8 && "strlen(_FOSSIL_)" );
2451 assert( gotSuffix==0 || gotSuffix==1 );
2452 }
2453 switch( zEnd[-1] ){
2454 case '_':{
2455 if( fossil_strnicmp("_FOSSIL_", &zEnd[-8], 8) ) return 0;
2456 if( 8==nFilename ) return 1;
2457 return zEnd[-9]=='/' ? 2 : gotSuffix;
2458 }
2459 case 'T':
2460 case 't':{
2461 if( nFilename<9 || zEnd[-9]!='.'
2462 || fossil_strnicmp(".fslckout", &zEnd[-9], 9) ){
2463 return 0;
2464 }
2465 if( 9==nFilename ) return 1;
2466 return zEnd[-10]=='/' ? 2 : gotSuffix;
2467 }
2468 default:{
2469 return 0;
2470 }
2471 }
2472 }
2473
2474 /*
2475 ** COMMAND: test-is-reserved-name
2476 **
2477 ** Usage: %fossil test-is-ckout-db FILENAMES...
2478 **
2479 ** Passes each given name to file_is_reserved_name() and outputs one
2480 ** line per file: the result value of that function followed by the
2481 ** name.
2482 */
2483 void test_is_reserved_name_cmd(void){
2484 int i;
2485
2486 if(g.argc<3){
2487 usage("FILENAME_1 [...FILENAME_N]");
2488 }
2489 for( i = 2; i < g.argc; ++i ){
2490 const int check = file_is_reserved_name(g.argv[i], -1);
2491 fossil_print("%d %s\n", check, g.argv[i]);
2492 }
2493 }
2494
+1 -1
--- src/manifest.c
+++ src/manifest.c
@@ -632,11 +632,11 @@
632632
zName = next_token(&x,0);
633633
if( zName==0 ) SYNTAX("missing filename on F-card");
634634
defossilize(zName);
635635
if( !file_is_simple_pathname_nonstrict(zName) ){
636636
SYNTAX("F-card filename is not a simple path");
637
- }else if( filename_is_ckout_db(zName,-1) ){
637
+ }else if( file_is_reserved_name(zName,-1) ){
638638
SYNTAX("F-card contains reserved name of a checkout db.");
639639
}
640640
zUuid = next_token(&x, &sz);
641641
if( p->zBaseline==0 || zUuid!=0 ){
642642
if( zUuid==0 ) SYNTAX("missing hash on F-card");
643643
--- src/manifest.c
+++ src/manifest.c
@@ -632,11 +632,11 @@
632 zName = next_token(&x,0);
633 if( zName==0 ) SYNTAX("missing filename on F-card");
634 defossilize(zName);
635 if( !file_is_simple_pathname_nonstrict(zName) ){
636 SYNTAX("F-card filename is not a simple path");
637 }else if( filename_is_ckout_db(zName,-1) ){
638 SYNTAX("F-card contains reserved name of a checkout db.");
639 }
640 zUuid = next_token(&x, &sz);
641 if( p->zBaseline==0 || zUuid!=0 ){
642 if( zUuid==0 ) SYNTAX("missing hash on F-card");
643
--- src/manifest.c
+++ src/manifest.c
@@ -632,11 +632,11 @@
632 zName = next_token(&x,0);
633 if( zName==0 ) SYNTAX("missing filename on F-card");
634 defossilize(zName);
635 if( !file_is_simple_pathname_nonstrict(zName) ){
636 SYNTAX("F-card filename is not a simple path");
637 }else if( file_is_reserved_name(zName,-1) ){
638 SYNTAX("F-card contains reserved name of a checkout db.");
639 }
640 zUuid = next_token(&x, &sz);
641 if( p->zBaseline==0 || zUuid!=0 ){
642 if( zUuid==0 ) SYNTAX("missing hash on F-card");
643
--- test/reserved-names.test
+++ test/reserved-names.test
@@ -72,19 +72,39 @@
7272
incr testNo
7373
7474
set reserved_result [lindex $reserved_names_test 0]
7575
set reserved_name [lindex $reserved_names_test 1]
7676
77
- fossil test-is-ckout-db $reserved_name
77
+ fossil test-is-reserved-name $reserved_name
7878
7979
test reserved-result-$testNo {
8080
[lindex [normalize_result] 0] eq $reserved_result
8181
}
8282
8383
test reserved-name-$testNo {
8484
[lindex [normalize_result] 1] eq $reserved_name
8585
}
86
+
87
+ fossil test-is-reserved-name [string toupper $reserved_name]
88
+
89
+ test reserved-result-upper-$testNo {
90
+ [lindex [normalize_result] 0] eq $reserved_result
91
+ }
92
+
93
+ test reserved-name-upper-$testNo {
94
+ [lindex [normalize_result] 1] eq [string toupper $reserved_name]
95
+ }
96
+
97
+ fossil test-is-reserved-name [string tolower $reserved_name]
98
+
99
+ test reserved-result-lower-$testNo {
100
+ [lindex [normalize_result] 0] eq $reserved_result
101
+ }
102
+
103
+ test reserved-name-lower-$testNo {
104
+ [lindex [normalize_result] 1] eq [string tolower $reserved_name]
105
+ }
86106
}
87107
88108
###############################################################################
89109
90110
test_cleanup
91111
--- test/reserved-names.test
+++ test/reserved-names.test
@@ -72,19 +72,39 @@
72 incr testNo
73
74 set reserved_result [lindex $reserved_names_test 0]
75 set reserved_name [lindex $reserved_names_test 1]
76
77 fossil test-is-ckout-db $reserved_name
78
79 test reserved-result-$testNo {
80 [lindex [normalize_result] 0] eq $reserved_result
81 }
82
83 test reserved-name-$testNo {
84 [lindex [normalize_result] 1] eq $reserved_name
85 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86 }
87
88 ###############################################################################
89
90 test_cleanup
91
--- test/reserved-names.test
+++ test/reserved-names.test
@@ -72,19 +72,39 @@
72 incr testNo
73
74 set reserved_result [lindex $reserved_names_test 0]
75 set reserved_name [lindex $reserved_names_test 1]
76
77 fossil test-is-reserved-name $reserved_name
78
79 test reserved-result-$testNo {
80 [lindex [normalize_result] 0] eq $reserved_result
81 }
82
83 test reserved-name-$testNo {
84 [lindex [normalize_result] 1] eq $reserved_name
85 }
86
87 fossil test-is-reserved-name [string toupper $reserved_name]
88
89 test reserved-result-upper-$testNo {
90 [lindex [normalize_result] 0] eq $reserved_result
91 }
92
93 test reserved-name-upper-$testNo {
94 [lindex [normalize_result] 1] eq [string toupper $reserved_name]
95 }
96
97 fossil test-is-reserved-name [string tolower $reserved_name]
98
99 test reserved-result-lower-$testNo {
100 [lindex [normalize_result] 0] eq $reserved_result
101 }
102
103 test reserved-name-lower-$testNo {
104 [lindex [normalize_result] 1] eq [string tolower $reserved_name]
105 }
106 }
107
108 ###############################################################################
109
110 test_cleanup
111

Keyboard Shortcuts

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