Presentation is loading. Please wait.

Presentation is loading. Please wait.

파일 시스템 및 디렉토리 ©숙대 창병모.

Similar presentations


Presentation on theme: "파일 시스템 및 디렉토리 ©숙대 창병모."— Presentation transcript:

1 파일 시스템 및 디렉토리 ©숙대 창병모

2 목표 파일 시스템 구현을 이해한다. 파일의 상태 및 구조를 이해한다. 디렉토리 구조 및 구현을 이해한다. ©숙대 창병모

3 파일 시스템 ©숙대 창병모

4 Inode (Index node) 한 파일은 하나의 i-node를 갖는다. 파일에 대한 모든 정보를 가지고 있음
file type 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 에서 읽어온다 ©숙대 창병모

5 The block map 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 ? ©숙대 창병모

6 Block map I-node Data blocks . . . to blocks Direct block 0 .. 9
pointers . . . Indirect pointers To blocks Double indirect pointers Locates up to 1000 indirects To blocks ©숙대 창병모

7 File system layout 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 : see Figure 11.21 the size of block in bytes the number of free blocks the number of used blocks ©숙대 창병모

8 File system layout i-list
all the inodes associated with the files on the disk each block in the inode list can hold about 40 inodes Data blocks for storing file blocks Bad blocks several blocks that cannot be used ©숙대 창병모

9 File system layout Inodes 1..40 Inodes 41..80 . . . . . . Boot block
Boot block Super block 1 Inodes 1..40 2 i-list Inodes 3 . . . Data block 200 201 Data block Data blocks . . . Data block ©숙대 창병모

10 파일 상태 ©숙대 창병모

11 파일 상태(file status) 파일 상태 어떤 것들이 있을까요? 어디에 있나요? 파일 자체에 대한 데이터 파일 타입
파일 크기 소유자 허가권 어디에 있나요? i-node ©숙대 창병모

12 stat() 역할 리턴 값 buf #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 구조체에 저장한다. 리턴 값 성공하면 0, 실패하면 -1 buf stat 구조체에 대한 포인터 ©숙대 창병모

13 stat 구조체 <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 */ }; ©숙대 창병모

14 stat 구조체 st_atime st_mtime st_ctime st_blksize st_blocks
마지막으로 파일의 데이터를 읽은 시각 st_mtime 마지막으로 파일의 데이터를 수정한 시각 st_ctime 파일의 내용이 아니고 이름/권한 같은 상태를 변경한 시각 st_blksize 가장 효율적인 I/O 블럭 크 (2장 8절 I/O 성능 참고, 8192 bytes) st_blocks 파일이 차지하고 있는 공간의 크기를 512 byte 블럭의 수로 ©숙대 창병모

15 파일 타입 보통 파일 (Regular file) 디렉토리 파일 (Directory file)
데이터를 포함하고 있는 텍스트 또는 이진 화일 디렉토리 파일 (Directory file) 파일의 이름들과 파일 정보에 대한 포인터들을 포함 문자 특수 파일 (Character special file) 시스템에 장착된 어떤 장치를 가리키는 파일 문자 단위로 데이터를 전송하는 장치 블록 특수 파일 (Block special file) 블럭 단위로 데이터를 전송하는 장치 ©숙대 창병모

16 파일 타입 FIFO 소켓 (socket) 심볼릭 링크 (Symbolic link) 프로세스 간 통신에 사용되는 파일
named pipe 라고도 불림 소켓 (socket) 네트웍을 통한 프로세스 간 통신에 사용되는 파일 심볼릭 링크 (Symbolic link) 다른 파일을 가리키는 포인터 역할을 하는 파일 ©숙대 창병모

17 파일 타입 검사 파일 타입을 검사하는 매크로 함수 해당 종류의 파일이면 1, 아니면 0 을 리턴
#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 값을 검사함 type special permission 4 3 9 st_mode ©숙대 창병모

18 예제: /* stat.c */ #include <sys/types.h> /* stat.c */
#include <sys/stat.h> int main(int argc, char *argv[]) { int i; struct stat buf; char *ptr; for (i = 1; i < argc; i++) { printf("%s: ", argv[i]); if (lstat(argv[i], &buf) < 0) { perror("lstat()"); continue; } if (S_ISREG(buf.st_mode)) ptr = "regular"; else if (S_ISDIR(buf.st_mode)) ptr = "directory"; else if (S_ISCHR(buf.st_mode)) ptr = "character special"; else if (S_ISBLK(buf.st_mode)) ptr = "block special"; else if (S_ISFIFO(buf.st_mode)) ptr = "fifo"; else if (S_ISLNK(buf.st_mode)) ptr = "symbolic link"; else if (S_ISSOCK(buf.st_mode)) ptr = "socket"; else ptr = "** unknown mode **"; printf("%s\n", ptr); exit(0); ©숙대 창병모

