From 316bd60579ea11daca4fa25e2720b7fa147cc7ba Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Wed, 30 May 2018 23:53:26 +0200 Subject: git-receiver: centralize settings to reuse later --- .gitignore | 2 +- Makefile.am | 14 ++-- src/blogc-git-receiver/post-receive.c | 75 +++----------------- src/blogc-git-receiver/post-receive.h | 4 -- src/blogc-git-receiver/pre-receive.c | 12 ++-- src/blogc-git-receiver/settings.c | 99 +++++++++++++++++++++++++++ src/blogc-git-receiver/settings.h | 18 +++++ src/blogc-git-receiver/shell.c | 19 +++-- tests/blogc-git-receiver/check_post_receive.c | 91 ------------------------ tests/blogc-git-receiver/check_settings.c | 91 ++++++++++++++++++++++++ 10 files changed, 238 insertions(+), 187 deletions(-) create mode 100644 src/blogc-git-receiver/settings.c create mode 100644 src/blogc-git-receiver/settings.h delete mode 100644 tests/blogc-git-receiver/check_post_receive.c create mode 100644 tests/blogc-git-receiver/check_settings.c diff --git a/.gitignore b/.gitignore index a09bb51..a83b78e 100644 --- a/.gitignore +++ b/.gitignore @@ -63,8 +63,8 @@ blogc*.html /tests/blogc/check_template_parser /tests/blogc-git-receiver/check_pre_receive_parser /tests/blogc-git-receiver/check_pre_receive.sh -/tests/blogc-git-receiver/check_post_receive /tests/blogc-git-receiver/check_post_receive.sh +/tests/blogc-git-receiver/check_settings /tests/blogc-git-receiver/check_shell_command_parser /tests/blogc-git-receiver/check_shell.sh /tests/blogc-make/check_atom diff --git a/Makefile.am b/Makefile.am index 986685e..7e25694 100644 --- a/Makefile.am +++ b/Makefile.am @@ -49,6 +49,7 @@ noinst_HEADERS = \ src/blogc-git-receiver/post-receive.h \ src/blogc-git-receiver/pre-receive.h \ src/blogc-git-receiver/pre-receive-parser.h \ + src/blogc-git-receiver/settings.h \ src/blogc-git-receiver/shell.h \ src/blogc-git-receiver/shell-command-parser.h \ src/blogc-make/atom.h \ @@ -197,6 +198,7 @@ libblogc_git_receiver_la_SOURCES = \ src/blogc-git-receiver/post-receive.c \ src/blogc-git-receiver/pre-receive.c \ src/blogc-git-receiver/pre-receive-parser.c \ + src/blogc-git-receiver/settings.c \ src/blogc-git-receiver/shell.c \ src/blogc-git-receiver/shell-command-parser.c \ $(NULL) @@ -725,23 +727,23 @@ check_PROGRAMS += \ if USE_LD_WRAP check_PROGRAMS += \ - tests/blogc-git-receiver/check_post_receive \ + tests/blogc-git-receiver/check_settings \ $(NULL) -tests_blogc_git_receiver_check_post_receive_SOURCES = \ - tests/blogc-git-receiver/check_post_receive.c \ +tests_blogc_git_receiver_check_settings_SOURCES = \ + tests/blogc-git-receiver/check_settings.c \ $(NULL) -tests_blogc_git_receiver_check_post_receive_CFLAGS = \ +tests_blogc_git_receiver_check_settings_CFLAGS = \ $(CMOCKA_CFLAGS) \ $(NULL) -tests_blogc_git_receiver_check_post_receive_LDFLAGS = \ +tests_blogc_git_receiver_check_settings_LDFLAGS = \ -no-install \ -Wl,--wrap=realpath \ $(NULL) -tests_blogc_git_receiver_check_post_receive_LDADD = \ +tests_blogc_git_receiver_check_settings_LDADD = \ $(CMOCKA_LIBS) \ libblogc_git_receiver.la \ libblogc_common.la \ diff --git a/src/blogc-git-receiver/post-receive.c b/src/blogc-git-receiver/post-receive.c index 93cae25..23d4dae 100644 --- a/src/blogc-git-receiver/post-receive.c +++ b/src/blogc-git-receiver/post-receive.c @@ -16,32 +16,8 @@ #include "../common/config-parser.h" #include "../common/error.h" #include "../common/file.h" - - -char* -bgr_post_receive_get_config_section(bc_config_t *config, const char *repo_path, - const char *home) -{ - char *rv = NULL; - char** sections = bc_config_list_sections(config); - for (size_t i = 0; sections[i] != NULL; i++) { - if (bc_str_starts_with(sections[i], "repo:")) { - char *tmp_repo = bc_strdup_printf("%s/repos/%s", home, sections[i] + 5); - char *real_tmp_repo = realpath(tmp_repo, NULL); // maybe not needed - free(tmp_repo); - if (real_tmp_repo == NULL) - continue; - if (0 == strcmp(real_tmp_repo, repo_path)) { - rv = bc_strdup(sections[i]); - free(real_tmp_repo); - break; - } - free(real_tmp_repo); - } - } - bc_strv_free(sections); - return rv; -} +#include "settings.h" +#include "post-receive.h" int @@ -76,56 +52,21 @@ bgr_post_receive_hook(int argc, char *argv[]) goto push; } - char *home = getenv("BLOGC_GIT_RECEIVER_BASEDIR"); - if (home == NULL) { - home = getenv("HOME"); - } - if (home == NULL) { - fprintf(stderr, "warning: failed to find user home path, " - "mirroring disabled\n"); - goto cleanup; - } - - char *config_file = bc_strdup_printf("%s/blogc-git-receiver.ini", home); - if ((0 != access(config_file, F_OK))) { + bc_config_t *config = bgr_settings_parse(); + if (config == NULL) { fprintf(stderr, "warning: repository mirroring disabled\n"); - free(config_file); - goto cleanup; - } - - size_t len; - bc_error_t *err = NULL; - char* config_content = bc_file_get_contents(config_file, true, &len, &err); - if (err != NULL) { - fprintf(stderr, "warning: failed to read configuration file (%s), " - "mirroring disabled: %s\n", config_file, err->msg); - bc_error_free(err); - free(config_file); - free(config_content); - goto cleanup; - } - - bc_config_t *config = bc_config_parse(config_content, len, NULL, &err); - free(config_content); - if (err != NULL) { - fprintf(stderr, "warning: failed to parse configuration file (%s), " - "mirroring disabled: %s\n", config_file, err->msg); - bc_error_free(err); - free(config_file); goto cleanup; } - free(config_file); - char *config_section = bgr_post_receive_get_config_section(config, repo_path, - home); - if (config_section == NULL) { + char *section = bgr_settings_get_section(config, repo_path); + if (section == NULL) { fprintf(stderr, "warning: repository mirroring disabled\n"); bc_config_free(config); goto cleanup; } - mirror = bc_strdup(bc_config_get(config, config_section, "mirror")); - free(config_section); + mirror = bc_strdup(bc_config_get(config, section, "mirror")); + free(section); bc_config_free(config); if (mirror == NULL) { diff --git a/src/blogc-git-receiver/post-receive.h b/src/blogc-git-receiver/post-receive.h index 97f7c82..3676541 100644 --- a/src/blogc-git-receiver/post-receive.h +++ b/src/blogc-git-receiver/post-receive.h @@ -9,10 +9,6 @@ #ifndef _POST_RECEIVE_H #define _POST_RECEIVE_H -#include "../common/config-parser.h" - -char* bgr_post_receive_get_config_section(bc_config_t *config, - const char *repo_path, const char *home); int bgr_post_receive_hook(int argc, char *argv[]); #endif /* _POST_RECEIVE_H */ diff --git a/src/blogc-git-receiver/pre-receive.c b/src/blogc-git-receiver/pre-receive.c index 20c6738..3668f8b 100644 --- a/src/blogc-git-receiver/pre-receive.c +++ b/src/blogc-git-receiver/pre-receive.c @@ -18,6 +18,7 @@ #include "../common/compat.h" #include "../common/utils.h" #include "../common/stdin.h" +#include "settings.h" #include "pre-receive-parser.h" #include "pre-receive.h" @@ -177,18 +178,15 @@ bgr_pre_receive_hook(int argc, char *argv[]) goto cleanup; } - char *home = getenv("BLOGC_GIT_RECEIVER_BASEDIR"); - if (home == NULL) { - home = getenv("HOME"); - } - if (home == NULL) { - fprintf(stderr, "error: failed to find user home path\n"); + const char *bd = bgr_settings_get_basedir(); + if (bd == NULL) { + fprintf(stderr, "error: failed to find user base directory path\n"); rv = 3; goto cleanup; } unsigned long epoch = time(NULL); - output_dir = bc_strdup_printf("%s/builds/%s-%lu", home, master, epoch); + output_dir = bc_strdup_printf("%s/builds/%s-%lu", bd, master, epoch); if (0 == access(output_dir, F_OK)) { char *tmp = output_dir; diff --git a/src/blogc-git-receiver/settings.c b/src/blogc-git-receiver/settings.c new file mode 100644 index 0000000..514fcdf --- /dev/null +++ b/src/blogc-git-receiver/settings.c @@ -0,0 +1,99 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2014-2017 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#include +#include +#include +#include +#include +#include +#include "../common/utils.h" +#include "../common/config-parser.h" +#include "../common/error.h" +#include "../common/file.h" +#include "settings.h" + + +const char* +bgr_settings_get_basedir(void) +{ + char *rv = getenv("BLOGC_GIT_RECEIVER_BASEDIR"); + if (rv != NULL) { + return rv; + } + return getenv("HOME"); +} + + +char* +bgr_settings_get_section(bc_config_t *config, const char *repo_path) +{ + const char *bd = bgr_settings_get_basedir(); + if (bd == NULL) { + return NULL; + } + char *rv = NULL; + char** sections = bc_config_list_sections(config); + for (size_t i = 0; sections[i] != NULL; i++) { + if (bc_str_starts_with(sections[i], "repo:")) { + char *tmp_repo = bc_strdup_printf("%s/repos/%s", bd, sections[i] + 5); + char *real_tmp_repo = realpath(tmp_repo, NULL); // maybe not needed + free(tmp_repo); + if (real_tmp_repo == NULL) + continue; + if (0 == strcmp(real_tmp_repo, repo_path)) { + rv = bc_strdup(sections[i]); + free(real_tmp_repo); + break; + } + free(real_tmp_repo); + } + } + bc_strv_free(sections); + return rv; +} + + +bc_config_t* +bgr_settings_parse(void) +{ + const char *bd = bgr_settings_get_basedir(); + if (bd == NULL) { + return NULL; + } + char *config_file = bc_strdup_printf("%s/blogc-git-receiver.ini", bd); + if ((0 != access(config_file, F_OK))) { + free(config_file); + return NULL; + } + + size_t len; + bc_error_t *err = NULL; + char* config_content = bc_file_get_contents(config_file, true, &len, &err); + if (err != NULL) { + fprintf(stderr, "warning: failed to read configuration file (%s): %s\n", + config_file, err->msg); + bc_error_free(err); + free(config_file); + free(config_content); + return NULL; + } + + bc_config_t *config = bc_config_parse(config_content, len, NULL, &err); + free(config_content); + if (err != NULL) { + fprintf(stderr, "warning: failed to parse configuration file (%s): %s\n", + config_file, err->msg); + bc_error_free(err); + free(config_file); + return NULL; + } + free(config_file); + + return config; +} diff --git a/src/blogc-git-receiver/settings.h b/src/blogc-git-receiver/settings.h new file mode 100644 index 0000000..1b89d0f --- /dev/null +++ b/src/blogc-git-receiver/settings.h @@ -0,0 +1,18 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2014-2017 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#ifndef _SETTINGS_H +#define _SETTINGS_H + +#include "../common/config-parser.h" + +const char* bgr_settings_get_basedir(void); +char* bgr_settings_get_section(bc_config_t *config, const char *repo_path); +bc_config_t* bgr_settings_parse(void); + +#endif /* _SETTINGS_H */ diff --git a/src/blogc-git-receiver/shell.c b/src/blogc-git-receiver/shell.c index eb859e8..f2eaf32 100644 --- a/src/blogc-git-receiver/shell.c +++ b/src/blogc-git-receiver/shell.c @@ -14,6 +14,7 @@ #include #include #include "../common/utils.h" +#include "settings.h" #include "shell-command-parser.h" #include "shell.h" @@ -34,13 +35,10 @@ bgr_shell(int argc, char *argv[]) goto cleanup; } - // get home path - char *home = getenv("BLOGC_GIT_RECEIVER_BASEDIR"); - if (home == NULL) { - home = getenv("HOME"); - } - if (home == NULL) { - fprintf(stderr, "error: failed to find user home path\n"); + // get base dir path + const char *bd = bgr_settings_get_basedir(); + if (bd == NULL) { + fprintf(stderr, "error: failed to find base directory path\n"); rv = 3; goto cleanup; } @@ -53,7 +51,7 @@ bgr_shell(int argc, char *argv[]) goto cleanup; } - repo = bc_strdup_printf("%s/repos/%s", home, tmp_repo); + repo = bc_strdup_printf("%s/repos/%s", bd, tmp_repo); quoted_repo = bc_shell_quote(repo); free(tmp_repo); @@ -132,9 +130,8 @@ bgr_shell(int argc, char *argv[]) git_exec: - if (0 != chdir(home)) { - fprintf(stderr, "error: failed to chdir (%s): %s\n", home, - strerror(errno)); + if (0 != chdir(bd)) { + fprintf(stderr, "error: failed to chdir (%s): %s\n", bd, strerror(errno)); rv = 3; goto cleanup; } diff --git a/tests/blogc-git-receiver/check_post_receive.c b/tests/blogc-git-receiver/check_post_receive.c deleted file mode 100644 index 5d2d742..0000000 --- a/tests/blogc-git-receiver/check_post_receive.c +++ /dev/null @@ -1,91 +0,0 @@ -/* - * blogc: A blog compiler. - * Copyright (C) 2014-2017 Rafael G. Martins - * - * This program can be distributed under the terms of the BSD License. - * See the file LICENSE. - */ - -#include -#include -#include -#include -#include -#include -#include "../../src/common/config-parser.h" -#include "../../src/common/utils.h" -#include "../../src/blogc-git-receiver/post-receive.h" - - -char* -__wrap_realpath(const char *path, char *resolved_path) -{ - const char *real_path = mock_type(const char*); - if (real_path == NULL) - return NULL; - assert_string_equal(path, real_path); - return bc_strdup(real_path); -} - - -static void -test_post_receive_get_config_section(void **state) -{ - bc_error_t *err = NULL; - - bc_config_t *config = bc_config_parse("", 0, NULL, &err); - assert_null(err); - assert_null(bgr_post_receive_get_config_section(config, - "/home/blogc/repos/foo.git", "/home/blogc")); - bc_config_free(config); - - will_return(__wrap_realpath, NULL); - will_return(__wrap_realpath, "/home/blogc/repos/bar.git"); - const char *conf = - "[repo:foo.git]\n" - "mirror = foo\n" - "\n" - "[repo:bar.git]\n" - "mirror = bar\n" - "\n" - "[repo:baz.git]\n" - "mirror = baz\n" - "\n"; - config = bc_config_parse(conf, strlen(conf), NULL, &err); - assert_null(err); - char *s = bgr_post_receive_get_config_section(config, - "/home/blogc/repos/bar.git", "/home/blogc"); - assert_string_equal(s, "repo:bar.git"); - free(s); - bc_config_free(config); - - will_return(__wrap_realpath, NULL); - will_return(__wrap_realpath, "/home/blogc/repos/asd/bar.git"); - conf = - "[repo:asd/foo.git]\n" - "mirror = foo\n" - "\n" - "[repo:asd/bar.git]\n" - "mirror = bar\n" - "\n" - "[repo:asd/baz.git]\n" - "mirror = baz\n" - "\n"; - config = bc_config_parse(conf, strlen(conf), NULL, &err); - assert_null(err); - s = bgr_post_receive_get_config_section(config, - "/home/blogc/repos/asd/bar.git", "/home/blogc"); - assert_string_equal(s, "repo:asd/bar.git"); - free(s); - bc_config_free(config); -} - - -int -main(void) -{ - const UnitTest tests[] = { - unit_test(test_post_receive_get_config_section), - }; - return run_tests(tests); -} diff --git a/tests/blogc-git-receiver/check_settings.c b/tests/blogc-git-receiver/check_settings.c new file mode 100644 index 0000000..63caa0c --- /dev/null +++ b/tests/blogc-git-receiver/check_settings.c @@ -0,0 +1,91 @@ +/* + * blogc: A blog compiler. + * Copyright (C) 2014-2017 Rafael G. Martins + * + * This program can be distributed under the terms of the BSD License. + * See the file LICENSE. + */ + +#include +#include +#include +#include +#include +#include +#include "../../src/common/config-parser.h" +#include "../../src/common/utils.h" +#include "../../src/blogc-git-receiver/settings.h" + + +char* +__wrap_realpath(const char *path, char *resolved_path) +{ + const char *real_path = mock_type(const char*); + if (real_path == NULL) + return NULL; + assert_string_equal(path, real_path); + return bc_strdup(real_path); +} + + +static void +test_settings_get_section(void **state) +{ + bc_error_t *err = NULL; + + setenv("HOME", "/home/blogc", 1); + + bc_config_t *config = bc_config_parse("", 0, NULL, &err); + assert_null(err); + assert_null(bgr_settings_get_section(config, "/home/blogc/repos/foo.git")); + bc_config_free(config); + + will_return(__wrap_realpath, NULL); + will_return(__wrap_realpath, "/home/blogc/repos/bar.git"); + const char *conf = + "[repo:foo.git]\n" + "mirror = foo\n" + "\n" + "[repo:bar.git]\n" + "mirror = bar\n" + "\n" + "[repo:baz.git]\n" + "mirror = baz\n" + "\n"; + config = bc_config_parse(conf, strlen(conf), NULL, &err); + assert_null(err); + char *s = bgr_settings_get_section(config, "/home/blogc/repos/bar.git"); + assert_string_equal(s, "repo:bar.git"); + free(s); + bc_config_free(config); + + setenv("BLOGC_GIT_RECEIVER_BASEDIR", "/home/bola", 1); + will_return(__wrap_realpath, NULL); + will_return(__wrap_realpath, "/home/bola/repos/asd/bar.git"); + conf = + "[repo:asd/foo.git]\n" + "mirror = foo\n" + "\n" + "[repo:asd/bar.git]\n" + "mirror = bar\n" + "\n" + "[repo:asd/baz.git]\n" + "mirror = baz\n" + "\n"; + config = bc_config_parse(conf, strlen(conf), NULL, &err); + assert_null(err); + s = bgr_settings_get_section(config, "/home/bola/repos/asd/bar.git"); + assert_string_equal(s, "repo:asd/bar.git"); + free(s); + bc_config_free(config); +} + + +int +main(void) +{ + const UnitTest tests[] = { + unit_test(test_settings_get_section), + }; + return run_tests(tests); +} -- cgit v1.2.3-18-g5258