2404
2405
2406
2407
2408
2409
2410
|
** special case, if it ends with a period then a pointer to the
** terminating NUL byte is returned.
*/
const char * file_extension(const char *zFileName){
const char * zExt = zFileName ? strrchr(zFileName, '.') : 0;
return zExt ? &zExt[1] : 0;
}
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
|
** special case, if it ends with a period then a pointer to the
** terminating NUL byte is returned.
*/
const char * file_extension(const char *zFileName){
const char * zExt = zFileName ? strrchr(zFileName, '.') : 0;
return zExt ? &zExt[1] : 0;
}
/*
** Returns true if the given filename ends with any of fossil's
** checkout database filenames: _FOSSIL_ or .fslckout. Specifically,
** it returns 1 if it's an exact match and 2 if it's the tail match
** on a longer input.
**
** zFilename must, for efficiency's sake, be a
** canonicalized/normalized name, e.g. using only '/' as directory
** separators.
**
** nFilename must be the strlen of zFilename. If it is negative,
** strlen() is used to calculate it.
*/
int filename_is_ckout_db(const char *zFilename, int nFilename){
const char *zEnd; /* one-after-the-end of zFilename */
int gotSuffix = 0; /* length of suffix (-wal, -shm, -journal) */
assert(zFilename && "API misuse");
if(nFilename<0) nFilename = (int)strlen(zFilename);
if(nFilename<8/*strlen _FOSSIL_*/) return 0;
zEnd = zFilename + nFilename;
if(nFilename>=12/*strlen _FOSSIL_-(shm|wal)*/){
/* Check for (-wal, -shm, -journal) suffixes, with an eye towards
** runtime speed. */
if('-'==zEnd[-4]){
if(fossil_stricmp("wal", &zEnd[-3])
&& fossil_stricmp("shm", &zEnd[-3])){
return 0;
}
gotSuffix = 4;
}else if(nFilename>=16/*strlen _FOSSIL_-journal*/ && '-'==zEnd[-8]){
if(fossil_stricmp("journal",&zEnd[-7])){
return 0;
}
gotSuffix = 8;
}
if(gotSuffix){
assert(4==gotSuffix || 8==gotSuffix);
zEnd -= gotSuffix;
nFilename -= gotSuffix;
gotSuffix = 1;
}
assert(nFilename>=8 && "strlen _FOSSIL_");
assert(gotSuffix==0 || gotSuffix==1);
}
switch(zEnd[-1]){
case '_': {
return fossil_strnicmp("_FOSSIL_", &zEnd[-8], 8)
? 0 : (8==nFilename
? 1
: ('/'==zEnd[-9] ? 2 : gotSuffix));
}
case 't': {
return (nFilename<9
|| '.'!=zEnd[-9]
|| fossil_strnicmp(".fslckout", &zEnd[-9], 9))
? 0 : (9==nFilename
? 1
: ('/'==zEnd[-10] ? 2 : gotSuffix));
}
default: {
return 0;
}
}
}
/*
** COMMAND: test-is-ckout-db
**
** Usage: %fossil test-is-ckout-db FILENAMES...
**
** Passes each given name to filename_is_ckout_db() and outputs one
** line per file: the result value of that function followed by the
** name.
*/
void test_is_ckout_name_cmd(void){
int i;
if(g.argc<3){
usage("FILENAME_1 [...FILENAME_N]");
}
for( i = 2; i < g.argc; ++i ){
const int check = filename_is_ckout_db(g.argv[i], -1);
fossil_print("%d %s\n", check, g.argv[i]);
}
}
|