This is an automated email from the ASF dual-hosted git repository.

pengzheng pushed a commit to branch feature/refactor_bundle_cache
in repository https://gitbox.apache.org/repos/asf/celix.git


The following commit(s) were added to refs/heads/feature/refactor_bundle_cache 
by this push:
     new de7f5c76 Embed libzip error code into Celix error code.
de7f5c76 is described below

commit de7f5c760545fdd86bba34565a9cc862509ffa38
Author: PengZheng <[email protected]>
AuthorDate: Tue Mar 28 12:04:05 2023 +0800

    Embed libzip error code into Celix error code.
    
    It fixes #502.
---
 libs/error_injector/celix_utils/CMakeLists.txt     |  3 ++
 .../celix_utils/include/celix_utils_ei.h           |  6 +++
 .../celix_utils/src/celix_utils_ei.cc              | 35 +++++++++++-
 libs/framework/gtest/CMakeLists.txt                |  2 +
 .../BundleArchiveWithErrorInjectionTestSuite.cc    |  2 +
 .../CelixFrameworkUtilsErrorInjectionTestSuite.cc  | 62 ++++++++++++++++++++++
 libs/framework/src/celix_framework_utils.c         | 20 +++----
 libs/utils/include/celix_errno.h                   |  6 +++
 libs/utils/src/celix_file_utils.c                  | 17 +++---
 9 files changed, 134 insertions(+), 19 deletions(-)

diff --git a/libs/error_injector/celix_utils/CMakeLists.txt 
b/libs/error_injector/celix_utils/CMakeLists.txt
index 2aaa1dde..259d5397 100644
--- a/libs/error_injector/celix_utils/CMakeLists.txt
+++ b/libs/error_injector/celix_utils/CMakeLists.txt
@@ -25,5 +25,8 @@ target_link_options(utils_ei INTERFACE
         LINKER:--wrap,celix_utils_createDirectory
         LINKER:--wrap,celix_utils_getLastModified
         LINKER:--wrap,celix_utils_deleteDirectory
+        LINKER:--wrap,celix_utils_writeOrCreateString
+        LINKER:--wrap,celix_utils_extractZipData
+        LINKER:--wrap,celix_utils_extractZipFile
 )
 add_library(Celix::utils_ei ALIAS utils_ei)
diff --git a/libs/error_injector/celix_utils/include/celix_utils_ei.h 
b/libs/error_injector/celix_utils/include/celix_utils_ei.h
index 8ca86158..303ee08f 100644
--- a/libs/error_injector/celix_utils/include/celix_utils_ei.h
+++ b/libs/error_injector/celix_utils/include/celix_utils_ei.h
@@ -35,6 +35,12 @@ CELIX_EI_DECLARE(celix_utils_getLastModified, 
celix_status_t);
 
 CELIX_EI_DECLARE(celix_utils_deleteDirectory, celix_status_t);
 
