LinuxUNIX Programming APUE Files Directories IT stat APUE





























































- Slides: 61

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


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 Page 3 UNIX System Programming by Yang-Sae Moon

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

stat 구조체 (2/3) APUE (Files & Directories) stat 구조체의 예 Page 5 UNIX System Programming by Yang-Sae Moon



파일 타입 (2/3) APUE (Files & Directories) 특수 파일 예제 Page 8 UNIX System Programming by Yang-Sae Moon


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


예제: stat. c (1/2) APUE (Files & Directories) Page 12 UNIX System Programming by Yang-Sae Moon

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

파일 허가권 (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 st_mode mask Meaning Octal Code S_IRUSR user-read 0400 S_IWUSR user-write 0200 S_IXUSR user-execute 0100 S_IRGRP group-read 0040 S_IWGRP group-write 0020 S_IXGRP group-execute 0010 S_IROTH other-read 0004 S_IWOTH other-write 0002 S_IXOTH other-execute 0001 Page 14 UNIX System Programming by Yang-Sae Moon

파일 허가권 (File Permissions) (2/2) APUE (Files & Directories) 실제 stat. h에 정의된 예제 Page 15 UNIX System Programming by Yang-Sae Moon

관련 명령어 APUE (Files & Directories) Linux/Unix 명령어 chmod • File Access Permission 설정 • stat 구조체의 st_mode 값을 변경 Linux/Unix 명령어 chown (root만 사용 가능) • File 소유 User ID 설정 • stat 구조체의 st_uid 값을 변경 Linux/Unix 명령어 chgrp (root만 사용 가능) • File 소유 Group ID 설정 • stat 구조체의 st_gid Page 16 UNIX System Programming by Yang-Sae Moon


허가권 (Permissions) (2/3) Page 18 APUE (Files & Directories) UNIX System Programming by Yang-Sae Moon



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

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 권한으로 수행되어야 함 Page 22 UNIX System Programming by Yang-Sae Moon

S_ISUID와 S_ISGID (3/3) APUE (Files & Directories) passwd 명령어가 수행될 때의 effective user ID Page 23 UNIX System Programming by Yang-Sae Moon

access() APUE (Files & Directories) #include <unistd. h> int access ( const char *pathname, int mode ); Real User ID와 Real Group ID로 파일의 허가권 검사 리턴 값: 성공하면 0, 실패하면 -1 mode 값 mode Description R_OK test for read permission W_OK test for write permission X_OK test for execute permission F_OK test for existence of tile Page 24 UNIX System Programming by Yang-Sae Moon

예제: access() (1/2) APUE (Files & Directories) Page 25 UNIX System Programming by Yang-Sae Moon

예제: access() (2/2) APUE (Files & Directories) Page 26 UNIX System Programming by Yang-Sae Moon

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 Page 27 UNIX System Programming by Yang-Sae Moon

예제: chmod() (1/2) APUE (Files & Directories) Page 28 UNIX System Programming by Yang-Sae Moon

예제: chmod() (2/2) APUE (Files & Directories) 실행 결과 Page 29 UNIX System Programming by Yang-Sae Moon

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()은 심볼릭 링크 자체를 변경한다. Linux/Unix 종류 및 버전에 따라 차이가 있을 수 있다. Page 30 UNIX System Programming by Yang-Sae Moon

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 Page 31 UNIX System Programming by Yang-Sae Moon

truncate() 예제 (1/2) APUE (Files & Directories) Page 32 UNIX System Programming by Yang-Sae Moon

truncate() 예제 (2/2) APUE (Files & Directories) Page 33 UNIX System Programming by Yang-Sae Moon


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

예제: unlink() (1/2) APUE (Files & Directories) Page 36 UNIX System Programming by Yang-Sae Moon

예제: unlink() (2/2) APUE (Files & Directories) 실행 결과 Page 37 UNIX System Programming by Yang-Sae Moon


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

utime() 예제: utime. c (1/2) Page 40 APUE (Files & Directories) UNIX System Programming by Yang-Sae Moon

utime() 예제: utime. c (2/2) Page 41 APUE (Files & Directories) UNIX System Programming by Yang-Sae Moon



rewinddir(), closedir() APUE (Files & Directories) #include <sys/types. h> #include <dirent. h> void rewinddir (DIR *dp); int closedir (DIR *dp); rewinddir() • 디렉토리 파일의 current file offset을 처음으로 옮긴다. (첫 번째 엔트리로 옮긴다. ) closedir() • 디렉토리 파일을 닫는다. • 리턴 값: 성공하면 0, 실패하면 -1 Page 44 UNIX System Programming by Yang-Sae Moon


rmdir() APUE (Files & Directories) #include <unistd. h> int rmdir (const char *pathname ); 비어있는 디렉토리를 삭제한다. 리턴 값: 성공하면 0, 실패하면 -1 Page 46 UNIX System Programming by Yang-Sae Moon


예제: listfiles. c (2/7) APUE (Files & Directories) • 추가: 현재 cs 1에서는 <time. h>를 include해야 함! Page 48 UNIX System Programming by Yang-Sae Moon

예제: listfiles. c (3/7) APUE (Files & Directories) Page 49 UNIX System Programming by Yang-Sae Moon

예제: listfiles. c (4/7) APUE (Files & Directories) +4: 앞의 네 바이트는 출력하지 말라는 의미 Page 50 UNIX System Programming by Yang-Sae Moon

예제: listfiles. c (5/7) APUE (Files & Directories) Page 51 UNIX System Programming by Yang-Sae Moon

예제: listfiles. c (6/7) APUE (Files & Directories) Page 52 UNIX System Programming by Yang-Sae Moon

예제: listfiles. c (7/7) APUE (Files & Directories) Page 53 UNIX System Programming by Yang-Sae Moon

chdir(), fchdir() APUE (Files & Directories) #include <unistd. h> int chdir (const char *pathname); int fchdir (int filedes); 현재 작업 디렉토리를 변경한다. ($ cd) 반환 값: 성공하면 0, 실패하면 -1 현재 작업 디렉토리는 프로세스의 속성 (프로세스의 working directory가 변하는 것임) Page 54 UNIX System Programming by Yang-Sae Moon

getcwd() APUE (Files & Directories) #include <unistd. h> char *getcwd (char *buf, size_t size ); 현재 작업 디렉토리의 경로명을 얻는다. ($ pwd) 반환 값: 성공하면 buf의 주소, 실패하면 NULL Page 55 UNIX System Programming by Yang-Sae Moon

예제: chdir(), getcwd() (1/2) Page 56 APUE (Files & Directories) UNIX System Programming by Yang-Sae Moon

예제: chdir(), getcwd() (2/2) APUE (Files & Directories) 실행 결과 Page 57 UNIX System Programming by Yang-Sae Moon

sync(), fsync() APUE (Files & Directories) #include <unistd. h> void sync(); int fsync(int filedes); 버퍼에 있는 내용을 디스크에 쓰도록 한다. sync()는 시스템 데몬 프로세스에 의해서 30초마다 호출된다. fsync()는 지정된 파일에 대해서만 I/O 작업을 수행하도록 한다. 참고) O_SYNC 플래그 (모든 write()를 sync mode로 처리함) Page 58 UNIX System Programming by Yang-Sae Moon

예제: sync() (1/2) APUE (Files & Directories) Page 59 UNIX System Programming by Yang-Sae Moon

예제: sync() (2/2) APUE (Files & Directories) Page 60 UNIX System Programming by Yang-Sae Moon

Homework#8 Page 61 UNIX System Programming by Yang-Sae Moon
Lrk v4 trojan
Unix
Cjis meaning
Dot powai files are binary files
Cjis security awareness
Apue
Apue
Apue
S_isuid
Directories pricing tables schedules and name list
Makefile multiple source directories
What are subject directories
Static hashing and dynamic hashing
System programming vs application programming
Greedy vs dynamic programming
Linear vs integer programming
Programing adalah
Perbedaan linear programming dan integer programming
Qvd files
Bob files
Download slidetodoc
File organization types
Crucial aspects of preparing digital audio files
Ged m files
Computer operating system worksheets
Azure file sync deduplication
320 fahrenheit to celsius
Chapter 17 darwin's theory of evolution
Visual basic and database files
49cfr391
Omfi media files
Flash to unity converter
Face recognition
File pointer
Chapter 17 summary things fall apart
Eznec download
Data files in c
Attach2dynamics
Arterial spurting pattern
Tsd export files
Finished files are the result of scientific study
Steve reich midi files
Sequential files in vb
Heap files in dbms
Uvm zoofiles
Ilf file
Edl files
Management files for
Interrelated collection of data
Arims labels
Save log stata
Stateless vs stateful server
Pascal files
Rftp
Www.bf.tku.edu.tw/files/news chapter 17
Open python files
Disadvantages of random access memory
Icons that show a folder represent a
Salesforce files connect implementation guide
Ibverbs header files not found
Stty in linux
Website