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 | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 64 | static void add_repo(const char *base, const char *path, repo_config_fn fn) |
| 65 | { |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 66 | struct stat st; |
| 67 | struct passwd *pwd; |
| 68 | char *p; |
Lars Hjemli | e16f178 | 2009-08-18 17:17:41 +0200 | [diff] [blame] | 69 | size_t size; |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 70 | |
| 71 | if (stat(path, &st)) { |
| 72 | fprintf(stderr, "Error accessing %s: %s (%d)\n", |
| 73 | path, strerror(errno), errno); |
| 74 | return; |
| 75 | } |
Lars Hjemli | 31ba37c | 2010-02-28 18:40:02 +0100 | [diff] [blame] | 76 | if (!stat(fmt("%s/noweb", path), &st)) |
| 77 | return; |
Jason A. Donenfeld | 119397b | 2010-07-29 20:38:01 +0200 | [diff] [blame^] | 78 | |
| 79 | owner = NULL; |
| 80 | if (ctx.cfg.enable_gitweb_owner) |
| 81 | git_config_from_file(git_owner_config, fmt("%s/config", path), NULL); |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 82 | if (base == path) |
| 83 | p = fmt("%s", path); |
| 84 | else |
| 85 | p = fmt("%s", path + strlen(base) + 1); |
| 86 | |
| 87 | if (!strcmp(p + strlen(p) - 5, "/.git")) |
| 88 | p[strlen(p) - 5] = '\0'; |
| 89 | |
| 90 | repo = cgit_add_repo(xstrdup(p)); |
Jason A. Donenfeld | 2e4a941 | 2010-07-29 19:47:50 +0200 | [diff] [blame] | 91 | if (ctx.cfg.remove_suffix) |
| 92 | if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git")) |
| 93 | *p = '\0'; |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 94 | repo->name = repo->url; |
| 95 | repo->path = xstrdup(path); |
Jason A. Donenfeld | 119397b | 2010-07-29 20:38:01 +0200 | [diff] [blame^] | 96 | while (!owner) { |
| 97 | if ((pwd = getpwuid(st.st_uid)) == NULL) { |
| 98 | fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n", |
| 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; |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 108 | |
| 109 | p = fmt("%s/description", path); |
| 110 | if (!stat(p, &st)) |
Lars Hjemli | e16f178 | 2009-08-18 17:17:41 +0200 | [diff] [blame] | 111 | readfile(p, &repo->desc, &size); |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 112 | |
| 113 | p = fmt("%s/README.html", path); |
| 114 | if (!stat(p, &st)) |
| 115 | repo->readme = "README.html"; |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 116 | |
| 117 | p = fmt("%s/cgitrc", path); |
| 118 | if (!stat(p, &st)) { |
| 119 | config_fn = fn; |
| 120 | parse_configfile(xstrdup(p), &repo_config); |
| 121 | } |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 122 | } |
| 123 | |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 124 | 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] | 125 | { |
| 126 | DIR *dir; |
| 127 | struct dirent *ent; |
| 128 | char *buf; |
| 129 | struct stat st; |
| 130 | |
| 131 | if (is_git_dir(path)) { |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 132 | add_repo(base, path, fn); |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 133 | return; |
| 134 | } |
Lars Hjemli | fb9bf55 | 2009-08-11 10:08:12 +0200 | [diff] [blame] | 135 | if (is_git_dir(fmt("%s/.git", path))) { |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 136 | add_repo(base, fmt("%s/.git", path), fn); |
Lars Hjemli | fb9bf55 | 2009-08-11 10:08:12 +0200 | [diff] [blame] | 137 | return; |
| 138 | } |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 139 | dir = opendir(path); |
| 140 | if (!dir) { |
| 141 | fprintf(stderr, "Error opening directory %s: %s (%d)\n", |
| 142 | path, strerror(errno), errno); |
| 143 | return; |
| 144 | } |
| 145 | while((ent = readdir(dir)) != NULL) { |
| 146 | if (ent->d_name[0] == '.') { |
| 147 | if (ent->d_name[1] == '\0') |
| 148 | continue; |
| 149 | if (ent->d_name[1] == '.' && ent->d_name[2] == '\0') |
| 150 | continue; |
| 151 | } |
| 152 | buf = malloc(strlen(path) + strlen(ent->d_name) + 2); |
| 153 | if (!buf) { |
| 154 | fprintf(stderr, "Alloc error on %s: %s (%d)\n", |
| 155 | path, strerror(errno), errno); |
| 156 | exit(1); |
| 157 | } |
| 158 | sprintf(buf, "%s/%s", path, ent->d_name); |
| 159 | if (stat(buf, &st)) { |
| 160 | fprintf(stderr, "Error checking path %s: %s (%d)\n", |
| 161 | buf, strerror(errno), errno); |
| 162 | free(buf); |
| 163 | continue; |
| 164 | } |
| 165 | if (S_ISDIR(st.st_mode)) |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 166 | scan_path(base, buf, fn); |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 167 | free(buf); |
| 168 | } |
| 169 | closedir(dir); |
| 170 | } |
| 171 | |
Jason A. Donenfeld | 3516502 | 2010-07-29 17:52:29 +0200 | [diff] [blame] | 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 | |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 200 | void scan_tree(const char *path, repo_config_fn fn) |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 201 | { |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 202 | scan_path(path, path, fn); |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 203 | } |