author | Lars Hjemli <hjemli@gmail.com> | 2008-03-24 16:26:08 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-03-24 16:26:08 (UTC) |
commit | 163037e79c6cde1073d555dbeae2a095298e6101 (patch) (unidiff) | |
tree | 37f36b95772eff04683a7f7efc1c5c369b7bf905 /cgit.c | |
parent | a4d1ca1dc6ff8171694d9e2280b6075a1beced0c (diff) | |
download | cgit-163037e79c6cde1073d555dbeae2a095298e6101.zip cgit-163037e79c6cde1073d555dbeae2a095298e6101.tar.gz cgit-163037e79c6cde1073d555dbeae2a095298e6101.tar.bz2 |
Move non-generic functions from shared.c to cgit.c
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | cgit.c | 156 |
1 files changed, 153 insertions, 3 deletions
@@ -1,140 +1,290 @@ | |||
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 | #include "ui-shared.h" |
12 | 12 | ||
13 | void config_cb(const char *name, const char *value) | ||
14 | { | ||
15 | if (!strcmp(name, "root-title")) | ||
16 | ctx.cfg.root_title = xstrdup(value); | ||
17 | else if (!strcmp(name, "css")) | ||
18 | ctx.cfg.css = xstrdup(value); | ||
19 | else if (!strcmp(name, "logo")) | ||
20 | ctx.cfg.logo = xstrdup(value); | ||
21 | else if (!strcmp(name, "index-header")) | ||
22 | ctx.cfg.index_header = xstrdup(value); | ||
23 | else if (!strcmp(name, "index-info")) | ||
24 | ctx.cfg.index_info = xstrdup(value); | ||
25 | else if (!strcmp(name, "logo-link")) | ||
26 | ctx.cfg.logo_link = xstrdup(value); | ||
27 | else if (!strcmp(name, "module-link")) | ||
28 | ctx.cfg.module_link = xstrdup(value); | ||
29 | else if (!strcmp(name, "virtual-root")) { | ||
30 | ctx.cfg.virtual_root = trim_end(value, '/'); | ||
31 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) | ||
32 | ctx.cfg.virtual_root = ""; | ||
33 | } else if (!strcmp(name, "nocache")) | ||
34 | ctx.cfg.nocache = atoi(value); | ||
35 | else if (!strcmp(name, "snapshots")) | ||
36 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); | ||
37 | else if (!strcmp(name, "enable-index-links")) | ||
38 | ctx.cfg.enable_index_links = atoi(value); | ||
39 | else if (!strcmp(name, "enable-log-filecount")) | ||
40 | ctx.cfg.enable_log_filecount = atoi(value); | ||
41 | else if (!strcmp(name, "enable-log-linecount")) | ||
42 | ctx.cfg.enable_log_linecount = atoi(value); | ||
43 | else if (!strcmp(name, "cache-root")) | ||
44 | ctx.cfg.cache_root = xstrdup(value); | ||
45 | else if (!strcmp(name, "cache-root-ttl")) | ||
46 | ctx.cfg.cache_root_ttl = atoi(value); | ||
47 | else if (!strcmp(name, "cache-repo-ttl")) | ||
48 | ctx.cfg.cache_repo_ttl = atoi(value); | ||
49 | else if (!strcmp(name, "cache-static-ttl")) | ||
50 | ctx.cfg.cache_static_ttl = atoi(value); | ||
51 | else if (!strcmp(name, "cache-dynamic-ttl")) | ||
52 | ctx.cfg.cache_dynamic_ttl = atoi(value); | ||
53 | else if (!strcmp(name, "max-message-length")) | ||
54 | ctx.cfg.max_msg_len = atoi(value); | ||
55 | else if (!strcmp(name, "max-repodesc-length")) | ||
56 | ctx.cfg.max_repodesc_len = atoi(value); | ||
57 | else if (!strcmp(name, "max-commit-count")) | ||
58 | ctx.cfg.max_commit_count = atoi(value); | ||
59 | else if (!strcmp(name, "summary-log")) | ||
60 | ctx.cfg.summary_log = atoi(value); | ||
61 | else if (!strcmp(name, "summary-branches")) | ||
62 | ctx.cfg.summary_branches = atoi(value); | ||
63 | else if (!strcmp(name, "summary-tags")) | ||
64 | ctx.cfg.summary_tags = atoi(value); | ||
65 | else if (!strcmp(name, "agefile")) | ||
66 | ctx.cfg.agefile = xstrdup(value); | ||
67 | else if (!strcmp(name, "renamelimit")) | ||
68 | ctx.cfg.renamelimit = atoi(value); | ||
69 | else if (!strcmp(name, "robots")) | ||
70 | ctx.cfg.robots = xstrdup(value); | ||
71 | else if (!strcmp(name, "clone-prefix")) | ||
72 | ctx.cfg.clone_prefix = xstrdup(value); | ||
73 | else if (!strcmp(name, "repo.group")) | ||
74 | ctx.cfg.repo_group = xstrdup(value); | ||
75 | else if (!strcmp(name, "repo.url")) | ||
76 | ctx.repo = cgit_add_repo(value); | ||
77 | else if (!strcmp(name, "repo.name")) | ||
78 | ctx.repo->name = xstrdup(value); | ||
79 | else if (ctx.repo && !strcmp(name, "repo.path")) | ||
80 | ctx.repo->path = trim_end(value, '/'); | ||
81 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) | ||
82 | ctx.repo->clone_url = xstrdup(value); | ||
83 | else if (ctx.repo && !strcmp(name, "repo.desc")) | ||
84 | ctx.repo->desc = xstrdup(value); | ||
85 | else if (ctx.repo && !strcmp(name, "repo.owner")) | ||
86 | ctx.repo->owner = xstrdup(value); | ||
87 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) | ||
88 | ctx.repo->defbranch = xstrdup(value); | ||
89 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) | ||
90 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ | ||
91 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) | ||
92 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); | ||
93 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) | ||
94 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); | ||
95 | else if (ctx.repo && !strcmp(name, "repo.module-link")) | ||
96 | ctx.repo->module_link= xstrdup(value); | ||
97 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { | ||
98 | if (*value == '/') | ||
99 | ctx.repo->readme = xstrdup(value); | ||
100 | else | ||
101 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); | ||
102 | } else if (!strcmp(name, "include")) | ||
103 | cgit_read_config(value, config_cb); | ||
104 | } | ||
105 | |||
106 | static void querystring_cb(const char *name, const char *value) | ||
107 | { | ||
108 | if (!strcmp(name,"r")) { | ||
109 | ctx.qry.repo = xstrdup(value); | ||
110 | ctx.repo = cgit_get_repoinfo(value); | ||
111 | } else if (!strcmp(name, "p")) { | ||
112 | ctx.qry.page = xstrdup(value); | ||
113 | } else if (!strcmp(name, "url")) { | ||
114 | cgit_parse_url(value); | ||
115 | } else if (!strcmp(name, "qt")) { | ||
116 | ctx.qry.grep = xstrdup(value); | ||
117 | } else if (!strcmp(name, "q")) { | ||
118 | ctx.qry.search = xstrdup(value); | ||
119 | } else if (!strcmp(name, "h")) { | ||
120 | ctx.qry.head = xstrdup(value); | ||
121 | ctx.qry.has_symref = 1; | ||
122 | } else if (!strcmp(name, "id")) { | ||
123 | ctx.qry.sha1 = xstrdup(value); | ||
124 | ctx.qry.has_sha1 = 1; | ||
125 | } else if (!strcmp(name, "id2")) { | ||
126 | ctx.qry.sha2 = xstrdup(value); | ||
127 | ctx.qry.has_sha1 = 1; | ||
128 | } else if (!strcmp(name, "ofs")) { | ||
129 | ctx.qry.ofs = atoi(value); | ||
130 | } else if (!strcmp(name, "path")) { | ||
131 | ctx.qry.path = trim_end(value, '/'); | ||
132 | } else if (!strcmp(name, "name")) { | ||
133 | ctx.qry.name = xstrdup(value); | ||
134 | } | ||
135 | } | ||
136 | |||
137 | static void prepare_context(struct cgit_context *ctx) | ||
138 | { | ||
139 | memset(ctx, 0, sizeof(ctx)); | ||
140 | ctx->cfg.agefile = "info/web/last-modified"; | ||
141 | ctx->cfg.cache_dynamic_ttl = 5; | ||
142 | ctx->cfg.cache_max_create_time = 5; | ||
143 | ctx->cfg.cache_repo_ttl = 5; | ||
144 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; | ||
145 | ctx->cfg.cache_root_ttl = 5; | ||
146 | ctx->cfg.cache_static_ttl = -1; | ||
147 | ctx->cfg.css = "/cgit.css"; | ||
148 | ctx->cfg.logo = "/git-logo.png"; | ||
149 | ctx->cfg.max_commit_count = 50; | ||
150 | ctx->cfg.max_lock_attempts = 5; | ||
151 | ctx->cfg.max_msg_len = 60; | ||
152 | ctx->cfg.max_repodesc_len = 60; | ||
153 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; | ||
154 | ctx->cfg.renamelimit = -1; | ||
155 | ctx->cfg.robots = "index, nofollow"; | ||
156 | ctx->cfg.root_title = "Git repository browser"; | ||
157 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; | ||
158 | ctx->page.mimetype = "text/html"; | ||
159 | ctx->page.charset = PAGE_ENCODING; | ||
160 | ctx->page.filename = NULL; | ||
161 | } | ||
162 | |||
13 | static int cgit_prepare_cache(struct cacheitem *item) | 163 | static int cgit_prepare_cache(struct cacheitem *item) |
14 | { | 164 | { |
15 | if (!ctx.repo && ctx.qry.repo) { | 165 | if (!ctx.repo && ctx.qry.repo) { |
16 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, | 166 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, |
17 | "Bad request"); | 167 | "Bad request"); |
18 | cgit_print_http_headers(&ctx); | 168 | cgit_print_http_headers(&ctx); |
19 | cgit_print_docstart(&ctx); | 169 | cgit_print_docstart(&ctx); |
20 | cgit_print_pageheader(&ctx); | 170 | cgit_print_pageheader(&ctx); |
21 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); | 171 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); |
22 | cgit_print_docend(); | 172 | cgit_print_docend(); |
23 | return 0; | 173 | return 0; |
24 | } | 174 | } |
25 | 175 | ||
26 | if (!ctx.repo) { | 176 | if (!ctx.repo) { |
27 | item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); | 177 | item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); |
28 | item->ttl = ctx.cfg.cache_root_ttl; | 178 | item->ttl = ctx.cfg.cache_root_ttl; |
29 | return 1; | 179 | return 1; |
30 | } | 180 | } |
31 | 181 | ||
32 | if (!cgit_cmd) { | 182 | if (!cgit_cmd) { |
33 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, | 183 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, |
34 | cache_safe_filename(ctx.repo->url), | 184 | cache_safe_filename(ctx.repo->url), |
35 | cache_safe_filename(ctx.qry.raw))); | 185 | cache_safe_filename(ctx.qry.raw))); |
36 | item->ttl = ctx.cfg.cache_repo_ttl; | 186 | item->ttl = ctx.cfg.cache_repo_ttl; |
37 | } else { | 187 | } else { |
38 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, | 188 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, |
39 | cache_safe_filename(ctx.repo->url), | 189 | cache_safe_filename(ctx.repo->url), |
40 | ctx.qry.page, | 190 | ctx.qry.page, |
41 | cache_safe_filename(ctx.qry.raw))); | 191 | cache_safe_filename(ctx.qry.raw))); |
42 | if (ctx.qry.has_symref) | 192 | if (ctx.qry.has_symref) |
43 | item->ttl = ctx.cfg.cache_dynamic_ttl; | 193 | item->ttl = ctx.cfg.cache_dynamic_ttl; |
44 | else if (ctx.qry.has_sha1) | 194 | else if (ctx.qry.has_sha1) |
45 | item->ttl = ctx.cfg.cache_static_ttl; | 195 | item->ttl = ctx.cfg.cache_static_ttl; |
46 | else | 196 | else |
47 | item->ttl = ctx.cfg.cache_repo_ttl; | 197 | item->ttl = ctx.cfg.cache_repo_ttl; |
48 | } | 198 | } |
49 | return 1; | 199 | return 1; |
50 | } | 200 | } |
51 | 201 | ||
52 | struct refmatch { | 202 | struct refmatch { |
53 | char *req_ref; | 203 | char *req_ref; |
54 | char *first_ref; | 204 | char *first_ref; |
55 | int match; | 205 | int match; |
56 | }; | 206 | }; |
57 | 207 | ||
58 | int find_current_ref(const char *refname, const unsigned char *sha1, | 208 | int find_current_ref(const char *refname, const unsigned char *sha1, |
59 | int flags, void *cb_data) | 209 | int flags, void *cb_data) |
60 | { | 210 | { |
61 | struct refmatch *info; | 211 | struct refmatch *info; |
62 | 212 | ||
63 | info = (struct refmatch *)cb_data; | 213 | info = (struct refmatch *)cb_data; |
64 | if (!strcmp(refname, info->req_ref)) | 214 | if (!strcmp(refname, info->req_ref)) |
65 | info->match = 1; | 215 | info->match = 1; |
66 | if (!info->first_ref) | 216 | if (!info->first_ref) |
67 | info->first_ref = xstrdup(refname); | 217 | info->first_ref = xstrdup(refname); |
68 | return info->match; | 218 | return info->match; |
69 | } | 219 | } |
70 | 220 | ||
71 | char *find_default_branch(struct cgit_repo *repo) | 221 | char *find_default_branch(struct cgit_repo *repo) |
72 | { | 222 | { |
73 | struct refmatch info; | 223 | struct refmatch info; |
74 | 224 | ||
75 | info.req_ref = repo->defbranch; | 225 | info.req_ref = repo->defbranch; |
76 | info.first_ref = NULL; | 226 | info.first_ref = NULL; |
77 | info.match = 0; | 227 | info.match = 0; |
78 | for_each_branch_ref(find_current_ref, &info); | 228 | for_each_branch_ref(find_current_ref, &info); |
79 | if (info.match) | 229 | if (info.match) |
80 | return info.req_ref; | 230 | return info.req_ref; |
81 | else | 231 | else |
82 | return info.first_ref; | 232 | return info.first_ref; |
83 | } | 233 | } |
84 | 234 | ||
85 | static int prepare_repo_cmd(struct cgit_context *ctx) | 235 | static int prepare_repo_cmd(struct cgit_context *ctx) |
86 | { | 236 | { |
87 | char *tmp; | 237 | char *tmp; |
88 | unsigned char sha1[20]; | 238 | unsigned char sha1[20]; |
89 | int nongit = 0; | 239 | int nongit = 0; |
90 | 240 | ||
91 | setenv("GIT_DIR", ctx->repo->path, 1); | 241 | setenv("GIT_DIR", ctx->repo->path, 1); |
92 | setup_git_directory_gently(&nongit); | 242 | setup_git_directory_gently(&nongit); |
93 | if (nongit) { | 243 | if (nongit) { |
94 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, | 244 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, |
95 | "config error"); | 245 | "config error"); |
96 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); | 246 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); |
97 | ctx->repo = NULL; | 247 | ctx->repo = NULL; |
98 | cgit_print_http_headers(ctx); | 248 | cgit_print_http_headers(ctx); |
99 | cgit_print_docstart(ctx); | 249 | cgit_print_docstart(ctx); |
100 | cgit_print_pageheader(ctx); | 250 | cgit_print_pageheader(ctx); |
101 | cgit_print_error(tmp); | 251 | cgit_print_error(tmp); |
102 | cgit_print_docend(); | 252 | cgit_print_docend(); |
103 | return 1; | 253 | return 1; |
104 | } | 254 | } |
105 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); | 255 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); |
106 | 256 | ||
107 | if (!ctx->qry.head) { | 257 | if (!ctx->qry.head) { |
108 | ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); | 258 | ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); |
109 | ctx->repo->defbranch = ctx->qry.head; | 259 | ctx->repo->defbranch = ctx->qry.head; |
110 | } | 260 | } |
111 | 261 | ||
112 | if (!ctx->qry.head) { | 262 | if (!ctx->qry.head) { |
113 | cgit_print_http_headers(ctx); | 263 | cgit_print_http_headers(ctx); |
114 | cgit_print_docstart(ctx); | 264 | cgit_print_docstart(ctx); |
115 | cgit_print_pageheader(ctx); | 265 | cgit_print_pageheader(ctx); |
116 | cgit_print_error("Repository seems to be empty"); | 266 | cgit_print_error("Repository seems to be empty"); |
117 | cgit_print_docend(); | 267 | cgit_print_docend(); |
118 | return 1; | 268 | return 1; |
119 | } | 269 | } |
120 | 270 | ||
121 | if (get_sha1(ctx->qry.head, sha1)) { | 271 | if (get_sha1(ctx->qry.head, sha1)) { |
122 | tmp = xstrdup(ctx->qry.head); | 272 | tmp = xstrdup(ctx->qry.head); |
123 | ctx->qry.head = ctx->repo->defbranch; | 273 | ctx->qry.head = ctx->repo->defbranch; |
124 | cgit_print_http_headers(ctx); | 274 | cgit_print_http_headers(ctx); |
125 | cgit_print_docstart(ctx); | 275 | cgit_print_docstart(ctx); |
126 | cgit_print_pageheader(ctx); | 276 | cgit_print_pageheader(ctx); |
127 | cgit_print_error(fmt("Invalid branch: %s", tmp)); | 277 | cgit_print_error(fmt("Invalid branch: %s", tmp)); |
128 | cgit_print_docend(); | 278 | cgit_print_docend(); |
129 | return 1; | 279 | return 1; |
130 | } | 280 | } |
131 | return 0; | 281 | return 0; |
132 | } | 282 | } |
133 | 283 | ||
134 | static void process_request(struct cgit_context *ctx) | 284 | static void process_request(struct cgit_context *ctx) |
135 | { | 285 | { |
136 | struct cgit_cmd *cmd; | 286 | struct cgit_cmd *cmd; |
137 | 287 | ||
138 | cmd = cgit_get_cmd(ctx); | 288 | cmd = cgit_get_cmd(ctx); |
139 | if (!cmd) { | 289 | if (!cmd) { |
140 | ctx->page.title = "cgit error"; | 290 | ctx->page.title = "cgit error"; |
@@ -152,152 +302,152 @@ static void process_request(struct cgit_context *ctx) | |||
152 | 302 | ||
153 | if (cmd->want_layout) { | 303 | if (cmd->want_layout) { |
154 | cgit_print_http_headers(ctx); | 304 | cgit_print_http_headers(ctx); |
155 | cgit_print_docstart(ctx); | 305 | cgit_print_docstart(ctx); |
156 | cgit_print_pageheader(ctx); | 306 | cgit_print_pageheader(ctx); |
157 | } | 307 | } |
158 | 308 | ||
159 | cmd->fn(ctx); | 309 | cmd->fn(ctx); |
160 | 310 | ||
161 | if (cmd->want_layout) | 311 | if (cmd->want_layout) |
162 | cgit_print_docend(); | 312 | cgit_print_docend(); |
163 | } | 313 | } |
164 | 314 | ||
165 | static long ttl_seconds(long ttl) | 315 | static long ttl_seconds(long ttl) |
166 | { | 316 | { |
167 | if (ttl<0) | 317 | if (ttl<0) |
168 | return 60 * 60 * 24 * 365; | 318 | return 60 * 60 * 24 * 365; |
169 | else | 319 | else |
170 | return ttl * 60; | 320 | return ttl * 60; |
171 | } | 321 | } |
172 | 322 | ||
173 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) | 323 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) |
174 | { | 324 | { |
175 | int stdout2; | 325 | int stdout2; |
176 | 326 | ||
177 | if (use_cache) { | 327 | if (use_cache) { |
178 | stdout2 = chk_positive(dup(STDOUT_FILENO), | 328 | stdout2 = chk_positive(dup(STDOUT_FILENO), |
179 | "Preserving STDOUT"); | 329 | "Preserving STDOUT"); |
180 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); | 330 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); |
181 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); | 331 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); |
182 | } | 332 | } |
183 | 333 | ||
184 | ctx.page.modified = time(NULL); | 334 | ctx.page.modified = time(NULL); |
185 | ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); | 335 | ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); |
186 | process_request(&ctx); | 336 | process_request(&ctx); |
187 | 337 | ||
188 | if (use_cache) { | 338 | if (use_cache) { |
189 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); | 339 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); |
190 | chk_positive(dup2(stdout2, STDOUT_FILENO), | 340 | chk_positive(dup2(stdout2, STDOUT_FILENO), |
191 | "Restoring original STDOUT"); | 341 | "Restoring original STDOUT"); |
192 | chk_zero(close(stdout2), "Closing temporary STDOUT"); | 342 | chk_zero(close(stdout2), "Closing temporary STDOUT"); |
193 | } | 343 | } |
194 | } | 344 | } |
195 | 345 | ||
196 | static void cgit_check_cache(struct cacheitem *item) | 346 | static void cgit_check_cache(struct cacheitem *item) |
197 | { | 347 | { |
198 | int i = 0; | 348 | int i = 0; |
199 | 349 | ||
200 | top: | 350 | top: |
201 | if (++i > ctx.cfg.max_lock_attempts) { | 351 | if (++i > ctx.cfg.max_lock_attempts) { |
202 | die("cgit_refresh_cache: unable to lock %s: %s", | 352 | die("cgit_refresh_cache: unable to lock %s: %s", |
203 | item->name, strerror(errno)); | 353 | item->name, strerror(errno)); |
204 | } | 354 | } |
205 | if (!cache_exist(item)) { | 355 | if (!cache_exist(item)) { |
206 | if (!cache_lock(item)) { | 356 | if (!cache_lock(item)) { |
207 | sleep(1); | 357 | sleep(1); |
208 | goto top; | 358 | goto top; |
209 | } | 359 | } |
210 | if (!cache_exist(item)) { | 360 | if (!cache_exist(item)) { |
211 | cgit_fill_cache(item, 1); | 361 | cgit_fill_cache(item, 1); |
212 | cache_unlock(item); | 362 | cache_unlock(item); |
213 | } else { | 363 | } else { |
214 | cache_cancel_lock(item); | 364 | cache_cancel_lock(item); |
215 | } | 365 | } |
216 | } else if (cache_expired(item) && cache_lock(item)) { | 366 | } else if (cache_expired(item) && cache_lock(item)) { |
217 | if (cache_expired(item)) { | 367 | if (cache_expired(item)) { |
218 | cgit_fill_cache(item, 1); | 368 | cgit_fill_cache(item, 1); |
219 | cache_unlock(item); | 369 | cache_unlock(item); |
220 | } else { | 370 | } else { |
221 | cache_cancel_lock(item); | 371 | cache_cancel_lock(item); |
222 | } | 372 | } |
223 | } | 373 | } |
224 | } | 374 | } |
225 | 375 | ||
226 | static void cgit_print_cache(struct cacheitem *item) | 376 | static void cgit_print_cache(struct cacheitem *item) |
227 | { | 377 | { |
228 | static char buf[4096]; | 378 | static char buf[4096]; |
229 | ssize_t i; | 379 | ssize_t i; |
230 | 380 | ||
231 | int fd = open(item->name, O_RDONLY); | 381 | int fd = open(item->name, O_RDONLY); |
232 | if (fd<0) | 382 | if (fd<0) |
233 | die("Unable to open cached file %s", item->name); | 383 | die("Unable to open cached file %s", item->name); |
234 | 384 | ||
235 | while((i=read(fd, buf, sizeof(buf))) > 0) | 385 | while((i=read(fd, buf, sizeof(buf))) > 0) |
236 | write(STDOUT_FILENO, buf, i); | 386 | write(STDOUT_FILENO, buf, i); |
237 | 387 | ||
238 | close(fd); | 388 | close(fd); |
239 | } | 389 | } |
240 | 390 | ||
241 | static void cgit_parse_args(int argc, const char **argv) | 391 | static void cgit_parse_args(int argc, const char **argv) |
242 | { | 392 | { |
243 | int i; | 393 | int i; |
244 | 394 | ||
245 | for (i = 1; i < argc; i++) { | 395 | for (i = 1; i < argc; i++) { |
246 | if (!strncmp(argv[i], "--cache=", 8)) { | 396 | if (!strncmp(argv[i], "--cache=", 8)) { |
247 | ctx.cfg.cache_root = xstrdup(argv[i]+8); | 397 | ctx.cfg.cache_root = xstrdup(argv[i]+8); |
248 | } | 398 | } |
249 | if (!strcmp(argv[i], "--nocache")) { | 399 | if (!strcmp(argv[i], "--nocache")) { |
250 | ctx.cfg.nocache = 1; | 400 | ctx.cfg.nocache = 1; |
251 | } | 401 | } |
252 | if (!strncmp(argv[i], "--query=", 8)) { | 402 | if (!strncmp(argv[i], "--query=", 8)) { |
253 | ctx.qry.raw = xstrdup(argv[i]+8); | 403 | ctx.qry.raw = xstrdup(argv[i]+8); |
254 | } | 404 | } |
255 | if (!strncmp(argv[i], "--repo=", 7)) { | 405 | if (!strncmp(argv[i], "--repo=", 7)) { |
256 | ctx.qry.repo = xstrdup(argv[i]+7); | 406 | ctx.qry.repo = xstrdup(argv[i]+7); |
257 | } | 407 | } |
258 | if (!strncmp(argv[i], "--page=", 7)) { | 408 | if (!strncmp(argv[i], "--page=", 7)) { |
259 | ctx.qry.page = xstrdup(argv[i]+7); | 409 | ctx.qry.page = xstrdup(argv[i]+7); |
260 | } | 410 | } |
261 | if (!strncmp(argv[i], "--head=", 7)) { | 411 | if (!strncmp(argv[i], "--head=", 7)) { |
262 | ctx.qry.head = xstrdup(argv[i]+7); | 412 | ctx.qry.head = xstrdup(argv[i]+7); |
263 | ctx.qry.has_symref = 1; | 413 | ctx.qry.has_symref = 1; |
264 | } | 414 | } |
265 | if (!strncmp(argv[i], "--sha1=", 7)) { | 415 | if (!strncmp(argv[i], "--sha1=", 7)) { |
266 | ctx.qry.sha1 = xstrdup(argv[i]+7); | 416 | ctx.qry.sha1 = xstrdup(argv[i]+7); |
267 | ctx.qry.has_sha1 = 1; | 417 | ctx.qry.has_sha1 = 1; |
268 | } | 418 | } |
269 | if (!strncmp(argv[i], "--ofs=", 6)) { | 419 | if (!strncmp(argv[i], "--ofs=", 6)) { |
270 | ctx.qry.ofs = atoi(argv[i]+6); | 420 | ctx.qry.ofs = atoi(argv[i]+6); |
271 | } | 421 | } |
272 | } | 422 | } |
273 | } | 423 | } |
274 | 424 | ||
275 | int main(int argc, const char **argv) | 425 | int main(int argc, const char **argv) |
276 | { | 426 | { |
277 | struct cacheitem item; | 427 | struct cacheitem item; |
278 | const char *cgit_config_env = getenv("CGIT_CONFIG"); | 428 | const char *cgit_config_env = getenv("CGIT_CONFIG"); |
279 | 429 | ||
280 | cgit_prepare_context(&ctx); | 430 | prepare_context(&ctx); |
281 | item.st.st_mtime = time(NULL); | 431 | item.st.st_mtime = time(NULL); |
282 | cgit_repolist.length = 0; | 432 | cgit_repolist.length = 0; |
283 | cgit_repolist.count = 0; | 433 | cgit_repolist.count = 0; |
284 | cgit_repolist.repos = NULL; | 434 | cgit_repolist.repos = NULL; |
285 | 435 | ||
286 | cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, | 436 | cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, |
287 | cgit_global_config_cb); | 437 | config_cb); |
288 | if (getenv("SCRIPT_NAME")) | 438 | if (getenv("SCRIPT_NAME")) |
289 | ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); | 439 | ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); |
290 | if (getenv("QUERY_STRING")) | 440 | if (getenv("QUERY_STRING")) |
291 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); | 441 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); |
292 | cgit_parse_args(argc, argv); | 442 | cgit_parse_args(argc, argv); |
293 | cgit_parse_query(ctx.qry.raw, cgit_querystring_cb); | 443 | cgit_parse_query(ctx.qry.raw, querystring_cb); |
294 | if (!cgit_prepare_cache(&item)) | 444 | if (!cgit_prepare_cache(&item)) |
295 | return 0; | 445 | return 0; |
296 | if (ctx.cfg.nocache) { | 446 | if (ctx.cfg.nocache) { |
297 | cgit_fill_cache(&item, 0); | 447 | cgit_fill_cache(&item, 0); |
298 | } else { | 448 | } else { |
299 | cgit_check_cache(&item); | 449 | cgit_check_cache(&item); |
300 | cgit_print_cache(&item); | 450 | cgit_print_cache(&item); |
301 | } | 451 | } |
302 | return 0; | 452 | return 0; |
303 | } | 453 | } |