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

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

commit 96b22b7d230c0282b118d534d96d4b655ff05ff7
Author: PengZheng <[email protected]>
AuthorDate: Sat May 27 18:50:05 2023 +0800

    Add tests for system bundle archive creation failures.
    
    Logging in celix_bundleArchive_create is also improved.
---
 libs/error_injector/CMakeLists.txt                 |   1 +
 libs/error_injector/unistd/CMakeLists.txt          |  24 +++++
 libs/error_injector/unistd/include/unistd_ei.h     |  35 +++++++
 libs/error_injector/unistd/src/unistd_ei.cc        |  33 +++++++
 libs/framework/gtest/CMakeLists.txt                |   3 +-
 .../src/CelixBundleCacheErrorInjectionTestSuite.cc | 107 ++++++++++++++-------
 libs/framework/src/bundle_archive.c                |  92 +++++++++---------
 7 files changed, 210 insertions(+), 85 deletions(-)

diff --git a/libs/error_injector/CMakeLists.txt 
b/libs/error_injector/CMakeLists.txt
index 309842cd..73ec6176 100644
--- a/libs/error_injector/CMakeLists.txt
+++ b/libs/error_injector/CMakeLists.txt
@@ -45,6 +45,7 @@ add_subdirectory(celix_bundle)
 add_subdirectory(celix_version)
 add_subdirectory(celix_hash_map)
 add_subdirectory(celix_long_hash_map)
+add_subdirectory(unistd)
 
 if (BUILD_CELIX_DFI)
     add_subdirectory(dfi)
diff --git a/libs/error_injector/unistd/CMakeLists.txt 
b/libs/error_injector/unistd/CMakeLists.txt
new file mode 100644
index 00000000..1576efda
--- /dev/null
+++ b/libs/error_injector/unistd/CMakeLists.txt
@@ -0,0 +1,24 @@
+# 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.
+
+add_library(unistd_ei STATIC src/unistd_ei.cc)
+
+target_include_directories(unistd_ei PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
+target_link_libraries(unistd_ei PUBLIC Celix::error_injector)
+
+target_link_options(unistd_ei INTERFACE LINKER:--wrap,getcwd)
+add_library(Celix::unistd_ei ALIAS unistd_ei)
diff --git a/libs/error_injector/unistd/include/unistd_ei.h 
b/libs/error_injector/unistd/include/unistd_ei.h
new file mode 100644
index 00000000..b10f6e2c
--- /dev/null
+++ b/libs/error_injector/unistd/include/unistd_ei.h
@@ -0,0 +1,35 @@
+/*
+ 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.
+ */
+
+#ifndef CELIX_UNISTD_EI_H
+#define CELIX_UNISTD_EI_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <unistd.h>
+
+#include "celix_error_injector.h"
+
+CELIX_EI_DECLARE(getcwd, char*);
+
+#ifdef __cplusplus
+}
+#endif
+#endif // CELIX_UNISTD_EI_H
diff --git a/libs/error_injector/unistd/src/unistd_ei.cc 
b/libs/error_injector/unistd/src/unistd_ei.cc
new file mode 100644
index 00000000..e1c2680e
--- /dev/null
+++ b/libs/error_injector/unistd/src/unistd_ei.cc
@@ -0,0 +1,33 @@
+/*
+ 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 "unistd_ei.h"
+
+#include <errno.h>
+
+extern "C" {
+char* __real_getcwd(char* buf, size_t size);
+CELIX_EI_DEFINE(getcwd, char*)
+char* __wrap_getcwd(char* buf, size_t size) {
+    errno = ENOMEM;
+    CELIX_EI_IMPL(getcwd);
+    errno = 0;
+    return __real_getcwd(buf, size);
+}
+}
diff --git a/libs/framework/gtest/CMakeLists.txt 
b/libs/framework/gtest/CMakeLists.txt
index d98a9b37..3cc35fb5 100644
--- a/libs/framework/gtest/CMakeLists.txt
+++ b/libs/framework/gtest/CMakeLists.txt
@@ -129,7 +129,7 @@ if (LINKER_WRAP_SUPPORTED)
             src/CelixFrameworkUtilsErrorInjectionTestSuite.cc
             src/CelixBundleContextBundlesWithErrorTestSuite.cc
             src/CelixBundleCacheErrorInjectionTestSuite.cc
-    )
+            )
     target_compile_definitions(test_framework_with_ei PRIVATE
             SIMPLE_TEST_BUNDLE1_LOCATION="${SIMPLE_TEST_BUNDLE1}"
             SIMPLE_CXX_BUNDLE_LOC="${SIMPLE_CXX_BUNDLE_LOC}"
@@ -144,6 +144,7 @@ if (LINKER_WRAP_SUPPORTED)
             Celix::utils_ei
             Celix::asprintf_ei
             Celix::dlfcn_ei
+            Celix::unistd_ei
             Celix::hash_map_ei
             GTest::gtest GTest::gtest_main
     )
diff --git 
a/libs/framework/gtest/src/CelixBundleCacheErrorInjectionTestSuite.cc 
b/libs/framework/gtest/src/CelixBundleCacheErrorInjectionTestSuite.cc
index ecd39064..fc8ddf83 100644
--- a/libs/framework/gtest/src/CelixBundleCacheErrorInjectionTestSuite.cc
+++ b/libs/framework/gtest/src/CelixBundleCacheErrorInjectionTestSuite.cc
@@ -17,8 +17,10 @@
   under the License.
  */
 
