blob: 9bf9b38b6af6dc891db10a686c3bf097bdcf658b [file] [log] [blame]
Jason A. Donenfeld35165022010-07-29 17:52:29 +02001/* 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
Lars Hjemli93397a72008-09-15 00:07:12 +020010#include "cgit.h"
Lars Hjemli74061ed2009-08-24 00:04:58 +020011#include "configfile.h"
Lars Hjemli93397a72008-09-15 00:07:12 +020012#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
Lars Hjemli74061ed2009-08-24 00:04:58 +020048struct cgit_repo *repo;
49repo_config_fn config_fn;
50
51static void repo_config(const char *name, const char *value)
Lars Hjemli93397a72008-09-15 00:07:12 +020052{
Lars Hjemli74061ed2009-08-24 00:04:58 +020053 config_fn(repo, name, value);
54}
55
56static void add_repo(const char *base, const char *path, repo_config_fn fn)
57{
Lars Hjemli93397a72008-09-15 00:07:12 +020058 struct stat st;
59 struct passwd *pwd;
60 char *p;
Lars Hjemlie16f1782009-08-18 17:17:41 +020061 size_t size;
Lars Hjemli93397a72008-09-15 00:07:12 +020062
63 if (stat(path, &st)) {
64 fprintf(stderr, "Error accessing %s: %s (%d)\n",
65 path, strerror(errno), errno);
66 return;
67 }
Lars Hjemli31ba37c2010-02-28 18:40:02 +010068 if (!stat(fmt("%s/noweb", path), &st))
69 return;
Lars Hjemli93397a72008-09-15 00:07:12 +020070 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);
Stefan Naewe6445a3a2009-08-20 08:24:51 +020086 p = (pwd && pwd->pw_gecos) ? strchr(pwd->pw_gecos, ',') : NULL;
87 if (p)
88 *p = '\0';
Lars Hjemli93397a72008-09-15 00:07:12 +020089 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))
Lars Hjemlie16f1782009-08-18 17:17:41 +020093 readfile(p, &repo->desc, &size);
Lars Hjemli93397a72008-09-15 00:07:12 +020094
95 p = fmt("%s/README.html", path);
96 if (!stat(p, &st))
97 repo->readme = "README.html";
Lars Hjemli74061ed2009-08-24 00:04:58 +020098
99 p = fmt("%s/cgitrc", path);
100 if (!stat(p, &st)) {
101 config_fn = fn;
102 parse_configfile(xstrdup(p), &repo_config);
103 }
Lars Hjemli93397a72008-09-15 00:07:12 +0200104}
105
Lars Hjemli74061ed2009-08-24 00:04:58 +0200106static void scan_path(const char *base, const char *path, repo_config_fn fn)
Lars Hjemli93397a72008-09-15 00:07:12 +0200107{
108 DIR *dir;
109 struct dirent *ent;
110 char *buf;
111 struct stat st;
112
113 if (is_git_dir(path)) {
Lars Hjemli74061ed2009-08-24 00:04:58 +0200114 add_repo(base, path, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200115 return;
116 }
Lars Hjemlifb9bf552009-08-11 10:08:12 +0200117 if (is_git_dir(fmt("%s/.git", path))) {
Lars Hjemli74061ed2009-08-24 00:04:58 +0200118 add_repo(base, fmt("%s/.git", path), fn);
Lars Hjemlifb9bf552009-08-11 10:08:12 +0200119 return;
120 }
Lars Hjemli93397a72008-09-15 00:07:12 +0200121 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))
Lars Hjemli74061ed2009-08-24 00:04:58 +0200148 scan_path(base, buf, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200149 free(buf);
150 }
151 closedir(dir);
152}
153
Jason A. Donenfeld35165022010-07-29 17:52:29 +0200154#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
Lars Hjemli74061ed2009-08-24 00:04:58 +0200182void scan_tree(const char *path, repo_config_fn fn)
Lars Hjemli93397a72008-09-15 00:07:12 +0200183{
Lars Hjemli74061ed2009-08-24 00:04:58 +0200184 scan_path(path, path, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200185}