author | Tero Marttila <terom@fixme.fi> |
Mon, 28 Dec 2009 20:36:29 +0200 | |
changeset 4 | 49362b34116c |
parent 3 | da7c6dcafb43 |
child 6 | 766df7c9b90d |
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" |
0 | 3 |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
4 |
#include <stdlib.h> |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
5 |
#include <unistd.h> |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
6 |
#include <sys/types.h> |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
7 |
#include <sys/stat.h> |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
8 |
#include <fcntl.h> |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
9 |
#include <sys/mman.h> |
2 | 10 |
#include <errno.h> |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
11 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
12 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
13 |
|
3
da7c6dcafb43
store cache->mode, error handling for pt_image_update_cache
Tero Marttila <terom@fixme.fi>
parents:
2
diff
changeset
|
14 |
static int pt_cache_new (struct pt_cache **cache_ptr, const char *path, int mode) |
0 | 15 |
{ |
16 |
struct pt_cache *cache; |
|
17 |
||
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
18 |
// alloc |
0 | 19 |
if ((cache = calloc(1, sizeof(*cache))) == NULL) |
20 |
return -1; |
|
21 |
||
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
22 |
if ((cache->path = strdup(path)) == NULL) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
23 |
goto error; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
24 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
25 |
// init |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
26 |
cache->fd = -1; |
3
da7c6dcafb43
store cache->mode, error handling for pt_image_update_cache
Tero Marttila <terom@fixme.fi>
parents:
2
diff
changeset
|
27 |
cache->mode = mode; |
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 |
// ok |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
30 |
*cache_ptr = cache; |
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 |
return 0; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
33 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
34 |
error: |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
35 |
// cleanup |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
36 |
if (cache) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
37 |
pt_cache_destroy(cache); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
38 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
39 |
return -1; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
40 |
} |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
41 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
42 |
int pt_cache_open (struct pt_cache **cache_ptr, const char *path, int mode) |
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 |
struct pt_cache *cache; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
45 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
46 |
// alloc |
3
da7c6dcafb43
store cache->mode, error handling for pt_image_update_cache
Tero Marttila <terom@fixme.fi>
parents:
2
diff
changeset
|
47 |
if (pt_cache_new(&cache, path, mode)) |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
48 |
return -1; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
49 |
|
0 | 50 |
// ok |
51 |
*cache_ptr = cache; |
|
52 |
||
53 |
return 0; |
|
54 |
} |
|
55 |
||
2 | 56 |
int pt_cache_stale (struct pt_cache *cache, const char *img_path) |
0 | 57 |
{ |
2 | 58 |
struct stat st_img, st_cache; |
59 |
||
60 |
// test original file |
|
61 |
if (stat(img_path, &st_img) < 0) |
|
62 |
return -1; |
|
63 |
||
64 |
// test cache file |
|
65 |
if (stat(cache->path, &st_cache) < 0) { |
|
66 |
// always stale if it doesn't exist yet |
|
67 |
if (errno == ENOENT) |
|
68 |
return 1; |
|
69 |
else |
|
70 |
return -1; |
|
71 |
} |
|
72 |
||
73 |
// compare mtime |
|
74 |
return (st_img.st_mtime > st_cache.st_mtime); |
|
0 | 75 |
} |
76 |
||
77 |
/** |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
78 |
* Abort any incomplete open operation, cleaning up |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
79 |
*/ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
80 |
static void pt_cache_abort (struct pt_cache *cache) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
81 |
{ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
82 |
if (cache->mmap != NULL) { |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
83 |
munmap(cache->mmap, cache->size); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
84 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
85 |
cache->mmap = NULL; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
86 |
} |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
87 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
88 |
if (cache->fd >= 0) { |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
89 |
close(cache->fd); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
90 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
91 |
cache->fd = -1; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
92 |
} |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
93 |
} |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
94 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
95 |
/** |
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
96 |
* Open the cache file as an fd for reading |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
97 |
* |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
98 |
* XXX: needs locking |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
99 |
*/ |
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
100 |
static int pt_cache_open_read (struct pt_cache *cache, int *fd_ptr) |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
101 |
{ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
102 |
int fd; |
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
103 |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
104 |
// actual open() |
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
105 |
if ((fd = open(cache->path, O_RDONLY)) < 0) |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
106 |
return -1; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
107 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
108 |
// ok |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
109 |
*fd_ptr = fd; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
110 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
111 |
return 0; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
112 |
} |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
113 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
114 |
/** |
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
115 |
* 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
|
116 |
*/ |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
117 |
static int pt_cache_open_tmp (struct pt_cache *cache, int *fd_ptr) |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
118 |
{ |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
119 |
int fd; |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
120 |
char tmp_path[1024]; |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
121 |
|
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
122 |
// check mode |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
123 |
if (!(cache->mode & PT_IMG_WRITE)) { |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
124 |
errno = EPERM; |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
125 |
return -1; |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
126 |
} |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
127 |
|
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
128 |
// get .tmp path |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
129 |
if (path_with_fext(cache->path, tmp_path, sizeof(tmp_path), ".tmp")) |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
130 |
return -1; |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
131 |
|
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
132 |
// open for write, create |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
133 |
// XXX: locking? |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
134 |
if ((fd = open(tmp_path, O_RDWR | O_CREAT, 0644)) < 0) |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
135 |
return -1; |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
136 |
|
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
137 |
// ok |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
138 |
*fd_ptr = fd; |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
139 |
|
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
140 |
return 0; |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
141 |
} |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
142 |
|
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
143 |
|
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
144 |
/** |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
145 |
* Mmap the opened cache file from offset PT_CACHE_PAGE, using the calculated size stored in cache->size |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
146 |
*/ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
147 |
static int pt_cache_open_mmap (struct pt_cache *cache, void **addr_ptr) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
148 |
{ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
149 |
int prot = 0; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
150 |
void *addr; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
151 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
152 |
// determine prot |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
153 |
if (cache->mode & PT_IMG_READ) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
154 |
prot |= PROT_READ; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
155 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
156 |
if (cache->mode & PT_IMG_WRITE) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
157 |
prot |= PROT_WRITE; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
158 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
159 |
// perform mmap() from second page on |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
160 |
if ((addr = mmap(NULL, cache->size, prot, MAP_SHARED, cache->fd, PT_CACHE_PAGE)) == MAP_FAILED) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
161 |
return -1; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
162 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
163 |
// ok |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
164 |
*addr_ptr = addr; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
165 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
166 |
return 0; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
167 |
} |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
168 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
169 |
/** |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
170 |
* Write out the cache header into the opened file |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
171 |
*/ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
172 |
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
|
173 |
{ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
174 |
size_t len = sizeof(*header); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
175 |
const char *buf = (const char *) header; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
176 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
177 |
// seek to start |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
178 |
if (lseek(cache->fd, 0, SEEK_SET) != 0) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
179 |
return -1; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
180 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
181 |
// write out full header |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
182 |
while (len) { |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
183 |
ssize_t ret; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
184 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
185 |
// try and write out the header |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
186 |
if ((ret = write(cache->fd, buf, len)) < 0) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
187 |
return -1; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
188 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
189 |
// update offset |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
190 |
buf += ret; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
191 |
len -= ret; |
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 |
// done |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
195 |
return 0; |
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 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
198 |
/** |
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
199 |
* Create a new .tmp cache file, open it, and write out the header. |
0 | 200 |
*/ |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
201 |
static int pt_cache_open_create (struct pt_cache *cache, struct pt_cache_header *header) |
0 | 202 |
{ |
2 | 203 |
// no access |
204 |
if (!(cache->mode & PT_IMG_WRITE)) { |
|
205 |
errno = EPERM; |
|
206 |
return -1; |
|
207 |
} |
|
208 |
||
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
209 |
// open as .tmp |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
210 |
if (pt_cache_open_tmp(cache, &cache->fd)) |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
211 |
return -1; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
212 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
213 |
// calculate data size |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
214 |
cache->size = sizeof(*header) + header->height * header->row_bytes; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
215 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
216 |
// grow file |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
217 |
if (ftruncate(cache->fd, PT_CACHE_PAGE + cache->size) < 0) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
218 |
goto error; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
219 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
220 |
// open mmap |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
221 |
if (pt_cache_open_mmap(cache, (void **) &cache->mmap)) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
222 |
goto error; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
223 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
224 |
// write header |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
225 |
if (pt_cache_write_header(cache, header)) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
226 |
goto error; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
227 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
228 |
// done |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
229 |
return 0; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
230 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
231 |
error: |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
232 |
// cleanup |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
233 |
pt_cache_abort(cache); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
234 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
235 |
return -1; |
0 | 236 |
} |
237 |
||
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
238 |
/** |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
239 |
* 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
|
240 |
*/ |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
241 |
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
|
242 |
{ |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
243 |
char tmp_path[1024]; |
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 |
// get .tmp path |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
246 |
if (path_with_fext(cache->path, tmp_path, sizeof(tmp_path), ".tmp")) |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
247 |
return -1; |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
248 |
|
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
249 |
// rename |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
250 |
if (rename(tmp_path, cache->path) < 0) |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
251 |
return -1; |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
252 |
|
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
253 |
// ok |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
254 |
return 0; |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
255 |
} |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
256 |
|
0 | 257 |
int pt_cache_update_png (struct pt_cache *cache, png_structp png, png_infop info) |
258 |
{ |
|
259 |
struct pt_cache_header header; |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
260 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
261 |
// XXX: check cache_mode |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
262 |
// XXX: check image doesn't use any options we don't handle |
0 | 263 |
|
264 |
memset(&header, 0, sizeof(header)); |
|
265 |
||
266 |
// fill in basic info |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
267 |
header.width = png_get_image_width(png, info); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
268 |
header.height = png_get_image_height(png, info); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
269 |
header.bit_depth = png_get_bit_depth(png, info); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
270 |
header.color_type = png_get_color_type(png, info); |
0 | 271 |
|
272 |
// fill in other info |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
273 |
header.row_bytes = png_get_rowbytes(png, info); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
274 |
|
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
275 |
// create .tmp and write out header |
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
276 |
if (pt_cache_open_create(cache, &header)) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
277 |
return -1; |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
278 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
279 |
// XXX: pallette etc. |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
280 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
281 |
// write out raw image data a row at a time |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
282 |
for (size_t row = 0; row < header.height; row++) { |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
283 |
// read row data, non-interlaced |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
284 |
png_read_row(png, cache->mmap + row * header.row_bytes, NULL); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
285 |
} |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
286 |
|
4
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
287 |
// move from .tmp to .cache |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
288 |
if (pt_cache_create_done(cache)) |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
289 |
return -1; |
49362b34116c
open cache as .tmp, and rename to .cache when done
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
290 |
|
1
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
291 |
// done! |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
292 |
return 0; |
0 | 293 |
} |
1
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 |
void pt_cache_destroy (struct pt_cache *cache) |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
296 |
{ |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
297 |
free(cache->path); |
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 |
pt_cache_abort(cache); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
300 |
|
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
301 |
free(cache); |
f3cde3db1fef
basic image/cache code compiles
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
302 |
} |