Skip to contentSkip to conversation

How can I get link target from a full path name.

DISCUSSION 墳墓 1 participant 1 message Aug 24, 2005

lore
Share & tools
lore

墳墓

author
#0 lore raw

Hello,
I would like to know how can I figure out where is a symbolic link file pointing to.

for example:

/a.txt is a symbolic link to /root/a.txt , how can I get /root/a.txt when I passing /a.txt
to my function?

I tought path_loookup and d_path is useful, and I wrote a function, but the path_lookup
function always return -2 , which seems means that there is no such file, but I am sure
the file exists.

Here is my code, any sugguestion? and thanks for your help.

int catfs_get_path_target ( const char * filename , char ** target_path ,
char ** target_mnt_point )

{
    int error = 0;
    
    char buffer[255];
    char buffer2[255];

    char * fullpath = NULL;
    char * mnt_point = NULL;
    struct nameidata nd;
    struct file * filp = NULL;
    
    if ( filename == NULL ) {
        error = -1;
        goto out;
    }
    
    error = path_lookup ( filename , 0 , &nd );
    
    if ( error ) {
        printk ( "Error:%d\n" , error );
        error = -1;
        goto out;
    }
    
    memset ( buffer , 0 , 255 );
    memset ( buffer2 , 0 , 255 );
    
    fullpath  = d_path(filp->f_dentry,filp->f_vfsmnt,buffer,255) + 1;
    mnt_point =
d_path(filp->f_vfsmnt->mnt_mountpoint,filp->f_vfsmnt->mnt_parent,buffer2,255);
    
    if ( mnt_point[strlen(mnt_point)-1] != '/' ) {
        strcat ( mnt_point , "/" );
    }

    *target_path = kmalloc ( sizeof(char) * strlen(fullpath) + 1 , GFP_KERNEL );

    if ( *target_path == NULL ) {
        *target_path = NULL;
        error = -1;
        goto out;
    }

    *target_mnt_point = kmalloc ( sizeof(char) * strlen(mnt_point) + 1

, GFP_KERNEL );

    if ( *target_mnt_point == NULL ) {
        *target_path = NULL;
        error = -1;
        goto out;
    }

    strcpy ( *target_path , fullpath );
    strcpy ( *target_mnt_point , mnt_point );

    path_release ( &nd );

out:

    return error;
}