src/lib/cache.c
author Tero Marttila <terom@fixme.fi>
Tue, 26 Jan 2010 21:22:43 +0200
changeset 123 81d1cad8b588
parent 76 2e07f7dccb67
child 177 b2768f3982f3
permissions -rw-r--r--
docfix README for new Makefile
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
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
    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
    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
    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
    // 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
    73
    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
    74
        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
    75
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    76
    // ok
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    77
    *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
    78
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    79
    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
    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
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
 * 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
    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
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
    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
    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
    88
    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
    89
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    90
    // 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
    91
    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
    92
        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
    93
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    94
    // 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
    95
    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
    96
        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
    97
        
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    98
        // 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
    99
        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
   100
            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
   101
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   102
        // 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
   103
        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
   104
        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
   105
    }
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   106
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   107
    // done
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   108
    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
   109
}
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   110
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
 * 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
   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
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
   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
    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
   117
    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
   118
    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
   119
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   120
    // 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
   121
    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
   122
        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
   123
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   124
    // 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
   125
    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
   126
        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
   127
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   128
    // 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
   129
    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
   130
        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
   131
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   132
    // ok
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   133
    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
   134
    
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   135
error:
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   136
    // close
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   137
    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
   138
    
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   139
    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
   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
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   142
int pt_cache_status (struct pt_cache *cache, const char *img_path)
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   143
{
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   144
    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
   145
    int ver;
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   146
    
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   147
    // test original file
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   148
    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
   149
        RETURN_ERROR(PT_ERR_IMG_STAT);
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   150
    
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   151
    // test cache file
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   152
    if (stat(cache->path, &st_cache) < 0) {
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   153
        // always stale if it doesn't exist yet
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   154
        if (errno == ENOENT)
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   155
            return PT_CACHE_NONE;
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   156
        else
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   157
            RETURN_ERROR(PT_ERR_CACHE_STAT);
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   158
    }
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   159
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   160
    // compare mtime
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   161
    if (st_img.st_mtime > st_cache.st_mtime)
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   162
        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
   163
    
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   164
    // 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
   165
    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
   166
        // fail
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   167
        return ver;
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   168
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   169
    // 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
   170
    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
   171
        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
   172
    
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   173
    // 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
   174
    return PT_CACHE_FRESH;
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   175
}
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   176
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   177
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
   178
{
54
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   179
    struct stat st;
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   180
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   181
    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
   182
        // 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
   183
        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
   184
54
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   185
    // stat
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   186
    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
   187
        // unknown
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   188
        info->cache_mtime = 0;
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   189
        info->cache_bytes = 0;
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   190
        info->cache_blocks = 0;
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   191
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   192
    } else {
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   193
        // 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
   194
        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
   195
        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
   196
        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
   197
        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
   198
    }
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
   199
}
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
   200
