add some additional pt_image_info fields for stat
authorTero Marttila <terom@fixme.fi>
Sun, 24 Jan 2010 23:17:10 +0200
changeset 54 4a25113cb2a4
parent 53 8a3165c604f8
child 55 a3542e78ecd8
add some additional pt_image_info fields for stat
include/pngtile.h
src/lib/cache.c
src/lib/image.c
src/util/main.c
--- a/include/pngtile.h	Sun Jan 24 23:04:55 2010 +0200
+++ b/include/pngtile.h	Sun Jan 24 23:17:10 2010 +0200
@@ -9,6 +9,7 @@
 #include <stddef.h>
 #include <stdio.h> // for FILE*
 #include <stdint.h>
+#include <sys/types.h> // for time_t
 
 /**
  * "Global" context shared between images
@@ -46,18 +47,24 @@
     PT_CACHE_STALE      = 2,
 };
 
-/** Metadata info for image */
+/** Metadata info for image. Values will be set to zero if not available */
 struct pt_image_info {
     /** Dimensions of image */
     size_t width, height;
 
+    /** Last update of image file */
+    time_t image_mtime;
+
     /** Size of image file in bytes */
     size_t image_bytes;
 
+    /** Last update of cache file */
+    time_t cache_mtime;
+
     /** Size of cache file in bytes */
     size_t cache_bytes;
 
-    /** Size of cache file in blocks (for sparse cache files) */
+    /** Size of cache file in blocks (for sparse cache files) - 512 bytes / block? */
     size_t cache_blocks;
 };
 
--- a/src/lib/cache.c	Sun Jan 24 23:04:55 2010 +0200
+++ b/src/lib/cache.c	Sun Jan 24 23:17:10 2010 +0200
@@ -69,6 +69,7 @@
 
 int pt_cache_info (struct pt_cache *cache, struct pt_image_info *info)
 {
+    struct stat st;
     int err;
 
     // ensure open
@@ -78,6 +79,20 @@
     info->width = cache->header->width;
     info->height = cache->header->height;
 
+    // stat
+    if (stat(cache->path, &st) < 0) {
+        // unknown
+        info->cache_mtime = 0;
+        info->cache_bytes = 0;
+        info->cache_blocks = 0;
+
+    } else {
+        // store
+        info->cache_mtime = st.st_mtime;
+        info->cache_bytes = st.st_size;
+        info->cache_blocks = st.st_blocks;
+    }
+
     return 0;
 }
 
--- a/src/lib/image.c	Sun Jan 24 23:04:55 2010 +0200
+++ b/src/lib/image.c	Sun Jan 24 23:17:10 2010 +0200
@@ -7,6 +7,8 @@
 #include "shared/log.h"
 
 #include <stdlib.h>
+#include <sys/stat.h>
+#include <unistd.h>
 #include <errno.h>
 
 #include <png.h>
@@ -204,11 +206,24 @@
 
 int pt_image_info (struct pt_image *image, const struct pt_image_info **info_ptr)
 {
+    struct stat st;
     int err;
 
     // update info
     if ((err = pt_cache_info(image->cache, &image->info)))
         return err;
+
+    // stat our info
+    if (stat(image->path, &st) < 0) {
+        // unknown
+        image->info.image_mtime = 0;
+        image->info.image_bytes = 0;
+
+    } else {
+        // store
+        image->info.image_mtime = st.st_mtime;
+        image->info.image_bytes = st.st_size;
+    }
     
     // return pointer
     *info_ptr = &image->info;
--- a/src/util/main.c	Sun Jan 24 23:04:55 2010 +0200
+++ b/src/util/main.c	Sun Jan 24 23:17:10 2010 +0200
@@ -201,11 +201,16 @@
         // show info
         const struct pt_image_info *img_info;
         
-        if ((err = pt_image_info(image, &img_info)))
+        if ((err = pt_image_info(image, &img_info))) {
             log_warn_errno("pt_image_info: %s: %s", img_path, pt_strerror(err));
 
-        else
+        } else {
             log_info("\tImage dimensions: %zux%zu", img_info->width, img_info->height);
+            log_info("\tImage mtime=%u, bytes=%zu", img_info->image_mtime, img_info->image_bytes);
+            log_info("\tCache mtime=%u, bytes=%zu, blocks=%zu (%zu bytes)", 
+                    img_info->cache_mtime, img_info->cache_bytes, img_info->cache_blocks, img_info->cache_blocks * 512,
+            );
+        }
 
         // render tile?
         if (ti.width && ti.height) {