파일시스템의 구조 : 일반 디렉토리들과 파일들을 조직화해서 모아놓은 것
- 파티션마다 파일 시스템을 설치
- boot block : 부팅에 사용
- super-block : 파티션의 정보 : I-node 사이즈, data block 사이즈(1K,2K,3K ...등의 크기, 몇개의 block이 있는지)
- i-node table : 파일시스템에 존재하는 파일들의 정보
- data blocks : 파일들의 실제 데이터
i-nodes
- i-node table : i-node들이 각각의 파일의 정보를 가지고 있음
- File type : 파일타입에 대한 정보
- 소유주, 어떤 그룹에 속해 있는지
- owner, group, other가 어떻게 접근 허용하는지에 대한 정보
- time stamps : 언제 마지막으로 접근했는지, 언제 수정됐는지, i-node의 정보가 언제 변경되었는지, 소유권변경 등
- 하드링크가 얼마나 있는지
- 파일의 사이즈
- 몇개의 block들이 할당되었는지
- 실제 파일의 data block들에 대한 포인터
- DB : 1K일 경우 14K -> 14개의 블록으로만 할당하면 용량이 부족
- 블록안에 포인터를 모아놓고 참조하면서 사용
- 파일 사이즈가 커질수록 참조하는 포인터들이 많아지므로 찾는 시간이 지연되어 오래걸림
Directory structure for /etc/passwd
- i-node에 파일이름이 없다면 - /directory data정보에 존재(block)
- 하나의 i-node를 여러개의 파일들이 가리킬 수 있음 -> 여러 이름으로 하나의 파일을 가리킬 수 있음(link)
- 데이터 블록이 바로 존재하지 않고 pointer가 계속 이어질 수 있음
- ex) 위 그림처럼 2번 i-node블록 -> /directory etc -> 7번 i-node블록 -> /etc directory psswd -> 6422 i-node -> /etc/passwd file
Hard links vs symbolic links
- Hard links : 하나의 파일에 여러가지 이름(link)가 존재할 수 있음/ i-node에는 파일이름이 저장되어 있지 않음
- symbolic links(or, soft links) : 데이터가 다른 파일의 이름 - ex) ln -s target name -> name이 target을 pointing하도록 symbolic link를 만들 수 있음
- 예제
- 61번 i-node를 /home/erena directory가 this라는 포인터로 가리키고 있고 /home/allyn directory가 that이라는 포인터로 동시에 가리키고 있음
- 309번 i-node를 /home/kiran directory가 other라는 symbolick link로 가리키고 있음 309번 안의 data block에는 /home/erena/this라는 위의 포인터를 가리키는 포인터가 존재 -> type에 symlink라고 표시되어 있음
- ex) ln -s /home/erena/this /home/kirena/other -> soft link
Functions for hard/symbolic links
- int link(const char *oldpath, const char *newpath); - oldpath가 가리키는 경로를 newpath로 똑같이 가리킴
- int symlink(const char *oldpath, const char *newpath); - oldpath를 newpath로 가리키게 함
- ssize_t readlink(const char *path, char *buf, size_t bufsiz); - symbolick link가 가리키는 것이 아닌 내용자체를 read해줌
- int unlink(const char *pathname); - link를 끊음 symlink는 삭제(i-node까지)하고 hard link는 끊어주고 다른 link가 있는 경우 i-node는 삭제되지 않음
stat(2) family of functions
- int stat(const char *pathname, struct stat *buf); - buf에 i-node의 정보 저장
- int fstat(int fd, struct stat *buf); - 파일을 open하여 i-node의 정보 저장
- int lstat(const char *pathname, struct stat *buf); - symlink자체의 파일정보를 가져와서 저장
struct stat
struct stat {
- dev_t st_dev; /* ID of device containing file */ -> ID - block Device의 정보 읽어옴(major,minor)
- ino_t st_ino; /* inode number */ -> Index 번호
- mode_t st_mode; /* protection */ -> 어떤 파일인지, 접근권한 저장
- nlink_t st_nlink; /* number of hard links */ -> 몇개의 파일 이름으로 포인팅 되었는지
- uid_t st_uid; /* user ID of owner */ -> 유저 ID
- gid_t st_gid; /* group ID of owner */ -> 그룹 ID
- dev_t st_rdev; /* device ID (if special file) */ -> 디바이스파일들의 ID정보(major, minor)
- off_t st_size; /* total size, in bytes */ -> 파일 사이즈
- blksize_t st_blksize; /* blocksize for filesystem I/O */ -> 각각의 블록 사이즈
- blkcnt_t st_blocks; /* number of blocks allocated */ -> 블록이 몇 개 있는지
- time_t st_atime; /* time of last access */ -> 언제 access 됐는지
- time_t st_mtime; /* time of last modification */ -> 언제 수정 됐는지(write,txt수정 등)
- time_t st_ctime; /* time of last status change */ -> status change가 언제 되었는지(i-node정보)
};
- st_mode
- st_mode에는 regular file인지 directory인지 등 저장된 파일이 어떤 타입인지에 대한 정보가 들어있음
- file type - 4byte
- permission - 읽기, 쓰기, 실행 등의 권한도 표시
- if문에서 S_IFMT라는 constant와 비교를 하거나 S_ISREG(각 타입마다 존재)라는 함수를 사용하여 비교할 수 있음
Directory functions
- DIR *opendir(const char *name); - 디렉토리파일을 엶
- int closedir(DIR *dirp); - 디렉토리파일을 닫음
- struct dirent *readdir(DIR *dirp); - 한 줄씩 정보를 읽어옴
- void rewinddir(DIR *dirp); - 처음으로 되돌림
- 마지막에 읽을 것이 없다면 NULL리턴
- struct에서 file name이 중요 -> 하나하나 읽어와서 처리
struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* not an offset; see NOTES */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file; not supported by all filesystem types */
char d_name[256]; /* filename */
};
Other directory functions
- int mkdir(const char *pathname, mode_t mode); -> 지정하는 모드로 directory를 만듦
- int rmdir(const char *pathname); -> 삭제
- int chdir(const char *path); -> 원하는 디렉토리로 이동
- char *getcwd(char *buf, size_t size); -> 현재 일하고 있는 working 디렉토리 위치를 리턴
'임베디드SW공학' 카테고리의 다른 글
[11주차]시그널과 타이머 (0) | 2018.11.27 |
---|---|
[10주차]리눅스 시스템 프로그래밍:Processes (0) | 2018.11.19 |
[8주차]리눅스시스템 프로그래밍: File I/O (0) | 2018.11.11 |
[6주차]Linux/Unix System Programming (0) | 2018.11.01 |
[5주차]git (0) | 2018.10.16 |