76
2e07f7dccb67 fix pt_cache_tmp_name bug
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   201
static int pt_cache_tmp_name (struct pt_cache *cache, char tmp_path[], size_t tmp_len)
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
   202
{
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
    // get .tmp path
76
2e07f7dccb67 fix pt_cache_tmp_name bug
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   204
    if (path_with_fext(cache->path, tmp_path, tmp_len, ".tmp"))
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
   205
        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
   206
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 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
   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
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   210
/**
71
01b021e406e9 pt_cache_size
Tero Marttila <terom@fixme.fi>
parents: 67
diff changeset
   211
 * Compute and return the full size of the .cache file
01b021e406e9 pt_cache_size
Tero Marttila <terom@fixme.fi>
parents: 67
diff changeset
   212
 */
01b021e406e9 pt_cache_size
Tero Marttila <terom@fixme.fi>
parents: 67
diff changeset
   213
static size_t pt_cache_size (size_t data_size)
01b021e406e9 pt_cache_size
Tero Marttila <terom@fixme.fi>
parents: 67
diff changeset
   214
{
01b021e406e9 pt_cache_size
Tero Marttila <terom@fixme.fi>
parents: 67
diff changeset
   215
    assert(sizeof(struct pt_cache_file) == PT_CACHE_HEADER_SIZE);
01b021e406e9 pt_cache_size
Tero Marttila <terom@fixme.fi>
parents: 67
diff changeset
   216
01b021e406e9 pt_cache_size
Tero Marttila <terom@fixme.fi>
parents: 67
diff changeset
   217
    return sizeof(struct pt_cache_file) + data_size;
01b021e406e9 pt_cache_size
Tero Marttila <terom@fixme.fi>
parents: 67
diff changeset
   218
}
01b021e406e9 pt_cache_size
Tero Marttila <terom@fixme.fi>
parents: 67
diff changeset
   219
01b021e406e9 pt_cache_size
Tero Marttila <terom@fixme.fi>
parents: 67
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
 * 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
   222
 */
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   223
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
   224
{
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   225
    int fd;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   226
    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
   227
    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
   228
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   229
    // get .tmp path
76
2e07f7dccb67 fix pt_cache_tmp_name bug
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   230
    if ((err = pt_cache_tmp_name(cache, tmp_path, sizeof(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
   231
        return err;
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   232
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
   233
    // 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
   234
    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
   235
        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
   236
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   237
    // ok
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   238
    *fd_ptr = fd;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   239
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   240
    return 0;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   241
}
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   242
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   243
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   244
/**
71
01b021e406e9 pt_cache_size
Tero Marttila <terom@fixme.fi>
parents: 67
diff changeset
   245
 * Mmap the pt_cache_file using pt_cache_size(data_size)
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   246
 */
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
   247
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
   248
{
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   249
    int prot = 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   250
    void *addr;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   251
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   252
    // determine prot
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   253
    prot |= PROT_READ;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   254
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   255
    if (!readonly) {
11
eb2a1472f084 PT_OPEN_UPDATE
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   256
        assert(cache->mode & PT_OPEN_UPDATE);
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   257
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   258
        prot |= PROT_WRITE;
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   259
    }
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   260
53
8a3165c604f8 fix cache->size calculation to not include the header
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   261
    // mmap() the full file including header
71
01b021e406e9 pt_cache_size
Tero Marttila <terom@fixme.fi>
parents: 67
diff changeset
   262
    if ((addr = mmap(NULL, pt_cache_size(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
   263
        RETURN_ERROR(PT_ERR_CACHE_MMAP);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   264
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   265
    // 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
   266
    *file_ptr = addr;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   267
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   268
    return 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   269
}
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   270
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   271
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
   272
{
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   273
    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
   274
    int err;
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   275
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   276
    // 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
   277
    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
   278
        return 0;
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   279
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   280
    // 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
   281
    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
   282
        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
   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
    // 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
   285
    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
   286
        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
   287
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   288
    // 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
   289
    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
   290
        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
   291
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   292
    // 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
   293
    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
   294
        JUMP_ERROR(err);
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   295
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   296
    // done
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   297
    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
   298
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   299
error:
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   300
    // cleanup
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   301
    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
   302
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   303
    return err;
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   304
}
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   305
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   306
/**
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   307
 * Write out the cache header into the opened file
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   308
 */
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   309
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
   310
{
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   311
    size_t len = sizeof(*header);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   312
    const char *buf = (const char *) header;
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
    // seek to start
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   315
    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
   316
        RETURN_ERROR(PT_ERR_CACHE_SEEK);
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
    // write out full header
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   319
    while (len) {
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   320
        ssize_t 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
        // 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
   323
        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
   324
            RETURN_ERROR(PT_ERR_CACHE_WRITE);
1
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
        // update offset
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   327
        buf += ret;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   328
        len -= ret;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   329
    }
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   330
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   331
    // done
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   332
    return 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   333
}
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   334
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
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
 * 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
   337
 */
11
eb2a1472f084 PT_OPEN_UPDATE
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   338
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
   339
{
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   340
    int err;
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   341
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
   342
    assert(cache->mode & PT_OPEN_UPDATE);
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   343
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   344
    // open as .tmp
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   345
    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
   346
        return err;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   347
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   348
    // 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
   349
    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
   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
    // grow file
71
01b021e406e9 pt_cache_size
Tero Marttila <terom@fixme.fi>
parents: 67
diff changeset
   353
    if (ftruncate(cache->fd, pt_cache_size(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
   354
        JUMP_SET_ERROR(err, PT_ERR_CACHE_TRUNC);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   355
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   356
    // 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
   357
    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
   358
        JUMP_ERROR(err);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   359
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   360
    // done
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   361
    return 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   362
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   363
error:
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   364
    // cleanup
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   365
    pt_cache_abort(cache);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   366
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   367
    return err;
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   368
}
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   369
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   370
/**
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   371
 * 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
   372
 */
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   373
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
   374
{
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   375
    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
   376
    int err;
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
    // get .tmp path
76
2e07f7dccb67 fix pt_cache_tmp_name bug
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   379
    if ((err = pt_cache_tmp_name(cache, tmp_path, sizeof(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
   380
        return err;
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   381
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   382
    // rename
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   383
    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
   384
        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
   385
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   386
    // ok
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   387
    return 0;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   388
}
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   389
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
   390
/**
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
 * 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
   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
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
   394
{
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
    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
   396
    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
   397
    
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
    // 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
   399
    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
   400
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
    // get .tmp path
76
2e07f7dccb67 fix pt_cache_tmp_name bug
Tero Marttila <terom@fixme.fi>
parents: 71
diff changeset
   402
    if ((err = pt_cache_tmp_name(cache, tmp_path, sizeof(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
   403
        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
   404
        
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
   405
        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
   406
    }
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
   407
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
   408
    // 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
   409
    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
   410
        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
   411
}
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
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   413
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
   414
{
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   415
    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
   416
    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
   417
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
   418
    // 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
   419
    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
   420
        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
   421
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
   422
    // 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
   423
    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
   424
        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
   425
    
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   426
    // 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
   427
    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
   428
    header.format = PT_IMG_PNG;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
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
    // 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
   431
    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
   432
        return err;
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   433
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   434
    // save any params
52
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   435
    if (params)
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   436
        header.params = *params;
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
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
    // 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
   439
    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
   440
        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
   441
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   442
    // 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
   443
    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
   444
        goto error;
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   445
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   446
    // done, commit .tmp
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   447
    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
   448
        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
   449
    
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
   450
    return 0;
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   451
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
   452
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
   453
    // 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
   454
    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
   455
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
   456
    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
   457
}
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
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
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
   460
{
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   461
    int err;
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   462
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   463
    // ensure open
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   464
    if ((err = pt_cache_open(cache)))
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   465
        return err;
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   466
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   467
    // render
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   468
    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
   469
        return err;
34
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   470
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   471
    return 0;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   472
}
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   473
59
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   474
int pt_cache_close (struct pt_cache *cache)
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   475
{
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   476
    if (cache->file != NULL) {
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   477
        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
   478
            RETURN_ERROR(PT_ERR_CACHE_MUNMAP);
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   479
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   480
        cache->file = NULL;
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
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   483
    if (cache->fd >= 0) {
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   484
        if (close(cache->fd))
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   485
            RETURN_ERROR(PT_ERR_CACHE_CLOSE);
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   486
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   487
        cache->fd = -1;
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   488
    }
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   489
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   490
    return 0;
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   491
}
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   492
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   493
void pt_cache_destroy (struct pt_cache *cache)
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   494
{
59
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   495
    // cleanup
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   496
    pt_cache_abort(cache);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   497
59
80135bdfd343 pt_cache_close
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   498
    free(cache->path);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   499
    free(cache);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   500
}
11
eb2a1472f084 PT_OPEN_UPDATE
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   501