Namespaces
Variants

std::filesystem:: status, std::filesystem:: symlink_status

From cppreference.net
헤더 파일에 정의됨 <filesystem>
(1) (C++17부터)
(2) (C++17부터)
(3) (C++17부터)
(4) (C++17부터)
1,2) p 로 식별된 파일 시스템 객체의 유형과 속성을 POSIX stat 과 같이 결정합니다(심볼릭 링크는 대상까지 따라감). 다음 설명에서 prms ( m & perms :: mask ) 의 결과이며, 여기서 m 은 POSIX struct stat 에서 st_mode 를 가져와 std:: filesystem :: perms 타입으로 변환한 것처럼 얻습니다.
  • p 가 일반 파일(POSIX S_ISREG 와 같이)인 경우, file_status ( file_type :: regular , prms ) 를 반환합니다.
  • p 가 디렉토리(POSIX S_ISDIR 와 같이)인 경우, file_status ( file_type :: directory , prms ) 를 반환합니다.
  • p 가 블록 특수 파일(POSIX S_ISBLK 와 같이)인 경우, file_status ( file_type :: block , prms ) 를 반환합니다.
  • p 가 문자 특수 파일(POSIX S_ISCHR 와 같이)인 경우, file_status ( file_type :: character , prms ) 를 반환합니다.
  • p 가 FIFO 또는 파이프 파일(POSIX S_ISFIFO 와 같이)인 경우, file_status ( file_type :: fifo , prms ) 를 반환합니다.
  • p 가 소켓(POSIX S_ISSOCK 와 같이)인 경우, file_status ( file_type :: socket , prms ) 를 반환합니다.
  • p 가 구현 정의 파일 유형을 가지는 경우, file_status ( file_type :: A , prms ) 를 반환하며, 여기서 A 는 해당 유형에 대한 구현 정의 file_type 상수입니다.
  • p 가 존재하지 않는 경우, file_status ( file_type :: not_found ) 를 반환합니다.
  • p 가 존재하지만 권한 부족 등으로 파일 속성을 확인할 수 없는 경우, file_status ( file_type :: unknown ) 를 반환합니다.
  • 오류로 인해 p 의 존재 여부조차 확인할 수 없는 경우, 비예외 오버로드는 ec 를 설정하고 file_status ( file_type :: none ) 를 반환하며, 예외 오버로드는 filesystem_error 를 발생시킵니다.
  • 그렇지 않은 경우, file_status ( file_type :: unknown , prms ) 를 반환합니다.
3,4) (1,2) 와 동일하지만, POSIX lstat 이 사용된 것처럼 동작합니다(심볼릭 링크를 따르지 않음):
  • 만약 p 가 심볼릭 링크인 경우, file_status ( file_type :: symlink ) 을 반환합니다.

목차

매개변수

p - 검사할 경로
ec - 비예외 발생 오버로드에서 오류 보고를 위한 출력 매개변수

반환값

파일 상태( filesystem::file_status 객체).

예외

noexcept 로 표시되지 않은 모든 오버로드는 메모리 할당이 실패할 경우 std::bad_alloc 을(를) throw할 수 있습니다.

1,3) 기본 OS API 오류 발생 시 std::filesystem::filesystem_error 를 발생시킵니다. 이는 p 를 첫 번째 경로 인수로, OS 오류 코드를 오류 코드 인수로 구성하여 생성됩니다.
2,4) OS API 호출이 실패할 경우 std:: error_code & 매개변수를 OS API 오류 코드로 설정하고, 오류가 발생하지 않을 경우 ec. clear ( ) 를 실행합니다.

참고 사항

이 함수가 제공하는 정보는 일반적으로 디렉터리 순회의 부산물로도 제공되며, filesystem::directory_entry 의 멤버 함수들을 통해 얻을 수 있습니다. 디렉터리 순회 중에는 status 를 다시 호출할 필요가 없습니다.

예제

#include <cstdio>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>
namespace fs = std::filesystem;
void demo_status(const fs::path& p, fs::file_status s)
{
    std::cout << p;
    // alternative: switch(s.type()) { case fs::file_type::regular: ...}
    if (fs::is_regular_file(s))
        std::cout << " is a regular file\n";
    if (fs::is_directory(s))
        std::cout << " is a directory\n";
    if (fs::is_block_file(s))
        std::cout << " is a block device\n";
    if (fs::is_character_file(s))
        std::cout << " is a character device\n";
    if (fs::is_fifo(s))
        std::cout << " is a named IPC pipe\n";
    if (fs::is_socket(s))
        std::cout << " is a named IPC socket\n";
    if (fs::is_symlink(s))
        std::cout << " is a symlink\n";
    if (!fs::exists(s))
        std::cout << " does not exist\n";
}
int main()
{
    // create files of different kinds
    fs::create_directory("sandbox");
    fs::create_directory("sandbox/dir");
    std::ofstream{"sandbox/file"}; // create regular file
    fs::create_symlink("file", "sandbox/symlink");
    mkfifo("sandbox/pipe", 0644);
    sockaddr_un addr;
    addr.sun_family = AF_UNIX;
    std::strcpy(addr.sun_path, "sandbox/sock");
    int fd = socket(PF_UNIX, SOCK_STREAM, 0);
    bind(fd, reinterpret_cast<sockaddr*>(&addr), sizeof addr);
    // demo different status accessors
    for (auto it{fs::directory_iterator("sandbox")}; it != fs::directory_iterator(); ++it)
        demo_status(*it, it->symlink_status()); // use cached status from directory entry
    demo_status("/dev/null", fs::status("/dev/null")); // direct calls to status
    demo_status("/dev/sda", fs::status("/dev/sda"));
    demo_status("sandbox/no", fs::status("/sandbox/no"));
    // cleanup (prefer std::unique_ptr-based custom deleters)
    close(fd);
    fs::remove_all("sandbox");
}

가능한 출력:

"sandbox/file" is a regular file
"sandbox/dir" is a directory
"sandbox/pipe" is a named IPC pipe
"sandbox/sock" is a named IPC socket
"sandbox/symlink" is a symlink
"/dev/null" is a character device
"/dev/sda" is a block device
"sandbox/no" does not exist

참고 항목

파일 유형 및 권한을 나타냄
(클래스)
파일 상태가 알려져 있는지 확인
(함수)
주어진 경로가 블록 장치를 참조하는지 확인
(함수)
주어진 경로가 문자 장치를 참조하는지 확인
(함수)
주어진 경로가 디렉터리를 참조하는지 확인
(함수)
(C++17)
주어진 경로가 명명된 파이프를 참조하는지 확인
(함수)
(C++17)
인수가 기타 파일을 참조하는지 확인
(함수)
인수가 일반 파일을 참조하는지 확인
(함수)
(C++17)
인수가 명명된 IPC 소켓을 참조하는지 확인
(함수)
(C++17)
인수가 심볼릭 링크를 참조하는지 확인
(함수)
(C++17)
경로가 존재하는 파일 시스템 객체를 참조하는지 확인
(함수)
이 디렉터리 엔트리가 지정하는 파일의 상태;
이 디렉터리 엔트리가 지정하는 파일/심볼릭 링크의 상태
( std::filesystem::directory_entry 의 공개 멤버 함수)