2016-11-12 20 views
0

내 OS는 리눅스입니다. 나는 C로 프로그램한다. 소프트 링크를 인식하기 위해 lstat()를 사용할 수 있다는 것을 안다. 즉, S_ISLNK (st.st_mode)를 사용하라. 하지만 링크가 하드 링크인지 어떻게 알 수 있습니까? 링크가 하드 링크 인 경우 일반 파일로 간주됩니다. 그러나 일반 파일과 하드 링크를 구별하고 싶습니다. 이 사건을 처리 할 방법이 있습니까?는 lstat는 사용하는 방법을() 경우 하드 링크를 결정하는 여부를

+0

는 소프트 다음 링크가 하드 링크되어야 않은 경우? –

+0

일반 파일 *은 * 하드 링크입니다. – wildplasser

답변

2

하지만 어떻게 링크 하드 링크입니다 인식 할 수 있습니까?

수 없습니다.

A "하드 링크는"실제로 특별한 일이 아니다. 이것은 디렉토리 엔트리와 같은 디스크상의 동일한 데이터를 가리키는 디렉토리 엔트리 일뿐입니다. 안정적으로 하드 링크를 식별 할 수있는 유일한 방법은 아이 노드로 파일 시스템에 모든 경로를 매핑하고 같은 값으로 어떤 것들 점을 볼 수있다.

0

구조체 합계는 하드 링크의 수에 대한 st_nlink 멤버가 있습니다. 그것은> 1이며 실제 파일 내용에 대한 하드 링크 중 하나에 파일이 명시되어 있습니다. 여기

struct stat { 
    dev_t  st_dev;  /* ID of device containing file */ 
    ino_t  st_ino;  /* inode number */ 
    mode_t st_mode; /* protection */ 
    nlink_t st_nlink; /* number of hard links */ 
    uid_t  st_uid;  /* user ID of owner */ 
    gid_t  st_gid;  /* group ID of owner */ 
    dev_t  st_rdev; /* device ID (if special file) */ 
    off_t  st_size; /* total size, in bytes */ 
    blksize_t st_blksize; /* blocksize for file system I/O */ 
    blkcnt_t st_blocks; /* number of 512B blocks allocated */ 
    time_t st_atime; /* time of last access */ 
    time_t st_mtime; /* time of last modification */ 
    time_t st_ctime; /* time of last status change */ 
}; 

샘플 프로그램이다

#include <sys/types.h> 
#include <sys/stat.h> 
#include <unistd.h> 
int main() 
{ 
    struct stat buf = {0}; 
    lstat("origfile", &buf); 
    printf("number of hard links for origfile: %d\n", buf.st_nlink); 
} 

출력 : 분명히

$ touch origfile 
$ ./a.out 
number of hard links for origfile: 1 
$ ln origfile hardlink1 
$ ./a.out 
number of hard links for origfile: 2 
$ ln origfile hardlink2 
$ ./a.out 
number of hard links for origfile: 3