scan-tree.c (view raw)
1#include "cgit.h"
2#include "html.h"
3
4#define MAX_PATH 4096
5
6/* return 1 if path contains a objects/ directory and a HEAD file */
7static int is_git_dir(const char *path)
8{
9 struct stat st;
10 static char buf[MAX_PATH];
11
12 if (snprintf(buf, MAX_PATH, "%s/objects", path) >= MAX_PATH) {
13 fprintf(stderr, "Insanely long path: %s\n", path);
14 return 0;
15 }
16 if (stat(buf, &st)) {
17 if (errno != ENOENT)
18 fprintf(stderr, "Error checking path %s: %s (%d)\n",
19 path, strerror(errno), errno);
20 return 0;
21 }
22 if (!S_ISDIR(st.st_mode))
23 return 0;
24
25 sprintf(buf, "%s/HEAD", path);
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_ISREG(st.st_mode))
33 return 0;
34
35 return 1;
36}
37
38char *readfile(const char *path)
39{
40 FILE *f;
41 static char buf[MAX_PATH];
42
43 if (!(f = fopen(path, "r")))
44 return NULL;
45 buf[0] = 0;
46 fgets(buf, MAX_PATH, f);
47 fclose(f);
48 return buf;
49}
50
51static void add_repo(const char *base, const char *path)
52{
53 struct cgit_repo *repo;
54 struct stat st;
55 struct passwd *pwd;
56 char *p;
57
58 if (stat(path, &st)) {
59 fprintf(stderr, "Error accessing %s: %s (%d)\n",
60 path, strerror(errno), errno);
61 return;
62 }
63 if ((pwd = getpwuid(st.st_uid)) == NULL) {
64 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
65 path, strerror(errno), errno);
66 return;
67 }
68 if (base == path)
69 p = fmt("%s", path);
70 else
71 p = fmt("%s", path + strlen(base) + 1);
72
73 if (!strcmp(p + strlen(p) - 5, "/.git"))
74 p[strlen(p) - 5] = '\0';
75
76 repo = cgit_add_repo(xstrdup(p));
77 repo->name = repo->url;
78 repo->path = xstrdup(path);
79 repo->owner = (pwd ? xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name) : "");
80
81 p = fmt("%s/description", path);
82 if (!stat(p, &st))
83 repo->desc = xstrdup(readfile(p));
84
85 p = fmt("%s/README.html", path);
86 if (!stat(p, &st))
87 repo->readme = "README.html";
88}
89
90static void scan_path(const char *base, const char *path)
91{
92 DIR *dir;
93 struct dirent *ent;
94 char *buf;
95 struct stat st;
96
97 if (is_git_dir(path)) {
98 add_repo(base, path);
99 return;
100 }
101 dir = opendir(path);
102 if (!dir) {
103 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
104 path, strerror(errno), errno);
105 return;
106 }
107 while((ent = readdir(dir)) != NULL) {
108 if (ent->d_name[0] == '.') {
109 if (ent->d_name[1] == '\0')
110 continue;
111 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
112 continue;
113 }
114 buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
115 if (!buf) {
116 fprintf(stderr, "Alloc error on %s: %s (%d)\n",
117 path, strerror(errno), errno);
118 exit(1);
119 }
120 sprintf(buf, "%s/%s", path, ent->d_name);
121 if (stat(buf, &st)) {
122 fprintf(stderr, "Error checking path %s: %s (%d)\n",
123 buf, strerror(errno), errno);
124 free(buf);
125 continue;
126 }
127 if (S_ISDIR(st.st_mode))
128 scan_path(base, buf);
129 free(buf);
130 }
131 closedir(dir);
132}
133
134void scan_tree(const char *path)
135{
136 scan_path(path, path);
137}