All of lore.kernel.org
 help / color / mirror / Atom feed
* How can I get link target from a full path name.
@ 2005-08-24  7:42 墳墓
  0 siblings, 0 replies; only message in thread
From: 墳墓 @ 2005-08-24  7:42 UTC (permalink / raw)
  To: linux-kernel

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;
}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2005-08-24  7:43 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-08-24  7:42 How can I get link target from a full path name 墳墓

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.