]>
git.cameronkatri.com Git - cgit.git/blob - scan-tree.c
6 /* return 1 if path contains a objects/ directory and a HEAD file */
7 static int is_git_dir(const char *path
)
10 static char buf
[MAX_PATH
];
12 if (snprintf(buf
, MAX_PATH
, "%s/objects", path
) >= MAX_PATH
) {
13 fprintf(stderr
, "Insanely long path: %s\n", path
);
18 fprintf(stderr
, "Error checking path %s: %s (%d)\n",
19 path
, strerror(errno
), errno
);
22 if (!S_ISDIR(st
.st_mode
))
25 sprintf(buf
, "%s/HEAD", path
);
28 fprintf(stderr
, "Error checking path %s: %s (%d)\n",
29 path
, strerror(errno
), errno
);
32 if (!S_ISREG(st
.st_mode
))
38 char *readfile(const char *path
)
41 static char buf
[MAX_PATH
];
43 if (!(f
= fopen(path
, "r")))
45 fgets(buf
, MAX_PATH
, f
);
50 static void add_repo(const char *base
, const char *path
)
52 struct cgit_repo
*repo
;
57 if (stat(path
, &st
)) {
58 fprintf(stderr
, "Error accessing %s: %s (%d)\n",
59 path
, strerror(errno
), errno
);
62 if ((pwd
= getpwuid(st
.st_uid
)) == NULL
) {
63 fprintf(stderr
, "Error reading owner-info for %s: %s (%d)\n",
64 path
, strerror(errno
), errno
);
70 p
= fmt("%s", path
+ strlen(base
) + 1);
72 if (!strcmp(p
+ strlen(p
) - 5, "/.git"))
73 p
[strlen(p
) - 5] = '\0';
75 repo
= cgit_add_repo(xstrdup(p
));
76 repo
->name
= repo
->url
;
77 repo
->path
= xstrdup(path
);
78 repo
->owner
= (pwd
? xstrdup(pwd
->pw_gecos
? pwd
->pw_gecos
: pwd
->pw_name
) : "");
80 p
= fmt("%s/description", path
);
82 repo
->desc
= xstrdup(readfile(p
));
84 p
= fmt("%s/README.html", path
);
86 repo
->readme
= "README.html";
89 static void scan_path(const char *base
, const char *path
)
96 if (is_git_dir(path
)) {
102 fprintf(stderr
, "Error opening directory %s: %s (%d)\n",
103 path
, strerror(errno
), errno
);
106 while((ent
= readdir(dir
)) != NULL
) {
107 if (ent
->d_name
[0] == '.') {
108 if (ent
->d_name
[1] == '\0')
110 if (ent
->d_name
[1] == '.' && ent
->d_name
[2] == '\0')
113 buf
= malloc(strlen(path
) + strlen(ent
->d_name
) + 2);
115 fprintf(stderr
, "Alloc error on %s: %s (%d)\n",
116 path
, strerror(errno
), errno
);
119 sprintf(buf
, "%s/%s", path
, ent
->d_name
);
120 if (stat(buf
, &st
)) {
121 fprintf(stderr
, "Error checking path %s: %s (%d)\n",
122 buf
, strerror(errno
), errno
);
126 if (S_ISDIR(st
.st_mode
))
127 scan_path(base
, buf
);
133 void scan_tree(const char *path
)
135 scan_path(path
, path
);