Lars Hjemli | 7640d90 | 2006-12-10 22:41:14 +0100 | [diff] [blame] | 1 | /* cgit.c: cgi for the git scm |
| 2 | * |
| 3 | * Copyright (C) 2006 Lars Hjemli |
| 4 | * |
| 5 | * Licensed under GNU General Public License v2 |
| 6 | * (see COPYING for full license text) |
| 7 | */ |
| 8 | |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 9 | #include "cgit.h" |
| 10 | |
Lars Hjemli | 76827d8 | 2006-12-10 23:50:16 +0100 | [diff] [blame] | 11 | const char cgit_version[] = CGIT_VERSION; |
| 12 | |
| 13 | const char cgit_doctype[] = |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 14 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" |
| 15 | " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; |
| 16 | |
Lars Hjemli | 76827d8 | 2006-12-10 23:50:16 +0100 | [diff] [blame] | 17 | const char cgit_error[] = |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 18 | "<div class='error'>%s</div>"; |
| 19 | |
Lars Hjemli | 76827d8 | 2006-12-10 23:50:16 +0100 | [diff] [blame] | 20 | const char cgit_lib_error[] = |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 21 | "<div class='error'>%s: %s</div>"; |
| 22 | |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 23 | int htmlfd = 0; |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 24 | |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 25 | char *cgit_root = "/usr/src/git"; |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 26 | char *cgit_root_title = "Git repository browser"; |
| 27 | char *cgit_css = "/cgit.css"; |
| 28 | char *cgit_logo = "/git-logo.png"; |
| 29 | char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/"; |
| 30 | char *cgit_virtual_root = NULL; |
| 31 | |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 32 | char *cgit_cache_root = "/var/cache/cgit"; |
| 33 | |
Lars Hjemli | 318d106 | 2006-12-11 12:10:12 +0100 | [diff] [blame^] | 34 | int cgit_max_lock_attempts = 5; |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 35 | int cgit_cache_root_ttl = 5; |
| 36 | int cgit_cache_repo_ttl = 5; |
| 37 | int cgit_cache_dynamic_ttl = 5; |
| 38 | int cgit_cache_static_ttl = -1; |
| 39 | int cgit_cache_max_create_time = 5; |
| 40 | |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 41 | char *cgit_repo_name = NULL; |
| 42 | char *cgit_repo_desc = NULL; |
| 43 | char *cgit_repo_owner = NULL; |
| 44 | |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 45 | int cgit_query_has_symref = 0; |
| 46 | int cgit_query_has_sha1 = 0; |
| 47 | |
| 48 | char *cgit_querystring = NULL; |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 49 | char *cgit_query_repo = NULL; |
| 50 | char *cgit_query_page = NULL; |
| 51 | char *cgit_query_head = NULL; |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 52 | char *cgit_query_sha1 = NULL; |
| 53 | |
| 54 | struct cacheitem cacheitem; |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 55 | |
| 56 | int cgit_parse_query(char *txt, configfn fn) |
| 57 | { |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 58 | char *t, *value = NULL, c; |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 59 | |
| 60 | if (!txt) |
| 61 | return 0; |
| 62 | |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 63 | t = txt = xstrdup(txt); |
| 64 | |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 65 | while((c=*t) != '\0') { |
| 66 | if (c=='=') { |
| 67 | *t = '\0'; |
| 68 | value = t+1; |
| 69 | } else if (c=='&') { |
| 70 | *t = '\0'; |
| 71 | (*fn)(txt, value); |
| 72 | txt = t+1; |
| 73 | value = NULL; |
| 74 | } |
| 75 | t++; |
| 76 | } |
| 77 | if (t!=txt) |
| 78 | (*fn)(txt, value); |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | void cgit_global_config_cb(const char *name, const char *value) |
| 83 | { |
| 84 | if (!strcmp(name, "root")) |
| 85 | cgit_root = xstrdup(value); |
| 86 | else if (!strcmp(name, "root-title")) |
| 87 | cgit_root_title = xstrdup(value); |
| 88 | else if (!strcmp(name, "css")) |
| 89 | cgit_css = xstrdup(value); |
| 90 | else if (!strcmp(name, "logo")) |
| 91 | cgit_logo = xstrdup(value); |
| 92 | else if (!strcmp(name, "logo-link")) |
| 93 | cgit_logo_link = xstrdup(value); |
| 94 | else if (!strcmp(name, "virtual-root")) |
| 95 | cgit_virtual_root = xstrdup(value); |
| 96 | } |
| 97 | |
| 98 | void cgit_repo_config_cb(const char *name, const char *value) |
| 99 | { |
| 100 | if (!strcmp(name, "name")) |
| 101 | cgit_repo_name = xstrdup(value); |
| 102 | else if (!strcmp(name, "desc")) |
| 103 | cgit_repo_desc = xstrdup(value); |
| 104 | else if (!strcmp(name, "owner")) |
| 105 | cgit_repo_owner = xstrdup(value); |
| 106 | } |
| 107 | |
| 108 | void cgit_querystring_cb(const char *name, const char *value) |
| 109 | { |
| 110 | if (!strcmp(name,"r")) |
| 111 | cgit_query_repo = xstrdup(value); |
| 112 | else if (!strcmp(name, "p")) |
| 113 | cgit_query_page = xstrdup(value); |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 114 | else if (!strcmp(name, "h")) { |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 115 | cgit_query_head = xstrdup(value); |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 116 | cgit_query_has_symref = 1; |
| 117 | } else if (!strcmp(name, "id")) { |
| 118 | cgit_query_sha1 = xstrdup(value); |
| 119 | cgit_query_has_sha1 = 1; |
| 120 | } |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | char *cgit_repourl(const char *reponame) |
| 124 | { |
| 125 | if (cgit_virtual_root) { |
| 126 | return fmt("%s/%s/", cgit_virtual_root, reponame); |
| 127 | } else { |
| 128 | return fmt("?r=%s", reponame); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | char *cgit_pageurl(const char *reponame, const char *pagename, |
| 133 | const char *query) |
| 134 | { |
| 135 | if (cgit_virtual_root) { |
| 136 | return fmt("%s/%s/%s/?%s", cgit_virtual_root, reponame, |
| 137 | pagename, query); |
| 138 | } else { |
| 139 | return fmt("?r=%s&p=%s&%s", reponame, pagename, query); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | static int cgit_print_branch_cb(const char *refname, const unsigned char *sha1, |
| 144 | int flags, void *cb_data) |
| 145 | { |
| 146 | struct commit *commit; |
| 147 | char buf[256], *url; |
| 148 | |
| 149 | commit = lookup_commit(sha1); |
| 150 | if (commit && !parse_commit(commit)){ |
| 151 | html("<tr><td>"); |
| 152 | url = cgit_pageurl(cgit_query_repo, "log", |
| 153 | fmt("h=%s", refname)); |
| 154 | html_link_open(url, NULL, NULL); |
| 155 | strncpy(buf, refname, sizeof(buf)); |
| 156 | html_txt(buf); |
| 157 | html_link_close(); |
| 158 | html("</td><td>"); |
| 159 | pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0, buf, |
| 160 | sizeof(buf), 0, NULL, NULL, 0); |
| 161 | html_txt(buf); |
| 162 | html("</td></tr>\n"); |
| 163 | } else { |
| 164 | html("<tr><td>"); |
| 165 | html_txt(buf); |
| 166 | html("</td><td>"); |
| 167 | htmlf("*** bad ref %s", sha1_to_hex(sha1)); |
| 168 | html("</td></tr>\n"); |
| 169 | } |
| 170 | return 0; |
| 171 | } |
| 172 | |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 173 | /* Sun, 06 Nov 1994 08:49:37 GMT */ |
| 174 | static char *http_date(time_t t) |
| 175 | { |
| 176 | static char day[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; |
| 177 | static char month[][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 178 | "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; |
| 179 | struct tm *tm = gmtime(&t); |
| 180 | return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], |
| 181 | tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, |
| 182 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 183 | } |
| 184 | |
| 185 | static int ttl_seconds(int ttl) |
| 186 | { |
| 187 | if (ttl<0) |
| 188 | return 60 * 60 * 24 * 365; |
| 189 | else |
| 190 | return ttl * 60; |
| 191 | } |
| 192 | |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 193 | static void cgit_print_docstart(char *title) |
| 194 | { |
| 195 | html("Content-Type: text/html; charset=utf-8\n"); |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 196 | htmlf("Last-Modified: %s\n", http_date(cacheitem.st.st_mtime)); |
| 197 | htmlf("Expires: %s\n", http_date(cacheitem.st.st_mtime + |
| 198 | ttl_seconds(cacheitem.ttl))); |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 199 | html("\n"); |
| 200 | html(cgit_doctype); |
| 201 | html("<html>\n"); |
| 202 | html("<head>\n"); |
| 203 | html("<title>"); |
| 204 | html_txt(title); |
| 205 | html("</title>\n"); |
Lars Hjemli | 76827d8 | 2006-12-10 23:50:16 +0100 | [diff] [blame] | 206 | htmlf("<meta name='generator' content='cgit v%s'/>\n", cgit_version); |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 207 | html("<link rel='stylesheet' type='text/css' href='"); |
| 208 | html_attr(cgit_css); |
| 209 | html("'/>\n"); |
| 210 | html("</head>\n"); |
| 211 | html("<body>\n"); |
| 212 | } |
| 213 | |
| 214 | static void cgit_print_docend() |
| 215 | { |
| 216 | html("</body>\n</html>\n"); |
| 217 | } |
| 218 | |
| 219 | static void cgit_print_pageheader(char *title) |
| 220 | { |
| 221 | html("<div id='header'>"); |
| 222 | htmlf("<a href='%s'>", cgit_logo_link); |
| 223 | htmlf("<img id='logo' src='%s'/>\n", cgit_logo); |
| 224 | htmlf("</a>"); |
| 225 | html_txt(title); |
| 226 | html("</div>"); |
| 227 | } |
| 228 | |
| 229 | static void cgit_print_repolist() |
| 230 | { |
| 231 | DIR *d; |
| 232 | struct dirent *de; |
| 233 | struct stat st; |
| 234 | char *name; |
| 235 | |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 236 | chdir(cgit_root); |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 237 | cgit_print_docstart(cgit_root_title); |
| 238 | cgit_print_pageheader(cgit_root_title); |
| 239 | |
| 240 | if (!(d = opendir("."))) { |
| 241 | htmlf(cgit_lib_error, "Unable to scan repository directory", |
| 242 | strerror(errno)); |
| 243 | cgit_print_docend(); |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | html("<h2>Repositories</h2>\n"); |
| 248 | html("<table class='list'>"); |
| 249 | html("<tr><th>Name</th><th>Description</th><th>Owner</th></tr>\n"); |
| 250 | while ((de = readdir(d)) != NULL) { |
| 251 | if (de->d_name[0] == '.') |
| 252 | continue; |
| 253 | if (stat(de->d_name, &st) < 0) |
| 254 | continue; |
| 255 | if (!S_ISDIR(st.st_mode)) |
| 256 | continue; |
| 257 | |
| 258 | cgit_repo_name = cgit_repo_desc = cgit_repo_owner = NULL; |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 259 | name = fmt("%s/info/cgit", de->d_name); |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 260 | if (cgit_read_config(name, cgit_repo_config_cb)) |
| 261 | continue; |
| 262 | |
| 263 | html("<tr><td>"); |
| 264 | html_link_open(cgit_repourl(de->d_name), NULL, NULL); |
| 265 | html_txt(cgit_repo_name); |
| 266 | html_link_close(); |
| 267 | html("</td><td>"); |
| 268 | html_txt(cgit_repo_desc); |
| 269 | html("</td><td>"); |
| 270 | html_txt(cgit_repo_owner); |
| 271 | html("</td></tr>\n"); |
| 272 | } |
| 273 | closedir(d); |
| 274 | html("</table>"); |
| 275 | cgit_print_docend(); |
| 276 | } |
| 277 | |
| 278 | static void cgit_print_branches() |
| 279 | { |
| 280 | html("<table class='list'>"); |
| 281 | html("<tr><th>Branch name</th><th>Head commit</th></tr>\n"); |
| 282 | for_each_branch_ref(cgit_print_branch_cb, NULL); |
| 283 | html("</table>"); |
| 284 | } |
| 285 | |
| 286 | static int get_one_line(char *txt) |
| 287 | { |
| 288 | char *t; |
| 289 | |
| 290 | for(t=txt; *t != '\n' && t != '\0'; t++) |
| 291 | ; |
| 292 | *t = '\0'; |
| 293 | return t-txt-1; |
| 294 | } |
| 295 | |
| 296 | static void cgit_print_commit_shortlog(struct commit *commit) |
| 297 | { |
| 298 | char *h, *t, *p; |
| 299 | char *tree = NULL, *author = NULL, *subject = NULL; |
| 300 | int len; |
| 301 | time_t sec; |
| 302 | struct tm *time; |
| 303 | char buf[32]; |
| 304 | |
| 305 | h = t = commit->buffer; |
| 306 | |
| 307 | if (strncmp(h, "tree ", 5)) |
| 308 | die("Bad commit format: %s", |
| 309 | sha1_to_hex(commit->object.sha1)); |
| 310 | |
| 311 | len = get_one_line(h); |
| 312 | tree = h+5; |
| 313 | h += len + 2; |
| 314 | |
| 315 | while (!strncmp(h, "parent ", 7)) |
| 316 | h += get_one_line(h) + 2; |
| 317 | |
| 318 | if (!strncmp(h, "author ", 7)) { |
| 319 | author = h+7; |
| 320 | h += get_one_line(h) + 2; |
| 321 | t = author; |
| 322 | while(t!=h && *t!='<') |
| 323 | t++; |
| 324 | *t='\0'; |
| 325 | p = t; |
| 326 | while(--t!=author && *t==' ') |
| 327 | *t='\0'; |
| 328 | while(++p!=h && *p!='>') |
| 329 | ; |
| 330 | while(++p!=h && !isdigit(*p)) |
| 331 | ; |
| 332 | |
| 333 | t = p; |
| 334 | while(++p && isdigit(*p)) |
| 335 | ; |
| 336 | *p = '\0'; |
| 337 | sec = atoi(t); |
| 338 | time = gmtime(&sec); |
| 339 | } |
| 340 | |
| 341 | while((len = get_one_line(h)) > 0) |
| 342 | h += len+2; |
| 343 | |
| 344 | h++; |
| 345 | len = get_one_line(h); |
| 346 | |
| 347 | subject = h; |
| 348 | |
| 349 | html("<tr><td>"); |
| 350 | strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", time); |
| 351 | html_txt(buf); |
| 352 | html("</td><td>"); |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 353 | char *qry = fmt("id=%s", sha1_to_hex(commit->object.sha1)); |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 354 | char *url = cgit_pageurl(cgit_query_repo, "view", qry); |
| 355 | html_link_open(url, NULL, NULL); |
| 356 | html_txt(subject); |
| 357 | html_link_close(); |
| 358 | html("</td><td>"); |
| 359 | html_txt(author); |
| 360 | html("</td></tr>\n"); |
| 361 | } |
| 362 | |
| 363 | static void cgit_print_log(const char *tip, int ofs, int cnt) |
| 364 | { |
| 365 | struct rev_info rev; |
| 366 | struct commit *commit; |
| 367 | const char *argv[2] = {NULL, tip}; |
| 368 | int n = 0; |
| 369 | |
| 370 | init_revisions(&rev, NULL); |
| 371 | rev.abbrev = DEFAULT_ABBREV; |
| 372 | rev.commit_format = CMIT_FMT_DEFAULT; |
| 373 | rev.verbose_header = 1; |
| 374 | rev.show_root_diff = 0; |
| 375 | setup_revisions(2, argv, &rev, NULL); |
| 376 | prepare_revision_walk(&rev); |
| 377 | |
| 378 | html("<h2>Log</h2>"); |
| 379 | html("<table class='list'>"); |
| 380 | html("<tr><th>Date</th><th>Message</th><th>Author</th></tr>\n"); |
| 381 | while ((commit = get_revision(&rev)) != NULL && n++ < 100) { |
| 382 | cgit_print_commit_shortlog(commit); |
| 383 | free(commit->buffer); |
| 384 | commit->buffer = NULL; |
| 385 | free_commit_list(commit->parents); |
| 386 | commit->parents = NULL; |
| 387 | } |
| 388 | html("</table>\n"); |
| 389 | } |
| 390 | |
| 391 | static void cgit_print_repo_summary() |
| 392 | { |
| 393 | html("<h2>"); |
| 394 | html_txt("Repo summary page"); |
| 395 | html("</h2>"); |
| 396 | cgit_print_branches(); |
| 397 | } |
| 398 | |
| 399 | static void cgit_print_object(char *hex) |
| 400 | { |
| 401 | unsigned char sha1[20]; |
| 402 | //struct object *object; |
| 403 | char type[20]; |
| 404 | unsigned char *buf; |
| 405 | unsigned long size; |
| 406 | |
| 407 | if (get_sha1_hex(hex, sha1)){ |
| 408 | htmlf(cgit_error, "Bad hex value"); |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | if (sha1_object_info(sha1, type, NULL)){ |
| 413 | htmlf(cgit_error, "Bad object name"); |
| 414 | return; |
| 415 | } |
| 416 | |
| 417 | buf = read_sha1_file(sha1, type, &size); |
| 418 | if (!buf) { |
| 419 | htmlf(cgit_error, "Error reading object"); |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | buf[size] = '\0'; |
| 424 | html("<h2>Object view</h2>"); |
| 425 | htmlf("sha1=%s<br/>type=%s<br/>size=%i<br/>", hex, type, size); |
| 426 | html("<pre>"); |
| 427 | html_txt(buf); |
| 428 | html("</pre>"); |
| 429 | } |
| 430 | |
| 431 | static void cgit_print_repo_page() |
| 432 | { |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 433 | if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) || |
| 434 | cgit_read_config("info/cgit", cgit_repo_config_cb)) { |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 435 | char *title = fmt("%s - %s", cgit_root_title, "Bad request"); |
| 436 | cgit_print_docstart(title); |
| 437 | cgit_print_pageheader(title); |
| 438 | htmlf(cgit_lib_error, "Unable to scan repository", |
| 439 | strerror(errno)); |
| 440 | cgit_print_docend(); |
| 441 | return; |
| 442 | } |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 443 | setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1); |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 444 | char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc); |
| 445 | cgit_print_docstart(title); |
| 446 | cgit_print_pageheader(title); |
| 447 | if (!cgit_query_page) |
| 448 | cgit_print_repo_summary(); |
| 449 | else if (!strcmp(cgit_query_page, "log")) { |
| 450 | cgit_print_log(cgit_query_head, 0, 100); |
| 451 | } else if (!strcmp(cgit_query_page, "view")) { |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 452 | cgit_print_object(cgit_query_sha1); |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 453 | } |
| 454 | cgit_print_docend(); |
| 455 | } |
| 456 | |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 457 | static void cgit_fill_cache(struct cacheitem *item) |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 458 | { |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 459 | htmlfd = item->fd; |
| 460 | item->st.st_mtime = time(NULL); |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 461 | if (cgit_query_repo) |
| 462 | cgit_print_repo_page(); |
| 463 | else |
| 464 | cgit_print_repolist(); |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | static void cgit_refresh_cache(struct cacheitem *item) |
| 468 | { |
Lars Hjemli | 318d106 | 2006-12-11 12:10:12 +0100 | [diff] [blame^] | 469 | int i = 0; |
| 470 | |
Lars Hjemli | f5069d8 | 2006-12-11 09:57:58 +0100 | [diff] [blame] | 471 | cache_prepare(item); |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 472 | top: |
Lars Hjemli | 318d106 | 2006-12-11 12:10:12 +0100 | [diff] [blame^] | 473 | if (++i > cgit_max_lock_attempts) { |
| 474 | die("cgit_refresh_cache: unable to lock %s: %s", |
| 475 | item->name, strerror(errno)); |
| 476 | } |
Lars Hjemli | f5069d8 | 2006-12-11 09:57:58 +0100 | [diff] [blame] | 477 | if (!cache_exist(item)) { |
| 478 | if (!cache_lock(item)) { |
Lars Hjemli | 318d106 | 2006-12-11 12:10:12 +0100 | [diff] [blame^] | 479 | sleep(1); |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 480 | goto top; |
| 481 | } |
Lars Hjemli | f5069d8 | 2006-12-11 09:57:58 +0100 | [diff] [blame] | 482 | if (!cache_exist(item)) |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 483 | cgit_fill_cache(item); |
Lars Hjemli | f5069d8 | 2006-12-11 09:57:58 +0100 | [diff] [blame] | 484 | cache_unlock(item); |
| 485 | } else if (cache_expired(item) && cache_lock(item)) { |
| 486 | if (cache_expired(item)) |
| 487 | cgit_fill_cache(item); |
| 488 | cache_unlock(item); |
Lars Hjemli | 25105d7 | 2006-12-10 22:31:36 +0100 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
| 492 | static void cgit_print_cache(struct cacheitem *item) |
| 493 | { |
| 494 | static char buf[4096]; |
| 495 | ssize_t i; |
| 496 | |
| 497 | int fd = open(item->name, O_RDONLY); |
| 498 | if (fd<0) |
| 499 | die("Unable to open cached file %s", item->name); |
| 500 | |
| 501 | while((i=read(fd, buf, sizeof(buf))) > 0) |
| 502 | write(STDOUT_FILENO, buf, i); |
| 503 | |
| 504 | close(fd); |
| 505 | } |
| 506 | |
| 507 | int main(int argc, const char **argv) |
| 508 | { |
| 509 | cgit_read_config("/etc/cgitrc", cgit_global_config_cb); |
| 510 | cgit_querystring = xstrdup(getenv("QUERY_STRING")); |
| 511 | cgit_parse_query(cgit_querystring, cgit_querystring_cb); |
| 512 | cgit_refresh_cache(&cacheitem); |
| 513 | cgit_print_cache(&cacheitem); |
Lars Hjemli | 0d169ad | 2006-12-09 15:18:17 +0100 | [diff] [blame] | 514 | return 0; |
| 515 | } |