19 실행 결과 symbolic link 에 대한 정보도 얻기 위해서 stat() 대신 lstat() 를 사용
% a.out /etc /dev/ttya /bin a.out /etc: directory /dev/ttya: symbolic link /bin: symbolic link a.out: regular symbolic link 에 대한 정보도 얻기 위해서 stat() 대신 lstat() 를 사용 ©숙대 창병모

20 파일 허가권 ©숙대 창병모

21 파일 허가권(File Permissions)
각 파일에 대한 권한 관리 각 파일마다 허가권이 있다. owner/group/others 구분해서 관리한다. 파일에 대한 권한 읽기 r 쓰기 w 실행 x ©숙대 창병모

22 파일 허가권(File Permissions)
파일 허가권(file access permission) stat 구조체의 st_mode 의 값 #include <sys/stat.h> st_mode mask Meaning S_IRUSR user-read S_IWUSR user-write S_IXUSR user-execute S_IRGRP group-read S_IWGRP group-write S_IXGRP group-execute S_IROTH other-read S_IWOTH other-write S_IXOTH other-execute type special permission 4 3 9 st_mode ©숙대 창병모

23 허가권 read 권한이 있어야 write 권한이 있어야 디렉토리에 write 권한과 execute 권한이 있어야
O_RDONLY O_RDWR 을 사용하여 파일을 열 수 있다 write 권한이 있어야 O_WRONLY O_RDWR O_TRUNC 을 사용하여 파일을 열 수 있다 디렉토리에 write 권한과 execute 권한이 있어야 그 디렉토리에 파일을 생성할 수 있고 그 디렉토리의 파일을 삭제할 수 있다 삭제할 때 그 파일에 대한 read write 권한은 없어도 됨 ©숙대 창병모

24 chmod(), fchmod() 파일에 대해 access permission을 변경한다 리턴 값
#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_IRUSR, S_IWUSR, S_IXUSR S_IRGRP, S_IWGRP, S_IXGRP S_IROTH, S_IWOTH, S_IXOTH ©숙대 창병모

25 예제 #include <sys/types.h> #include <sys/stat.h>
#include "error.h" int main() { struct stat statbuf; /* turn on set-group-ID and turn off group-execute */ if (stat("foo", &statbuf) < 0) error("stat(foo)"); if (chmod("foo", (statbuf.st_mode & ~S_IXGRP) | S_ISGID) < 0) error("chmod(foo)"); /* set absolute mode to "rw-r--r--" */ if (chmod("bar", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) error("chmod(bar)"); return 0; } ©숙대 창병모

26 [실행 결과] $ ls -l foo bar -rw------- 1 lsj 없음 0 Jul 21 12:04 bar
-rw-rw-rw lsj 없음 Jul 21 12:04 foo $ a.out -rw-r--r lsj 없음 Jul 21 12:04 bar -rw-rwlrw lsj 없음 Jul 21 12:04 foo ©숙대 창병모

27 chown() 파일의 user ID와 group ID를 변경한다. 리턴 lchown()은 심볼릭 링크 자체를 변경한다
#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()은 심볼릭 링크 자체를 변경한다 super-user만 변환 가능 ©숙대 창병모

28 링크(link) ©숙대 창병모

29 stat 구조체 <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 */ }; ©숙대 창병모

30 Link count i-node 의 link count 파일의 삭제 파일의 이동 stat 구조체의 st_nlink
이 i-node 를 가리키는 directory entry 수 파일의 삭제 directory block에서 파일을 삭제하면 i-node 의 link count 가 1 감소 link count 가 0 에 도달하면 그 파일의 i-node 와 data block 이 삭제됨 파일의 이동 UNIX 명령어 mv 파일의 data block 이나 i-node 의 변화는 없이 directory entry 만 변경됨 ©숙대 창병모

31 link() 이미 존재하는 파일(existingpath)에 대해서 새로운 디렉토리 항목(newpath)을 만든다 리턴 값
#include <unistd.h> int link (const char *existingpath, const char *newpath ); 이미 존재하는 파일(existingpath)에 대해서 새로운 디렉토리 항목(newpath)을 만든다 리턴 값 성공하면 0, 실패하면 -1 hard link가 만들어짐 같은 i-node를 가리키는 directory entry가 하나 생김 그 i-node의 link count 가 하나 증가 ©숙대 창병모

32 Link to a File Directory1 int link(path1, path2) i-list char *path1;
main(argc, argv) int argc; char *argv[ ]; { int link(); if (link(argv[1], argv[2]) == -1) { exit(1); } exit(0) } i-list name1 36 inode Directory2 data blocks name2 36 ©숙대 창병모

