aboutsummaryrefslogtreecommitdiffstats
path: root/shared.c
diff options
context:
space:
mode:
Diffstat (limited to 'shared.c')
-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
@@ -48,12 +48,13 @@ struct cgit_repo *cgit_add_repo(const char *url)
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;
@@ -62,6 +63,9 @@ struct cgit_repo *cgit_add_repo(const char *url)
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
@@ -355,3 +359,59 @@ int cgit_parse_snapshots_mask(const char *str)
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}