src/lib/cache.c
author Tero Marttila <terom@fixme.fi>
Mon, 25 Jan 2010 02:19:10 +0200
changeset 66 55949307182c
parent 60 cb6407844ab5
child 67 d8d9aa430ecc
permissions -rw-r--r--
pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#include "cache.h"
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
     2
#include "shared/util.h"
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
     3
#include "shared/log.h" // only LOG_DEBUG
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
     4
#include "error.h"
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
     6
#include <stdlib.h>
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
     7
#include <unistd.h>
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
     8
#include <sys/types.h>
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
     9
#include <sys/stat.h>
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    10
#include <fcntl.h>
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    11
#include <sys/mman.h>
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
    12
#include <errno.h>
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
    13
#include <assert.h>
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    14
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    15
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
    16
int pt_cache_new (struct pt_cache **cache_ptr, const char *path, int mode)
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
{
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
    struct pt_cache *cache;
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
    19
    int err;
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    21
    // alloc
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
    if ((cache = calloc(1, sizeof(*cache))) == NULL)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
    23
        RETURN_ERROR(PT_ERR_MEM);
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    25
    if ((cache->path = strdup(path)) == NULL)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
    26
        JUMP_SET_ERROR(err, PT_ERR_MEM);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    27
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    28
    // init
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    29
    cache->fd = -1;
3
da7c6dcafb43 store cache->mode, error handling for pt_image_update_cache
Tero Marttila <terom@fixme.fi>
parents: 2
diff changeset
    30
    cache->mode = mode;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    31
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    32
    // ok
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    33
    *cache_ptr = cache;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    34
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    35
    return 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    36
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    37
error:
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    38
    // cleanup
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    39
    if (cache)
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    40
        pt_cache_destroy(cache);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    41
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
    42
    return err;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    43
}
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
    44
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    45
/**
59
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    46
 * Force-clean pt_cache, warn on errors
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    47
 */
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    48
static void pt_cache_abort (struct pt_cache *cache)
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    49
{
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    50
    if (cache->file != NULL) {
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    51
        if (munmap(cache->file, sizeof(struct pt_cache_file) + cache->file->header.data_size))
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    52
            log_warn_errno("munmap: %p, %zu", cache->file, sizeof(struct pt_cache_file) + cache->file->header.data_size);
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    53
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    54
        cache->file = NULL;
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    55
    }
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    56
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    57
    if (cache->fd >= 0) {
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    58
        if (close(cache->fd))
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    59
            log_warn_errno("close: %d", cache->fd);
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    60
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    61
        cache->fd = -1;
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    62
    }
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    63
}
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    64
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    65
/**
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    66
 * Open the cache file as an fd for reading
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    67
 *
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    68
 * XXX: use some kind of locking?
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    69
 */
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    70
static int pt_cache_open_read_fd (struct pt_cache *cache, int *fd_ptr)
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    71
{
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    72
    int fd;
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    73
    
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    74
    // actual open()
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    75
    if ((fd = open(cache->path, O_RDONLY)) < 0)
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    76
        RETURN_ERROR_ERRNO(PT_ERR_OPEN_MODE, EACCES);
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    77
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    78
    // ok
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    79
    *fd_ptr = fd;
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    80
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    81
    return 0;
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    82
}
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    83
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    84
/**
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    85
 * Read in the cache header from the open file
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    86
 */
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    87
static int pt_cache_read_header (int fd, struct pt_cache_header *header)
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    88
{
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    89
    size_t len = sizeof(*header);
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    90
    char *buf = (char *) header;
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    91
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    92
    // seek to start
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    93
    if (lseek(fd, 0, SEEK_SET) != 0)
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    94
        RETURN_ERROR(PT_ERR_CACHE_SEEK);
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    95
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    96
    // write out full header
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    97
    while (len) {
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    98
        ssize_t ret;
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    99
        
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   100
        // try and write out the header
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   101
        if ((ret = read(fd, buf, len)) <= 0)
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   102
            RETURN_ERROR(PT_ERR_CACHE_READ);
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   103
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   104
        // update offset
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   105
        buf += ret;
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   106
        len -= ret;
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   107
    }
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   108
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   109
    // done
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   110
    return 0;
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   111
}
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   112
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   113
/**
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   114
 * Read and return the version number from the cache file, temporarily opening it if needed
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   115
 */
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   116
static int pt_cache_version (struct pt_cache *cache)
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   117
{
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   118
    int fd;
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   119
    struct pt_cache_header header;
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   120
    int ret;
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   121
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   122
    // already open?
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   123
    if (cache->file)
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   124
        return cache->file->header.version;
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   125
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   126
    // temp. open
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   127
    if ((ret = pt_cache_open_read_fd(cache, &fd)))
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   128
        return ret;
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   129
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   130
    // read header
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   131
    if ((ret = pt_cache_read_header(fd, &header)))
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   132
        JUMP_ERROR(ret);
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   133
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   134
    // ok
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   135
    ret = header.version;
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   136
    
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   137
error:
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   138
    // close
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   139
    close(fd);
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   140
    
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   141
    return ret;
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   142
}
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   143
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   144
int pt_cache_status (struct pt_cache *cache, const char *img_path)
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   145
{
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   146
    struct stat st_img, st_cache;
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   147
    int ver;
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   148
    
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   149
    // test original file
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   150
    if (stat(img_path, &st_img) < 0)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   151
        RETURN_ERROR(PT_ERR_IMG_STAT);
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   152
    
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   153
    // test cache file
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   154
    if (stat(cache->path, &st_cache) < 0) {
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   155
        // always stale if it doesn't exist yet
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   156
        if (errno == ENOENT)
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   157
            return PT_CACHE_NONE;
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   158
        else
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   159
            RETURN_ERROR(PT_ERR_CACHE_STAT);
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   160
    }
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   161
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   162
    // compare mtime
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   163
    if (st_img.st_mtime > st_cache.st_mtime)
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   164
        return PT_CACHE_STALE;
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   165
    
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   166
    // read version
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   167
    if ((ver = pt_cache_version(cache)) < 0)
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   168
        // fail
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   169
        return ver;
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   170
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   171
    // compare version
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   172
    if (ver != PT_CACHE_VERSION)
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   173
        return PT_CACHE_INCOMPAT;
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   174
    
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   175
    // ok, should be in order
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   176
    return PT_CACHE_FRESH;
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   177
}
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   178
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   179
void pt_cache_info (struct pt_cache *cache, struct pt_image_info *info)
10
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   180
{
54
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   181
    struct stat st;
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   182
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   183
    if (cache->file)
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   184
        // img info
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   185
        pt_png_info(&cache->file->header.png, info);
10
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   186
54
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   187
    // stat
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   188
    if (stat(cache->path, &st) < 0) {
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   189
        // unknown
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   190
        info->cache_mtime = 0;
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   191
        info->cache_bytes = 0;
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   192
        info->cache_blocks = 0;
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   193
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   194
    } else {
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   195
        // store
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   196
        info->cache_version = pt_cache_version(cache);
54
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   197
        info->cache_mtime = st.st_mtime;
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   198
        info->cache_bytes = st.st_size;
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   199
        info->cache_blocks = st.st_blocks;
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   200
    }
10
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   201
}
6806a90d934f make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   202
66
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   203
static int pt_cache_tmp_name (struct pt_cache *cache, char tmp_path[1024])
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   204
{
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   205
    // get .tmp path
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   206
    if (path_with_fext(cache->path, tmp_path, sizeof(tmp_path), ".tmp"))
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   207
        RETURN_ERROR(PT_ERR_PATH);
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   208
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   209
    return 0;
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   210
}
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   211
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   212
/**
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   213
 * Open the .tmp cache file as an fd for writing
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   214
 */
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   215
static int pt_cache_open_tmp_fd (struct pt_cache *cache, int *fd_ptr)
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   216
{
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   217
    int fd;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   218
    char tmp_path[1024];
66
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   219
    int err;
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   220
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   221
    // get .tmp path
66
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   222
    if ((err = pt_cache_tmp_name(cache, tmp_path)))
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   223
        return err;
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   224
66
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   225
    // open for write, create, fail if someone else already opened it for update
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   226
    if ((fd = open(tmp_path, O_RDWR | O_CREAT | O_EXCL, 0644)) < 0)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   227
        RETURN_ERROR(PT_ERR_CACHE_OPEN_TMP);
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   228
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   229
    // ok
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   230
    *fd_ptr = fd;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   231
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   232
    return 0;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   233
}
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   234
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   235
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   236
/**
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   237
 * Mmap the pt_cache_file using sizeof(struct pt_cache_file) + data_size
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   238
 */
