src/lib/cache.c
changeset 59 80135bdfd343
parent 56 d5e3089906da
child 60 cb6407844ab5
--- 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);
 }