summaryrefslogtreecommitdiffabout
path: root/shared.c
Unidiff
Diffstat (limited to 'shared.c') (more/less context) (ignore whitespace changes)
-rw-r--r--shared.c62
1 files changed, 61 insertions, 1 deletions
diff --git a/shared.c b/shared.c
index cce0af4..d7b2d5a 100644
--- a/shared.c
+++ b/shared.c
@@ -35,46 +35,50 @@ int chk_non_negative(int result, char *msg)
35 35
36struct cgit_repo *cgit_add_repo(const char *url) 36struct cgit_repo *cgit_add_repo(const char *url)
37{ 37{
38 struct cgit_repo *ret; 38 struct cgit_repo *ret;
39 39
40 if (++cgit_repolist.count > cgit_repolist.length) { 40 if (++cgit_repolist.count > cgit_repolist.length) {
41 if (cgit_repolist.length == 0) 41 if (cgit_repolist.length == 0)
42 cgit_repolist.length = 8; 42 cgit_repolist.length = 8;
43 else 43 else
44 cgit_repolist.length *= 2; 44 cgit_repolist.length *= 2;
45 cgit_repolist.repos = xrealloc(cgit_repolist.repos, 45 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
46 cgit_repolist.length * 46 cgit_repolist.length *
47 sizeof(struct cgit_repo)); 47 sizeof(struct cgit_repo));
48 } 48 }
49 49
50 ret = &cgit_repolist.repos[cgit_repolist.count-1]; 50 ret = &cgit_repolist.repos[cgit_repolist.count-1];
51 memset(ret, 0, sizeof(struct cgit_repo));
51 ret->url = trim_end(url, '/'); 52 ret->url = trim_end(url, '/');
52 ret->name = ret->url; 53 ret->name = ret->url;
53 ret->path = NULL; 54 ret->path = NULL;
54 ret->desc = "[no description]"; 55 ret->desc = "[no description]";
55 ret->owner = NULL; 56 ret->owner = NULL;
56 ret->group = ctx.cfg.repo_group; 57 ret->section = ctx.cfg.section;
57 ret->defbranch = "master"; 58 ret->defbranch = "master";
58 ret->snapshots = ctx.cfg.snapshots; 59 ret->snapshots = ctx.cfg.snapshots;
59 ret->enable_log_filecount = ctx.cfg.enable_log_filecount; 60 ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
60 ret->enable_log_linecount = ctx.cfg.enable_log_linecount; 61 ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
61 ret->max_stats = ctx.cfg.max_stats; 62 ret->max_stats = ctx.cfg.max_stats;
62 ret->module_link = ctx.cfg.module_link; 63 ret->module_link = ctx.cfg.module_link;
63 ret->readme = NULL; 64 ret->readme = NULL;
64 ret->mtime = -1; 65 ret->mtime = -1;
66 ret->about_filter = ctx.cfg.about_filter;
67 ret->commit_filter = ctx.cfg.commit_filter;
68 ret->source_filter = ctx.cfg.source_filter;
65 return ret; 69 return ret;
66} 70}
67 71
68struct cgit_repo *cgit_get_repoinfo(const char *url) 72struct cgit_repo *cgit_get_repoinfo(const char *url)
69{ 73{
70 int i; 74 int i;
71 struct cgit_repo *repo; 75 struct cgit_repo *repo;
72 76
73 for (i=0; i<cgit_repolist.count; i++) { 77 for (i=0; i<cgit_repolist.count; i++) {
74 repo = &cgit_repolist.repos[i]; 78 repo = &cgit_repolist.repos[i];
75 if (!strcmp(repo->url, url)) 79 if (!strcmp(repo->url, url))
76 return repo; 80 return repo;
77 } 81 }
78 return NULL; 82 return NULL;
79} 83}
80 84
@@ -342,16 +346,72 @@ int cgit_parse_snapshots_mask(const char *str)
342 str += strspn(str,delim); 346 str += strspn(str,delim);
343 tl = strcspn(str,delim); 347 tl = strcspn(str,delim);
344 if (!tl) 348 if (!tl)
345 break; 349 break;
346 for (f = cgit_snapshot_formats; f->suffix; f++) { 350 for (f = cgit_snapshot_formats; f->suffix; f++) {
347 sl = strlen(f->suffix); 351 sl = strlen(f->suffix);
348 if((tl == sl && !strncmp(f->suffix, str, tl)) || 352 if((tl == sl && !strncmp(f->suffix, str, tl)) ||
349 (tl == sl-1 && !strncmp(f->suffix+1, str, tl-1))) { 353 (tl == sl-1 && !strncmp(f->suffix+1, str, tl-1))) {
350 rv |= f->bit; 354 rv |= f->bit;
351 break; 355 break;
352 } 356 }
353 } 357 }
354 str += tl; 358 str += tl;
355 } 359 }
356 return rv; 360 return rv;
357} 361}
362
363int cgit_open_filter(struct cgit_filter *filter)
364{
365
366 filter->old_stdout = chk_positive(dup(STDOUT_FILENO),
367 "Unable to duplicate STDOUT");
368 chk_zero(pipe(filter->pipe_fh), "Unable to create pipe to subprocess");
369 filter->pid = chk_non_negative(fork(), "Unable to create subprocess");
370 if (filter->pid == 0) {
371 close(filter->pipe_fh[1]);
372 chk_non_negative(dup2(filter->pipe_fh[0], STDIN_FILENO),
373 "Unable to use pipe as STDIN");
374 execvp(filter->cmd, filter->argv);
375 die("Unable to exec subprocess %s: %s (%d)", filter->cmd,
376 strerror(errno), errno);
377 }
378 close(filter->pipe_fh[0]);
379 chk_non_negative(dup2(filter->pipe_fh[1], STDOUT_FILENO),
380 "Unable to use pipe as STDOUT");
381 close(filter->pipe_fh[1]);
382 return 0;
383}
384
385int cgit_close_filter(struct cgit_filter *filter)
386{
387 chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO),
388 "Unable to restore STDOUT");
389 close(filter->old_stdout);
390 if (filter->pid < 0)
391 return 0;
392 waitpid(filter->pid, &filter->exitstatus, 0);
393 if (WIFEXITED(filter->exitstatus) && !WEXITSTATUS(filter->exitstatus))
394 return 0;
395 die("Subprocess %s exited abnormally", filter->cmd);
396}
397
398/* Read the content of the specified file into a newly allocated buffer,
399 * zeroterminate the buffer and return 0 on success, errno otherwise.
400 */
401int readfile(const char *path, char **buf, size_t *size)
402{
403 int fd;
404 struct stat st;
405
406 fd = open(path, O_RDONLY);
407 if (fd == -1)
408 return errno;
409 if (fstat(fd, &st))
410 return errno;
411 if (!S_ISREG(st.st_mode))
412 return EISDIR;
413 *buf = xmalloc(st.st_size + 1);
414 *size = read_in_full(fd, *buf, st.st_size);
415 (*buf)[*size] = '\0';
416 return (*size == st.st_size ? 0 : errno);
417}