Add bool IsDirectory(char*) to libpsl-native

This commit is contained in:
Andrew Schwartzmeyer
2016-07-14 17:34:12 -07:00
parent 3276846c40
commit 955d5f8bcb
5 changed files with 94 additions and 0 deletions
+1
View File
@@ -8,6 +8,7 @@ add_library(psl-native SHARED
getcomputername.cpp
getlinkcount.cpp
getfullyqualifiedname.cpp
isdirectory.cpp
issymlink.cpp
isexecutable.cpp
setdate.cpp
+51
View File
@@ -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);
}
+10
View File
@@ -0,0 +1,10 @@
#pragma once
#include "pal.h"
#include <stdbool.h>
PAL_BEGIN_EXTERNC
bool IsDirectory(const char* path);
PAL_END_EXTERNC
+1
View File
@@ -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);
}