| | @@ -2408,90 +2408,86 @@ |
| 2408 | 2408 | const char * zExt = zFileName ? strrchr(zFileName, '.') : 0; |
| 2409 | 2409 | return zExt ? &zExt[1] : 0; |
| 2410 | 2410 | } |
| 2411 | 2411 | |
| 2412 | 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){ |
| 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){ |
| 2426 | 2424 | const char *zEnd; /* one-after-the-end of zFilename */ |
| 2427 | 2425 | int gotSuffix = 0; /* length of suffix (-wal, -shm, -journal) */ |
| 2428 | 2426 | |
| 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_") */ |
| 2432 | 2430 | zEnd = zFilename + nFilename; |
| 2433 | | - if(nFilename>=12/*strlen _FOSSIL_-(shm|wal)*/){ |
| 2431 | + if( nFilename>=12 ){ /* strlen("_FOSSIL_-(shm|wal)") */ |
| 2434 | 2432 | /* Check for (-wal, -shm, -journal) suffixes, with an eye towards |
| 2435 | 2433 | ** 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) ){ |
| 2439 | 2437 | return 0; |
| 2440 | 2438 | } |
| 2441 | 2439 | 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; |
| 2446 | 2442 | gotSuffix = 8; |
| 2447 | 2443 | } |
| 2448 | | - if(gotSuffix){ |
| 2449 | | - assert(4==gotSuffix || 8==gotSuffix); |
| 2444 | + if( gotSuffix ){ |
| 2445 | + assert( 4==gotSuffix || 8==gotSuffix ); |
| 2450 | 2446 | zEnd -= gotSuffix; |
| 2451 | 2447 | nFilename -= gotSuffix; |
| 2452 | 2448 | gotSuffix = 1; |
| 2453 | 2449 | } |
| 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:{ |
| 2473 | 2469 | return 0; |
| 2474 | 2470 | } |
| 2475 | 2471 | } |
| 2476 | 2472 | } |
| 2477 | 2473 | |
| 2478 | 2474 | /* |
| 2479 | | -** COMMAND: test-is-ckout-db |
| 2475 | +** COMMAND: test-is-reserved-name |
| 2480 | 2476 | ** |
| 2481 | 2477 | ** Usage: %fossil test-is-ckout-db FILENAMES... |
| 2482 | 2478 | ** |
| 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 |
| 2484 | 2480 | ** line per file: the result value of that function followed by the |
| 2485 | 2481 | ** name. |
| 2486 | 2482 | */ |
| 2487 | | -void test_is_ckout_name_cmd(void){ |
| 2483 | +void test_is_reserved_name_cmd(void){ |
| 2488 | 2484 | int i; |
| 2489 | 2485 | |
| 2490 | 2486 | if(g.argc<3){ |
| 2491 | 2487 | usage("FILENAME_1 [...FILENAME_N]"); |
| 2492 | 2488 | } |
| 2493 | 2489 | 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); |
| 2495 | 2491 | fossil_print("%d %s\n", check, g.argv[i]); |
| 2496 | 2492 | } |
| 2497 | 2493 | } |
| 2498 | 2494 | |