move stale-update logic to main
authorTero Marttila <terom@fixme.fi>
Mon, 28 Dec 2009 20:43:33 +0200
changeset 5 4b440fa03183
parent 4 49362b34116c
child 6 766df7c9b90d
move stale-update logic to main
src/lib/image.c
src/lib/pngtile.h
src/util/main.c
--- a/src/lib/image.c	Mon Dec 28 20:36:29 2009 +0200
+++ b/src/lib/image.c	Mon Dec 28 20:43:33 2009 +0200
@@ -145,7 +145,6 @@
 {
     struct pt_image *image;
     char cache_path[_POSIX_PATH_MAX];
-    int stale;
 
     // XXX: verify that the path exists and looks like a PNG file
 
@@ -161,17 +160,6 @@
     if (pt_cache_open(&image->cache, cache_path, cache_mode))
         goto error;
     
-    // compare cache with image
-    // XXX: check cache_mode
-    if ((stale = pt_cache_stale(image->cache, image->path)) < 0)
-        goto error;
-
-    // update if not fresh
-    if (stale) {
-        if (pt_image_update_cache(image))
-            goto error;
-    }
-
     // ok, ready for access
     *image_ptr = image;
 
@@ -183,6 +171,17 @@
     return -1;
 }
 
+int pt_image_stale (struct pt_image *image)
+{
+    return pt_cache_stale(image->cache, image->path);
+}
+
+int pt_image_update (struct pt_image *image)
+{
+    return pt_image_update_cache(image);
+}
+
+
 void pt_image_destroy (struct pt_image *image)
 {
     free(image->path);
--- a/src/lib/pngtile.h	Mon Dec 28 20:36:29 2009 +0200
+++ b/src/lib/pngtile.h	Mon Dec 28 20:43:33 2009 +0200
@@ -33,8 +33,16 @@
  * @param path filesystem path to .png file
  * @param mode combination of PT_IMG_* flags
  */
-int pt_image_open (struct pt_image **img_ptr, struct pt_ctx *ctx, const char *png_path, int cache_mode);
+int pt_image_open (struct pt_image **image_ptr, struct pt_ctx *ctx, const char *png_path, int cache_mode);
 
+/**
+ * Check the given image's cache is stale - in other words, the image needs to be updated.
+ */
+int pt_image_stale (struct pt_image *image);
 
+/**
+ * Update the given image's cache.
+ */
+int pt_image_update (struct pt_image *image);
 
 #endif
--- a/src/util/main.c	Mon Dec 28 20:36:29 2009 +0200
+++ b/src/util/main.c	Mon Dec 28 20:43:33 2009 +0200
@@ -77,25 +77,46 @@
 
 
     struct pt_ctx *ctx = NULL;
-    struct pt_image *image = NULL;;
+    struct pt_image *image = NULL;
+    int ret;
 
     log_debug("Processing %d images...", argc - optind);
 
     for (int i = optind; i < argc; i++) {
         const char *img_path = argv[i];
 
-        log_debug("Load image from: %s", img_path);
+        log_debug("Loading image from: %s...", img_path);
 
         // open
         if (pt_image_open(&image, ctx, img_path, PT_IMG_READ | PT_IMG_WRITE)) {
             log_errno("pt_image_open: %s", img_path);
-
             continue;
+        }
 
-        } else {
-            log_info("Opened image at: %s", img_path);
+        log_info("Opened image at: %s", img_path);
+        
+        // check if stale
+        if ((ret = pt_image_stale(image)) < 0) {
+            log_errno("pt_image_stale: %s", img_path);
+            goto error;
+        }
+        
+        // update if stale
+        if (ret) {
+            log_info("Image cache is stale, updating...");
 
+            if (pt_image_update(image)) {
+                log_warn_errno("pt_image_update: %s", img_path);
+            }
+
+            log_debug("Image cache updated");
         }
+
+        // done
+
+error:
+        // cleanup
+        pt_image_destroy(image);
     }
 
     // XXX: done