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