1 / 66

Linux/UNIX Programming APUE (Files & Directories) 문양세 강원대학교 IT 대학 컴퓨터과학전공

Linux/UNIX Programming APUE (Files & Directories) 문양세 강원대학교 IT 대학 컴퓨터과학전공. 강의 목표 및 내용. APUE (Files & Directories). 강의 목표 파일의 상태 및 구조를 이해한다 . 파일 시스템 구현을 이해한다 . 디렉토리 구조 및 구현을 이해한다 . 강의 내용 파일 상태 파일 접근 허가권 파일 시스템 구현 링크 (link) 디렉토리. stat() – 파일 상태 확인. APUE (Files & Directories).

lester-hahn
Download Presentation

Linux/UNIX Programming APUE (Files & Directories) 문양세 강원대학교 IT 대학 컴퓨터과학전공

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linux/UNIX Programming APUE (Files & Directories) 문양세 강원대학교 IT대학 컴퓨터과학전공

  2. 강의 목표 및 내용 APUE (Files & Directories) • 강의 목표 • 파일의 상태 및 구조를 이해한다. • 파일 시스템 구현을 이해한다. • 디렉토리 구조 및 구현을 이해한다. • 강의 내용 • 파일 상태 • 파일 접근 허가권 • 파일 시스템 구현 • 링크 (link) • 디렉토리

  3. stat() – 파일 상태 확인 APUE (Files & Directories) #include <sys/types.h> #include <sys/stat.h> int stat (const char *pathname, struct stat *buf ); int fstat (int filedes, struct stat *buf ); int lstat (const char *pathname, struct stat *buf ); 역할: 주어진 파일에 대한 정보를 stat 구조체에 얻어 온다. buf: stat 구조체에 대한 포인터 (정보를 가져올 장소) lstat()은 Symbolic Link가 가리키는 파일이 아닌 Symbolic Link 자체에 대한 정보를 얻는다. 리턴 값: 성공하면 0, 실패하면 -1

  4. stat 구조체 (1/3) APUE (Files & Directories) <sys/stat.h>에 정의 struct stat { mode_t st_mode; /* file type & mode (permissions) */ ino_t st_ino; /* i-node number (serial number) */ dev_t st_dev; /* device number (filesystem) */ dev_t st_rdev; /* device number for special files */ nlink_t st_nlink; /* number of links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ off_t st_size; /* size in bytes, for regular files */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last file status change */ long st_blksize;/* best I/O block size */ long st_blocks; /* number of 512-byte blocks allocated */ };

  5. stat 구조체 (2/3) APUE (Files & Directories) stat 구조체의 예 UNIX/Linux 종류 및 버전에 따라 stat 구조체는 다를 수 있음!

  6. stat 구조체 (3/3) APUE (Files & Directories) st_atime:마지막으로 파일의 데이터를 읽은 시각 st_mtime:마지막으로 파일의 데이터를 수정한 시각 st_ctime:파일의 내용이 아니고 이름/권한 같은 상태를 변경한 시각 st_blksize:가장 효율적인 I/O 블럭 크기 (예: 8192 bytes) st_blocks:파일이 차지하고 있는 공간의 크기 (512 byte의 블록 수)

  7. 파일 타입 (1/3) APUE (Files & Directories) • 보통 파일 (Regular File) • 데이터를 포함하고 있는 텍스트 또는 이진 파일 • 디렉토리 파일 (Directory File) • 파일의 이름들과 파일 정보에 대한 포인터들을 포함 • 문자 특수 파일 (Character Special File) • 시스템에 장착된 어떠 장치를 가리키는 파일 • 문자 단위로 데이터를 전송하는 장치 (c---------) • 블록 특수 파일 (Block Special File) • 시스템에 장착된 어떤 장치를 가리키는 파일 • 블럭 단위로 데이터를 전송하는 장치 (b---------)

  8. 파일 타입 (2/3) APUE (Files & Directories) 특수 파일 예제

  9. 파일 타입 (3/3) APUE (Files & Directories) • FIFO • 프로세스 간 통신에 사용되는 파일 (IPC Programming에서 사용됨) • Named Pipe라고도 부름 • 소켓 (socket) • 네트워크를 통한 프로세스 간 통신에 사용되는 파일 • Network Programming에서 사용하는 보편적인 방법 • 심볼릭 링크 (Symbolic link) • 다른 파일을 가리키는 포인터 역할을 하는 파일 (Windows의 “바로가기”에 해당)

  10. 파일 타입 검사 (1/2) APUE (Files & Directories) • 파일 타입을 검사하는 매크로 함수 • stat.h 파일(/usr/include/sys/stat.h)에 정의되어 있음 • S_ISREG() : 정규 파일 • S_ISDIR() : 디렉토리 파일 • S_ISCHR() : 문자 특수 파일 • S_ISBLK() : 블록 특수 파일 • S_ISFIFO() : pipe 또는 FIFO • S_ISLNK() : 심볼릭 링크 • S_ISSOCK() : 소켓 • 해당 종류의 파일이면 1, 아니면 0을 리턴 • stat 구조체의 st_mode 값을 검사함 st_mode type special permission 4 bits 3 bits 9 bits

  11. 파일 타입 검사 (2/2) APUE (Files & Directories) • 파일 타입 상수 • stat.h 파일(/usr/include/sys/stat.h)에 정의되어 있음 • S_IFREG : 정규 파일 • S_IFDIR : 디렉토리 파일 • S_IFCHR : 문자 특수 파일 • S_IFBLK : 블록 특수 파일 • S_IFFIFO : pipe 또는 FIFO • S_IFLNK : 심볼릭 링크 • S_IFSOCK : 소켓 • S_ISxxx() 매크로 함수는 S_IFxxx 상수 값의 설정 여부를 판단

  12. 예제: stat.c (1/2) APUE (Files & Directories)

  13. 예제: stat.c (2/2) APUE (Files & Directories) 실행 결과 Symbolic Link 자체의 정보를 얻기 위해서는 stat() 대신 lstat()을 사용

  14. 파일 허가권 (File Permissions) (1/2) APUE (Files & Directories) File access permission bits (stat 구조체의 st_mode 값) st_mode type special permission 4 bits 3 bits 9 bits

  15. 파일 허가권 (File Permissions) (2/2) APUE (Files & Directories) 실제 stat.h에 정의된 예제

  16. 관련 명령어 APUE (Files & Directories) • UNIX 명령어 chmod • File Access Permission 설정 • stat 구조체의 st_mode값을 변경 • UNIX 명령어 chown(root만 사용 가능) • File 소유 User ID 설정 • stat 구조체의 st_uid값을 변경 • UNIX 명령어 chgrp(root만 사용 가능) • File 소유 Group ID 설정 • stat 구조체의 st_gid

  17. 허가권 (Permissions) (1/3) APUE (Files & Directories) • Read 권한이 있어야 (open() 함수에서) • O_RDONLY, O_RDWR를 사용하여 파일을 열 수 있다. • Write 권한이 있어야 (open() 함수에서) • O_WRONLY, O_RDWR, O_TRUNC를 사용하여 파일을 열 수 있다. • 디렉토리에write 권한과 execute 권한이 있어야 • 해당 디렉토리에 파일을 생성할 수 있고, • 그 디렉토리의 파일을 삭제할 수 있다

  18. 허가권 (Permissions) (2/3) APUE (Files & Directories)

  19. 허가권 (Permissions) (3/3) APUE (Files & Directories) 파일이 포함된 모든 상위 디렉토리에 대해 execute 권한이 있어야 그 파일을 열 수 있다. 디렉토리에 대한 read 권한이 있어야 디렉토리 안에 들어 있는 파일 이름 목록을 읽을 수 있다. 디렉토리에 대한 write 권한이 있어야 디렉토리에 파일을 생성 삭제 할 수 있다. 디렉토리에 대한 execute 권한이 있어야 그 디렉토리나 그 하위 디렉토리에 있는 파일을 열 수 있다.

  20. Effective User ID APUE (Files & Directories) • Real User ID와 Real Group ID • 실제 사용자 ID와 그 사용자가 속한 그룹 ID • 로그인한 사용자 ID • 일반적으로, Shell 상에서 작업을 수행할 때의 User 및 Group ID로 이해할 수 있음 • Effective User ID와 Effective Group ID • 프로세스의 속성 • 일반적으로, 프로그램이 수행될 때의 User ID 및 Group ID로 이해할 수 있음 • 대부분의 경우 Real User ID 와 Real Group ID 가 사용되나, 다음에 설명하는 S_ISUID와 S_ISGID 비트가 Set된 경우에는 다르게 동작함

  21. S_ISUID와 S_ISGID (1/3) APUE (Files & Directories) • stat 구조체의 st_mode의 비트로 표현됨 • S_ISUID : set-user-ID • S_ISGID : set-group-ID • st_mode의 S_ISUID 비트가 설정된 실행 파일을 실행한 경우 • 그 실행 파일이 실행된 프로세스의 Effective User ID는 • Real User ID가 아니고, • 그 실행 파일 소유자의 User ID가 된다. • st_mode의 S_ISGID 비트가 설정된 실행 파일을 실행한 경우 • 그 실행 파일이 실행된 프로세스의 Effective Group ID는 • Real Group ID가 아니고, • 그 실행 파일 소유자의 group ID가 된다. st_mode type special permission 4 bits 3 bits 9 bits

  22. S_ISUID와 S_ISGID (2/3) APUE (Files & Directories) • S_ISUID, S_ISGID 모두가 설정된 실행 파일을 실행하는 경우 • Real User ID, Real Group ID 의 권한이 아니고, • 그 파일 소유 User ID, Group ID 의 권한으로 실행됨 • 예제) passwd명령어: 실행은 아무나 하나, root 권한으로 수행되어야 함

  23. S_ISUID와 S_ISGID (3/3) APUE (Files & Directories) passwd명령어가 수행될 때의 effective user ID

  24. access() APUE (Files & Directories) #include <unistd.h> int access ( const char *pathname, intmode ); Real User ID와 Real Group ID로 파일의 허가권 검사 리턴 값: 성공하면 0, 실패하면 -1 mode 값

  25. 예제: access() (1/2) APUE (Files & Directories)

  26. 예제: access() (2/2) APUE (Files & Directories)

  27. chmod(), fchmod() APUE (Files & Directories) #include <sys/stat.h> #include <sys/types.h> int chmod (const char *pathname, mode_t mode ); int fchmod (int filedes, mode_t mode ); • 파일에 대해 Access Permission을 변경한다 • stat 구조체의 st_mode 변경 • 리턴 값: 성공하면 0, 실패하면 -1 • mode : bitwise OR • S_ISUID, S_ISGID, S_ISVTX • S_IRUSR, S_IWUSR, S_IXUSR • S_IRGRP, S_IWGRP, S_IXGRP • S_IROTH, S_IWOTH, S_IXOTH

  28. 예제: chmod() (1/2) APUE (Files & Directories)

  29. 예제: chmod() (2/2) APUE (Files & Directories) 실행 결과

  30. chown() APUE (Files & Directories) #include <sys/types.h> #include <unistd.h> int chown (const char *pathname, uid_t owner, gid_t group ); int fchown (int filedes, uid_t owner, gid_t group ); int lchown (const char *pathname, uid_t owner, gid_t group ); • 파일의 User ID와 Group ID를 변경한다. • stat 구조체의 st_uid, st_gid 변경 • 리턴 값: 성공하면 0, 실패하면 -1 • lchown()은 심볼릭 링크 자체를 변경한다. • UNIX 종류 및 버전에 따라 차이가 있을 수 있다. • BSD 기반 시스템에서는 super-user만 변환 가능 • System V 계열 시스템은 일반 사용자도 변경 가능

  31. truncate(), ftruncate() APUE (Files & Directories) #include <sys/types.h> #include <unistd.h> int truncate (const char *pathname, off_t length ); int ftruncate (int filedes, off_t length ); 파일의 크기를 주어진 length로 줄인다. 리턴 값: 성공하면 0, 실패하면 -1

  32. truncate() 예제 APUE (Files & Directories)

  33. 강의 목표 및 내용 APUE (Files & Directories) • 강의 목표 • 파일의 상태 및 구조를 이해한다. • 파일 시스템 구현을 이해한다. • 디렉토리 구조 및 구현을 이해한다. • 강의 내용 • 파일 상태 • 파일 접근 허가권 • 파일 시스템 구현 - skip • 링크 (link) • 디렉토리

  34. Block I/O APUE (Files & Directories) I/O is always done in terms of blocks. Sequence of a read() system call

  35. Inode (Index Node) APUE (Files & Directories) • 한 파일은 하나의 i-node를 갖는다. • 파일에 대한 정보를 가지고 있다. • file size • file permissions • the owner and group ids • the last modification and last access times • if it's a regular or directory, the location of data blocks • if it's a special file, device numbers • if it's a symbolic link, the value of the symbolic link • stat 구조체의 필드는 i-node 에서 읽어온다.

  36. The Block Map (1/2) APUE (Files & Directories) • Direct block pointers • 10 direct block pointers • Indirect block pointer • an indirect block to address 1024 blocks • Double indirect block pointer • an double indirect block to contain 1024 indirect block pointers • How many blocks can be addressed?

  37. The Block Map (2/2) APUE (Files & Directories) Inode Disk blocks Direct block pointers to blocks 0 .. 9 . . . To blocks 10 .. 1033 (1024 blocks) Indirect pointers Double indirect pointers Locates up to 1000 indirects To blocks 1034 .. 2057

  38. File System Layout (1/3) APUE (Files & Directories) • Boot Block • Boot code that is used when UNIX is first activated. • Super Block (information about the entire file system) • the total number of blocks in the file system • the number of inodes in the inode free list • a bit map of free blocks • the size of block in bytes • the number of free blocks • the number of used blocks

  39. File System Layout (2/3) APUE (Files & Directories) • i-list (다음 페이지 참조) • all the inodes associated with the files on the disk • each block in the inode list can hold about 40 inodes • User Blocks • for storing file blocks • Bad Blocks • Several block that cannot be used

  40. 0 Boot block Super block 1 Inodes 1..40 2 i-list Inodes 41..80 3 . . . User block 200 201 User block User blocks . . . User block File System Layout (3/3) APUE (Files & Directories)

  41. 강의 목표 및 내용 APUE (Files & Directories) • 강의 목표 • 파일의 상태 및 구조를 이해한다. • 파일 시스템 구현을 이해한다. • 디렉토리 구조 및 구현을 이해한다. • 강의 내용 • 파일 상태 • 파일 접근 허가권 • 파일 시스템 구현 • 링크 (link) • 디렉토리

  42. unlink() APUE (Files & Directories) #include <unistd.h> int unlink (const char *pathname); • 파일을 삭제하는 역할을 수행함(엄밀히 말해서, 파일의 link count를 감소시킴) • stat 구조체의 st_mode 변경 • 리턴 값: 성공하면 0, 실패하면 -1 • 파일이 삭제될 경우, inode와 data block이 삭제(free)된다.

  43. 예제: unlink() (1/2) APUE (Files & Directories)

  44. 예제: unlink() (2/2) APUE (Files & Directories) 실행 결과

  45. symlink() - Symbolic Link – skip APUE (Files & Directories) • Symbolic Link는 파일에 대한 간접적인 포인터임 • 실제 파일에 대한 “경로”를 저장하고 있는 파일임 #include <unistd.h> int symlink (const char *actualpath, const char *sympath ); • Symbolic Link를 만든다. • Symbolic Link File의 내용은 actualpath에 해당함 • Symbolic Link File의 크기는 actualpath 문자열의 길이 (즉, 저장되는 내용이 문자열) • 리턴 값: 성공하면 0, 실패하면 -1 • 파라미터 설명 • actualpath: 실제 파일 • sympath: 만들 Symbolic Link의 이름

  46. 파일 관련 시간 APUE (Files & Directories) • stat 구조체의 st_atime • 파일의 데이터가 마지막으로 읽혔던(read 되었던) 시간 • 즉, 마지막으로 read()가 호출된 시간 • stat 구조체의 st_mtime • 파일의 데이터가 마지막으로 변경(write)된 시간 • 즉, 마지막으로 write()가 호출된 시간 • stat 구조체의 st_ctime • 파일의 stat 구조체의 내용이 마지막으로 변경된 시간 • chmod(), chown() 등이 호출된 시간

  47. utime() APUE (Files & Directories) #include <sys/types.h> #include <utime.h> int utime (const char *pathname, const struct utimbuf *times ); 파일의 최종 접근 시간과 최종 변경 시간을 조정한다. times가 NULL 이면, 현재시간으로 설정된다. 리턴 값: 성공하면 0, 실패하면 -1 UNIX 명령어 touch 참고 struct utimbuf { time_t actime; /* access time */ time_t modtime; /* modification time */ } 각 필드는 1970-1-1 00:00 부터 현재까지의 경과 시간을 초로 환산한 값

  48. utime() 예제: utime.c (1/2) APUE (Files & Directories)

  49. utime() 예제: utime.c (2/2) APUE (Files & Directories)

  50. 디렉토리 (Directory) APUE (Files & Directories) • 디렉토리 파일 • 일종의 파일이므로 open, read, close 함수 등을 사용할 수 있다. • 디렉토리 파일 사용에 편리한 새로운 함수들도 제공된다. • 디렉토리 파일의 내용은 구조체 dirent의 배열 형태로 저장됨 • file name: 파일 이름, 하위 디렉토리 이름, “.”, “..” • i-node number #include <dirent.h> struct dirent { ino_t d_ino; /* i-node number */ char d_name[NAME_MAX + 1]; /* filename */ }

More Related