# HG changeset patch # User Tero Marttila # Date 1264377546 -7200 # Node ID 80135bdfd3431b81ada21fd245dc70e7d154eb9d # Parent d0295e6deb6218e3c6bbd93e031f09bcfe8c378f pt_cache_close diff -r d0295e6deb62 -r 80135bdfd343 include/pngtile.h --- 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, diff -r d0295e6deb62 -r 80135bdfd343 src/lib/cache.c --- 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); } diff -r d0295e6deb62 -r 80135bdfd343 src/lib/cache.h --- 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); diff -r d0295e6deb62 -r 80135bdfd343 src/lib/error.c --- 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",