4.1 Introduction

이번 장에는 저번 장에서 파일 열기, 읽기, 쓰기를 본 것에 이어 파일 시스템의 추가적인 기능과 프로퍼티를 볼 것이다. stat 함수와 stat 구조를 볼 것이다. 이 과정에서 소유자, 권한 등의 것들을 수정하는 함수에 대해 설명할 것이다.

4.2 stat, fstat, fstatat, and lstat Functions

#include <sys/stat.h>
int stat(const char *restrict pathname, struct stat *restrict buf );
int fstat(int fd, struct stat *buf );
int lstat(const char *restrict pathname, struct stat *restrict buf );
int fstatat(int fd, const char *restrict pathname,
struct stat *restrict buf, int flag);
All four return: 0 if OK, −1 on error

stat함수는 pathname이 주어지면 이름이 붙여진 파일들의 구조 정보를 리턴한다..

fstat함수는 파일 디스크립터(fd)에 의해 열려 있는 파일의 정보를 얻는다.

lstat함수는 stat함수와 비슷하지만 이름이 붙여진 파일이 symbolic link라면 lstat함수는 symbolic link에 대한 정보를 리턴한다. (symbolic link에 의해 레퍼런스된 파일이 아닌)

fstatat함수는 pathname과 관계된 열린 디렉토리에 대한 분석을 리턴한다.

4.3 File Types

대부분의 파일은 레귤러 파일과 디렉토리 두 개로 나누어지지만 추가적인 파일 타입이 존재한다.

  1. Regular file. The most common type of file, which contains data of some form. There is no distinction to the UNIX kernel whether this data is text or binary. Any interpretation of the contents of a regular file is left to the application processing the file. One notable exception to this is with binary executable files. To execute a program, the kernel must understand its format. All binary executable files conform to a format that allows the kernel to identify where to load a program’s text and data.
  2. Directory file. A file that contains the names of other files and pointers to information on these files. Any process that has read permission for a directory file can read the contents of the directory, but only the kernel can write directly to a directory file. Processes must use the functions described in this chapter to make changes to a directory.
  3. Block special file. A type of file providing buffered I/O access in fixed-size units to devices such as disk drives. Note that FreeBSD no longer supports block special files. All access to devices is through the character special interface.
  4. Character special file. A type of file providing unbuffered I/O access in variable-sized units to devices. All devices on a system are either block special files or character special files.
  5. FIFO. A type of file used for communication between processes. It’s sometimes called a named pipe. We describe FIFOs in Section 15.5.
  6. Socket. A type of file used for network communication between processes. A socket can also be used for non-network communication between processes on a single host. We use sockets for interprocess communication in Chapter 16.
  7. Symbolic link. A type of file that points to another file. We talk more about symbolic links in Section 4.17.

파일의 타입은 stat 구조의 st_mode 멤버로 인코드 되어있다.

스크린샷 2022-01-19 오후 4.13.40.png