blob: 6d1941e2f38a58c60160520d760ceb2431b1542b [file] [log] [blame]
Jason A. Donenfeld35165022010-07-29 17:52:29 +02001/* scan-tree.c
2 *
3 * Copyright (C) 2008-2009 Lars Hjemli
Jason A. Donenfeld7f08e032012-07-12 20:00:40 +02004 * Copyright (C) 2010, 2012 Jason A. Donenfeld <Jason@zx2c4.com>
Jason A. Donenfeld35165022010-07-29 17:52:29 +02005 *
6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text)
8 */
9
Lars Hjemli93397a72008-09-15 00:07:12 +020010#include "cgit.h"
Lars Hjemli74061ed2009-08-24 00:04:58 +020011#include "configfile.h"
Lars Hjemli93397a72008-09-15 00:07:12 +020012#include "html.h"
13
14#define MAX_PATH 4096
15
16/* return 1 if path contains a objects/ directory and a HEAD file */
17static int is_git_dir(const char *path)
18{
19 struct stat st;
20 static char buf[MAX_PATH];
21
22 if (snprintf(buf, MAX_PATH, "%s/objects", path) >= MAX_PATH) {
23 fprintf(stderr, "Insanely long path: %s\n", path);
24 return 0;
25 }
26 if (stat(buf, &st)) {
27 if (errno != ENOENT)
28 fprintf(stderr, "Error checking path %s: %s (%d)\n",
29 path, strerror(errno), errno);
30 return 0;
31 }
32 if (!S_ISDIR(st.st_mode))
33 return 0;
34
35 sprintf(buf, "%s/HEAD", path);
36 if (stat(buf, &st)) {
37 if (errno != ENOENT)
38 fprintf(stderr, "Error checking path %s: %s (%d)\n",
39 path, strerror(errno), errno);
40 return 0;
41 }
42 if (!S_ISREG(st.st_mode))
43 return 0;
44
45 return 1;
46}
47
Lars Hjemli74061ed2009-08-24 00:04:58 +020048struct cgit_repo *repo;
49repo_config_fn config_fn;
Jason A. Donenfeld119397b2010-07-29 20:38:01 +020050char *owner;
Jason A. Donenfeldb56be4b2012-07-11 04:24:01 +020051char *desc;
Jason A. Donenfeldfc9181f2012-07-11 05:32:45 +020052char *section;
Lars Hjemli74061ed2009-08-24 00:04:58 +020053
54static void repo_config(const char *name, const char *value)
Lars Hjemli93397a72008-09-15 00:07:12 +020055{
Lars Hjemli74061ed2009-08-24 00:04:58 +020056 config_fn(repo, name, value);
57}
58
Jason A. Donenfeldb56be4b2012-07-11 04:24:01 +020059static int gitweb_config(const char *key, const char *value, void *cb)
Jason A. Donenfeld119397b2010-07-29 20:38:01 +020060{
Jason A. Donenfeldb56be4b2012-07-11 04:24:01 +020061 if (ctx.cfg.enable_gitweb_owner && !strcmp(key, "gitweb.owner"))
Jason A. Donenfeld119397b2010-07-29 20:38:01 +020062 owner = xstrdup(value);
Jason A. Donenfeldb56be4b2012-07-11 04:24:01 +020063 else if (ctx.cfg.enable_gitweb_desc && !strcmp(key, "gitweb.description"))
64 desc = xstrdup(value);
Jason A. Donenfeldfc9181f2012-07-11 05:32:45 +020065 else if (ctx.cfg.enable_gitweb_section && !strcmp(key, "gitweb.category"))
66 section = xstrdup(value);
Jason A. Donenfeld119397b2010-07-29 20:38:01 +020067 return 0;
68}
69
Jason A. Donenfeldb56be4b2012-07-11 04:24:01 +020070
71
Lars Hjemli797110e2010-08-21 15:44:09 +020072static char *xstrrchr(char *s, char *from, int c)
73{
74 while (from >= s && *from != c)
75 from--;
76 return from < s ? NULL : from;
77}
78
Lars Hjemli74061ed2009-08-24 00:04:58 +020079static void add_repo(const char *base, const char *path, repo_config_fn fn)
80{
Lars Hjemli93397a72008-09-15 00:07:12 +020081 struct stat st;
82 struct passwd *pwd;
Lars Hjemli797110e2010-08-21 15:44:09 +020083 char *rel, *p, *slash;
84 int n;
Lars Hjemlie16f1782009-08-18 17:17:41 +020085 size_t size;
Lars Hjemli93397a72008-09-15 00:07:12 +020086
87 if (stat(path, &st)) {
88 fprintf(stderr, "Error accessing %s: %s (%d)\n",
89 path, strerror(errno), errno);
90 return;
91 }
Felix Hanleye0c6f232010-11-08 19:41:13 +010092
93 if (ctx.cfg.strict_export && stat(fmt("%s/%s", path, ctx.cfg.strict_export), &st))
94 return;
95
Lars Hjemli31ba37c2010-02-28 18:40:02 +010096 if (!stat(fmt("%s/noweb", path), &st))
97 return;
Jason A. Donenfeld119397b2010-07-29 20:38:01 +020098
99 owner = NULL;
Jason A. Donenfeldb56be4b2012-07-11 04:24:01 +0200100 desc = NULL;
Jason A. Donenfeldfc9181f2012-07-11 05:32:45 +0200101 section = NULL;
Jason A. Donenfeldb56be4b2012-07-11 04:24:01 +0200102 git_config_from_file(gitweb_config, fmt("%s/config", path), NULL);
103
Lars Hjemli93397a72008-09-15 00:07:12 +0200104 if (base == path)
Lars Hjemli797110e2010-08-21 15:44:09 +0200105 rel = xstrdup(fmt("%s", path));
Lars Hjemli93397a72008-09-15 00:07:12 +0200106 else
Lars Hjemli797110e2010-08-21 15:44:09 +0200107 rel = xstrdup(fmt("%s", path + strlen(base) + 1));
Lars Hjemli93397a72008-09-15 00:07:12 +0200108
Lars Hjemli797110e2010-08-21 15:44:09 +0200109 if (!strcmp(rel + strlen(rel) - 5, "/.git"))
110 rel[strlen(rel) - 5] = '\0';
Lars Hjemli93397a72008-09-15 00:07:12 +0200111
Lars Hjemli797110e2010-08-21 15:44:09 +0200112 repo = cgit_add_repo(rel);
Jason A. Donenfeld2e4a9412010-07-29 19:47:50 +0200113 if (ctx.cfg.remove_suffix)
114 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
115 *p = '\0';
Lars Hjemli93397a72008-09-15 00:07:12 +0200116 repo->name = repo->url;
117 repo->path = xstrdup(path);
Jason A. Donenfeld119397b2010-07-29 20:38:01 +0200118 while (!owner) {
119 if ((pwd = getpwuid(st.st_uid)) == NULL) {
120 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
121 path, strerror(errno), errno);
122 break;
123 }
124 if (pwd->pw_gecos)
125 if ((p = strchr(pwd->pw_gecos, ',')))
126 *p = '\0';
127 owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
128 }
129 repo->owner = owner;
Lars Hjemli93397a72008-09-15 00:07:12 +0200130
Jason A. Donenfeldb56be4b2012-07-11 04:24:01 +0200131 if (desc)
132 repo->desc = desc;
133 else {
134 p = fmt("%s/description", path);
135 if (!stat(p, &st))
136 readfile(p, &repo->desc, &size);
137 }
Lars Hjemli93397a72008-09-15 00:07:12 +0200138
Lars Hjemli515edb02010-08-21 15:08:01 +0200139 if (!repo->readme) {
140 p = fmt("%s/README.html", path);
141 if (!stat(p, &st))
142 repo->readme = "README.html";
143 }
Jason A. Donenfeldfc9181f2012-07-11 05:32:45 +0200144 if (section)
145 repo->section = section;
Lars Hjemli797110e2010-08-21 15:44:09 +0200146 if (ctx.cfg.section_from_path) {
147 n = ctx.cfg.section_from_path;
148 if (n > 0) {
149 slash = rel;
150 while (slash && n && (slash = strchr(slash, '/')))
151 n--;
152 } else {
153 slash = rel + strlen(rel);
154 while (slash && n && (slash = xstrrchr(rel, slash, '/')))
155 n++;
156 }
157 if (slash && !n) {
158 *slash = '\0';
159 repo->section = xstrdup(rel);
160 *slash = '/';
161 if (!prefixcmp(repo->name, repo->section)) {
162 repo->name += strlen(repo->section);
163 if (*repo->name == '/')
164 repo->name++;
165 }
166 }
167 }
Lars Hjemli74061ed2009-08-24 00:04:58 +0200168
169 p = fmt("%s/cgitrc", path);
170 if (!stat(p, &st)) {
171 config_fn = fn;
172 parse_configfile(xstrdup(p), &repo_config);
173 }
Jamie Couture2a8f5532011-06-03 19:21:01 -0400174
175 free(rel);
Lars Hjemli93397a72008-09-15 00:07:12 +0200176}
177
Lars Hjemli74061ed2009-08-24 00:04:58 +0200178static void scan_path(const char *base, const char *path, repo_config_fn fn)
Lars Hjemli93397a72008-09-15 00:07:12 +0200179{
Johan Herland682adbc2010-11-15 20:40:43 +0100180 DIR *dir = opendir(path);
Lars Hjemli93397a72008-09-15 00:07:12 +0200181 struct dirent *ent;
182 char *buf;
183 struct stat st;
184
Lars Hjemli93397a72008-09-15 00:07:12 +0200185 if (!dir) {
186 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
187 path, strerror(errno), errno);
188 return;
189 }
Johan Herland682adbc2010-11-15 20:40:43 +0100190 if (is_git_dir(path)) {
191 add_repo(base, path, fn);
192 goto end;
193 }
194 if (is_git_dir(fmt("%s/.git", path))) {
195 add_repo(base, fmt("%s/.git", path), fn);
196 goto end;
197 }
Lars Hjemli93397a72008-09-15 00:07:12 +0200198 while((ent = readdir(dir)) != NULL) {
199 if (ent->d_name[0] == '.') {
200 if (ent->d_name[1] == '\0')
201 continue;
202 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
203 continue;
Johan Herlanddf522792010-11-15 20:41:00 +0100204 if (!ctx.cfg.scan_hidden_path)
205 continue;
Lars Hjemli93397a72008-09-15 00:07:12 +0200206 }
207 buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
208 if (!buf) {
209 fprintf(stderr, "Alloc error on %s: %s (%d)\n",
210 path, strerror(errno), errno);
211 exit(1);
212 }
213 sprintf(buf, "%s/%s", path, ent->d_name);
214 if (stat(buf, &st)) {
215 fprintf(stderr, "Error checking path %s: %s (%d)\n",
216 buf, strerror(errno), errno);
217 free(buf);
218 continue;
219 }
220 if (S_ISDIR(st.st_mode))
Lars Hjemli74061ed2009-08-24 00:04:58 +0200221 scan_path(base, buf, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200222 free(buf);
223 }
Johan Herland682adbc2010-11-15 20:40:43 +0100224end:
Lars Hjemli93397a72008-09-15 00:07:12 +0200225 closedir(dir);
226}
227
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200228#define lastc(s) s[strlen(s) - 1]
229
230void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
231{
232 char line[MAX_PATH * 2], *z;
233 FILE *projects;
234 int err;
235
236 projects = fopen(projectsfile, "r");
237 if (!projects) {
238 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
239 projectsfile, strerror(errno), errno);
Stefan Gehnf15c5832011-03-26 09:51:39 +0100240 return;
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200241 }
242 while (fgets(line, sizeof(line), projects) != NULL) {
243 for (z = &lastc(line);
244 strlen(line) && strchr("\n\r", *z);
245 z = &lastc(line))
246 *z = '\0';
247 if (strlen(line))
248 scan_path(path, fmt("%s/%s", path, line), fn);
249 }
250 if ((err = ferror(projects))) {
251 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
252 projectsfile, strerror(err), err);
253 }
254 fclose(projects);
255}
256
Lars Hjemli74061ed2009-08-24 00:04:58 +0200257void scan_tree(const char *path, repo_config_fn fn)
Lars Hjemli93397a72008-09-15 00:07:12 +0200258{
Lars Hjemli74061ed2009-08-24 00:04:58 +0200259 scan_path(path, path, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200260}