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