LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-16-2003, 03:31 PM   #1
rozeboom
LQ Newbie
 
Registered: Oct 2003
Distribution: CentOS5
Posts: 20

Rep: Reputation: 0
Discovering hard link paths


How would you go about discovering the paths of a given file's hardlinks? I know you can do a stat on the file to get the link-count, but how do you find the actual paths? I want to be able to find these in a C program.
 
Old 12-16-2003, 06:20 PM   #2
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Rep: Reputation: 30
Use 'readlink()':

Code:
#include <stdio.h>
#include <unistd.h>

int main(int *argc, char *argv[])
{
        char buf[100];
        int i = 0;
        if (!argv[1]) {
                printf("Usage: enter the path to the symlink.\n");
                return -1;
        }
        if (readlink(argv[1], buf, sizeof(buf)) == -1) {
                printf("readlink\n");
                return -1;
        }
        while (buf[i] != '\n')
                i++;
        buf[i] = '\0';
        printf("%s\n", buf);
        return 0;
}
There may be easier ways to do this.

Tarts

{edit}

'man ln', hardlinks dont have paths, mabey you mean symlink?

Tarts

Last edited by Tarts; 12-17-2003 at 09:42 AM.
 
Old 12-18-2003, 09:09 AM   #3
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
i dont think that information is stored in the filesystem because its not something you would really ever need to know. hard links must exist on the same filesystem so the only way that comes to mind is to stat every file on the filesystem and if the inode matches then youve got a link. we might be lucky enough to have a filesytem expert among us who has a better technique but thats all i can think of.
 
Old 12-18-2003, 09:49 AM   #4
rozeboom
LQ Newbie
 
Registered: Oct 2003
Distribution: CentOS5
Posts: 20

Original Poster
Rep: Reputation: 0
Thanks, Tarts, for the reply about readlink. I had found that and it appears to only give information on symlinks. The case that I am working on is for a backup application which needs to duplicate a subset of the file system. In order to produce a proper duplicate, I need to be able to determine what files are hardlinks, which I can do from stat, and then also determine how to reconstruct the same relationship on the backup disk.

Perhaps, as kev82 suggested, I may have to keep track of hardlink inodes as I step through the subset. Sounds like a workable solution, thanks!
 
Old 12-18-2003, 10:08 AM   #5
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
i suggest you have a look at the source of tar to see whats done their as the backup maybe approached in a different way.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how do I know if a file has a hard link to it? granny Linux - Newbie 13 04-04-2015 05:02 AM
Soft Link and Hard Link Moataz Red Hat 1 04-25-2005 06:30 AM
hard link? linuxzouk Linux - Newbie 5 05-15-2004 12:20 AM
mozilla composer and link paths Nic-MDKman Linux - Software 0 02-14-2004 04:04 AM
Automatically resolving WINDOWS paths to pre-configured Linux paths gazzy Linux - General 1 09-05-2003 10:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration