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


##########
libs/framework/src/celix_framework_utils.c:
##########
@@ -124,34 +126,67 @@ static bool isEmbeddedBundleUrlValid(celix_framework_t 
*fw, const char* bundleUR
     return valid;
 }
 
-static bool extractBundlePath(celix_framework_t *fw, const char* bundlePath, 
const char* extractPath) {
+static bool celix_framework_utils_isBundlePathNewerThan(celix_framework_t *fw, 
const char* bundlePath, const struct timespec* time) {
+    struct timespec bundleModTime;
+    char pathBuffer[CELIX_DEFAULT_STRING_CREATE_BUFFER_SIZE];
+    char* resolvedPath = 
celix_framework_utils_resolveFileBundleUrl(pathBuffer, sizeof(pathBuffer), fw, 
bundlePath, true);
+    celix_status_t status = celix_utils_getLastModified(resolvedPath, 
&bundleModTime);
+    if (status != CELIX_SUCCESS) {
+        fw_logCode(fw->logger, CELIX_LOG_LEVEL_ERROR, status, "Cannot get last 
modified time for bundle %s", resolvedPath);
+        return false;
+    }
+    double diff = celix_difftime(&bundleModTime, time);
+    celix_utils_freeStringIfNotEqual(pathBuffer, resolvedPath);
+    return diff < 0.0;
+}
+
+bool celix_framework_utils_isBundleUrlNewerThan(celix_framework_t* fw, const 
char* bundleURL, const struct timespec* time) {
+    if (time == NULL) {
+        return true;
+    }
+
+    char* trimmedUrl = celix_utils_trim(bundleURL);
+    bool newer;
+    size_t fileSchemeLen = sizeof(FILE_URL_SCHEME)-1;
+    size_t embeddedSchemeLen = sizeof(EMBEDDED_URL_SCHEME)-1;
+    if (strncasecmp(FILE_URL_SCHEME, trimmedUrl, fileSchemeLen) == 0) {
+        newer = celix_framework_utils_isBundlePathNewerThan(fw, trimmedUrl + 
fileSchemeLen, time); //skip the file:// part
+    } else if (strncasecmp(EMBEDDED_URL_SCHEME, trimmedUrl, embeddedSchemeLen) 
== 0) {
+        newer = true; //for now embedded zip always considered newer
+    } else {
+        newer = celix_framework_utils_isBundlePathNewerThan(fw, trimmedUrl, 
time);
+    }
+    free(trimmedUrl);
+
+    return newer;
+}
+
+static bool 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;
 
-    char* resolvedPath = resolveFileBundleUrl(fw, bundlePath, false);
+    char buffer[CELIX_DEFAULT_STRING_CREATE_BUFFER_SIZE];
+    char* resolvedPath = celix_framework_utils_resolveFileBundleUrl(buffer, 
sizeof(buffer), fw, bundlePath, false);
     assert(resolvedPath != NULL); //should be caught by 
celix_framework_utils_isBundleUrlValid
-
     celix_status_t status = celix_utils_extractZipFile(resolvedPath, 
extractPath, &err);
-    if (status == CELIX_SUCCESS) {
-        FW_LOG(CELIX_LOG_LEVEL_TRACE, "Bundle zip `%s` extracted.", 
resolvedPath);
-    } else {
+    if (status != CELIX_SUCCESS) {
         FW_LOG(CELIX_LOG_LEVEL_ERROR, "Could not extract bundle zip file `%s` 
to `%s`: %s", resolvedPath, extractPath, err);
     }
-    free(resolvedPath);
+    celix_utils_freeStringIfNotEqual(buffer, resolvedPath);
     return status == CELIX_SUCCESS;
 }
 
-static bool extractBundleEmbedded(celix_framework_t *fw, const char* 
embeddedBundle, const char* extractPath) {
+static bool 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;
     asprintf(&startSymbol, "%s%s%s", EMBEDDED_BUNDLE_PREFIX, embeddedBundle, 
EMBEDDED_BUNDLE_START_POSTFIX);
     asprintf(&endSymbol, "%s%s%s", EMBEDDED_BUNDLE_PREFIX, embeddedBundle, 
EMBEDDED_BUNDLE_END_POSTFIX);
 
-    void* main = dlopen(NULL, RTLD_NOW);
-    void* start = dlsym(main, startSymbol);
-    void* end = dlsym(main, endSymbol);
-    dlclose(main);
+    void* prog = dlopen(NULL, RTLD_NOW);
+    void* start = dlsym(prog, startSymbol);
+    void* end = dlsym(prog, endSymbol);
+    dlclose(prog);
 
     if (start == NULL || end == NULL) {

Review Comment:
   There is memory leak in `celix_utils_extractZipData`:
   
   ```C
       if (source == NULL || zip == NULL) {
           status = CELIX_FILE_IO_EXCEPTION;
           *errorOut = zip_error_strerror(&zipError); // what 
zip_error_strerror returns is actually malloced
       }
   
   ```



-- 
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