author | Tero Marttila <terom@qmsk.net> |
Sat, 15 Jul 2017 11:42:22 +0300 | |
changeset 183 | 1f56a81f0c69 |
parent 177 | b2768f3982f3 |
permissions | -rw-r--r-- |
0 | 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 | 5 |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
6 |
#include <stdlib.h> |
177 | 7 |
#include <string.h> |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
8 |
#include <unistd.h> |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
9 |
#include <sys/types.h> |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
10 |
#include <sys/stat.h> |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
11 |
#include <fcntl.h> |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
12 |
#include <sys/mman.h> |
2 | 13 |
#include <errno.h> |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
14 |
#include <assert.h> |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
15 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
16 |
|
9 | 17 |
int pt_cache_new (struct pt_cache **cache_ptr, const char *path, int mode) |
0 | 18 |
{ |
19 |
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
|
20 |
int err; |
0 | 21 |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
22 |
// alloc |
0 | 23 |
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
|
24 |
RETURN_ERROR(PT_ERR_MEM); |
0 | 25 |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
26 |
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
|
27 |
JUMP_SET_ERROR(err, PT_ERR_MEM); |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
28 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
29 |
// init |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
30 |
cache->fd = -1; |
3
da7c6dcafb43
store cache->mode, error handling for pt_image_update_cache
Tero Marttila <terom@fixme.fi>
parents:
2
diff
changeset
|
31 |
cache->mode = mode; |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
32 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
33 |
// ok |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
34 |
*cache_ptr = cache; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
35 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
36 |
return 0; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
37 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
38 |
error: |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
39 |
// cleanup |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
40 |
if (cache) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
41 |
pt_cache_destroy(cache); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
42 |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
15
diff
changeset
|
43 |
return err; |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
44 |
} |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
45 |
|
56
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
46 |
/** |
59 | 47 |
* Force-clean pt_cache, warn on errors |
48 |
*/ |
|
49 |
static void pt_cache_abort (struct pt_cache *cache) |
|
50 |
{ |
|
51 |
if (cache->file != NULL) { |
|
52 |
if (munmap(cache->file, sizeof(struct pt_cache_file) + cache->file->header.data_size)) |
|
53 |
log_warn_errno("munmap: %p, %zu", cache->file, sizeof(struct pt_cache_file) + cache->file->header.data_size); |
|
54 |
||
55 |
cache->file = NULL; |
|
56 |
} |
|
57 |
||
58 |
if (cache->fd >= 0) { |
|
59 |
if (close(cache->fd)) |
|
60 |
log_warn_errno("close: %d", cache->fd); |
|
61 |
||
62 |
cache->fd = -1; |
|
63 |
} |
|
64 |
} |
|
65 |
||
66 |
/** |
|
56
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
67 |
* 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
|
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 |
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
|
70 |
{ |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
71 |
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
|
72 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
73 |
// 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
|
74 |
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
|
75 |
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
|
76 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
77 |
// ok |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
78 |
*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
|
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 |
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
|
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 |
/** |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
84 |
* 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
|
85 |
*/ |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
86 |
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
|
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 |
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
|
89 |
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
|
90 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
91 |
// 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
|
92 |
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
|
93 |
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
|
94 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
95 |
// 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
|
96 |
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
|
97 |
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
|
98 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
99 |
// 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
|
100 |
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
|
101 |
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
|
102 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
103 |
// 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
|
104 |
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
|
105 |
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
|
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 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
108 |
// done |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
109 |
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
|
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 |
/** |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
113 |
* 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
|
114 |
*/ |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
115 |
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
|
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 |
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
|
118 |
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
|
119 |
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
|
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 |
// 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
|
122 |
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
|
123 |
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
|
124 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
125 |
// 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
|
126 |
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
|
127 |
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
|
128 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
129 |
// 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
|
130 |
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
|
131 |
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
|
132 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
133 |
// ok |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
134 |
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
|
135 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
136 |
error: |
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 |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
138 |
close(fd); |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
139 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
140 |
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
|
141 |
} |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
142 |
|
8 | 143 |
int pt_cache_status (struct pt_cache *cache, const char *img_path) |
0 | 144 |
{ |
2 | 145 |
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
|
146 |
int ver; |
2 | 147 |
|
148 |
// test original file |
|
149 |
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
|
150 |
RETURN_ERROR(PT_ERR_IMG_STAT); |
2 | 151 |
|
152 |
// test cache file |
|
153 |
if (stat(cache->path, &st_cache) < 0) { |
|
154 |
// always stale if it doesn't exist yet |
|
155 |
if (errno == ENOENT) |
|
8 | 156 |
return PT_CACHE_NONE; |
2 | 157 |
else |
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
15
diff
changeset
|
158 |
RETURN_ERROR(PT_ERR_CACHE_STAT); |
2 | 159 |
} |
160 |
||
161 |
// compare mtime |
|
8 | 162 |
if (st_img.st_mtime > st_cache.st_mtime) |
163 |
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
|
164 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
165 |
// 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
|
166 |
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
|
167 |
// fail |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
168 |
return ver; |
8 | 169 |
|
56
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
170 |
// 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
|
171 |
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
|
172 |
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
|
173 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
174 |
// 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
|
175 |
return PT_CACHE_FRESH; |
0 | 176 |
} |
177 |
||
56
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
178 |
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
|
179 |
{ |
54
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
53
diff
changeset
|
180 |
struct stat st; |
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
15
diff
changeset
|
181 |
|
56
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
182 |
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
|
183 |
// 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
|
184 |
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
|
185 |
|
54
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
53
diff
changeset
|
186 |
// stat |
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
53
diff
changeset
|
187 |
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
|
188 |
// unknown |
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
53
diff
changeset
|
189 |
info->cache_mtime = 0; |
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
53
diff
changeset
|
190 |
info->cache_bytes = 0; |
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
53
diff
changeset
|
191 |
info->cache_blocks = 0; |
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
53
diff
changeset
|
192 |
|
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
53
diff
changeset
|
193 |
} else { |
4a25113cb2a4
add some additional pt_image_info fields for stat
Tero Marttila <terom@fixme.fi>
parents:
53
diff
changeset
|
194 |
// 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
|
195 |
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
|
196 |
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
|
197 |
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
|
198 |
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
|
199 |
} |
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
|
200 |
} |
6806a90d934f
make pt_cache_open no-op if already open, pt_image_info -> pt_cache_info
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
201 |
|
76 | 202 |
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
|
203 |
{ |
55949307182c
pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents:
60
diff
changeset
|
204 |
// get .tmp path |
76 | 205 |
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
|
206 |
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
|
207 |
|
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 |
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
|
209 |
} |
55949307182c
pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents:
60
diff
changeset
|
210 |
|
0 | 211 |
/** |
71 | 212 |
* Compute and return the full size of the .cache file |
213 |
*/ |
|
214 |
static size_t pt_cache_size (size_t data_size) |
|
215 |
{ |
|
216 |
assert(sizeof(struct pt_cache_file) == PT_CACHE_HEADER_SIZE); |
|
217 |
||
218 |
return sizeof(struct pt_cache_file) + data_size; |
|
219 |
} |
|
220 |
||
221 |
/** |
|
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
222 |
* 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
|
223 |
*/ |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
224 |
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
|
225 |
{ |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
226 |
int fd; |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
227 |
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
|
228 |
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
|
229 |
|
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
230 |
// get .tmp path |
76 | 231 |
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
|
232 |
return err; |
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
233 |
|
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
|
234 |
// 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
|
235 |
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
|
236 |
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
|
237 |
|
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
238 |
// ok |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
239 |
*fd_ptr = fd; |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
240 |
|
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
241 |
return 0; |
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 |
|
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
245 |
/** |
71 | 246 |
* 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
|
247 |
*/ |
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
|
248 |
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
|
249 |
{ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
250 |
int prot = 0; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
251 |
void *addr; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
252 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
253 |
// determine prot |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
254 |
prot |= PROT_READ; |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
255 |
|
9 | 256 |
if (!readonly) { |
11 | 257 |
assert(cache->mode & PT_OPEN_UPDATE); |
9 | 258 |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
259 |
prot |= PROT_WRITE; |
9 | 260 |
} |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
261 |
|
53
8a3165c604f8
fix cache->size calculation to not include the header
Tero Marttila <terom@fixme.fi>
parents:
52
diff
changeset
|
262 |
// mmap() the full file including header |
71 | 263 |
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
|
264 |
RETURN_ERROR(PT_ERR_CACHE_MMAP); |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
265 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
266 |
// 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
|
267 |
*file_ptr = addr; |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
268 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
269 |
return 0; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
270 |
} |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
271 |
|
56
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
272 |
int pt_cache_open (struct pt_cache *cache) |
9 | 273 |
{ |
56
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
274 |
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
|
275 |
int err; |
9 | 276 |
|
56
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
277 |
// 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
|
278 |
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
|
279 |
return 0; |
9 | 280 |
|
56
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
281 |
// 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
|
282 |
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
|
283 |
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
|
284 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
285 |
// 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
|
286 |
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
|
287 |
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
|
288 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
289 |
// 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
|
290 |
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
|
291 |
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
|
292 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
293 |
// 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
|
294 |
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
|
295 |
JUMP_ERROR(err); |
9 | 296 |
|
297 |
// done |
|
298 |
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
|
299 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
300 |
error: |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
301 |
// cleanup |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
302 |
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
|
303 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
304 |
return err; |
9 | 305 |
} |
306 |
||
307 |
/** |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
308 |
* Write out the cache header into the opened file |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
309 |
*/ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
310 |
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
|
311 |
{ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
312 |
size_t len = sizeof(*header); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
313 |
const char *buf = (const char *) header; |
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 |
// seek to start |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
316 |
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
|
317 |
RETURN_ERROR(PT_ERR_CACHE_SEEK); |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
318 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
319 |
// write out full header |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
320 |
while (len) { |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
321 |
ssize_t ret; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
322 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
323 |
// 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
|
324 |
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
|
325 |
RETURN_ERROR(PT_ERR_CACHE_WRITE); |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
326 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
327 |
// update offset |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
328 |
buf += ret; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
329 |
len -= ret; |
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 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
332 |
// done |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
333 |
return 0; |
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 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
336 |
/** |
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
337 |
* Create a new .tmp cache file, open it, and write out the header. |
0 | 338 |
*/ |
11 | 339 |
static int pt_cache_create (struct pt_cache *cache, struct pt_cache_header *header) |
0 | 340 |
{ |
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
15
diff
changeset
|
341 |
int err; |
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
342 |
|
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
|
343 |
assert(cache->mode & PT_OPEN_UPDATE); |
2 | 344 |
|
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
345 |
// open as .tmp |
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
15
diff
changeset
|
346 |
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
|
347 |
return err; |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
348 |
|
56
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
349 |
// 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
|
350 |
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
|
351 |
JUMP_ERROR(err); |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
352 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
353 |
// grow file |
71 | 354 |
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
|
355 |
JUMP_SET_ERROR(err, PT_ERR_CACHE_TRUNC); |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
356 |
|
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
357 |
// 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
|
358 |
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
|
359 |
JUMP_ERROR(err); |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
360 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
361 |
// done |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
362 |
return 0; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
363 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
364 |
error: |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
365 |
// cleanup |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
366 |
pt_cache_abort(cache); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
367 |
|
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
15
diff
changeset
|
368 |
return err; |
0 | 369 |
} |
370 |
||
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
371 |
/** |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
372 |
* 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
|
373 |
*/ |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
374 |
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
|
375 |
{ |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
376 |
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
|
377 |
int err; |
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
378 |
|
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
379 |
// get .tmp path |
76 | 380 |
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
|
381 |
return err; |
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
382 |
|
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
383 |
// rename |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
384 |
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
|
385 |
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
|
386 |
|
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
387 |
// ok |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
388 |
return 0; |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
389 |
} |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
390 |
|
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
|
391 |
/** |
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 |
* 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
|
393 |
*/ |
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 |
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
|
395 |
{ |
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 |
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
|
397 |
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
|
398 |
|
55949307182c
pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents:
60
diff
changeset
|
399 |
// 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
|
400 |
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
|
401 |
|
55949307182c
pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents:
60
diff
changeset
|
402 |
// get .tmp path |
76 | 403 |
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
|
404 |
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
|
405 |
|
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 |
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
|
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 |
|
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 |
// 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
|
410 |
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
|
411 |
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
|
412 |
} |
55949307182c
pt_cache_update checks mode, aborts .tmp, and use O_EXCL for opening the .tmp
Tero Marttila <terom@fixme.fi>
parents:
60
diff
changeset
|
413 |
|
56
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
414 |
int pt_cache_update (struct pt_cache *cache, struct pt_png_img *img, const struct pt_image_params *params) |
0 | 415 |
{ |
416 |
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
|
417 |
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
|
418 |
|
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 |
// 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
|
420 |
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
|
421 |
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
|
422 |
|
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 |
// 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
|
424 |
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
|
425 |
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
|
426 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
427 |
// 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
|
428 |
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
|
429 |
header.format = PT_IMG_PNG; |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
430 |
|
56
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
431 |
// 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
|
432 |
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
|
433 |
return err; |
6
766df7c9b90d
--force-update and store palette
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
434 |
|
56
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
435 |
// save any params |
52
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
436 |
if (params) |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
437 |
header.params = *params; |
148a120ea7d5
skip contiguous regions of some background color
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
438 |
|
56
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
439 |
// 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
|
440 |
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
|
441 |
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
|
442 |
|
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
443 |
// 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
|
444 |
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
|
445 |
goto error; |
7
997906f5fd2d
mmap header, implement pt_image_info (post-update)
Tero Marttila <terom@fixme.fi>
parents:
6
diff
changeset
|
446 |
|
56
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
447 |
// done, commit .tmp |
17
baf3fe7c6354
add library error codes, and fix image fopen error handling
Tero Marttila <terom@fixme.fi>
parents:
15
diff
changeset
|
448 |
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
|
449 |
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
|
450 |
|
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
|
451 |
return 0; |
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
452 |
|
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
|
453 |
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
|
454 |
// 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
|
455 |
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
|
456 |
|
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
|
457 |
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
|
458 |
} |
3576e00388fd
draw clipped tiles by filling in a zeros for the background pixels
Tero Marttila <terom@fixme.fi>
parents:
11
diff
changeset
|
459 |
|
56
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
460 |
int pt_cache_tile (struct pt_cache *cache, struct pt_tile *tile) |
34 | 461 |
{ |
462 |
int err; |
|
463 |
||
464 |
// ensure open |
|
465 |
if ((err = pt_cache_open(cache))) |
|
466 |
return err; |
|
467 |
||
56
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
468 |
// render |
d5e3089906da
major refactoring of pt_cache, split off all PNG processing into pt_png
Tero Marttila <terom@fixme.fi>
parents:
55
diff
changeset
|
469 |
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
|
470 |
return err; |
34 | 471 |
|
9 | 472 |
return 0; |
473 |
} |
|
474 |
||
59 | 475 |
int pt_cache_close (struct pt_cache *cache) |
476 |
{ |
|
477 |
if (cache->file != NULL) { |
|
478 |
if (munmap(cache->file, sizeof(struct pt_cache_file) + cache->file->header.data_size)) |
|
479 |
RETURN_ERROR(PT_ERR_CACHE_MUNMAP); |
|
480 |
||
481 |
cache->file = NULL; |
|
482 |
} |
|
483 |
||
484 |
if (cache->fd >= 0) { |
|
485 |
if (close(cache->fd)) |
|
486 |
RETURN_ERROR(PT_ERR_CACHE_CLOSE); |
|
487 |
||
488 |
cache->fd = -1; |
|
489 |
} |
|
490 |
||
491 |
return 0; |
|
492 |
} |
|
493 |
||
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
494 |
void pt_cache_destroy (struct pt_cache *cache) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
495 |
{ |
59 | 496 |
// cleanup |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
497 |
pt_cache_abort(cache); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
498 |
|
59 | 499 |
free(cache->path); |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
500 |
free(cache); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
501 |
} |
11 | 502 |