Presentation is loading. Please wait.

Presentation is loading. Please wait.

파일 접근권한 제어 stat 구조체의 st_mode 항목에 파일의 종류와 접근권한 정보저장 st_mode 값의 구조.

Similar presentations


Presentation on theme: "파일 접근권한 제어 stat 구조체의 st_mode 항목에 파일의 종류와 접근권한 정보저장 st_mode 값의 구조."— Presentation transcript:

1 파일 접근권한 제어 stat 구조체의 st_mode 항목에 파일의 종류와 접근권한 정보저장 st_mode 값의 구조

2 파일 종류 검색[1] 상수를 이용한 파일 종류 검색 파일의 종류 검색 관련 상수
st_mode 값과 상수값을 AND(&) 연산하면 파일의 종류 부분만 남게 된다.

3 [예제 3-3] 상수를 이용해 파일 종류 검색하기 (test1.c)
ex3_3.c 01 #include <sys/types.h> 02 #include <sys/stat.h> 03 #include <stdio.h> 04 05 int main(void) { struct stat buf; int kind; 08 stat("unix.txt", &buf); 10 printf("Mode = %o (16진수: %x)\n", (unsigned int)buf.st_mode, (unsigned int)buf.st_mode); 12 kind = buf.st_mode & S_IFMT; printf("Kind = %x\n", kind); 15 switch (kind) { case S_IFIFO: printf("unix.txt : FIFO\n"); break; case S_IFDIR: printf("unix.txt : Directory\n"); break;

4 [예제 3-3] 상수를 이용해 파일 종류 검색하기 # ex3_3.out Mode = 100644 (16진수: 81a4)
case S_IFREG: printf("unix.txt : Regular File\n"); break; } 27 return 0; 29 } # ex3_3.out Mode = (16진수: 81a4) Kind = 8000 unix.txt : Regular File

5 파일 종류 검색[2] 매크로를 이용한 파일 종류 검색 각 매크로는 인자로 받은 mode 값을 0xF000과 AND연산 수행
이 매크로는 POSIX 표준

6 [예제 3-4] 매크로를 이용해 파일 종류 검색하기 (test2.c)
ex3_4.c 01 #include <sys/types.h> 02 #include <sys/stat.h> 03 #include <stdio.h> 04 05 int main(void) { struct stat buf; 07 08 stat("unix.txt", &buf); 09 printf("Mode = %o (16진수: %x)\n",(unsigned int)buf.st_mode, (unsigned int)buf.st_mode); 11 12 if(S_ISFIFO(buf.st_mode)) printf("unix.txt : FIFO\n"); 13 if(S_ISDIR(buf.st_mode)) printf("unix.txt : Directory\n"); 14 if(S_ISREG(buf.st_mode)) printf("unix.txt : Regualr File\n"); 15 return 0; 17 } # ex3_4.out Mode = (16진수: 81a4) unix.txt : Regular File

7 파일 접근 권한 검색[1] 상수를 이용한 파일 접근 권한 검색 소유자의 접근권한 추출과 관련된 상수만 정의
소유자 외 그룹과 기타사용자의 접근권한은? st_mode의 값을 왼쪽으로 3비트 이동시키거나 상수값을 오른쪽으로 3비트 이동시켜 AND 수행 st_mode & (S_IREAD >> 3)

8 시프트 연산없이 직접 AND 연산이 가능한 상수 정의
파일 접근 권한 검색[2] POSIX에서 정의한 접근권한 검색 관련 상수 시프트 연산없이 직접 AND 연산이 가능한 상수 정의

9 [예제 3-5] 상수를 이용해 파일 접근 권한 검색하기 (test3.c)
ex3_5.c 01 #include <sys/types.h> 02 #include <sys/stat.h> 03 #include <stdio.h> 04 05 int main(void) { struct stat buf; 07 stat("unix.txt", &buf); 09 printf("Mode = %o (16진수: %x)\n", (unsigned int)buf.st_mode, (unsigned int)buf.st_mode); 10 if ((buf.st_mode & S_IREAD) != 0) printf("unix.txt : user has a read permission\n"); 13 if ((buf.st_mode & (S_IREAD >> 3)) != 0) printf("unix.txt : group has a read permission\n"); 16 if ((buf.st_mode & S_IROTH) != 0) printf("unix.txt : other have a read permission\n"); 19 return 0; 21 } # ex3_5.out Mode = (16진수: 81a4) unix.txt : user has a read permission unix.txt : group has a read permission unix.txt : other have a read permission

