plugins/in_tail/tail_file.c
/*
 * Given an open file descriptor, return the filename. This function is a
 * bit slow and it aims to be used only when a file is rotated.
 */
Given an open file descriptor, return the filename. This function is a
bit slow and it aims to be used only when a file is rotated.
Given an open file descriptor return the filename . This function is a bit slow and it aims to be used only when a file is rotated .
char *flb_tail_file_name(struct flb_tail_file *file)
{
    int ret;
    char *buf;
#ifdef __linux__
    ssize_t s;
    char tmp[128];
#elif defined(__APPLE__)
    char path[PATH_MAX];
#elif defined(FLB_SYSTEM_WINDOWS)
    HANDLE h;
#endif
    buf = flb_malloc(PATH_MAX);
    if (!buf) {
        flb_errno();
        return NULL;
    }
#ifdef __linux__
    ret = snprintf(tmp, sizeof(tmp) - 1, "/proc/%i/fd/%i", getpid(), file->fd);
    if (ret == -1) {
        flb_errno();
        flb_free(buf);
        return NULL;
    }
    s = readlink(tmp, buf, PATH_MAX);
    if (s == -1) {
        flb_free(buf);
        flb_errno();
        return NULL;
    }
    buf[s] = '\0';
#elif __APPLE__
    int len;
    ret = fcntl(file->fd, F_GETPATH, path);
    if (ret == -1) {
        flb_errno();
        flb_free(buf);
        return NULL;
    }
    len = strlen(path);
    memcpy(buf, path, len);
    buf[len] = '\0';
#elif defined(FLB_SYSTEM_WINDOWS)
    int len;
    h = (HANDLE) _get_osfhandle(file->fd);
    if (h == INVALID_HANDLE_VALUE) {
        flb_errno();
        flb_free(buf);
        return NULL;
    }
    len = GetFinalPathNameByHandleA(h, buf, PATH_MAX, FILE_NAME_NORMALIZED);
    if (len == 0 || len >= PATH_MAX) {
        flb_free(buf);
        return NULL;
    }
    if (strstr(buf, "\\\\?\\")) {
        memmove(buf, buf + 4, len + 1);
    }
#endif
    return buf;
}
char * flb_tail_file_name ( struct flb_tail_file * file ) { int ret ; char * buf ; #ifdef __linux__ ssize_t s ; char tmp [ 128 ] ; #elif defined ( __APPLE__ ) char path [ PATH_MAX ] ; #elif defined ( FLB_SYSTEM_WINDOWS ) HANDLE h ; #endif buf = flb_malloc ( PATH_MAX ) ; if ( ! buf ) { flb_errno ( ) ; return NULL ; } #ifdef __linux__ ret = snprintf ( tmp , sizeof ( tmp ) - 1 , " " , getpid ( ) , file -> fd ) ; if ( ret == -1 ) { flb_errno ( ) ; flb_free ( buf ) ; return NULL ; } s = readlink ( tmp , buf , PATH_MAX ) ; if ( s == -1 ) { flb_free ( buf ) ; flb_errno ( ) ; return NULL ; } buf [ s ] = ' \0 ' ; #elif __APPLE__ int len ; ret = fcntl ( file -> fd , F_GETPATH , path ) ; if ( ret == -1 ) { flb_errno ( ) ; flb_free ( buf ) ; return NULL ; } len = strlen ( path ) ; memcpy ( buf , path , len ) ; buf [ len ] = ' \0 ' ; #elif defined ( FLB_SYSTEM_WINDOWS ) int len ; h = ( HANDLE ) _get_osfhandle ( file -> fd ) ; if ( h == INVALID_HANDLE_VALUE ) { flb_errno ( ) ; flb_free ( buf ) ; return NULL ; } len = GetFinalPathNameByHandleA ( h , buf , PATH_MAX , FILE_NAME_NORMALIZED ) ; if ( len == 0 || len >= PATH_MAX ) { flb_free ( buf ) ; return NULL ; } if ( strstr ( buf , " \\ \\ \\ " ) ) { memmove ( buf , buf + 4 , len + 1 ) ; } #endif return buf ; }
Given an open file descriptor, return the filename.
Given an open file descriptor return the filename .
/* This function returns the length of the string excluding "\0"
     * and the resulting path has a "\\?\" prefix.
     */