all repos — cgit @ 3516502aa0df95ecc241caa30161741f59e4e600

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