std::experimental::filesystem:: status, std::experimental::filesystem:: symlink_status
From cppreference.net
<
cpp
|
experimental
|
fs
|
헤더 파일에 정의됨
<experimental/filesystem>
|
||
|
file_status status
(
const
path
&
p
)
;
file_status status ( const path & p, error_code & ec ) noexcept ; |
(1) | (filesystem TS) |
|
file_status symlink_status
(
const
path
&
p
)
;
file_status symlink_status ( const path & p, error_code & ec ) noexcept ; |
(2) | (filesystem TS) |
1)
p
로 식별되는 파일 시스템 객체의 유형과 속성을 POSIX
stat
과 같이 결정합니다(심볼릭 링크는 대상까지 추적됨).
-
- p 가 일반 파일인 경우 file_status ( file_type :: regular ) 를 반환합니다.
- p 가 디렉터리인 경우 file_status ( file_type :: directory ) 를 반환합니다.
- p 가 블록 특수 파일인 경우 file_status ( file_type :: block ) 를 반환합니다.
- p 가 문자 특수 파일인 경우 file_status ( file_type :: character ) 를 반환합니다.
- p 가 FIFO 또는 파이프 파일인 경우 file_status ( file_type :: fifo ) 를 반환합니다.
- p 가 소켓인 경우 file_status ( file_type :: socket ) 를 반환합니다.
- 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 ) 를 반환합니다.
2)
(1)
과 동일하지만, POSIX
lstat
이 사용된 것처럼 동작합니다(심볼릭 링크를 따르지 않음):
-
- 만약 p 가 심볼릭 링크인 경우, file_status ( file_type :: symlink ) 를 반환합니다.
목차 |
매개변수
| p | - | 검사할 경로 |
| ec | - | 비예외 발생 오버로드에서 오류 보고를 위한 출력 매개변수 |
반환값
파일 상태( file_status 객체).
예외
The overload that does not take an error_code & parameter throws filesystem_error on underlying OS API errors, constructed with p as the first argument and the OS error code as the error code argument. std:: bad_alloc may be thrown if memory allocation fails. The overload taking an error_code & parameter sets it to the OS API error code if an OS API call fails, and executes ec. clear ( ) if no errors occur. This overload has
noexcept
명세:
noexcept
참고 사항
이 함수가 제공하는 정보는 일반적으로 디렉토리 순회의 부산물로도 제공되며,
directory_entry
의 멤버 함수들을 통해 얻을 수 있습니다. 디렉토리 순회 중에는
status
를 다시 호출하는 것은 불필요합니다.
예제
이 코드 실행
#include <cstdio> #include <cstring> #include <experimental/filesystem> #include <fstream> #include <iostream> #include <sys/socket.h> #include <sys/stat.h> #include <sys/un.h> #include <unistd.h> namespace fs = std::experimental::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"); std::ofstream("sandbox/file"); // create regular file fs::create_directory("sandbox/dir"); mkfifo("sandbox/pipe", 0644); struct 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, (struct sockaddr*)&addr, sizeof addr); fs::create_symlink("file", "sandbox/symlink"); // 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 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
참고 항목
|
파일 유형과 권한을 나타냄
(클래스) |
|
|
파일 상태가 알려져 있는지 확인
(함수) |
|
|
주어진 경로가 블록 장치를 참조하는지 확인
(함수) |
|
|
주어진 경로가 문자 장치를 참조하는지 확인
(함수) |
|
|
주어진 경로가 디렉터리를 참조하는지 확인
(함수) |
|
|
주어진 경로가 명명된 파이프를 참조하는지 확인
(함수) |
|
|
인수가
기타
파일을 참조하는지 확인
(함수) |
|
|
인수가 일반 파일을 참조하는지 확인
(함수) |
|
|
인수가 명명된 IPC 소켓을 참조하는지 확인
(함수) |
|
|
인수가 심볼릭 링크를 참조하는지 확인
(함수) |
|
|
경로가 존재하는 파일 시스템 객체를 참조하는지 확인
(함수) |
|
|
이 디렉터리 엔트리가 지정하는 파일의 캐시된 상태
이 디렉터리 엔트리가 지정하는 파일의 캐시된 심볼릭 링크 상태 (
std::experimental::filesystem::directory_entry
의 public 멤버 함수)
|