Computer Security

#12 파일 속성 본문

리눅스 시스템 프로그래밍

#12 파일 속성

쿠리 Kuri 2022. 9. 17. 18:30
반응형

파일 속성 조회

int stat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);
int lstat(const char *pathname, struct stat *statbuf);

파라미터

 

- pathname : 파일 경로

 

- fd : 열린 파일 디스크립터

 

- statbuf : 파일 속성 버퍼

 

 

 

 

반환 값

 

- 성공 시 0

 

- 실패 시 -1

 

 

 


stat 명령같은 경우,  소프트 링크같은경우에 소프트링크가 가르키는 원본파일의 정보를 가져온다.

하지만, lstat 의 경우 pathname이 가르키는 파일이 심볼릭링크 파일인 경우, 원본파일의 정보를 가져오는 것이 아니라, 심볼릭 링크파일의 정보를 가져온다.

 

 

 

 

 

 

 


어떤 정보를 가져오는지 알아보자.

struct stat {
    dev_t     st_dev;     /* ID of device containing file */
    ino_t     st_ino;     /* inode number */
    mode_t    st_mode;    /* 파일의 종류 및 접근권한 */
    nlink_t   st_nlink;   /* hardlink 된 횟수 */
    uid_t     st_uid;     /* 파일의 owner */
    gid_t     st_gid;     /* group ID of owner */
    dev_t     st_rdev;    /* device ID (if special file) */
    off_t     st_size;    /* 파일의 크기(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 */
};

 

 

 

 

 


파일의 type을 조사 할 수 있는 매크로를 알아보자.

 

 

 

 

 


예시

stat(pathname, &sb);
if (S_ISREG(sb.st_mode)) {
     /* Handle regular file */
}

 

반응형
Comments