리눅스 메타데이터

Jongwon·2021년 12월 10일
0

Linux Programming

목록 보기
15/25

stat 구조체
파일의 메타데이터를 가지고있는 구조체

struct stat {
    mode_t st_mode;    //파일 종류와 권한
    ino_t st_inode;    //Inode 번호
    nlink_t st_nlink;  //동일한 Inode 가리키는 link 수(hard link)
    uid_t st_uid;      //user id
    gid_t st_gid;      //group id
    dev_t st_dev;      //device위치
    dev_t st_rdev;     //파일 시스템이 setup되지 않은 raw device
    off_t st_size;     //파일 크기
    time_t st_atime;   //access time(접근 시간) - read()시
    time_t st_mtime;   //modify time(파일 데이터 변경시간) - write()시
    time_t st_ctime;   //status change time(metadata 변경시간) - link(), chmod(), write()시
    long st_blocksize;
    long st_blocks;
}

mode_t st_mode

  • 파일 형태 검사하는 매크로, 해당하면 1, 아니면 0 반환
S_ISREG(st_mode) : regular file
S_ISDIR(st_mode) : directory file
S_ISCHR(st_mode) : character device file
S_ISBLK(st_mode) : block device file
S_ISFIFO(st_mode) : fifo file
S_ISLNK(st_mode) : symbolic link file
S_ISSOCKET(st_mode) : socket file
  • 권한 관련 매크로 - R/W/X
S_IRUSR, S_IWUSR, S_IXUSR : user_read, user_write, user_execute
S_IRGRP, S_IWGRP, S_IXGRP : group_read, group_write, group_execute
S_IROTH, S_IWOTH, S_IXOTH : others_read, others_write, others_execute
S_IRWXU : user read+write+execute
S_IRWXG : group read+write+execute
S_IRWXO : others read+write+execute

디렉토리에 대하여 read는 디렉토리 안 파일명을 읽을 수 있고, write는 디렉토리에 파일 생성 및 제거, execution은 하위 디렉토리나 파일 접근 가능

  • 권한 관련 매크로 - UID, GID
    커널이 관리하고, 쉘에 세팅이 되어 있다.(즉 상속받는 모든 프로세스에 Id가 저장되어 있다)
S_ISUID : setuid
S_ISGID : setgid

read ID : 프로세스를 실제로 사용하는 사용자의 Id(로그인 시 사용된 Id).
effective ID : 파일에 대한 접근 권한의 기준이 되는 Id
일반적으로는 real id와 effective id는 일치한다.
setuid, setgid : 켜져있다면, 파일의 소유자가 effective Id가 되어 실행된다.

  • sticky bit
    파일이 존재하는 디렉토리에 접근가능해도 파일 삭제는 파일 소유자만 가능하도록 설정하는 비트
S_ISVTX : sticky bit

예전엔 swap area에 저장된 프로세스 수행이미지가 프로그램 종료 후에도 남아있게 하여 다음에 실행 시 더 빠르게 실행되도록 하기 위해 사용
현재는 공동 작업 디렉토리에서 다른 사용자에 의해 파일이 삭제되는 것을 방지하기 위해 사용

  • 시간 정보
    파일의 관리 및 보안에 사용
st_atime
st_mtime
st_ctime



메타데이터 관련 함수

<sys/types.h> + <sys/stat.h> 헤더

  • int stat(const char pathname, struct stat buf)
  • int fstat(int fd, struct stat *buf)
  • int lstat(const char pathname, struct stat buf) stat과 동일하나 심볼릭링크는 link파일 자체의 stat

  • mode_t umask(mode_t cmask) OS에 시스템호출을 통해 프로세스 내에서 유효한 umask값을 설정, 이전 umask값 반환
umask
파일 접근모드 생성 마스크, 보통은 로그인 초기 파일에 지정함
/etc/bashrc 에서 umask값 수정가능

파일의 최초 권한은 mode & ~umask & 01777

  • int chmod(const char *pathname, mode_t mode)
  • int fchmod(int fd, mode_t mode)
    새로운 권한 setid, sticky bit 설정, 성공은 0, -1이면 에러
    단, file 소유자와 effective uid가 동일해야 함.

  • int truncate(const char *pathname, off_t length)
  • int ftruncate(int fd, off_t length)
    파일의 크기를 length 바이트로 잘라냄, 기존 크기가 length보다 작다면 hole 만듬. 성공은 0, -1이면 에러

<unistd.h> 헤더

  • int access(const char *pathname, int mode)
    현재 내 real id가 r/w/x/f 권한이 있는지 확인, 0이면 권한존재, -1은 에러
mode 종류
    R_OK: read 여부 확인
    W_OK: write 여부 확인
    X_OK: execute 여부 확인
    F_OK: 파일 존재여부 확인

<sys/ioctl.h> 헤더

  • int ioctl(int fd, int request ...)
    디바이스마다 다른 서브커맨드(R/W/X, open, close 외에)를 수행. 개발자가 만들 때 커맨드 결정

<utime.h> 헤더

  • int utime(const char pathname, const utimebuf times)
    inode에 있는 access time와 modify time을 times로 수정(NULL이면 현재시간으로). 0이면 성공, -1은 에러
struct utime {
    time_t access_time;
    time_t modify_time;
} utimebuf

time_t는 1970.01.01 00:00부터의 초
profile
Backend Engineer

0개의 댓글