mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Add bool IsDirectory(char*) to libpsl-native
This commit is contained in:
@@ -8,6 +8,7 @@ add_library(psl-native SHARED
|
||||
getcomputername.cpp
|
||||
getlinkcount.cpp
|
||||
getfullyqualifiedname.cpp
|
||||
isdirectory.cpp
|
||||
issymlink.cpp
|
||||
isexecutable.cpp
|
||||
setdate.cpp
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
//! @file isdirectory.cpp
|
||||
//! @author Andrew Schwartzmeyer <andschwa@microsoft.com>
|
||||
//! @brief returns if the path is a directory
|
||||
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <pwd.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include "getstat.h"
|
||||
#include "getpwuid.h"
|
||||
#include "getfileowner.h"
|
||||
#include "isdirectory.h"
|
||||
|
||||
//! @brief returns if the path is a directory; uses stat and so follows symlinks
|
||||
//!
|
||||
//! IsDirectory
|
||||
//!
|
||||
//! @param[in] path
|
||||
//! @parblock
|
||||
//! A pointer to the buffer that contains the file name
|
||||
//!
|
||||
//! char* is marshaled as an LPStr, which on Linux is UTF-8.
|
||||
//! @endparblock
|
||||
//!
|
||||
//! @exception errno Passes this error via errno to GetLastError:
|
||||
//! - ERROR_INVALID_PARAMETER: parameter is not valid
|
||||
//!
|
||||
//! @retval true if directory, false otherwise
|
||||
//!
|
||||
bool IsDirectory(const char* path)
|
||||
{
|
||||
int32_t ret = 0;
|
||||
errno = 0;
|
||||
|
||||
if (!path)
|
||||
{
|
||||
errno = ERROR_INVALID_PARAMETER;
|
||||
return false;
|
||||
}
|
||||
|
||||
struct stat buf;
|
||||
ret = GetStat(path, &buf);
|
||||
if (ret != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return S_ISDIR(buf.st_mode);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "pal.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
PAL_BEGIN_EXTERNC
|
||||
|
||||
bool IsDirectory(const char* path);
|
||||
|
||||
PAL_END_EXTERNC
|
||||
@@ -9,6 +9,7 @@ add_executable(psl-native-test
|
||||
test-getcomputername.cpp
|
||||
test-getlinkcount.cpp
|
||||
test-getfullyqualifiedname.cpp
|
||||
test-isdirectory.cpp
|
||||
test-issymlink.cpp
|
||||
test-isexecutable.cpp
|
||||
test-createsymlink.cpp
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
//! @file test-isdirectory.cpp
|
||||
//! @author Andrew Schwartzmeyer <andschwa@microsoft.com>
|
||||
//! @brief Tests IsDirectory
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include "isdirectory.h"
|
||||
|
||||
TEST(IsDirectoryTest, RootIsDirectory)
|
||||
{
|
||||
EXPECT_TRUE(IsDirectory("/"));
|
||||
}
|
||||
|
||||
TEST(IsDirectoryTest, BinLsIsNotDirectory)
|
||||
{
|
||||
EXPECT_FALSE(IsDirectory("/bin/ls"));
|
||||
}
|
||||
|
||||
|
||||
TEST(IsDirectoryTest, ReturnsFalseForFakeDirectory)
|
||||
{
|
||||
EXPECT_FALSE(IsDirectory("SomeMadeUpFileNameThatDoesNotExist"));
|
||||
EXPECT_EQ(errno, ERROR_FILE_NOT_FOUND);
|
||||
}
|
||||
|
||||
TEST(IsDirectoryTest, ReturnsFalseForNullInput)
|
||||
{
|
||||
EXPECT_FALSE(IsDirectory(NULL));
|
||||
EXPECT_EQ(errno, ERROR_INVALID_PARAMETER);
|
||||
}
|
||||
Reference in New Issue
Block a user