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; |
| 50 | |
| 51 | static void repo_config(const char *name, const char *value) |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 52 | { |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 53 | config_fn(repo, name, value); |
| 54 | } |
| 55 | |
| 56 | static void add_repo(const char *base, const char *path, repo_config_fn fn) |
| 57 | { |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 58 | struct stat st; |
| 59 | struct passwd *pwd; |
| 60 | char *p; |
Lars Hjemli | e16f178 | 2009-08-18 17:17:41 +0200 | [diff] [blame] | 61 | size_t size; |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 62 | |
| 63 | if (stat(path, &st)) { |
| 64 | fprintf(stderr, "Error accessing %s: %s (%d)\n", |
| 65 | path, strerror(errno), errno); |
| 66 | return; |
| 67 | } |
Lars Hjemli | 31ba37c | 2010-02-28 18:40:02 +0100 | [diff] [blame] | 68 | if (!stat(fmt("%s/noweb", path), &st)) |
| 69 | return; |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 70 | if ((pwd = getpwuid(st.st_uid)) == NULL) { |
| 71 | fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n", |
| 72 | path, strerror(errno), errno); |
| 73 | return; |
| 74 | } |
| 75 | if (base == path) |
| 76 | p = fmt("%s", path); |
| 77 | else |
| 78 | p = fmt("%s", path + strlen(base) + 1); |
| 79 | |
| 80 | if (!strcmp(p + strlen(p) - 5, "/.git")) |
| 81 | p[strlen(p) - 5] = '\0'; |
| 82 | |
| 83 | repo = cgit_add_repo(xstrdup(p)); |
| 84 | repo->name = repo->url; |
| 85 | repo->path = xstrdup(path); |
Stefan Naewe | 6445a3a | 2009-08-20 08:24:51 +0200 | [diff] [blame] | 86 | p = (pwd && pwd->pw_gecos) ? strchr(pwd->pw_gecos, ',') : NULL; |
| 87 | if (p) |
| 88 | *p = '\0'; |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 89 | repo->owner = (pwd ? xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name) : ""); |
| 90 | |
| 91 | p = fmt("%s/description", path); |
| 92 | if (!stat(p, &st)) |
Lars Hjemli | e16f178 | 2009-08-18 17:17:41 +0200 | [diff] [blame] | 93 | readfile(p, &repo->desc, &size); |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 94 | |
| 95 | p = fmt("%s/README.html", path); |
| 96 | if (!stat(p, &st)) |
| 97 | repo->readme = "README.html"; |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 98 | |
| 99 | p = fmt("%s/cgitrc", path); |
| 100 | if (!stat(p, &st)) { |
| 101 | config_fn = fn; |
| 102 | parse_configfile(xstrdup(p), &repo_config); |
| 103 | } |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 106 | 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] | 107 | { |
| 108 | DIR *dir; |
| 109 | struct dirent *ent; |
| 110 | char *buf; |
| 111 | struct stat st; |
| 112 | |
| 113 | if (is_git_dir(path)) { |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 114 | add_repo(base, path, fn); |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 115 | return; |
| 116 | } |
Lars Hjemli | fb9bf55 | 2009-08-11 10:08:12 +0200 | [diff] [blame] | 117 | if (is_git_dir(fmt("%s/.git", path))) { |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 118 | add_repo(base, fmt("%s/.git", path), fn); |
Lars Hjemli | fb9bf55 | 2009-08-11 10:08:12 +0200 | [diff] [blame] | 119 | return; |
| 120 | } |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 121 | dir = opendir(path); |
| 122 | if (!dir) { |
| 123 | fprintf(stderr, "Error opening directory %s: %s (%d)\n", |
| 124 | path, strerror(errno), errno); |
| 125 | return; |
| 126 | } |
| 127 | while((ent = readdir(dir)) != NULL) { |
| 128 | if (ent->d_name[0] == '.') { |
| 129 | if (ent->d_name[1] == '\0') |
| 130 | continue; |
| 131 | if (ent->d_name[1] == '.' && ent->d_name[2] == '\0') |
| 132 | continue; |
| 133 | } |
| 134 | buf = malloc(strlen(path) + strlen(ent->d_name) + 2); |
| 135 | if (!buf) { |
| 136 | fprintf(stderr, "Alloc error on %s: %s (%d)\n", |
| 137 | path, strerror(errno), errno); |
| 138 | exit(1); |
| 139 | } |
| 140 | sprintf(buf, "%s/%s", path, ent->d_name); |
| 141 | if (stat(buf, &st)) { |
| 142 | fprintf(stderr, "Error checking path %s: %s (%d)\n", |
| 143 | buf, strerror(errno), errno); |
| 144 | free(buf); |
| 145 | continue; |
| 146 | } |
| 147 | if (S_ISDIR(st.st_mode)) |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 148 | scan_path(base, buf, fn); |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 149 | free(buf); |
| 150 | } |
| 151 | closedir(dir); |
| 152 | } |
| 153 | |
Jason A. Donenfeld | 3516502 | 2010-07-29 17:52:29 +0200 | [diff] [blame^] | 154 | #define lastc(s) s[strlen(s) - 1] |
| 155 | |
| 156 | void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn) |
| 157 | { |
| 158 | char line[MAX_PATH * 2], *z; |
| 159 | FILE *projects; |
| 160 | int err; |
| 161 | |
| 162 | projects = fopen(projectsfile, "r"); |
| 163 | if (!projects) { |
| 164 | fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n", |
| 165 | projectsfile, strerror(errno), errno); |
| 166 | } |
| 167 | while (fgets(line, sizeof(line), projects) != NULL) { |
| 168 | for (z = &lastc(line); |
| 169 | strlen(line) && strchr("\n\r", *z); |
| 170 | z = &lastc(line)) |
| 171 | *z = '\0'; |
| 172 | if (strlen(line)) |
| 173 | scan_path(path, fmt("%s/%s", path, line), fn); |
| 174 | } |
| 175 | if ((err = ferror(projects))) { |
| 176 | fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n", |
| 177 | projectsfile, strerror(err), err); |
| 178 | } |
| 179 | fclose(projects); |
| 180 | } |
| 181 | |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 182 | void scan_tree(const char *path, repo_config_fn fn) |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 183 | { |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 184 | scan_path(path, path, fn); |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 185 | } |