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