From ee1d059ae1ea730504cc790207e5c0c62cb03277 Mon Sep 17 00:00:00 2001 From: James Truher Date: Fri, 16 Aug 2019 11:51:33 -0700 Subject: [PATCH 01/18] start GetCommonStat The goal is to create a stat call which can be called from any non-Windows platform. CommonStat is a stat like structure which has common elements from stat on all systems and uses the largest type for structure members to allow values from any platform --- src/libpsl-native/src/CMakeLists.txt | 1 + src/libpsl-native/src/getcommonstat.cpp | 37 +++++++++++++++++++ src/libpsl-native/src/getcommonstat.h | 30 +++++++++++++++ src/libpsl-native/test/test-getcommonstat.cpp | 27 ++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 src/libpsl-native/src/getcommonstat.cpp create mode 100644 src/libpsl-native/src/getcommonstat.h create mode 100644 src/libpsl-native/test/test-getcommonstat.cpp diff --git a/src/libpsl-native/src/CMakeLists.txt b/src/libpsl-native/src/CMakeLists.txt index 889ba7b..3cf316c 100644 --- a/src/libpsl-native/src/CMakeLists.txt +++ b/src/libpsl-native/src/CMakeLists.txt @@ -2,6 +2,7 @@ include(CheckIncludeFiles) add_library(psl-native SHARED getstat.cpp + getcommonstat.cpp getpwuid.cpp getppid.cpp getuserfrompid.cpp diff --git a/src/libpsl-native/src/getcommonstat.cpp b/src/libpsl-native/src/getcommonstat.cpp new file mode 100644 index 0000000..3e8c3d5 --- /dev/null +++ b/src/libpsl-native/src/getcommonstat.cpp @@ -0,0 +1,37 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//! @brief returns the stat of a file + +#include "getcommonstat.h" + +#include +#include +#include +#include +#include +#include +#include + +// Provide a common structure for the various different stat structures +int GetCommonStat(const char* path, struct CommonStat* commonStat) +{ + struct stat st; + struct CommonStat cs; + if (GetStat(path, &st) == 0) + { + cs.Inode = st.st_ino; + cs.IsDirectory = S_ISDIR(st.st_mode); + cs.IsFile = S_ISREG(st.st_mode); + cs.IsBlockDevice = S_ISBLK(st.st_mode); + cs.IsCharacterDevice = S_ISCHR(st.st_mode); + cs.IsNamedPipe = S_ISFIFO(st.st_mode); + cs.IsSocket = S_ISSOCK(st.st_mode); + cs.Mode = st.st_mode; + cs.UserId = st.st_uid; + cs.GroupId = st.st_gid; + commonStat = &cs; + return 0; + } + return -1; +} diff --git a/src/libpsl-native/src/getcommonstat.h b/src/libpsl-native/src/getcommonstat.h new file mode 100644 index 0000000..8af6112 --- /dev/null +++ b/src/libpsl-native/src/getcommonstat.h @@ -0,0 +1,30 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#pragma once + +#include "pal.h" + +#include + +PAL_BEGIN_EXTERNC + +struct CommonStat +{ + long Inode; + int Mode; + int UserId; + int GroupId; + int IsDirectory; + int IsFile; + int IsSymbolicLink; + int IsBlockDevice; + int IsCharacterDevice; + int IsNamedPipe; + int IsSocket; +}; + +int32_t GetStat(const char* path, struct stat* buf); +int GetCommonStat(const char* path, CommonStat* cs); + +PAL_END_EXTERNC diff --git a/src/libpsl-native/test/test-getcommonstat.cpp b/src/libpsl-native/test/test-getcommonstat.cpp new file mode 100644 index 0000000..d4cca16 --- /dev/null +++ b/src/libpsl-native/test/test-getcommonstat.cpp @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//! @brief Tests IsDirectory + +#include +#include +#include +#include "isdirectory.h" +#include "getstatcommon.h" + +TEST(IsDirectoryTest, RootIsDirectory) +{ + EXPECT_TRUE(IsDirectory("/")); +} + +TEST(IsDirectoryTest, BinLsIsNotDirectory) +{ + EXPECT_FALSE(IsDirectory("/bin/ls")); +} + + +TEST(IsDirectoryTest, ReturnsFalseForFakeDirectory) +{ + EXPECT_FALSE(IsDirectory("SomeMadeUpFileNameThatDoesNotExist")); + EXPECT_EQ(ENOENT, errno); +} From 123e32f8d0dbee32844e3c9a9ec7a267794f7366 Mon Sep 17 00:00:00 2001 From: James Truher Date: Fri, 23 Aug 2019 15:10:30 -0700 Subject: [PATCH 02/18] common stat working now Added lstat fstat probably not needed. --- src/libpsl-native/src/getcommonstat.cpp | 74 +++++-- src/libpsl-native/src/getcommonstat.h | 9 + src/libpsl-native/src/getstat.cpp | 13 ++ src/libpsl-native/src/getstat.h | 1 + src/libpsl-native/src/isdirectory.cpp | 2 + src/libpsl-native/test/CMakeLists.txt | 1 + src/libpsl-native/test/test-getcommonstat.cpp | 201 +++++++++++++++++- 7 files changed, 279 insertions(+), 22 deletions(-) diff --git a/src/libpsl-native/src/getcommonstat.cpp b/src/libpsl-native/src/getcommonstat.cpp index 3e8c3d5..12e3c5c 100644 --- a/src/libpsl-native/src/getcommonstat.cpp +++ b/src/libpsl-native/src/getcommonstat.cpp @@ -13,25 +13,71 @@ #include #include -// Provide a common structure for the various different stat structures +#include + +// Provide a common structure for the various different stat structures. +// This should be safe to call on all platforms +int GetCommonLStat(const char* path, struct CommonStat* commonStat) +{ + struct stat st; + assert(path); + errno = 0; + if (lstat(path, &st) == 0) + { + commonStat->Inode = st.st_ino; + commonStat->Mode = st.st_mode; + commonStat->UserId = st.st_uid; + commonStat->GroupId = st.st_gid; + commonStat->HardlinkCount = st.st_nlink; + commonStat->Size = st.st_size; + commonStat->AccessTime = st.st_atimespec.tv_sec; + commonStat->ModifiedTime = st.st_mtimespec.tv_sec; + commonStat->CreationTime = st.st_ctimespec.tv_sec; + commonStat->BlockSize = st.st_blksize; + commonStat->DeviceId = st.st_dev; + commonStat->NumberOfBlocks = st.st_blocks; + commonStat->IsBlockDevice = S_ISBLK(st.st_mode); + commonStat->IsCharacterDevice = S_ISCHR(st.st_mode); + commonStat->IsDirectory = S_ISDIR(st.st_mode); + commonStat->IsFile = S_ISREG(st.st_mode); + commonStat->IsNamedPipe = S_ISFIFO(st.st_mode); + commonStat->IsSocket = S_ISSOCK(st.st_mode); + commonStat->IsSymbolicLink = S_ISLNK(st.st_mode); + return 0; + } + return -1; +} + +// Provide a common structure for the various different stat structures. +// This should be safe to call on all platforms int GetCommonStat(const char* path, struct CommonStat* commonStat) { struct stat st; - struct CommonStat cs; - if (GetStat(path, &st) == 0) + assert(path); + errno = 0; + if (stat(path, &st) == 0) { - cs.Inode = st.st_ino; - cs.IsDirectory = S_ISDIR(st.st_mode); - cs.IsFile = S_ISREG(st.st_mode); - cs.IsBlockDevice = S_ISBLK(st.st_mode); - cs.IsCharacterDevice = S_ISCHR(st.st_mode); - cs.IsNamedPipe = S_ISFIFO(st.st_mode); - cs.IsSocket = S_ISSOCK(st.st_mode); - cs.Mode = st.st_mode; - cs.UserId = st.st_uid; - cs.GroupId = st.st_gid; - commonStat = &cs; + commonStat->Inode = st.st_ino; + commonStat->Mode = st.st_mode; + commonStat->UserId = st.st_uid; + commonStat->GroupId = st.st_gid; + commonStat->HardlinkCount = st.st_nlink; + commonStat->Size = st.st_size; + commonStat->AccessTime = st.st_atimespec.tv_sec; + commonStat->ModifiedTime = st.st_mtimespec.tv_sec; + commonStat->CreationTime = st.st_ctimespec.tv_sec; + commonStat->BlockSize = st.st_blksize; + commonStat->DeviceId = st.st_dev; + commonStat->NumberOfBlocks = st.st_blocks; + commonStat->IsBlockDevice = S_ISBLK(st.st_mode); + commonStat->IsCharacterDevice = S_ISCHR(st.st_mode); + commonStat->IsDirectory = S_ISDIR(st.st_mode); + commonStat->IsFile = S_ISREG(st.st_mode); + commonStat->IsNamedPipe = S_ISFIFO(st.st_mode); + commonStat->IsSocket = S_ISSOCK(st.st_mode); + commonStat->IsSymbolicLink = S_ISLNK(st.st_mode); return 0; } return -1; } + diff --git a/src/libpsl-native/src/getcommonstat.h b/src/libpsl-native/src/getcommonstat.h index 8af6112..0c1a206 100644 --- a/src/libpsl-native/src/getcommonstat.h +++ b/src/libpsl-native/src/getcommonstat.h @@ -15,6 +15,14 @@ struct CommonStat int Mode; int UserId; int GroupId; + int HardlinkCount; + long Size; + long AccessTime; + long ModifiedTime; + long CreationTime; + long BlockSize; + int DeviceId; + int NumberOfBlocks; int IsDirectory; int IsFile; int IsSymbolicLink; @@ -26,5 +34,6 @@ struct CommonStat int32_t GetStat(const char* path, struct stat* buf); int GetCommonStat(const char* path, CommonStat* cs); +int GetCommonLStat(const char* path, CommonStat* cs); PAL_END_EXTERNC diff --git a/src/libpsl-native/src/getstat.cpp b/src/libpsl-native/src/getstat.cpp index 2cc6f66..68affd0 100644 --- a/src/libpsl-native/src/getstat.cpp +++ b/src/libpsl-native/src/getstat.cpp @@ -45,3 +45,16 @@ int32_t GetStat(const char* path, struct stat* buf) return stat(path, buf); } + + +// DO NOT use in managed code +// use externally defined structs in managed code has proven to be buggy +// (memory corruption issues due to layout difference between platforms) +// see https://github.com/dotnet/corefx/issues/29700#issuecomment-389313075 +int32_t GetLStat(const char* path, struct stat* buf) +{ + assert(path); + errno = 0; + + return lstat(path, buf); +} diff --git a/src/libpsl-native/src/getstat.h b/src/libpsl-native/src/getstat.h index 12270e0..2489c1c 100644 --- a/src/libpsl-native/src/getstat.h +++ b/src/libpsl-native/src/getstat.h @@ -10,5 +10,6 @@ PAL_BEGIN_EXTERNC int32_t GetStat(const char* path, struct stat* buf); +int32_t GetLStat(const char* path, struct stat* buf); PAL_END_EXTERNC diff --git a/src/libpsl-native/src/isdirectory.cpp b/src/libpsl-native/src/isdirectory.cpp index 1123dc5..429d9a7 100644 --- a/src/libpsl-native/src/isdirectory.cpp +++ b/src/libpsl-native/src/isdirectory.cpp @@ -15,6 +15,8 @@ #include #include +#include + //! @brief returns if the path is a directory; uses stat and so follows symlinks //! //! IsDirectory diff --git a/src/libpsl-native/test/CMakeLists.txt b/src/libpsl-native/test/CMakeLists.txt index eb496e0..5f9ba2e 100644 --- a/src/libpsl-native/test/CMakeLists.txt +++ b/src/libpsl-native/test/CMakeLists.txt @@ -6,6 +6,7 @@ add_executable(psl-native-test test-getuserfrompid.cpp test-getcurrentprocessid.cpp test-getcomputername.cpp + test-getcommonstat.cpp test-getlinkcount.cpp test-isdirectory.cpp test-isfile.cpp diff --git a/src/libpsl-native/test/test-getcommonstat.cpp b/src/libpsl-native/test/test-getcommonstat.cpp index d4cca16..4b78846 100644 --- a/src/libpsl-native/test/test-getcommonstat.cpp +++ b/src/libpsl-native/test/test-getcommonstat.cpp @@ -7,21 +7,206 @@ #include #include #include "isdirectory.h" -#include "getstatcommon.h" +#include "getcommonstat.h" -TEST(IsDirectoryTest, RootIsDirectory) +TEST(GetCommonStat, RootIsDirectory) { - EXPECT_TRUE(IsDirectory("/")); + CommonStat cs; + GetCommonStat("/", &cs); + bool isDir = IsDirectory("/"); + bool fromCommonStat = (bool)cs.IsDirectory; + EXPECT_EQ(isDir, fromCommonStat); } -TEST(IsDirectoryTest, BinLsIsNotDirectory) +TEST(GetCommonStat, BinLsIsNotDirectory) { - EXPECT_FALSE(IsDirectory("/bin/ls")); + CommonStat cs; + GetCommonStat("/bin/ls", &cs); + bool isDir = IsDirectory("/bin/ls"); + bool fromCommonStat = (bool)cs.IsDirectory; + EXPECT_EQ(isDir, fromCommonStat); } -TEST(IsDirectoryTest, ReturnsFalseForFakeDirectory) +TEST(GetCommonStat, ReturnsFalseForFakeDirectory) { - EXPECT_FALSE(IsDirectory("SomeMadeUpFileNameThatDoesNotExist")); - EXPECT_EQ(ENOENT, errno); + CommonStat cs; + int badDir = GetCommonStat("/A/Really/Bad/Directory",&cs); + EXPECT_EQ(badDir, -1); } + +TEST(GetCommonStat, GetOwnerIdOfRoot) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %u /", "r"); +#else + p = popen("/usr/bin/stat -c %u /", "r"); +#endif + int uid = -1; + fscanf(p, "%d", &uid); + pclose(p); + GetCommonStat("/", &cs); + EXPECT_EQ(uid, cs.UserId); +} + +TEST(GetCommonStat, GetGroupId) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %g /", "r"); +#else + p = popen("/usr/bin/stat -c %g /", "r"); +#endif + int gid = -1; + fscanf(p, "%d", &gid); + pclose(p); + GetCommonStat("/", &cs); + EXPECT_EQ(gid, cs.UserId); +} + +TEST(GetCommonStat, GetInodeNumber) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %i /", "r"); +#else + p = popen("/usr/bin/stat -c %i /", "r"); +#endif + long inode = -1; + fscanf(p, "%ld", &inode); + pclose(p); + GetCommonStat("/", &cs); + EXPECT_EQ(inode, cs.Inode); +} + +TEST(GetCommonStat, GetSize) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %z /", "r"); +#else + p = popen("/usr/bin/stat -c %z /", "r"); +#endif + long size = -1; + fscanf(p, "%ld", &size); + pclose(p); + GetCommonStat("/", &cs); + EXPECT_EQ(size, cs.Size); +} + +TEST(GetCommonStat, GetBlockSize) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %k /", "r"); +#else + p = popen("/usr/bin/stat -c %k /", "r"); +#endif + long bSize = -1; + fscanf(p, "%ld", &bSize); + pclose(p); + GetCommonStat("/", &cs); + EXPECT_EQ(bSize, cs.BlockSize); +} + +TEST(GetCommonStat, GetBlockCount) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %b /", "r"); +#else + p = popen("/usr/bin/stat -c %b /", "r"); +#endif + int bSize = -1; + fscanf(p, "%d", &bSize); + pclose(p); + GetCommonStat("/", &cs); + EXPECT_EQ(bSize, cs.NumberOfBlocks); +} + +TEST(GetCommonStat, GetLinkCount) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %l /", "r"); +#else + p = popen("/usr/bin/stat -c %l /", "r"); +#endif + int linkcount = -1; + fscanf(p, "%d", &linkcount); + pclose(p); + GetCommonStat("/", &cs); + EXPECT_EQ(linkcount, cs.HardlinkCount); +} + +TEST(GetCommonStat, GetDeviceId) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %d /", "r"); +#else + p = popen("/usr/bin/stat -c %d /", "r"); +#endif + int deviceId = -1; + fscanf(p, "%d", &deviceId); + pclose(p); + GetCommonStat("/", &cs); + EXPECT_EQ(deviceId, cs.DeviceId); +} + +TEST(GetCommonStat, GetATime) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %a /", "r"); +#else + p = popen("/usr/bin/stat -c %a /", "r"); +#endif + long aTime = -1; + fscanf(p, "%ld", &aTime); + pclose(p); + GetCommonStat("/", &cs); + EXPECT_EQ(aTime, cs.AccessTime); +} + +TEST(GetCommonStat, GetMTime) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %m /", "r"); +#else + p = popen("/usr/bin/stat -c %m /", "r"); +#endif + long mTime = -1; + fscanf(p, "%ld", &mTime); + pclose(p); + GetCommonStat("/", &cs); + EXPECT_EQ(mTime, cs.ModifiedTime); +} + +TEST(GetCommonStat, GetCTime) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %c /", "r"); +#else + p = popen("/usr/bin/stat -c %c /", "r"); +#endif + long cTime = -1; + fscanf(p, "%ld", &cTime); + pclose(p); + GetCommonStat("/", &cs); + EXPECT_EQ(cTime, cs.CreationTime); +} \ No newline at end of file From b7a400497f4cd3223e381c26d091c1ab1528a22f Mon Sep 17 00:00:00 2001 From: James Truher Date: Sat, 24 Aug 2019 00:41:54 -0700 Subject: [PATCH 03/18] fix incorrect stat arguments in tests Fix incorrect time references --- src/libpsl-native/src/getcommonstat.cpp | 16 ++++++- src/libpsl-native/test/test-getcommonstat.cpp | 47 ++++++++++++------- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/libpsl-native/src/getcommonstat.cpp b/src/libpsl-native/src/getcommonstat.cpp index 12e3c5c..63a2363 100644 --- a/src/libpsl-native/src/getcommonstat.cpp +++ b/src/libpsl-native/src/getcommonstat.cpp @@ -30,9 +30,15 @@ int GetCommonLStat(const char* path, struct CommonStat* commonStat) commonStat->GroupId = st.st_gid; commonStat->HardlinkCount = st.st_nlink; commonStat->Size = st.st_size; - commonStat->AccessTime = st.st_atimespec.tv_sec; +#if defined (__APPLE__) + commonStat->AccessTime = st.st_atimespec.tv_sec; commonStat->ModifiedTime = st.st_mtimespec.tv_sec; commonStat->CreationTime = st.st_ctimespec.tv_sec; +#else + commonStat->AccessTime = st.st_atime; + commonStat->ModifiedTime = st.st_mtime; + commonStat->CreationTime = st.st_ctime; +#endif commonStat->BlockSize = st.st_blksize; commonStat->DeviceId = st.st_dev; commonStat->NumberOfBlocks = st.st_blocks; @@ -63,9 +69,15 @@ int GetCommonStat(const char* path, struct CommonStat* commonStat) commonStat->GroupId = st.st_gid; commonStat->HardlinkCount = st.st_nlink; commonStat->Size = st.st_size; - commonStat->AccessTime = st.st_atimespec.tv_sec; +#if defined (__APPLE__) + commonStat->AccessTime = st.st_atimespec.tv_sec; commonStat->ModifiedTime = st.st_mtimespec.tv_sec; commonStat->CreationTime = st.st_ctimespec.tv_sec; +#else + commonStat->AccessTime = st.st_atime; + commonStat->ModifiedTime = st.st_mtime; + commonStat->CreationTime = st.st_ctime; +#endif commonStat->BlockSize = st.st_blksize; commonStat->DeviceId = st.st_dev; commonStat->NumberOfBlocks = st.st_blocks; diff --git a/src/libpsl-native/test/test-getcommonstat.cpp b/src/libpsl-native/test/test-getcommonstat.cpp index 4b78846..353c148 100644 --- a/src/libpsl-native/test/test-getcommonstat.cpp +++ b/src/libpsl-native/test/test-getcommonstat.cpp @@ -45,9 +45,10 @@ TEST(GetCommonStat, GetOwnerIdOfRoot) p = popen("/usr/bin/stat -c %u /", "r"); #endif int uid = -1; - fscanf(p, "%d", &uid); + int result = fscanf(p, "%d", &uid); pclose(p); GetCommonStat("/", &cs); + EXPECT_EQ(result, 1); EXPECT_EQ(uid, cs.UserId); } @@ -61,9 +62,10 @@ TEST(GetCommonStat, GetGroupId) p = popen("/usr/bin/stat -c %g /", "r"); #endif int gid = -1; - fscanf(p, "%d", &gid); + int result = fscanf(p, "%d", &gid); pclose(p); GetCommonStat("/", &cs); + EXPECT_EQ(result, 1); EXPECT_EQ(gid, cs.UserId); } @@ -77,9 +79,10 @@ TEST(GetCommonStat, GetInodeNumber) p = popen("/usr/bin/stat -c %i /", "r"); #endif long inode = -1; - fscanf(p, "%ld", &inode); + int result = fscanf(p, "%ld", &inode); pclose(p); GetCommonStat("/", &cs); + EXPECT_EQ(result, 1); EXPECT_EQ(inode, cs.Inode); } @@ -90,12 +93,13 @@ TEST(GetCommonStat, GetSize) #if defined (__APPLE__) p = popen("/usr/bin/stat -f %z /", "r"); #else - p = popen("/usr/bin/stat -c %z /", "r"); + p = popen("/usr/bin/stat -c %s /", "r"); #endif long size = -1; - fscanf(p, "%ld", &size); + int result = fscanf(p, "%ld", &size); pclose(p); GetCommonStat("/", &cs); + EXPECT_EQ(result, 1); EXPECT_EQ(size, cs.Size); } @@ -106,12 +110,13 @@ TEST(GetCommonStat, GetBlockSize) #if defined (__APPLE__) p = popen("/usr/bin/stat -f %k /", "r"); #else - p = popen("/usr/bin/stat -c %k /", "r"); + p = popen("/usr/bin/stat -c %o /", "r"); #endif long bSize = -1; - fscanf(p, "%ld", &bSize); + int result = fscanf(p, "%ld", &bSize); pclose(p); GetCommonStat("/", &cs); + EXPECT_EQ(result, 1); EXPECT_EQ(bSize, cs.BlockSize); } @@ -125,9 +130,10 @@ TEST(GetCommonStat, GetBlockCount) p = popen("/usr/bin/stat -c %b /", "r"); #endif int bSize = -1; - fscanf(p, "%d", &bSize); + int result = fscanf(p, "%d", &bSize); pclose(p); GetCommonStat("/", &cs); + EXPECT_EQ(result, 1); EXPECT_EQ(bSize, cs.NumberOfBlocks); } @@ -138,12 +144,13 @@ TEST(GetCommonStat, GetLinkCount) #if defined (__APPLE__) p = popen("/usr/bin/stat -f %l /", "r"); #else - p = popen("/usr/bin/stat -c %l /", "r"); + p = popen("/usr/bin/stat -c %h /", "r"); #endif int linkcount = -1; - fscanf(p, "%d", &linkcount); + int result = fscanf(p, "%d", &linkcount); pclose(p); GetCommonStat("/", &cs); + EXPECT_EQ(result, 1); EXPECT_EQ(linkcount, cs.HardlinkCount); } @@ -157,9 +164,10 @@ TEST(GetCommonStat, GetDeviceId) p = popen("/usr/bin/stat -c %d /", "r"); #endif int deviceId = -1; - fscanf(p, "%d", &deviceId); + int result = fscanf(p, "%d", &deviceId); pclose(p); GetCommonStat("/", &cs); + EXPECT_EQ(result, 1); EXPECT_EQ(deviceId, cs.DeviceId); } @@ -170,12 +178,13 @@ TEST(GetCommonStat, GetATime) #if defined (__APPLE__) p = popen("/usr/bin/stat -f %a /", "r"); #else - p = popen("/usr/bin/stat -c %a /", "r"); + p = popen("/usr/bin/stat -c %X /", "r"); #endif long aTime = -1; - fscanf(p, "%ld", &aTime); + int result = fscanf(p, "%ld", &aTime); pclose(p); GetCommonStat("/", &cs); + EXPECT_EQ(result, 1); EXPECT_EQ(aTime, cs.AccessTime); } @@ -186,12 +195,13 @@ TEST(GetCommonStat, GetMTime) #if defined (__APPLE__) p = popen("/usr/bin/stat -f %m /", "r"); #else - p = popen("/usr/bin/stat -c %m /", "r"); + p = popen("/usr/bin/stat -c %Y /", "r"); #endif long mTime = -1; - fscanf(p, "%ld", &mTime); + int result = fscanf(p, "%ld", &mTime); pclose(p); GetCommonStat("/", &cs); + EXPECT_EQ(result, 1); EXPECT_EQ(mTime, cs.ModifiedTime); } @@ -202,11 +212,12 @@ TEST(GetCommonStat, GetCTime) #if defined (__APPLE__) p = popen("/usr/bin/stat -f %c /", "r"); #else - p = popen("/usr/bin/stat -c %c /", "r"); + p = popen("/usr/bin/stat -c %Z /", "r"); #endif long cTime = -1; - fscanf(p, "%ld", &cTime); + int result = fscanf(p, "%ld", &cTime); pclose(p); GetCommonStat("/", &cs); + EXPECT_EQ(result, 1); EXPECT_EQ(cTime, cs.CreationTime); -} \ No newline at end of file +} From 927012005d64f70905f4587ccd24efaf372b8b97 Mon Sep 17 00:00:00 2001 From: James Truher Date: Mon, 26 Aug 2019 10:27:10 -0700 Subject: [PATCH 04/18] Add getgrgid api --- src/libpsl-native/src/CMakeLists.txt | 1 + src/libpsl-native/src/getgrgid.cpp | 67 ++++++++++++++++++++++++ src/libpsl-native/src/getgrgid.h | 14 +++++ src/libpsl-native/test/CMakeLists.txt | 2 + src/libpsl-native/test/test-getgrgid.cpp | 14 +++++ 5 files changed, 98 insertions(+) create mode 100644 src/libpsl-native/src/getgrgid.cpp create mode 100644 src/libpsl-native/src/getgrgid.h create mode 100644 src/libpsl-native/test/test-getgrgid.cpp diff --git a/src/libpsl-native/src/CMakeLists.txt b/src/libpsl-native/src/CMakeLists.txt index 3cf316c..de30e98 100644 --- a/src/libpsl-native/src/CMakeLists.txt +++ b/src/libpsl-native/src/CMakeLists.txt @@ -4,6 +4,7 @@ add_library(psl-native SHARED getstat.cpp getcommonstat.cpp getpwuid.cpp + getgrgid.cpp getppid.cpp getuserfrompid.cpp getfileowner.cpp diff --git a/src/libpsl-native/src/getgrgid.cpp b/src/libpsl-native/src/getgrgid.cpp new file mode 100644 index 0000000..aa3fd32 --- /dev/null +++ b/src/libpsl-native/src/getgrgid.cpp @@ -0,0 +1,67 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//! @brief returns the groupname for a gid + +#include "getgrgid.h" + +#include +#include +#include +#include +#include +#include + +//! @brief GetGrGid returns the groupname for a gid +//! +//! GetGrGid +//! +//! @param[in] gid +//! @parblock +//! The group identifier to lookup. +//! @endparblock +//! +//! @retval groupname as UTF-8 string, or NULL if unsuccessful +//! +char* GetGrGid(gid_t gid) +{ + int32_t ret = 0; + struct group grp; + struct group* result = NULL; + char* buf; + + int buflen = sysconf(_SC_GETPW_R_SIZE_MAX); + if (buflen < 1) + { + buflen = 2048; + } + +allocate: + buf = (char*)calloc(buflen, sizeof(char)); + + errno = 0; + ret = getgrgid_r(gid, &grp, buf, buflen, &result); + + if (ret != 0) + { + if (errno == ERANGE) + { + free(buf); + buflen *= 2; + goto allocate; + } + return NULL; + } + + // no result + if (result == NULL) + { + return NULL; + } + + // allocate copy on heap so CLR can free it + size_t userlen = strnlen(grp.gr_name, buflen); + char* groupname = strndup(grp.gr_name, userlen); + free(buf); + return groupname; +} diff --git a/src/libpsl-native/src/getgrgid.h b/src/libpsl-native/src/getgrgid.h new file mode 100644 index 0000000..039b1d7 --- /dev/null +++ b/src/libpsl-native/src/getgrgid.h @@ -0,0 +1,14 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#pragma once + +#include "pal.h" + +#include + +PAL_BEGIN_EXTERNC + +char* GetGrGid(gid_t gid); + +PAL_END_EXTERNC diff --git a/src/libpsl-native/test/CMakeLists.txt b/src/libpsl-native/test/CMakeLists.txt index 5f9ba2e..4ba819b 100644 --- a/src/libpsl-native/test/CMakeLists.txt +++ b/src/libpsl-native/test/CMakeLists.txt @@ -8,6 +8,8 @@ add_executable(psl-native-test test-getcomputername.cpp test-getcommonstat.cpp test-getlinkcount.cpp + test-getgrgid.cpp + test-getpwuid.cpp test-isdirectory.cpp test-isfile.cpp test-issymlink.cpp diff --git a/src/libpsl-native/test/test-getgrgid.cpp b/src/libpsl-native/test/test-getgrgid.cpp new file mode 100644 index 0000000..f6304ab --- /dev/null +++ b/src/libpsl-native/test/test-getgrgid.cpp @@ -0,0 +1,14 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//! @brief Unit tests for GetUserFromPid + +#include +#include +#include "getgrgid.h" + +TEST(GetGrGid, Success) +{ + char* expected = getgrgid(getegid())->gr_name; + EXPECT_STREQ(GetGrGid(getegid()), expected); +} \ No newline at end of file From edeec7fe7b8a407f23106fe1ba427c8edeb5b533 Mon Sep 17 00:00:00 2001 From: James Truher Date: Mon, 26 Aug 2019 10:28:09 -0700 Subject: [PATCH 05/18] add test for getpwuid --- src/libpsl-native/test/test-getpwuid.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/libpsl-native/test/test-getpwuid.cpp diff --git a/src/libpsl-native/test/test-getpwuid.cpp b/src/libpsl-native/test/test-getpwuid.cpp new file mode 100644 index 0000000..5c4b52d --- /dev/null +++ b/src/libpsl-native/test/test-getpwuid.cpp @@ -0,0 +1,14 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//! @brief Unit tests for GetUserFromPid + +#include +#include +#include "getpwuid.h" + +TEST(GetPwUid, Success) +{ + char* expected = getpwuid(geteuid())->pw_name; + EXPECT_STREQ(GetPwUid(geteuid()), expected); +} From 44277bb57e46135ab48bd7eaae869e6c4b7b32e4 Mon Sep 17 00:00:00 2001 From: James Truher Date: Wed, 28 Aug 2019 12:04:49 -0700 Subject: [PATCH 06/18] Split lstat and stat up Create tests for lstat and add a couple more tests for stat --- src/libpsl-native/src/CMakeLists.txt | 2 + src/libpsl-native/src/getcommonlstat.cpp | 57 +++++ src/libpsl-native/src/getcommonlstat.h | 18 ++ src/libpsl-native/src/getcommonstat.cpp | 39 --- src/libpsl-native/src/getcommonstat.h | 1 - src/libpsl-native/src/getlstat.cpp | 47 ++++ src/libpsl-native/src/getlstat.h | 14 ++ src/libpsl-native/src/getstat.cpp | 12 - src/libpsl-native/src/getstat.h | 1 - src/libpsl-native/test/CMakeLists.txt | 1 + .../test/test-getcommonlstat.cpp | 223 ++++++++++++++++++ src/libpsl-native/test/test-getcommonstat.cpp | 17 ++ 12 files changed, 379 insertions(+), 53 deletions(-) create mode 100644 src/libpsl-native/src/getcommonlstat.cpp create mode 100644 src/libpsl-native/src/getcommonlstat.h create mode 100644 src/libpsl-native/src/getlstat.cpp create mode 100644 src/libpsl-native/src/getlstat.h create mode 100644 src/libpsl-native/test/test-getcommonlstat.cpp diff --git a/src/libpsl-native/src/CMakeLists.txt b/src/libpsl-native/src/CMakeLists.txt index de30e98..3b06458 100644 --- a/src/libpsl-native/src/CMakeLists.txt +++ b/src/libpsl-native/src/CMakeLists.txt @@ -2,7 +2,9 @@ include(CheckIncludeFiles) add_library(psl-native SHARED getstat.cpp + getlstat.cpp getcommonstat.cpp + getcommonlstat.cpp getpwuid.cpp getgrgid.cpp getppid.cpp diff --git a/src/libpsl-native/src/getcommonlstat.cpp b/src/libpsl-native/src/getcommonlstat.cpp new file mode 100644 index 0000000..ace39cb --- /dev/null +++ b/src/libpsl-native/src/getcommonlstat.cpp @@ -0,0 +1,57 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//! @brief returns the stat of a file + + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "getcommonlstat.h" + +// Provide a common structure for the various different stat structures. +// This should be safe to call on all platforms +int GetCommonLStat(const char* path, struct CommonStat* commonStat) +{ + struct stat st; + assert(path); + errno = 0; + if (lstat(path, &st) == 0) + { + commonStat->Inode = st.st_ino; + commonStat->Mode = st.st_mode; + commonStat->UserId = st.st_uid; + commonStat->GroupId = st.st_gid; + commonStat->HardlinkCount = st.st_nlink; + commonStat->Size = st.st_size; +#if defined (__APPLE__) + commonStat->AccessTime = st.st_atimespec.tv_sec; + commonStat->ModifiedTime = st.st_mtimespec.tv_sec; + commonStat->CreationTime = st.st_ctimespec.tv_sec; +#else + commonStat->AccessTime = st.st_atime; + commonStat->ModifiedTime = st.st_mtime; + commonStat->CreationTime = st.st_ctime; +#endif + commonStat->BlockSize = st.st_blksize; + commonStat->DeviceId = st.st_dev; + commonStat->NumberOfBlocks = st.st_blocks; + commonStat->IsBlockDevice = S_ISBLK(st.st_mode); + commonStat->IsCharacterDevice = S_ISCHR(st.st_mode); + commonStat->IsDirectory = S_ISDIR(st.st_mode); + commonStat->IsFile = S_ISREG(st.st_mode); + commonStat->IsNamedPipe = S_ISFIFO(st.st_mode); + commonStat->IsSocket = S_ISSOCK(st.st_mode); + commonStat->IsSymbolicLink = S_ISLNK(st.st_mode); + return 0; + } + return -1; +} + diff --git a/src/libpsl-native/src/getcommonlstat.h b/src/libpsl-native/src/getcommonlstat.h new file mode 100644 index 0000000..dfcf472 --- /dev/null +++ b/src/libpsl-native/src/getcommonlstat.h @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#pragma once + +#include "pal.h" + + +#include + +#include "getcommonstat.h" + +PAL_BEGIN_EXTERNC + +int32_t GetLStat(const char* path, struct stat* buf); +int GetCommonLStat(const char* path, CommonStat* cs); + +PAL_END_EXTERNC diff --git a/src/libpsl-native/src/getcommonstat.cpp b/src/libpsl-native/src/getcommonstat.cpp index 63a2363..3d3a61a 100644 --- a/src/libpsl-native/src/getcommonstat.cpp +++ b/src/libpsl-native/src/getcommonstat.cpp @@ -15,45 +15,6 @@ #include -// Provide a common structure for the various different stat structures. -// This should be safe to call on all platforms -int GetCommonLStat(const char* path, struct CommonStat* commonStat) -{ - struct stat st; - assert(path); - errno = 0; - if (lstat(path, &st) == 0) - { - commonStat->Inode = st.st_ino; - commonStat->Mode = st.st_mode; - commonStat->UserId = st.st_uid; - commonStat->GroupId = st.st_gid; - commonStat->HardlinkCount = st.st_nlink; - commonStat->Size = st.st_size; -#if defined (__APPLE__) - commonStat->AccessTime = st.st_atimespec.tv_sec; - commonStat->ModifiedTime = st.st_mtimespec.tv_sec; - commonStat->CreationTime = st.st_ctimespec.tv_sec; -#else - commonStat->AccessTime = st.st_atime; - commonStat->ModifiedTime = st.st_mtime; - commonStat->CreationTime = st.st_ctime; -#endif - commonStat->BlockSize = st.st_blksize; - commonStat->DeviceId = st.st_dev; - commonStat->NumberOfBlocks = st.st_blocks; - commonStat->IsBlockDevice = S_ISBLK(st.st_mode); - commonStat->IsCharacterDevice = S_ISCHR(st.st_mode); - commonStat->IsDirectory = S_ISDIR(st.st_mode); - commonStat->IsFile = S_ISREG(st.st_mode); - commonStat->IsNamedPipe = S_ISFIFO(st.st_mode); - commonStat->IsSocket = S_ISSOCK(st.st_mode); - commonStat->IsSymbolicLink = S_ISLNK(st.st_mode); - return 0; - } - return -1; -} - // Provide a common structure for the various different stat structures. // This should be safe to call on all platforms int GetCommonStat(const char* path, struct CommonStat* commonStat) diff --git a/src/libpsl-native/src/getcommonstat.h b/src/libpsl-native/src/getcommonstat.h index 0c1a206..f1057f0 100644 --- a/src/libpsl-native/src/getcommonstat.h +++ b/src/libpsl-native/src/getcommonstat.h @@ -34,6 +34,5 @@ struct CommonStat int32_t GetStat(const char* path, struct stat* buf); int GetCommonStat(const char* path, CommonStat* cs); -int GetCommonLStat(const char* path, CommonStat* cs); PAL_END_EXTERNC diff --git a/src/libpsl-native/src/getlstat.cpp b/src/libpsl-native/src/getlstat.cpp new file mode 100644 index 0000000..9df33ef --- /dev/null +++ b/src/libpsl-native/src/getlstat.cpp @@ -0,0 +1,47 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//! @brief returns the stat of a file + +#include "getlstat.h" + +#include +#include +#include +#include +#include +#include +#include + +//! @brief GetStat returns the stat of a file. This simply delegates to the +//! stat() system call and maps errno to the expected values for GetLastError. +//! +//! GetStat +//! +//! @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 +//! +//! @param[in] stat +//! @parblock +//! A pointer to the buffer in which to place the stat information +//! @endparblock +//! +//! @retval 0 if successful +//! @retval -1 if failed +//! + +// DO NOT use in managed code +// use externally defined structs in managed code has proven to be buggy +// (memory corruption issues due to layout difference between platforms) +// see https://github.com/dotnet/corefx/issues/29700#issuecomment-389313075 +int32_t GetLStat(const char* path, struct stat* buf) +{ + assert(path); + errno = 0; + + return lstat(path, buf); +} diff --git a/src/libpsl-native/src/getlstat.h b/src/libpsl-native/src/getlstat.h new file mode 100644 index 0000000..4a0604a --- /dev/null +++ b/src/libpsl-native/src/getlstat.h @@ -0,0 +1,14 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#pragma once + +#include "pal.h" + +#include + +PAL_BEGIN_EXTERNC + +int32_t GetLStat(const char* path, struct stat* buf); + +PAL_END_EXTERNC diff --git a/src/libpsl-native/src/getstat.cpp b/src/libpsl-native/src/getstat.cpp index 68affd0..8120e22 100644 --- a/src/libpsl-native/src/getstat.cpp +++ b/src/libpsl-native/src/getstat.cpp @@ -46,15 +46,3 @@ int32_t GetStat(const char* path, struct stat* buf) return stat(path, buf); } - -// DO NOT use in managed code -// use externally defined structs in managed code has proven to be buggy -// (memory corruption issues due to layout difference between platforms) -// see https://github.com/dotnet/corefx/issues/29700#issuecomment-389313075 -int32_t GetLStat(const char* path, struct stat* buf) -{ - assert(path); - errno = 0; - - return lstat(path, buf); -} diff --git a/src/libpsl-native/src/getstat.h b/src/libpsl-native/src/getstat.h index 2489c1c..12270e0 100644 --- a/src/libpsl-native/src/getstat.h +++ b/src/libpsl-native/src/getstat.h @@ -10,6 +10,5 @@ PAL_BEGIN_EXTERNC int32_t GetStat(const char* path, struct stat* buf); -int32_t GetLStat(const char* path, struct stat* buf); PAL_END_EXTERNC diff --git a/src/libpsl-native/test/CMakeLists.txt b/src/libpsl-native/test/CMakeLists.txt index 4ba819b..9d3f4f5 100644 --- a/src/libpsl-native/test/CMakeLists.txt +++ b/src/libpsl-native/test/CMakeLists.txt @@ -7,6 +7,7 @@ add_executable(psl-native-test test-getcurrentprocessid.cpp test-getcomputername.cpp test-getcommonstat.cpp + test-getcommonlstat.cpp test-getlinkcount.cpp test-getgrgid.cpp test-getpwuid.cpp diff --git a/src/libpsl-native/test/test-getcommonlstat.cpp b/src/libpsl-native/test/test-getcommonlstat.cpp new file mode 100644 index 0000000..450e417 --- /dev/null +++ b/src/libpsl-native/test/test-getcommonlstat.cpp @@ -0,0 +1,223 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//! @brief Tests IsDirectory + +#include +#include +#include +#include "isdirectory.h" +#include "getcommonlstat.h" + +TEST(GetCommonLStat, RootIsDirectory) +{ + CommonStat cs; + GetCommonLStat("/", &cs); + bool isDir = IsDirectory("/"); + bool fromCommonLStat = (bool)cs.IsDirectory; + EXPECT_EQ(isDir, fromCommonLStat); +} + +TEST(GetCommonLStat, BinLsIsNotDirectory) +{ + CommonStat cs; + GetCommonLStat("/bin/ls", &cs); + bool isDir = IsDirectory("/bin/ls"); + bool fromCommonLStat = (bool)cs.IsDirectory; + EXPECT_EQ(isDir, fromCommonLStat); +} + + +TEST(GetCommonLStat, ReturnsFalseForFakeDirectory) +{ + CommonStat cs; + int badDir = GetCommonLStat("/A/Really/Bad/Directory",&cs); + EXPECT_EQ(badDir, -1); +} + +TEST(GetCommonLStat, GetOwnerIdOfRoot) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %u /", "r"); +#else + p = popen("/usr/bin/stat -c %u /", "r"); +#endif + int uid = -1; + int result = fscanf(p, "%d", &uid); + pclose(p); + GetCommonLStat("/", &cs); + EXPECT_EQ(result, 1); + EXPECT_EQ(uid, cs.UserId); +} + +TEST(GetCommonLStat, GetGroupId) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %g /", "r"); +#else + p = popen("/usr/bin/stat -c %g /", "r"); +#endif + int gid = -1; + int result = fscanf(p, "%d", &gid); + pclose(p); + GetCommonLStat("/", &cs); + EXPECT_EQ(result, 1); + EXPECT_EQ(gid, cs.UserId); +} + +TEST(GetCommonLStat, GetInodeNumber) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %i /", "r"); +#else + p = popen("/usr/bin/stat -c %i /", "r"); +#endif + long inode = -1; + int result = fscanf(p, "%ld", &inode); + pclose(p); + GetCommonLStat("/", &cs); + EXPECT_EQ(result, 1); + EXPECT_EQ(inode, cs.Inode); +} + +TEST(GetCommonLStat, GetSize) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %z /", "r"); +#else + p = popen("/usr/bin/stat -c %s /", "r"); +#endif + long size = -1; + int result = fscanf(p, "%ld", &size); + pclose(p); + GetCommonLStat("/", &cs); + EXPECT_EQ(result, 1); + EXPECT_EQ(size, cs.Size); +} + +TEST(GetCommonLStat, GetBlockSize) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %k /", "r"); +#else + p = popen("/usr/bin/stat -c %o /", "r"); +#endif + long bSize = -1; + int result = fscanf(p, "%ld", &bSize); + pclose(p); + GetCommonLStat("/", &cs); + EXPECT_EQ(result, 1); + EXPECT_EQ(bSize, cs.BlockSize); +} + +TEST(GetCommonLStat, GetBlockCount) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %b /", "r"); +#else + p = popen("/usr/bin/stat -c %b /", "r"); +#endif + int bSize = -1; + int result = fscanf(p, "%d", &bSize); + pclose(p); + GetCommonLStat("/", &cs); + EXPECT_EQ(result, 1); + EXPECT_EQ(bSize, cs.NumberOfBlocks); +} + +TEST(GetCommonLStat, GetLinkCount) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %l /", "r"); +#else + p = popen("/usr/bin/stat -c %h /", "r"); +#endif + int linkcount = -1; + int result = fscanf(p, "%d", &linkcount); + pclose(p); + GetCommonLStat("/", &cs); + EXPECT_EQ(result, 1); + EXPECT_EQ(linkcount, cs.HardlinkCount); +} + +TEST(GetCommonLStat, GetDeviceId) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %d /", "r"); +#else + p = popen("/usr/bin/stat -c %d /", "r"); +#endif + int deviceId = -1; + int result = fscanf(p, "%d", &deviceId); + pclose(p); + GetCommonLStat("/", &cs); + EXPECT_EQ(result, 1); + EXPECT_EQ(deviceId, cs.DeviceId); +} + +TEST(GetCommonLStat, GetATime) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %a /", "r"); +#else + p = popen("/usr/bin/stat -c %X /", "r"); +#endif + long aTime = -1; + int result = fscanf(p, "%ld", &aTime); + pclose(p); + GetCommonLStat("/", &cs); + EXPECT_EQ(result, 1); + EXPECT_EQ(aTime, cs.AccessTime); +} + +TEST(GetCommonLStat, GetMTime) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %m /", "r"); +#else + p = popen("/usr/bin/stat -c %Y /", "r"); +#endif + long mTime = -1; + int result = fscanf(p, "%ld", &mTime); + pclose(p); + GetCommonLStat("/", &cs); + EXPECT_EQ(result, 1); + EXPECT_EQ(mTime, cs.ModifiedTime); +} + +TEST(GetCommonLStat, GetCTime) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %c /", "r"); +#else + p = popen("/usr/bin/stat -c %Z /", "r"); +#endif + long cTime = -1; + int result = fscanf(p, "%ld", &cTime); + pclose(p); + GetCommonLStat("/", &cs); + EXPECT_EQ(result, 1); + EXPECT_EQ(cTime, cs.CreationTime); +} diff --git a/src/libpsl-native/test/test-getcommonstat.cpp b/src/libpsl-native/test/test-getcommonstat.cpp index 353c148..d9699fc 100644 --- a/src/libpsl-native/test/test-getcommonstat.cpp +++ b/src/libpsl-native/test/test-getcommonstat.cpp @@ -86,6 +86,23 @@ TEST(GetCommonStat, GetInodeNumber) EXPECT_EQ(inode, cs.Inode); } +TEST(GetCommonStat, GetMode) +{ + FILE *p; + CommonStat cs; +#if defined (__APPLE__) + p = popen("/usr/bin/stat -f %p /", "r"); +#else + p = popen("/usr/bin/stat -c %f /", "r"); +#endif + long mode = -1; + int result = fscanf(p, "%lo", &mode); + pclose(p); + GetCommonStat("/", &cs); + EXPECT_EQ(result, 1); + EXPECT_EQ(mode, cs.Mode); +} + TEST(GetCommonStat, GetSize) { FILE *p; From b368d03e05ebc254c08d1903e089be20d92795e7 Mon Sep 17 00:00:00 2001 From: James Truher Date: Wed, 28 Aug 2019 12:26:16 -0700 Subject: [PATCH 07/18] Update mode test to scan for hex on Linux --- src/libpsl-native/test/test-getcommonstat.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/libpsl-native/test/test-getcommonstat.cpp b/src/libpsl-native/test/test-getcommonstat.cpp index d9699fc..6e1ec72 100644 --- a/src/libpsl-native/test/test-getcommonstat.cpp +++ b/src/libpsl-native/test/test-getcommonstat.cpp @@ -90,13 +90,14 @@ TEST(GetCommonStat, GetMode) { FILE *p; CommonStat cs; + unsigned int mode = -1; #if defined (__APPLE__) - p = popen("/usr/bin/stat -f %p /", "r"); + p = popen("/usr/bin/stat -f %p /", "r"); + int result = fscanf(p, "%lo", &mode); #else - p = popen("/usr/bin/stat -c %f /", "r"); + p = popen("/usr/bin/stat -c %f /", "r"); + int result = fscanf(p, "%x", &mode); #endif - long mode = -1; - int result = fscanf(p, "%lo", &mode); pclose(p); GetCommonStat("/", &cs); EXPECT_EQ(result, 1); From 73f89d1d83ea126f9a0ddd3f040448e387087ae8 Mon Sep 17 00:00:00 2001 From: James Truher Date: Wed, 28 Aug 2019 14:30:50 -0700 Subject: [PATCH 08/18] Changes needed to build on OpenSUSE --- build.psm1 | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/build.psm1 b/build.psm1 index d08d6d3..cd3e8df 100644 --- a/build.psm1 +++ b/build.psm1 @@ -1985,7 +1985,8 @@ function Start-PSBootstrap { } } elseif ($Environment.IsSUSEFamily) { # Build tools - $Deps += "gcc", "cmake", "make" + # we're using a flag which requires an up to date compiler + $Deps += "gcc7", "cmake", "make", "gcc7-c++" # Packaging tools if ($Package) { $Deps += "ruby-devel", "rpmbuild", "groff", 'libffi-devel' } @@ -2003,6 +2004,21 @@ function Start-PSBootstrap { Start-NativeExecution { Invoke-Expression "$baseCommand $Deps" } + + # After installation, create the appropriate symbolic links + foreach ($compiler in "gcc-7","g++-7") { + $itemPath = "/usr/bin/${compiler}" + $name = $compiler -replace "-7" + $linkPath = "/usr/bin/${name}" + $link = Get-Item -Path $linkPath -ErrorAction SilentlyContinue + if ($link) { + if ($link.Target) { # Symbolic link - remove it + Remove-Item -Force -Path $linkPath + } + } + New-Item -Type SymbolicLink -Target $itemPath -Path $linkPath + } + } elseif ($Environment.IsMacOS) { precheck 'brew' "Bootstrap dependency 'brew' not found, must install Homebrew! See http://brew.sh/" From 83ce14f8db5be4aa02aff58b1a13f847ed1ce6f6 Mon Sep 17 00:00:00 2001 From: James Truher Date: Wed, 28 Aug 2019 14:41:10 -0700 Subject: [PATCH 09/18] add debian to ubuntu clause for installing dependencies --- build.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.psm1 b/build.psm1 index cd3e8df..a2b289f 100644 --- a/build.psm1 +++ b/build.psm1 @@ -1927,7 +1927,7 @@ function Start-PSBootstrap { # Install ours and .NET's dependencies $Deps = @() - if ($Environment.IsUbuntu) { + if ($Environment.IsUbuntu -or $Environment.IsDebian) { # Build tools $Deps += "curl", "g++", "cmake", "make" From 550b00bd335f2b5a93405043dfdf0e1b49d3f177 Mon Sep 17 00:00:00 2001 From: James Truher Date: Sun, 1 Sep 2019 01:24:16 -0700 Subject: [PATCH 10/18] Add support for SetUid, SetGid, Sticky bits Add tests for new bits Add clean up for getlinkcount test which was leaving files behind --- src/libpsl-native/src/getcommonlstat.cpp | 3 + src/libpsl-native/src/getcommonstat.cpp | 3 + src/libpsl-native/src/getcommonstat.h | 3 + .../test/test-getcommonlstat.cpp | 65 +++++++++++++++++ src/libpsl-native/test/test-getcommonstat.cpp | 69 ++++++++++++++++++- src/libpsl-native/test/test-getlinkcount.cpp | 20 +++++- 6 files changed, 159 insertions(+), 4 deletions(-) diff --git a/src/libpsl-native/src/getcommonlstat.cpp b/src/libpsl-native/src/getcommonlstat.cpp index ace39cb..311b7f1 100644 --- a/src/libpsl-native/src/getcommonlstat.cpp +++ b/src/libpsl-native/src/getcommonlstat.cpp @@ -50,6 +50,9 @@ int GetCommonLStat(const char* path, struct CommonStat* commonStat) commonStat->IsNamedPipe = S_ISFIFO(st.st_mode); commonStat->IsSocket = S_ISSOCK(st.st_mode); commonStat->IsSymbolicLink = S_ISLNK(st.st_mode); + commonStat->IsSetUid = (st.st_mode & 0xE00) == S_ISUID; + commonStat->IsSetGid = (st.st_mode & 0xE00) == S_ISGID; + commonStat->IsSticky = (st.st_mode & 0xE00) == S_ISVTX; return 0; } return -1; diff --git a/src/libpsl-native/src/getcommonstat.cpp b/src/libpsl-native/src/getcommonstat.cpp index 3d3a61a..1fc6cac 100644 --- a/src/libpsl-native/src/getcommonstat.cpp +++ b/src/libpsl-native/src/getcommonstat.cpp @@ -49,6 +49,9 @@ int GetCommonStat(const char* path, struct CommonStat* commonStat) commonStat->IsNamedPipe = S_ISFIFO(st.st_mode); commonStat->IsSocket = S_ISSOCK(st.st_mode); commonStat->IsSymbolicLink = S_ISLNK(st.st_mode); + commonStat->IsSetUid = (st.st_mode & 0xE00) == S_ISUID; + commonStat->IsSetGid = (st.st_mode & 0xE00) == S_ISGID; + commonStat->IsSticky = (st.st_mode & 0xE00) == S_ISVTX; return 0; } return -1; diff --git a/src/libpsl-native/src/getcommonstat.h b/src/libpsl-native/src/getcommonstat.h index f1057f0..cb26875 100644 --- a/src/libpsl-native/src/getcommonstat.h +++ b/src/libpsl-native/src/getcommonstat.h @@ -30,6 +30,9 @@ struct CommonStat int IsCharacterDevice; int IsNamedPipe; int IsSocket; + int IsSetUid; + int IsSetGid; + int IsSticky; }; int32_t GetStat(const char* path, struct stat* buf); diff --git a/src/libpsl-native/test/test-getcommonlstat.cpp b/src/libpsl-native/test/test-getcommonlstat.cpp index 450e417..faddb4d 100644 --- a/src/libpsl-native/test/test-getcommonlstat.cpp +++ b/src/libpsl-native/test/test-getcommonlstat.cpp @@ -221,3 +221,68 @@ TEST(GetCommonLStat, GetCTime) EXPECT_EQ(result, 1); EXPECT_EQ(cTime, cs.CreationTime); } + +TEST(GetCommonLStat, Mode001) +{ + const std::string ftemplate = "/tmp/CommonStatModeF_XXXXXX"; + char fname[PATH_MAX]; + struct stat buffer; + int fd; + CommonStat cs; + strcpy(fname, ftemplate.c_str()); + fd = mkstemp(fname); + chmod(fname, S_IRWXU | S_IRWXG | S_IROTH | S_IWOTH); + lstat(fname, &buffer); + GetCommonLStat(fname, &cs); + unlink(fname); + EXPECT_EQ(cs.Mode, buffer.st_mode); +} + +TEST(GetCommonLStat, Mode002) +{ + const std::string ftemplate = "/tmp/CommonStatModeF_XXXXXX"; + char fname[PATH_MAX]; + struct stat buffer; + int fd; + CommonStat cs; + strcpy(fname, ftemplate.c_str()); + fd = mkstemp(fname); + chmod(fname, S_IRWXU | S_IRWXG | S_IROTH | S_IWOTH | S_ISUID ); + lstat(fname, &buffer); + GetCommonLStat(fname, &cs); + unlink(fname); + EXPECT_EQ(cs.Mode, buffer.st_mode); +} + +TEST(GetCommonLStat, Mode003) +{ + const std::string ftemplate = "/tmp/CommonStatModeF_XXXXXX"; + char fname[PATH_MAX]; + struct stat buffer; + int fd; + CommonStat cs; + strcpy(fname, ftemplate.c_str()); + fd = mkstemp(fname); + chmod(fname, S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH | S_ISGID ); + lstat(fname, &buffer); + GetCommonLStat(fname, &cs); + unlink(fname); + EXPECT_EQ(cs.Mode, buffer.st_mode); +} + +TEST(GetCommonLStat, Mode004) +{ + const std::string ftemplate = "/tmp/CommonStatModeD_XXXXXX"; + char dname[PATH_MAX]; + struct stat buffer; + char * fd; + CommonStat cs; + strcpy(dname, ftemplate.c_str()); + fd = mkdtemp(dname); + chmod(dname, S_IRWXU | S_IRWXG | S_IROTH | S_IWOTH | S_ISVTX ); + lstat(dname, &buffer); + GetCommonLStat(dname, &cs); + rmdir(dname); + EXPECT_EQ(cs.Mode, buffer.st_mode); +} + diff --git a/src/libpsl-native/test/test-getcommonstat.cpp b/src/libpsl-native/test/test-getcommonstat.cpp index 6e1ec72..35a02d2 100644 --- a/src/libpsl-native/test/test-getcommonstat.cpp +++ b/src/libpsl-native/test/test-getcommonstat.cpp @@ -8,6 +8,8 @@ #include #include "isdirectory.h" #include "getcommonstat.h" +#include +#include TEST(GetCommonStat, RootIsDirectory) { @@ -93,7 +95,7 @@ TEST(GetCommonStat, GetMode) unsigned int mode = -1; #if defined (__APPLE__) p = popen("/usr/bin/stat -f %p /", "r"); - int result = fscanf(p, "%lo", &mode); + int result = fscanf(p, "%o", &mode); #else p = popen("/usr/bin/stat -c %f /", "r"); int result = fscanf(p, "%x", &mode); @@ -239,3 +241,68 @@ TEST(GetCommonStat, GetCTime) EXPECT_EQ(result, 1); EXPECT_EQ(cTime, cs.CreationTime); } + +TEST(GetCommonStat, Mode001) +{ + const std::string ftemplate = "/tmp/CommonStatModeF_XXXXXX"; + char fname[PATH_MAX]; + struct stat buffer; + int fd; + CommonStat cs; + strcpy(fname, ftemplate.c_str()); + fd = mkstemp(fname); + chmod(fname, S_IRWXU | S_IRWXG | S_IROTH | S_IWOTH); + stat(fname, &buffer); + GetCommonStat(fname, &cs); + unlink(fname); + EXPECT_EQ(cs.Mode, buffer.st_mode); +} + +TEST(GetCommonStat, Mode002) +{ + const std::string ftemplate = "/tmp/CommonStatModeF_XXXXXX"; + char fname[PATH_MAX]; + struct stat buffer; + int fd; + CommonStat cs; + strcpy(fname, ftemplate.c_str()); + fd = mkstemp(fname); + chmod(fname, S_IRWXU | S_IRWXG | S_IROTH | S_IWOTH | S_ISUID ); + stat(fname, &buffer); + GetCommonStat(fname, &cs); + unlink(fname); + EXPECT_EQ(cs.Mode, buffer.st_mode); +} + +TEST(GetCommonStat, Mode003) +{ + const std::string ftemplate = "/tmp/CommonStatModeF_XXXXXX"; + char fname[PATH_MAX]; + struct stat buffer; + int fd; + CommonStat cs; + strcpy(fname, ftemplate.c_str()); + fd = mkstemp(fname); + chmod(fname, S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH | S_ISGID ); + stat(fname, &buffer); + GetCommonStat(fname, &cs); + unlink(fname); + EXPECT_EQ(cs.Mode, buffer.st_mode); +} + +TEST(GetCommonStat, Mode004) +{ + const std::string ftemplate = "/tmp/CommonStatModeD_XXXXXX"; + char dname[PATH_MAX]; + struct stat buffer; + char * fd; + CommonStat cs; + strcpy(dname, ftemplate.c_str()); + fd = mkdtemp(dname); + chmod(dname, S_IRWXU | S_IRWXG | S_IROTH | S_IWOTH | S_ISVTX ); + stat(dname, &buffer); + GetCommonStat(dname, &cs); + rmdir(dname); + EXPECT_EQ(cs.Mode, buffer.st_mode); +} + diff --git a/src/libpsl-native/test/test-getlinkcount.cpp b/src/libpsl-native/test/test-getlinkcount.cpp index 741c0c5..0d3b6b6 100644 --- a/src/libpsl-native/test/test-getlinkcount.cpp +++ b/src/libpsl-native/test/test-getlinkcount.cpp @@ -33,6 +33,11 @@ class getLinkCountTest : public ::testing::Test file = fileTemplateBuf; } + ~getLinkCountTest() + { + unlink(fileTemplateBuf); + } + void createFileForTesting(const std::string &theFile) { std::ofstream ofs; @@ -69,10 +74,14 @@ TEST_F(getLinkCountTest, LinkCountOfSinglyLinkedFile) { createFileForTesting(file); int32_t ret = GetLinkCount(file, &count); + +printf("001 %s\n", file); + + removeFile(file); + ASSERT_EQ(0, ret); EXPECT_EQ(1, count); - removeFile(file); } TEST_F(getLinkCountTest, LinkCountOfMultiplyLinkedFile) @@ -80,10 +89,15 @@ TEST_F(getLinkCountTest, LinkCountOfMultiplyLinkedFile) createFileForTesting(file); std::string newFile = createHardLink(file); int32_t ret = GetLinkCount(file, &count); - ASSERT_EQ(0, ret); - EXPECT_EQ(2, count); + +printf("002 %s\n", file); +printf("003 %s\n", newFile.c_str()); removeFile(file); removeFile(newFile); + + ASSERT_EQ(0, ret); + EXPECT_EQ(2, count); + } From 8469cc55d1a316e5fb497a246d321ec4b24064da Mon Sep 17 00:00:00 2001 From: James Truher Date: Thu, 5 Sep 2019 10:26:15 -0700 Subject: [PATCH 11/18] add check for whether the mkstemp or mkdtemp worked --- src/libpsl-native/test/test-getcommonlstat.cpp | 4 ++++ src/libpsl-native/test/test-getcommonstat.cpp | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/libpsl-native/test/test-getcommonlstat.cpp b/src/libpsl-native/test/test-getcommonlstat.cpp index faddb4d..545c6db 100644 --- a/src/libpsl-native/test/test-getcommonlstat.cpp +++ b/src/libpsl-native/test/test-getcommonlstat.cpp @@ -231,6 +231,7 @@ TEST(GetCommonLStat, Mode001) CommonStat cs; strcpy(fname, ftemplate.c_str()); fd = mkstemp(fname); + EXPECT_NE(fd, -1); chmod(fname, S_IRWXU | S_IRWXG | S_IROTH | S_IWOTH); lstat(fname, &buffer); GetCommonLStat(fname, &cs); @@ -247,6 +248,7 @@ TEST(GetCommonLStat, Mode002) CommonStat cs; strcpy(fname, ftemplate.c_str()); fd = mkstemp(fname); + EXPECT_NE(fd, -1); chmod(fname, S_IRWXU | S_IRWXG | S_IROTH | S_IWOTH | S_ISUID ); lstat(fname, &buffer); GetCommonLStat(fname, &cs); @@ -263,6 +265,7 @@ TEST(GetCommonLStat, Mode003) CommonStat cs; strcpy(fname, ftemplate.c_str()); fd = mkstemp(fname); + EXPECT_NE(fd, -1); chmod(fname, S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH | S_ISGID ); lstat(fname, &buffer); GetCommonLStat(fname, &cs); @@ -279,6 +282,7 @@ TEST(GetCommonLStat, Mode004) CommonStat cs; strcpy(dname, ftemplate.c_str()); fd = mkdtemp(dname); + EXPECT_NE(fd, ftemplate); chmod(dname, S_IRWXU | S_IRWXG | S_IROTH | S_IWOTH | S_ISVTX ); lstat(dname, &buffer); GetCommonLStat(dname, &cs); diff --git a/src/libpsl-native/test/test-getcommonstat.cpp b/src/libpsl-native/test/test-getcommonstat.cpp index 35a02d2..cb88137 100644 --- a/src/libpsl-native/test/test-getcommonstat.cpp +++ b/src/libpsl-native/test/test-getcommonstat.cpp @@ -251,6 +251,7 @@ TEST(GetCommonStat, Mode001) CommonStat cs; strcpy(fname, ftemplate.c_str()); fd = mkstemp(fname); + EXPECT_NE(fd, -1); chmod(fname, S_IRWXU | S_IRWXG | S_IROTH | S_IWOTH); stat(fname, &buffer); GetCommonStat(fname, &cs); @@ -267,6 +268,7 @@ TEST(GetCommonStat, Mode002) CommonStat cs; strcpy(fname, ftemplate.c_str()); fd = mkstemp(fname); + EXPECT_NE(fd, -1); chmod(fname, S_IRWXU | S_IRWXG | S_IROTH | S_IWOTH | S_ISUID ); stat(fname, &buffer); GetCommonStat(fname, &cs); @@ -283,6 +285,7 @@ TEST(GetCommonStat, Mode003) CommonStat cs; strcpy(fname, ftemplate.c_str()); fd = mkstemp(fname); + EXPECT_NE(fd, -1); chmod(fname, S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH | S_ISGID ); stat(fname, &buffer); GetCommonStat(fname, &cs); @@ -299,6 +302,7 @@ TEST(GetCommonStat, Mode004) CommonStat cs; strcpy(dname, ftemplate.c_str()); fd = mkdtemp(dname); + EXPECT_NE(fd, ftemplate); chmod(dname, S_IRWXU | S_IRWXG | S_IROTH | S_IWOTH | S_ISVTX ); stat(dname, &buffer); GetCommonStat(dname, &cs); From b3c42d4e9af7945aca17853484a847bbe2664c56 Mon Sep 17 00:00:00 2001 From: James Truher Date: Wed, 23 Oct 2019 16:20:46 -0700 Subject: [PATCH 12/18] Fix test to actually compare with the GroupId --- src/libpsl-native/test/test-getcommonlstat.cpp | 2 +- src/libpsl-native/test/test-getcommonstat.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libpsl-native/test/test-getcommonlstat.cpp b/src/libpsl-native/test/test-getcommonlstat.cpp index 545c6db..f2847ad 100644 --- a/src/libpsl-native/test/test-getcommonlstat.cpp +++ b/src/libpsl-native/test/test-getcommonlstat.cpp @@ -66,7 +66,7 @@ TEST(GetCommonLStat, GetGroupId) pclose(p); GetCommonLStat("/", &cs); EXPECT_EQ(result, 1); - EXPECT_EQ(gid, cs.UserId); + EXPECT_EQ(gid, cs.GroupId); } TEST(GetCommonLStat, GetInodeNumber) diff --git a/src/libpsl-native/test/test-getcommonstat.cpp b/src/libpsl-native/test/test-getcommonstat.cpp index cb88137..5b81b20 100644 --- a/src/libpsl-native/test/test-getcommonstat.cpp +++ b/src/libpsl-native/test/test-getcommonstat.cpp @@ -68,7 +68,7 @@ TEST(GetCommonStat, GetGroupId) pclose(p); GetCommonStat("/", &cs); EXPECT_EQ(result, 1); - EXPECT_EQ(gid, cs.UserId); + EXPECT_EQ(gid, cs.GroupId); } TEST(GetCommonStat, GetInodeNumber) From a9a4ff0d12674c0c065e198b961204c053b176fa Mon Sep 17 00:00:00 2001 From: James Truher Date: Thu, 24 Oct 2019 09:56:31 -0700 Subject: [PATCH 13/18] fix name for time of last status change member --- src/libpsl-native/gmock.pc | 9 +++++++++ src/libpsl-native/gmock_main.pc | 9 +++++++++ src/libpsl-native/gtest.pc | 9 +++++++++ src/libpsl-native/gtest_main.pc | 10 ++++++++++ src/libpsl-native/src/getcommonlstat.cpp | 4 ++-- src/libpsl-native/src/getcommonstat.cpp | 4 ++-- src/libpsl-native/src/getcommonstat.h | 2 +- src/libpsl-native/test/test-getcommonlstat.cpp | 2 +- src/libpsl-native/test/test-getcommonstat.cpp | 2 +- 9 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 src/libpsl-native/gmock.pc create mode 100644 src/libpsl-native/gmock_main.pc create mode 100644 src/libpsl-native/gtest.pc create mode 100644 src/libpsl-native/gtest_main.pc diff --git a/src/libpsl-native/gmock.pc b/src/libpsl-native/gmock.pc new file mode 100644 index 0000000..dd45b5f --- /dev/null +++ b/src/libpsl-native/gmock.pc @@ -0,0 +1,9 @@ +libdir=/usr/local/lib +includedir=/usr/local/include + +Name: gmock +Description: GoogleMock (without main() function) +Version: 1.9.0 +URL: https://github.com/google/googletest +Libs: -L${libdir} -lgmock +Cflags: -I${includedir} -DGTEST_HAS_PTHREAD=1 diff --git a/src/libpsl-native/gmock_main.pc b/src/libpsl-native/gmock_main.pc new file mode 100644 index 0000000..ad1e97b --- /dev/null +++ b/src/libpsl-native/gmock_main.pc @@ -0,0 +1,9 @@ +libdir=/usr/local/lib +includedir=/usr/local/include + +Name: gmock_main +Description: GoogleMock (with main() function) +Version: 1.9.0 +URL: https://github.com/google/googletest +Libs: -L${libdir} -lgmock_main +Cflags: -I${includedir} -DGTEST_HAS_PTHREAD=1 diff --git a/src/libpsl-native/gtest.pc b/src/libpsl-native/gtest.pc new file mode 100644 index 0000000..c7c84e6 --- /dev/null +++ b/src/libpsl-native/gtest.pc @@ -0,0 +1,9 @@ +libdir=/usr/local/lib +includedir=/usr/local/include + +Name: gtest +Description: GoogleTest (without main() function) +Version: 1.9.0 +URL: https://github.com/google/googletest +Libs: -L${libdir} -lgtest +Cflags: -I${includedir} -DGTEST_HAS_PTHREAD=1 diff --git a/src/libpsl-native/gtest_main.pc b/src/libpsl-native/gtest_main.pc new file mode 100644 index 0000000..4c5d885 --- /dev/null +++ b/src/libpsl-native/gtest_main.pc @@ -0,0 +1,10 @@ +libdir=/usr/local/lib +includedir=/usr/local/include + +Name: gtest_main +Description: GoogleTest (with main() function) +Version: 1.9.0 +URL: https://github.com/google/googletest +Requires: gtest +Libs: -L${libdir} -lgtest_main +Cflags: -I${includedir} -DGTEST_HAS_PTHREAD=1 diff --git a/src/libpsl-native/src/getcommonlstat.cpp b/src/libpsl-native/src/getcommonlstat.cpp index 311b7f1..729335a 100644 --- a/src/libpsl-native/src/getcommonlstat.cpp +++ b/src/libpsl-native/src/getcommonlstat.cpp @@ -34,11 +34,11 @@ int GetCommonLStat(const char* path, struct CommonStat* commonStat) #if defined (__APPLE__) commonStat->AccessTime = st.st_atimespec.tv_sec; commonStat->ModifiedTime = st.st_mtimespec.tv_sec; - commonStat->CreationTime = st.st_ctimespec.tv_sec; + commonStat->ChangeTime = st.st_ctimespec.tv_sec; #else commonStat->AccessTime = st.st_atime; commonStat->ModifiedTime = st.st_mtime; - commonStat->CreationTime = st.st_ctime; + commonStat->ChangeTime = st.st_ctime; #endif commonStat->BlockSize = st.st_blksize; commonStat->DeviceId = st.st_dev; diff --git a/src/libpsl-native/src/getcommonstat.cpp b/src/libpsl-native/src/getcommonstat.cpp index 1fc6cac..53b50c2 100644 --- a/src/libpsl-native/src/getcommonstat.cpp +++ b/src/libpsl-native/src/getcommonstat.cpp @@ -33,11 +33,11 @@ int GetCommonStat(const char* path, struct CommonStat* commonStat) #if defined (__APPLE__) commonStat->AccessTime = st.st_atimespec.tv_sec; commonStat->ModifiedTime = st.st_mtimespec.tv_sec; - commonStat->CreationTime = st.st_ctimespec.tv_sec; + commonStat->ChangeTime = st.st_ctimespec.tv_sec; #else commonStat->AccessTime = st.st_atime; commonStat->ModifiedTime = st.st_mtime; - commonStat->CreationTime = st.st_ctime; + commonStat->ChangeTime = st.st_ctime; #endif commonStat->BlockSize = st.st_blksize; commonStat->DeviceId = st.st_dev; diff --git a/src/libpsl-native/src/getcommonstat.h b/src/libpsl-native/src/getcommonstat.h index cb26875..b8993c6 100644 --- a/src/libpsl-native/src/getcommonstat.h +++ b/src/libpsl-native/src/getcommonstat.h @@ -19,7 +19,7 @@ struct CommonStat long Size; long AccessTime; long ModifiedTime; - long CreationTime; + long ChangeTime; long BlockSize; int DeviceId; int NumberOfBlocks; diff --git a/src/libpsl-native/test/test-getcommonlstat.cpp b/src/libpsl-native/test/test-getcommonlstat.cpp index f2847ad..fca9890 100644 --- a/src/libpsl-native/test/test-getcommonlstat.cpp +++ b/src/libpsl-native/test/test-getcommonlstat.cpp @@ -219,7 +219,7 @@ TEST(GetCommonLStat, GetCTime) pclose(p); GetCommonLStat("/", &cs); EXPECT_EQ(result, 1); - EXPECT_EQ(cTime, cs.CreationTime); + EXPECT_EQ(cTime, cs.ChangeTime); } TEST(GetCommonLStat, Mode001) diff --git a/src/libpsl-native/test/test-getcommonstat.cpp b/src/libpsl-native/test/test-getcommonstat.cpp index 5b81b20..1df6a3d 100644 --- a/src/libpsl-native/test/test-getcommonstat.cpp +++ b/src/libpsl-native/test/test-getcommonstat.cpp @@ -239,7 +239,7 @@ TEST(GetCommonStat, GetCTime) pclose(p); GetCommonStat("/", &cs); EXPECT_EQ(result, 1); - EXPECT_EQ(cTime, cs.CreationTime); + EXPECT_EQ(cTime, cs.ChangeTime); } TEST(GetCommonStat, Mode001) From b0a44b4128afd5e5684c334465c803a886cd8692 Mon Sep 17 00:00:00 2001 From: James Truher Date: Thu, 24 Oct 2019 11:57:19 -0700 Subject: [PATCH 14/18] remove extraneous spaces --- build.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.psm1 b/build.psm1 index a2b289f..a4bce85 100644 --- a/build.psm1 +++ b/build.psm1 @@ -2018,7 +2018,7 @@ function Start-PSBootstrap { } New-Item -Type SymbolicLink -Target $itemPath -Path $linkPath } - + } elseif ($Environment.IsMacOS) { precheck 'brew' "Bootstrap dependency 'brew' not found, must install Homebrew! See http://brew.sh/" From 887d5809bc7a816417963571990d77fc938bdeb1 Mon Sep 17 00:00:00 2001 From: James Truher Date: Thu, 24 Oct 2019 13:40:41 -0700 Subject: [PATCH 15/18] Make sure that stat can be found in /usr/bin On Alpine it seems to be in /bin --- build.psm1 | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/build.psm1 b/build.psm1 index a4bce85..5319c0d 100644 --- a/build.psm1 +++ b/build.psm1 @@ -439,6 +439,19 @@ function Start-BuildNativeUnixBinaries { git clean -qfdX $Native + # the stat tests rely on stat(1). On most platforms, this is /usr/bin/stat, + # but on alpine it is in /bin. Be sure that /usr/bin/stat exists, or + # create a link from /bin/stat. If /bin/stat is missing, we'll have to fail the build + + if ( ! (Test-Path /usr/bin/stat) ) { + if ( Test-Path /bin/stat ) { + New-Item -Type SymbolicLink -Path /usr/bin/stat -Target /bin/stat + } + else { + throw "Cannot create symlink to stat(1)" + } + } + try { Push-Location $Native if ($BuildLinuxArm) { From a8873057b514c167812b8a0e9467a8069556ce82 Mon Sep 17 00:00:00 2001 From: James Truher Date: Thu, 24 Oct 2019 15:22:43 -0700 Subject: [PATCH 16/18] Remove debugging statements Add empty line to end of files where they belong --- src/libpsl-native/src/getcommonlstat.h | 1 + src/libpsl-native/src/getcommonstat.h | 1 + src/libpsl-native/src/getgrgid.cpp | 1 + src/libpsl-native/src/getgrgid.h | 1 + src/libpsl-native/src/getlstat.cpp | 1 + src/libpsl-native/src/getlstat.h | 1 + src/libpsl-native/src/isdirectory.cpp | 1 + src/libpsl-native/test/test-getgrgid.cpp | 3 ++- src/libpsl-native/test/test-getlinkcount.cpp | 5 ----- src/libpsl-native/test/test-getpwuid.cpp | 1 + 10 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/libpsl-native/src/getcommonlstat.h b/src/libpsl-native/src/getcommonlstat.h index dfcf472..de67a92 100644 --- a/src/libpsl-native/src/getcommonlstat.h +++ b/src/libpsl-native/src/getcommonlstat.h @@ -16,3 +16,4 @@ int32_t GetLStat(const char* path, struct stat* buf); int GetCommonLStat(const char* path, CommonStat* cs); PAL_END_EXTERNC + diff --git a/src/libpsl-native/src/getcommonstat.h b/src/libpsl-native/src/getcommonstat.h index b8993c6..9fd9f40 100644 --- a/src/libpsl-native/src/getcommonstat.h +++ b/src/libpsl-native/src/getcommonstat.h @@ -39,3 +39,4 @@ int32_t GetStat(const char* path, struct stat* buf); int GetCommonStat(const char* path, CommonStat* cs); PAL_END_EXTERNC + diff --git a/src/libpsl-native/src/getgrgid.cpp b/src/libpsl-native/src/getgrgid.cpp index aa3fd32..73dd40c 100644 --- a/src/libpsl-native/src/getgrgid.cpp +++ b/src/libpsl-native/src/getgrgid.cpp @@ -65,3 +65,4 @@ char* GetGrGid(gid_t gid) free(buf); return groupname; } + diff --git a/src/libpsl-native/src/getgrgid.h b/src/libpsl-native/src/getgrgid.h index 039b1d7..a2aaa30 100644 --- a/src/libpsl-native/src/getgrgid.h +++ b/src/libpsl-native/src/getgrgid.h @@ -12,3 +12,4 @@ PAL_BEGIN_EXTERNC char* GetGrGid(gid_t gid); PAL_END_EXTERNC + diff --git a/src/libpsl-native/src/getlstat.cpp b/src/libpsl-native/src/getlstat.cpp index 9df33ef..0517851 100644 --- a/src/libpsl-native/src/getlstat.cpp +++ b/src/libpsl-native/src/getlstat.cpp @@ -45,3 +45,4 @@ int32_t GetLStat(const char* path, struct stat* buf) return lstat(path, buf); } + diff --git a/src/libpsl-native/src/getlstat.h b/src/libpsl-native/src/getlstat.h index 4a0604a..7ebe280 100644 --- a/src/libpsl-native/src/getlstat.h +++ b/src/libpsl-native/src/getlstat.h @@ -12,3 +12,4 @@ PAL_BEGIN_EXTERNC int32_t GetLStat(const char* path, struct stat* buf); PAL_END_EXTERNC + diff --git a/src/libpsl-native/src/isdirectory.cpp b/src/libpsl-native/src/isdirectory.cpp index 429d9a7..5afd2b6 100644 --- a/src/libpsl-native/src/isdirectory.cpp +++ b/src/libpsl-native/src/isdirectory.cpp @@ -43,3 +43,4 @@ bool IsDirectory(const char* path) return S_ISDIR(buf.st_mode); } + diff --git a/src/libpsl-native/test/test-getgrgid.cpp b/src/libpsl-native/test/test-getgrgid.cpp index f6304ab..017dd1c 100644 --- a/src/libpsl-native/test/test-getgrgid.cpp +++ b/src/libpsl-native/test/test-getgrgid.cpp @@ -11,4 +11,5 @@ TEST(GetGrGid, Success) { char* expected = getgrgid(getegid())->gr_name; EXPECT_STREQ(GetGrGid(getegid()), expected); -} \ No newline at end of file +} + diff --git a/src/libpsl-native/test/test-getlinkcount.cpp b/src/libpsl-native/test/test-getlinkcount.cpp index 0d3b6b6..d9c388f 100644 --- a/src/libpsl-native/test/test-getlinkcount.cpp +++ b/src/libpsl-native/test/test-getlinkcount.cpp @@ -75,8 +75,6 @@ TEST_F(getLinkCountTest, LinkCountOfSinglyLinkedFile) createFileForTesting(file); int32_t ret = GetLinkCount(file, &count); -printf("001 %s\n", file); - removeFile(file); ASSERT_EQ(0, ret); @@ -90,9 +88,6 @@ TEST_F(getLinkCountTest, LinkCountOfMultiplyLinkedFile) std::string newFile = createHardLink(file); int32_t ret = GetLinkCount(file, &count); -printf("002 %s\n", file); -printf("003 %s\n", newFile.c_str()); - removeFile(file); removeFile(newFile); diff --git a/src/libpsl-native/test/test-getpwuid.cpp b/src/libpsl-native/test/test-getpwuid.cpp index 5c4b52d..563eff8 100644 --- a/src/libpsl-native/test/test-getpwuid.cpp +++ b/src/libpsl-native/test/test-getpwuid.cpp @@ -12,3 +12,4 @@ TEST(GetPwUid, Success) char* expected = getpwuid(geteuid())->pw_name; EXPECT_STREQ(GetPwUid(geteuid()), expected); } + From 773adca317fa78b7c7e7945bc8e9a6a64d312396 Mon Sep 17 00:00:00 2001 From: "James Truher [MSFT]" Date: Mon, 4 Nov 2019 16:16:33 -0800 Subject: [PATCH 17/18] Update src/libpsl-native/src/getgrgid.cpp Co-Authored-By: Ilya --- src/libpsl-native/src/getgrgid.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libpsl-native/src/getgrgid.cpp b/src/libpsl-native/src/getgrgid.cpp index 73dd40c..27329ae 100644 --- a/src/libpsl-native/src/getgrgid.cpp +++ b/src/libpsl-native/src/getgrgid.cpp @@ -53,7 +53,7 @@ char* GetGrGid(gid_t gid) return NULL; } - // no result + // no group found if (result == NULL) { return NULL; From 9ac7b2a01f40d2e79d52291c1b8cb915b1a40bb5 Mon Sep 17 00:00:00 2001 From: James Truher Date: Mon, 4 Nov 2019 16:26:56 -0800 Subject: [PATCH 18/18] Remove extraneous lines --- src/libpsl-native/src/getcommonlstat.cpp | 1 - src/libpsl-native/src/getcommonlstat.h | 1 - 2 files changed, 2 deletions(-) diff --git a/src/libpsl-native/src/getcommonlstat.cpp b/src/libpsl-native/src/getcommonlstat.cpp index 729335a..3aa878d 100644 --- a/src/libpsl-native/src/getcommonlstat.cpp +++ b/src/libpsl-native/src/getcommonlstat.cpp @@ -3,7 +3,6 @@ //! @brief returns the stat of a file - #include #include #include diff --git a/src/libpsl-native/src/getcommonlstat.h b/src/libpsl-native/src/getcommonlstat.h index de67a92..dc2aaba 100644 --- a/src/libpsl-native/src/getcommonlstat.h +++ b/src/libpsl-native/src/getcommonlstat.h @@ -5,7 +5,6 @@ #include "pal.h" - #include #include "getcommonstat.h"