summaryrefslogtreecommitdiffstats
path: root/aports-cache.c
blob: 303239a4f8ece5673f8307cbd3bbd679bce40473 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>

#define _GNU_SOURCE

#include <err.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <spawn.h>

#ifdef DEBUG
#define debug_printf(args...) fprintf(stderr, ## args)
#else
#define debug_printf(args...)
#endif

#if defined(__APPLE__)
#define st_mtim	st_mtimespec
#endif

static int spawn_shell_pipe(char *const argv[], pid_t *pid, int outfd)
{
	int pipefd[2];
	posix_spawn_file_actions_t factions;

	if (pipe(pipefd) < 0) {
		warn("pipe");
		return -1;
	}

	if (posix_spawn_file_actions_init(&factions) < 0) {
		warn("posix_spawn_file_actions_init");
		goto err1;
	}

	if (outfd >=0 && posix_spawn_file_actions_adddup2(&factions, outfd, STDOUT_FILENO) < 0) {
		warn("posix_spawn_file_actions_adddup2");
		goto err2;
	}

	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) {
		warn("%s", argv[0]);
		goto err2;
	}

	close(pipefd[0]);
	return pipefd[1];

err2:
	posix_spawn_file_actions_destroy(&factions);
err1:
	close(pipefd[0]);
	close(pipefd[1]);
	return -1;
}

static int read_apkbuild(int shellfd, size_t size, const char *name)
{
#define ECHO_SEPARATOR	"echo \"-\"\n"
#define ECHO_STR(var)	"[ -n \"$" #var "\" ] && echo \" " #var ": $" #var "\"\n"
#define ECHO_LIST(var)	"[ -n \"$" #var "\" ] && echo ' " #var ":' && " \
			"for i in $" #var "; do\n" \
			"\techo \"  - \\\"$i\\\"\"\n" \
			"done\n"

	char *fmtbuf =
		"unset pkgname"
			" pkgver"
			" pkgrel"
			" arch"
			" options"
			" depends"
			" makedepends"
			" checkdepends"
			" subpackages"
			" source"
			" license"
			" url"
			"\n"
		"cd %s\n"
		"aports_cache_pwd=\"$PWD\"\n"

		". ./APKBUILD >/dev/null\n"

		"if [ \"$PWD\" != \"$aports_cache_pwd\" ]; then\n"
		"	echo \"%s: PWD changed\" >&2\n"
		"	exit 1\n"
		"fi\n"

		ECHO_SEPARATOR
		ECHO_STR(pkgname)
		ECHO_STR(pkgver)
		ECHO_LIST(arch)
		ECHO_LIST(options)
		ECHO_LIST(depends)
		ECHO_LIST(makedepends)
		ECHO_LIST(checkdepends)
		ECHO_LIST(subpackages)
		ECHO_LIST(linuguas)
		ECHO_LIST(source)
		ECHO_STR(url)
		ECHO_STR(license)
		"cd ..\n\n";
	char buf[strlen(fmtbuf) + 512];
	size_t bufsize = snprintf(buf, sizeof(buf), fmtbuf, name, name);
	ssize_t r, offs=0;

	while (1) {
		r = write(shellfd, &buf[offs], bufsize - offs);
		if (r < 0) {
			warn("write to shell pipe");
			return -1;
		}

		offs += r;
		if (r == 0 || offs == bufsize)
			break;
	}

	return 0;
}

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;
}

static int cache_refresh_or_check(int dirfd, const char *cachefile, int shellfd)
{
	struct stat cache;
	DIR *dir;
	struct dirent *ent;
	int r = 0;
	int dirfd2 = dup(dirfd);

	if (shellfd == -1 && fstatat(dirfd, cachefile, &cache, 0) == -1)
		return 1;

	dir = fdopendir(dirfd2);
	if (dir == NULL)
		return -1;
	
	while(1) {
		char buf[PATH_MAX];
		struct stat apkbuild;

		errno = 0;
		ent = readdir(dir);
		if (ent == NULL) {
			if (errno != 0) {
				warn("readdir");
				closedir(dir);
				return -1;
			}
			break;
		}

		if (ent->d_name[0] == '.')
			continue;

		snprintf(buf, sizeof(buf), "%s/APKBUILD", ent->d_name);

		if (fstatat(dirfd, buf, &apkbuild, 0) == -1) {
			warn("%s", buf);
			continue;
		}

		if (shellfd != -1) {
			read_apkbuild(shellfd, apkbuild.st_size, ent->d_name);
			continue;
		}

		if (is_newer(apkbuild.st_mtim, cache.st_mtim)) {
			debug_printf("modified: %s\n", buf);
			r = 1;
			break;
		}
	}

	closedir(dir);
	return r;
}

int aports_cache_check(int dirfd, const char *cachefile)
{
	return cache_refresh_or_check(dirfd, cachefile, -1);
}

int aports_cache_refresh(int dirfd, const char *cachefile, char *const shell_argv[])
{
	pid_t shell_pid;
	int shellfd, outfd = -1;
	int status;

	if (cachefile) {
		outfd = open(cachefile, O_WRONLY | O_CREAT, 0660);
		if (outfd < 0) {
			warn("%s", cachefile);
			return -1;
		}
	}

	shellfd = spawn_shell_pipe(shell_argv, &shell_pid, outfd);
	debug_printf("shell pid: %d\n", shell_pid);

	if (outfd >= 0)
		close(outfd);

	cache_refresh_or_check(dirfd, cachefile, shellfd);
	write(shellfd, "exit 0\n", 7);

	if (waitpid(shell_pid, &status, 0) < 0) {
		warn("waitpid");
		return -1;
	}

	return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
}