all repos — cgit @ 184c5655b2e350dbd0dd8be75d3f370f22aa6dee

a hyperfast web frontend for git written in c

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;
 51char *desc;
 52char *section;
 53
 54static void repo_config(const char *name, const char *value)
 55{
 56	config_fn(repo, name, value);
 57}
 58
 59static int gitweb_config(const char *key, const char *value, void *cb)
 60{
 61	if (ctx.cfg.enable_gitweb_owner && !strcmp(key, "gitweb.owner"))
 62		owner = xstrdup(value);
 63	else if (ctx.cfg.enable_gitweb_desc && !strcmp(key, "gitweb.description"))
 64		desc = xstrdup(value);
 65	else if (ctx.cfg.enable_gitweb_section && !strcmp(key, "gitweb.category"))
 66		section = xstrdup(value);
 67	return 0;
 68}
 69
 70
 71
 72static 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
 79static void add_repo(const char *base, const char *path, repo_config_fn fn)
 80{
 81	struct stat st;
 82	struct passwd *pwd;
 83	char *rel, *p, *slash;
 84	int n;
 85	size_t size;
 86
 87	if (stat(path, &st)) {
 88		fprintf(stderr, "Error accessing %s: %s (%d)\n",
 89			path, strerror(errno), errno);
 90		return;
 91	}
 92
 93	if (ctx.cfg.strict_export && stat(fmt("%s/%s", path, ctx.cfg.strict_export), &st))
 94		return;
 95
 96	if (!stat(fmt("%s/noweb", path), &st))
 97		return;
 98
 99	owner = NULL;
100	desc = NULL;
101	section = NULL;
102	git_config_from_file(gitweb_config, fmt("%s/config", path), NULL);
103	
104	if (base == path)
105		rel = xstrdup(fmt("%s", path));
106	else
107		rel = xstrdup(fmt("%s", path + strlen(base) + 1));
108
109	if (!strcmp(rel + strlen(rel) - 5, "/.git"))
110		rel[strlen(rel) - 5] = '\0';
111
112	repo = cgit_add_repo(rel);
113	if (ctx.cfg.remove_suffix)
114		if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
115			*p = '\0';
116	repo->name = repo->url;
117	repo->path = xstrdup(path);
118	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;
130
131	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	}
138
139	if (!repo->readme) {
140		p = fmt("%s/README.html", path);
141		if (!stat(p, &st))
142			repo->readme = "README.html";
143	}
144	if (section)
145		repo->section = section;
146	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	}
168
169	p = fmt("%s/cgitrc", path);
170	if (!stat(p, &st)) {
171		config_fn = fn;
172		parse_configfile(xstrdup(p), &repo_config);
173	}
174
175	free(rel);
176}
177
178static void scan_path(const char *base, const char *path, repo_config_fn fn)
179{
180	DIR *dir = opendir(path);
181	struct dirent *ent;
182	char *buf;
183	struct stat st;
184
185	if (!dir) {
186		fprintf(stderr, "Error opening directory %s: %s (%d)\n",
187			path, strerror(errno), errno);
188		return;
189	}
190	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	}
198	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;
204			if (!ctx.cfg.scan_hidden_path)
205				continue;
206		}
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))
221			scan_path(base, buf, fn);
222		free(buf);
223	}
224end:
225	closedir(dir);
226}
227
228#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);
240		return;
241	}
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
257void scan_tree(const char *path, repo_config_fn fn)
258{
259	scan_path(path, path, fn);
260}