10 함수를 사용한 파일 접근 권한 검색 : access(2)
파일 접근 권한 검색[3] 함수를 사용한 파일 접근 권한 검색 : access(2) path에 지정된 파일이 amode로 지정한 권한을 가졌는지 확인하고 리턴 접근권한이 있으면 0을, 오류가 있으면 -1을 리턴 오류메시지 ENOENT : 파일이 없음 EACCESS : 접근권한이 없음 amode 값 R_OK : 읽기 권한 확인 W_OK : 쓰기 권한 확인 X_OK : 실행 권한 확인 F_OK : 파일이 존재하는지 확인 #include <unistd.h> int access(const char *path, int amode);

11 [예제 3-6] access 함수를 이용해 접근 권한 검색하기 (test4.c)
01 #include <sys/errno.h> 02 #include <unistd.h> 03 #include <stdio.h> 04 05 extern int errno; 06 07 int main(void) { int per; 09 if (access("unix.bak", F_OK) == -1 && errno == ENOENT) printf("unix.bak: File not exist.\n"); 12 per = access("unix.txt", R_OK); if (per == 0) printf("unix.txt: Read permission is permitted.\n"); else if (per == -1 && errno == EACCES) printf("unix.txt: Read permission is not permitted.\n"); 18 return 0; 20 } # ls -l unix* -rw-r--r root other 24 1월 8일 15:47 unix.txt # ex3_6.out unix.bak: File not exist. unix.txt: Read permission is permitted.

12 파일 기술자로 접근 권한 변경 : fchmod(2)
파일 접근권한 변경 파일명으로 접근권한 변경 : chmod(2) path에 지정한 파일의 접근권한을 mode값에 따라 변경 접근권한을 더할 때는 OR연산자를, 뺄 때는 NOT연산 후 AND 연산자 사용 chmod(path, S_ORWXU); chmod(path, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH); mode |= S_IWGRP; mode &= ~(S_IROTH); 파일 기술자로 접근 권한 변경 : fchmod(2) #include <sys/types.h> #include <sys/stat.h> int chmod(const char *path, mode_t mode); mode 값 설정 후 chmod(path, mode) 잊지말기! #include <sys/types.h> #include <sys/stat.h> int fchmod(int fd, mode_t mode);

13 [예제 3-7] chmod 함수 사용하기 (test5.c)
ex3_7.c 01 #include <sys/types.h> 02 #include <sys/stat.h> 03 #include <stdio.h> 04 05 int main(void) { struct stat buf; 07 chmod("unix.txt", S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH); stat("unix.txt", &buf); printf("1.Mode = %o\n", (unsigned int)buf.st_mode); 11 buf.st_mode |= S_IWGRP; buf.st_mode &= ~(S_IROTH); chmod("unix.txt", buf.st_mode); stat("unix.txt", &buf); printf("2.Mode = %o\n", (unsigned int)buf.st_mode); 17 return 0; 19 } mode값에 따라 권한이 어떻게 바뀌었나? # ls -l unix.txt -rw-r--r root other 24 1월 8일 15:47 unix.txt # ex3_7.out 1.Mode = 2.Mode = -rwxrwx root other 24 1월 8일 15:47 unix.txt

14 링크 파일 생성[1] 링크 하드링크 생성 : link(2) 이미 있는 파일이나 디렉토리에 접근할 수 있는 새로운 이름
같은 파일/디렉토리지만 여러 이름으로 접근할 수 있게 한다 하드링크 : 기존 파일과 동일한 inode 사용, inode에 저장된 링크 개수 증가 심볼릭 링크 : 기존 파일에 접근하는 다른 파일 생성(다른 inode 사용) 하드링크 생성 : link(2) 두 경로는 같은 파일시스템에 존재해야 함 #include <unistd.h> int link(const char *existing, const char *new);

15 [예제 3-8] link 함수 사용하기 (test6.c)
ex3_8.c 01 #include <sys/types.h> 02 #include <sys/stat.h> 03 #include <unistd.h> 04 #include <stdio.h> 05 06 int main(void) { struct stat buf; 08 stat("unix.txt", &buf); printf("Before Link Count = %d\n", (int)buf.st_nlink); 11 link("unix.txt", "unix.ln"); 13 stat("unix.txt", &buf); printf("After Link Count = %d\n", (int)buf.st_nlink); 16 return 0; 18 } # ls -l unix* -rwxrwx root other 월 8일 15:47 unix.txt # ex3_8.out Before Link Count = 1 After Link Count = 2 -rwxrwx root other 월 8일 15:47 unix.ln -rwxrwx root other 월 8일 15:47 unix.txt

