pt_cache_close
authorTero Marttila <terom@fixme.fi>
Mon, 25 Jan 2010 01:59:06 +0200
changeset 59 80135bdfd343
parent 58 d0295e6deb62
child 60 cb6407844ab5
pt_cache_close
include/pngtile.h
src/lib/cache.c
src/lib/cache.h
src/lib/error.c
--- a/include/pngtile.h	Mon Jan 25 01:57:54 2010 +0200
+++ b/include/pngtile.h	Mon Jan 25 01:59:06 2010 +0200
@@ -221,6 +221,8 @@
     PT_ERR_CACHE_MMAP,
     PT_ERR_CACHE_RENAME_TMP,
     PT_ERR_CACHE_VERSION,
+    PT_ERR_CACHE_MUNMAP,
+    PT_ERR_CACHE_CLOSE,
 
     PT_ERR_TILE_CLIP,
 
--- a/src/lib/cache.c	Mon Jan 25 01:57:54 2010 +0200
+++ b/src/lib/cache.c	Mon Jan 25 01:59:06 2010 +0200
@@ -43,6 +43,26 @@
 }
 
 /**
+ * Force-clean pt_cache, warn on errors
+ */
+static void pt_cache_abort (struct pt_cache *cache)
+{
+    if (cache->file != NULL) {
+        if (munmap(cache->file, sizeof(struct pt_cache_file) + cache->file->header.data_size))
+            log_warn_errno("munmap: %p, %zu", cache->file, sizeof(struct pt_cache_file) + cache->file->header.data_size);
+
+        cache->file = NULL;
+    }
+
+    if (cache->fd >= 0) {
+        if (close(cache->fd))
+            log_warn_errno("close: %d", cache->fd);
+
+        cache->fd = -1;
+    }
+}
+
+/**
  * Open the cache file as an fd for reading
  *
  * XXX: use some kind of locking?
@@ -181,24 +201,6 @@
 }
 
 /**
- * Abort any incomplete open operation, cleaning up
- */
-static void pt_cache_abort (struct pt_cache *cache)
-{
-    if (cache->file != NULL) {
-        munmap(cache->file, sizeof(struct pt_cache_file) + cache->file->header.data_size);
-
-        cache->file = NULL;
-    }
-
-    if (cache->fd >= 0) {
-        close(cache->fd);
-
-        cache->fd = -1;
-    }
-}
-
-/**
  * Open the .tmp cache file as an fd for writing
  */
 static int pt_cache_open_tmp_fd (struct pt_cache *cache, int *fd_ptr)
@@ -420,12 +422,31 @@
     return 0;
 }
 
+int pt_cache_close (struct pt_cache *cache)
+{
+    if (cache->file != NULL) {
+        if (munmap(cache->file, sizeof(struct pt_cache_file) + cache->file->header.data_size))
+            RETURN_ERROR(PT_ERR_CACHE_MUNMAP);
+
+        cache->file = NULL;
+    }
+
+    if (cache->fd >= 0) {
+        if (close(cache->fd))
+            RETURN_ERROR(PT_ERR_CACHE_CLOSE);
+
+        cache->fd = -1;
+    }
+
+    return 0;
+}
+
 void pt_cache_destroy (struct pt_cache *cache)
 {
-    free(cache->path);
-
+    // cleanup
     pt_cache_abort(cache);
 
+    free(cache->path);
     free(cache);
 }
 
--- a/src/lib/cache.h	Mon Jan 25 01:57:54 2010 +0200
+++ b/src/lib/cache.h	Mon Jan 25 01:59:06 2010 +0200
@@ -117,6 +117,11 @@
 int pt_cache_tile (struct pt_cache *cache, struct pt_tile *tile);
 
 /**
+ * Close the cache, if opened
+ */
+int pt_cache_close (struct pt_cache *cache);
+
+/**
  * Release all resources associated with the given cache object without any cleanup.
  */
 void pt_cache_destroy (struct pt_cache *cache);
--- a/src/lib/error.c	Mon Jan 25 01:57:54 2010 +0200
+++ b/src/lib/error.c	Mon Jan 25 01:59:06 2010 +0200
@@ -26,6 +26,8 @@
     [PT_ERR_CACHE_MMAP]         = "mmap(.cache)",
     [PT_ERR_CACHE_RENAME_TMP]   = "rename(.tmp, .cache)",
     [PT_ERR_CACHE_VERSION]      = "Incompatible .cache version",
+    [PT_ERR_CACHE_MUNMAP]       = "munmap(cache->file)",
+    [PT_ERR_CACHE_CLOSE]        = "close(cache->fd)",
 
     [PT_ERR_PTHREAD_CREATE]     = "pthread_create",
     [PT_ERR_CTX_SHUTDOWN]       = "pt_ctx is shutting down",