summaryrefslogtreecommitdiffabout
path: root/scan-tree.c
Unidiff
Diffstat (limited to 'scan-tree.c') (more/less context) (ignore whitespace changes)
-rw-r--r--scan-tree.c42
1 files changed, 36 insertions, 6 deletions
diff --git a/scan-tree.c b/scan-tree.c
index e987824..6ba9193 100644
--- a/scan-tree.c
+++ b/scan-tree.c
@@ -1,203 +1,233 @@
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)
65{
66 while (from >= s && *from != c)
67 from--;
68 return from < s ? NULL : from;
69}
70
64static 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)
65{ 72{
66 struct stat st; 73 struct stat st;
67 struct passwd *pwd; 74 struct passwd *pwd;
68 char *p; 75 char *rel, *p, *slash;
76 int n;
69 size_t size; 77 size_t size;
70 78
71 if (stat(path, &st)) { 79 if (stat(path, &st)) {
72 fprintf(stderr, "Error accessing %s: %s (%d)\n", 80 fprintf(stderr, "Error accessing %s: %s (%d)\n",
73 path, strerror(errno), errno); 81 path, strerror(errno), errno);
74 return; 82 return;
75 } 83 }
76 if (!stat(fmt("%s/noweb", path), &st)) 84 if (!stat(fmt("%s/noweb", path), &st))
77 return; 85 return;
78 86
79 owner = NULL; 87 owner = NULL;
80 if (ctx.cfg.enable_gitweb_owner) 88 if (ctx.cfg.enable_gitweb_owner)
81 git_config_from_file(git_owner_config, fmt("%s/config", path), NULL); 89 git_config_from_file(git_owner_config, fmt("%s/config", path), NULL);
82 if (base == path) 90 if (base == path)
83 p = fmt("%s", path); 91 rel = xstrdup(fmt("%s", path));
84 else 92 else
85 p = fmt("%s", path + strlen(base) + 1); 93 rel = xstrdup(fmt("%s", path + strlen(base) + 1));
86 94
87 if (!strcmp(p + strlen(p) - 5, "/.git")) 95 if (!strcmp(rel + strlen(rel) - 5, "/.git"))
88 p[strlen(p) - 5] = '\0'; 96 rel[strlen(rel) - 5] = '\0';
89 97
90 repo = cgit_add_repo(xstrdup(p)); 98 repo = cgit_add_repo(rel);
91 if (ctx.cfg.remove_suffix) 99 if (ctx.cfg.remove_suffix)
92 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git")) 100 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
93 *p = '\0'; 101 *p = '\0';
94 repo->name = repo->url; 102 repo->name = repo->url;
95 repo->path = xstrdup(path); 103 repo->path = xstrdup(path);
96 while (!owner) { 104 while (!owner) {
97 if ((pwd = getpwuid(st.st_uid)) == NULL) { 105 if ((pwd = getpwuid(st.st_uid)) == NULL) {
98 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n", 106 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
99 path, strerror(errno), errno); 107 path, strerror(errno), errno);
100 break; 108 break;
101 } 109 }
102 if (pwd->pw_gecos) 110 if (pwd->pw_gecos)
103 if ((p = strchr(pwd->pw_gecos, ','))) 111 if ((p = strchr(pwd->pw_gecos, ',')))
104 *p = '\0'; 112 *p = '\0';
105 owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name); 113 owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
106 } 114 }
107 repo->owner = owner; 115 repo->owner = owner;
108 116
109 p = fmt("%s/description", path); 117 p = fmt("%s/description", path);
110 if (!stat(p, &st)) 118 if (!stat(p, &st))
111 readfile(p, &repo->desc, &size); 119 readfile(p, &repo->desc, &size);
112 120
113 p = fmt("%s/README.html", path); 121 p = fmt("%s/README.html", path);
114 if (!stat(p, &st)) 122 if (!stat(p, &st))
115 repo->readme = "README.html"; 123 repo->readme = "README.html";
124 if (ctx.cfg.section_from_path) {
125 n = ctx.cfg.section_from_path;
126 if (n > 0) {
127 slash = rel;
128 while (slash && n && (slash = strchr(slash, '/')))
129 n--;
130 } else {
131 slash = rel + strlen(rel);
132 while (slash && n && (slash = xstrrchr(rel, slash, '/')))
133 n++;
134 }
135 if (slash && !n) {
136 *slash = '\0';
137 repo->section = xstrdup(rel);
138 *slash = '/';
139 if (!prefixcmp(repo->name, repo->section)) {
140 repo->name += strlen(repo->section);
141 if (*repo->name == '/')
142 repo->name++;
143 }
144 }
145 }
116 146
117 p = fmt("%s/cgitrc", path); 147 p = fmt("%s/cgitrc", path);
118 if (!stat(p, &st)) { 148 if (!stat(p, &st)) {
119 config_fn = fn; 149 config_fn = fn;
120 parse_configfile(xstrdup(p), &repo_config); 150 parse_configfile(xstrdup(p), &repo_config);
121 } 151 }
122} 152}
123 153
124static void scan_path(const char *base, const char *path, repo_config_fn fn) 154static void scan_path(const char *base, const char *path, repo_config_fn fn)
125{ 155{
126 DIR *dir; 156 DIR *dir;
127 struct dirent *ent; 157 struct dirent *ent;
128 char *buf; 158 char *buf;
129 struct stat st; 159 struct stat st;
130 160
131 if (is_git_dir(path)) { 161 if (is_git_dir(path)) {
132 add_repo(base, path, fn); 162 add_repo(base, path, fn);
133 return; 163 return;
134 } 164 }
135 if (is_git_dir(fmt("%s/.git", path))) { 165 if (is_git_dir(fmt("%s/.git", path))) {
136 add_repo(base, fmt("%s/.git", path), fn); 166 add_repo(base, fmt("%s/.git", path), fn);
137 return; 167 return;
138 } 168 }
139 dir = opendir(path); 169 dir = opendir(path);
140 if (!dir) { 170 if (!dir) {
141 fprintf(stderr, "Error opening directory %s: %s (%d)\n", 171 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
142 path, strerror(errno), errno); 172 path, strerror(errno), errno);
143 return; 173 return;
144 } 174 }
145 while((ent = readdir(dir)) != NULL) { 175 while((ent = readdir(dir)) != NULL) {
146 if (ent->d_name[0] == '.') { 176 if (ent->d_name[0] == '.') {
147 if (ent->d_name[1] == '\0') 177 if (ent->d_name[1] == '\0')
148 continue; 178 continue;
149 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0') 179 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
150 continue; 180 continue;
151 } 181 }
152 buf = malloc(strlen(path) + strlen(ent->d_name) + 2); 182 buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
153 if (!buf) { 183 if (!buf) {
154 fprintf(stderr, "Alloc error on %s: %s (%d)\n", 184 fprintf(stderr, "Alloc error on %s: %s (%d)\n",
155 path, strerror(errno), errno); 185 path, strerror(errno), errno);
156 exit(1); 186 exit(1);
157 } 187 }
158 sprintf(buf, "%s/%s", path, ent->d_name); 188 sprintf(buf, "%s/%s", path, ent->d_name);
159 if (stat(buf, &st)) { 189 if (stat(buf, &st)) {
160 fprintf(stderr, "Error checking path %s: %s (%d)\n", 190 fprintf(stderr, "Error checking path %s: %s (%d)\n",
161 buf, strerror(errno), errno); 191 buf, strerror(errno), errno);
162 free(buf); 192 free(buf);
163 continue; 193 continue;
164 } 194 }
165 if (S_ISDIR(st.st_mode)) 195 if (S_ISDIR(st.st_mode))
166 scan_path(base, buf, fn); 196 scan_path(base, buf, fn);
167 free(buf); 197 free(buf);
168 } 198 }
169 closedir(dir); 199 closedir(dir);
170} 200}
171 201
172#define lastc(s) s[strlen(s) - 1] 202#define lastc(s) s[strlen(s) - 1]
173 203
174void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn) 204void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
175{ 205{
176 char line[MAX_PATH * 2], *z; 206 char line[MAX_PATH * 2], *z;
177 FILE *projects; 207 FILE *projects;
178 int err; 208 int err;
179 209
180 projects = fopen(projectsfile, "r"); 210 projects = fopen(projectsfile, "r");
181 if (!projects) { 211 if (!projects) {
182 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n", 212 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
183 projectsfile, strerror(errno), errno); 213 projectsfile, strerror(errno), errno);
184 } 214 }
185 while (fgets(line, sizeof(line), projects) != NULL) { 215 while (fgets(line, sizeof(line), projects) != NULL) {
186 for (z = &lastc(line); 216 for (z = &lastc(line);
187 strlen(line) && strchr("\n\r", *z); 217 strlen(line) && strchr("\n\r", *z);
188 z = &lastc(line)) 218 z = &lastc(line))
189 *z = '\0'; 219 *z = '\0';
190 if (strlen(line)) 220 if (strlen(line))
191 scan_path(path, fmt("%s/%s", path, line), fn); 221 scan_path(path, fmt("%s/%s", path, line), fn);
192 } 222 }
193 if ((err = ferror(projects))) { 223 if ((err = ferror(projects))) {
194 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n", 224 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
195 projectsfile, strerror(err), err); 225 projectsfile, strerror(err), err);
196 } 226 }
197 fclose(projects); 227 fclose(projects);
198} 228}
199 229
200void scan_tree(const char *path, repo_config_fn fn) 230void scan_tree(const char *path, repo_config_fn fn)
201{ 231{
202 scan_path(path, path, fn); 232 scan_path(path, path, fn);
203} 233}