]> gitweb.ps.run Git - ps-cgit/blob - scan-tree.c
t0107: Skip ZIP tests if unzip(1) isn't available
[ps-cgit] / scan-tree.c
1 /* scan-tree.c
2  * 
3  * Copyright (C) 2008-2009 Lars Hjemli
4  * Copyright (C) 2010, 2012 Jason A. Donenfeld <Jason@zx2c4.com>
5  *
6  * Licensed under GNU General Public License v2
7  *   (see COPYING for full license text)
8  */
9
10 #include "cgit.h"
11 #include "scan-tree.h"
12 #include "configfile.h"
13 #include "html.h"
14
15 /* return 1 if path contains a objects/ directory and a HEAD file */
16 static int is_git_dir(const char *path)
17 {
18         struct stat st;
19         struct strbuf pathbuf = STRBUF_INIT;
20         int result = 0;
21
22         strbuf_addf(&pathbuf, "%s/objects", path);
23         if (stat(pathbuf.buf, &st)) {
24                 if (errno != ENOENT)
25                         fprintf(stderr, "Error checking path %s: %s (%d)\n",
26                                 path, strerror(errno), errno);
27                 goto out;
28         }
29         if (!S_ISDIR(st.st_mode))
30                 goto out;
31
32         strbuf_reset(&pathbuf);
33         strbuf_addf(&pathbuf, "%s/HEAD", path);
34         if (stat(pathbuf.buf, &st)) {
35                 if (errno != ENOENT)
36                         fprintf(stderr, "Error checking path %s: %s (%d)\n",
37                                 path, strerror(errno), errno);
38                 goto out;
39         }
40         if (!S_ISREG(st.st_mode))
41                 goto out;
42
43         result = 1;
44 out:
45         strbuf_release(&pathbuf);
46         return result;
47 }
48
49 struct cgit_repo *repo;
50 repo_config_fn config_fn;
51
52 static void repo_config(const char *name, const char *value)
53 {
54         config_fn(repo, name, value);
55 }
56
57 static int gitconfig_config(const char *key, const char *value, void *cb)
58 {
59         if (!strcmp(key, "gitweb.owner"))
60                 config_fn(repo, "owner", value);
61         else if (!strcmp(key, "gitweb.description"))
62                 config_fn(repo, "desc", value);
63         else if (!strcmp(key, "gitweb.category"))
64                 config_fn(repo, "section", value);
65         else if (!prefixcmp(key, "cgit."))
66                 config_fn(repo, key + 5, value);
67
68         return 0;
69 }
70
71 static char *xstrrchr(char *s, char *from, int c)
72 {
73         while (from >= s && *from != c)
74                 from--;
75         return from < s ? NULL : from;
76 }
77
78 static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn)
79 {
80         struct stat st;
81         struct passwd *pwd;
82         size_t pathlen;
83         struct strbuf rel = STRBUF_INIT;
84         char *p, *slash;
85         int n;
86         size_t size;
87
88         if (stat(path->buf, &st)) {
89                 fprintf(stderr, "Error accessing %s: %s (%d)\n",
90                         path->buf, strerror(errno), errno);
91                 return;
92         }
93
94         strbuf_addch(path, '/');
95         pathlen = path->len;
96
97         if (ctx.cfg.strict_export) {
98                 strbuf_addstr(path, ctx.cfg.strict_export);
99                 if(stat(path->buf, &st))
100                         return;
101                 strbuf_setlen(path, pathlen);
102         }
103
104         strbuf_addstr(path, "noweb");
105         if (!stat(path->buf, &st))
106                 return;
107         strbuf_setlen(path, pathlen);
108
109         if (strncmp(base, path->buf, strlen(base)))
110                 strbuf_addbuf(&rel, path);
111         else
112                 strbuf_addstr(&rel, path->buf + strlen(base) + 1);
113
114         if (!strcmp(rel.buf + rel.len - 5, "/.git"))
115                 strbuf_setlen(&rel, rel.len - 5);
116
117         repo = cgit_add_repo(rel.buf);
118         config_fn = fn;
119         if (ctx.cfg.enable_git_config) {
120                 strbuf_addstr(path, "config");
121                 git_config_from_file(gitconfig_config, path->buf, NULL);
122                 strbuf_setlen(path, pathlen);
123         }
124
125         if (ctx.cfg.remove_suffix)
126                 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
127                         *p = '\0';
128         repo->path = xstrdup(path->buf);
129         while (!repo->owner) {
130                 if ((pwd = getpwuid(st.st_uid)) == NULL) {
131                         fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
132                                 path->buf, strerror(errno), errno);
133                         break;
134                 }
135                 if (pwd->pw_gecos)
136                         if ((p = strchr(pwd->pw_gecos, ',')))
137                                 *p = '\0';
138                 repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
139         }
140
141         if (repo->desc == cgit_default_repo_desc || !repo->desc) {
142                 strbuf_addstr(path, "description");
143                 if (!stat(path->buf, &st))
144                         readfile(path->buf, &repo->desc, &size);
145                 strbuf_setlen(path, pathlen);
146         }
147
148         if (!repo->readme) {
149                 strbuf_addstr(path, "README.html");
150                 if (!stat(path->buf, &st))
151                         repo->readme = "README.html";
152                 strbuf_setlen(path, pathlen);
153         }
154         if (ctx.cfg.section_from_path) {
155                 n  = ctx.cfg.section_from_path;
156                 if (n > 0) {
157                         slash = rel.buf;
158                         while (slash && n && (slash = strchr(slash, '/')))
159                                 n--;
160                 } else {
161                         slash = rel.buf + rel.len;
162                         while (slash && n && (slash = xstrrchr(rel.buf, slash, '/')))
163                                 n++;
164                 }
165                 if (slash && !n) {
166                         *slash = '\0';
167                         repo->section = xstrdup(rel.buf);
168                         *slash = '/';
169                         if (!prefixcmp(repo->name, repo->section)) {
170                                 repo->name += strlen(repo->section);
171                                 if (*repo->name == '/')
172                                         repo->name++;
173                         }
174                 }
175         }
176
177         strbuf_addstr(path, "cgitrc");
178         if (!stat(path->buf, &st))
179                 parse_configfile(xstrdup(path->buf), &repo_config);
180
181         strbuf_release(&rel);
182 }
183
184 static void scan_path(const char *base, const char *path, repo_config_fn fn)
185 {
186         DIR *dir = opendir(path);
187         struct dirent *ent;
188         struct strbuf pathbuf = STRBUF_INIT;
189         size_t pathlen = strlen(path);
190         struct stat st;
191
192         if (!dir) {
193                 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
194                         path, strerror(errno), errno);
195                 return;
196         }
197
198         strbuf_add(&pathbuf, path, strlen(path));
199         if (is_git_dir(pathbuf.buf)) {
200                 add_repo(base, &pathbuf, fn);
201                 goto end;
202         }
203         strbuf_addstr(&pathbuf, "/.git");
204         if (is_git_dir(pathbuf.buf)) {
205                 add_repo(base, &pathbuf, fn);
206                 goto end;
207         }
208         /*
209          * Add one because we don't want to lose the trailing '/' when we
210          * reset the length of pathbuf in the loop below.
211          */
212         pathlen++;
213         while ((ent = readdir(dir)) != NULL) {
214                 if (ent->d_name[0] == '.') {
215                         if (ent->d_name[1] == '\0')
216                                 continue;
217                         if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
218                                 continue;
219                         if (!ctx.cfg.scan_hidden_path)
220                                 continue;
221                 }
222                 strbuf_setlen(&pathbuf, pathlen);
223                 strbuf_addstr(&pathbuf, ent->d_name);
224                 if (stat(pathbuf.buf, &st)) {
225                         fprintf(stderr, "Error checking path %s: %s (%d)\n",
226                                 pathbuf.buf, strerror(errno), errno);
227                         continue;
228                 }
229                 if (S_ISDIR(st.st_mode))
230                         scan_path(base, pathbuf.buf, fn);
231         }
232 end:
233         strbuf_release(&pathbuf);
234         closedir(dir);
235 }
236
237 #define lastc(s) s[strlen(s) - 1]
238
239 void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
240 {
241         struct strbuf line = STRBUF_INIT;
242         FILE *projects;
243         int err;
244
245         projects = fopen(projectsfile, "r");
246         if (!projects) {
247                 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
248                         projectsfile, strerror(errno), errno);
249                 return;
250         }
251         while (strbuf_getline(&line, projects, '\n') != EOF) {
252                 if (!line.len)
253                         continue;
254                 strbuf_insert(&line, 0, "/", 1);
255                 strbuf_insert(&line, 0, path, strlen(path));
256                 scan_path(path, line.buf, fn);
257         }
258         if ((err = ferror(projects))) {
259                 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
260                         projectsfile, strerror(err), err);
261         }
262         fclose(projects);
263         strbuf_release(&line);
264 }
265
266 void scan_tree(const char *path, repo_config_fn fn)
267 {
268         scan_path(path, path, fn);
269 }