Namespaces
Variants

std::filesystem::directory_entry:: is_symlink

From cppreference.net
bool is_symlink ( ) const ;
(1) (C++17 이후)
bool is_symlink ( std:: error_code & ec ) const noexcept ;
(2) (C++17 이후)

가리키는 객체가 심링크인지 확인합니다. 효과적으로 다음을 반환합니다:

1) std:: filesystem :: is_symlink ( symlink_status ( ) ) ,
2) std:: filesystem :: is_symlink ( symlink_status ( ec ) ) .

목차

매개변수

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

반환값

true 참조된 파일 시스템 객체가 심볼릭 링크인 경우, false 그렇지 않은 경우.

예외

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

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

예제

#include <cstdio>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <functional>
#include <iostream>
#include <memory>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>
namespace fs = std::filesystem;
void print_entry_type(const std::filesystem::directory_entry
**변경 없음** - C++ 특정 용어와 HTML 태그는 번역하지 않음& entry)
{
    std::cout << entry.path() << ": ";
    if (!entry.exists())
        std::cout << "존재하지 않습니다";
    if (entry.is_block_file())
        std::cout << "는 블록 장치입니다 ";
    if (entry.is_character_file())
        std::cout << "는 문자 장치입니다 ";
    if (entry.is_directory())
        std::cout << "is a directory ";
    if (entry.is_fifo())
        std::cout << "는 명명된 IPC 파이프입니다 ";
    if (entry.is_regular_file())
        std::cout << "는 일반 파일입니다 ";
    if (entry.is_socket())
        std::cout << "는 명명된 IPC 소켓입니다 ";
    if (entry.is_symlink())
        std::cout << "(심볼릭 링크)";
    if (entry.is_other())
        std::cout << "(다른 `other` 파일)";
    std::cout << '\n';
}
template<typename Type, typename Fun>
class scoped_cleanup
{
    std::unique_ptr<Type, std::function<void(const Type*)>> u;
public:
    scoped_cleanup(Type* ptr, Fun fun) : u{ptr, std::move(fun)} {}
};
int main()
{
    // 다양한 종류의 파일을 생성합니다.
    std::filesystem::current_path(fs::temp_directory_path());
    const std::filesystem::path sandbox{"샌드박스"};
    scoped_cleanup remove_all_at_exit{&sandbox, [](const fs::path* p)
    {
        std::cout << "cleanup: remove_all(" << *p << ")\n";
        fs::remove_all(*p);
    }};
    std::filesystem::create_directory(sandbox);
    std::ofstream{sandbox/"파일"}; // 일반 파일을 생성합니다
    std::filesystem::create_directory(sandbox/"dir");
    mkfifo((sandbox/"파이프").string().data(), 0644);
    struct sockaddr_un addr; addr.sun_family = AF_UNIX;
    std::strcpy(addr.sun_path, (sandbox/"소켓").string().data());
    int fd{socket(PF_UNIX, SOCK_STREAM, 0)};
    scoped_cleanup close_socket_at_exit{&fd, [](const int* f)
    {
        std::cout << "cleanup: close socket #" << *f << '\n';
        close(*f);
    }};
    bind(fd, reinterpret_cast<sockaddr*>(std::addressof(addr)), sizeof addr);
    fs::create_symlink("파일", sandbox/"심링크");
    for (std::filesystem::directory_entry entry: fs::directory_iterator(sandbox))
        print_entry_type(entry);
    // 파일 시스템 객체 상태를 직접 요청:
    for (const char* str : {"/dev/null", "/dev/cpu", "/usr/include/c++",
                            "/usr/include/asm", "/usr/include/time.h"})
        print_entry_type(fs::directory_entry{str});
} // scoped_cleanup 객체를 통한 정리

가능한 출력:

"sandbox/symlink": 일반 파일입니다 (심볼릭 링크)
"sandbox/sock": 명명된 IPC 소켓입니다 (`other` 파일 유형)
"sandbox/pipe": 명명된 IPC 파이프입니다 (`other` 파일 유형)
"sandbox/dir": 디렉토리입니다
"sandbox/file": 일반 파일입니다
"/dev/null": 문자 장치입니다 (`other` 파일 유형)
"/dev/cpu": 존재하지 않습니다
"/usr/include/c++": 디렉토리입니다
"/usr/include/asm": 디렉토리입니다 (심볼릭 링크)
"/usr/include/time.h": 일반 파일입니다
cleanup: 소켓 #3 닫기
cleanup: remove_all("sandbox")

참고 항목

(C++17)
인수가 심볼릭 링크를 참조하는지 여부를 확인합니다
(함수)