scan-tree.c (view raw)
1/* scan-tree.c
2 *
3 * Copyright (C) 2008-2009 Lars Hjemli
4 * Copyright (C) 2010 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 "configfile.h"
12#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
48struct cgit_repo *repo;
49repo_config_fn config_fn;
50char *owner;
51
52static void repo_config(const char *name, const char *value)
53{
54 config_fn(repo, name, value);
55}
56
57static int git_owner_config(const char *key, const char *value, void *cb)
58{
59 if (!strcmp(key, "gitweb.owner"))
60 owner = xstrdup(value);
61 return 0;
62}
63
64static char *xstrrchr(char *s, char *from, int c)
65{
66 while (from >= s && *from != c)
67 from--;
68 return from < s ? NULL : from;
69}
70
71static void add_repo(const char *base, const char *path, repo_config_fn fn)
72{
73 struct stat st;
74 struct passwd *pwd;
75 char *rel, *p, *slash;
76 int n;
77 size_t size;
78
79 if (stat(path, &st)) {
80 fprintf(stderr, "Error accessing %s: %s (%d)\n",
81 path, strerror(errno), errno);
82 return;
83 }
84 if (!stat(fmt("%s/noweb", path), &st))
85 return;
86
87 owner = NULL;
88 if (ctx.cfg.enable_gitweb_owner)
89 git_config_from_file(git_owner_config, fmt("%s/config", path), NULL);
90 if (base == path)
91 rel = xstrdup(fmt("%s", path));
92 else
93 rel = xstrdup(fmt("%s", path + strlen(base) + 1));
94
95 if (!strcmp(rel + strlen(rel) - 5, "/.git"))
96 rel[strlen(rel) - 5] = '\0';
97
98 repo = cgit_add_repo(rel);
99 if (ctx.cfg.remove_suffix)
100 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
101 *p = '\0';
102 repo->name = repo->url;
103 repo->path = xstrdup(path);
104 while (!owner) {
105 if ((pwd = getpwuid(st.st_uid)) == NULL) {
106 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
107 path, strerror(errno), errno);
108 break;
109 }
110 if (pwd->pw_gecos)
111 if ((p = strchr(pwd->pw_gecos, ',')))
112 *p = '\0';
113 owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
114 }
115 repo->owner = owner;
116
117 p = fmt("%s/description", path);
118 if (!stat(p, &st))
119 readfile(p, &repo->desc, &size);
120
121 p = fmt("%s/README.html", path);
122 if (!stat(p, &st))
123 repo->readme = "README.html";
124 if (ctx.cfg.section_from_path) {
125 n = ctx.cfg.section_from_path;
126 if (n > 0) {
127 slash = rel;
128 while (slash && n && (slash = strchr(slash, '/')))
129 n--;
130 } else {
131 slash = rel + strlen(rel);
132 while (slash && n && (slash = xstrrchr(rel, slash, '/')))
133 n++;
134 }
135 if (slash && !n) {
136 *slash = '\0';
137 repo->section = xstrdup(rel);
138 *slash = '/';
139 if (!prefixcmp(repo->name, repo->section)) {
140 repo->name += strlen(repo->section);
141 if (*repo->name == '/')
142 repo->name++;
143 }
144 }
145 }
146
147 p = fmt("%s/cgitrc", path);
148 if (!stat(p, &st)) {
149 config_fn = fn;
150 parse_configfile(xstrdup(p), &repo_config);
151 }
152}
153
154static void scan_path(const char *base, const char *path, repo_config_fn fn)
155{
156 DIR *dir;
157 struct dirent *ent;
158 char *buf;
159 struct stat st;
160
161 if (is_git_dir(path)) {
162 add_repo(base, path, fn);
163 return;
164 }
165 if (is_git_dir(fmt("%s/.git", path))) {
166 add_repo(base, fmt("%s/.git", path), fn);
167 return;
168 }
169 dir = opendir(path);
170 if (!dir) {
171 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
172 path, strerror(errno), errno);
173 return;
174 }
175 while((ent = readdir(dir)) != NULL) {
176 if (ent->d_name[0] == '.') {
177 if (ent->d_name[1] == '\0')
178 continue;
179 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
180 continue;
181 }
182 buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
183 if (!buf) {
184 fprintf(stderr, "Alloc error on %s: %s (%d)\n",
185 path, strerror(errno), errno);
186 exit(1);
187 }
188 sprintf(buf, "%s/%s", path, ent->d_name);
189 if (stat(buf, &st)) {
190 fprintf(stderr, "Error checking path %s: %s (%d)\n",
191 buf, strerror(errno), errno);
192 free(buf);
193 continue;
194 }
195 if (S_ISDIR(st.st_mode))
196 scan_path(base, buf, fn);
197 free(buf);
198 }
199 closedir(dir);
200}
201
202#define lastc(s) s[strlen(s) - 1]
203
204void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
205{
206 char line[MAX_PATH * 2], *z;
207 FILE *projects;
208 int err;
209
210 projects = fopen(projectsfile, "r");
211 if (!projects) {
212 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
213 projectsfile, strerror(errno), errno);
214 }
215 while (fgets(line, sizeof(line), projects) != NULL) {
216 for (z = &lastc(line);
217 strlen(line) && strchr("\n\r", *z);
218 z = &lastc(line))
219 *z = '\0';
220 if (strlen(line))
221 scan_path(path, fmt("%s/%s", path, line), fn);
222 }
223 if ((err = ferror(projects))) {
224 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
225 projectsfile, strerror(err), err);
226 }
227 fclose(projects);
228}
229
230void scan_tree(const char *path, repo_config_fn fn)
231{
232 scan_path(path, path, fn);
233}