Add bool IsFile(char*) to libpsl-native

This commit is contained in:
Andrew Schwartzmeyer 2016-07-13 20:50:00 -07:00
parent 3dacf45789
commit 90a5382b30
5 changed files with 82 additions and 0 deletions

View file

@ -8,6 +8,7 @@ add_library(psl-native SHARED
getcomputername.cpp
getlinkcount.cpp
getfullyqualifiedname.cpp
isfile.cpp
isdirectory.cpp
issymlink.cpp
isexecutable.cpp

View file

@ -0,0 +1,40 @@
//! @file isfile.cpp
//! @author Andrew Schwartzmeyer <andschwa@microsoft.com>
//! @brief returns if the path exists
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <string.h>
#include <unistd.h>
#include "isfile.h"
//! @brief returns if the path is a file or directory
//!
//! IsFile
//!
//! @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 path exists, false otherwise
//!
bool IsFile(const char* path)
{
errno = 0;
if (!path)
{
errno = ERROR_INVALID_PARAMETER;
return false;
}
return access(path, F_OK) != -1;
}

View file

@ -0,0 +1,10 @@
#pragma once
#include "pal.h"
#include <stdbool.h>
PAL_BEGIN_EXTERNC
bool IsFile(const char* path);
PAL_END_EXTERNC

View file

@ -10,6 +10,7 @@ add_executable(psl-native-test
test-getlinkcount.cpp
test-getfullyqualifiedname.cpp
test-isdirectory.cpp
test-isfile.cpp
test-issymlink.cpp
test-isexecutable.cpp
test-createsymlink.cpp

View file

@ -0,0 +1,30 @@
//! @file test-isfile.cpp
//! @author Andrew Schwartzmeyer <andschwa@microsoft.com>
//! @brief Tests Isfile
#include <gtest/gtest.h>
#include <errno.h>
#include <unistd.h>
#include "isfile.h"
TEST(IsFileTest, RootIsFile)
{
EXPECT_TRUE(IsFile("/"));
}
TEST(IsFileTest, BinLsIsFile)
{
EXPECT_TRUE(IsFile("/bin/ls"));
}
TEST(IsFileTest, CannotGetOwnerOfFakeFile)
{
EXPECT_FALSE(IsFile("SomeMadeUpFileNameThatDoesNotExist"));
EXPECT_EQ(errno, ERROR_FILE_NOT_FOUND);
}
TEST(IsFileTest, ReturnsFalseForNullInput)
{
EXPECT_FALSE(IsFile(NULL));
EXPECT_EQ(errno, ERROR_INVALID_PARAMETER);
}