본문 바로가기

Programming/Linux_Platform

파일의 속성을 check 하는 코드 - fstatat, fstat, stat, lstat


아래는 dirfd 아래에 asecName 이라는 파일이 regular file인지 check 하는 코드


ex) dirfd = "/mnt/secure/asec/"

     asecName = "1234"


이면 /mnt/secure/asec/1234 라는 regular file 이 있는지 check 해 준다.



    struct stat sb;

    bool ret = (fstatat(dirfd, asecName, &sb, AT_SYMLINK_NOFOLLOW) == 0)

        && S_ISREG(sb.st_mode);


http://www.tutorialspoint.com/unix_system_calls/stat.htm


The following POSIX macros are defined to check the file type using the st_mode field:

TagDescription
S_ISREG(m)is it a regular file?
S_ISDIR(m)directory?
S_ISCHR(m)character device?
S_ISBLK(m)block device?
S_ISFIFO(m)FIFO (named pipe)?
S_ISLNK(m)symbolic link? (Not in POSIX.1-1996.)
S_ISSOCK(m)socket? (Not in POSIX.1-1996.)


The following flags are defined for the st_mode field:

S_IFMT0170000bitmask for the file type bitfields
S_IFSOCK0140000socket
S_IFLNK0120000symbolic link
S_IFREG0100000regular file
S_IFBLK0060000block device
S_IFDIR0040000directory
S_IFCHR0020000character device
S_IFIFO0010000FIFO
S_ISUID0004000set UID bit
S_ISGID0002000set-group-ID bit (see below)
S_ISVTX0001000sticky bit (see below)
S_IRWXU00700mask for file owner permissions
S_IRUSR00400owner has read permission
S_IWUSR00200owner has write permission
S_IXUSR00100owner has execute permission
S_IRWXG00070mask for group permissions
S_IRGRP00040group has read permission
S_IWGRP00020group has write permission
S_IXGRP00010group has execute permission
S_IRWXO00007mask for permissions for others (not in group)
S_IROTH00004others have read permission
S_IWOTH00002others have write permission
S_IXOTH00001others have execute permission