Fossil SCM

Added checks of (-wal, -shm, -journal) db suffixes.

stephan 2020-08-17 17:34 reject-ckout-db
Commit 4ed1a294ff5b90e210b1d8eb98cb0f7919b9bc43241d18a0a8c8a2749afd5f57
1 file changed +39 -13
+39 -13
--- src/file.c
+++ src/file.c
@@ -2421,35 +2421,61 @@
24212421
** canonicalized/normalized name, e.g. using only '/' as directory
24222422
** separators.
24232423
**
24242424
** nFilename must be the strlen of zFilename. If it is negative,
24252425
** strlen() is used to calculate it.
2426
-**
2427
-** TODO: https://fossil-scm.org/sec2020/info/972cf9c302f5413f
2428
-** TL;DR: check for the -wal, -shm, -journal suffix forms of the db
2429
-** names.
24302426
*/
24312427
int filename_is_ckout_db(const char *zFilename, int nFilename){
2432
- const char *zEnd;
2428
+ const char *zEnd; /* one-after-the-end of zFilename */
2429
+ int gotSuffix = 0; /* length of suffix (-wal, -shm, -journal) */
24332430
2434
- if(nFilename>=0 && nFilename<8/*strlen _FOSSIL_*/) return 0;
2435
- else if(nFilename<0) nFilename = (int)strlen(zFilename);
2436
- if(nFilename<8) return 0;
2431
+ assert(zFilename && "API misuse");
2432
+ if(nFilename<0) nFilename = (int)strlen(zFilename);
2433
+ if(nFilename<8/*strlen _FOSSIL_*/) return 0;
24372434
zEnd = zFilename + nFilename;
2435
+ if(nFilename>=12/*strlen _FOSSIL_-(shm|wal)*/){
2436
+ /* Check for (-wal, -shm, -journal) suffixes, with an eye towards
2437
+ ** runtime speed. */
2438
+ if('-'==zEnd[-4]){
2439
+ if(fossil_stricmp("wal", &zEnd[-3])
2440
+ && fossil_stricmp("shm", &zEnd[-3])){
2441
+ return 0;
2442
+ }
2443
+ gotSuffix = 4;
2444
+ }else if(nFilename>=16/*strlen _FOSSIL_-journal*/ && '-'==zEnd[-8]){
2445
+ if(fossil_stricmp("journal",&zEnd[-7])){
2446
+ return 0;
2447
+ }
2448
+ gotSuffix = 8;
2449
+ }
2450
+ if(gotSuffix){
2451
+ assert(4==gotSuffix || 8==gotSuffix);
2452
+ zEnd -= gotSuffix;
2453
+ nFilename -= gotSuffix;
2454
+ gotSuffix = 1;
2455
+ }
2456
+ assert(nFilename>=8 && "strlen _FOSSIL_");
2457
+ assert(gotSuffix==0 || gotSuffix==1);
2458
+ }
24382459
switch(zEnd[-1]){
24392460
case '_': {
2440
- return fossil_strcmp("_FOSSIL_", &zEnd[-8])
2441
- ? 0 : (8==nFilename ? 1 : ('/'==zEnd[-9] ? 2 : 0));
2461
+ return fossil_strnicmp("_FOSSIL_", &zEnd[-8], 8)
2462
+ ? 0 : (8==nFilename
2463
+ ? 1
2464
+ : ('/'==zEnd[-9] ? 2 : gotSuffix));
24422465
}
24432466
case 't': {
24442467
return (nFilename<9
24452468
|| '.'!=zEnd[-9]
2446
- || fossil_strcmp(".fslckout", &zEnd[-9]))
2447
- ? 0 : (9==nFilename ? 1 : ('/'==zEnd[-10] ? 2 : 0));
2469
+ || fossil_strnicmp(".fslckout", &zEnd[-9], 9))
2470
+ ? 0 : (9==nFilename
2471
+ ? 1
2472
+ : ('/'==zEnd[-10] ? 2 : gotSuffix));
24482473
}
2449
- default:
2474
+ default: {
24502475
return 0;
2476
+ }
24512477
}
24522478
}
24532479
24542480
/*
24552481
** COMMAND: test-is-ckout-db
24562482
--- src/file.c
+++ src/file.c
@@ -2421,35 +2421,61 @@
2421 ** canonicalized/normalized name, e.g. using only '/' as directory
2422 ** separators.
2423 **
2424 ** nFilename must be the strlen of zFilename. If it is negative,
2425 ** strlen() is used to calculate it.
2426 **
2427 ** TODO: https://fossil-scm.org/sec2020/info/972cf9c302f5413f
2428 ** TL;DR: check for the -wal, -shm, -journal suffix forms of the db
2429 ** names.
2430 */
2431 int filename_is_ckout_db(const char *zFilename, int nFilename){
2432 const char *zEnd;
 
2433
2434 if(nFilename>=0 && nFilename<8/*strlen _FOSSIL_*/) return 0;
2435 else if(nFilename<0) nFilename = (int)strlen(zFilename);
2436 if(nFilename<8) return 0;
2437 zEnd = zFilename + nFilename;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2438 switch(zEnd[-1]){
2439 case '_': {
2440 return fossil_strcmp("_FOSSIL_", &zEnd[-8])
2441 ? 0 : (8==nFilename ? 1 : ('/'==zEnd[-9] ? 2 : 0));
 
 
2442 }
2443 case 't': {
2444 return (nFilename<9
2445 || '.'!=zEnd[-9]
2446 || fossil_strcmp(".fslckout", &zEnd[-9]))
2447 ? 0 : (9==nFilename ? 1 : ('/'==zEnd[-10] ? 2 : 0));
 
 
2448 }
2449 default:
2450 return 0;
 
2451 }
2452 }
2453
2454 /*
2455 ** COMMAND: test-is-ckout-db
2456
--- src/file.c
+++ src/file.c
@@ -2421,35 +2421,61 @@
2421 ** canonicalized/normalized name, e.g. using only '/' as directory
2422 ** separators.
2423 **
2424 ** nFilename must be the strlen of zFilename. If it is negative,
2425 ** strlen() is used to calculate it.
 
 
 
 
2426 */
2427 int filename_is_ckout_db(const char *zFilename, int nFilename){
2428 const char *zEnd; /* one-after-the-end of zFilename */
2429 int gotSuffix = 0; /* length of suffix (-wal, -shm, -journal) */
2430
2431 assert(zFilename && "API misuse");
2432 if(nFilename<0) nFilename = (int)strlen(zFilename);
2433 if(nFilename<8/*strlen _FOSSIL_*/) return 0;
2434 zEnd = zFilename + nFilename;
2435 if(nFilename>=12/*strlen _FOSSIL_-(shm|wal)*/){
2436 /* Check for (-wal, -shm, -journal) suffixes, with an eye towards
2437 ** runtime speed. */
2438 if('-'==zEnd[-4]){
2439 if(fossil_stricmp("wal", &zEnd[-3])
2440 && fossil_stricmp("shm", &zEnd[-3])){
2441 return 0;
2442 }
2443 gotSuffix = 4;
2444 }else if(nFilename>=16/*strlen _FOSSIL_-journal*/ && '-'==zEnd[-8]){
2445 if(fossil_stricmp("journal",&zEnd[-7])){
2446 return 0;
2447 }
2448 gotSuffix = 8;
2449 }
2450 if(gotSuffix){
2451 assert(4==gotSuffix || 8==gotSuffix);
2452 zEnd -= gotSuffix;
2453 nFilename -= gotSuffix;
2454 gotSuffix = 1;
2455 }
2456 assert(nFilename>=8 && "strlen _FOSSIL_");
2457 assert(gotSuffix==0 || gotSuffix==1);
2458 }
2459 switch(zEnd[-1]){
2460 case '_': {
2461 return fossil_strnicmp("_FOSSIL_", &zEnd[-8], 8)
2462 ? 0 : (8==nFilename
2463 ? 1
2464 : ('/'==zEnd[-9] ? 2 : gotSuffix));
2465 }
2466 case 't': {
2467 return (nFilename<9
2468 || '.'!=zEnd[-9]
2469 || fossil_strnicmp(".fslckout", &zEnd[-9], 9))
2470 ? 0 : (9==nFilename
2471 ? 1
2472 : ('/'==zEnd[-10] ? 2 : gotSuffix));
2473 }
2474 default: {
2475 return 0;
2476 }
2477 }
2478 }
2479
2480 /*
2481 ** COMMAND: test-is-ckout-db
2482

Keyboard Shortcuts

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