src/lib/cache.c
author Tero Marttila <terom@fixme.fi>
Mon, 25 Jan 2010 01:41:25 +0200
changeset 56 d5e3089906da
parent 55 a3542e78ecd8
child 59 80135bdfd343
permissions -rw-r--r--
major refactoring of pt_cache, split off all PNG processing into pt_png
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
/**
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    46
 * 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
    47
 *
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    48
 * 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
    49
 */
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    50
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
    51
{
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    52
    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
    53
    
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    54
    // 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
    55
    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
    56
        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
    57
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    58
    // ok
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    59
    *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
    60
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    61
    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
    62
}
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    63
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    64
/**
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    65
 * 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
    66
 */
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    67
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
    68
{
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    69
    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
    70
    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
    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
    // 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
    73
    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
    74
        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
    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
    // 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
    77
    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
    78
        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
    79
        
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    80
        // 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
    81
        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
    82
            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
    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
        // 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
    85
        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
    86
        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
    87
    }
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
    // done
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    90
    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
    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
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
 * 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
    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
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
    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
    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
    99
    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
   100
    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
   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
    // 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
   103
    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
   104
        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
   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
    // 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
   107
    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
   108
        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
   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
    // 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
   111
    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
   112
        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
   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
    // ok
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   115
    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
   116
    
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   117
error:
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   118
    // close
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   119
    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
   120
    
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   121
    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
   122
}
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   123
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   124
int pt_cache_status (struct pt_cache *cache, const char *img_path)
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   125
{
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   126
    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
   127
    int ver;
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   128
    
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   129
    // test original file
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   130
    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
   131
        RETURN_ERROR(PT_ERR_IMG_STAT);
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   132
    
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   133
    // test cache file
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   134
    if (stat(cache->path, &st_cache) < 0) {
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   135
        // always stale if it doesn't exist yet
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   136
        if (errno == ENOENT)
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   137
            return PT_CACHE_NONE;
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   138
        else
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   139
            RETURN_ERROR(PT_ERR_CACHE_STAT);
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   140
    }
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   141
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   142
    // compare mtime
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   143
    if (st_img.st_mtime > st_cache.st_mtime)
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   144
        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
   145
    
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   146
    // 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
   147
    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
   148
        // fail
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   149
        return ver;
8
400ddf1e7aa9 pt_image_status
Tero Marttila <terom@fixme.fi>
parents: 7
diff changeset
   150
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   151
    // 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
   152
    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
   153
        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
   154
    
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   155
    // 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
   156
    return PT_CACHE_FRESH;
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   157
}
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   158
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   159
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
   160
{
54
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   161
    struct stat st;
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   162
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
    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
   164
        // 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
   165
        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
   166
54
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   167
    // stat
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   168
    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
   169
        // unknown
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   170
        info->cache_mtime = 0;
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   171
        info->cache_bytes = 0;
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   172
        info->cache_blocks = 0;
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   173
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   174
    } else {
4a25113cb2a4 add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents: 53
diff changeset
   175
        // 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
   176
        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
   177
        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
   178
        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
   179
        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
   180
    }
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
   181
}
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
   182
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   183
/**
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   184
 * Abort any incomplete open operation, cleaning up
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   185
 */
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   186
static void pt_cache_abort (struct pt_cache *cache)
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   187
{
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   188
    if (cache->file != NULL) {
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   189
        munmap(cache->file, sizeof(struct pt_cache_file) + cache->file->header.data_size);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   190
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   191
        cache->file = NULL;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   192
    }
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   193
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   194
    if (cache->fd >= 0) {
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   195
        close(cache->fd);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   196
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   197
        cache->fd = -1;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   198
    }
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   199
}
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   200
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   201
/**
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   202
 * 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
   203
 */
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   204
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
   205
{
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   206
    int fd;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   207
    char tmp_path[1024];
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   208
    
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   209
    // get .tmp path
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   210
    if (path_with_fext(cache->path, tmp_path, sizeof(tmp_path), ".tmp"))
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   211
        RETURN_ERROR(PT_ERR_PATH);
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   212
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   213
    // open for write, create
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   214
    // XXX: locking? At least O_EXCL...
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   215
    if ((fd = open(tmp_path, O_RDWR | O_CREAT, 0644)) < 0)
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   216
        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
   217
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   218
    // ok
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   219
    *fd_ptr = fd;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   220
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   221
    return 0;
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   222
}
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   223
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
/**
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   226
 * 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
   227
 */
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   228
static int pt_cache_open_mmap (struct pt_cache *cache, void **addr_ptr, size_t data_size, bool readonly)
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   229
{
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   230
    int prot = 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   231
    void *addr;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   232
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   233
    // determine prot
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   234
    prot |= PROT_READ;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   235
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   236
    if (!readonly) {
11
eb2a1472f084 PT_OPEN_UPDATE
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   237
        assert(cache->mode & PT_OPEN_UPDATE);
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   238
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   239
        prot |= PROT_WRITE;
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   240
    }
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   241
53
8a3165c604f8 fix cache->size calculation to not include the header
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   242
    // 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
   243
    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
   244
        RETURN_ERROR(PT_ERR_CACHE_MMAP);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   245
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   246
    // ok
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   247
    *addr_ptr = addr;
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
    return 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   250
}
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   251
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   252
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
   253
{
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
    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
   255
    int err;
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   256
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   257
    // 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
   258
    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
   259
        return 0;
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   260
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   261
    // 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
   262
    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
   263
        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
   264
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   265
    // 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
   266
    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
   267
        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
   268
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   269
    // 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
   270
    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
   271
        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
   272
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   273
    // mmap the header + data
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   274
    if ((err = pt_cache_open_mmap(cache, (void **) &cache->file, header.data_size, true)))
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   275
        JUMP_ERROR(err);
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   276
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   277
    // done
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   278
    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
   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
error:
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   281
    // cleanup
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   282
    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
   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
    return err;
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   285
}
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   286
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   287
/**
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   288
 * Write out the cache header into the opened file
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   289
 */
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   290
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
   291
{
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   292
    size_t len = sizeof(*header);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   293
    const char *buf = (const char *) header;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   294
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   295
    // seek to start
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   296
    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
   297
        RETURN_ERROR(PT_ERR_CACHE_SEEK);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   298
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   299
    // write out full header
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   300
    while (len) {
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   301
        ssize_t ret;
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
        // 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
   304
        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
   305
            RETURN_ERROR(PT_ERR_CACHE_WRITE);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   306
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   307
        // update offset
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   308
        buf += ret;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   309
        len -= ret;
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
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   312
    // done
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   313
    return 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   314
}
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   315
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   316
/**
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   317
 * 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
   318
 */
11
eb2a1472f084 PT_OPEN_UPDATE
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   319
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
   320
{
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   321
    int err;
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   322
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   323
    // no access
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   324
    if (!(cache->mode & PT_OPEN_UPDATE))
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   325
        RETURN_ERROR(PT_ERR_OPEN_MODE);
2
05de54150a4c up to a test client
Tero Marttila <terom@fixme.fi>
parents: 1
diff changeset
   326
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   327
    // open as .tmp
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   328
    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
   329
        return err;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   330
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   331
    // 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
   332
    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
   333
        JUMP_ERROR(err);
1
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
    // 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
   336
    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
   337
        JUMP_SET_ERROR(err, PT_ERR_CACHE_TRUNC);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   338
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   339
    // mmap header and data
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
    if ((err = pt_cache_open_mmap(cache, (void **) &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
   341
        JUMP_ERROR(err);
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   342
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   343
    // done
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   344
    return 0;
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   345
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   346
error:
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   347
    // cleanup
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   348
    pt_cache_abort(cache);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   349
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   350
    return err;
0
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   351
}
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   352
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   353
/**
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   354
 * 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
   355
 */
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   356
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
   357
{
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   358
    char tmp_path[1024];
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   359
    
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   360
    // get .tmp path
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   361
    if (path_with_fext(cache->path, tmp_path, sizeof(tmp_path), ".tmp"))
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   362
        RETURN_ERROR(PT_ERR_PATH);
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   363
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   364
    // rename
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   365
    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
   366
        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
   367
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   368
    // ok
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   369
    return 0;
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
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   372
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
   373
{
cff7fac35cc2 initial code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   374
    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
   375
    int err;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   376
    
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   377
    // XXX: check cache_mode
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   378
    // XXX: close any already-opened cache 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
   379
    
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   380
    // 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
   381
    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
   382
    header.format = PT_IMG_PNG;
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   383
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   384
    // 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
   385
    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
   386
        return err;
6
766df7c9b90d --force-update and store palette
Tero Marttila <terom@fixme.fi>
parents: 4
diff changeset
   387
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   388
    // save any params
52
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   389
    if (params)
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   390
        header.params = *params;
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   391
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   392
    // 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
   393
    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
   394
        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
   395
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   396
    // 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
   397
    if ((err = pt_png_decode(img, &cache->file->header.png, &cache->file->header.params, cache->file->data)))
52
148a120ea7d5 skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents: 34
diff changeset
   398
        return err;
7
997906f5fd2d mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents: 6
diff changeset
   399
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   400
    // done, commit .tmp
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   401
    if ((err = pt_cache_create_done(cache)))
18
f92a24ab046e add missing lib/tile.*
Tero Marttila <terom@fixme.fi>
parents: 17
diff changeset
   402
        // XXX: pt_cache_abort?
17
baf3fe7c6354 add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents: 15
diff changeset
   403
        return err;
4
49362b34116c open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents: 3
diff changeset
   404
12
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   405
    return 0;
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   406
}
3576e00388fd draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents: 11
diff changeset
   407
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   408
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
   409
{
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   410
    int err;
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   411
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   412
    // ensure open
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   413
    if ((err = pt_cache_open(cache)))
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   414
        return err;
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   415
56
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   416
    // render
d5e3089906da major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
   417
    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
   418
        return err;
34
a387bc77ad52 implement zoom
Tero Marttila <terom@fixme.fi>
parents: 18
diff changeset
   419
9
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   420
    return 0;
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   421
}
a31048ff76a2 pt_cache_open, pt_image_tile
Tero Marttila <terom@fixme.fi>
parents: 8
diff changeset
   422
1
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   423
void pt_cache_destroy (struct pt_cache *cache)
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   424
{
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   425
    free(cache->path);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   426
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   427
    pt_cache_abort(cache);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   428
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   429
    free(cache);
f3cde3db1fef basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents: 0
diff changeset
   430
}
11
eb2a1472f084 PT_OPEN_UPDATE
Tero Marttila <terom@fixme.fi>
parents: 10
diff changeset
   431