Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to figure out if a file is a link?

I have the below code only a part of it is shown here and I am checking if a the type of file.

struct stat *buf /* just to show the type buf is*/ 

switch (buf.st_mode & S_IFMT) {
     case S_IFBLK:  printf(" block device\n");            break;
     case S_IFCHR:  printf(" character device\n");        break;
     case S_IFDIR:  printf(" directory\n");               break;
     case S_IFIFO:  printf(" FIFO/pipe\n");               break;
     case S_IFLNK:  printf(" symlink\n");                 break;
     case S_IFREG:  printf(" regular file\n");            break;
     case S_IFSOCK: printf(" socket\n");                  break;
     default:       printf(" unknown?\n");                break;
}

The problem: value of st_mode obtained when I do a printf("\nMode: %d\n",buf.st_mode); the result is 33188.

I tested my program with a regular file type and a symbolic link. In both cases the output was "regular file" i.e the symbolic link case is failing and I fail to understand why?

like image 955
Eternal Learner Avatar asked Oct 21 '10 06:10

Eternal Learner


People also ask

How will you recognize a file is a hard link or a soft link?

The primary difference between these links is that a hard link directly references the file. In contrast, a soft link is a name-based reference to a file. However, a Hard link links files and directories inside the same file system, whereas a Soft link may traverse file system boundaries.

How do I know if a path is symbolic link?

path. islink() method in Python is used to check whether the given path represents an existing directory entry that is a symbolic link or not.

How do you tell if a file is a hard link Windows?

A simple way for one-off checks: Are file attributes changing together? If you want to test whether the files A and B are hard links to the same content and you do not want to install or run anything, open the Properties window of file A and toggle for example its Read-only attribute.

How do I identify a soft link?

Showing soft link using Find command in UnixWhen you use the find command with option type and specify the type as small L ( l for the link), it displays all soft links in the specified path.


1 Answers

From the stat (2) man page:

stat() stats the file pointed to by path and fills in buf.

lstat() is identical to stat(), except that if path is a symbolic link, then the link itself is stat-ed, not the file that it refers to.

In other words, the stat call will follow the symbolic link to the target file and retrieve the information for that. Try using lstat instead, it will give you the information for the link.


If you do the following:

touch junkfile
ln -s junkfile junklink

then compile and run the following program:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main (void) {
    struct stat buf;
    int x;

    x = stat ("junklink", &buf);
    if (S_ISLNK(buf.st_mode)) printf (" stat says link\n");
    if (S_ISREG(buf.st_mode)) printf (" stat says file\n");

    x = lstat ("junklink", &buf);
    if (S_ISLNK(buf.st_mode)) printf ("lstat says link\n");
    if (S_ISREG(buf.st_mode)) printf ("lstat says file\n");

    return 0;
}

you will get:

 stat says file
lstat says link

as expected.

like image 94
paxdiablo Avatar answered Sep 30 '22 19:09

paxdiablo