blob: 1e18f3c9b89e72607fccaa3bc6574c7233c6fb41 [file] [log] [blame]
Lars Hjemli93397a72008-09-15 00:07:12 +02001#include "cgit.h"
Lars Hjemli74061ed2009-08-24 00:04:58 +02002#include "configfile.h"
Lars Hjemli93397a72008-09-15 00:07:12 +02003#include "html.h"
4
5#define MAX_PATH 4096
6
7/* return 1 if path contains a objects/ directory and a HEAD file */
8static 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 Hjemli74061ed2009-08-24 00:04:58 +020039struct cgit_repo *repo;
40repo_config_fn config_fn;
41
42static void repo_config(const char *name, const char *value)
Lars Hjemli93397a72008-09-15 00:07:12 +020043{
Lars Hjemli74061ed2009-08-24 00:04:58 +020044 config_fn(repo, name, value);
45}
46
47static void add_repo(const char *base, const char *path, repo_config_fn fn)
48{
Lars Hjemli93397a72008-09-15 00:07:12 +020049 struct stat st;
50 struct passwd *pwd;
51 char *p;
Lars Hjemlie16f1782009-08-18 17:17:41 +020052 size_t size;
Lars Hjemli93397a72008-09-15 00:07:12 +020053
54 if (stat(path, &st)) {
55 fprintf(stderr, "Error accessing %s: %s (%d)\n",
56 path, strerror(errno), errno);
57 return;
58 }
Lars Hjemli31ba37c2010-02-28 18:40:02 +010059 if (!stat(fmt("%s/noweb", path), &st))
60 return;
Lars Hjemli93397a72008-09-15 00:07:12 +020061 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 Naewe6445a3a2009-08-20 08:24:51 +020077 p = (pwd && pwd->pw_gecos) ? strchr(pwd->pw_gecos, ',') : NULL;
78 if (p)
79 *p = '\0';
Lars Hjemli93397a72008-09-15 00:07:12 +020080 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 Hjemlie16f1782009-08-18 17:17:41 +020084 readfile(p, &repo->desc, &size);
Lars Hjemli93397a72008-09-15 00:07:12 +020085
86 p = fmt("%s/README.html", path);
87 if (!stat(p, &st))
88 repo->readme = "README.html";
Lars Hjemli74061ed2009-08-24 00:04:58 +020089
90 p = fmt("%s/cgitrc", path);
91 if (!stat(p, &st)) {
92 config_fn = fn;
93 parse_configfile(xstrdup(p), &repo_config);
94 }
Lars Hjemli93397a72008-09-15 00:07:12 +020095}
96
Lars Hjemli74061ed2009-08-24 00:04:58 +020097static void scan_path(const char *base, const char *path, repo_config_fn fn)
Lars Hjemli93397a72008-09-15 00:07:12 +020098{
99 DIR *dir;
100 struct dirent *ent;
101 char *buf;
102 struct stat st;
103
104 if (is_git_dir(path)) {
Lars Hjemli74061ed2009-08-24 00:04:58 +0200105 add_repo(base, path, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200106 return;
107 }
Lars Hjemlifb9bf552009-08-11 10:08:12 +0200108 if (is_git_dir(fmt("%s/.git", path))) {
Lars Hjemli74061ed2009-08-24 00:04:58 +0200109 add_repo(base, fmt("%s/.git", path), fn);
Lars Hjemlifb9bf552009-08-11 10:08:12 +0200110 return;
111 }
Lars Hjemli93397a72008-09-15 00:07:12 +0200112 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 Hjemli74061ed2009-08-24 00:04:58 +0200139 scan_path(base, buf, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200140 free(buf);
141 }
142 closedir(dir);
143}
144
Lars Hjemli74061ed2009-08-24 00:04:58 +0200145void scan_tree(const char *path, repo_config_fn fn)
Lars Hjemli93397a72008-09-15 00:07:12 +0200146{
Lars Hjemli74061ed2009-08-24 00:04:58 +0200147 scan_path(path, path, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200148}