summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNatanael Copa <ncopa@alpinelinux.org>2017-06-17 18:30:12 +0200
committerNatanael Copa <ncopa@alpinelinux.org>2017-06-17 23:44:22 +0200
commitba7822e19ca358ef8a75b20d1774e277b4551c4e (patch)
treed876462771ebf2c7c0281389d290e90faa867b0b
parent48e74865011722a7ac0ca3e1b45ea407f8c79db5 (diff)
downloadaports-cache-ba7822e19ca358ef8a75b20d1774e277b4551c4e.tar.bz2
aports-cache-ba7822e19ca358ef8a75b20d1774e277b4551c4e.tar.xz
move out main function to its own file
-rw-r--r--Makefile4
-rw-r--r--aports-cache.c118
-rw-r--r--main.c91
3 files changed, 135 insertions, 78 deletions
diff --git a/Makefile b/Makefile
index f1f231a..7fe606e 100644
--- a/Makefile
+++ b/Makefile
@@ -10,5 +10,5 @@ aports-cache-objs := aports-cache.o
%.o: %.c
$(CC) $(CFLAGS) $(CFLAGS_$(notdir $@)) -c -o $@ $<
-aports-cache: aports-cache.o
- $(CC) $(LDFLAGS) -o $@ $(LIBS_$(notdir $@)) $<
+aports-cache: aports-cache.o main.o
+ $(CC) $(LDFLAGS) -o $@ $(LIBS_$(notdir $@)) $^
diff --git a/aports-cache.c b/aports-cache.c
index c14860a..02899e0 100644
--- a/aports-cache.c
+++ b/aports-cache.c
@@ -4,11 +4,10 @@
#define _GNU_SOURCE
+#include <err.h>
#include <errno.h>
#include <dirent.h>
-#include <err.h>
#include <fcntl.h>
-#include <getopt.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
@@ -22,29 +21,43 @@
#endif
-int spawn_shell_pipe(char *const argv[], pid_t *pid)
+static int spawn_shell_pipe(char *const argv[], pid_t *pid)
{
int pipefd[2];
posix_spawn_file_actions_t factions;
- if (pipe(pipefd) < 0)
- err(1, "pipe");
+ if (pipe(pipefd) < 0) {
+ warn("pipe");
+ return -1;
+ }
- if (posix_spawn_file_actions_init(&factions) < 0)
- err(1, "posix_spawn_file_actions_init");
+ if (posix_spawn_file_actions_init(&factions) < 0) {
+ warn("posix_spawn_file_actions_init");
+ goto err1;
+ }
- if (posix_spawn_file_actions_adddup2(&factions, pipefd[0], 0) < 0)
- err(1, "posix_spawn_file_actions_adddup2");
+ if (posix_spawn_file_actions_adddup2(&factions, pipefd[0], 0) < 0) {
+ warn("posix_spawn_file_actions_adddup2");
+ goto err2;
+ }
- if (posix_spawn(pid, argv[0], &factions, NULL, argv, NULL) < 0)
- err(1, "posiz_spawn");
- close(pipefd[0]);
+ if (posix_spawn(pid, argv[0], &factions, NULL, argv, NULL) < 0) {
+ warn("%s", argv[0]);
+ goto err2;
+ }
- debug_printf("shellfd=%i\n", pipefd[1]);
+ close(pipefd[0]);
return pipefd[1];
+
+err2:
+ posix_spawn_file_actions_destroy(&factions);
+err1:
+ close(pipefd[0]);
+ close(pipefd[1]);
+ return -1;
}
-int read_apkbuild(int shellfd, int aportfd, size_t size, const char *name)
+static int read_apkbuild(int shellfd, size_t size, const char *name)
{
#define ECHO_STR(var) "[ -n \"$" #var "\" ] && echo \" " #var ": $" #var "\"\n"
#define ECHO_LIST(var) "[ -n \"$" #var "\" ] && echo ' " #var ":' && " \
@@ -96,8 +109,11 @@ int read_apkbuild(int shellfd, int aportfd, size_t size, const char *name)
while (1) {
r = write(shellfd, &buf[offs], bufsize - offs);
- if (r < 0)
- err(1, "shell pipe");
+ if (r < 0) {
+ warn("write to shell pipe");
+ return -1;
+ }
+
offs += r;
if (r == 0 || offs == bufsize)
break;
@@ -106,12 +122,12 @@ int read_apkbuild(int shellfd, int aportfd, size_t size, const char *name)
return 0;
}
-int is_newer(struct timespec a, struct timespec b)
+static int is_newer(struct timespec a, struct timespec b)
{
return (a.tv_sec == b.tv_sec) ? a.tv_nsec > b.tv_nsec : a.tv_sec > b.tv_sec;
}
-int cache_refresh_or_check(int dirfd, const char *cachefile, int shellfd)
+static int cache_refresh_or_check(int dirfd, const char *cachefile, int shellfd)
{
struct stat cache;
DIR *dir;
@@ -119,7 +135,6 @@ int cache_refresh_or_check(int dirfd, const char *cachefile, int shellfd)
int r = 0;
int dirfd2 = dup(dirfd);
- debug_printf("shellfd(check)||:=%i\n", shellfd);
if (shellfd == -1 && fstatat(dirfd, cachefile, &cache, 0) == -1)
return 1;
@@ -135,8 +150,11 @@ int cache_refresh_or_check(int dirfd, const char *cachefile, int shellfd)
errno = 0;
ent = readdir(dir);
if (ent == NULL) {
- if (errno != 0)
- err(1, "readdir");
+ if (errno != 0) {
+ warn("readdir");
+ closedir(dir);
+ return -1;
+ }
break;
}
@@ -151,11 +169,7 @@ int cache_refresh_or_check(int dirfd, const char *cachefile, int shellfd)
}
if (shellfd != -1) {
- int fd = openat(dirfd, buf, O_RDONLY);
- if (fd == -1)
- err(1, "%s", buf);
- read_apkbuild(shellfd, fd, apkbuild.st_size, ent->d_name);
- close(fd);
+ read_apkbuild(shellfd, apkbuild.st_size, ent->d_name);
continue;
}
@@ -170,12 +184,12 @@ int cache_refresh_or_check(int dirfd, const char *cachefile, int shellfd)
return r;
}
-int cache_check(int dirfd, const char *cachefile)
+int aports_cache_check(int dirfd, const char *cachefile)
{
return cache_refresh_or_check(dirfd, cachefile, -1);
}
-int cache_refresh(int dirfd, const char *cachefile, char *const shell_argv[])
+int aports_cache_refresh(int dirfd, const char *cachefile, char *const shell_argv[])
{
pid_t shell_pid;
int shellfd = spawn_shell_pipe(shell_argv, &shell_pid);
@@ -186,51 +200,3 @@ int cache_refresh(int dirfd, const char *cachefile, char *const shell_argv[])
return 0;
}
-int main(int argc, char *argv[])
-{
- static struct option opts[] = {
- {"verbose", no_argument, 0, 'v' },
- {"force", no_argument, 0, 'f' },
- {"shell", required_argument, 0, 's' },
- { 0, 0, 0, 0}
- };
- int i, c, verbose=0;
- const char *cachefile = ".aports.cache.yaml";
- char dirpath_buf[PATH_MAX];
- char *dirpath = dirpath_buf;
- int dirfd;
- char *shell_argv[] = {"/bin/sh", NULL};
-
- while ((c = getopt_long(argc, argv, "fs:v", opts, &i)) != -1) {
- switch(c) {
- case 's':
- shell_argv[0] = optarg;
- break;
- case 'v':
- verbose = 1;
- break;
- }
- }
-
- if (optind < argc) {
- dirpath = argv[optind];
- } else {
- dirpath = getcwd(dirpath_buf, sizeof(dirpath_buf));
- }
-
- dirfd = open(dirpath, O_DIRECTORY);
- if (dirfd < 0)
- err(1, "%s", dirpath);
- fchdir(dirfd);
-
- if (!cache_check(dirfd, cachefile))
- return 0;
-
- if (verbose)
- printf("updating cache for %s\n", dirpath);
- cache_refresh(dirfd, cachefile, shell_argv);
- close(dirfd);
-
- return 0;
-}
-
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..23de13b
--- /dev/null
+++ b/main.c
@@ -0,0 +1,91 @@
+
+#define _GNU_SOURCE
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <getopt.h>
+#include <limits.h>
+#include <unistd.h>
+#include <stdio.h>
+
+#include "aports-cache.h"
+
+static void usage(int exitcode)
+{
+ printf("usage %s [OPTS] DIR\n"
+ "options:\n"
+ " -h,--help show this help\n"
+ " -c,--check only check if cache is updated\n"
+ " -u,--update force cache update\n"
+ " -v,--verbose enable verbose mode\n"
+ " -s,--shell PROG use PROG as shell instead of /bin/sh\n"
+ "\n", program_invocation_name);
+}
+
+int main(int argc, char *argv[])
+{
+ static struct option opts[] = {
+ {"check", no_argument, 0, 'c' },
+ {"help", no_argument, 0, 'h' },
+
+ {"verbose", no_argument, 0, 'v' },
+ {"update", no_argument, 0, 'u' },
+ {"shell", required_argument, 0, 's' },
+ { 0, 0, 0, 0}
+ };
+ int i, c, verbose=0, check_only=0, force_update=0;
+ const char *cachefile = ".aports.cache.yaml";
+ char dirpath_buf[PATH_MAX];
+ char *dirpath = dirpath_buf;
+ int dirfd;
+
+ char *shell_argv[] = {"/bin/sh", NULL};
+
+ while ((c = getopt_long(argc, argv, "fs:v", opts, &i)) != -1) {
+ switch(c) {
+ case 'c':
+ check_only = 1;
+ break;
+ case 'h':
+ usage(0);
+ break;
+ case 's':
+ shell_argv[0] = optarg;
+ break;
+ case 'u':
+ force_update = 1;
+ break;
+ case 'v':
+ verbose = 1;
+ break;
+ }
+ }
+
+ if (optind < argc) {
+ dirpath = argv[optind];
+ } else {
+ dirpath = getcwd(dirpath_buf, sizeof(dirpath_buf));
+ }
+
+ dirfd = open(dirpath, O_DIRECTORY);
+ if (dirfd < 0)
+ err(1, "%s", dirpath);
+ fchdir(dirfd);
+
+ if (!force_update)
+ c =aports_cache_check(dirfd, cachefile);
+
+ if (check_only || !c) {
+ if (verbose)
+ printf("cache is %sup to date\n", c ? "not " : "");
+ return c;
+ }
+
+ if (verbose)
+ printf("updating cache for %s\n", dirpath);
+ aports_cache_refresh(dirfd, cachefile, shell_argv);
+ close(dirfd);
+
+ return 0;
+}
+