33 unlink() 파일의 link count를 감소 시킴 리턴 값
#include <unistd.h> int unlink (const char *pathname); 파일의 link count를 감소 시킴 리턴 값 성공하면 0, 실패하면 -1 unlink()는 directry entry를 지우며, 파일의 link count를 1 감소 시킨다 link count 가 0이 되면, 파일을 디스크에서 제거한다. i-node와 data block 삭제 ©숙대 창병모

34 예제: unlink.c #include <sys/types.h> /* unlink.c */
#include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include "error.h" int main() { int fd, len; char buf[20]; fd = open("tempfile", O_RDWR | O_CREAT | O_TRUNC, 0666); if (fd == -1) error("open1"); unlink("tempfile"); len = write(fd, "How are you?", 12); if (len != 12) error("write"); lseek(fd, 0L, SEEK_SET); len = read(fd, buf, sizeof(buf)); if (len < 0) error("read"); buf[len] = '\0'; printf("%s\n", buf); close(fd); fd = open("tempfile", O_RDWR); if (fd == -1) error("open2"); return 0; } ©숙대 창병모

35 utime() 파일의 최종 접근 시간과 최종 변경 시간을 조정한다. times가 NULL 이면, 현재시간으로 설정된다.
#include <sys/types.h> #include <utime.h> int utime (const char *pathname, const struct utimbuf *times ); 파일의 최종 접근 시간과 최종 변경 시간을 조정한다. times가 NULL 이면, 현재시간으로 설정된다. 리턴 값 성공하면 0, 실패하면 -1 UNIX 명령어 touch 참고 ©숙대 창병모

36 utime() 각 필드는 1970-1-1 00:00 부터 현재까지의 경과 시간을 초로 환산한 값 struct utimbuf {
time_t actime; /* access time */ time_t modtime; /* modification time */ } 각 필드는 :00 부터 현재까지의 경과 시간을 초로 환산한 값 ©숙대 창병모

37 예제: utime.c #include <sys/types.h> /* utime.c */
#include <sys/stat.h> #include <fcntl.h> #include <utime.h> int main(int argc, char *argv[]) { int i; struct stat statbuf; struct utimbuf timebuf; for (i = 1; i < argc; i++) { if (stat(argv[i], &statbuf) < 0) /* fetch current times */ perror(argv[i]); if (open(argv[i], O_RDWR | O_TRUNC) < 0) /* truncate */ timebuf.actime = statbuf.st_atime; timebuf.modtime = statbuf.st_mtime; if (utime(argv[i], &timebuf) < 0) /* reset times */ } return 0; ©숙대 창병모

38 디렉토리 ©숙대 창병모

39 What is a Directory A kind of file used to organize
regular, pipe, special files and other directory format imposed by Unix file names are contained only in directory entries permissions r: readable w: writable x: searchable ©숙대 창병모

40 Directory and File A Directory i-list name 36 inode data blocks
©숙대 창병모

41 Directory 2 usr4 bin3 cp7 ls5 test.c6 /usr/test.c Block # Permissions
Inode # Permissions 1 2 dr-xr-xr-x 2 3 dr-xr-xr-x 4 dr-xr-xr-x usr4 5 r-xr-xr-x bin3 6 204,206-r-xr-xr-x cp7 ls5 test.c6 . 2 .. 2 bin 3 usr 4 200 201 . 3 .. 2 ls 5 cp 7 /usr/test.c 202 . 4 .. 2 test.c 6 ls executable 203 204 test.c 1st block 205 cp executable ©숙대 창병모 206 test.c 2nd block

42 Directory 디렉토리 파일 디렉토리 파일의 내용은 구조체 dirent의 배열
일종의 파일이므로 open, read, close 함수 등을 사용할 수 있다 디렉토리 파일 사용에 편리한 새로운 함수들도 제공된다. 디렉토리 파일의 내용은 구조체 dirent의 배열 d_name : 파일 이름, 하위 디렉토리 이름, ".", ".." d_ino: i-node number #include <dirent.h> struct dirent { ino_t d_ino; /* i-node number */ char d_name[NAME_MAX + 1]; /* filename */ } ©숙대 창병모

43 디렉토리 접근 opendir()로 디렉토리 파일을 열고 리턴 값 DIR 구조체
#include <sys/types.h> #include <dirent.h> DIR *opendir (const char *pathname); struct dirent *readdir(DIR *dp); opendir()로 디렉토리 파일을 열고 리턴 값 성공하면 DIR 구조체 주소, 실패하면 NULL DIR 구조체 열린 디렉토리를 나타내는 구조체 like FILE struct ©숙대 창병모

