summaryrefslogtreecommitdiffstats
path: root/main.c
blob: 4ee7aa86dbffda5cb6411fea3a95bf0211a6e2d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

#define _GNU_SOURCE
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "aports-cache.h"

static char *program_invocation_name;

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);
	exit(exitcode);
}

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' },
		{"stdout",	no_argument,	0,	0x0101},
		{ 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, rc;

	char *shell_argv[] = {"/bin/sh",  NULL};
	program_invocation_name = argv[0];


	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 0x0101:
			cachefile=NULL;
			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);
	rc = aports_cache_refresh(dirfd, cachefile, shell_argv);
	close(dirfd);

	return rc;
}