all repos — cgit @ 63816ec59748c622623f20e653a7bed87f4f05a4

a hyperfast web frontend for git written in c

scan-tree.c (view raw)

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