#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/uio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <libglusterfsclient.h>

int readdirs(const char* path){

    int ret = 0;
    char file_name[1024];
    struct dirent *dirp = NULL;
    glusterfs_dir_t dirfd = NULL;

    dirfd = glusterfs_opendir( path );
    if( dirfd == NULL ) {
        printf("dirfd is null\n");
        return -1;
    }

    while( (dirp = glusterfs_readdir(dirfd)) != NULL ) {
        printf("%s %s %d %d\n", dirp->d_name, &dirp->d_name[8], dirp->d_type, dirp->d_reclen );

    }
    glusterfs_closedir( dirfd );

    return ret;
}

int mount(){
    int ret = 0;
    glusterfs_init_params_t params;
    char *mount_point = "/myvp";

    memset (&params, 0, sizeof (params));
    params.specfile = "/etc/glusterfs/glusterfs-client.vol";
    params.volume_name = "myvp";
    params.logfile = "./glusterfs.log";
    params.loglevel = "DEBUG";
    params.lookup_timeout = 600;
    params.stat_timeout = 600;
    ret = glusterfs_mount( mount_point, &params );

    return ret;
}
void handle_error( char* msg )
{
    char buf[1024];
    char *p_msg = "";
    int errno_ = errno ;

    if( msg ) p_msg = msg;

    snprintf( buf, sizeof(buf), "[Errno %d] %s %s" , errno_,strerror(errno_), p_msg);

    printf( "%s\n", buf );
}

int main(){
    int ret = 0;
    char *dir_path =( "/myvp/test" );

    ret = mount();
    if ( ret < 0 ) {
        handle_error(NULL);
        return ret;
    }
    ret = readdirs( dir_path );
    if ( ret < 0 ) {
        handle_error( dir_path );
        return ret;
    }
    return ret;
}
