1 /*-
2 * Copyright (c) 2007-2014, Juniper Networks, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include "stand.h"
28
29 #include <sys/stat.h>
30 #include <sys/stdint.h>
31 #include <string.h>
32 #include <zlib.h>
33
34 static int pkg_open(const char *, struct open_file *);
35 static int pkg_close(struct open_file *);
36 static int pkg_read(struct open_file *, void *, size_t, size_t *);
37 static off_t pkg_seek(struct open_file *, off_t, int);
38 static int pkg_stat(struct open_file *, struct stat *);
39 static int pkg_readdir(struct open_file *, struct dirent *);
40 static off_t pkg_atol(const char *, unsigned);
41
42 struct fs_ops pkgfs_fsops = {
43 .fs_name = "pkg",
44 .fo_open = pkg_open,
45 .fo_close = pkg_close,
46 .fo_read = pkg_read,
47 .fo_write = null_write,
48 .fo_seek = pkg_seek,
49 .fo_stat = pkg_stat,
50 .fo_readdir = pkg_readdir,
51 };
52
53 #define PKG_BUFSIZE 512
54 #define PKG_MAXCACHESZ (512 * 1024)
55
56 #define PKG_FILEEXT ".tgz"
57
58 /*
59 * Layout of POSIX 'ustar' header.
60 */
61 struct ustar_hdr {
62 char ut_name[100];
63 char ut_mode[8];
64 char ut_uid[8];
65 char ut_gid[8];
66 char ut_size[12];
67 char ut_mtime[12];
68 char ut_checksum[8];
69 char ut_typeflag[1];
70 char ut_linkname[100];
71 char ut_magic[6]; /* For POSIX: "ustar\0" */
72 char ut_version[2]; /* For POSIX: "00" */
73 char ut_uname[32];
74 char ut_gname[32];
75 char ut_rdevmajor[8];
76 char ut_rdevminor[8];
77 union {
78 struct {
79 char prefix[155];
80 } posix;
81 struct {
82 char atime[12];
83 char ctime[12];
84 char offset[12];
85 char longnames[4];
86 char unused[1];
87 struct gnu_sparse {
88 char offset[12];
89 char numbytes[12];
90 } sparse[4];
91 char isextended[1];
92 char realsize[12];
93 } gnu;
94 } u;
95 u_char __padding[12];
96 };
97
98 struct package;
99
100 struct tarfile
101 {
102 struct package *tf_pkg;
103 struct tarfile *tf_next;
104 struct ustar_hdr tf_hdr;
105 off_t tf_ofs;
106 off_t tf_size;
107 off_t tf_fp;
108 size_t tf_cachesz;
109 void *tf_cache;
110 };
111
112 struct package
113 {
114 struct package *pkg_chain;
115 int pkg_fd;
116 off_t pkg_ofs;
117 z_stream pkg_zs;
118 struct tarfile *pkg_first;
119 struct tarfile *pkg_last;
120 u_char pkg_buf[PKG_BUFSIZE];
121 };
122
123 static struct package *package = NULL;
124
125 static int new_package(int, struct package **);
126 static int cache_data(struct tarfile *tf, int);
127
128 void
pkgfs_cleanup(void)129 pkgfs_cleanup(void)
130 {
131 struct package *chain;
132 struct tarfile *tf, *tfn;
133
134 while (package != NULL) {
135 inflateEnd(&package->pkg_zs);
136 close(package->pkg_fd);
137
138 tf = package->pkg_first;
139 while (tf != NULL) {
140 tfn = tf->tf_next;
141 if (tf->tf_cachesz > 0)
142 free(tf->tf_cache);
143 free(tf);
144 tf = tfn;
145 }
146
147 chain = package->pkg_chain;
148 free(package);
149 package = chain;
150 }
151 }
152
153 int
pkgfs_init(const char * pkgname,struct fs_ops * proto)154 pkgfs_init(const char *pkgname, struct fs_ops *proto)
155 {
156 struct package *pkg;
157 int error, fd;
158
159 pkg = NULL;
160 if (proto != &pkgfs_fsops)
161 pkgfs_cleanup();
162
163 exclusive_file_system = proto;
164
165 fd = open(pkgname, O_RDONLY);
166
167 exclusive_file_system = NULL;
168
169 DEBUG_PRINTF(0, ("%s(%s: '%s') -> %d (error=%d)\n", __func__,
170 proto->fs_name, pkgname, fd, errno));
171
172 if (fd == -1)
173 return (errno);
174
175 error = new_package(fd, &pkg);
176 if (error) {
177 close(fd);
178 return (error);
179 }
180
181 if (pkg == NULL)
182 return (EDOOFUS);
183
184 pkg->pkg_chain = package;
185 package = pkg;
186 exclusive_file_system = &pkgfs_fsops;
187 return (0);
188 }
189
190 static int get_mode(struct tarfile *);
191 static int get_zipped(struct package *, void *, size_t);
192 static int new_package(int, struct package **);
193 static struct tarfile *scan_tarfile(struct package *, struct tarfile *);
194
195 static int
pkg_open_follow(const char * fn,struct open_file * f,int lnks)196 pkg_open_follow(const char *fn, struct open_file *f, int lnks)
197 {
198 struct tarfile *tf;
199
200 if (fn == NULL || f == NULL)
201 return (EINVAL);
202
203 if (package == NULL)
204 return (ENXIO);
205
206 /*
207 * We can only read from a package, so reject request to open
208 * for write-only or read-write.
209 */
210 if (f->f_flags != F_READ)
211 return (EPERM);
212
213 /*
214 * Scan the file headers for the named file. We stop scanning
215 * at the first filename that has the .pkg extension. This is
216 * a package within a package. We assume we have all the files
217 * we need up-front and without having to dig within nested
218 * packages.
219 *
220 * Note that we preserve streaming properties as much as possible.
221 */
222 while (*fn == '/')
223 fn++;
224
225 /*
226 * Allow opening of the root directory for use by readdir()
227 * to support listing files in the package.
228 */
229 if (*fn == '\0') {
230 f->f_fsdata = NULL;
231 return (0);
232 }
233
234 tf = scan_tarfile(package, NULL);
235 while (tf != NULL) {
236 if (strcmp(fn, tf->tf_hdr.ut_name) == 0) {
237 f->f_fsdata = tf;
238 tf->tf_fp = 0; /* Reset the file pointer. */
239 DEBUG_PRINTF(1, ("%s: found %s type %c\n", __func__,
240 fn, tf->tf_hdr.ut_typeflag[0]));
241 if (tf->tf_hdr.ut_typeflag[0] == '2') {
242 /* we have a symlink
243 * Note: ut_linkname is only 100 chars!
244 */
245 if (lnks++ >= 8)
246 return (EMLINK);
247 return pkg_open_follow(tf->tf_hdr.ut_linkname,
248 f, lnks);
249 }
250 return (0);
251 }
252 tf = scan_tarfile(package, tf);
253 }
254 return (errno);
255 }
256
257 static int
pkg_open(const char * fn,struct open_file * f)258 pkg_open(const char *fn, struct open_file *f)
259 {
260 return pkg_open_follow(fn, f, 0);
261 }
262
263 static int
pkg_close(struct open_file * f)264 pkg_close(struct open_file *f)
265 {
266 struct tarfile *tf;
267
268 tf = (struct tarfile *)f->f_fsdata;
269 if (tf == NULL)
270 return (0);
271
272 /*
273 * Free up the cache if we read all of the file.
274 */
275 DEBUG_PRINTF(1, ("%s(%s)\n", __func__, tf->tf_hdr.ut_name));
276 if (tf->tf_fp == tf->tf_size && tf->tf_cachesz > 0) {
277 free(tf->tf_cache);
278 tf->tf_cachesz = 0;
279 }
280 return (0);
281 }
282
283 static int
pkg_read(struct open_file * f,void * buf,size_t size,size_t * res)284 pkg_read(struct open_file *f, void *buf, size_t size, size_t *res)
285 {
286 struct tarfile *tf;
287 char *p;
288 off_t fp;
289 size_t sz;
290
291 tf = (struct tarfile *)f->f_fsdata;
292 if (tf == NULL) {
293 if (res != NULL)
294 *res = size;
295 return (EBADF);
296 }
297
298 DEBUG_PRINTF(4, ("%s(%s,%zd)\n", __func__, tf->tf_hdr.ut_name, size));
299
300 if (tf->tf_cachesz == 0)
301 cache_data(tf, 1);
302
303 fp = tf->tf_fp;
304 p = buf;
305 sz = 0;
306 while (size > 0) {
307 sz = tf->tf_size - fp;
308 if (fp < tf->tf_cachesz && tf->tf_cachesz < tf->tf_size)
309 sz = tf->tf_cachesz - fp;
310 if (size < sz)
311 sz = size;
312 if (sz == 0)
313 break;
314
315 if (fp < tf->tf_cachesz) {
316 /* Satisfy the request from cache. */
317 memcpy(p, tf->tf_cache + fp, sz);
318 fp += sz;
319 p += sz;
320 size -= sz;
321 continue;
322 }
323
324 if (get_zipped(tf->tf_pkg, p, sz) == -1) {
325 sz = -1;
326 break;
327 }
328
329 fp += sz;
330 p += sz;
331 size -= sz;
332 }
333
334 tf->tf_fp = fp;
335 if (res != NULL)
336 *res = size;
337 DEBUG_PRINTF(4, ("%s(%s) res=%zd\n", __func__, tf->tf_hdr.ut_name,
338 (ssize_t)(tf->tf_size - tf->tf_fp)));
339 return ((sz == -1) ? errno : 0);
340 }
341
342 static off_t
pkg_seek(struct open_file * f,off_t ofs,int whence)343 pkg_seek(struct open_file *f, off_t ofs, int whence)
344 {
345 char buf[512];
346 struct tarfile *tf;
347 off_t delta;
348 off_t nofs;
349 size_t sz, res;
350 int error;
351
352 tf = (struct tarfile *)f->f_fsdata;
353 if (tf == NULL) {
354 errno = EBADF;
355 return (-1);
356 }
357
358 switch (whence) {
359 case SEEK_SET:
360 delta = ofs - tf->tf_fp;
361 break;
362 case SEEK_CUR:
363 delta = ofs;
364 break;
365 case SEEK_END:
366 delta = tf->tf_size - tf->tf_fp + ofs;
367 break;
368 default:
369 errno = EINVAL;
370 return (-1);
371 }
372
373 if (delta < 0) {
374 /* seeking backwards - ok if within cache */
375 if (tf->tf_cachesz > 0 && tf->tf_fp <= tf->tf_cachesz) {
376 nofs = tf->tf_fp + delta;
377 if (nofs >= 0) {
378 tf->tf_fp = nofs;
379 return (tf->tf_fp);
380 }
381 }
382 DEBUG_PRINTF(3, ("%s: negative file seek (%jd)\n", __func__,
383 (intmax_t)delta));
384 errno = ESPIPE;
385 return (-1);
386 }
387
388 while (delta > 0 && tf->tf_fp < tf->tf_size) {
389 sz = (delta > sizeof(buf)) ? sizeof(buf) : delta;
390 error = pkg_read(f, buf, sz, &res);
391 if (error != 0) {
392 errno = error;
393 return (-1);
394 }
395 delta -= sz - res;
396 }
397
398 return (tf->tf_fp);
399 }
400
401 static int
pkg_stat(struct open_file * f,struct stat * sb)402 pkg_stat(struct open_file *f, struct stat *sb)
403 {
404 struct tarfile *tf;
405
406 tf = (struct tarfile *)f->f_fsdata;
407 if (tf == NULL)
408 return (EBADF);
409 memset(sb, 0, sizeof(*sb));
410 sb->st_mode = get_mode(tf);
411 if ((sb->st_mode & S_IFMT) == 0) {
412 /* tar file bug - assume regular file */
413 sb->st_mode |= S_IFREG;
414 }
415 sb->st_size = tf->tf_size;
416 sb->st_blocks = (tf->tf_size + 511) / 512;
417 sb->st_mtime = pkg_atol(tf->tf_hdr.ut_mtime, 12);
418 sb->st_dev = (off_t)((uintptr_t)tf->tf_pkg);
419 sb->st_ino = tf->tf_ofs; /* unique per tf_pkg */
420 return (0);
421 }
422
423 static int
pkg_readdir(struct open_file * f,struct dirent * d)424 pkg_readdir(struct open_file *f, struct dirent *d)
425 {
426 struct tarfile *tf;
427
428 tf = (struct tarfile *)f->f_fsdata;
429 if (tf != NULL)
430 return (EBADF);
431
432 tf = scan_tarfile(package, NULL);
433 if (tf == NULL)
434 return (ENOENT);
435
436 d->d_fileno = 0;
437 d->d_reclen = sizeof(*d);
438 d->d_type = DT_REG;
439 memcpy(d->d_name, tf->tf_hdr.ut_name, sizeof(d->d_name));
440 return (0);
441 }
442
443 /*
444 * Low-level support functions.
445 */
446
447 static int
get_byte(struct package * pkg,off_t * op)448 get_byte(struct package *pkg, off_t *op)
449 {
450 int c;
451
452 if (pkg->pkg_zs.avail_in == 0) {
453 c = read(pkg->pkg_fd, pkg->pkg_buf, PKG_BUFSIZE);
454 if (c <= 0)
455 return (-1);
456 pkg->pkg_zs.avail_in = c;
457 pkg->pkg_zs.next_in = pkg->pkg_buf;
458 }
459
460 c = *pkg->pkg_zs.next_in;
461 pkg->pkg_zs.next_in++;
462 pkg->pkg_zs.avail_in--;
463 (*op)++;
464 return (c);
465 }
466
467 static int
get_zipped(struct package * pkg,void * buf,size_t bufsz)468 get_zipped(struct package *pkg, void *buf, size_t bufsz)
469 {
470 int c;
471
472 pkg->pkg_zs.next_out = buf;
473 pkg->pkg_zs.avail_out = bufsz;
474
475 while (pkg->pkg_zs.avail_out) {
476 if (pkg->pkg_zs.avail_in == 0) {
477 c = read(pkg->pkg_fd, pkg->pkg_buf, PKG_BUFSIZE);
478 if (c <= 0) {
479 errno = EIO;
480 return (-1);
481 }
482 pkg->pkg_zs.avail_in = c;
483 pkg->pkg_zs.next_in = pkg->pkg_buf;
484 }
485
486 c = inflate(&pkg->pkg_zs, Z_SYNC_FLUSH);
487 if (c != Z_OK && c != Z_STREAM_END) {
488 errno = EIO;
489 return (-1);
490 }
491 }
492
493 pkg->pkg_ofs += bufsz;
494 return (0);
495 }
496
497 /**
498 * @brief
499 * cache data of a tarfile
500 *
501 * @param[in] tf
502 * tarfile pointer
503 *
504 * @param[in] force
505 * If file size > PKG_MAXCACHESZ, cache that much
506 *
507 * @return 0, -1 (errno set to error value)
508 */
509 static int
cache_data(struct tarfile * tf,int force)510 cache_data(struct tarfile *tf, int force)
511 {
512 struct package *pkg;
513 size_t sz;
514
515 if (tf == NULL) {
516 DEBUG_PRINTF(5, ("%s: no file to cache data for?\n",
517 __func__));
518 errno = EINVAL;
519 return (-1);
520 }
521
522 pkg = tf->tf_pkg;
523 if (pkg == NULL) {
524 DEBUG_PRINTF(5, ("%s: no package associated with file?\n",
525 __func__));
526 errno = EINVAL;
527 return (-1);
528 }
529
530 if (tf->tf_cachesz > 0) {
531 DEBUG_PRINTF(5, ("%s: data already cached\n", __func__));
532 errno = EINVAL;
533 return (-1);
534 }
535
536 if (tf->tf_ofs != pkg->pkg_ofs) {
537 DEBUG_PRINTF(5, ("%s: caching after force read of file %s?\n",
538 __func__, tf->tf_hdr.ut_name));
539 errno = EINVAL;
540 return (-1);
541 }
542
543 /* We don't cache everything... */
544 if (tf->tf_size > PKG_MAXCACHESZ && !force) {
545 errno = ENOBUFS;
546 return (-1);
547 }
548
549 sz = tf->tf_size < PKG_MAXCACHESZ ? tf->tf_size : PKG_MAXCACHESZ;
550 /* All files are padded to a multiple of 512 bytes. */
551 sz = (sz + 0x1ff) & ~0x1ff;
552
553 tf->tf_cache = malloc(sz);
554 if (tf->tf_cache == NULL) {
555 DEBUG_PRINTF(5, ("%s: could not allocate %d bytes\n",
556 __func__, (int)sz));
557 errno = ENOMEM;
558 return (-1);
559 }
560
561 tf->tf_cachesz = sz;
562 return (get_zipped(pkg, tf->tf_cache, sz));
563 }
564
565 /*
566 * Note that this implementation does not (and should not!) obey
567 * locale settings; you cannot simply substitute strtol here, since
568 * it does obey locale.
569 */
570 static off_t
pkg_atol8(const char * p,unsigned char_cnt)571 pkg_atol8(const char *p, unsigned char_cnt)
572 {
573 int64_t l, limit, last_digit_limit;
574 int digit, sign, base;
575
576 base = 8;
577 limit = INT64_MAX / base;
578 last_digit_limit = INT64_MAX % base;
579
580 while (*p == ' ' || *p == '\t')
581 p++;
582 if (*p == '-') {
583 sign = -1;
584 p++;
585 } else
586 sign = 1;
587
588 l = 0;
589 digit = *p - '0';
590 while (digit >= 0 && digit < base && char_cnt-- > 0) {
591 if (l>limit || (l == limit && digit > last_digit_limit)) {
592 l = UINT64_MAX; /* Truncate on overflow. */
593 break;
594 }
595 l = (l * base) + digit;
596 digit = *++p - '0';
597 }
598 return (sign < 0) ? -l : l;
599 }
600
601 /*
602 * Parse a base-256 integer. This is just a straight signed binary
603 * value in big-endian order, except that the high-order bit is
604 * ignored. Remember that "int64_t" may or may not be exactly 64
605 * bits; the implementation here tries to avoid making any assumptions
606 * about the actual size of an int64_t. It does assume we're using
607 * twos-complement arithmetic, though.
608 */
609 static int64_t
pkg_atol256(const char * _p,unsigned char_cnt)610 pkg_atol256(const char *_p, unsigned char_cnt)
611 {
612 int64_t l, upper_limit, lower_limit;
613 const unsigned char *p = (const unsigned char *)_p;
614
615 upper_limit = INT64_MAX / 256;
616 lower_limit = INT64_MIN / 256;
617
618 /* Pad with 1 or 0 bits, depending on sign. */
619 if ((0x40 & *p) == 0x40)
620 l = (int64_t)-1;
621 else
622 l = 0;
623 l = (l << 6) | (0x3f & *p++);
624 while (--char_cnt > 0) {
625 if (l > upper_limit) {
626 l = INT64_MAX; /* Truncate on overflow */
627 break;
628 } else if (l < lower_limit) {
629 l = INT64_MIN;
630 break;
631 }
632 l = (l << 8) | (0xff & (int64_t)*p++);
633 }
634 return (l);
635 }
636
637 static off_t
pkg_atol(const char * p,unsigned char_cnt)638 pkg_atol(const char *p, unsigned char_cnt)
639 {
640 /*
641 * Technically, GNU pkg considers a field to be in base-256
642 * only if the first byte is 0xff or 0x80.
643 */
644 if (*p & 0x80)
645 return (pkg_atol256(p, char_cnt));
646 return (pkg_atol8(p, char_cnt));
647 }
648
649 static int
get_mode(struct tarfile * tf)650 get_mode(struct tarfile *tf)
651 {
652 return (pkg_atol(tf->tf_hdr.ut_mode, sizeof(tf->tf_hdr.ut_mode)));
653 }
654
655 /* GZip flag byte */
656 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
657 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
658 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
659 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
660 #define COMMENT 0x10 /* bit 4 set: file comment present */
661 #define RESERVED 0xE0 /* bits 5..7: reserved */
662
663 static int
new_package(int fd,struct package ** pp)664 new_package(int fd, struct package **pp)
665 {
666 struct package *pkg;
667 off_t ofs;
668 int flags, i, error;
669
670 pkg = malloc(sizeof(*pkg));
671 if (pkg == NULL)
672 return (ENOMEM);
673
674 bzero(pkg, sizeof(*pkg));
675 pkg->pkg_fd = fd;
676
677 /*
678 * Parse the header.
679 */
680 error = EFTYPE;
681 ofs = 0;
682
683 /* Check megic. */
684 if (get_byte(pkg, &ofs) != 0x1f || get_byte(pkg, &ofs) != 0x8b)
685 goto fail;
686 /* Check method. */
687 if (get_byte(pkg, &ofs) != Z_DEFLATED)
688 goto fail;
689 /* Check flags. */
690 flags = get_byte(pkg, &ofs);
691 if (flags & RESERVED)
692 goto fail;
693
694 /* Skip time, xflags and OS code. */
695 for (i = 0; i < 6; i++) {
696 if (get_byte(pkg, &ofs) == -1)
697 goto fail;
698 }
699
700 /* Skip extra field. */
701 if (flags & EXTRA_FIELD) {
702 i = (get_byte(pkg, &ofs) & 0xff) |
703 ((get_byte(pkg, &ofs) << 8) & 0xff);
704 while (i-- > 0) {
705 if (get_byte(pkg, &ofs) == -1)
706 goto fail;
707 }
708 }
709
710 /* Skip original file name. */
711 if (flags & ORIG_NAME) {
712 do {
713 i = get_byte(pkg, &ofs);
714 } while (i != 0 && i != -1);
715 if (i == -1)
716 goto fail;
717 }
718
719 /* Print the comment if it's there. */
720 if (flags & COMMENT) {
721 while (1) {
722 i = get_byte(pkg, &ofs);
723 if (i == -1)
724 goto fail;
725 if (i == 0)
726 break;
727 putchar(i);
728 }
729 }
730
731 /* Skip the CRC. */
732 if (flags & HEAD_CRC) {
733 if (get_byte(pkg, &ofs) == -1)
734 goto fail;
735 if (get_byte(pkg, &ofs) == -1)
736 goto fail;
737 }
738
739 /*
740 * Done parsing the ZIP header. Start the inflation engine.
741 */
742 error = inflateInit2(&pkg->pkg_zs, -15);
743 if (error != Z_OK)
744 goto fail;
745
746 *pp = pkg;
747 return (0);
748
749 fail:
750 free(pkg);
751 return (error);
752 }
753
754 static struct tarfile *
scan_tarfile(struct package * pkg,struct tarfile * last)755 scan_tarfile(struct package *pkg, struct tarfile *last)
756 {
757 char buf[512];
758 struct tarfile *cur;
759 off_t ofs;
760 size_t sz;
761
762 cur = (last != NULL) ? last->tf_next : pkg->pkg_first;
763 if (cur == NULL) {
764 ofs = (last != NULL) ? last->tf_ofs + last->tf_size :
765 pkg->pkg_ofs;
766 ofs = (ofs + 0x1ff) & ~0x1ff;
767
768 /* Check if we've reached EOF. */
769 if (ofs < pkg->pkg_ofs) {
770 errno = ENOSPC;
771 return (NULL);
772 }
773
774 if (ofs != pkg->pkg_ofs) {
775 if (last != NULL && pkg->pkg_ofs == last->tf_ofs) {
776 if (cache_data(last, 0) == -1)
777 return (NULL);
778 } else {
779 sz = ofs - pkg->pkg_ofs;
780 while (sz != 0) {
781 if (sz > sizeof(buf))
782 sz = sizeof(buf);
783 if (get_zipped(pkg, buf, sz) == -1)
784 return (NULL);
785 sz = ofs - pkg->pkg_ofs;
786 }
787 }
788 }
789
790 cur = malloc(sizeof(*cur));
791 if (cur == NULL)
792 return (NULL);
793 memset(cur, 0, sizeof(*cur));
794 cur->tf_pkg = pkg;
795
796 while (1) {
797 if (get_zipped(pkg, &cur->tf_hdr,
798 sizeof(cur->tf_hdr)) == -1) {
799 free(cur);
800 return (NULL);
801 }
802
803 /*
804 * There are always 2 empty blocks appended to
805 * a PKG. It marks the end of the archive.
806 */
807 if (strncmp(cur->tf_hdr.ut_magic, "ustar", 5) != 0) {
808 free(cur);
809 errno = ENOSPC;
810 return (NULL);
811 }
812
813 cur->tf_ofs = pkg->pkg_ofs;
814 cur->tf_size = pkg_atol(cur->tf_hdr.ut_size,
815 sizeof(cur->tf_hdr.ut_size));
816
817 if (cur->tf_hdr.ut_name[0] != '+')
818 break;
819
820 /*
821 * Skip package meta-files.
822 */
823 ofs = cur->tf_ofs + cur->tf_size;
824 ofs = (ofs + 0x1ff) & ~0x1ff;
825 while (pkg->pkg_ofs < ofs) {
826 if (get_zipped(pkg, buf, sizeof(buf)) == -1) {
827 free(cur);
828 return (NULL);
829 }
830 }
831 }
832
833 if (last != NULL)
834 last->tf_next = cur;
835 else
836 pkg->pkg_first = cur;
837 pkg->pkg_last = cur;
838 }
839
840 return (cur);
841 }
842