Jason A. Donenfeld | 3516502 | 2010-07-29 17:52:29 +0200 | [diff] [blame] | 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 | |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 10 | #include "cgit.h" |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 11 | #include "configfile.h" |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 12 | #include "html.h" |
| 13 | |
| 14 | #define MAX_PATH 4096 |
| 15 | |
| 16 | /* return 1 if path contains a objects/ directory and a HEAD file */ |
| 17 | static int is_git_dir(const char *path) |
| 18 | { |
| 19 | struct stat st; |
| 20 | static char buf[MAX_PATH]; |
| 21 | |
| 22 | if (snprintf(buf, MAX_PATH, "%s/objects", path) >= MAX_PATH) { |
| 23 | fprintf(stderr, "Insanely long path: %s\n", path); |
| 24 | return 0; |
| 25 | } |
| 26 | if (stat(buf, &st)) { |
| 27 | if (errno != ENOENT) |
| 28 | fprintf(stderr, "Error checking path %s: %s (%d)\n", |
| 29 | path, strerror(errno), errno); |
| 30 | return 0; |
| 31 | } |
| 32 | if (!S_ISDIR(st.st_mode)) |
| 33 | return 0; |
| 34 | |
| 35 | sprintf(buf, "%s/HEAD", path); |
| 36 | if (stat(buf, &st)) { |
| 37 | if (errno != ENOENT) |
| 38 | fprintf(stderr, "Error checking path %s: %s (%d)\n", |
| 39 | path, strerror(errno), errno); |
| 40 | return 0; |
| 41 | } |
| 42 | if (!S_ISREG(st.st_mode)) |
| 43 | return 0; |
| 44 | |
| 45 | return 1; |
| 46 | } |
| 47 | |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 48 | struct cgit_repo *repo; |
| 49 | repo_config_fn config_fn; |
Jason A. Donenfeld | 119397b | 2010-07-29 20:38:01 +0200 | [diff] [blame] | 50 | char *owner; |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 51 | |
| 52 | static void repo_config(const char *name, const char *value) |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 53 | { |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 54 | config_fn(repo, name, value); |
| 55 | } |
| 56 | |
Jason A. Donenfeld | 119397b | 2010-07-29 20:38:01 +0200 | [diff] [blame] | 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 | |
Lars Hjemli | 797110e | 2010-08-21 15:44:09 +0200 | [diff] [blame^] | 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 | |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 71 | static void add_repo(const char *base, const char *path, repo_config_fn fn) |
| 72 | { |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 73 | struct stat st; |
| 74 | struct passwd *pwd; |
Lars Hjemli | 797110e | 2010-08-21 15:44:09 +0200 | [diff] [blame^] | 75 | char *rel, *p, *slash; |
| 76 | int n; |
Lars Hjemli | e16f178 | 2009-08-18 17:17:41 +0200 | [diff] [blame] | 77 | size_t size; |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 78 | |
| 79 | if (stat(path, &st)) { |
| 80 | fprintf(stderr, "Error accessing %s: %s (%d)\n", |
| 81 | path, strerror(errno), errno); |
| 82 | return; |
| 83 | } |
Lars Hjemli | 31ba37c | 2010-02-28 18:40:02 +0100 | [diff] [blame] | 84 | if (!stat(fmt("%s/noweb", path), &st)) |
| 85 | return; |
Jason A. Donenfeld | 119397b | 2010-07-29 20:38:01 +0200 | [diff] [blame] | 86 | |
| 87 | owner = NULL; |
| 88 | if (ctx.cfg.enable_gitweb_owner) |
| 89 | git_config_from_file(git_owner_config, fmt("%s/config", path), NULL); |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 90 | if (base == path) |
Lars Hjemli | 797110e | 2010-08-21 15:44:09 +0200 | [diff] [blame^] | 91 | rel = xstrdup(fmt("%s", path)); |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 92 | else |
Lars Hjemli | 797110e | 2010-08-21 15:44:09 +0200 | [diff] [blame^] | 93 | rel = xstrdup(fmt("%s", path + strlen(base) + 1)); |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 94 | |
Lars Hjemli | 797110e | 2010-08-21 15:44:09 +0200 | [diff] [blame^] | 95 | if (!strcmp(rel + strlen(rel) - 5, "/.git")) |
| 96 | rel[strlen(rel) - 5] = '\0'; |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 97 | |
Lars Hjemli | 797110e | 2010-08-21 15:44:09 +0200 | [diff] [blame^] | 98 | repo = cgit_add_repo(rel); |
Jason A. Donenfeld | 2e4a941 | 2010-07-29 19:47:50 +0200 | [diff] [blame] | 99 | if (ctx.cfg.remove_suffix) |
| 100 | if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git")) |
| 101 | *p = '\0'; |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 102 | repo->name = repo->url; |
| 103 | repo->path = xstrdup(path); |
Jason A. Donenfeld | 119397b | 2010-07-29 20:38:01 +0200 | [diff] [blame] | 104 | while (!owner) { |
| 105 | if ((pwd = getpwuid(st.st_uid)) == NULL) { |
| 106 | fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n", |
| 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; |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 116 | |
| 117 | p = fmt("%s/description", path); |
| 118 | if (!stat(p, &st)) |
Lars Hjemli | e16f178 | 2009-08-18 17:17:41 +0200 | [diff] [blame] | 119 | readfile(p, &repo->desc, &size); |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 120 | |
| 121 | p = fmt("%s/README.html", path); |
| 122 | if (!stat(p, &st)) |
| 123 | repo->readme = "README.html"; |
Lars Hjemli | 797110e | 2010-08-21 15:44:09 +0200 | [diff] [blame^] | 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 | } |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 146 | |
| 147 | p = fmt("%s/cgitrc", path); |
| 148 | if (!stat(p, &st)) { |
| 149 | config_fn = fn; |
| 150 | parse_configfile(xstrdup(p), &repo_config); |
| 151 | } |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 154 | static void scan_path(const char *base, const char *path, repo_config_fn fn) |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 155 | { |
| 156 | DIR *dir; |
| 157 | struct dirent *ent; |
| 158 | char *buf; |
| 159 | struct stat st; |
| 160 | |
| 161 | if (is_git_dir(path)) { |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 162 | add_repo(base, path, fn); |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 163 | return; |
| 164 | } |
Lars Hjemli | fb9bf55 | 2009-08-11 10:08:12 +0200 | [diff] [blame] | 165 | if (is_git_dir(fmt("%s/.git", path))) { |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 166 | add_repo(base, fmt("%s/.git", path), fn); |
Lars Hjemli | fb9bf55 | 2009-08-11 10:08:12 +0200 | [diff] [blame] | 167 | return; |
| 168 | } |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 169 | dir = opendir(path); |
| 170 | if (!dir) { |
| 171 | fprintf(stderr, "Error opening directory %s: %s (%d)\n", |
| 172 | path, strerror(errno), errno); |
| 173 | return; |
| 174 | } |
| 175 | while((ent = readdir(dir)) != NULL) { |
| 176 | if (ent->d_name[0] == '.') { |
| 177 | if (ent->d_name[1] == '\0') |
| 178 | continue; |
| 179 | if (ent->d_name[1] == '.' && ent->d_name[2] == '\0') |
| 180 | continue; |
| 181 | } |
| 182 | buf = malloc(strlen(path) + strlen(ent->d_name) + 2); |
| 183 | if (!buf) { |
| 184 | fprintf(stderr, "Alloc error on %s: %s (%d)\n", |
| 185 | path, strerror(errno), errno); |
| 186 | exit(1); |
| 187 | } |
| 188 | sprintf(buf, "%s/%s", path, ent->d_name); |
| 189 | if (stat(buf, &st)) { |
| 190 | fprintf(stderr, "Error checking path %s: %s (%d)\n", |
| 191 | buf, strerror(errno), errno); |
| 192 | free(buf); |
| 193 | continue; |
| 194 | } |
| 195 | if (S_ISDIR(st.st_mode)) |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 196 | scan_path(base, buf, fn); |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 197 | free(buf); |
| 198 | } |
| 199 | closedir(dir); |
| 200 | } |
| 201 | |
Jason A. Donenfeld | 3516502 | 2010-07-29 17:52:29 +0200 | [diff] [blame] | 202 | #define lastc(s) s[strlen(s) - 1] |
| 203 | |
| 204 | void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn) |
| 205 | { |
| 206 | char line[MAX_PATH * 2], *z; |
| 207 | FILE *projects; |
| 208 | int err; |
| 209 | |
| 210 | projects = fopen(projectsfile, "r"); |
| 211 | if (!projects) { |
| 212 | fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n", |
| 213 | projectsfile, strerror(errno), errno); |
| 214 | } |
| 215 | while (fgets(line, sizeof(line), projects) != NULL) { |
| 216 | for (z = &lastc(line); |
| 217 | strlen(line) && strchr("\n\r", *z); |
| 218 | z = &lastc(line)) |
| 219 | *z = '\0'; |
| 220 | if (strlen(line)) |
| 221 | scan_path(path, fmt("%s/%s", path, line), fn); |
| 222 | } |
| 223 | if ((err = ferror(projects))) { |
| 224 | fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n", |
| 225 | projectsfile, strerror(err), err); |
| 226 | } |
| 227 | fclose(projects); |
| 228 | } |
| 229 | |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 230 | void scan_tree(const char *path, repo_config_fn fn) |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 231 | { |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 232 | scan_path(path, path, fn); |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 233 | } |