16 링크 파일 생성[2] 심볼릭 링크 생성 : symlink(2) #include <unistd.h>
int symlink(const char *name1, const char *name2); 01 #include <sys/types.h> 02 #include <sys/stat.h> 03 #include <unistd.h> 04 05 int main(void) { symlink("unix.txt", "unix.sym"); 07 return 0; 09 } [예제 3-9] symlink 함수 사용하기 (test7.c) ex3_9.c # ls -l unix* -rwxrwx root other 월 8일 15:47 unix.ln -rwxrwx root other 월 8일 15:47 unix.txt # ex3_9.out lrwxrwxrwx 1 root other 월 11일 18:48 unix.sym -> unix.txt

17 심볼릭 링크의 내용 읽기 : readlink(2)
심볼릭 링크 정보 검색 심볼릭 링크 정보 검색 : lstat(2) lstat : 심볼릭 링크 자체의 파일 정보 검색 심볼릭 링크를 stat 함수로 검색하면 원본 파일에 대한 정보가 검색된다. 심볼릭 링크의 내용 읽기 : readlink(2) 심볼릭 링크의 데이터 블록에 저장된 내용 읽기 원본 파일의 경로 읽기 : realpath(3) 심볼릭 링크가 가리키는 원본 파일의 실제 경로명 출력 #include <sys/types.h> #include <sys/stat.h> int lstat(const char *path, struct stat *buf); #include <unistd.h> ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsiz); #include <stdlib.h> char *realpath(const char *restrict file_name, char *restrict resolved_name);

18 [예제 3-10] lstat 함수 사용하기 (test8.c)
ex3_10.c 01 #include <sys/types.h> 02 #include <sys/stat.h> 03 #include <unistd.h> 04 #include <stdio.h> 05 06 int main(void) { struct stat buf; 08 printf("1. stat : unix.txt ---\n"); stat("unix.txt", &buf); printf("unix.txt : Link Count = %d\n", (int)buf.st_nlink); printf("unix.txt : Inode = %d\n", (int)buf.st_ino); 13 printf("2. stat : unix.sym ---\n"); stat("unix.sym", &buf); printf("unix.sym : Link Count = %d\n", (int)buf.st_nlink); printf("unix.sym : Inode = %d\n", (int)buf.st_ino); 18 printf("3. lstat : unix.sym ---\n"); lstat("unix.sym", &buf);

19 [예제 3-10] lstat 함수 사용하기 printf("unix.sym : Link Count = %d\n", (int)buf.st_nlink); printf("unix.sym : Inode = %d\n", (int)buf.st_ino); 23 return 0; 25 } # ls -li unix* 192 -rwxrwx root other 월 8일 15:47 unix.ln 202 lrwxrwxrwx 1 root other 월 11일 18:48 unix.sym->unix.txt 192 -rwxrwx root other 월 8일 15:47 unix.txt # ex3_10.out 1. stat : unix.txt --- unix.txt : Link Count = 2 unix.txt : Inode = 192 2. stat : unix.sym --- unix.sym : Link Count = 2 unix.sym : Inode = 192 3. lstat : unix.sym --- unix.sym : Link Count = 1 unix.sym : Inode = 202

20 [예제 3-11] readlink 함수 사용하기 (test9.c)
ex3_11.c 01 #include <sys/stat.h> 02 #include <unistd.h> 03 #include <stdlib.h> 04 #include <stdio.h> 05 06 int main(void) { char buf[BUFSIZ]; int n; 09 n = readlink("unix.sym", buf, BUFSIZ); if (n == -1) { perror("readlink"); exit(1); } 15 buf[n] = '\0'; printf("unix.sym : READLINK = %s\n", buf); 18 return 0; 20 } # ex3_11.out unix.sym : READLINK = unix.txt # ls -l unix.sym lrwxrwxrwx 1 root other 8 1월 11일 18:48 unix.sym ->unix.txt

21 [예제 3-12] realpath 함수 사용하기 01 #include <sys/stat.h>
ex3_12.c 01 #include <sys/stat.h> 02 #include <stdlib.h> 03 #include <stdio.h> 04 05 int main(void) { char buf[BUFSIZ]; 07 realpath("unix.sym", buf); printf("unix.sym : REALPATH = %s\n", buf); 10 return 0; 12 } # ex3_12.out unix.sym : REALPATH = /export/home/jw/syspro/ch3/unix.txt

22 실습(p166) 연습문제 1 연습문제 2 연습문제 3


Download ppt "파일 접근권한 제어 stat 구조체의 st_mode 항목에 파일의 종류와 접근권한 정보저장 st_mode 값의 구조."

Similar presentations


Ads by Google