-#include "asprintf_ei.h"
-#include "bundle_archive_private.h"
+#include "gtest/gtest.h"
+
+#include <string>
+
 #include "celix_bundle_cache.h"
 #include "celix_constants.h"
 #include "celix_file_utils.h"
@@ -26,75 +28,83 @@
 #include "celix_log.h"
 #include "celix_properties.h"
 #include "celix_utils_ei.h"
+
+#include "asprintf_ei.h"
+#include "bundle_archive_private.h"
+#include "bundle_revision_private.h"
 #include "framework_private.h"
 #include "malloc_ei.h"
-#include "gtest/gtest.h"
-#include <string>
+#include "manifest.h"
+#include "unistd_ei.h"
 
 class CelixBundleCacheErrorInjectionTestSuite : public ::testing::Test {
-public:
+  public:
     CelixBundleCacheErrorInjectionTestSuite() {
         fw.configurationMap = celix_properties_create();
         fw.logger = celix_frameworkLogger_create(CELIX_LOG_LEVEL_TRACE);
     }
 
     ~CelixBundleCacheErrorInjectionTestSuite() override {
+        celix_ei_expect_getcwd(nullptr, 0, nullptr);
         celix_ei_expect_celix_utils_writeOrCreateString(nullptr, 0, nullptr);
         celix_ei_expect_celix_utils_createDirectory(nullptr, 0, CELIX_SUCCESS);
         celix_ei_expect_celix_utils_deleteDirectory(nullptr, 0, CELIX_SUCCESS);
         celix_ei_expect_celix_utils_strdup(nullptr, 0, nullptr);
         celix_ei_expect_asprintf(nullptr, 0, -1);
         celix_ei_expect_celix_stringHashMap_create(nullptr, 0, nullptr);
+        celix_ei_expect_malloc(nullptr, 0, nullptr);
         celix_ei_expect_calloc(nullptr, 0, nullptr);
         celix_frameworkLogger_destroy(fw.logger);
         celix_properties_destroy(fw.configurationMap);
     }
-
+    void createCache(celix_bundle_cache_t** cache) {
+        celix_properties_setBool(fw.configurationMap, 
CELIX_FRAMEWORK_CACHE_USE_TMP_DIR, true);
+        EXPECT_EQ(CELIX_SUCCESS, celix_bundleCache_create(&fw, &fw.cache));
+        *cache = fw.cache;
+    }
     struct celix_framework fw {};
 };
 
 TEST_F(CelixBundleCacheErrorInjectionTestSuite, CacheCreateErrorTest) {
-    celix_bundle_cache_t *cache = nullptr;
-    celix_ei_expect_calloc((void*) celix_bundleCache_create, 0, nullptr);
+    celix_bundle_cache_t* cache = nullptr;
+    celix_ei_expect_calloc((void*)celix_bundleCache_create, 0, nullptr);
     EXPECT_EQ(CELIX_ENOMEM, celix_bundleCache_create(&fw, &cache));
     EXPECT_EQ(nullptr, cache);
-    celix_ei_expect_celix_stringHashMap_create((void*) 
celix_bundleCache_create, 0, nullptr);
+    
celix_ei_expect_celix_stringHashMap_create((void*)celix_bundleCache_create, 0, 
nullptr);
     EXPECT_EQ(CELIX_ENOMEM, celix_bundleCache_create(&fw, &cache));
     EXPECT_EQ(nullptr, cache);
     celix_properties_setBool(fw.configurationMap, 
CELIX_FRAMEWORK_CACHE_USE_TMP_DIR, true);
-    celix_ei_expect_asprintf((void*) celix_bundleCache_create, 0, -1);
+    celix_ei_expect_asprintf((void*)celix_bundleCache_create, 0, -1);
     EXPECT_EQ(CELIX_ENOMEM, celix_bundleCache_create(&fw, &cache));
     EXPECT_EQ(nullptr, cache);
     celix_properties_setBool(fw.configurationMap, 
CELIX_FRAMEWORK_CACHE_USE_TMP_DIR, false);
-    celix_ei_expect_celix_utils_strdup((void*) celix_bundleCache_create, 0, 
nullptr);
+    celix_ei_expect_celix_utils_strdup((void*)celix_bundleCache_create, 0, 
nullptr);
     EXPECT_EQ(CELIX_ENOMEM, celix_bundleCache_create(&fw, &cache));
     EXPECT_EQ(nullptr, cache);
     celix_properties_setBool(fw.configurationMap, 
CELIX_FRAMEWORK_CLEAN_CACHE_DIR_ON_CREATE, true);
-    celix_ei_expect_celix_utils_deleteDirectory((void*) 
celix_bundleCache_create, 1, CELIX_FILE_IO_EXCEPTION);
+    
celix_ei_expect_celix_utils_deleteDirectory((void*)celix_bundleCache_create, 1, 
CELIX_FILE_IO_EXCEPTION);
     EXPECT_EQ(CELIX_FILE_IO_EXCEPTION, celix_bundleCache_create(&fw, &cache));
     EXPECT_EQ(nullptr, cache);
     celix_properties_setBool(fw.configurationMap, 
CELIX_FRAMEWORK_CLEAN_CACHE_DIR_ON_CREATE, false);
-    celix_ei_expect_celix_utils_createDirectory((void*) 
celix_bundleCache_create, 0, CELIX_FILE_IO_EXCEPTION);
+    
celix_ei_expect_celix_utils_createDirectory((void*)celix_bundleCache_create, 0, 
CELIX_FILE_IO_EXCEPTION);
     EXPECT_EQ(CELIX_FILE_IO_EXCEPTION, celix_bundleCache_create(&fw, &cache));
     EXPECT_EQ(nullptr, cache);
 }
 
 TEST_F(CelixBundleCacheErrorInjectionTestSuite, CacheDeleteErrorTest) {
-    celix_bundle_cache_t *cache = nullptr;
-    celix_properties_setBool(fw.configurationMap, 
CELIX_FRAMEWORK_CACHE_USE_TMP_DIR, true);
-    EXPECT_EQ(CELIX_SUCCESS, celix_bundleCache_create(&fw, &cache));
-    celix_ei_expect_celix_utils_deleteDirectory((void*) 
celix_bundleCache_deleteCacheDir, 0, CELIX_FILE_IO_EXCEPTION);
+    celix_bundle_cache_t* cache = nullptr;
+    createCache(&cache);
+    
celix_ei_expect_celix_utils_deleteDirectory((void*)celix_bundleCache_deleteCacheDir,
 0, CELIX_FILE_IO_EXCEPTION);
     EXPECT_EQ(CELIX_FILE_IO_EXCEPTION, 
celix_bundleCache_deleteCacheDir(cache));
     EXPECT_EQ(CELIX_SUCCESS, celix_bundleCache_destroy(cache));
 }
 
 TEST_F(CelixBundleCacheErrorInjectionTestSuite, ArchiveCreateErrorTest) {
-    celix_bundle_cache_t *cache = nullptr;
+    celix_bundle_cache_t* cache = nullptr;
+    createCache(&cache);
+
     bundle_archive_t* archive = nullptr;
-    celix_properties_setBool(fw.configurationMap, 
CELIX_FRAMEWORK_CACHE_USE_TMP_DIR, true);
-    EXPECT_EQ(CELIX_SUCCESS, celix_bundleCache_create(&fw, &cache));
-    fw.cache = cache;
-    celix_ei_expect_celix_utils_writeOrCreateString((void*) 
celix_bundleCache_createArchive, 0, nullptr);
+    
celix_ei_expect_celix_utils_writeOrCreateString((void*)celix_bundleCache_createArchive,
 0, nullptr);
     EXPECT_EQ(CELIX_ENOMEM, celix_bundleCache_createArchive(cache, 1, 
SIMPLE_TEST_BUNDLE1_LOCATION, &archive));
     EXPECT_EQ(nullptr, archive);
     EXPECT_EQ(-1, celix_bundleCache_findBundleIdForLocation(cache, 
SIMPLE_TEST_BUNDLE1_LOCATION));
@@ -103,7 +113,7 @@ TEST_F(CelixBundleCacheErrorInjectionTestSuite, 
ArchiveCreateErrorTest) {
 
     EXPECT_EQ(CELIX_SUCCESS, celix_bundleCache_create(&fw, &cache));
     fw.cache = cache;
-    celix_ei_expect_calloc((void*) celix_bundleArchive_create, 0, nullptr);
+    celix_ei_expect_calloc((void*)celix_bundleArchive_create, 0, nullptr);
     EXPECT_EQ(CELIX_ENOMEM, celix_bundleCache_createArchive(cache, 1, 
SIMPLE_TEST_BUNDLE1_LOCATION, &archive));
     EXPECT_EQ(nullptr, archive);
     EXPECT_EQ(-1, celix_bundleCache_findBundleIdForLocation(cache, 
SIMPLE_TEST_BUNDLE1_LOCATION));
@@ -111,14 +121,40 @@ TEST_F(CelixBundleCacheErrorInjectionTestSuite, 
ArchiveCreateErrorTest) {
     EXPECT_EQ(CELIX_SUCCESS, celix_bundleCache_destroy(cache));
 }
 
+TEST_F(CelixBundleCacheErrorInjectionTestSuite, SystemArchiveCreateErrorTest) {
+    celix_bundle_cache_t* cache = nullptr;
+    createCache(&cache);
+
+    bundle_archive_t* archive = nullptr;
+    celix_ei_expect_calloc((void*)celix_bundleArchive_create, 0, nullptr);
+    EXPECT_EQ(CELIX_ENOMEM, celix_bundleCache_createSystemArchive(&fw, 
&archive));
+    EXPECT_EQ(nullptr, archive);
+
+    celix_ei_expect_getcwd((void*)celix_bundleArchive_create, 0, nullptr);
+    EXPECT_EQ(CELIX_ENOMEM, celix_bundleCache_createSystemArchive(&fw, 
&archive));
+    EXPECT_EQ(nullptr, archive);
+
+    celix_ei_expect_getcwd((void*)celix_bundleArchive_create, 0, nullptr, 2);
+    EXPECT_EQ(CELIX_ENOMEM, celix_bundleCache_createSystemArchive(&fw, 
&archive));
+    EXPECT_EQ(nullptr, archive);
+
+    celix_ei_expect_malloc((void*)manifest_create, 0, nullptr);
+    EXPECT_EQ(CELIX_ENOMEM, celix_bundleCache_createSystemArchive(&fw, 
&archive));
+    EXPECT_EQ(nullptr, archive);
+
+    celix_ei_expect_calloc((void*)celix_bundleRevision_create, 0, nullptr);
+    EXPECT_EQ(CELIX_ENOMEM, celix_bundleCache_createSystemArchive(&fw, 
&archive));
+    EXPECT_EQ(nullptr, archive);
+
+    EXPECT_EQ(CELIX_SUCCESS, celix_bundleCache_destroy(cache));
+}
+
 TEST_F(CelixBundleCacheErrorInjectionTestSuite, ArchiveDestroyErrorTest) {
-    celix_bundle_cache_t *cache = nullptr;
+    celix_bundle_cache_t* cache = nullptr;
+    createCache(&cache);
     bundle_archive_t* archive = nullptr;
-    celix_properties_setBool(fw.configurationMap, 
CELIX_FRAMEWORK_CACHE_USE_TMP_DIR, true);
-    EXPECT_EQ(CELIX_SUCCESS, celix_bundleCache_create(&fw, &cache));
-    fw.cache = cache;
     EXPECT_EQ(CELIX_SUCCESS, celix_bundleCache_createArchive(cache, 1, 
SIMPLE_TEST_BUNDLE1_LOCATION, &archive));
-    celix_ei_expect_celix_utils_deleteDirectory((void*) 
celix_bundleCache_destroyArchive, 1, CELIX_FILE_IO_EXCEPTION);
+    
celix_ei_expect_celix_utils_deleteDirectory((void*)celix_bundleCache_destroyArchive,
 1, CELIX_FILE_IO_EXCEPTION);
     std::string storeRoot = 
celix_bundleArchive_getPersistentStoreRoot(archive);
     EXPECT_EQ(CELIX_FILE_IO_EXCEPTION, celix_bundleCache_destroyArchive(cache, 
archive));
     EXPECT_TRUE(celix_utils_directoryExists(storeRoot.c_str()));
@@ -126,20 +162,19 @@ TEST_F(CelixBundleCacheErrorInjectionTestSuite, 
ArchiveDestroyErrorTest) {
 }
 
 TEST_F(CelixBundleCacheErrorInjectionTestSuite, 
CreateBundleArchivesCacheErrorTest) {
-    celix_bundle_cache_t *cache = nullptr;
+    celix_bundle_cache_t* cache = nullptr;
     celix_properties_set(fw.configurationMap, CELIX_AUTO_START_1, 
SIMPLE_TEST_BUNDLE1_LOCATION);
-    celix_properties_setBool(fw.configurationMap, 
CELIX_FRAMEWORK_CACHE_USE_TMP_DIR, true);
-    EXPECT_EQ(CELIX_SUCCESS, celix_bundleCache_create(&fw, &cache));
-    fw.cache = cache;
-    celix_ei_expect_celix_utils_deleteDirectory((void*) 
celix_bundleCache_createBundleArchivesCache, 0, CELIX_FILE_IO_EXCEPTION);
+    createCache(&cache);
+    celix_ei_expect_celix_utils_deleteDirectory(
+        (void*)celix_bundleCache_createBundleArchivesCache, 0, 
CELIX_FILE_IO_EXCEPTION);
     EXPECT_EQ(CELIX_FILE_IO_EXCEPTION, 
celix_bundleCache_createBundleArchivesCache(&fw, true));
-    celix_ei_expect_celix_utils_writeOrCreateString((void*) 
celix_bundleCache_createBundleArchivesCache, 1, nullptr);
+    
celix_ei_expect_celix_utils_writeOrCreateString((void*)celix_bundleCache_createBundleArchivesCache,
 1, nullptr);
     EXPECT_EQ(CELIX_ENOMEM, celix_bundleCache_createBundleArchivesCache(&fw, 
true));
     celix_properties_unset(fw.configurationMap, CELIX_AUTO_START_1);
     celix_properties_set(fw.configurationMap, CELIX_AUTO_INSTALL, 
SIMPLE_TEST_BUNDLE1_LOCATION);
-    celix_ei_expect_celix_utils_writeOrCreateString((void*) 
celix_bundleCache_createBundleArchivesCache, 1, nullptr);
+    
celix_ei_expect_celix_utils_writeOrCreateString((void*)celix_bundleCache_createBundleArchivesCache,
 1, nullptr);
     EXPECT_EQ(CELIX_ENOMEM, celix_bundleCache_createBundleArchivesCache(&fw, 
true));
-    celix_ei_expect_celix_utils_writeOrCreateString((void*) 
celix_bundleCache_createArchive, 0, nullptr);
+    
celix_ei_expect_celix_utils_writeOrCreateString((void*)celix_bundleCache_createArchive,
 0, nullptr);
     EXPECT_EQ(CELIX_ENOMEM, celix_bundleCache_createBundleArchivesCache(&fw, 
true));
     EXPECT_EQ(CELIX_SUCCESS, celix_bundleCache_destroy(cache));
 }
\ No newline at end of file
diff --git a/libs/framework/src/bundle_archive.c 
b/libs/framework/src/bundle_archive.c
index bd08bb24..ceae451e 100644
--- a/libs/framework/src/bundle_archive.c
+++ b/libs/framework/src/bundle_archive.c
@@ -17,27 +17,25 @@
  * under the License.
  */
 
-#include "bundle_archive_private.h"
-
-#include <string.h>
+#include <assert.h>
+#include <dirent.h>
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <stdio.h>
-#include <time.h>
+#include <string.h>
 #include <sys/stat.h>
-#include <dirent.h>
+#include <time.h>
 #include <unistd.h>
-#include <string.h>
-#include <assert.h>
 
 #include "celix_constants.h"
-#include "celix_utils_api.h"
-#include "linked_list_iterator.h"
-#include "framework_private.h"
 #include "celix_file_utils.h"
-#include "bundle_revision_private.h"
 #include "celix_framework_utils_private.h"
+#include "celix_utils_api.h"
 
+#include "bundle_archive_private.h"
+#include "bundle_revision_private.h"
+#include "framework_private.h"
+#include "linked_list_iterator.h"
 
 celix_status_t celix_bundleArchive_getLastModifiedInternal(bundle_archive_pt 
archive, struct timespec *lastModified);
 
@@ -55,7 +53,6 @@ struct bundleArchive {
     char *savedBundleStatePropertiesPath;
     char* storeRoot;
     char* resourceCacheRoot;
-    bool isSystemBundle;
     char* bundleSymbolicName; //read from the manifest
     char* bundleVersion; //read from the manifest
 
@@ -210,28 +207,27 @@ celix_status_t 
celix_bundleArchive_createCacheDirectory(bundle_archive_pt archiv
 
 celix_status_t celix_bundleArchive_create(celix_framework_t* fw, const char 
*archiveRoot, long id, const char *location, bundle_archive_pt *bundle_archive) 
{
     celix_status_t status = CELIX_SUCCESS;
+    const char* error = NULL;
     bundle_archive_pt archive = calloc(1, sizeof(*archive));
-
-    if (archive) {
-        archive->fw = fw;
-        archive->id = id;
-        archive->isSystemBundle = id == CELIX_FRAMEWORK_BUNDLE_ID;
-        celixThreadMutex_create(&archive->lock, NULL);
-    }
+    bool isSystemBundle = id == CELIX_FRAMEWORK_BUNDLE_ID;
 
     if (archive == NULL) {
         status = CELIX_ENOMEM;
-        fw_logCode(fw->logger, CELIX_LOG_LEVEL_ERROR, status, "Could not 
create archive. Out of memory.");
-        bundleArchive_destroy(archive);
-        return status;
+        goto calloc_failed;
     }
 
-    int rc;
-    if (archive->isSystemBundle) {
+    archive->fw = fw;
+    archive->id = id;
+    celixThreadMutex_create(&archive->lock, NULL);
+
+    if (isSystemBundle) {
         archive->resourceCacheRoot = getcwd(NULL, 0);
         archive->storeRoot = getcwd(NULL, 0);
+        if (archive->resourceCacheRoot == NULL || archive->storeRoot == NULL) {
+            status = CELIX_ENOMEM;
+        }
     } else {
-        // assert(location != NULL)
+        int rc;
         archive->location = celix_utils_strdup(location);
         archive->archiveRoot = celix_utils_strdup(archiveRoot);
         rc = asprintf(&archive->savedBundleStatePropertiesPath, "%s/%s", 
archiveRoot,
@@ -239,45 +235,45 @@ celix_status_t 
celix_bundleArchive_create(celix_framework_t* fw, const char *arc
         if (rc < 0 || archive->location == NULL || 
archive->savedBundleStatePropertiesPath == NULL
             || archive->archiveRoot == NULL) {
             status = CELIX_ENOMEM;
-            fw_logCode(fw->logger, CELIX_LOG_LEVEL_ERROR, status, "Could not 
create archive. Out of memory.");
-            bundleArchive_destroy(archive);
-            return status;
         }
     }
+    if (status != CELIX_SUCCESS) {
+        error = "Failed to setup archive paths.";
+        goto init_failed;
+    }
 
     manifest_pt manifest = NULL;
-    if (archive->isSystemBundle) {
+    if (isSystemBundle) {
         status = manifest_create(&manifest);
     } else {
         status = celix_bundleArchive_createCacheDirectory(archive, &manifest);
     }
     if (status != CELIX_SUCCESS) {
-        fw_logCode(fw->logger, CELIX_LOG_LEVEL_ERROR, status, "Could not 
create archive. Failed to initialize archive or create manifest.");
-        bundleArchive_destroy(archive);
-        return status;
+        error = "Failed to initialize archive or create manifest.";
+        goto init_failed;
     }
 
-    if (archive->isSystemBundle) {
-        status = celix_bundleRevision_create(fw, archive->archiveRoot, NULL, 
manifest, &archive->revision);
-    } else {
-        status = celix_bundleRevision_create(fw, archive->archiveRoot, 
archive->location, manifest, &archive->revision);
-    }
+    status = celix_bundleRevision_create(fw, archive->archiveRoot, 
archive->location, manifest, &archive->revision);
     if (status != CELIX_SUCCESS) {
-        fw_logCode(fw->logger, CELIX_LOG_LEVEL_ERROR, status, "Could not 
create archive. Could not create bundle revision.");
-        bundleArchive_destroy(archive);
-        return status;
+        error = "Could not create bundle revision.";
+        goto init_failed;
     }
 
-    if (!archive->isSystemBundle) {
+    if (!isSystemBundle) {
         celix_bundleArchive_storeBundleStateProperties(archive);
     }
 
     *bundle_archive = archive;
+    return CELIX_SUCCESS;
+init_failed:
+    bundleArchive_destroy(archive);
+calloc_failed:
+    framework_logIfError(fw->logger, status, error, "Could not create 
archive.");
     return status;
 }
 
 celix_status_t bundleArchive_destroy(bundle_archive_pt archive) {
-       if (archive != NULL) {
+    if (archive != NULL) {
         free(archive->location);
         free(archive->savedBundleStatePropertiesPath);
         free(archive->archiveRoot);
@@ -288,13 +284,13 @@ celix_status_t bundleArchive_destroy(bundle_archive_pt 
archive) {
         bundleRevision_destroy(archive->revision);
         celixThreadMutex_destroy(&archive->lock);
         free(archive);
-       }
-       return CELIX_SUCCESS;
+    }
+    return CELIX_SUCCESS;
 }
 
 celix_status_t bundleArchive_getId(bundle_archive_pt archive, long *id) {
-     *id = archive->id;
-       return CELIX_SUCCESS;
+    *id = archive->id;
+    return CELIX_SUCCESS;
 }
 
 long celix_bundleArchive_getId(bundle_archive_pt archive) {
@@ -453,7 +449,7 @@ celix_status_t bundleArchive_close(bundle_archive_pt 
archive) {
 
 celix_status_t bundleArchive_closeAndDelete(bundle_archive_pt archive) {
     celix_status_t status = CELIX_SUCCESS;
-    if (!archive->isSystemBundle) {
+    if (archive->id != CELIX_FRAMEWORK_BUNDLE_ID) {
         const char* err = NULL;
         status = celix_utils_deleteDirectory(archive->archiveRoot, &err);
         framework_logIfError(archive->fw->logger, status, NULL, "Failed to 
delete archive root '%s': %s", archive->archiveRoot, err);

Reply via email to