+CELIX_EI_DECLARE(celix_utils_writeOrCreateString, char *);
+
+CELIX_EI_DECLARE(celix_utils_extractZipData, celix_status_t);
+
+CELIX_EI_DECLARE(celix_utils_extractZipFile, celix_status_t);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/libs/error_injector/celix_utils/src/celix_utils_ei.cc 
b/libs/error_injector/celix_utils/src/celix_utils_ei.cc
index 85645667..d073000a 100644
--- a/libs/error_injector/celix_utils/src/celix_utils_ei.cc
+++ b/libs/error_injector/celix_utils/src/celix_utils_ei.cc
@@ -33,7 +33,7 @@ celix_status_t __real_celix_utils_createDirectory(const char* 
path, bool failIfP
 CELIX_EI_DEFINE(celix_utils_createDirectory, celix_status_t)
 celix_status_t __wrap_celix_utils_createDirectory(const char* path, bool 
failIfPresent, const char** errorOut) {
     if (errorOut) {
-        *errorOut = nullptr;
+        *errorOut = "Error Injected";
     }
     CELIX_EI_IMPL(celix_utils_createDirectory);
     return __real_celix_utils_createDirectory(path, failIfPresent, errorOut);
@@ -50,10 +50,41 @@ celix_status_t __real_celix_utils_deleteDirectory(const 
char* path, const char**
 CELIX_EI_DEFINE(celix_utils_deleteDirectory, celix_status_t)
 celix_status_t __wrap_celix_utils_deleteDirectory(const char* path, const 
char** errorOut) {
     if (errorOut) {
-        *errorOut = nullptr;
+        *errorOut = "Error Injected";
     }
     CELIX_EI_IMPL(celix_utils_deleteDirectory);
     return __real_celix_utils_deleteDirectory(path, errorOut);
 }
 
+CELIX_EI_DEFINE(celix_utils_writeOrCreateString, char *)
+char* __wrap_celix_utils_writeOrCreateString(char* buffer, size_t bufferSize, 
const char* format, ...) {
+    CELIX_EI_IMPL(celix_utils_writeOrCreateString);
+    va_list args;
+    va_start(args, format);
+    char *ret = celix_utils_writeOrCreateVString(buffer, bufferSize, format, 
args);
+    va_end(args);
+    return ret;
 }
+
+
+celix_status_t __real_celix_utils_extractZipData(const void *zipData, size_t 
zipDataSize, const char* extractToDir, const char** errorOut);
+CELIX_EI_DEFINE(celix_utils_extractZipData, celix_status_t)
+celix_status_t __wrap_celix_utils_extractZipData(const void *zipData, size_t 
zipDataSize, const char* extractToDir, const char** errorOut) {
+    if (errorOut) {
+        *errorOut = "Error Injected";
+    }
+    CELIX_EI_IMPL(celix_utils_extractZipData);
+    return __real_celix_utils_extractZipData(zipData, zipDataSize, 
extractToDir, errorOut);
+}
+
+celix_status_t __real_celix_utils_extractZipFile(const char *zipFilePath, 
const char* extractToDir, const char** errorOut);
+CELIX_EI_DEFINE(celix_utils_extractZipFile, celix_status_t)
+celix_status_t __wrap_celix_utils_extractZipFile(const char *zipFilePath, 
const char* extractToDir, const char** errorOut) {
+    if (errorOut) {
+        *errorOut = "Error Injected";
+    }
+    CELIX_EI_IMPL(celix_utils_extractZipFile);
+    return __real_celix_utils_extractZipFile(zipFilePath, extractToDir, 
errorOut);
+}
+
+}
\ No newline at end of file
diff --git a/libs/framework/gtest/CMakeLists.txt 
b/libs/framework/gtest/CMakeLists.txt
index 93da09c8..2bbe5f27 100644
--- a/libs/framework/gtest/CMakeLists.txt
+++ b/libs/framework/gtest/CMakeLists.txt
@@ -117,12 +117,14 @@ setup_target_for_coverage(test_framework SCAN_DIR ..)
 if (LINKER_WRAP_SUPPORTED)
     add_executable(test_framework_with_ei
             src/BundleArchiveWithErrorInjectionTestSuite.cc
+            src/CelixFrameworkUtilsErrorInjectionTestSuite.cc
     )
     target_compile_definitions(test_framework_with_ei PRIVATE
             SIMPLE_TEST_BUNDLE1_LOCATION="${SIMPLE_TEST_BUNDLE1}"
     )
     target_include_directories(test_framework_with_ei PRIVATE ../src)
     add_celix_bundle_dependencies(test_framework_with_ei simple_test_bundle1)
+    celix_target_embedded_bundles(test_framework_with_ei simple_test_bundle1)
     celix_deprecated_utils_headers(test_framework_with_ei)
     target_link_libraries(test_framework_with_ei PRIVATE
             Celix::framework_obj
diff --git 
a/libs/framework/gtest/src/BundleArchiveWithErrorInjectionTestSuite.cc 
b/libs/framework/gtest/src/BundleArchiveWithErrorInjectionTestSuite.cc
index d08a6364..e2bb9559 100644
--- a/libs/framework/gtest/src/BundleArchiveWithErrorInjectionTestSuite.cc
+++ b/libs/framework/gtest/src/BundleArchiveWithErrorInjectionTestSuite.cc
@@ -54,6 +54,8 @@ public:
         celix_ei_expect_celix_utils_createDirectory(nullptr, 0, CELIX_SUCCESS);
         celix_ei_expect_celix_utils_getLastModified(nullptr, 0, CELIX_SUCCESS);
         celix_ei_expect_celix_utils_deleteDirectory(nullptr, 0, CELIX_SUCCESS);
+        celix_ei_expect_celix_utils_writeOrCreateString(nullptr, 0, nullptr);
+        celix_ei_expect_celix_utils_extractZipData(nullptr, 0, CELIX_SUCCESS);
     }
 
     void installBundleAndExpectFailure() {
diff --git 
a/libs/framework/gtest/src/CelixFrameworkUtilsErrorInjectionTestSuite.cc 
b/libs/framework/gtest/src/CelixFrameworkUtilsErrorInjectionTestSuite.cc
new file mode 100644
index 00000000..1d1f8b3c
--- /dev/null
+++ b/libs/framework/gtest/src/CelixFrameworkUtilsErrorInjectionTestSuite.cc
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+#include <gtest/gtest.h>
+
+#include <dirent.h>
+
+#include "celix/FrameworkFactory.h"
+#include "celix/FrameworkUtils.h"
+
+#include "celix_framework_utils_private.h"
+#include "celix_file_utils.h"
+#include "celix_utils_ei.h"
+
+class CelixFrameworkUtilsErrorInjectionTestSuite : public ::testing::Test {
+public:
+    CelixFrameworkUtilsErrorInjectionTestSuite () {
+        framework = 
celix::createFramework({{"CELIX_LOGGING_DEFAULT_ACTIVE_LOG_LEVEL", "trace"}});
+    }
+
+    ~CelixFrameworkUtilsErrorInjectionTestSuite () override {
+        celix_ei_expect_celix_utils_extractZipData(nullptr, 0, CELIX_SUCCESS);
+        celix_ei_expect_celix_utils_extractZipFile(nullptr, 0, CELIX_SUCCESS);
+    }
+    std::shared_ptr<celix::Framework> framework{};
+};
+
+TEST_F(CelixFrameworkUtilsErrorInjectionTestSuite, testExtractEmbeddedBundle) {
+    const char* testExtractDir = "extractEmbeddedBundleTestDir";
+    celix_utils_deleteDirectory(testExtractDir, nullptr);
+
+    celix_ei_expect_celix_utils_extractZipData(CELIX_EI_UNKNOWN_CALLER, 0, 
CELIX_BUNDLE_EXCEPTION);
+    auto status = 
celix_framework_utils_extractBundle(framework->getCFramework(), 
"embedded://simple_test_bundle1", testExtractDir);
+    EXPECT_EQ(status, CELIX_BUNDLE_EXCEPTION);
+    celix_utils_deleteDirectory(testExtractDir, nullptr);
+}
+
+TEST_F(CelixFrameworkUtilsErrorInjectionTestSuite, testExtractFileBundle) {
+    const char* testExtractDir = "extractFileBundleTestDir";
+    celix_utils_deleteDirectory(testExtractDir, nullptr);
+
+    celix_ei_expect_celix_utils_extractZipFile(CELIX_EI_UNKNOWN_CALLER, 0, 
CELIX_ENOMEM);
+    auto status = 
celix_framework_utils_extractBundle(framework->getCFramework(), 
SIMPLE_TEST_BUNDLE1_LOCATION, testExtractDir);
+    EXPECT_EQ(status, CELIX_ENOMEM);
+    celix_utils_deleteDirectory(testExtractDir, nullptr);
+}
\ No newline at end of file
diff --git a/libs/framework/src/celix_framework_utils.c 
b/libs/framework/src/celix_framework_utils.c
index 76ec207b..ccbf2077 100644
--- a/libs/framework/src/celix_framework_utils.c
+++ b/libs/framework/src/celix_framework_utils.c
@@ -161,7 +161,7 @@ bool 
celix_framework_utils_isBundleUrlNewerThan(celix_framework_t* fw, const cha
     return newer;
 }
 
-static bool celix_framework_utils_extractBundlePath(celix_framework_t *fw, 
const char* bundlePath, const char* extractPath) {
+static celix_status_t 
celix_framework_utils_extractBundlePath(celix_framework_t *fw, const char* 
bundlePath, const char* extractPath) {
     FW_LOG(CELIX_LOG_LEVEL_TRACE, "Extracting bundle url `%s` to dir `%s`", 
bundlePath, extractPath);
     const char* err = NULL;
 
@@ -171,10 +171,10 @@ static bool 
celix_framework_utils_extractBundlePath(celix_framework_t *fw, const
     celix_status_t status = celix_utils_extractZipFile(resolvedPath, 
extractPath, &err);
     framework_logIfError(fw->logger, status, err, "Could not extract bundle 
zip file `%s` to `%s`", resolvedPath, extractPath);
     celix_utils_freeStringIfNotEqual(buffer, resolvedPath);
-    return status == CELIX_SUCCESS;
+    return status;
 }
 
-static bool celix_framework_utils_extractBundleEmbedded(celix_framework_t *fw, 
const char* embeddedBundle, const char* extractPath) {
+static celix_status_t 
celix_framework_utils_extractBundleEmbedded(celix_framework_t *fw, const char* 
embeddedBundle, const char* extractPath) {
     FW_LOG(CELIX_LOG_LEVEL_TRACE, "Extracting embedded bundle `%s` to dir 
`%s`", embeddedBundle, extractPath);
     char* startSymbol = NULL;
     char* endSymbol = NULL;
@@ -190,7 +190,7 @@ static bool 
celix_framework_utils_extractBundleEmbedded(celix_framework_t *fw, c
         FW_LOG(CELIX_LOG_LEVEL_ERROR, "Cannot extract embedded bundle, could 
not find symbols `%s` and/or `%s` for embedded bundle `%s`", startSymbol, 
endSymbol, embeddedBundle);
         free(startSymbol);
         free(endSymbol);
-        return false;
+        return CELIX_ILLEGAL_ARGUMENT;
     }
     free(startSymbol);
     free(endSymbol);
@@ -201,7 +201,7 @@ static bool 
celix_framework_utils_extractBundleEmbedded(celix_framework_t *fw, c
         FW_LOG(CELIX_LOG_LEVEL_TRACE, "Embedded bundle zip `%s` extracted to 
`%s`", embeddedBundle, extractPath);
     }
     framework_logIfError(fw->logger, status, err, "Could not extract embedded 
bundle zip `%s` to `%s`", embeddedBundle, extractPath);
-    return status == CELIX_SUCCESS;
+    return status;
 }
 
 celix_status_t celix_framework_utils_extractBundle(celix_framework_t *fw, 
const char *bundleURL,  const char* extractPath) {
@@ -210,19 +210,19 @@ celix_status_t 
celix_framework_utils_extractBundle(celix_framework_t *fw, const
     }
     char* trimmedUrl = celix_utils_trim(bundleURL);
 
-    bool extracted;
+    celix_status_t status;
     size_t fileSchemeLen = sizeof(FILE_URL_SCHEME)-1;
     size_t embeddedSchemeLen = sizeof(EMBEDDED_URL_SCHEME)-1;
     if (strncasecmp(FILE_URL_SCHEME, trimmedUrl, fileSchemeLen) == 0) {
-        extracted = celix_framework_utils_extractBundlePath(fw, trimmedUrl + 
fileSchemeLen, extractPath);
+        status = celix_framework_utils_extractBundlePath(fw, trimmedUrl + 
fileSchemeLen, extractPath);
     } else if (strncasecmp(EMBEDDED_URL_SCHEME, trimmedUrl, embeddedSchemeLen) 
== 0) {
-        extracted = celix_framework_utils_extractBundleEmbedded(fw, trimmedUrl 
+ embeddedSchemeLen, extractPath);
+        status = celix_framework_utils_extractBundleEmbedded(fw, trimmedUrl + 
embeddedSchemeLen, extractPath);
     } else {
-        extracted = celix_framework_utils_extractBundlePath(fw, trimmedUrl, 
extractPath);
+        status = celix_framework_utils_extractBundlePath(fw, trimmedUrl, 
extractPath);
     }
 
     free(trimmedUrl);
-    return extracted ? CELIX_SUCCESS : CELIX_FILE_IO_EXCEPTION;
+    return status;
 }
 
 bool celix_framework_utils_isBundleUrlValid(celix_framework_t *fw, const char 
*bundleURL, bool silent) {
diff --git a/libs/utils/include/celix_errno.h b/libs/utils/include/celix_errno.h
index 98fd6bfd..1fd0d6bf 100644
--- a/libs/utils/include/celix_errno.h
+++ b/libs/utils/include/celix_errno.h
@@ -95,6 +95,12 @@ const char* celix_strerror(celix_status_t status);
  */
 #define CELIX_FACILITY_HTTP 2
 
+/*!
+ * The facility of the libzip error code
+ *
+ */
+#define CELIX_FACILITY_ZIP 3
+
 /*!
  * Make the error code accroding to the specification
  * \param fac Facility
diff --git a/libs/utils/src/celix_file_utils.c 
b/libs/utils/src/celix_file_utils.c
index c930618c..945d7fe5 100644
--- a/libs/utils/src/celix_file_utils.c
+++ b/libs/utils/src/celix_file_utils.c
@@ -34,6 +34,8 @@ static const char * const DIRECTORY_ALREADY_EXISTS_ERROR = 
"Directory already ex
 static const char * const FILE_ALREADY_EXISTS_AND_NOT_DIR_ERROR = "File 
already exists, but is not a directory.";
 static const char * const CANNOT_DELETE_DIRECTORY_PATH_IS_FILE = "Cannot 
delete directory; Path points to a file.";
 static const char * const ERROR_OPENING_ZIP = "Error opening zip file.";
+static const char * const ERROR_OPENING_FILE_ZIP = "Error opening file in 
zip.";
+static const char * const ERROR_READING_FILE_ZIP = "Error reading file in 
zip.";
 
 bool celix_utils_fileExists(const char* path) {
     struct stat st;
@@ -197,8 +199,8 @@ static celix_status_t celix_utils_extractZipInternal(zip_t 
*zip, const char* ext
             //file
             zip_file_t *zf = zip_fopen_index(zip, i, 0);
             if (!zf) {
-                status = CELIX_FILE_IO_EXCEPTION;
-                *errorOut = zip_strerror(zip);
+                status = CELIX_ERROR_MAKE(CELIX_FACILITY_ZIP, 
zip_error_code_zip(zip_get_error(zip)));
+                *errorOut = ERROR_OPENING_FILE_ZIP;
             } else {
                 FILE* f = fopen(path, "w+");
                 if (f) {
@@ -208,12 +210,13 @@ static celix_status_t 
celix_utils_extractZipInternal(zip_t *zip, const char* ext
                         read = zip_fread(zf, buf, bufSize);
                     }
                     if (read < 0) {
-                        status = CELIX_FILE_IO_EXCEPTION;
-                        *errorOut = zip_file_strerror(zf);
+
+                        status = CELIX_ERROR_MAKE(CELIX_FACILITY_ZIP, 
zip_error_code_zip(zip_file_get_error(zf)));
+                        *errorOut = ERROR_READING_FILE_ZIP;
                     }
                     fclose(f);
                 } else {
-                    status = CELIX_FILE_IO_EXCEPTION;
+                    status = CELIX_ERROR_MAKE(CELIX_FACILITY_CERRNO,errno);
                     *errorOut = strerror(errno);
                 }
                 zip_fclose(zf);
@@ -242,7 +245,7 @@ celix_status_t celix_utils_extractZipFile(const char* 
zipPath, const char* extra
         zip_close(zip);
     } else {
         //note libzip can give more info with zip_error_to_str if needed (but 
this requires an allocated string buf).
-        status = CELIX_FILE_IO_EXCEPTION;
+        status = CELIX_ERROR_MAKE(CELIX_FACILITY_ZIP, error);
         *errorOut = ERROR_OPENING_ZIP;
     }
 
@@ -270,7 +273,7 @@ celix_status_t celix_utils_extractZipData(const void 
*zipData, size_t zipDataSiz
     }
 
     if (source == NULL || zip == NULL) {
-        status = CELIX_FILE_IO_EXCEPTION;
+        status = CELIX_ERROR_MAKE(CELIX_FACILITY_ZIP, 
zip_error_code_zip(&zipError));
         *errorOut = ERROR_OPENING_ZIP;
     }
     if (zip != NULL) {

Reply via email to