|
diff --git a/cgit.c b/cgit.c index 52d9bd3..d1abaa0 100644 --- a/ cgit.c+++ b/ cgit.c |
|
@@ -1,171 +1,100 @@ |
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 | |
10 | |
11 | const char cgit_version[] = CGIT_VERSION; |
11 | const char cgit_version[] = CGIT_VERSION; |
12 | |
12 | |
13 | int htmlfd = 0; |
| |
14 | |
| |
15 | char *cgit_root = "/usr/src/git"; |
| |
16 | char *cgit_root_title = "Git repository browser"; |
| |
17 | char *cgit_css = "/cgit.css"; |
| |
18 | char *cgit_logo = "/git-logo.png"; |
| |
19 | char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/"; |
| |
20 | char *cgit_virtual_root = NULL; |
| |
21 | |
| |
22 | char *cgit_cache_root = "/var/cache/cgit"; |
| |
23 | |
| |
24 | int cgit_max_lock_attempts = 5; |
| |
25 | int cgit_cache_root_ttl = 5; |
| |
26 | int cgit_cache_repo_ttl = 5; |
| |
27 | int cgit_cache_dynamic_ttl = 5; |
| |
28 | int cgit_cache_static_ttl = -1; |
| |
29 | int cgit_cache_max_create_time = 5; |
| |
30 | |
| |
31 | char *cgit_repo_name = NULL; |
| |
32 | char *cgit_repo_desc = NULL; |
| |
33 | char *cgit_repo_owner = NULL; |
| |
34 | |
| |
35 | int cgit_query_has_symref = 0; |
| |
36 | int cgit_query_has_sha1 = 0; |
| |
37 | |
| |
38 | char *cgit_querystring = NULL; |
| |
39 | char *cgit_query_repo = NULL; |
| |
40 | char *cgit_query_page = NULL; |
| |
41 | char *cgit_query_head = NULL; |
| |
42 | char *cgit_query_sha1 = NULL; |
| |
43 | |
| |
44 | struct cacheitem cacheitem; |
| |
45 | |
| |
46 | void cgit_global_config_cb(const char *name, const char *value) |
| |
47 | { |
| |
48 | if (!strcmp(name, "root")) |
| |
49 | cgit_root = xstrdup(value); |
| |
50 | else if (!strcmp(name, "root-title")) |
| |
51 | cgit_root_title = xstrdup(value); |
| |
52 | else if (!strcmp(name, "css")) |
| |
53 | cgit_css = xstrdup(value); |
| |
54 | else if (!strcmp(name, "logo")) |
| |
55 | cgit_logo = xstrdup(value); |
| |
56 | else if (!strcmp(name, "logo-link")) |
| |
57 | cgit_logo_link = xstrdup(value); |
| |
58 | else if (!strcmp(name, "virtual-root")) |
| |
59 | cgit_virtual_root = xstrdup(value); |
| |
60 | } |
| |
61 | |
| |
62 | void cgit_repo_config_cb(const char *name, const char *value) |
| |
63 | { |
| |
64 | if (!strcmp(name, "name")) |
| |
65 | cgit_repo_name = xstrdup(value); |
| |
66 | else if (!strcmp(name, "desc")) |
| |
67 | cgit_repo_desc = xstrdup(value); |
| |
68 | else if (!strcmp(name, "owner")) |
| |
69 | cgit_repo_owner = xstrdup(value); |
| |
70 | } |
| |
71 | |
| |
72 | void cgit_querystring_cb(const char *name, const char *value) |
| |
73 | { |
| |
74 | if (!strcmp(name,"r")) |
| |
75 | cgit_query_repo = xstrdup(value); |
| |
76 | else if (!strcmp(name, "p")) |
| |
77 | cgit_query_page = xstrdup(value); |
| |
78 | else if (!strcmp(name, "h")) { |
| |
79 | cgit_query_head = xstrdup(value); |
| |
80 | cgit_query_has_symref = 1; |
| |
81 | } else if (!strcmp(name, "id")) { |
| |
82 | cgit_query_sha1 = xstrdup(value); |
| |
83 | cgit_query_has_sha1 = 1; |
| |
84 | } |
| |
85 | } |
| |
86 | |
| |
87 | static void cgit_print_repo_page(struct cacheitem *item) |
13 | static void cgit_print_repo_page(struct cacheitem *item) |
88 | { |
14 | { |
89 | if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) || |
15 | if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) || |
90 | cgit_read_config("info/cgit", cgit_repo_config_cb)) { |
16 | cgit_read_config("info/cgit", cgit_repo_config_cb)) { |
91 | char *title = fmt("%s - %s", cgit_root_title, "Bad request"); |
17 | char *title = fmt("%s - %s", cgit_root_title, "Bad request"); |
92 | cgit_print_docstart(title, item); |
18 | cgit_print_docstart(title, item); |
93 | cgit_print_pageheader(title); |
19 | cgit_print_pageheader(title); |
94 | cgit_print_error(fmt("Unable to scan repository: %s", |
20 | cgit_print_error(fmt("Unable to scan repository: %s", |
95 | strerror(errno))); |
21 | strerror(errno))); |
96 | cgit_print_docend(); |
22 | cgit_print_docend(); |
97 | return; |
23 | return; |
98 | } |
24 | } |
99 | setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1); |
25 | setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1); |
100 | char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc); |
26 | char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc); |
101 | cgit_print_docstart(title, item); |
27 | cgit_print_docstart(title, item); |
102 | cgit_print_pageheader(title); |
28 | cgit_print_pageheader(title); |
103 | if (!cgit_query_page) |
29 | if (!cgit_query_page) |
104 | cgit_print_summary(); |
30 | cgit_print_summary(); |
105 | else if (!strcmp(cgit_query_page, "log")) { |
31 | else if (!strcmp(cgit_query_page, "log")) { |
106 | cgit_print_log(cgit_query_head, 0, 100); |
32 | cgit_print_log(cgit_query_head, 0, 100); |
107 | } else if (!strcmp(cgit_query_page, "view")) { |
33 | } else if (!strcmp(cgit_query_page, "view")) { |
108 | cgit_print_view(cgit_query_sha1); |
34 | cgit_print_view(cgit_query_sha1); |
109 | } |
35 | } |
110 | cgit_print_docend(); |
36 | cgit_print_docend(); |
111 | } |
37 | } |
112 | |
38 | |
113 | static void cgit_fill_cache(struct cacheitem *item) |
39 | static void cgit_fill_cache(struct cacheitem *item) |
114 | { |
40 | { |
115 | htmlfd = item->fd; |
41 | htmlfd = item->fd; |
116 | item->st.st_mtime = time(NULL); |
42 | item->st.st_mtime = time(NULL); |
117 | if (cgit_query_repo) |
43 | if (cgit_query_repo) |
118 | cgit_print_repo_page(item); |
44 | cgit_print_repo_page(item); |
119 | else |
45 | else |
120 | cgit_print_repolist(item); |
46 | cgit_print_repolist(item); |
121 | } |
47 | } |
122 | |
48 | |
123 | static void cgit_refresh_cache(struct cacheitem *item) |
49 | static void cgit_check_cache(struct cacheitem *item) |
124 | { |
50 | { |
125 | int i = 0; |
51 | int i = 0; |
126 | |
52 | |
127 | cache_prepare(item); |
53 | cache_prepare(item); |
128 | top: |
54 | top: |
129 | if (++i > cgit_max_lock_attempts) { |
55 | if (++i > cgit_max_lock_attempts) { |
130 | die("cgit_refresh_cache: unable to lock %s: %s", |
56 | die("cgit_refresh_cache: unable to lock %s: %s", |
131 | item->name, strerror(errno)); |
57 | item->name, strerror(errno)); |
132 | } |
58 | } |
133 | if (!cache_exist(item)) { |
59 | if (!cache_exist(item)) { |
134 | if (!cache_lock(item)) { |
60 | if (!cache_lock(item)) { |
135 | sleep(1); |
61 | sleep(1); |
136 | goto top; |
62 | goto top; |
137 | } |
63 | } |
138 | if (!cache_exist(item)) |
64 | if (!cache_exist(item)) |
139 | cgit_fill_cache(item); |
65 | cgit_fill_cache(item); |
140 | cache_unlock(item); |
66 | cache_unlock(item); |
141 | } else if (cache_expired(item) && cache_lock(item)) { |
67 | } else if (cache_expired(item) && cache_lock(item)) { |
142 | if (cache_expired(item)) |
68 | if (cache_expired(item)) |
143 | cgit_fill_cache(item); |
69 | cgit_fill_cache(item); |
144 | cache_unlock(item); |
70 | cache_unlock(item); |
145 | } |
71 | } |
146 | } |
72 | } |
147 | |
73 | |
148 | static void cgit_print_cache(struct cacheitem *item) |
74 | static void cgit_print_cache(struct cacheitem *item) |
149 | { |
75 | { |
150 | static char buf[4096]; |
76 | static char buf[4096]; |
151 | ssize_t i; |
77 | ssize_t i; |
152 | |
78 | |
153 | int fd = open(item->name, O_RDONLY); |
79 | int fd = open(item->name, O_RDONLY); |
154 | if (fd<0) |
80 | if (fd<0) |
155 | die("Unable to open cached file %s", item->name); |
81 | die("Unable to open cached file %s", item->name); |
156 | |
82 | |
157 | while((i=read(fd, buf, sizeof(buf))) > 0) |
83 | while((i=read(fd, buf, sizeof(buf))) > 0) |
158 | write(STDOUT_FILENO, buf, i); |
84 | write(STDOUT_FILENO, buf, i); |
159 | |
85 | |
160 | close(fd); |
86 | close(fd); |
161 | } |
87 | } |
162 | |
88 | |
163 | int main(int argc, const char **argv) |
89 | int main(int argc, const char **argv) |
164 | { |
90 | { |
| |
91 | struct cacheitem item; |
| |
92 | |
165 | cgit_read_config("/etc/cgitrc", cgit_global_config_cb); |
93 | cgit_read_config("/etc/cgitrc", cgit_global_config_cb); |
166 | cgit_querystring = xstrdup(getenv("QUERY_STRING")); |
94 | cgit_querystring = xstrdup(getenv("QUERY_STRING")); |
167 | cgit_parse_query(cgit_querystring, cgit_querystring_cb); |
95 | cgit_parse_query(cgit_querystring, cgit_querystring_cb); |
168 | cgit_refresh_cache(&cacheitem); |
96 | |
169 | cgit_print_cache(&cacheitem); |
97 | cgit_check_cache(&item); |
| |
98 | cgit_print_cache(&item); |
170 | return 0; |
99 | return 0; |
171 | } |
100 | } |
|