plugins/in_tail/tail_scan_win32.c
/*
 * Perform patern match on the given path string. This function
 * supports patterns with "nested" wildcards like below.
 *
 *     tail_scan_pattern("C:\fluent-bit\*\*.txt", ctx);
 */
Perform patern match on the given path string. This function
supports patterns with "nested" wildcards like below.


Perform patern match on the given path string . This function supports patterns with " nested " wildcards like below .
static int tail_scan_pattern(const char *path, struct flb_tail_config *ctx)
{
    char *star, *p0, *p1;
    char pattern[MAX_PATH];
    char buf[MAX_PATH];
    int ret;
    int n_added = 0;
    HANDLE h;
    WIN32_FIND_DATA data;
    if (strlen(path) > MAX_PATH - 1) {
        flb_plg_error(ctx->ins, "path too long '%s'");
        return -1;
    }
    star = strchr(path, '*');
    if (star == NULL) {
        return -1;
    }
    p0 = star;
    while (path <= p0 && *p0 != '\\') {
        p0--;
    }
    p1 = star;
    while (*p1 && *p1 != '\\') {
        p1++;
    }
    memcpy(pattern, path, (p1 - path));
    pattern[p1 - path] = '\0';
    h = FindFirstFileA(pattern, &data);
    if (h == INVALID_HANDLE_VALUE) {
        return -1;
    }
    do {
        if (!strcmp(".", data.cFileName) || !strcmp("..", data.cFileName)) {
            continue;
        }
        if (strchr(data.cFileName, '*')) {
            continue;
        }
        memcpy(buf, path, p0 - path + 1);
        buf[p0 - path + 1] = '\0';
        if (strlen(buf) + strlen(data.cFileName) + strlen(p1) > MAX_PATH - 1) {
            flb_plg_warn(ctx->ins, "'%s%s%s' is too long", buf, data.cFileName, p1);
            continue;
        }
        strcat(buf, data.cFileName);
        strcat(buf, p1);
        if (strchr(p1, '*')) {
            ret = tail_scan_pattern(buf, ctx); 
            if (ret >= 0) {
                n_added += ret;
            }
            continue;
        }
        ret = tail_register_file(buf, ctx);
        if (ret == 0) {
            n_added++;
        }
    } while (FindNextFileA(h, &data) != 0);
    FindClose(h);
    return n_added;
}
static int tail_scan_pattern ( const char * path , struct flb_tail_config * ctx ) { char * star , * p0 , * p1 ; char pattern [ MAX_PATH ] ; char buf [ MAX_PATH ] ; int ret ; int n_added = 0 ; HANDLE h ; WIN32_FIND_DATA data ; if ( strlen ( path ) > MAX_PATH - 1 ) { flb_plg_error ( ctx -> ins , " " ) ; return -1 ; } star = strchr ( path , ' ' ) ; if ( star == NULL ) { return -1 ; } p0 = star ; while ( path <= p0 && * p0 != ' \\ ' ) { p0 -- ; } p1 = star ; while ( * p1 && * p1 != ' \\ ' ) { p1 ++ ; } memcpy ( pattern , path , ( p1 - path ) ) ; pattern [ p1 - path ] = ' \0 ' ; h = FindFirstFileA ( pattern , & data ) ; if ( h == INVALID_HANDLE_VALUE ) { return -1 ; } do { if ( ! strcmp ( " " , data . cFileName ) || ! strcmp ( " " , data . cFileName ) ) { continue ; } if ( strchr ( data . cFileName , ' ' ) ) { continue ; } memcpy ( buf , path , p0 - path + 1 ) ; buf [ p0 - path + 1 ] = ' \0 ' ; if ( strlen ( buf ) + strlen ( data . cFileName ) + strlen ( p1 ) > MAX_PATH - 1 ) { flb_plg_warn ( ctx -> ins , " " , buf , data . cFileName , p1 ) ; continue ; } strcat ( buf , data . cFileName ) ; strcat ( buf , p1 ) ; if ( strchr ( p1 , ' ' ) ) { ret = tail_scan_pattern ( buf , ctx ) ; if ( ret >= 0 ) { n_added += ret ; } continue ; } ret = tail_register_file ( buf , ctx ) ; if ( ret == 0 ) { n_added ++ ; } } while ( FindNextFileA ( h , & data ) != 0 ) ; FindClose ( h ) ; return n_added ; }
Perform patern match on the given path string.
Perform patern match on the given path string .
/*
     * C:\data\tmp\input_*.conf
     *            0<-----|
     */ /*
     * C:\data\tmp\input_*.conf
     *                   |---->1
     */ /* Ignore the current and parent dirs */ /* Avoid an infinite loop */ /* Create a path (prefix + filename + suffix) */ /* recursive */ /* Try to register the target file */