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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
/*-
* Copyright (c) 1996
* Jean-Marc Zucconi
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* $FreeBSD$ */
#include <sys/types.h>
#include <err.h>
#include <errno.h>
#include <limits.h> /* needed for INT_MAX */
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include <sys/param.h> /* for MAXHOSTNAMELEN */
#include <sys/time.h> /* for struct timeval, gettimeofday */
#include "fetch.h"
static struct fetch_state clean_fetch_state;
static sigjmp_buf sigbuf;
static int get(struct fetch_state *volatile fs);
static void
usage(void)
{
fprintf(stderr,
"usage: fetch [-ADHILMNPRTVablFmnpqrstv] [-o outputfile] "
"[-S bytes]\n"
" [-f file -h host [-c dir] | URL]\n");
exit(EX_USAGE);
}
int
main(int argc, char *const *argv)
{
int c;
char *ep;
struct fetch_state fs;
const char *change_to_dir, *file_to_get, *hostname;
int error, rv;
unsigned long l;
init_schemes();
fs = clean_fetch_state;
fs.fs_verbose = 1;
fs.fs_reportsize = 0;
fs.fs_expectedsize = -1;
change_to_dir = file_to_get = hostname = 0;
#define OPT_STRING "Aabc:D:Ff:h:HIlLmMnNo:pPqRrS:stT:vV:"
while ((c = getopt(argc, argv, OPT_STRING)) != -1) {
switch (c) {
case 'A':
fs.fs_auto_retry = -1;
break;
case 'D': case 'H': case 'I': case 'L': case 'N': case 'V':
break; /* ncftp compatibility */
case 'F':
fs.fs_forcerestart = 1;
break;
case 'a':
fs.fs_auto_retry = 1;
break;
case 'b':
fs.fs_linux_bug = 1;
break;
case 'c':
change_to_dir = optarg;
break;
case 'f':
file_to_get = optarg;
break;
case 'h':
hostname = optarg;
break;
case 'l':
fs.fs_linkfile = 1;
break;
case 'm': case 'M':
fs.fs_mirror = 1;
break;
case 'n':
fs.fs_newtime = 1;
break;
case 'o':
fs.fs_outputfile = optarg;
break;
case 'p': case 'P':
fs.fs_passive_mode = 1;
break;
case 'q':
fs.fs_verbose = 0;
break;
case 'r':
fs.fs_restart = 1;
break;
case 'R':
fs.fs_precious = 1;
break;
case 't':
fs.fs_use_connect = 1;
break;
case 's':
fs.fs_reportsize = 1;
break;
case 'S':
/* strtol sets errno to ERANGE in the case of overflow */
errno = 0;
l = strtoul(optarg, &ep, 0);
if (!optarg[0] || *ep || errno != 0 || l > INT_MAX)
errx(EX_USAGE, "invalid size value: `%s'",
optarg);
fs.fs_expectedsize = l;
break;
case 'T':
/* strtol sets errno to ERANGE in the case of overflow */
errno = 0;
l = strtoul(optarg, &ep, 0);
if (!optarg[0] || *ep || errno != 0 || l > INT_MAX)
errx(EX_USAGE, "invalid timeout value: `%s'",
optarg);
fs.fs_timeout = l;
break;
case 'v':
if (fs.fs_verbose < 2)
fs.fs_verbose = 2;
else
fs.fs_verbose++;
break;
default:
case '?':
usage();
}
}
clean_fetch_state = fs; /* preserve option settings */
if (argv[optind] && (hostname || change_to_dir || file_to_get)) {
warnx("cannot use -h, -c, or -f with a URI argument");
usage();
}
if (fs.fs_mirror && fs.fs_restart)
errx(EX_USAGE, "-m and -r are mutually exclusive.");
if (argv[optind] == 0) {
char *uri;
if (hostname == 0) hostname = "localhost";
if (change_to_dir == 0) change_to_dir = "";
if (file_to_get == 0) {
usage();
}
uri = alloca(sizeof("ftp://") + strlen(hostname) +
strlen(change_to_dir) + 5 + strlen(file_to_get));
strcpy(uri, "ftp://");
strcat(uri, hostname);
strcat(uri, "/");
if (change_to_dir[0] == '/') {
strcat(uri, "%2f");
strcat(uri, change_to_dir+1);
}
else strcat(uri, change_to_dir);
if (file_to_get[0] != '/' && uri[strlen(uri) - 1] != '/')
strcat(uri, "/");
strcat(uri, file_to_get);
error = parse_uri(&fs, uri);
if (error)
return error;
return get(&fs);
}
for (rv = 0; argv[optind] != 0; optind++) {
error = parse_uri(&fs, argv[optind]);
if (error) {
rv = error;
continue;
}
error = get(&fs);
if (error) {
rv = error;
}
fs = clean_fetch_state;
}
return rv;
}
/*
* The signal handling is probably more complex than it needs to be,
* but it doesn't cost a lot, so we'll be extra-careful. Using
* siglongjmp() to get out of the signal handler allows us to
* call rm() without having to store the state variable in some global
* spot where the signal handler can get at it. We also obviate the need
* for a separate timeout signal handler.
*/
static int
get(struct fetch_state *volatile fs)
{
volatile int error;
struct sigaction oldhup, oldint, oldquit, oldterm;
struct sigaction catch;
volatile sigset_t omask;
sigemptyset(&catch.sa_mask);
sigaddset(&catch.sa_mask, SIGHUP);
sigaddset(&catch.sa_mask, SIGINT);
sigaddset(&catch.sa_mask, SIGQUIT);
sigaddset(&catch.sa_mask, SIGTERM);
sigaddset(&catch.sa_mask, SIGALRM);
catch.sa_handler = catchsig;
catch.sa_flags = 0;
sigprocmask(SIG_BLOCK, &catch.sa_mask, (sigset_t *)&omask);
sigaction(SIGHUP, &catch, &oldhup);
sigaction(SIGINT, &catch, &oldint);
sigaction(SIGQUIT, &catch, &oldquit);
sigaction(SIGTERM, &catch, &oldterm);
error = sigsetjmp(sigbuf, 0);
if (error == SIGALRM) {
rm(fs);
unsetup_sigalrm();
fprintf(stderr, "\n"); /* just in case */
warnx("%s: %s: timed out", fs->fs_outputfile, fs->fs_status);
goto close;
} else if (error) {
rm(fs);
fprintf(stderr, "\n"); /* just in case */
warnx("%s: interrupted by signal: %s", fs->fs_status,
sys_signame[error]);
sigdelset(&omask, error);
signal(error, SIG_DFL);
sigprocmask(SIG_SETMASK, (sigset_t *)&omask, 0);
raise(error); /* so that it gets reported as such */
}
sigprocmask(SIG_SETMASK, (sigset_t *)&omask, 0);
error = fs->fs_retrieve(fs);
close:
sigaction(SIGHUP, &oldhup, 0);
sigaction(SIGINT, &oldint, 0);
sigaction(SIGQUIT, &oldquit, 0);
sigaction(SIGTERM, &oldterm, 0);
fs->fs_close(fs);
return error;
}
/*
* Utility functions
*/
/*
* Handle all signals by jumping back into get().
*/
void
catchsig(int sig)
{
siglongjmp(sigbuf, sig);
}
/*
* Used to generate the progress display when not in quiet mode.
* Return != 0 when the file appears to be truncated.
*/
int
display(struct fetch_state *fs, off_t size, ssize_t n)
{
static off_t bytes;
static off_t bytestart;
static int pr, stdoutatty, init = 0;
static struct timeval t0, t_start;
static char *s;
struct timezone tz;
struct timeval t;
float d;
int truncated;
if (size != -1 && n == -1 && bytes != size) {
truncated = 1;
} else
truncated = 0;
if (init == 0) {
init = 1;
gettimeofday(&t0, &tz);
t_start = t0;
bytes = pr = 0;
stdoutatty = isatty(STDOUT_FILENO);
if (size > 0)
asprintf (&s, "Receiving %s (%qd bytes)%s", fs->fs_outputfile,
(quad_t)size,
size ? "" : " [appending]");
else
asprintf (&s, "Receiving %s", fs->fs_outputfile);
if (fs->fs_verbose)
fprintf (stderr, "%s", s);
bytestart = bytes = n;
goto out;
}
gettimeofday(&t, &tz);
if (n == -1) {
if(stdoutatty && fs->fs_verbose) {
if (size > 0)
fprintf (stderr, "\r%s: 100%%", s);
else
fprintf (stderr, "\r%s: %qd Kbytes", s, (long long)bytes/1024);
}
bytes -= bytestart;
d = t.tv_sec + t.tv_usec/1.e6 - t_start.tv_sec - t_start.tv_usec/1.e6;
if (fs->fs_verbose)
fprintf (stderr, "\n%qd bytes transferred in %.1f seconds",
(long long)bytes, d);
d = bytes/d;
if (fs->fs_verbose) {
if (d < 1000)
fprintf (stderr, " (%.0f bytes/s)\n", d);
else {
d /=1024;
fprintf (stderr, " (%.2f Kbytes/s)\n", d);
}
}
free(s);
init = 0;
goto out;
}
bytes += n;
d = t.tv_sec + t.tv_usec/1.e6 - t0.tv_sec - t0.tv_usec/1.e6;
if (d < 5) /* display every 5 sec. */
goto out;
t0 = t;
pr++;
if(stdoutatty && fs->fs_verbose) {
if (size > 1000000)
fprintf (stderr, "\r%s: %2qd%%", s, (long long)(bytes/(size/100)));
else if (size > 0)
fprintf (stderr, "\r%s: %2qd%%", s, (long long)(100*bytes/size));
else
fprintf (stderr, "\r%s: %qd Kbytes", s, (long long)(bytes/1024));
}
out:
if (truncated != 0)
fprintf(stderr, "WARNING: File %s appears to be truncated: "
"%qd/%qd bytes\n",
fs->fs_outputfile,
(quad_t)bytes, (quad_t)size);
return truncated;
}
|