all repos — cgit @ 119397b175874bd606952e93b7249ae4ffb9afbe

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;
 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 void add_repo(const char *base, const char *path, repo_config_fn fn)
 65{
 66	struct stat st;
 67	struct passwd *pwd;
 68	char *p;
 69	size_t size;
 70
 71	if (stat(path, &st)) {
 72		fprintf(stderr, "Error accessing %s: %s (%d)\n",
 73			path, strerror(errno), errno);
 74		return;
 75	}
 76	if (!stat(fmt("%s/noweb", path), &st))
 77		return;
 78
 79	owner = NULL;
 80	if (ctx.cfg.enable_gitweb_owner)
 81		git_config_from_file(git_owner_config, fmt("%s/config", path), NULL);
 82	if (base == path)
 83		p = fmt("%s", path);
 84	else
 85		p = fmt("%s", path + strlen(base) + 1);
 86
 87	if (!strcmp(p + strlen(p) - 5, "/.git"))
 88		p[strlen(p) - 5] = '\0';
 89
 90	repo = cgit_add_repo(xstrdup(p));
 91	if (ctx.cfg.remove_suffix)
 92		if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
 93			*p = '\0';
 94	repo->name = repo->url;
 95	repo->path = xstrdup(path);
 96	while (!owner) {
 97		if ((pwd = getpwuid(st.st_uid)) == NULL) {
 98			fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
 99				path, strerror(errno), errno);
100			break;
101		}
102		if (pwd->pw_gecos)
103			if ((p = strchr(pwd->pw_gecos, ',')))
104				*p = '\0';
105		owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
106	}
107	repo->owner = owner;
108
109	p = fmt("%s/description", path);
110	if (!stat(p, &st))
111		readfile(p, &repo->desc, &size);
112
113	p = fmt("%s/README.html", path);
114	if (!stat(p, &st))
115		repo->readme = "README.html";
116
117	p = fmt("%s/cgitrc", path);
118	if (!stat(p, &st)) {
119		config_fn = fn;
120		parse_configfile(xstrdup(p), &repo_config);
121	}
122}
123
124static void scan_path(const char *base, const char *path, repo_config_fn fn)
125{
126	DIR *dir;
127	struct dirent *ent;
128	char *buf;
129	struct stat st;
130
131	if (is_git_dir(path)) {
132		add_repo(base, path, fn);
133		return;
134	}
135	if (is_git_dir(fmt("%s/.git", path))) {
136		add_repo(base, fmt("%s/.git", path), fn);
137		return;
138	}
139	dir = opendir(path);
140	if (!dir) {
141		fprintf(stderr, "Error opening directory %s: %s (%d)\n",
142			path, strerror(errno), errno);
143		return;
144	}
145	while((ent = readdir(dir)) != NULL) {
146		if (ent->d_name[0] == '.') {
147			if (ent->d_name[1] == '\0')
148				continue;
149			if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
150				continue;
151		}
152		buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
153		if (!buf) {
154			fprintf(stderr, "Alloc error on %s: %s (%d)\n",
155				path, strerror(errno), errno);
156			exit(1);
157		}
158		sprintf(buf, "%s/%s", path, ent->d_name);
159		if (stat(buf, &st)) {
160			fprintf(stderr, "Error checking path %s: %s (%d)\n",
161				buf, strerror(errno), errno);
162			free(buf);
163			continue;
164		}
165		if (S_ISDIR(st.st_mode))
166			scan_path(base, buf, fn);
167		free(buf);
168	}
169	closedir(dir);
170}
171
172#define lastc(s) s[strlen(s) - 1]
173
174void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
175{
176	char line[MAX_PATH * 2], *z;
177	FILE *projects;
178	int err;
179	
180	projects = fopen(projectsfile, "r");
181	if (!projects) {
182		fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
183			projectsfile, strerror(errno), errno);
184	}
185	while (fgets(line, sizeof(line), projects) != NULL) {
186		for (z = &lastc(line);
187		     strlen(line) && strchr("\n\r", *z);
188		     z = &lastc(line))
189			*z = '\0';
190		if (strlen(line))
191			scan_path(path, fmt("%s/%s", path, line), fn);
192	}
193	if ((err = ferror(projects))) {
194		fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
195			projectsfile, strerror(err), err);
196	}
197	fclose(projects);
198}
199
200void scan_tree(const char *path, repo_config_fn fn)
201{
202	scan_path(path, path, fn);
203}