60
cb6407844ab5 have pt_cache_open_mmap take a struct pt_cache_file ** to avoid nasty strict-aliasing-breaking cast
Tero Marttila <terom@fixme.fi>
parents: 59
diff changeset
   239
static int pt_cache_open_mmap (struct pt_cache *cache, struct pt_cache_file **file_ptr, size_t data_size, bool readonly)
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   240
{
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   241
    int prot = 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   242
    void *addr;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   243
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   244
    // determine prot
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   245
    prot |= PROT_READ;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   246
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   247
    if (!readonly) {
11
eb2a1472f084 PT_OPEN_UPDATE
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   248
        assert(cache->mode & PT_OPEN_UPDATE);
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   249
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   250
        prot |= PROT_WRITE;
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   251
    }
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   252
53
8a3165c604f8 fix cache->size calculation to not include the header
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   253
    // mmap() the full file including header
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   254
    if ((addr = mmap(NULL, sizeof(struct pt_cache_file) + data_size, prot, MAP_SHARED, cache->fd, 0)) == MAP_FAILED)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   255
        RETURN_ERROR(PT_ERR_CACHE_MMAP);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   256
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   257
    // ok
60
cb6407844ab5 have pt_cache_open_mmap take a struct pt_cache_file ** to avoid nasty strict-aliasing-breaking cast
Tero Marttila <terom@fixme.fi>
parents: 59
diff changeset
   258
    *file_ptr = addr;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   259
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   260
    return 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   261
}
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   262
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   263
int pt_cache_open (struct pt_cache *cache)
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   264
{
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   265
    struct pt_cache_header header;
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   266
    int err;
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   267
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   268
    // ignore if already open
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   269
    if (cache->file)
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   270
        return 0;
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   271
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   272
    // open the .cache in readonly mode
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   273
    if ((err = pt_cache_open_read_fd(cache, &cache->fd)))
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   274
        return err;
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   275
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   276
    // read in header
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   277
    if ((err = pt_cache_read_header(cache->fd, &header)))
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   278
        JUMP_ERROR(err);
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   279
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   280
    // check version
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   281
    if (header.version != PT_CACHE_VERSION)
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   282
        JUMP_SET_ERROR(err, PT_ERR_CACHE_VERSION);
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   283
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   284
    // mmap the header + data
60
cb6407844ab5 have pt_cache_open_mmap take a struct pt_cache_file ** to avoid nasty strict-aliasing-breaking cast
Tero Marttila <terom@fixme.fi>
parents: 59
diff changeset
   285
    if ((err = pt_cache_open_mmap(cache, &cache->file, header.data_size, true)))
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   286
        JUMP_ERROR(err);
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   287
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   288
    // done
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   289
    return 0;
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   290
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   291
error:
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   292
    // cleanup
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   293
    pt_cache_abort(cache);
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   294
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   295
    return err;
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   296
}
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   297
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   298
/**
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   299
 * Write out the cache header into the opened file
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   300
 */
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   301
static int pt_cache_write_header (struct pt_cache *cache, const struct pt_cache_header *header)
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   302
{
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   303
    size_t len = sizeof(*header);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   304
    const char *buf = (const char *) header;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   305
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   306
    // seek to start
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   307
    if (lseek(cache->fd, 0, SEEK_SET) != 0)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   308
        RETURN_ERROR(PT_ERR_CACHE_SEEK);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   309
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   310
    // write out full header
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   311
    while (len) {
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   312
        ssize_t ret;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   313
        
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   314
        // try and write out the header
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   315
        if ((ret = write(cache->fd, buf, len)) <= 0)
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   316
            RETURN_ERROR(PT_ERR_CACHE_WRITE);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   317
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   318
        // update offset
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   319
        buf += ret;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   320
        len -= ret;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   321
    }
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   322
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   323
    // done
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   324
    return 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   325
}
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   326
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   327
/**
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   328
 * Create a new .tmp cache file, open it, and write out the header.
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   329
 */
