|
diff --git a/cgit.c b/cgit.c index 79e0e43..dbb023e 100644 --- a/ cgit.c+++ b/ cgit.c |
|
@@ -1,302 +1,303 @@ |
1 | /* cgit.c: cgi for the git scm |
1 | /* cgit.c: cgi for the git scm |
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 "cmd.h" |
10 | #include "cmd.h" |
| |
11 | #include "ui-shared.h" |
11 | |
12 | |
12 | static int cgit_prepare_cache(struct cacheitem *item) |
13 | static int cgit_prepare_cache(struct cacheitem *item) |
13 | { |
14 | { |
14 | if (!ctx.repo && ctx.qry.repo) { |
15 | if (!ctx.repo && ctx.qry.repo) { |
15 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, |
16 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, |
16 | "Bad request"); |
17 | "Bad request"); |
17 | cgit_print_http_headers(&ctx); |
18 | cgit_print_http_headers(&ctx); |
18 | cgit_print_docstart(&ctx); |
19 | cgit_print_docstart(&ctx); |
19 | cgit_print_pageheader(&ctx); |
20 | cgit_print_pageheader(&ctx); |
20 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); |
21 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); |
21 | cgit_print_docend(); |
22 | cgit_print_docend(); |
22 | return 0; |
23 | return 0; |
23 | } |
24 | } |
24 | |
25 | |
25 | if (!ctx.repo) { |
26 | if (!ctx.repo) { |
26 | item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); |
27 | item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); |
27 | item->ttl = ctx.cfg.cache_root_ttl; |
28 | item->ttl = ctx.cfg.cache_root_ttl; |
28 | return 1; |
29 | return 1; |
29 | } |
30 | } |
30 | |
31 | |
31 | if (!cgit_cmd) { |
32 | if (!cgit_cmd) { |
32 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, |
33 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, |
33 | cache_safe_filename(ctx.repo->url), |
34 | cache_safe_filename(ctx.repo->url), |
34 | cache_safe_filename(ctx.qry.raw))); |
35 | cache_safe_filename(ctx.qry.raw))); |
35 | item->ttl = ctx.cfg.cache_repo_ttl; |
36 | item->ttl = ctx.cfg.cache_repo_ttl; |
36 | } else { |
37 | } else { |
37 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, |
38 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, |
38 | cache_safe_filename(ctx.repo->url), |
39 | cache_safe_filename(ctx.repo->url), |
39 | ctx.qry.page, |
40 | ctx.qry.page, |
40 | cache_safe_filename(ctx.qry.raw))); |
41 | cache_safe_filename(ctx.qry.raw))); |
41 | if (ctx.qry.has_symref) |
42 | if (ctx.qry.has_symref) |
42 | item->ttl = ctx.cfg.cache_dynamic_ttl; |
43 | item->ttl = ctx.cfg.cache_dynamic_ttl; |
43 | else if (ctx.qry.has_sha1) |
44 | else if (ctx.qry.has_sha1) |
44 | item->ttl = ctx.cfg.cache_static_ttl; |
45 | item->ttl = ctx.cfg.cache_static_ttl; |
45 | else |
46 | else |
46 | item->ttl = ctx.cfg.cache_repo_ttl; |
47 | item->ttl = ctx.cfg.cache_repo_ttl; |
47 | } |
48 | } |
48 | return 1; |
49 | return 1; |
49 | } |
50 | } |
50 | |
51 | |
51 | struct refmatch { |
52 | struct refmatch { |
52 | char *req_ref; |
53 | char *req_ref; |
53 | char *first_ref; |
54 | char *first_ref; |
54 | int match; |
55 | int match; |
55 | }; |
56 | }; |
56 | |
57 | |
57 | int find_current_ref(const char *refname, const unsigned char *sha1, |
58 | int find_current_ref(const char *refname, const unsigned char *sha1, |
58 | int flags, void *cb_data) |
59 | int flags, void *cb_data) |
59 | { |
60 | { |
60 | struct refmatch *info; |
61 | struct refmatch *info; |
61 | |
62 | |
62 | info = (struct refmatch *)cb_data; |
63 | info = (struct refmatch *)cb_data; |
63 | if (!strcmp(refname, info->req_ref)) |
64 | if (!strcmp(refname, info->req_ref)) |
64 | info->match = 1; |
65 | info->match = 1; |
65 | if (!info->first_ref) |
66 | if (!info->first_ref) |
66 | info->first_ref = xstrdup(refname); |
67 | info->first_ref = xstrdup(refname); |
67 | return info->match; |
68 | return info->match; |
68 | } |
69 | } |
69 | |
70 | |
70 | char *find_default_branch(struct cgit_repo *repo) |
71 | char *find_default_branch(struct cgit_repo *repo) |
71 | { |
72 | { |
72 | struct refmatch info; |
73 | struct refmatch info; |
73 | |
74 | |
74 | info.req_ref = repo->defbranch; |
75 | info.req_ref = repo->defbranch; |
75 | info.first_ref = NULL; |
76 | info.first_ref = NULL; |
76 | info.match = 0; |
77 | info.match = 0; |
77 | for_each_branch_ref(find_current_ref, &info); |
78 | for_each_branch_ref(find_current_ref, &info); |
78 | if (info.match) |
79 | if (info.match) |
79 | return info.req_ref; |
80 | return info.req_ref; |
80 | else |
81 | else |
81 | return info.first_ref; |
82 | return info.first_ref; |
82 | } |
83 | } |
83 | |
84 | |
84 | static int prepare_repo_cmd(struct cgit_context *ctx) |
85 | static int prepare_repo_cmd(struct cgit_context *ctx) |
85 | { |
86 | { |
86 | char *tmp; |
87 | char *tmp; |
87 | unsigned char sha1[20]; |
88 | unsigned char sha1[20]; |
88 | int nongit = 0; |
89 | int nongit = 0; |
89 | |
90 | |
90 | setenv("GIT_DIR", ctx->repo->path, 1); |
91 | setenv("GIT_DIR", ctx->repo->path, 1); |
91 | setup_git_directory_gently(&nongit); |
92 | setup_git_directory_gently(&nongit); |
92 | if (nongit) { |
93 | if (nongit) { |
93 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, |
94 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, |
94 | "config error"); |
95 | "config error"); |
95 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); |
96 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); |
96 | ctx->repo = NULL; |
97 | ctx->repo = NULL; |
97 | cgit_print_http_headers(ctx); |
98 | cgit_print_http_headers(ctx); |
98 | cgit_print_docstart(ctx); |
99 | cgit_print_docstart(ctx); |
99 | cgit_print_pageheader(ctx); |
100 | cgit_print_pageheader(ctx); |
100 | cgit_print_error(tmp); |
101 | cgit_print_error(tmp); |
101 | cgit_print_docend(); |
102 | cgit_print_docend(); |
102 | return 1; |
103 | return 1; |
103 | } |
104 | } |
104 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); |
105 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); |
105 | |
106 | |
106 | if (!ctx->qry.head) { |
107 | if (!ctx->qry.head) { |
107 | ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); |
108 | ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); |
108 | ctx->repo->defbranch = ctx->qry.head; |
109 | ctx->repo->defbranch = ctx->qry.head; |
109 | } |
110 | } |
110 | |
111 | |
111 | if (!ctx->qry.head) { |
112 | if (!ctx->qry.head) { |
112 | cgit_print_http_headers(ctx); |
113 | cgit_print_http_headers(ctx); |
113 | cgit_print_docstart(ctx); |
114 | cgit_print_docstart(ctx); |
114 | cgit_print_pageheader(ctx); |
115 | cgit_print_pageheader(ctx); |
115 | cgit_print_error("Repository seems to be empty"); |
116 | cgit_print_error("Repository seems to be empty"); |
116 | cgit_print_docend(); |
117 | cgit_print_docend(); |
117 | return 1; |
118 | return 1; |
118 | } |
119 | } |
119 | |
120 | |
120 | if (get_sha1(ctx->qry.head, sha1)) { |
121 | if (get_sha1(ctx->qry.head, sha1)) { |
121 | tmp = xstrdup(ctx->qry.head); |
122 | tmp = xstrdup(ctx->qry.head); |
122 | ctx->qry.head = ctx->repo->defbranch; |
123 | ctx->qry.head = ctx->repo->defbranch; |
123 | cgit_print_http_headers(ctx); |
124 | cgit_print_http_headers(ctx); |
124 | cgit_print_docstart(ctx); |
125 | cgit_print_docstart(ctx); |
125 | cgit_print_pageheader(ctx); |
126 | cgit_print_pageheader(ctx); |
126 | cgit_print_error(fmt("Invalid branch: %s", tmp)); |
127 | cgit_print_error(fmt("Invalid branch: %s", tmp)); |
127 | cgit_print_docend(); |
128 | cgit_print_docend(); |
128 | return 1; |
129 | return 1; |
129 | } |
130 | } |
130 | return 0; |
131 | return 0; |
131 | } |
132 | } |
132 | |
133 | |
133 | static void process_request(struct cgit_context *ctx) |
134 | static void process_request(struct cgit_context *ctx) |
134 | { |
135 | { |
135 | struct cgit_cmd *cmd; |
136 | struct cgit_cmd *cmd; |
136 | |
137 | |
137 | cmd = cgit_get_cmd(ctx); |
138 | cmd = cgit_get_cmd(ctx); |
138 | if (!cmd) { |
139 | if (!cmd) { |
139 | ctx->page.title = "cgit error"; |
140 | ctx->page.title = "cgit error"; |
140 | ctx->repo = NULL; |
141 | ctx->repo = NULL; |
141 | cgit_print_http_headers(ctx); |
142 | cgit_print_http_headers(ctx); |
142 | cgit_print_docstart(ctx); |
143 | cgit_print_docstart(ctx); |
143 | cgit_print_pageheader(ctx); |
144 | cgit_print_pageheader(ctx); |
144 | cgit_print_error("Invalid request"); |
145 | cgit_print_error("Invalid request"); |
145 | cgit_print_docend(); |
146 | cgit_print_docend(); |
146 | return; |
147 | return; |
147 | } |
148 | } |
148 | |
149 | |
149 | if (cmd->want_repo && prepare_repo_cmd(ctx)) |
150 | if (cmd->want_repo && prepare_repo_cmd(ctx)) |
150 | return; |
151 | return; |
151 | |
152 | |
152 | if (cmd->want_layout) { |
153 | if (cmd->want_layout) { |
153 | cgit_print_http_headers(ctx); |
154 | cgit_print_http_headers(ctx); |
154 | cgit_print_docstart(ctx); |
155 | cgit_print_docstart(ctx); |
155 | cgit_print_pageheader(ctx); |
156 | cgit_print_pageheader(ctx); |
156 | } |
157 | } |
157 | |
158 | |
158 | cmd->fn(ctx); |
159 | cmd->fn(ctx); |
159 | |
160 | |
160 | if (cmd->want_layout) |
161 | if (cmd->want_layout) |
161 | cgit_print_docend(); |
162 | cgit_print_docend(); |
162 | } |
163 | } |
163 | |
164 | |
164 | static long ttl_seconds(long ttl) |
165 | static long ttl_seconds(long ttl) |
165 | { |
166 | { |
166 | if (ttl<0) |
167 | if (ttl<0) |
167 | return 60 * 60 * 24 * 365; |
168 | return 60 * 60 * 24 * 365; |
168 | else |
169 | else |
169 | return ttl * 60; |
170 | return ttl * 60; |
170 | } |
171 | } |
171 | |
172 | |
172 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) |
173 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) |
173 | { |
174 | { |
174 | int stdout2; |
175 | int stdout2; |
175 | |
176 | |
176 | if (use_cache) { |
177 | if (use_cache) { |
177 | stdout2 = chk_positive(dup(STDOUT_FILENO), |
178 | stdout2 = chk_positive(dup(STDOUT_FILENO), |
178 | "Preserving STDOUT"); |
179 | "Preserving STDOUT"); |
179 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); |
180 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); |
180 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); |
181 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); |
181 | } |
182 | } |
182 | |
183 | |
183 | ctx.page.modified = time(NULL); |
184 | ctx.page.modified = time(NULL); |
184 | ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); |
185 | ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); |
185 | process_request(&ctx); |
186 | process_request(&ctx); |
186 | |
187 | |
187 | if (use_cache) { |
188 | if (use_cache) { |
188 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); |
189 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); |
189 | chk_positive(dup2(stdout2, STDOUT_FILENO), |
190 | chk_positive(dup2(stdout2, STDOUT_FILENO), |
190 | "Restoring original STDOUT"); |
191 | "Restoring original STDOUT"); |
191 | chk_zero(close(stdout2), "Closing temporary STDOUT"); |
192 | chk_zero(close(stdout2), "Closing temporary STDOUT"); |
192 | } |
193 | } |
193 | } |
194 | } |
194 | |
195 | |
195 | static void cgit_check_cache(struct cacheitem *item) |
196 | static void cgit_check_cache(struct cacheitem *item) |
196 | { |
197 | { |
197 | int i = 0; |
198 | int i = 0; |
198 | |
199 | |
199 | top: |
200 | top: |
200 | if (++i > ctx.cfg.max_lock_attempts) { |
201 | if (++i > ctx.cfg.max_lock_attempts) { |
201 | die("cgit_refresh_cache: unable to lock %s: %s", |
202 | die("cgit_refresh_cache: unable to lock %s: %s", |
202 | item->name, strerror(errno)); |
203 | item->name, strerror(errno)); |
203 | } |
204 | } |
204 | if (!cache_exist(item)) { |
205 | if (!cache_exist(item)) { |
205 | if (!cache_lock(item)) { |
206 | if (!cache_lock(item)) { |
206 | sleep(1); |
207 | sleep(1); |
207 | goto top; |
208 | goto top; |
208 | } |
209 | } |
209 | if (!cache_exist(item)) { |
210 | if (!cache_exist(item)) { |
210 | cgit_fill_cache(item, 1); |
211 | cgit_fill_cache(item, 1); |
211 | cache_unlock(item); |
212 | cache_unlock(item); |
212 | } else { |
213 | } else { |
213 | cache_cancel_lock(item); |
214 | cache_cancel_lock(item); |
214 | } |
215 | } |
215 | } else if (cache_expired(item) && cache_lock(item)) { |
216 | } else if (cache_expired(item) && cache_lock(item)) { |
216 | if (cache_expired(item)) { |
217 | if (cache_expired(item)) { |
217 | cgit_fill_cache(item, 1); |
218 | cgit_fill_cache(item, 1); |
218 | cache_unlock(item); |
219 | cache_unlock(item); |
219 | } else { |
220 | } else { |
220 | cache_cancel_lock(item); |
221 | cache_cancel_lock(item); |
221 | } |
222 | } |
222 | } |
223 | } |
223 | } |
224 | } |
224 | |
225 | |
225 | static void cgit_print_cache(struct cacheitem *item) |
226 | static void cgit_print_cache(struct cacheitem *item) |
226 | { |
227 | { |
227 | static char buf[4096]; |
228 | static char buf[4096]; |
228 | ssize_t i; |
229 | ssize_t i; |
229 | |
230 | |
230 | int fd = open(item->name, O_RDONLY); |
231 | int fd = open(item->name, O_RDONLY); |
231 | if (fd<0) |
232 | if (fd<0) |
232 | die("Unable to open cached file %s", item->name); |
233 | die("Unable to open cached file %s", item->name); |
233 | |
234 | |
234 | while((i=read(fd, buf, sizeof(buf))) > 0) |
235 | while((i=read(fd, buf, sizeof(buf))) > 0) |
235 | write(STDOUT_FILENO, buf, i); |
236 | write(STDOUT_FILENO, buf, i); |
236 | |
237 | |
237 | close(fd); |
238 | close(fd); |
238 | } |
239 | } |
239 | |
240 | |
240 | static void cgit_parse_args(int argc, const char **argv) |
241 | static void cgit_parse_args(int argc, const char **argv) |
241 | { |
242 | { |
242 | int i; |
243 | int i; |
243 | |
244 | |
244 | for (i = 1; i < argc; i++) { |
245 | for (i = 1; i < argc; i++) { |
245 | if (!strncmp(argv[i], "--cache=", 8)) { |
246 | if (!strncmp(argv[i], "--cache=", 8)) { |
246 | ctx.cfg.cache_root = xstrdup(argv[i]+8); |
247 | ctx.cfg.cache_root = xstrdup(argv[i]+8); |
247 | } |
248 | } |
248 | if (!strcmp(argv[i], "--nocache")) { |
249 | if (!strcmp(argv[i], "--nocache")) { |
249 | ctx.cfg.nocache = 1; |
250 | ctx.cfg.nocache = 1; |
250 | } |
251 | } |
251 | if (!strncmp(argv[i], "--query=", 8)) { |
252 | if (!strncmp(argv[i], "--query=", 8)) { |
252 | ctx.qry.raw = xstrdup(argv[i]+8); |
253 | ctx.qry.raw = xstrdup(argv[i]+8); |
253 | } |
254 | } |
254 | if (!strncmp(argv[i], "--repo=", 7)) { |
255 | if (!strncmp(argv[i], "--repo=", 7)) { |
255 | ctx.qry.repo = xstrdup(argv[i]+7); |
256 | ctx.qry.repo = xstrdup(argv[i]+7); |
256 | } |
257 | } |
257 | if (!strncmp(argv[i], "--page=", 7)) { |
258 | if (!strncmp(argv[i], "--page=", 7)) { |
258 | ctx.qry.page = xstrdup(argv[i]+7); |
259 | ctx.qry.page = xstrdup(argv[i]+7); |
259 | } |
260 | } |
260 | if (!strncmp(argv[i], "--head=", 7)) { |
261 | if (!strncmp(argv[i], "--head=", 7)) { |
261 | ctx.qry.head = xstrdup(argv[i]+7); |
262 | ctx.qry.head = xstrdup(argv[i]+7); |
262 | ctx.qry.has_symref = 1; |
263 | ctx.qry.has_symref = 1; |
263 | } |
264 | } |
264 | if (!strncmp(argv[i], "--sha1=", 7)) { |
265 | if (!strncmp(argv[i], "--sha1=", 7)) { |
265 | ctx.qry.sha1 = xstrdup(argv[i]+7); |
266 | ctx.qry.sha1 = xstrdup(argv[i]+7); |
266 | ctx.qry.has_sha1 = 1; |
267 | ctx.qry.has_sha1 = 1; |
267 | } |
268 | } |
268 | if (!strncmp(argv[i], "--ofs=", 6)) { |
269 | if (!strncmp(argv[i], "--ofs=", 6)) { |
269 | ctx.qry.ofs = atoi(argv[i]+6); |
270 | ctx.qry.ofs = atoi(argv[i]+6); |
270 | } |
271 | } |
271 | } |
272 | } |
272 | } |
273 | } |
273 | |
274 | |
274 | int main(int argc, const char **argv) |
275 | int main(int argc, const char **argv) |
275 | { |
276 | { |
276 | struct cacheitem item; |
277 | struct cacheitem item; |
277 | const char *cgit_config_env = getenv("CGIT_CONFIG"); |
278 | const char *cgit_config_env = getenv("CGIT_CONFIG"); |
278 | |
279 | |
279 | cgit_prepare_context(&ctx); |
280 | cgit_prepare_context(&ctx); |
280 | item.st.st_mtime = time(NULL); |
281 | item.st.st_mtime = time(NULL); |
281 | cgit_repolist.length = 0; |
282 | cgit_repolist.length = 0; |
282 | cgit_repolist.count = 0; |
283 | cgit_repolist.count = 0; |
283 | cgit_repolist.repos = NULL; |
284 | cgit_repolist.repos = NULL; |
284 | |
285 | |
285 | cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, |
286 | cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, |
286 | cgit_global_config_cb); |
287 | cgit_global_config_cb); |
287 | if (getenv("SCRIPT_NAME")) |
288 | if (getenv("SCRIPT_NAME")) |
288 | ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); |
289 | ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); |
289 | if (getenv("QUERY_STRING")) |
290 | if (getenv("QUERY_STRING")) |
290 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); |
291 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); |
291 | cgit_parse_args(argc, argv); |
292 | cgit_parse_args(argc, argv); |
292 | cgit_parse_query(ctx.qry.raw, cgit_querystring_cb); |
293 | cgit_parse_query(ctx.qry.raw, cgit_querystring_cb); |
293 | if (!cgit_prepare_cache(&item)) |
294 | if (!cgit_prepare_cache(&item)) |
294 | return 0; |
295 | return 0; |
295 | if (ctx.cfg.nocache) { |
296 | if (ctx.cfg.nocache) { |
296 | cgit_fill_cache(&item, 0); |
297 | cgit_fill_cache(&item, 0); |
297 | } else { |
298 | } else { |
298 | cgit_check_cache(&item); |
299 | cgit_check_cache(&item); |
299 | cgit_print_cache(&item); |
300 | cgit_print_cache(&item); |
300 | } |
301 | } |
301 | return 0; |
302 | return 0; |
302 | } |
303 | } |
|