blob: d1abaa052135a8be8f0a13c0ccb9e7c53c4c8347 [file] [log] [blame]
Lars Hjemli7640d902006-12-10 22:41:14 +01001/* 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 Hjemli0d169ad2006-12-09 15:18:17 +01009#include "cgit.h"
10
Lars Hjemli76827d82006-12-10 23:50:16 +010011const char cgit_version[] = CGIT_VERSION;
12
Lars Hjemli5a106eb2006-12-11 16:38:30 +010013static void cgit_print_repo_page(struct cacheitem *item)
Lars Hjemli0d169ad2006-12-09 15:18:17 +010014{
Lars Hjemli25105d72006-12-10 22:31:36 +010015 if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) ||
16 cgit_read_config("info/cgit", cgit_repo_config_cb)) {
Lars Hjemli0d169ad2006-12-09 15:18:17 +010017 char *title = fmt("%s - %s", cgit_root_title, "Bad request");
Lars Hjemli5a106eb2006-12-11 16:38:30 +010018 cgit_print_docstart(title, item);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010019 cgit_print_pageheader(title);
Lars Hjemli5a106eb2006-12-11 16:38:30 +010020 cgit_print_error(fmt("Unable to scan repository: %s",
21 strerror(errno)));
Lars Hjemli0d169ad2006-12-09 15:18:17 +010022 cgit_print_docend();
23 return;
24 }
Lars Hjemli25105d72006-12-10 22:31:36 +010025 setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010026 char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc);
Lars Hjemli5a106eb2006-12-11 16:38:30 +010027 cgit_print_docstart(title, item);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010028 cgit_print_pageheader(title);
29 if (!cgit_query_page)
Lars Hjemlid14c5f62006-12-11 17:04:19 +010030 cgit_print_summary();
Lars Hjemli0d169ad2006-12-09 15:18:17 +010031 else if (!strcmp(cgit_query_page, "log")) {
32 cgit_print_log(cgit_query_head, 0, 100);
33 } else if (!strcmp(cgit_query_page, "view")) {
Lars Hjemlidf631192006-12-11 17:12:26 +010034 cgit_print_view(cgit_query_sha1);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010035 }
36 cgit_print_docend();
37}
38
Lars Hjemli25105d72006-12-10 22:31:36 +010039static void cgit_fill_cache(struct cacheitem *item)
Lars Hjemli0d169ad2006-12-09 15:18:17 +010040{
Lars Hjemli25105d72006-12-10 22:31:36 +010041 htmlfd = item->fd;
42 item->st.st_mtime = time(NULL);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010043 if (cgit_query_repo)
Lars Hjemli5a106eb2006-12-11 16:38:30 +010044 cgit_print_repo_page(item);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010045 else
Lars Hjemli5a106eb2006-12-11 16:38:30 +010046 cgit_print_repolist(item);
Lars Hjemli25105d72006-12-10 22:31:36 +010047}
48
Lars Hjemli44923f82006-12-11 17:25:41 +010049static void cgit_check_cache(struct cacheitem *item)
Lars Hjemli25105d72006-12-10 22:31:36 +010050{
Lars Hjemli318d1062006-12-11 12:10:12 +010051 int i = 0;
52
Lars Hjemlif5069d82006-12-11 09:57:58 +010053 cache_prepare(item);
Lars Hjemli25105d72006-12-10 22:31:36 +010054 top:
Lars Hjemli318d1062006-12-11 12:10:12 +010055 if (++i > cgit_max_lock_attempts) {
56 die("cgit_refresh_cache: unable to lock %s: %s",
57 item->name, strerror(errno));
58 }
Lars Hjemlif5069d82006-12-11 09:57:58 +010059 if (!cache_exist(item)) {
60 if (!cache_lock(item)) {
Lars Hjemli318d1062006-12-11 12:10:12 +010061 sleep(1);
Lars Hjemli25105d72006-12-10 22:31:36 +010062 goto top;
63 }
Lars Hjemlif5069d82006-12-11 09:57:58 +010064 if (!cache_exist(item))
Lars Hjemli25105d72006-12-10 22:31:36 +010065 cgit_fill_cache(item);
Lars Hjemlif5069d82006-12-11 09:57:58 +010066 cache_unlock(item);
67 } else if (cache_expired(item) && cache_lock(item)) {
68 if (cache_expired(item))
69 cgit_fill_cache(item);
70 cache_unlock(item);
Lars Hjemli25105d72006-12-10 22:31:36 +010071 }
72}
73
74static void cgit_print_cache(struct cacheitem *item)
75{
76 static char buf[4096];
77 ssize_t i;
78
79 int fd = open(item->name, O_RDONLY);
80 if (fd<0)
81 die("Unable to open cached file %s", item->name);
82
83 while((i=read(fd, buf, sizeof(buf))) > 0)
84 write(STDOUT_FILENO, buf, i);
85
86 close(fd);
87}
88
89int main(int argc, const char **argv)
90{
Lars Hjemli44923f82006-12-11 17:25:41 +010091 struct cacheitem item;
92
Lars Hjemli25105d72006-12-10 22:31:36 +010093 cgit_read_config("/etc/cgitrc", cgit_global_config_cb);
94 cgit_querystring = xstrdup(getenv("QUERY_STRING"));
95 cgit_parse_query(cgit_querystring, cgit_querystring_cb);
Lars Hjemli44923f82006-12-11 17:25:41 +010096
97 cgit_check_cache(&item);
98 cgit_print_cache(&item);
Lars Hjemli0d169ad2006-12-09 15:18:17 +010099 return 0;
100}