diff options
| author | dacctal <donotcontactmevia@email.invalid> | 2026-06-25 21:19:44 +0000 |
|---|---|---|
| committer | dacctal <donotcontactmevia@email.invalid> | 2026-06-25 21:19:44 +0000 |
| commit | d64dc64740b7705c64c0ddbff3f989669c016955 (patch) | |
| tree | cc76e02e9daf9127251f9794cc36de2f38288d48 /src | |
| parent | 724c40efd999597c0e3262181b73e5ff3bbf705f (diff) | |
FCC: improve fetch_git w/ null checks & error handling1.2.0
Diffstat (limited to 'src')
| -rw-r--r-- | src/fetch_git.c | 92 |
1 files changed, 45 insertions, 47 deletions
diff --git a/src/fetch_git.c b/src/fetch_git.c index 570cb1d..8784a03 100644 --- a/src/fetch_git.c +++ b/src/fetch_git.c @@ -1,4 +1,3 @@ - /* pkgit - package it! @@ -18,7 +17,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ - #include <fcntl.h> #include <stdio.h> #include <string.h> @@ -29,56 +27,56 @@ #include "vars.h" int fetch_git(Pkg pkg) { - if (pkg.url == NULL || pkg.src == NULL) { - fprintf(stderr, "%s invalid pkg: url or src is NULL\n", print_warning); - return -1; + if (pkg.url == NULL || pkg.src == NULL) { + fprintf(stderr, "%s invalid pkg: url or src is NULL\n", print_warning); + return -1; + } + + pid_t pid = fork(); + if (pid < 0) { + perror("fork failed"); + return -1; + } + + if (pid == 0) { + if (!is_verbose) { + int nullfd = open("/dev/null", O_WRONLY); + if (nullfd >= 0) { + dup2(nullfd, STDOUT_FILENO); + dup2(nullfd, STDERR_FILENO); + close(nullfd); + } } - pid_t pid = fork(); - if (pid < 0) { - perror("fork failed"); - return -1; - } + const char *argv[12]; + int i = 0; + + argv[i++] = "git"; + argv[i++] = "-c"; + argv[i++] = "advice.detachedHead=false"; + argv[i++] = "clone"; - if (pid == 0) { - if (!is_verbose) { - int nullfd = open("/dev/null", O_WRONLY); - if (nullfd >= 0) { - dup2(nullfd, STDOUT_FILENO); - dup2(nullfd, STDERR_FILENO); - close(nullfd); - } - } - - const char *argv[12]; - int i = 0; - - argv[i++] = "git"; - argv[i++] = "-c"; - argv[i++] = "advice.detachedHead=false"; - argv[i++] = "clone"; - - if (pkg.ver != NULL && strcmp(pkg.ver, "HEAD") != 0) { - argv[i++] = "--branch"; - argv[i++] = pkg.ver; - } - - argv[i++] = "--recursive"; - argv[i++] = pkg.url; - argv[i++] = pkg.src; - argv[i] = NULL; - - execvp("git", (char *const *)argv); - _exit(127); + if (pkg.ver != NULL && strcmp(pkg.ver, "HEAD") != 0) { + argv[i++] = "--branch"; + argv[i++] = pkg.ver; } - int status; - waitpid(pid, &status, 0); + argv[i++] = "--recursive"; + argv[i++] = pkg.url; + argv[i++] = pkg.src; + argv[i] = NULL; - int result = WIFEXITED(status) ? WEXITSTATUS(status) : -1; - if (result != 0) { - printf("%s git clone failed: %d\n", print_warning, result); - } + execvp("git", (char *const *)argv); + _exit(127); + } + + int status; + waitpid(pid, &status, 0); + + int result = WIFEXITED(status) ? WEXITSTATUS(status) : -1; + if (result != 0) { + printf("%s git clone failed: %d\n", print_warning, result); + } - return result; + return result; } |
