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
|
/*
* download.c - source file downloader
* https://rawnix.org
*
* Copyright (c) 2025 zorz@gmx.com
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "mkpkg.h"
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
static int is_url(const char *s)
{
return str_starts_with(s, "http://") ||
str_starts_with(s, "https://") ||
str_starts_with(s, "ftp://");
}
static const char *url_basename(const char *url)
{
const char *slash = strrchr(url, '/');
return (slash && slash[1]) ? slash + 1 : "download";
}
static void format_bytes(char *buf, size_t bufsz, long long bytes)
{
if (bytes >= (long long)1024 * 1024 * 1024)
snprintf(buf, bufsz, "%.2f GB",
(double)bytes / (1024.0 * 1024.0 * 1024.0));
else if (bytes >= 1024 * 1024)
snprintf(buf, bufsz, "%.2f MB",
(double)bytes / (1024.0 * 1024.0));
else if (bytes >= 1024)
snprintf(buf, bufsz, "%.1f KB", (double)bytes / 1024.0);
else
snprintf(buf, bufsz, "%lld B", bytes);
}
/* ── per-download job ────────────────────────────────────────────────── */
typedef struct {
pid_t pid;
char dest[PATH_MAX_LEN];
char url[PATH_MAX_LEN];
int done;
int status;
} job_t;
/*
* Spawn wget for a single download.
* stdout and stderr are silenced — mkpkg owns all terminal output.
*/
static pid_t spawn_wget(const char *url, const char *dest)
{
pid_t pid = fork();
if (pid < 0)
err(E_DOWNLOAD, "fork");
if (pid == 0) {
int devnull = open("/dev/null", O_WRONLY);
if (devnull >= 0) {
dup2(devnull, STDOUT_FILENO);
dup2(devnull, STDERR_FILENO);
close(devnull);
}
execlp("wget", "wget",
"--quiet",
"-O", dest,
url,
(char *)NULL);
_exit(E_DOWNLOAD);
}
return pid;
}
/*
* Poll all jobs with WNOHANG, print a spinner + total bytes progress.
* Returns number of failed downloads.
*/
static int wait_with_progress(job_t *jobs, int njobs)
{
static const char *spin[] = {
"⠋","⠙","⠹","⠸","⠼","⠴","⠦","⠧","⠇","⠏"
};
int remaining = njobs;
int failed = 0;
int spin_idx = 0;
while (remaining > 0) {
int i;
for (i = 0; i < njobs; i++) {
int st;
if (jobs[i].done) continue;
if (waitpid(jobs[i].pid, &st, WNOHANG) == jobs[i].pid) {
jobs[i].done = 1;
jobs[i].status = st;
remaining--;
if (!WIFEXITED(st) || WEXITSTATUS(st) != 0) {
failed++;
unlink(jobs[i].dest);
}
}
}
{
long long total = 0;
int active = 0;
struct stat st;
for (i = 0; i < njobs; i++) {
if (stat(jobs[i].dest, &st) == 0) {
total += st.st_size;
if (!jobs[i].done) active++;
}
}
char bytes_str[32];
format_bytes(bytes_str, sizeof(bytes_str), total);
fprintf(stderr, "\r %s %s (%d/%d active) ",
spin[spin_idx % 10], bytes_str, active, njobs);
fflush(stderr);
spin_idx++;
}
if (remaining > 0) {
struct timespec ts = { 0, 250000000L }; /* 250 ms */
nanosleep(&ts, NULL);
}
}
fprintf(stderr, "\r%60s\r", "");
fflush(stderr);
return failed;
}
/* ── download_sources ────────────────────────────────────────────────── */
int download_sources(pkgfile_t *pkgfile, config_t *config)
{
/* heap-allocated job array — avoids ~2 MB stack frame */
job_t *jobs = xmalloc(MAX_SOURCES * sizeof(job_t));
int njobs = 0;
int i, failed;
for (i = 0; i < pkgfile->source_count; i++) {
source_t *src = &pkgfile->sources[i];
const char *url = src->url;
char dest[PATH_MAX_LEN];
if (src->rename && strcmp(src->rename, "SKIP") == 0)
continue;
if (!is_url(url)) {
/* local file — verify it exists */
char local[PATH_MAX_LEN];
snprintf(local, sizeof(local), "%s/%s",
config->root_dir, url);
if (access(local, F_OK) != 0)
errx(E_DOWNLOAD, "source not found: %s", url);
continue;
}
/* determine destination filename */
if (src->rename && src->rename[0]) {
snprintf(dest, sizeof(dest), "%s/%s",
config->source_dir, src->rename);
} else {
snprintf(dest, sizeof(dest), "%s/%s",
config->source_dir, url_basename(url));
}
if (access(dest, F_OK) == 0) {
info("already exists: %s", dest);
continue;
}
info("downloading %s", url);
jobs[njobs].done = 0;
jobs[njobs].status = 0;
jobs[njobs].pid = spawn_wget(url, dest);
snprintf(jobs[njobs].dest, sizeof(jobs[njobs].dest), "%s", dest);
snprintf(jobs[njobs].url, sizeof(jobs[njobs].url), "%s", url);
njobs++;
}
if (njobs == 0) {
free(jobs);
return 0;
}
failed = wait_with_progress(jobs, njobs);
for (i = 0; i < njobs; i++) {
if (jobs[i].done &&
(!WIFEXITED(jobs[i].status) ||
WEXITSTATUS(jobs[i].status) != 0))
warning("download failed: %s", jobs[i].url);
}
free(jobs);
if (failed)
errx(E_DOWNLOAD, "%d download(s) failed", failed);
info("downloaded %d file%s", njobs, njobs == 1 ? "" : "s");
return 0;
}
|