11
eb2a1472f084 PT_OPEN_UPDATE
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   330
static int pt_cache_create (struct pt_cache *cache, struct pt_cache_header *header)
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   331
{
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   332
    int err;
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   333
66
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   334
    assert(cache->mode & PT_OPEN_UPDATE);
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   335
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   336
    // open as .tmp
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   337
    if ((err = pt_cache_open_tmp_fd(cache, &cache->fd)))
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   338
        return err;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   339
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   340
    // write header
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   341
    if ((err = pt_cache_write_header(cache, header)))
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   342
        JUMP_ERROR(err);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   343
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   344
    // grow file
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   345
    if (ftruncate(cache->fd, sizeof(struct pt_cache_file) + header->data_size) < 0)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   346
        JUMP_SET_ERROR(err, PT_ERR_CACHE_TRUNC);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   347
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   348
    // mmap header and data
60
cb6407844ab5 have pt_cache_open_mmap take a struct pt_cache_file ** to avoid nasty strict-aliasing-breaking cast
Tero Marttila <terom@fixme.fi>
parents: 59
diff changeset
   349
    if ((err = pt_cache_open_mmap(cache, &cache->file, header->data_size, false)))
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   350
        JUMP_ERROR(err);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   351
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   352
    // done
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   353
    return 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   354
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   355
error:
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   356
    // cleanup
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   357
    pt_cache_abort(cache);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   358
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   359
    return err;
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   360
}
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   361
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   362
/**
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   363
 * Rename the opened .tmp to .cache
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   364
 */
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   365
static int pt_cache_create_done (struct pt_cache *cache)
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   366
{
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   367
    char tmp_path[1024];
66
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   368
    int err;
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   369
    
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   370
    // get .tmp path
66
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   371
    if ((err = pt_cache_tmp_name(cache, tmp_path)))
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   372
        return err;
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   373
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   374
    // rename
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   375
    if (rename(tmp_path, cache->path) < 0)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   376
        RETURN_ERROR(PT_ERR_CACHE_RENAME_TMP);
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   377
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   378
    // ok
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   379
    return 0;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   380
}
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   381
66
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   382
/**
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   383
 * Abort a failed cache update after cache_create
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   384
 */
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   385
static void pt_cache_create_abort (struct pt_cache *cache)
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   386
{
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   387
    char tmp_path[1024];
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   388
    int err;
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   389
    
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   390
    // close open stuff
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   391
    pt_cache_abort(cache);
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   392
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   393
    // get .tmp path
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   394
    if ((err = pt_cache_tmp_name(cache, tmp_path))) {
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   395
        log_warn_errno("pt_cache_tmp_name: %s: %s", cache->path, pt_strerror(err));
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   396
        
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   397
        return;
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   398
    }
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   399
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   400
    // remove .tmp
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   401
    if (unlink(tmp_path))
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   402
        log_warn_errno("unlink: %s", tmp_path);
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   403
}
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   404
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   405
int pt_cache_update (struct pt_cache *cache, struct pt_png_img *img, const struct pt_image_params *params)
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   406
{
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   407
    struct pt_cache_header header;
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   408
    int err;
66
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   409
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   410
    // check mode
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   411
    if (!(cache->mode & PT_OPEN_UPDATE))
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   412
        RETURN_ERROR(PT_ERR_OPEN_MODE);
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   413
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   414
    // close if open
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   415
    if ((err = pt_cache_close(cache)))
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   416
        return err;
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   417
    
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   418
    // prep header
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   419
    header.version = PT_CACHE_VERSION;
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   420
    header.format = PT_IMG_PNG;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   421
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   422
    // read img header
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   423
    if ((err = pt_png_read_header(img, &header.png, &header.data_size)))
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   424
        return err;
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   425
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   426
    // save any params
52
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   427
    if (params)
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   428
        header.params = *params;
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   429
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   430
    // create/open .tmp and write out header
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   431
    if ((err = pt_cache_create(cache, &header)))
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   432
        return err;
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   433
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   434
    // decode to disk
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   435
    if ((err = pt_png_decode(img, &cache->file->header.png, &cache->file->header.params, cache->file->data)))
66
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   436
        goto error;
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   437
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   438
    // done, commit .tmp
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   439
    if ((err = pt_cache_create_done(cache)))
66
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   440
        goto error;
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   441
    
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   442
    return 0;
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   443
66
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   444
error:
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   445
    // cleanup .tmp
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   446
    pt_cache_create_abort(cache);
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   447
55949307182c pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents: 60
diff changeset
   448
    return err;
12
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   449
}
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   450
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   451
int pt_cache_tile (struct pt_cache *cache, struct pt_tile *tile)
34
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   452
{
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   453
    int err;
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   454
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   455
    // ensure open
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   456
    if ((err = pt_cache_open(cache)))
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   457
        return err;
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   458
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   459
    // render
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   460
    if ((err = pt_png_tile(&cache->file->header.png, cache->file->data, tile)))
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   461
        return err;
34
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   462
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   463
    return 0;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   464
}
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   465
59
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   466
int pt_cache_close (struct pt_cache *cache)
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   467
{
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   468
    if (cache->file != NULL) {
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   469
        if (munmap(cache->file, sizeof(struct pt_cache_file) + cache->file->header.data_size))
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   470
            RETURN_ERROR(PT_ERR_CACHE_MUNMAP);
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   471
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   472
        cache->file = NULL;
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   473
    }
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   474
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   475
    if (cache->fd >= 0) {
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   476
        if (close(cache->fd))
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   477
            RETURN_ERROR(PT_ERR_CACHE_CLOSE);
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   478
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   479
        cache->fd = -1;
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   480
    }
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   481
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   482
    return 0;
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   483
}
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   484
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   485
void pt_cache_destroy (struct pt_cache *cache)
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   486
{
59
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   487
    // cleanup
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   488
    pt_cache_abort(cache);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   489
59
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   490
    free(cache->path);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   491
    free(cache);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   492
}
11
eb2a1472f084 PT_OPEN_UPDATE
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   493