summaryrefslogtreecommitdiffabout
path: root/scan-tree.c
Unidiff
Diffstat (limited to 'scan-tree.c') (more/less context) (ignore whitespace changes)
-rw-r--r--scan-tree.c4
1 files changed, 4 insertions, 0 deletions
diff --git a/scan-tree.c b/scan-tree.c
index b5b50f3..a0e09ce 100644
--- a/scan-tree.c
+++ b/scan-tree.c
@@ -1,235 +1,239 @@
1/* scan-tree.c 1/* scan-tree.c
2 * 2 *
3 * Copyright (C) 2008-2009 Lars Hjemli 3 * Copyright (C) 2008-2009 Lars Hjemli
4 * Copyright (C) 2010 Jason A. Donenfeld <Jason@zx2c4.com> 4 * Copyright (C) 2010 Jason A. Donenfeld <Jason@zx2c4.com>
5 * 5 *
6 * Licensed under GNU General Public License v2 6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text) 7 * (see COPYING for full license text)
8 */ 8 */
9 9
10#include "cgit.h" 10#include "cgit.h"
11#include "configfile.h" 11#include "configfile.h"
12#include "html.h" 12#include "html.h"
13 13
14#define MAX_PATH 4096 14#define MAX_PATH 4096
15 15
16/* return 1 if path contains a objects/ directory and a HEAD file */ 16/* return 1 if path contains a objects/ directory and a HEAD file */
17static int is_git_dir(const char *path) 17static int is_git_dir(const char *path)
18{ 18{
19 struct stat st; 19 struct stat st;
20 static char buf[MAX_PATH]; 20 static char buf[MAX_PATH];
21 21
22 if (snprintf(buf, MAX_PATH, "%s/objects", path) >= MAX_PATH) { 22 if (snprintf(buf, MAX_PATH, "%s/objects", path) >= MAX_PATH) {
23 fprintf(stderr, "Insanely long path: %s\n", path); 23 fprintf(stderr, "Insanely long path: %s\n", path);
24 return 0; 24 return 0;
25 } 25 }
26 if (stat(buf, &st)) { 26 if (stat(buf, &st)) {
27 if (errno != ENOENT) 27 if (errno != ENOENT)
28 fprintf(stderr, "Error checking path %s: %s (%d)\n", 28 fprintf(stderr, "Error checking path %s: %s (%d)\n",
29 path, strerror(errno), errno); 29 path, strerror(errno), errno);
30 return 0; 30 return 0;
31 } 31 }
32 if (!S_ISDIR(st.st_mode)) 32 if (!S_ISDIR(st.st_mode))
33 return 0; 33 return 0;
34 34
35 sprintf(buf, "%s/HEAD", path); 35 sprintf(buf, "%s/HEAD", path);
36 if (stat(buf, &st)) { 36 if (stat(buf, &st)) {
37 if (errno != ENOENT) 37 if (errno != ENOENT)
38 fprintf(stderr, "Error checking path %s: %s (%d)\n", 38 fprintf(stderr, "Error checking path %s: %s (%d)\n",
39 path, strerror(errno), errno); 39 path, strerror(errno), errno);
40 return 0; 40 return 0;
41 } 41 }
42 if (!S_ISREG(st.st_mode)) 42 if (!S_ISREG(st.st_mode))
43 return 0; 43 return 0;
44 44
45 return 1; 45 return 1;
46} 46}
47 47
48struct cgit_repo *repo; 48struct cgit_repo *repo;
49repo_config_fn config_fn; 49repo_config_fn config_fn;
50char *owner; 50char *owner;
51 51
52static void repo_config(const char *name, const char *value) 52static void repo_config(const char *name, const char *value)
53{ 53{
54 config_fn(repo, name, value); 54 config_fn(repo, name, value);
55} 55}
56 56
57static int git_owner_config(const char *key, const char *value, void *cb) 57static int git_owner_config(const char *key, const char *value, void *cb)
58{ 58{
59 if (!strcmp(key, "gitweb.owner")) 59 if (!strcmp(key, "gitweb.owner"))
60 owner = xstrdup(value); 60 owner = xstrdup(value);
61 return 0; 61 return 0;
62} 62}
63 63
64static char *xstrrchr(char *s, char *from, int c) 64static char *xstrrchr(char *s, char *from, int c)
65{ 65{
66 while (from >= s && *from != c) 66 while (from >= s && *from != c)
67 from--; 67 from--;
68 return from < s ? NULL : from; 68 return from < s ? NULL : from;
69} 69}
70 70
71static void add_repo(const char *base, const char *path, repo_config_fn fn) 71static void add_repo(const char *base, const char *path, repo_config_fn fn)
72{ 72{
73 struct stat st; 73 struct stat st;
74 struct passwd *pwd; 74 struct passwd *pwd;
75 char *rel, *p, *slash; 75 char *rel, *p, *slash;
76 int n; 76 int n;
77 size_t size; 77 size_t size;
78 78
79 if (stat(path, &st)) { 79 if (stat(path, &st)) {
80 fprintf(stderr, "Error accessing %s: %s (%d)\n", 80 fprintf(stderr, "Error accessing %s: %s (%d)\n",
81 path, strerror(errno), errno); 81 path, strerror(errno), errno);
82 return; 82 return;
83 } 83 }
84
85 if (ctx.cfg.strict_export && stat(fmt("%s/%s", path, ctx.cfg.strict_export), &st))
86 return;
87
84 if (!stat(fmt("%s/noweb", path), &st)) 88 if (!stat(fmt("%s/noweb", path), &st))
85 return; 89 return;
86 90
87 owner = NULL; 91 owner = NULL;
88 if (ctx.cfg.enable_gitweb_owner) 92 if (ctx.cfg.enable_gitweb_owner)
89 git_config_from_file(git_owner_config, fmt("%s/config", path), NULL); 93 git_config_from_file(git_owner_config, fmt("%s/config", path), NULL);
90 if (base == path) 94 if (base == path)
91 rel = xstrdup(fmt("%s", path)); 95 rel = xstrdup(fmt("%s", path));
92 else 96 else
93 rel = xstrdup(fmt("%s", path + strlen(base) + 1)); 97 rel = xstrdup(fmt("%s", path + strlen(base) + 1));
94 98
95 if (!strcmp(rel + strlen(rel) - 5, "/.git")) 99 if (!strcmp(rel + strlen(rel) - 5, "/.git"))
96 rel[strlen(rel) - 5] = '\0'; 100 rel[strlen(rel) - 5] = '\0';
97 101
98 repo = cgit_add_repo(rel); 102 repo = cgit_add_repo(rel);
99 if (ctx.cfg.remove_suffix) 103 if (ctx.cfg.remove_suffix)
100 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git")) 104 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
101 *p = '\0'; 105 *p = '\0';
102 repo->name = repo->url; 106 repo->name = repo->url;
103 repo->path = xstrdup(path); 107 repo->path = xstrdup(path);
104 while (!owner) { 108 while (!owner) {
105 if ((pwd = getpwuid(st.st_uid)) == NULL) { 109 if ((pwd = getpwuid(st.st_uid)) == NULL) {
106 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n", 110 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
107 path, strerror(errno), errno); 111 path, strerror(errno), errno);
108 break; 112 break;
109 } 113 }
110 if (pwd->pw_gecos) 114 if (pwd->pw_gecos)
111 if ((p = strchr(pwd->pw_gecos, ','))) 115 if ((p = strchr(pwd->pw_gecos, ',')))
112 *p = '\0'; 116 *p = '\0';
113 owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name); 117 owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
114 } 118 }
115 repo->owner = owner; 119 repo->owner = owner;
116 120
117 p = fmt("%s/description", path); 121 p = fmt("%s/description", path);
118 if (!stat(p, &st)) 122 if (!stat(p, &st))
119 readfile(p, &repo->desc, &size); 123 readfile(p, &repo->desc, &size);
120 124
121 if (!repo->readme) { 125 if (!repo->readme) {
122 p = fmt("%s/README.html", path); 126 p = fmt("%s/README.html", path);
123 if (!stat(p, &st)) 127 if (!stat(p, &st))
124 repo->readme = "README.html"; 128 repo->readme = "README.html";
125 } 129 }
126 if (ctx.cfg.section_from_path) { 130 if (ctx.cfg.section_from_path) {
127 n = ctx.cfg.section_from_path; 131 n = ctx.cfg.section_from_path;
128 if (n > 0) { 132 if (n > 0) {
129 slash = rel; 133 slash = rel;
130 while (slash && n && (slash = strchr(slash, '/'))) 134 while (slash && n && (slash = strchr(slash, '/')))
131 n--; 135 n--;
132 } else { 136 } else {
133 slash = rel + strlen(rel); 137 slash = rel + strlen(rel);
134 while (slash && n && (slash = xstrrchr(rel, slash, '/'))) 138 while (slash && n && (slash = xstrrchr(rel, slash, '/')))
135 n++; 139 n++;
136 } 140 }
137 if (slash && !n) { 141 if (slash && !n) {
138 *slash = '\0'; 142 *slash = '\0';
139 repo->section = xstrdup(rel); 143 repo->section = xstrdup(rel);
140 *slash = '/'; 144 *slash = '/';
141 if (!prefixcmp(repo->name, repo->section)) { 145 if (!prefixcmp(repo->name, repo->section)) {
142 repo->name += strlen(repo->section); 146 repo->name += strlen(repo->section);
143 if (*repo->name == '/') 147 if (*repo->name == '/')
144 repo->name++; 148 repo->name++;
145 } 149 }
146 } 150 }
147 } 151 }
148 152
149 p = fmt("%s/cgitrc", path); 153 p = fmt("%s/cgitrc", path);
150 if (!stat(p, &st)) { 154 if (!stat(p, &st)) {
151 config_fn = fn; 155 config_fn = fn;
152 parse_configfile(xstrdup(p), &repo_config); 156 parse_configfile(xstrdup(p), &repo_config);
153 } 157 }
154} 158}
155 159
156static void scan_path(const char *base, const char *path, repo_config_fn fn) 160static void scan_path(const char *base, const char *path, repo_config_fn fn)
157{ 161{
158 DIR *dir; 162 DIR *dir;
159 struct dirent *ent; 163 struct dirent *ent;
160 char *buf; 164 char *buf;
161 struct stat st; 165 struct stat st;
162 166
163 if (is_git_dir(path)) { 167 if (is_git_dir(path)) {
164 add_repo(base, path, fn); 168 add_repo(base, path, fn);
165 return; 169 return;
166 } 170 }
167 if (is_git_dir(fmt("%s/.git", path))) { 171 if (is_git_dir(fmt("%s/.git", path))) {
168 add_repo(base, fmt("%s/.git", path), fn); 172 add_repo(base, fmt("%s/.git", path), fn);
169 return; 173 return;
170 } 174 }
171 dir = opendir(path); 175 dir = opendir(path);
172 if (!dir) { 176 if (!dir) {
173 fprintf(stderr, "Error opening directory %s: %s (%d)\n", 177 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
174 path, strerror(errno), errno); 178 path, strerror(errno), errno);
175 return; 179 return;
176 } 180 }
177 while((ent = readdir(dir)) != NULL) { 181 while((ent = readdir(dir)) != NULL) {
178 if (ent->d_name[0] == '.') { 182 if (ent->d_name[0] == '.') {
179 if (ent->d_name[1] == '\0') 183 if (ent->d_name[1] == '\0')
180 continue; 184 continue;
181 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0') 185 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
182 continue; 186 continue;
183 } 187 }
184 buf = malloc(strlen(path) + strlen(ent->d_name) + 2); 188 buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
185 if (!buf) { 189 if (!buf) {
186 fprintf(stderr, "Alloc error on %s: %s (%d)\n", 190 fprintf(stderr, "Alloc error on %s: %s (%d)\n",
187 path, strerror(errno), errno); 191 path, strerror(errno), errno);
188 exit(1); 192 exit(1);
189 } 193 }
190 sprintf(buf, "%s/%s", path, ent->d_name); 194 sprintf(buf, "%s/%s", path, ent->d_name);
191 if (stat(buf, &st)) { 195 if (stat(buf, &st)) {
192 fprintf(stderr, "Error checking path %s: %s (%d)\n", 196 fprintf(stderr, "Error checking path %s: %s (%d)\n",
193 buf, strerror(errno), errno); 197 buf, strerror(errno), errno);
194 free(buf); 198 free(buf);
195 continue; 199 continue;
196 } 200 }
197 if (S_ISDIR(st.st_mode)) 201 if (S_ISDIR(st.st_mode))
198 scan_path(base, buf, fn); 202 scan_path(base, buf, fn);
199 free(buf); 203 free(buf);
200 } 204 }
201 closedir(dir); 205 closedir(dir);
202} 206}
203 207
204#define lastc(s) s[strlen(s) - 1] 208#define lastc(s) s[strlen(s) - 1]
205 209
206void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn) 210void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
207{ 211{
208 char line[MAX_PATH * 2], *z; 212 char line[MAX_PATH * 2], *z;
209 FILE *projects; 213 FILE *projects;
210 int err; 214 int err;
211 215
212 projects = fopen(projectsfile, "r"); 216 projects = fopen(projectsfile, "r");
213 if (!projects) { 217 if (!projects) {
214 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n", 218 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
215 projectsfile, strerror(errno), errno); 219 projectsfile, strerror(errno), errno);
216 } 220 }
217 while (fgets(line, sizeof(line), projects) != NULL) { 221 while (fgets(line, sizeof(line), projects) != NULL) {
218 for (z = &lastc(line); 222 for (z = &lastc(line);
219 strlen(line) && strchr("\n\r", *z); 223 strlen(line) && strchr("\n\r", *z);
220 z = &lastc(line)) 224 z = &lastc(line))
221 *z = '\0'; 225 *z = '\0';
222 if (strlen(line)) 226 if (strlen(line))
223 scan_path(path, fmt("%s/%s", path, line), fn); 227 scan_path(path, fmt("%s/%s", path, line), fn);
224 } 228 }
225 if ((err = ferror(projects))) { 229 if ((err = ferror(projects))) {
226 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n", 230 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
227 projectsfile, strerror(err), err); 231 projectsfile, strerror(err), err);
228 } 232 }
229 fclose(projects); 233 fclose(projects);
230} 234}
231 235
232void scan_tree(const char *path, repo_config_fn fn) 236void scan_tree(const char *path, repo_config_fn fn)
233{ 237{
234 scan_path(path, path, fn); 238 scan_path(path, path, fn);
235} 239}