diff --git a/level_zero/tools/source/sysman/linux/fs_access.cpp b/level_zero/tools/source/sysman/linux/fs_access.cpp index 095fa05f92..5c0e9d934b 100644 --- a/level_zero/tools/source/sysman/linux/fs_access.cpp +++ b/level_zero/tools/source/sysman/linux/fs_access.cpp @@ -278,12 +278,10 @@ bool FsAccess::isRootUser() { } bool FsAccess::directoryExists(const std::string path) { - ::DIR *pDir = ::opendir(path.c_str()); - if (pDir != NULL) { - ::closedir(pDir); - return true; + if (accessSyscall(path.c_str(), F_OK)) { + return false; } - return false; + return true; } // Procfs Access @@ -564,7 +562,7 @@ bool SysfsAccess::fileExists(const std::string file) { } bool SysfsAccess::directoryExists(const std::string path) { - return FsAccess::fileExists(fullPath(path).c_str()); + return FsAccess::directoryExists(fullPath(path).c_str()); } bool SysfsAccess::isMyDeviceFile(const std::string dev) { diff --git a/level_zero/tools/source/sysman/linux/fs_access.h b/level_zero/tools/source/sysman/linux/fs_access.h index 238be464d8..3e920de00f 100644 --- a/level_zero/tools/source/sysman/linux/fs_access.h +++ b/level_zero/tools/source/sysman/linux/fs_access.h @@ -7,6 +7,8 @@ #pragma once +#include "shared/source/os_interface/linux/sys_calls.h" + #include "level_zero/ze_api.h" #include "level_zero/zet_api.h" @@ -52,6 +54,7 @@ class FsAccess { protected: FsAccess(); + decltype(&NEO::SysCalls::access) accessSyscall = NEO::SysCalls::access; }; class ProcfsAccess : private FsAccess { @@ -77,7 +80,7 @@ class ProcfsAccess : private FsAccess { static const std::string fdDir; }; -class SysfsAccess : private FsAccess { +class SysfsAccess : protected FsAccess { public: static SysfsAccess *create(const std::string file); SysfsAccess() = default; diff --git a/level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h b/level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h index 5c92ddca2c..b24f0e3dc9 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h +++ b/level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h @@ -96,5 +96,15 @@ class SysmanMultiDeviceFixture : public MultiDeviceFixture, public ::testing::Te uint32_t subDeviceCount = 0u; }; +class PublicFsAccess : public L0::FsAccess { + public: + using FsAccess::accessSyscall; +}; + +class PublicSysfsAccess : public L0::SysfsAccess { + public: + using SysfsAccess::accessSyscall; +}; + } // namespace ult } // namespace L0 diff --git a/level_zero/tools/test/unit_tests/sources/sysman/linux/test_sysman.cpp b/level_zero/tools/test/unit_tests/sources/sysman/linux/test_sysman.cpp index 68b5cf7443..5042311ac0 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/linux/test_sysman.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/linux/test_sysman.cpp @@ -12,6 +12,14 @@ namespace L0 { namespace ult { +inline static int mockAccessFailure(const char *pathname, int mode) { + return -1; +} + +inline static int mockAccessSuccess(const char *pathname, int mode) { + return 0; +} + using MockDeviceSysmanGetTest = Test; TEST_F(MockDeviceSysmanGetTest, GivenValidSysmanHandleSetInDeviceStructWhenGetThisSysmanHandleThenHandlesShouldBeSimilar) { SysmanDeviceImp *sysman = new SysmanDeviceImp(device->toHandle()); @@ -41,21 +49,24 @@ TEST_F(SysmanDeviceFixture, GivenCreateFsAccessHandleWhenCallinggetFsAccessThenC EXPECT_EQ(&pLinuxSysmanImp->getFsAccess(), pLinuxSysmanImp->pFsAccess); } -TEST_F(SysmanDeviceFixture, GivenCreateFsAccessHandleWhenCallingdirectoryExistsWithDifferentPathsThenDesiredResultsAreObtained) { - auto FsAccess = FsAccess::create(); +TEST_F(SysmanDeviceFixture, GivenPublicFsAccessClassWhenCallingDirectoryExistsWithValidAndInvalidPathThenSuccessAndFailureAreReturnedRespectively) { + PublicFsAccess *tempFsAccess = new PublicFsAccess(); + tempFsAccess->accessSyscall = mockAccessSuccess; char cwd[PATH_MAX]; std::string path = getcwd(cwd, PATH_MAX); - EXPECT_TRUE(FsAccess->directoryExists(path)); + EXPECT_TRUE(tempFsAccess->directoryExists(path)); + tempFsAccess->accessSyscall = mockAccessFailure; path = "invalidDiretory"; - EXPECT_FALSE(FsAccess->directoryExists(path)); - delete FsAccess; + EXPECT_FALSE(tempFsAccess->directoryExists(path)); + delete tempFsAccess; } -TEST_F(SysmanDeviceFixture, GivenCreateSysfsAccessHandleWhenCallingDirectoryExistsWithInvalidPathThenFalseIsRetured) { - auto SysfsAccess = SysfsAccess::create(""); +TEST_F(SysmanDeviceFixture, GivenPublicSysfsAccessClassWhenCallingDirectoryExistsWithInvalidPathThenFalseIsRetured) { + PublicFsAccess *tempSysfsAccess = new PublicFsAccess(); + tempSysfsAccess->accessSyscall = mockAccessFailure; std::string path = "invalidDiretory"; - EXPECT_FALSE(SysfsAccess->directoryExists(path)); - delete SysfsAccess; + EXPECT_FALSE(tempSysfsAccess->directoryExists(path)); + delete tempSysfsAccess; } TEST_F(SysmanDeviceFixture, GivenValidPathnameWhenCallingFsAccessExistsThenSuccessIsReturned) {