all repos — cgit @ e6c960c7c0f0d2e54b51cc43ef190df3ce52755e

a hyperfast web frontend for git written in c

scan-tree.c (view raw)

  1#include "cgit.h"
  2#include "configfile.h"
  3#include "html.h"
  4
  5#define MAX_PATH 4096
  6
  7/* return 1 if path contains a objects/ directory and a HEAD file */
  8static int is_git_dir(const char *path)
  9{
 10	struct stat st;
 11	static char buf[MAX_PATH];
 12
 13	if (snprintf(buf, MAX_PATH, "%s/objects", path) >= MAX_PATH) {
 14		fprintf(stderr, "Insanely long path: %s\n", path);
 15		return 0;
 16	}
 17	if (stat(buf, &st)) {
 18		if (errno != ENOENT)
 19			fprintf(stderr, "Error checking path %s: %s (%d)\n",
 20				path, strerror(errno), errno);
 21		return 0;
 22	}
 23	if (!S_ISDIR(st.st_mode))
 24		return 0;
 25
 26	sprintf(buf, "%s/HEAD", path);
 27	if (stat(buf, &st)) {
 28		if (errno != ENOENT)
 29			fprintf(stderr, "Error checking path %s: %s (%d)\n",
 30				path, strerror(errno), errno);
 31		return 0;
 32	}
 33	if (!S_ISREG(st.st_mode))
 34		return 0;
 35
 36	return 1;
 37}
 38
 39struct cgit_repo *repo;
 40repo_config_fn config_fn;
 41
 42static void repo_config(const char *name, const char *value)
 43{
 44	config_fn(repo, name, value);
 45}
 46
 47static void add_repo(const char *base, const char *path, repo_config_fn fn)
 48{
 49	struct stat st;
 50	struct passwd *pwd;
 51	char *p;
 52	size_t size;
 53
 54	if (stat(path, &st)) {
 55		fprintf(stderr, "Error accessing %s: %s (%d)\n",
 56			path, strerror(errno), errno);
 57		return;
 58	}
 59	if (!stat(fmt("%s/noweb", path), &st))
 60		return;
 61	if ((pwd = getpwuid(st.st_uid)) == NULL) {
 62		fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
 63			path, strerror(errno), errno);
 64		return;
 65	}
 66	if (base == path)
 67		p = fmt("%s", path);
 68	else
 69		p = fmt("%s", path + strlen(base) + 1);
 70
 71	if (!strcmp(p + strlen(p) - 5, "/.git"))
 72		p[strlen(p) - 5] = '\0';
 73
 74	repo = cgit_add_repo(xstrdup(p));
 75	repo->name = repo->url;
 76	repo->path = xstrdup(path);
 77	p = (pwd && pwd->pw_gecos) ? strchr(pwd->pw_gecos, ',') : NULL;
 78	if (p)
 79		*p = '\0';
 80	repo->owner = (pwd ? xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name) : "");
 81
 82	p = fmt("%s/description", path);
 83	if (!stat(p, &st))
 84		readfile(p, &repo->desc, &size);
 85
 86	p = fmt("%s/README.html", path);
 87	if (!stat(p, &st))
 88		repo->readme = "README.html";
 89
 90	p = fmt("%s/cgitrc", path);
 91	if (!stat(p, &st)) {
 92		config_fn = fn;
 93		parse_configfile(xstrdup(p), &repo_config);
 94	}
 95}
 96
 97static void scan_path(const char *base, const char *path, repo_config_fn fn)
 98{
 99	DIR *dir;
100	struct dirent *ent;
101	char *buf;
102	struct stat st;
103
104	if (is_git_dir(path)) {
105		add_repo(base, path, fn);
106		return;
107	}
108	if (is_git_dir(fmt("%s/.git", path))) {
109		add_repo(base, fmt("%s/.git", path), fn);
110		return;
111	}
112	dir = opendir(path);
113	if (!dir) {
114		fprintf(stderr, "Error opening directory %s: %s (%d)\n",
115			path, strerror(errno), errno);
116		return;
117	}
118	while((ent = readdir(dir)) != NULL) {
119		if (ent->d_name[0] == '.') {
120			if (ent->d_name[1] == '\0')
121				continue;
122			if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
123				continue;
124		}
125		buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
126		if (!buf) {
127			fprintf(stderr, "Alloc error on %s: %s (%d)\n",
128				path, strerror(errno), errno);
129			exit(1);
130		}
131		sprintf(buf, "%s/%s", path, ent->d_name);
132		if (stat(buf, &st)) {
133			fprintf(stderr, "Error checking path %s: %s (%d)\n",
134				buf, strerror(errno), errno);
135			free(buf);
136			continue;
137		}
138		if (S_ISDIR(st.st_mode))
139			scan_path(base, buf, fn);
140		free(buf);
141	}
142	closedir(dir);
143}
144
145void scan_tree(const char *path, repo_config_fn fn)
146{
147	scan_path(path, path, fn);
148}