blob: dbca7971105ae17732267822abe7049e98f2f523 [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 }
59 if ((pwd = getpwuid(st.st_uid)) == NULL) {
60 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
61 path, strerror(errno), errno);
62 return;
63 }
64 if (base == path)
65 p = fmt("%s", path);
66 else
67 p = fmt("%s", path + strlen(base) + 1);
68
69 if (!strcmp(p + strlen(p) - 5, "/.git"))
70 p[strlen(p) - 5] = '\0';
71
72 repo = cgit_add_repo(xstrdup(p));
73 repo->name = repo->url;
74 repo->path = xstrdup(path);
Stefan Naewe6445a3a2009-08-20 08:24:51 +020075 p = (pwd && pwd->pw_gecos) ? strchr(pwd->pw_gecos, ',') : NULL;
76 if (p)
77 *p = '\0';
Lars Hjemli93397a72008-09-15 00:07:12 +020078 repo->owner = (pwd ? xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name) : "");
79
80 p = fmt("%s/description", path);
81 if (!stat(p, &st))
Lars Hjemlie16f1782009-08-18 17:17:41 +020082 readfile(p, &repo->desc, &size);
Lars Hjemli93397a72008-09-15 00:07:12 +020083
84 p = fmt("%s/README.html", path);
85 if (!stat(p, &st))
86 repo->readme = "README.html";
Lars Hjemli74061ed2009-08-24 00:04:58 +020087
88 p = fmt("%s/cgitrc", path);
89 if (!stat(p, &st)) {
90 config_fn = fn;
91 parse_configfile(xstrdup(p), &repo_config);
92 }
Lars Hjemli93397a72008-09-15 00:07:12 +020093}
94
Lars Hjemli74061ed2009-08-24 00:04:58 +020095static void scan_path(const char *base, const char *path, repo_config_fn fn)
Lars Hjemli93397a72008-09-15 00:07:12 +020096{
97 DIR *dir;
98 struct dirent *ent;
99 char *buf;
100 struct stat st;
101
102 if (is_git_dir(path)) {
Lars Hjemli74061ed2009-08-24 00:04:58 +0200103 add_repo(base, path, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200104 return;
105 }
Lars Hjemlifb9bf552009-08-11 10:08:12 +0200106 if (is_git_dir(fmt("%s/.git", path))) {
Lars Hjemli74061ed2009-08-24 00:04:58 +0200107 add_repo(base, fmt("%s/.git", path), fn);
Lars Hjemlifb9bf552009-08-11 10:08:12 +0200108 return;
109 }
Lars Hjemli93397a72008-09-15 00:07:12 +0200110 dir = opendir(path);
111 if (!dir) {
112 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
113 path, strerror(errno), errno);
114 return;
115 }
116 while((ent = readdir(dir)) != NULL) {
117 if (ent->d_name[0] == '.') {
118 if (ent->d_name[1] == '\0')
119 continue;
120 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
121 continue;
122 }
123 buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
124 if (!buf) {
125 fprintf(stderr, "Alloc error on %s: %s (%d)\n",
126 path, strerror(errno), errno);
127 exit(1);
128 }
129 sprintf(buf, "%s/%s", path, ent->d_name);
130 if (stat(buf, &st)) {
131 fprintf(stderr, "Error checking path %s: %s (%d)\n",
132 buf, strerror(errno), errno);
133 free(buf);
134 continue;
135 }
136 if (S_ISDIR(st.st_mode))
Lars Hjemli74061ed2009-08-24 00:04:58 +0200137 scan_path(base, buf, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200138 free(buf);
139 }
140 closedir(dir);
141}
142
Lars Hjemli74061ed2009-08-24 00:04:58 +0200143void scan_tree(const char *path, repo_config_fn fn)
Lars Hjemli93397a72008-09-15 00:07:12 +0200144{
Lars Hjemli74061ed2009-08-24 00:04:58 +0200145 scan_path(path, path, fn);
Lars Hjemli93397a72008-09-15 00:07:12 +0200146}