44 readdir() readdir() 디렉토리의 current file position 직접 쓸 수 없다 디렉토리 내용을 읽는다
해당 디렉토리에 대한 읽기 권한이 있어야 한다 한번에 dirent 하나씩 읽는다. 디렉토리의 current file position 읽을 때마다 읽은 구조체 dirent 크기 만큼 증가한다. 직접 쓸 수 없다 그러나 쓰기권한이 있어도 write 함수로 직접 쓸 수는 없다. mkdir rmdir 를 사용해야 함 ©숙대 창병모

45 rewinddir(), closedir()
#include <sys/types.h> #include <dirent.h> void rewinddir (DIR *dp); int closedir (DIR *dp); rewinddir() 디렉토리 파일의 current file position을 처음으로 옮긴다 closedir() 디렉토리 파일을 닫는다. 리턴 값: 성공하면 0, 실패하며 -1 ©숙대 창병모

46 mkdir() 새로운 디렉토리를 만든다. 리턴 값 성공하면 "." 와 ".." 파일은 자동적으로 만들어진다.
#include <sys/types.h> #include <sys/stat.h> int mkdir (const char *pathname, mode_t mode ); 새로운 디렉토리를 만든다. 리턴 값 성공하면 0, 실패하면 -1 성공하면 "." 와 ".." 파일은 자동적으로 만들어진다. "."은 이 디렉토리 파일의 i-node를, ".."은 부모 디렉토리 파일의 i-node를 가르킨다 ©숙대 창병모

47 rmdir() 비어 있는 디렉토리를 삭제한다. 리턴 값 #include <unistd.h>
int rmdir (const char *pathname ); 비어 있는 디렉토리를 삭제한다. 디렉토리 파일의 link count를 감소 시킴 link count가 0이 되면 삭제됨 리턴 값 성공하면 0, 실패하면 -1 ©숙대 창병모

48 예제: listfiles.c #include <sys/types.h> /* listfiles.c */
#include <sys/stat.h> #include <dirent.h> #include <stdio.h> /* typeOfFile - return the letter indicating the file type. */ char typeOfFile(mode_t mode) { switch (mode & S_IFMT) { case S_IFREG: return('-'); case S_IFDIR: return('d'); case S_IFCHR: return('c'); case S_IFBLK: return('b'); case S_IFLNK: return('l'); case S_IFIFO: return('p'); case S_IFSOCK: return('s'); } return('?'); ©숙대 창병모

49 예제: listfiles.c /* permOfFile - return the file permissions in an "ls"-like string. */ char* permOfFile(mode_t mode) { int i; char *p; static char perms[10]; p = perms; strcpy(perms, " "); for (i=0; i < 3; i++) { if (mode & (S_IREAD >> i*3)) *p = 'r'; p++; if (mode & (S_IWRITE >> i*3)) *p = 'w'; if (mode & (S_IEXEC >> i*3)) *p = 'x'; } if ((mode & S_ISUID) != 0) perms[2] = 's'; if ((mode & S_ISGID) != 0) perms[5] = 's'; if ((mode & S_ISVTX) != 0) perms[8] = 't'; return(perms); ©숙대 창병모

50 예제: listfiles.c /* outputStatInfo - print out the contents of the stat structure. */ void outputStatInfo(char *pathname, char *filename, struct stat *st) { int n; char slink[BUFSIZ+1]; printf("%5d ", st->st_blocks); printf("%c%s ", typeOfFile(st->st_mode), permOfFile(st->st_mode)); printf("%3d ", st->st_nlink); printf("%5d/%-5d ", st->st_uid, st->st_gid); if (((st->st_mode & S_IFMT) != S_IFCHR) && ((st->st_mode & S_IFMT) != S_IFBLK)) printf("%9d ", st->st_size); else printf("%4d,%4d ", major(st->st_rdev), minor(st->st_rdev)); printf("%.12s ", ctime(&st->st_mtime) + 4); printf("%s", filename); if ((st->st_mode & S_IFMT) == S_IFLNK) { if ((n = readlink(pathname, slink, sizeof(slink))) < 0) printf(" -> ???"); else printf(" -> %.*s", n, slink); } ©숙대 창병모

51 예제: listfiles.c int main(int argc, char **argv) { DIR *dp;
char *dirname, filename[BUFSIZ+1]; struct stat st; struct dirent *d; while (--argc) { /* for each directory on the command line... */ dirname = *++argv; if ((dp = opendir(dirname)) == NULL) /* Open the directory */ perror(dirname); printf("%s:\n", dirname); while ((d = readdir(dp)) != NULL) { /* For each file in the directory... */ sprintf(filename, "%s/%s", dirname, d->d_name); // the full file name. if (lstat(filename, &st) < 0) /* Find out about it. */ perror(filename); outputStatInfo(filename, d->d_name, &st); // Print out the info putchar('\n'); } closedir(dp); return 0; ©숙대 창병모


Download ppt "파일 시스템 및 디렉토리 ©숙대 창병모."

Similar presentations


Ads by Google