author | Lars Hjemli <hjemli@gmail.com> | 2007-05-20 20:09:55 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2007-05-20 20:09:55 (UTC) |
commit | dc3ac3f76077c5d612d42e8beb4878e43acfc58a (patch) (unidiff) | |
tree | dfb996c0ce9833841578e1f0accbb5a387c01237 /cache.c | |
parent | 977a0b173df6fe1a4d362fe4c70f9badff1fd46c (diff) | |
parent | 3de63b264c36888dfd42dfdf3fc0aad4ce0c2b5c (diff) | |
download | cgit-dc3ac3f76077c5d612d42e8beb4878e43acfc58a.zip cgit-dc3ac3f76077c5d612d42e8beb4878e43acfc58a.tar.gz cgit-dc3ac3f76077c5d612d42e8beb4878e43acfc58a.tar.bz2 |
Merge branch 'virtual-url'
* virtual-url:
Don't be fooled by trailing '/' in url-parameter
cache_safe_filename() needs more buffers
Enable url=value querystring parameter
Add lookup-function for valid repo commands
Move cgit_get_repoinfo into shared.c
-rw-r--r-- | cache.c | 22 |
1 files changed, 15 insertions, 7 deletions
@@ -1,111 +1,119 @@ | |||
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 | 10 | ||
11 | const int NOLOCK = -1; | 11 | const int NOLOCK = -1; |
12 | 12 | ||
13 | char *cache_safe_filename(const char *unsafe) | 13 | char *cache_safe_filename(const char *unsafe) |
14 | { | 14 | { |
15 | static char buf[PATH_MAX]; | 15 | static char buf[4][PATH_MAX]; |
16 | char *s = buf; | 16 | static int bufidx; |
17 | char *s; | ||
17 | char c; | 18 | char c; |
18 | 19 | ||
20 | bufidx++; | ||
21 | bufidx &= 3; | ||
22 | s = buf[bufidx]; | ||
23 | |||
19 | while(unsafe && (c = *unsafe++) != 0) { | 24 | while(unsafe && (c = *unsafe++) != 0) { |
20 | if (c == '/' || c == ' ' || c == '&' || c == '|' || | 25 | if (c == '/' || c == ' ' || c == '&' || c == '|' || |
21 | c == '>' || c == '<' || c == '.') | 26 | c == '>' || c == '<' || c == '.') |
22 | c = '_'; | 27 | c = '_'; |
23 | *s++ = c; | 28 | *s++ = c; |
24 | } | 29 | } |
25 | *s = '\0'; | 30 | *s = '\0'; |
26 | return buf; | 31 | return buf[bufidx]; |
27 | } | 32 | } |
28 | 33 | ||
29 | int cache_exist(struct cacheitem *item) | 34 | int cache_exist(struct cacheitem *item) |
30 | { | 35 | { |
31 | if (stat(item->name, &item->st)) { | 36 | if (stat(item->name, &item->st)) { |
32 | item->st.st_mtime = 0; | 37 | item->st.st_mtime = 0; |
33 | return 0; | 38 | return 0; |
34 | } | 39 | } |
35 | return 1; | 40 | return 1; |
36 | } | 41 | } |
37 | 42 | ||
38 | int cache_create_dirs() | 43 | int cache_create_dirs() |
39 | { | 44 | { |
40 | char *path; | 45 | char *path; |
41 | 46 | ||
42 | path = fmt("%s", cgit_cache_root); | 47 | path = fmt("%s", cgit_cache_root); |
43 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 48 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
44 | return 0; | 49 | return 0; |
45 | 50 | ||
46 | if (!cgit_query_repo) | 51 | if (!cgit_repo) |
47 | return 0; | 52 | return 0; |
48 | 53 | ||
49 | path = fmt("%s/%s", cgit_cache_root, cgit_query_repo); | 54 | path = fmt("%s/%s", cgit_cache_root, |
55 | cache_safe_filename(cgit_repo->url)); | ||
56 | |||
50 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 57 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
51 | return 0; | 58 | return 0; |
52 | 59 | ||
53 | if (cgit_query_page) { | 60 | if (cgit_query_page) { |
54 | path = fmt("%s/%s/%s", cgit_cache_root, cgit_query_repo, | 61 | path = fmt("%s/%s/%s", cgit_cache_root, |
62 | cache_safe_filename(cgit_repo->url), | ||
55 | cgit_query_page); | 63 | cgit_query_page); |
56 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 64 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
57 | return 0; | 65 | return 0; |
58 | } | 66 | } |
59 | return 1; | 67 | return 1; |
60 | } | 68 | } |
61 | 69 | ||
62 | int cache_refill_overdue(const char *lockfile) | 70 | int cache_refill_overdue(const char *lockfile) |
63 | { | 71 | { |
64 | struct stat st; | 72 | struct stat st; |
65 | 73 | ||
66 | if (stat(lockfile, &st)) | 74 | if (stat(lockfile, &st)) |
67 | return 0; | 75 | return 0; |
68 | else | 76 | else |
69 | return (time(NULL) - st.st_mtime > cgit_cache_max_create_time); | 77 | return (time(NULL) - st.st_mtime > cgit_cache_max_create_time); |
70 | } | 78 | } |
71 | 79 | ||
72 | int cache_lock(struct cacheitem *item) | 80 | int cache_lock(struct cacheitem *item) |
73 | { | 81 | { |
74 | int i = 0; | 82 | int i = 0; |
75 | char *lockfile = xstrdup(fmt("%s.lock", item->name)); | 83 | char *lockfile = xstrdup(fmt("%s.lock", item->name)); |
76 | 84 | ||
77 | top: | 85 | top: |
78 | if (++i > cgit_max_lock_attempts) | 86 | if (++i > cgit_max_lock_attempts) |
79 | die("cache_lock: unable to lock %s: %s", | 87 | die("cache_lock: unable to lock %s: %s", |
80 | item->name, strerror(errno)); | 88 | item->name, strerror(errno)); |
81 | 89 | ||
82 | item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); | 90 | item->fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); |
83 | 91 | ||
84 | if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs()) | 92 | if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs()) |
85 | goto top; | 93 | goto top; |
86 | 94 | ||
87 | if (item->fd == NOLOCK && errno == EEXIST && | 95 | if (item->fd == NOLOCK && errno == EEXIST && |
88 | cache_refill_overdue(lockfile) && !unlink(lockfile)) | 96 | cache_refill_overdue(lockfile) && !unlink(lockfile)) |
89 | goto top; | 97 | goto top; |
90 | 98 | ||
91 | free(lockfile); | 99 | free(lockfile); |
92 | return (item->fd > 0); | 100 | return (item->fd > 0); |
93 | } | 101 | } |
94 | 102 | ||
95 | int cache_unlock(struct cacheitem *item) | 103 | int cache_unlock(struct cacheitem *item) |
96 | { | 104 | { |
97 | close(item->fd); | 105 | close(item->fd); |
98 | return (rename(fmt("%s.lock", item->name), item->name) == 0); | 106 | return (rename(fmt("%s.lock", item->name), item->name) == 0); |
99 | } | 107 | } |
100 | 108 | ||
101 | int cache_cancel_lock(struct cacheitem *item) | 109 | int cache_cancel_lock(struct cacheitem *item) |
102 | { | 110 | { |
103 | return (unlink(fmt("%s.lock", item->name)) == 0); | 111 | return (unlink(fmt("%s.lock", item->name)) == 0); |
104 | } | 112 | } |
105 | 113 | ||
106 | int cache_expired(struct cacheitem *item) | 114 | int cache_expired(struct cacheitem *item) |
107 | { | 115 | { |
108 | if (item->ttl < 0) | 116 | if (item->ttl < 0) |
109 | return 0; | 117 | return 0; |
110 | return item->st.st_mtime + item->ttl * 60 < time(NULL); | 118 | return item->st.st_mtime + item->ttl * 60 < time(NULL); |
111 | } | 119 | } |