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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
|
/* $OpenBSD: file.c,v 1.7 1998/10/13 23:09:52 marc Exp $ */
#ifndef lint
static const char *rcsid = "$OpenBSD: file.c,v 1.7 1998/10/13 23:09:52 marc Exp $";
#endif
/*
* FreeBSD install - a package for the installation and maintainance
* of non-core utilities.
*
* 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.
*
* Jordan K. Hubbard
* 18 July 1993
*
* Miscellaneous file access utilities.
*
*/
#include "lib.h"
#include <sys/wait.h>
#include <assert.h>
#include <err.h>
#include <netdb.h>
#include <pwd.h>
#include <time.h>
/* This is as ftpGetURL from FreeBSD's ftpio.c, except that it uses
* OpenBSD's ftp command to do all FTP.
*/
static FILE *
ftpGetURL(char *url, int *retcode)
{
FILE *ftp;
pid_t pid_ftp;
int p[2];
*retcode=0;
if (pipe(p) < 0) {
*retcode = 1;
return NULL;
}
pid_ftp = fork();
if (pid_ftp < 0) {
*retcode = 1;
return NULL;
}
if (pid_ftp == 0) {
/* child */
dup2(p[1],1);
close(p[1]);
fprintf(stderr, ">>> ftp -o - %s\n",url);
execl("/usr/bin/ftp","ftp","-V","-o","-",url,NULL);
exit(1);
} else {
/* parent */
ftp = fdopen(p[0],"r");
close(p[1]);
if (ftp == (FILE *) NULL) {
*retcode = 1;
return NULL;
}
}
return ftp;
}
/* Quick check to see if a file exists */
Boolean
fexists(char *fname)
{
struct stat dummy;
if (!lstat(fname, &dummy))
return TRUE;
return FALSE;
}
/* Quick check to see if something is a directory */
Boolean
isdir(char *fname)
{
struct stat sb;
if (lstat(fname, &sb) != FAIL && S_ISDIR(sb.st_mode))
return TRUE;
else
return FALSE;
}
/* Check if something is a link to a directory */
Boolean
islinktodir(char *fname)
{
struct stat sb;
if (lstat(fname, &sb) != FAIL && S_ISLNK(sb.st_mode))
if (stat(fname, &sb) != FAIL && S_ISDIR(sb.st_mode))
return TRUE; /* link to dir! */
else
return FALSE; /* link to non-dir */
else
return FALSE; /* non-link */
}
/* Check to see if file is a dir, and is empty */
Boolean
isemptydir(char *fname)
{
if (isdir(fname) || islinktodir(fname)) {
DIR *dirp;
struct dirent *dp;
dirp = opendir(fname);
if (!dirp)
return FALSE; /* no perms, leave it alone */
for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..")) {
closedir(dirp);
return FALSE;
}
}
(void)closedir(dirp);
return TRUE;
}
return FALSE;
}
Boolean
isfile(char *fname)
{
struct stat sb;
if (stat(fname, &sb) != FAIL && S_ISREG(sb.st_mode))
return TRUE;
return FALSE;
}
/* Check to see if file is a file and is empty. If nonexistent or not
a file, say "it's empty", otherwise return TRUE if zero sized. */
Boolean
isemptyfile(char *fname)
{
struct stat sb;
if (stat(fname, &sb) != FAIL && S_ISREG(sb.st_mode)) {
if (sb.st_size != 0)
return FALSE;
}
return TRUE;
}
/* Returns TRUE if file is a URL specification */
Boolean
isURL(char *fname)
{
/*
* I'm sure there are other types of URL specifications that I could
* also be looking for here, but for now I'll just be happy to get ftp
* working.
*/
if (!fname)
return FALSE;
while (isspace(*fname))
++fname;
if (!strncmp(fname, "ftp://", 6))
return TRUE;
return FALSE;
}
/* Returns the host part of a URL */
char *
fileURLHost(char *fname, char *where, int max)
{
char *ret;
while (isspace(*fname))
++fname;
/* Don't ever call this on a bad URL! */
fname += strlen("ftp://");
/* Do we have a place to stick our work? */
if ((ret = where) != NULL) {
while (*fname && *fname != '/' && max--)
*where++ = *fname++;
*where = '\0';
return ret;
}
/* If not, they must really want us to stomp the original string */
ret = fname;
while (*fname && *fname != '/')
++fname;
*fname = '\0';
return ret;
}
/* Returns the filename part of a URL */
char *
fileURLFilename(char *fname, char *where, int max)
{
char *ret;
while (isspace(*fname))
++fname;
/* Don't ever call this on a bad URL! */
fname += strlen("ftp://");
/* Do we have a place to stick our work? */
if ((ret = where) != NULL) {
while (*fname && *fname != '/')
++fname;
if (*fname == '/') {
while (*fname && max--)
*where++ = *fname++;
}
*where = '\0';
return ret;
}
/* If not, they must really want us to stomp the original string */
while (*fname && *fname != '/')
++fname;
return fname;
}
/*
* Try and fetch a file by URL, returning the directory name for where
* it's unpacked, if successful.
*/
char *
fileGetURL(char *base, char *spec)
{
char host[MAXHOSTNAMELEN], file[FILENAME_MAX];
char *cp, *rp;
char fname[FILENAME_MAX];
char pen[FILENAME_MAX];
FILE *ftp;
pid_t tpid;
int i, status;
char *hint;
rp = NULL;
/* Special tip that sysinstall left for us */
hint = getenv("PKG_ADD_BASE");
if (!isURL(spec)) {
if (!base && !hint)
return NULL;
/* We've been given an existing URL (that's known-good) and now we need
to construct a composite one out of that and the basename we were
handed as a dependency. */
if (base) {
strcpy(fname, base);
/* Advance back two slashes to get to the root of the package hierarchy */
cp = strrchr(fname, '/');
if (cp) {
*cp = '\0'; /* chop name */
cp = strrchr(fname, '/');
}
if (cp) {
*(cp + 1) = '\0';
strcat(cp, "All/");
strcat(cp, spec);
strcat(cp, ".tgz");
}
else
return NULL;
}
else {
/* Otherwise, we've been given an environment variable hinting at the right location from sysinstall */
strcpy(fname, hint);
strcat(fname, spec);
}
}
else
strcpy(fname, spec);
cp = fileURLHost(fname, host, MAXHOSTNAMELEN);
if (!*cp) {
warnx("URL `%s' has bad host part!", fname);
return NULL;
}
cp = fileURLFilename(fname, file, FILENAME_MAX);
if (!*cp) {
warnx("URL `%s' has bad filename part!", fname);
return NULL;
}
if (Verbose)
printf("Trying to fetch %s.\n", fname);
ftp = ftpGetURL(fname, &status);
if (ftp) {
pen[0] = '\0';
if ((rp = make_playpen(pen, sizeof(pen), 0)) != NULL) {
rp=strdup(pen); /* be safe for nested calls */
if (Verbose)
printf("Extracting from FTP connection into %s\n", pen);
tpid = fork();
if (!tpid) {
dup2(fileno(ftp), 0);
i = execl("/bin/tar", "tar", Verbose ? "-xzvf" : "-xzf", "-", 0);
exit(i);
}
else {
int pstat;
fclose(ftp);
tpid = waitpid(tpid, &pstat, 0);
if (Verbose)
printf("tar command returns %d status\n", WEXITSTATUS(pstat));
}
}
else
printf("Error: Unable to construct a new playpen for FTP!\n");
fclose(ftp);
}
else
printf("Error: FTP Unable to get %s: %s\n",
fname,
status ? "Error while performing FTP" :
hstrerror(h_errno));
return rp;
}
char *
fileFindByPath(char *base, char *fname)
{
static char tmp[FILENAME_MAX];
char *cp;
if (ispkgpattern(fname)) {
if ((cp=findbestmatchingname(".",fname)) != NULL) {
strcpy (tmp, cp);
return tmp;
}
} else {
if (fexists(fname) && isfile(fname)) {
strcpy(tmp, fname);
return tmp;
}
}
if (base) {
strcpy(tmp, base);
cp = strrchr(tmp, '/');
if (cp) {
*cp = '\0'; /* chop name */
cp = strrchr(tmp, '/');
}
if (cp) {
*(cp + 1) = '\0';
strcat(cp, "All/");
strcat(cp, fname);
strcat(cp, ".tgz");
if (ispkgpattern(tmp)) {
cp=findbestmatchingname(dirname_of(tmp),
basename_of(tmp));
if (cp) {
char *s;
s=strrchr(tmp,'/');
assert(s != NULL);
strcpy(s+1, cp);
return tmp;
}
} else {
if (fexists(tmp)) {
return tmp;
}
}
}
}
cp = getenv("PKG_PATH");
while (cp) {
char *cp2 = strsep(&cp, ":");
snprintf(tmp, FILENAME_MAX, "%s/%s.tgz", cp2 ? cp2 : cp, fname);
if (ispkgpattern(tmp)) {
char *s;
s = findbestmatchingname(dirname_of(tmp),
basename_of(tmp));
if (s){
char *t;
t=strrchr(tmp, '/');
strcpy(t+1, s);
return tmp;
}
} else {
if (fexists(tmp) && isfile(tmp)) {
return tmp;
}
}
}
return NULL;
}
char *
fileGetContents(char *fname)
{
char *contents;
struct stat sb;
int fd;
if (stat(fname, &sb) == FAIL) {
cleanup(0);
errx(2, "can't stat '%s'", fname);
}
contents = (char *)malloc((size_t)(sb.st_size) + 1);
fd = open(fname, O_RDONLY, 0);
if (fd == FAIL) {
cleanup(0);
errx(2, "unable to open '%s' for reading", fname);
}
if (read(fd, contents, (size_t) sb.st_size) != (size_t) sb.st_size) {
cleanup(0);
errx(2, "short read on '%s' - did not get %qd bytes",
fname, (long long)sb.st_size);
}
close(fd);
contents[(size_t)sb.st_size] = '\0';
return contents;
}
/* Takes a filename and package name, returning (in "try") the canonical "preserve"
* name for it.
*/
Boolean
make_preserve_name(char *try, size_t max, char *name, char *file)
{
int len, i;
if ((len = strlen(file)) == 0)
return FALSE;
else
i = len - 1;
strncpy(try, file, max);
if (try[i] == '/') /* Catch trailing slash early and save checking in the loop */
--i;
for (; i; i--) {
if (try[i] == '/') {
try[i + 1]= '.';
strncpy(&try[i + 2], &file[i + 1], max - i - 2);
break;
}
}
if (!i) {
try[0] = '.';
strncpy(try + 1, file, max - 1);
}
/* I should probably be called rude names for these inline assignments */
strncat(try, ".", max -= strlen(try));
strncat(try, name, max -= strlen(name));
strncat(try, ".", max--);
strncat(try, "backup", max -= 6);
return TRUE;
}
/* Write the contents of "str" to a file */
void
write_file(char *name, char *str)
{
FILE *fp;
size_t len;
if ((fp = fopen(name, "w")) == (FILE *) NULL) {
cleanup(0);
errx(2, "cannot fopen '%s' for writing", name);
}
len = strlen(str);
if (fwrite(str, 1, len, fp) != len) {
cleanup(0);
errx(2, "short fwrite on '%s', tried to write %d bytes",
name, len);
}
if (fclose(fp)) {
cleanup(0);
errx(2, "failure to fclose '%s'", name);
}
}
void
copy_file(char *dir, char *fname, char *to)
{
char cmd[FILENAME_MAX];
if (fname[0] == '/')
snprintf(cmd, FILENAME_MAX, "cp -p -r %s %s", fname, to);
else
snprintf(cmd, FILENAME_MAX, "cp -p -r %s/%s %s", dir, fname, to);
if (vsystem(cmd)) {
cleanup(0);
errx(2, "could not perform '%s'", cmd);
}
}
void
move_file(char *dir, char *fname, char *to)
{
char cmd[FILENAME_MAX];
if (fname[0] == '/')
snprintf(cmd, FILENAME_MAX, "mv %s %s", fname, to);
else
snprintf(cmd, FILENAME_MAX, "mv %s/%s %s", dir, fname, to);
if (vsystem(cmd)) {
cleanup(0);
errx(2, "could not perform '%s'", cmd);
}
}
/*
* Copy a hierarchy (possibly from dir) to the current directory, or
* if "to" is TRUE, from the current directory to a location someplace
* else.
*
* Though slower, using tar to copy preserves symlinks and everything
* without me having to write some big hairy routine to do it.
*/
void
copy_hierarchy(char *dir, char *fname, Boolean to)
{
char cmd[FILENAME_MAX * 3];
if (!to) {
/* If absolute path, use it */
if (*fname == '/')
dir = "/";
snprintf(cmd, FILENAME_MAX * 3, "tar cf - -C %s %s | tar xpf -",
dir, fname);
}
else
snprintf(cmd, FILENAME_MAX * 3, "tar cf - %s | tar xpf - -C %s",
fname, dir);
#ifdef DEBUG
printf("Using '%s' to copy trees.\n", cmd);
#endif
if (system(cmd)) {
cleanup(0);
errx(2, "copy_file: could not perform '%s'", cmd);
}
}
/* Unpack a tar file */
int
unpack(char *pkg, char *flist)
{
char args[10], suff[80], *cp;
args[0] = '\0';
/*
* Figure out by a crude heuristic whether this or not this is probably
* compressed.
*/
if (strcmp(pkg, "-")) {
cp = strrchr(pkg, '.');
if (cp) {
strcpy(suff, cp + 1);
if (strchr(suff, 'z') || strchr(suff, 'Z'))
strcpy(args, "-z");
}
}
else
strcpy(args, "z");
strcat(args, "xpf");
if (vsystem("tar %s %s %s", args, pkg, flist ? flist : "")) {
warnx("tar extract of %s failed!", pkg);
return 1;
}
return 0;
}
/*
* Using fmt, replace all instances of:
*
* %F With the parameter "name"
* %D With the parameter "dir"
* %B Return the directory part ("base") of %D/%F
* %f Return the filename part of %D/%F
*
* Check that no overflows can occur.
*/
void
format_cmd(char *buf, size_t size, char *fmt, char *dir, char *name)
{
char scratch[FILENAME_MAX * 2];
char *bufp;
char *cp;
for (bufp = buf ; (int)(bufp - buf) < size && *fmt ; ) {
if (*fmt == '%') {
switch (*++fmt) {
case 'F':
strnncpy(bufp, size - (int)(bufp - buf), name, strlen(name));
bufp += strlen(bufp);
break;
case 'D':
strnncpy(bufp, size - (int)(bufp - buf), dir, strlen(dir));
bufp += strlen(bufp);
break;
case 'B':
(void) snprintf(scratch, sizeof(scratch), "%s/%s", dir, name);
if ((cp = strrchr(scratch, '/')) == (char *) NULL) {
cp = scratch;
}
strnncpy(bufp, size - (int)(bufp - buf), scratch, (size_t)(cp - scratch));
bufp += strlen(bufp);
break;
case 'f':
(void) snprintf(scratch, sizeof(scratch), "%s/%s", dir, name);
if ((cp = strrchr(scratch, '/')) == (char *) NULL) {
cp = scratch;
} else {
cp++;
}
strnncpy(bufp, size - (int)(bufp - buf), cp, strlen(cp));
bufp += strlen(bufp);
break;
default:
*bufp++ = *fmt;
break;
}
++fmt;
} else {
*bufp++ = *fmt++;
}
}
*bufp = '\0';
}
|