open cache as .tmp, and rename to .cache when done
authorTero Marttila <terom@fixme.fi>
Mon, 28 Dec 2009 20:36:29 +0200
changeset 4 49362b34116c
parent 3 da7c6dcafb43
child 5 4b440fa03183
open cache as .tmp, and rename to .cache when done
Makefile
src/lib/cache.c
src/lib/image.c
src/shared/util.c
src/shared/util.h
--- a/Makefile	Mon Dec 28 19:58:51 2009 +0200
+++ b/Makefile	Mon Dec 28 20:36:29 2009 +0200
@@ -16,7 +16,8 @@
 all: depend lib/libpngtile.so bin/util
 
 lib/libpngtile.so : \
-	build/obj/lib/image.o build/obj/lib/cache.o
+	build/obj/lib/image.o build/obj/lib/cache.o \
+	build/obj/shared/util.o
 
 bin/util: \
 	lib/libpngtile.so \
--- a/src/lib/cache.c	Mon Dec 28 19:58:51 2009 +0200
+++ b/src/lib/cache.c	Mon Dec 28 20:36:29 2009 +0200
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "shared/util.h"
 
 #include <stdlib.h>
 #include <unistd.h>
@@ -92,25 +93,16 @@
 }
 
 /**
- * Open the cache file as an fd.
+ * Open the cache file as an fd for reading
  *
  * XXX: needs locking
  */
-static int pt_cache_open_fd (struct pt_cache *cache, int *fd_ptr)
+static int pt_cache_open_read (struct pt_cache *cache, int *fd_ptr)
 {
     int fd;
-    int flags = 0;
-
-    // determine open flags
-    // XXX: O_RDONLY | O_WRONLY == O_RDWR?
-    if (cache->mode & PT_IMG_READ)
-        flags |= O_RDONLY;
-
-    if (cache->mode & PT_IMG_WRITE)
-        flags |= (O_WRONLY | O_CREAT);
-
+    
     // actual open()
-    if ((fd = open(cache->path, flags)) < 0)
+    if ((fd = open(cache->path, O_RDONLY)) < 0)
         return -1;
 
     // ok
@@ -120,6 +112,36 @@
 }
 
 /**
+ * Open the .tmp cache file as an fd for writing
+ */
+static int pt_cache_open_tmp (struct pt_cache *cache, int *fd_ptr)
+{
+    int fd;
+    char tmp_path[1024];
+    
+    // check mode
+    if (!(cache->mode & PT_IMG_WRITE)) {
+        errno = EPERM;
+        return -1;
+    }
+    
+    // get .tmp path
+    if (path_with_fext(cache->path, tmp_path, sizeof(tmp_path), ".tmp"))
+        return -1;
+
+    // open for write, create
+    // XXX: locking?
+    if ((fd = open(tmp_path, O_RDWR | O_CREAT, 0644)) < 0)
+        return -1;
+
+    // ok
+    *fd_ptr = fd;
+
+    return 0;
+}
+
+
+/**
  * Mmap the opened cache file from offset PT_CACHE_PAGE, using the calculated size stored in cache->size
  */
 static int pt_cache_open_mmap (struct pt_cache *cache, void **addr_ptr)
@@ -174,7 +196,7 @@
 }
 
 /**
- * Create a new cache file, open it, and write out the header.
+ * Create a new .tmp cache file, open it, and write out the header.
  */
 static int pt_cache_open_create (struct pt_cache *cache, struct pt_cache_header *header)
 {
@@ -184,8 +206,8 @@
         return -1;
     }
 
-    // open
-    if (pt_cache_open_fd(cache, &cache->fd))
+    // open as .tmp
+    if (pt_cache_open_tmp(cache, &cache->fd))
         return -1;
 
     // calculate data size
@@ -213,6 +235,25 @@
     return -1;
 }
 
+/**
+ * Rename the opened .tmp to .cache
+ */
+static int pt_cache_create_done (struct pt_cache *cache)
+{
+    char tmp_path[1024];
+    
+    // get .tmp path
+    if (path_with_fext(cache->path, tmp_path, sizeof(tmp_path), ".tmp"))
+        return -1;
+
+    // rename
+    if (rename(tmp_path, cache->path) < 0)
+        return -1;
+
+    // ok
+    return 0;
+}
+
 int pt_cache_update_png (struct pt_cache *cache, png_structp png, png_infop info)
 {
     struct pt_cache_header header;
@@ -231,7 +272,7 @@
     // fill in other info
     header.row_bytes = png_get_rowbytes(png, info);
 
-    // create and write out header
+    // create .tmp and write out header
     if (pt_cache_open_create(cache, &header))
         return -1;
 
@@ -243,6 +284,10 @@
         png_read_row(png, cache->mmap + row * header.row_bytes, NULL);
     }
 
+    // move from .tmp to .cache
+    if (pt_cache_create_done(cache))
+        return -1;
+
     // done!
     return 0;
 }
--- a/src/lib/image.c	Mon Dec 28 19:58:51 2009 +0200
+++ b/src/lib/image.c	Mon Dec 28 20:36:29 2009 +0200
@@ -1,5 +1,6 @@
 #include "image.h"
 #include "cache.h"
+#include "shared/util.h"
 
 #include <stdlib.h>
 #include <limits.h> // for _POSIX_PATH_MAX
@@ -137,22 +138,7 @@
  */
 static int pt_image_cache_path (struct pt_image *image, char *buf, size_t len)
 {
-    char *ext;
-
-    // XXX: be more careful about buf len
-    
-    // copy filename
-    strncpy(buf, image->path, len);
-
-    // find .ext
-    if ((ext = strrchr(buf, '.')) == NULL)
-        return -1;
-
-    // change to .cache
-    strncpy(ext, ".cache", (buf + len) - ext);
-
-    // hmmk
-    return 0;
+    return path_with_fext(image->path, buf, len, ".cache"); 
 }
 
 int pt_image_open (struct pt_image **image_ptr, struct pt_ctx *ctx, const char *path, int cache_mode)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shared/util.c	Mon Dec 28 20:36:29 2009 +0200
@@ -0,0 +1,35 @@
+#include "util.h"
+
+#include <string.h>
+
+int chfext (char *buf, size_t len, const char *newext)
+{
+    char *ext;
+
+    // find .ext
+    if ((ext = strrchr(buf, '.')) == NULL)
+        return -1;
+
+    // check length
+    if (ext + strlen(newext) >= buf + len)
+        return -1;
+
+    // change to .foo
+    strcpy(ext, newext);
+    
+    // ok
+    return 0;
+}
+
+int path_with_fext (const char *path, char *buf, size_t len, const char *newext)
+{
+    // verify length
+    if (strlen(path) > len)
+        return -1;
+
+    // copy filename
+    strcpy(buf, path);
+
+    // change fext
+    return chfext(buf, len, newext);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/shared/util.h	Mon Dec 28 20:36:29 2009 +0200
@@ -0,0 +1,17 @@
+#ifndef SHARED_UTIL_H
+#define SHARED_UTIL_H
+
+#include <stddef.h>
+
+/**
+ * Replace the file extension in the given path buffer with the given extension, which should be of the form ".foo"
+ */
+int chfext (char *path, size_t len, const char *newext);
+
+/**
+ * Copy filesystem path with new file extension
+ */
+int path_with_fext (const char *path, char *buf, size_t len, const char *newext);
+
+
+#endif