Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 1 | #include "cgit.h" |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 2 | #include "configfile.h" |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 3 | #include "html.h" |
| 4 | |
| 5 | #define MAX_PATH 4096 |
| 6 | |
| 7 | /* return 1 if path contains a objects/ directory and a HEAD file */ |
| 8 | static int is_git_dir(const char *path) |
| 9 | { |
| 10 | struct stat st; |
| 11 | static char buf[MAX_PATH]; |
| 12 | |
| 13 | if (snprintf(buf, MAX_PATH, "%s/objects", path) >= MAX_PATH) { |
| 14 | fprintf(stderr, "Insanely long path: %s\n", path); |
| 15 | return 0; |
| 16 | } |
| 17 | if (stat(buf, &st)) { |
| 18 | if (errno != ENOENT) |
| 19 | fprintf(stderr, "Error checking path %s: %s (%d)\n", |
| 20 | path, strerror(errno), errno); |
| 21 | return 0; |
| 22 | } |
| 23 | if (!S_ISDIR(st.st_mode)) |
| 24 | return 0; |
| 25 | |
| 26 | sprintf(buf, "%s/HEAD", path); |
| 27 | if (stat(buf, &st)) { |
| 28 | if (errno != ENOENT) |
| 29 | fprintf(stderr, "Error checking path %s: %s (%d)\n", |
| 30 | path, strerror(errno), errno); |
| 31 | return 0; |
| 32 | } |
| 33 | if (!S_ISREG(st.st_mode)) |
| 34 | return 0; |
| 35 | |
| 36 | return 1; |
| 37 | } |
| 38 | |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 39 | struct cgit_repo *repo; |
| 40 | repo_config_fn config_fn; |
| 41 | |
| 42 | static void repo_config(const char *name, const char *value) |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 43 | { |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 44 | config_fn(repo, name, value); |
| 45 | } |
| 46 | |
| 47 | static void add_repo(const char *base, const char *path, repo_config_fn fn) |
| 48 | { |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 49 | struct stat st; |
| 50 | struct passwd *pwd; |
| 51 | char *p; |
Lars Hjemli | e16f178 | 2009-08-18 17:17:41 +0200 | [diff] [blame] | 52 | size_t size; |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 53 | |
| 54 | if (stat(path, &st)) { |
| 55 | fprintf(stderr, "Error accessing %s: %s (%d)\n", |
| 56 | path, strerror(errno), errno); |
| 57 | return; |
| 58 | } |
Lars Hjemli | 31ba37c | 2010-02-28 18:40:02 +0100 | [diff] [blame^] | 59 | if (!stat(fmt("%s/noweb", path), &st)) |
| 60 | return; |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 61 | if ((pwd = getpwuid(st.st_uid)) == NULL) { |
| 62 | fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n", |
| 63 | path, strerror(errno), errno); |
| 64 | return; |
| 65 | } |
| 66 | if (base == path) |
| 67 | p = fmt("%s", path); |
| 68 | else |
| 69 | p = fmt("%s", path + strlen(base) + 1); |
| 70 | |
| 71 | if (!strcmp(p + strlen(p) - 5, "/.git")) |
| 72 | p[strlen(p) - 5] = '\0'; |
| 73 | |
| 74 | repo = cgit_add_repo(xstrdup(p)); |
| 75 | repo->name = repo->url; |
| 76 | repo->path = xstrdup(path); |
Stefan Naewe | 6445a3a | 2009-08-20 08:24:51 +0200 | [diff] [blame] | 77 | p = (pwd && pwd->pw_gecos) ? strchr(pwd->pw_gecos, ',') : NULL; |
| 78 | if (p) |
| 79 | *p = '\0'; |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 80 | repo->owner = (pwd ? xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name) : ""); |
| 81 | |
| 82 | p = fmt("%s/description", path); |
| 83 | if (!stat(p, &st)) |
Lars Hjemli | e16f178 | 2009-08-18 17:17:41 +0200 | [diff] [blame] | 84 | readfile(p, &repo->desc, &size); |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 85 | |
| 86 | p = fmt("%s/README.html", path); |
| 87 | if (!stat(p, &st)) |
| 88 | repo->readme = "README.html"; |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 89 | |
| 90 | p = fmt("%s/cgitrc", path); |
| 91 | if (!stat(p, &st)) { |
| 92 | config_fn = fn; |
| 93 | parse_configfile(xstrdup(p), &repo_config); |
| 94 | } |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 97 | 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] | 98 | { |
| 99 | DIR *dir; |
| 100 | struct dirent *ent; |
| 101 | char *buf; |
| 102 | struct stat st; |
| 103 | |
| 104 | if (is_git_dir(path)) { |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 105 | add_repo(base, path, fn); |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 106 | return; |
| 107 | } |
Lars Hjemli | fb9bf55 | 2009-08-11 10:08:12 +0200 | [diff] [blame] | 108 | if (is_git_dir(fmt("%s/.git", path))) { |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 109 | add_repo(base, fmt("%s/.git", path), fn); |
Lars Hjemli | fb9bf55 | 2009-08-11 10:08:12 +0200 | [diff] [blame] | 110 | return; |
| 111 | } |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 112 | dir = opendir(path); |
| 113 | if (!dir) { |
| 114 | fprintf(stderr, "Error opening directory %s: %s (%d)\n", |
| 115 | path, strerror(errno), errno); |
| 116 | return; |
| 117 | } |
| 118 | while((ent = readdir(dir)) != NULL) { |
| 119 | if (ent->d_name[0] == '.') { |
| 120 | if (ent->d_name[1] == '\0') |
| 121 | continue; |
| 122 | if (ent->d_name[1] == '.' && ent->d_name[2] == '\0') |
| 123 | continue; |
| 124 | } |
| 125 | buf = malloc(strlen(path) + strlen(ent->d_name) + 2); |
| 126 | if (!buf) { |
| 127 | fprintf(stderr, "Alloc error on %s: %s (%d)\n", |
| 128 | path, strerror(errno), errno); |
| 129 | exit(1); |
| 130 | } |
| 131 | sprintf(buf, "%s/%s", path, ent->d_name); |
| 132 | if (stat(buf, &st)) { |
| 133 | fprintf(stderr, "Error checking path %s: %s (%d)\n", |
| 134 | buf, strerror(errno), errno); |
| 135 | free(buf); |
| 136 | continue; |
| 137 | } |
| 138 | if (S_ISDIR(st.st_mode)) |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 139 | scan_path(base, buf, fn); |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 140 | free(buf); |
| 141 | } |
| 142 | closedir(dir); |
| 143 | } |
| 144 | |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 145 | void scan_tree(const char *path, repo_config_fn fn) |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 146 | { |
Lars Hjemli | 74061ed | 2009-08-24 00:04:58 +0200 | [diff] [blame] | 147 | scan_path(path, path, fn); |
Lars Hjemli | 93397a7 | 2008-09-15 00:07:12 +0200 | [diff] [blame] | 148 | } |