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 780d405..b5b50f3 100644
--- a/scan-tree.c
+++ b/scan-tree.c
@@ -1,205 +1,235 @@
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 if (!repo->readme) { 121 if (!repo->readme) {
114 p = fmt("%s/README.html", path); 122 p = fmt("%s/README.html", path);
115 if (!stat(p, &st)) 123 if (!stat(p, &st))
116 repo->readme = "README.html"; 124 repo->readme = "README.html";
117 } 125 }
126 if (ctx.cfg.section_from_path) {
127 n = ctx.cfg.section_from_path;
128 if (n > 0) {
129 slash = rel;
130 while (slash && n && (slash = strchr(slash, '/')))
131 n--;
132 } else {
133 slash = rel + strlen(rel);
134 while (slash && n && (slash = xstrrchr(rel, slash, '/')))
135 n++;
136 }
137 if (slash && !n) {
138 *slash = '\0';
139 repo->section = xstrdup(rel);
140 *slash = '/';
141 if (!prefixcmp(repo->name, repo->section)) {
142 repo->name += strlen(repo->section);
143 if (*repo->name == '/')
144 repo->name++;
145 }
146 }
147 }
118 148
119 p = fmt("%s/cgitrc", path); 149 p = fmt("%s/cgitrc", path);
120 if (!stat(p, &st)) { 150 if (!stat(p, &st)) {
121 config_fn = fn; 151 config_fn = fn;
122 parse_configfile(xstrdup(p), &repo_config); 152 parse_configfile(xstrdup(p), &repo_config);
123 } 153 }
124} 154}
125 155
126static void scan_path(const char *base, const char *path, repo_config_fn fn) 156static void scan_path(const char *base, const char *path, repo_config_fn fn)
127{ 157{
128 DIR *dir; 158 DIR *dir;
129 struct dirent *ent; 159 struct dirent *ent;
130 char *buf; 160 char *buf;
131 struct stat st; 161 struct stat st;
132 162
133 if (is_git_dir(path)) { 163 if (is_git_dir(path)) {
134 add_repo(base, path, fn); 164 add_repo(base, path, fn);
135 return; 165 return;
136 } 166 }
137 if (is_git_dir(fmt("%s/.git", path))) { 167 if (is_git_dir(fmt("%s/.git", path))) {
138 add_repo(base, fmt("%s/.git", path), fn); 168 add_repo(base, fmt("%s/.git", path), fn);
139 return; 169 return;
140 } 170 }
141 dir = opendir(path); 171 dir = opendir(path);
142 if (!dir) { 172 if (!dir) {
143 fprintf(stderr, "Error opening directory %s: %s (%d)\n", 173 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
144 path, strerror(errno), errno); 174 path, strerror(errno), errno);
145 return; 175 return;
146 } 176 }
147 while((ent = readdir(dir)) != NULL) { 177 while((ent = readdir(dir)) != NULL) {
148 if (ent->d_name[0] == '.') { 178 if (ent->d_name[0] == '.') {
149 if (ent->d_name[1] == '\0') 179 if (ent->d_name[1] == '\0')
150 continue; 180 continue;
151 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0') 181 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
152 continue; 182 continue;
153 } 183 }
154 buf = malloc(strlen(path) + strlen(ent->d_name) + 2); 184 buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
155 if (!buf) { 185 if (!buf) {
156 fprintf(stderr, "Alloc error on %s: %s (%d)\n", 186 fprintf(stderr, "Alloc error on %s: %s (%d)\n",
157 path, strerror(errno), errno); 187 path, strerror(errno), errno);
158 exit(1); 188 exit(1);
159 } 189 }
160 sprintf(buf, "%s/%s", path, ent->d_name); 190 sprintf(buf, "%s/%s", path, ent->d_name);
161 if (stat(buf, &st)) { 191 if (stat(buf, &st)) {
162 fprintf(stderr, "Error checking path %s: %s (%d)\n", 192 fprintf(stderr, "Error checking path %s: %s (%d)\n",
163 buf, strerror(errno), errno); 193 buf, strerror(errno), errno);
164 free(buf); 194 free(buf);
165 continue; 195 continue;
166 } 196 }
167 if (S_ISDIR(st.st_mode)) 197 if (S_ISDIR(st.st_mode))
168 scan_path(base, buf, fn); 198 scan_path(base, buf, fn);
169 free(buf); 199 free(buf);
170 } 200 }
171 closedir(dir); 201 closedir(dir);
172} 202}
173 203
174#define lastc(s) s[strlen(s) - 1] 204#define lastc(s) s[strlen(s) - 1]
175 205
176void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn) 206void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
177{ 207{
178 char line[MAX_PATH * 2], *z; 208 char line[MAX_PATH * 2], *z;
179 FILE *projects; 209 FILE *projects;
180 int err; 210 int err;
181 211
182 projects = fopen(projectsfile, "r"); 212 projects = fopen(projectsfile, "r");
183 if (!projects) { 213 if (!projects) {
184 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n", 214 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
185 projectsfile, strerror(errno), errno); 215 projectsfile, strerror(errno), errno);
186 } 216 }
187 while (fgets(line, sizeof(line), projects) != NULL) { 217 while (fgets(line, sizeof(line), projects) != NULL) {
188 for (z = &lastc(line); 218 for (z = &lastc(line);
189 strlen(line) && strchr("\n\r", *z); 219 strlen(line) && strchr("\n\r", *z);
190 z = &lastc(line)) 220 z = &lastc(line))
191 *z = '\0'; 221 *z = '\0';
192 if (strlen(line)) 222 if (strlen(line))
193 scan_path(path, fmt("%s/%s", path, line), fn); 223 scan_path(path, fmt("%s/%s", path, line), fn);
194 } 224 }
195 if ((err = ferror(projects))) { 225 if ((err = ferror(projects))) {
196 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n", 226 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
197 projectsfile, strerror(err), err); 227 projectsfile, strerror(err), err);
198 } 228 }
199 fclose(projects); 229 fclose(projects);
200} 230}
201 231
202void scan_tree(const char *path, repo_config_fn fn) 232void scan_tree(const char *path, repo_config_fn fn)
203{ 233{
204 scan_path(path, path, fn); 234 scan_path(path, path, fn);
205} 235}