author | Lars Hjemli <hjemli@gmail.com> | 2008-03-27 08:22:13 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-03-27 08:22:13 (UTC) |
commit | ee4056bd2c902a12dea67874368863fe60ea5a5f (patch) (unidiff) | |
tree | d54da54ace7b8999cf8785d877a0a9cad5262a0c /cache.c | |
parent | dc3282f0baa14949439593729a45fbe143e3622c (diff) | |
download | cgit-ee4056bd2c902a12dea67874368863fe60ea5a5f.zip cgit-ee4056bd2c902a12dea67874368863fe60ea5a5f.tar.gz cgit-ee4056bd2c902a12dea67874368863fe60ea5a5f.tar.bz2 |
Add cache.h
The functions found in cache.c are only used by cgit.c, so there's no
point in rebuilding all object files when the cache interface is changed.
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | cache.c | 1 |
1 files changed, 1 insertions, 0 deletions
@@ -1,119 +1,120 @@ | |||
1 | /* cache.c: cache management | 1 | /* cache.c: cache management |
2 | * | 2 | * |
3 | * Copyright (C) 2006 Lars Hjemli | 3 | * Copyright (C) 2006 Lars Hjemli |
4 | * | 4 | * |
5 | * Licensed under GNU General Public License v2 | 5 | * Licensed under GNU General Public License v2 |
6 | * (see COPYING for full license text) | 6 | * (see COPYING for full license text) |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include "cgit.h" | 9 | #include "cgit.h" |
10 | #include "cache.h" | ||
10 | 11 | ||
11 | const int NOLOCK = -1; | 12 | const int NOLOCK = -1; |
12 | 13 | ||
13 | char *cache_safe_filename(const char *unsafe) | 14 | char *cache_safe_filename(const char *unsafe) |
14 | { | 15 | { |
15 | static char buf[4][PATH_MAX]; | 16 | static char buf[4][PATH_MAX]; |
16 | static int bufidx; | 17 | static int bufidx; |
17 | char *s; | 18 | char *s; |
18 | char c; | 19 | char c; |
19 | 20 | ||
20 | bufidx++; | 21 | bufidx++; |
21 | bufidx &= 3; | 22 | bufidx &= 3; |
22 | s = buf[bufidx]; | 23 | s = buf[bufidx]; |
23 | 24 | ||
24 | while(unsafe && (c = *unsafe++) != 0) { | 25 | while(unsafe && (c = *unsafe++) != 0) { |
25 | if (c == '/' || c == ' ' || c == '&' || c == '|' || | 26 | if (c == '/' || c == ' ' || c == '&' || c == '|' || |
26 | c == '>' || c == '<' || c == '.') | 27 | c == '>' || c == '<' || c == '.') |
27 | c = '_'; | 28 | c = '_'; |
28 | *s++ = c; | 29 | *s++ = c; |
29 | } | 30 | } |
30 | *s = '\0'; | 31 | *s = '\0'; |
31 | return buf[bufidx]; | 32 | return buf[bufidx]; |
32 | } | 33 | } |
33 | 34 | ||
34 | int cache_exist(struct cacheitem *item) | 35 | int cache_exist(struct cacheitem *item) |
35 | { | 36 | { |
36 | if (stat(item->name, &item->st)) { | 37 | if (stat(item->name, &item->st)) { |
37 | item->st.st_mtime = 0; | 38 | item->st.st_mtime = 0; |
38 | return 0; | 39 | return 0; |
39 | } | 40 | } |
40 | return 1; | 41 | return 1; |
41 | } | 42 | } |
42 | 43 | ||
43 | int cache_create_dirs() | 44 | int cache_create_dirs() |
44 | { | 45 | { |
45 | char *path; | 46 | char *path; |
46 | 47 | ||
47 | path = fmt("%s", ctx.cfg.cache_root); | 48 | path = fmt("%s", ctx.cfg.cache_root); |
48 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 49 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
49 | return 0; | 50 | return 0; |
50 | 51 | ||
51 | if (!ctx.repo) | 52 | if (!ctx.repo) |
52 | return 0; | 53 | return 0; |
53 | 54 | ||
54 | path = fmt("%s/%s", ctx.cfg.cache_root, | 55 | path = fmt("%s/%s", ctx.cfg.cache_root, |
55 | cache_safe_filename(ctx.repo->url)); | 56 | cache_safe_filename(ctx.repo->url)); |
56 | 57 | ||
57 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 58 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
58 | return 0; | 59 | return 0; |
59 | 60 | ||
60 | if (ctx.qry.page) { | 61 | if (ctx.qry.page) { |
61 | path = fmt("%s/%s/%s", ctx.cfg.cache_root, | 62 | path = fmt("%s/%s/%s", ctx.cfg.cache_root, |
62 | cache_safe_filename(ctx.repo->url), | 63 | cache_safe_filename(ctx.repo->url), |
63 | ctx.qry.page); | 64 | ctx.qry.page); |
64 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 65 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
65 | return 0; | 66 | return 0; |
66 | } | 67 | } |
67 | return 1; | 68 | return 1; |
68 | } | 69 | } |
69 | 70 | ||
70 | int cache_refill_overdue(const char *lockfile) | 71 | int cache_refill_overdue(const char *lockfile) |
71 | { | 72 | { |
72 | struct stat st; | 73 | struct stat st; |
73 | 74 | ||
74 | if (stat(lockfile, &st)) | 75 | if (stat(lockfile, &st)) |
75 | return 0; | 76 | return 0; |
76 | else | 77 | else |
77 | return (time(NULL) - st.st_mtime > ctx.cfg.cache_max_create_time); | 78 | return (time(NULL) - st.st_mtime > ctx.cfg.cache_max_create_time); |
78 | } | 79 | } |
79 | 80 | ||
80 | int cache_lock(struct cacheitem *item) | 81 | int cache_lock(struct cacheitem *item) |
81 | { | 82 | { |
82 | int i = 0; | 83 | int i = 0; |
83 | char *lockfile = xstrdup(fmt("%s.lock", item->name)); | 84 | char *lockfile = xstrdup(fmt("%s.lock", item->name)); |
84 | 85 | ||
85 | top: | 86 | top: |
86 | if (++i > ctx.cfg.max_lock_attempts) | 87 | if (++i > ctx.cfg.max_lock_attempts) |
87 | die("cache_lock: unable to lock %s: %s", | 88 | die("cache_lock: unable to lock %s: %s", |
88 | item->name, strerror(errno)); | 89 | item->name, strerror(errno)); |
89 | 90 | ||
90 | item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); | 91 | item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); |
91 | 92 | ||
92 | if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs()) | 93 | if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs()) |
93 | goto top; | 94 | goto top; |
94 | 95 | ||
95 | if (item->fd == NOLOCK && errno == EEXIST && | 96 | if (item->fd == NOLOCK && errno == EEXIST && |
96 | cache_refill_overdue(lockfile) && !unlink(lockfile)) | 97 | cache_refill_overdue(lockfile) && !unlink(lockfile)) |
97 | goto top; | 98 | goto top; |
98 | 99 | ||
99 | free(lockfile); | 100 | free(lockfile); |
100 | return (item->fd > 0); | 101 | return (item->fd > 0); |
101 | } | 102 | } |
102 | 103 | ||
103 | int cache_unlock(struct cacheitem *item) | 104 | int cache_unlock(struct cacheitem *item) |
104 | { | 105 | { |
105 | close(item->fd); | 106 | close(item->fd); |
106 | return (rename(fmt("%s.lock", item->name), item->name) == 0); | 107 | return (rename(fmt("%s.lock", item->name), item->name) == 0); |
107 | } | 108 | } |
108 | 109 | ||
109 | int cache_cancel_lock(struct cacheitem *item) | 110 | int cache_cancel_lock(struct cacheitem *item) |
110 | { | 111 | { |
111 | return (unlink(fmt("%s.lock", item->name)) == 0); | 112 | return (unlink(fmt("%s.lock", item->name)) == 0); |
112 | } | 113 | } |
113 | 114 | ||
114 | int cache_expired(struct cacheitem *item) | 115 | int cache_expired(struct cacheitem *item) |
115 | { | 116 | { |
116 | if (item->ttl < 0) | 117 | if (item->ttl < 0) |
117 | return 0; | 118 | return 0; |
118 | return item->st.st_mtime + item->ttl * 60 < time(NULL); | 119 | return item->st.st_mtime + item->ttl * 60 < time(NULL); |
119 | } | 120 | } |