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