system/FileDescriptor: add IsPipe(), IsSocket()

This commit is contained in:
Max Kellermann 2018-08-20 16:05:03 +02:00
parent 11396d4fba
commit b5c569cd30
3 changed files with 27 additions and 0 deletions

View File

@ -80,6 +80,7 @@ public:
using FileDescriptor::IsDefined;
#ifndef _WIN32
using FileDescriptor::IsValid;
using FileDescriptor::IsSocket;
#endif
using FileDescriptor::Get;
using FileDescriptor::Set;

View File

@ -65,6 +65,20 @@ FileDescriptor::IsValid() const noexcept
return IsDefined() && fcntl(fd, F_GETFL) >= 0;
}
bool
FileDescriptor::IsPipe() const noexcept
{
struct stat st;
return IsDefined() && fstat(fd, &st) == 0 && S_ISFIFO(st.st_mode);
}
bool
FileDescriptor::IsSocket() const noexcept
{
struct stat st;
return IsDefined() && fstat(fd, &st) == 0 && S_ISSOCK(st.st_mode);
}
#endif
bool

View File

@ -75,6 +75,18 @@ public:
*/
gcc_pure
bool IsValid() const noexcept;
/**
* Ask the kernel whether this is a pipe.
*/
gcc_pure
bool IsPipe() const noexcept;
/**
* Ask the kernel whether this is a socket descriptor.
*/
gcc_pure
bool IsSocket() const noexcept;
#endif
/**