PengZheng commented on code in PR #476:
URL: https://github.com/apache/celix/pull/476#discussion_r1139755734


##########
libs/framework/src/celix_bundle_cache.c:
##########
@@ -71,124 +137,222 @@ celix_status_t 
celix_bundleCache_create(celix_framework_t* fw, celix_bundle_cach
         }
 
         asprintf(&cache->cacheDir, "/tmp/celix-cache-%s-%s", pg, 
celix_framework_getUUID(fw));
-        cache->deleteOnDestroy = true;
     } else {
+        const char* cacheDir = celix_bundleCache_cacheDirPath(fw);
         cache->cacheDir = celix_utils_strdup(cacheDir);
-        cache->deleteOnDestroy = false;
+    }
+
+    if (cache->deleteOnCreate) {
+        status = celix_bundleCache_deleteCacheDir(cache);
+        if (status != CELIX_SUCCESS) {
+            celix_bundleCache_destroy(cache);
+            return status;
+        }
+    }
+
+    const char* errorStr;
+    status = celix_utils_createDirectory(cache->cacheDir, false, &errorStr);
+    if (status != CELIX_SUCCESS) {
+        fw_logCode(fw->logger, CELIX_LOG_LEVEL_ERROR, status, "Cannot create 
bundle cache directory %s, error %s", cache->cacheDir, errorStr);
+        celix_bundleCache_destroy(cache);
+        return status;
     }
 
     *out = cache;
-       return CELIX_SUCCESS;
+       return status;
 }
 
 celix_status_t celix_bundleCache_destroy(celix_bundle_cache_t* cache) {
     celix_status_t status = CELIX_SUCCESS;
        if (cache->deleteOnDestroy) {
-        status = celix_bundleCache_delete(cache);
+        status = celix_bundleCache_deleteCacheDir(cache);
        }
        free(cache->cacheDir);
+    celix_stringHashMap_destroy(cache->locationToBundleIdLookupMap);
+    celixThreadMutex_destroy(&cache->mutex);
        free(cache);
        return status;
 }
 
-celix_status_t celix_bundleCache_delete(celix_bundle_cache_t* cache) {
+celix_status_t celix_bundleCache_deleteCacheDir(celix_bundle_cache_t* cache) {
     const char* err = NULL;
+    celixThreadMutex_lock(&cache->mutex);
     celix_status_t status = celix_utils_deleteDirectory(cache->cacheDir, &err);
-    if (err != NULL) {
-        FW_LOG(CELIX_LOG_LEVEL_ERROR, "Cannot delete cache dir at %s: %s", 
cache->cacheDir, err);
+    celixThreadMutex_unlock(&cache->mutex);
+    if (status != CELIX_SUCCESS) {
+        fw_logCode(cache->fw->logger, CELIX_LOG_LEVEL_ERROR, status, "Cannot 
delete bundle cache directory %s: %s", cache->cacheDir, err);
     }
     return status;
 }
 
-celix_status_t celix_bundleCache_getArchives(celix_bundle_cache_t* cache, 
array_list_pt *archives) {
+celix_status_t celix_bundleCache_createArchive(celix_framework_t* fw, long id, 
const char *location, bundle_archive_t** archiveOut) {
        celix_status_t status = CELIX_SUCCESS;
+    bundle_archive_t* archive = NULL;
+
+    char archiveRootBuffer[CELIX_DEFAULT_STRING_CREATE_BUFFER_SIZE];
+    char *archiveRoot = celix_utils_writeOrCreateString(archiveRootBuffer, 
sizeof(archiveRootBuffer), CELIX_BUNDLE_ARCHIVE_ROOT_FORMAT, 
fw->cache->cacheDir, id);
+    if (archiveRoot) {
+        celixThreadMutex_lock(&fw->cache->mutex);
+               status = bundleArchive_create(fw, archiveRoot, id, location, 
&archive);
+        celixThreadMutex_unlock(&fw->cache->mutex);
+        if (status != CELIX_SUCCESS) {
+            celix_utils_freeStringIfNotEqual(archiveRootBuffer, archiveRoot);

Review Comment:
   It should always be called in pair with `celix_utils_writeOrCreateString`, 
otherwise memory leak may happen.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@celix.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to