From ed990e78362d4b6067e3c304a61d0387381b9051 Mon Sep 17 00:00:00 2001 From: Joseph Hickey Date: Mon, 28 Dec 2020 12:15:09 -0500 Subject: [PATCH] Fix #697, use POSIX dir implementation on VxWorks6 Instead of maintaining a one-off implementation for VxWorks 6, use the POSIX implementation for this module. The only incompatibility is the prototype for mkdir() which is missing the second argument. This can be worked around with a simple compatibility macro that is only enabled for VxWorks 6.x builds. --- src/os/vxworks/CMakeLists.txt | 15 +- src/os/vxworks/inc/os-impl-dirs.h | 12 ++ src/os/vxworks/src/os-impl-dirs.c | 172 ------------------ .../src/coveragetest-posix-dirs.c} | 44 +++-- .../ut-stubs/inc/OCS_errno.h | 1 + .../ut-stubs/inc/OCS_fcntl.h | 4 + .../ut-stubs/override_inc/errno.h | 1 + .../ut-stubs/override_inc/fcntl.h | 3 + src/unit-test-coverage/vxworks/CMakeLists.txt | 2 +- 9 files changed, 53 insertions(+), 201 deletions(-) delete mode 100644 src/os/vxworks/src/os-impl-dirs.c rename src/unit-test-coverage/{vxworks/src/coveragetest-dirs.c => portable/src/coveragetest-posix-dirs.c} (72%) diff --git a/src/os/vxworks/CMakeLists.txt b/src/os/vxworks/CMakeLists.txt index 9d1cc84d7..912f3e74c 100644 --- a/src/os/vxworks/CMakeLists.txt +++ b/src/os/vxworks/CMakeLists.txt @@ -33,6 +33,7 @@ set(VXWORKS_IMPL_SRCLIST ../portable/os-impl-bsd-select.c ../portable/os-impl-posix-io.c ../portable/os-impl-posix-files.c + ../portable/os-impl-posix-dirs.c ) if (OSAL_CONFIG_INCLUDE_SHELL) @@ -45,15 +46,6 @@ else () ) endif () -if (CMAKE_SYSTEM_VERSION VERSION_GREATER_EQUAL 7.0) - list(APPEND VXWORKS_IMPL_SRCLIST - ../portable/os-impl-posix-dirs.c - ) -else () - list(APPEND VXWORKS_IMPL_SRCLIST - src/os-impl-dirs.c - ) -endif () # If some form of module loading is configured, # then build the module loader if (OSAL_CONFIG_INCLUDE_DYNAMIC_LOADER) @@ -86,3 +78,8 @@ add_library(osal_vxworks_impl OBJECT ${VXWORKS_BASE_SRCLIST} ${VXWORKS_IMPL_SRCLIST} ) + +if (CMAKE_SYSTEM_VERSION VERSION_LESS 7.0) + target_compile_definitions(osal_vxworks_impl PRIVATE OSAL_VXWORKS6_COMPATIBILITY) +endif () + diff --git a/src/os/vxworks/inc/os-impl-dirs.h b/src/os/vxworks/inc/os-impl-dirs.h index 43029aaa1..9dc4447f7 100644 --- a/src/os/vxworks/inc/os-impl-dirs.h +++ b/src/os/vxworks/inc/os-impl-dirs.h @@ -31,8 +31,20 @@ #include "osconfig.h" #include #include +#include #include +/* + * In VxWorks 6.x the system mkdir() function only has a path argument + * In VxWorks 7 it is now POSIX compilant and adds a mode argument + * + * This macro simply discards the second argument, allowing code to use + * mkdir() in a consistent, POSIX compliant fashion. + */ +#ifdef OSAL_VXWORKS6_COMPATIBILITY +#define mkdir(path,mode) mkdir(path) +#endif + typedef struct { DIR *dp; diff --git a/src/os/vxworks/src/os-impl-dirs.c b/src/os/vxworks/src/os-impl-dirs.c deleted file mode 100644 index 4f774e216..000000000 --- a/src/os/vxworks/src/os-impl-dirs.c +++ /dev/null @@ -1,172 +0,0 @@ -/* - * NASA Docket No. GSC-18,370-1, and identified as "Operating System Abstraction Layer" - * - * Copyright (c) 2019 United States Government as represented by - * the Administrator of the National Aeronautics and Space Administration. - * All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * \file os-impl-dirs.c - * \ingroup vxworks - * \author joseph.p.hickey@nasa.gov - * - */ - -/**************************************************************************************** - INCLUDE FILES -****************************************************************************************/ - -#include "os-vxworks.h" -#include "os-impl-dirs.h" -#include "os-shared-dir.h" -#include "os-shared-idmap.h" - -/*---------------------------------------------------------------- - * - * Function: OS_DirCreate_Impl - * - * Purpose: Implemented per internal OSAL API - * See prototype for argument/return detail - * - *-----------------------------------------------------------------*/ -int32 OS_DirCreate_Impl(const char *local_path, uint32 access) -{ - int32 return_code; - - if (mkdir(local_path) != OK) - { - return_code = OS_ERROR; - } - else - { - return_code = OS_SUCCESS; - } - - return return_code; -} /* end OS_DirCreate_Impl */ - -/*---------------------------------------------------------------- - * - * Function: OS_DirOpen_Impl - * - * Purpose: Implemented per internal OSAL API - * See prototype for argument/return detail - * - *-----------------------------------------------------------------*/ -int32 OS_DirOpen_Impl(const OS_object_token_t *token, const char *local_path) -{ - OS_impl_dir_internal_record_t *impl; - - impl = OS_OBJECT_TABLE_GET(OS_impl_dir_table, *token); - - impl->dp = opendir(local_path); - if (impl->dp == NULL) - { - return OS_ERROR; - } - return OS_SUCCESS; -} /* end OS_DirOpen_Impl */ - -/*---------------------------------------------------------------- - * - * Function: OS_DirClose_Impl - * - * Purpose: Implemented per internal OSAL API - * See prototype for argument/return detail - * - *-----------------------------------------------------------------*/ -int32 OS_DirClose_Impl(const OS_object_token_t *token) -{ - OS_impl_dir_internal_record_t *impl; - - impl = OS_OBJECT_TABLE_GET(OS_impl_dir_table, *token); - - closedir(impl->dp); - impl->dp = NULL; - return OS_SUCCESS; -} /* end OS_DirClose_Impl */ - -/*---------------------------------------------------------------- - * - * Function: OS_DirRead_Impl - * - * Purpose: Implemented per internal OSAL API - * See prototype for argument/return detail - * - *-----------------------------------------------------------------*/ -int32 OS_DirRead_Impl(const OS_object_token_t *token, os_dirent_t *dirent) -{ - struct dirent * de; - OS_impl_dir_internal_record_t *impl; - - impl = OS_OBJECT_TABLE_GET(OS_impl_dir_table, *token); - - /* NOTE - the readdir() call is non-reentrant .... - * However, this is performed while the global dir table lock is taken. - * Therefore this ensures that only one such call can occur at any given time. - * - * Static analysis tools may warn about this because they do not know - * this function is externally serialized via the global lock. - */ - /* cppcheck-suppress readdirCalled */ - /* cppcheck-suppress nonreentrantFunctionsreaddir */ - de = readdir(impl->dp); - if (de == NULL) - { - return OS_ERROR; - } - - strncpy(dirent->FileName, de->d_name, sizeof(dirent->FileName) - 1); - dirent->FileName[sizeof(dirent->FileName) - 1] = 0; - - return OS_SUCCESS; -} /* end OS_DirRead_Impl */ - -/*---------------------------------------------------------------- - * - * Function: OS_DirRewind_Impl - * - * Purpose: Implemented per internal OSAL API - * See prototype for argument/return detail - * - *-----------------------------------------------------------------*/ -int32 OS_DirRewind_Impl(const OS_object_token_t *token) -{ - OS_impl_dir_internal_record_t *impl; - - impl = OS_OBJECT_TABLE_GET(OS_impl_dir_table, *token); - - rewinddir(impl->dp); - return OS_SUCCESS; -} /* end OS_DirRewind_Impl */ - -/*---------------------------------------------------------------- - * - * Function: OS_DirRemove_Impl - * - * Purpose: Implemented per internal OSAL API - * See prototype for argument/return detail - * - *-----------------------------------------------------------------*/ -int32 OS_DirRemove_Impl(const char *local_path) -{ - if (rmdir(local_path) < 0) - { - return OS_ERROR; - } - - return OS_SUCCESS; -} /* end OS_DirRemove_Impl */ diff --git a/src/unit-test-coverage/vxworks/src/coveragetest-dirs.c b/src/unit-test-coverage/portable/src/coveragetest-posix-dirs.c similarity index 72% rename from src/unit-test-coverage/vxworks/src/coveragetest-dirs.c rename to src/unit-test-coverage/portable/src/coveragetest-posix-dirs.c index 9fc7c3f4f..371e3b254 100644 --- a/src/unit-test-coverage/vxworks/src/coveragetest-dirs.c +++ b/src/unit-test-coverage/portable/src/coveragetest-posix-dirs.c @@ -19,19 +19,17 @@ */ /** - * \file coveragetest-dirs.c - * \ingroup vxworks + * \file coveragetest-posix-dirs.c * \author joseph.p.hickey@nasa.gov * */ -#include "os-vxworks-coveragetest.h" -#include "ut-adaptor-dirs.h" +#include "os-portable-coveragetest.h" #include "os-shared-dir.h" +#include "os-shared-idmap.h" #include -#include #include #include #include @@ -42,10 +40,10 @@ void Test_OS_DirCreate_Impl(void) * Test Case For: * int32 OS_DirCreate_Impl(const char *local_path, uint32 access) */ - OSAPI_TEST_FUNCTION_RC(OS_DirCreate_Impl("dir", 0), OS_SUCCESS); + OSAPI_TEST_FUNCTION_RC(OS_DirCreate_Impl, ("dir", 0), OS_SUCCESS); UT_SetDefaultReturnValue(UT_KEY(OCS_mkdir), -1); - OSAPI_TEST_FUNCTION_RC(OS_DirCreate_Impl("dir", 0), OS_ERROR); + OSAPI_TEST_FUNCTION_RC(OS_DirCreate_Impl, ("dir", 0), OS_ERROR); } void Test_OS_DirOpen_Impl(void) @@ -54,11 +52,13 @@ void Test_OS_DirOpen_Impl(void) * Test Case For: * int32 OS_DirOpen_Impl(uint32 local_id, const char *local_path) */ - OS_object_token_t token = UT_TOKEN_0; + OS_object_token_t token; - OSAPI_TEST_FUNCTION_RC(OS_DirOpen_Impl(&token, "dir"), OS_SUCCESS); + memset(&token, 0, sizeof(token)); + + OSAPI_TEST_FUNCTION_RC(OS_DirOpen_Impl, (&token, "dir"), OS_SUCCESS); UT_SetDefaultReturnValue(UT_KEY(OCS_opendir), -1); - OSAPI_TEST_FUNCTION_RC(OS_DirOpen_Impl(&token, "dir"), OS_ERROR); + OSAPI_TEST_FUNCTION_RC(OS_DirOpen_Impl, (&token, "dir"), OS_ERROR); } void Test_OS_DirClose_Impl(void) @@ -67,9 +67,11 @@ void Test_OS_DirClose_Impl(void) * Test Case For: * int32 OS_DirClose_Impl(uint32 local_id) */ - OS_object_token_t token = UT_TOKEN_0; + OS_object_token_t token; + + memset(&token, 0, sizeof(token)); - OSAPI_TEST_FUNCTION_RC(OS_DirClose_Impl(&token), OS_SUCCESS); + OSAPI_TEST_FUNCTION_RC(OS_DirClose_Impl, (&token), OS_SUCCESS); } void Test_OS_DirRead_Impl(void) @@ -79,12 +81,14 @@ void Test_OS_DirRead_Impl(void) * int32 OS_DirRead_Impl(uint32 local_id, os_dirent_t *dirent) */ os_dirent_t dirent_buff; - OS_object_token_t token = UT_TOKEN_0; + OS_object_token_t token; - OSAPI_TEST_FUNCTION_RC(OS_DirRead_Impl(&token, &dirent_buff), OS_SUCCESS); + memset(&token, 0, sizeof(token)); + + OSAPI_TEST_FUNCTION_RC(OS_DirRead_Impl, (&token, &dirent_buff), OS_SUCCESS); UT_SetDefaultReturnValue(UT_KEY(OCS_readdir), -1); - OSAPI_TEST_FUNCTION_RC(OS_DirRead_Impl(&token, &dirent_buff), OS_ERROR); + OSAPI_TEST_FUNCTION_RC(OS_DirRead_Impl, (&token, &dirent_buff), OS_ERROR); } void Test_OS_DirRewind_Impl(void) @@ -93,9 +97,11 @@ void Test_OS_DirRewind_Impl(void) * Test Case For: * int32 OS_DirRewind_Impl(uint32 local_id) */ - OS_object_token_t token = UT_TOKEN_0; + OS_object_token_t token; + + memset(&token, 0, sizeof(token)); - OSAPI_TEST_FUNCTION_RC(OS_DirRewind_Impl(&token), OS_SUCCESS); + OSAPI_TEST_FUNCTION_RC(OS_DirRewind_Impl, (&token), OS_SUCCESS); } void Test_OS_DirRemove_Impl(void) @@ -104,10 +110,10 @@ void Test_OS_DirRemove_Impl(void) * Test Case For: * int32 OS_DirRemove_Impl(const char *local_path) */ - OSAPI_TEST_FUNCTION_RC(OS_DirRemove_Impl("dir"), OS_SUCCESS); + OSAPI_TEST_FUNCTION_RC(OS_DirRemove_Impl, ("dir"), OS_SUCCESS); UT_SetDefaultReturnValue(UT_KEY(OCS_rmdir), -1); - OSAPI_TEST_FUNCTION_RC(OS_DirRemove_Impl("dir"), OS_ERROR); + OSAPI_TEST_FUNCTION_RC(OS_DirRemove_Impl, ("dir"), OS_ERROR); } /* ------------------- End of test cases --------------------------------------*/ diff --git a/src/unit-test-coverage/ut-stubs/inc/OCS_errno.h b/src/unit-test-coverage/ut-stubs/inc/OCS_errno.h index 8d7d89b93..1e0fbdbed 100644 --- a/src/unit-test-coverage/ut-stubs/inc/OCS_errno.h +++ b/src/unit-test-coverage/ut-stubs/inc/OCS_errno.h @@ -30,6 +30,7 @@ /* POSIX-specific errnos */ #define OCS_EINTR 0x1801 #define OCS_EAGAIN 0x1802 +#define OCS_EEXIST 0x180a #define OCS_EINVAL 0x1803 #define OCS_EMSGSIZE 0x1804 #define OCS_ETIMEDOUT 0x1805 diff --git a/src/unit-test-coverage/ut-stubs/inc/OCS_fcntl.h b/src/unit-test-coverage/ut-stubs/inc/OCS_fcntl.h index 96bc3cdb1..48408b35c 100644 --- a/src/unit-test-coverage/ut-stubs/inc/OCS_fcntl.h +++ b/src/unit-test-coverage/ut-stubs/inc/OCS_fcntl.h @@ -38,6 +38,10 @@ * * These are exposed to apps through fcntl.h and sys/stat.h */ +#define OCS_S_IRWXO 0x1110 +#define OCS_S_IRWXG 0x2220 +#define OCS_S_IRWXU 0x4440 + #define OCS_S_IXOTH 0x1000 #define OCS_S_IXGRP 0x2000 #define OCS_S_IXUSR 0x4000 diff --git a/src/unit-test-coverage/ut-stubs/override_inc/errno.h b/src/unit-test-coverage/ut-stubs/override_inc/errno.h index 636ac2a34..b69c4cbfb 100644 --- a/src/unit-test-coverage/ut-stubs/override_inc/errno.h +++ b/src/unit-test-coverage/ut-stubs/override_inc/errno.h @@ -30,6 +30,7 @@ #define EINTR OCS_EINTR #define EAGAIN OCS_EAGAIN #define EINVAL OCS_EINVAL +#define EEXIST OCS_EEXIST #define EMSGSIZE OCS_EMSGSIZE #define ETIMEDOUT OCS_ETIMEDOUT #define ESPIPE OCS_ESPIPE diff --git a/src/unit-test-coverage/ut-stubs/override_inc/fcntl.h b/src/unit-test-coverage/ut-stubs/override_inc/fcntl.h index 8b0f2f289..50b359062 100644 --- a/src/unit-test-coverage/ut-stubs/override_inc/fcntl.h +++ b/src/unit-test-coverage/ut-stubs/override_inc/fcntl.h @@ -27,6 +27,9 @@ /* ----------------------------------------- */ /* mappings for declarations in fcntl.h */ /* ----------------------------------------- */ +#define S_IRWXU OCS_S_IRWXU +#define S_IRWXG OCS_S_IRWXG +#define S_IRWXO OCS_S_IRWXO #define S_IXOTH OCS_S_IXOTH #define S_IXGRP OCS_S_IXGRP #define S_IXUSR OCS_S_IXUSR diff --git a/src/unit-test-coverage/vxworks/CMakeLists.txt b/src/unit-test-coverage/vxworks/CMakeLists.txt index afcf47eb1..5bc877276 100644 --- a/src/unit-test-coverage/vxworks/CMakeLists.txt +++ b/src/unit-test-coverage/vxworks/CMakeLists.txt @@ -5,7 +5,6 @@ set(VXWORKS_MODULE_LIST common console countsem - dirs dirs-globals files filesys @@ -25,6 +24,7 @@ set(VXWORKS_PORTABLE_BLOCK_LIST posix-gettime posix-io posix-files + posix-dirs console-bsp bsd-select