2 * Copyright (c) 2007-2014, Juniper Networks, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
33 #include <sys/stdint.h>
38 #define DBG(x) printf x
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);
51 struct fs_ops pkgfs_fsops = {
62 #define PKG_BUFSIZE 512
63 #define PKG_MAXCACHESZ (512 * 1024)
65 #define PKG_FILEEXT ".tgz"
68 * Layout of POSIX 'ustar' header.
79 char ut_linkname[100];
80 char ut_magic[6]; /* For POSIX: "ustar\0" */
81 char ut_version[2]; /* For POSIX: "00" */
104 u_char __padding[12];
111 struct package *tf_pkg;
112 struct tarfile *tf_next;
113 struct ustar_hdr tf_hdr;
123 struct package *pkg_chain;
127 struct tarfile *pkg_first;
128 struct tarfile *pkg_last;
129 u_char pkg_buf[PKG_BUFSIZE];
132 static struct package *package = NULL;
134 static int new_package(int, struct package **);
135 static int cache_data(struct tarfile *tf, int);
140 struct package *chain;
141 struct tarfile *tf, *tfn;
143 while (package != NULL) {
144 inflateEnd(&package->pkg_zs);
145 close(package->pkg_fd);
147 tf = package->pkg_first;
150 if (tf->tf_cachesz > 0)
156 chain = package->pkg_chain;
163 pkgfs_init(const char *pkgname, struct fs_ops *proto)
169 if (proto != &pkgfs_fsops)
172 exclusive_file_system = proto;
174 fd = open(pkgname, O_RDONLY);
176 exclusive_file_system = NULL;
181 error = new_package(fd, &pkg);
190 pkg->pkg_chain = package;
192 exclusive_file_system = &pkgfs_fsops;
196 static int get_mode(struct tarfile *);
197 static int get_zipped(struct package *, void *, size_t);
198 static int new_package(int, struct package **);
199 static struct tarfile *scan_tarfile(struct package *, struct tarfile *);
202 pkg_open(const char *fn, struct open_file *f)
206 if (fn == NULL || f == NULL)
213 * We can only read from a package, so reject request to open
214 * for write-only or read-write.
216 if (f->f_flags != F_READ)
220 * Scan the file headers for the named file. We stop scanning
221 * at the first filename that has the .pkg extension. This is
222 * a package within a package. We assume we have all the files
223 * we need up-front and without having to dig within nested
226 * Note that we preserve streaming properties as much as possible.
232 * Allow opening of the root directory for use by readdir()
233 * to support listing files in the package.
240 tf = scan_tarfile(package, NULL);
242 if (strcmp(fn, tf->tf_hdr.ut_name) == 0) {
244 tf->tf_fp = 0; /* Reset the file pointer. */
247 tf = scan_tarfile(package, tf);
253 pkg_close(struct open_file *f)
257 tf = (struct tarfile *)f->f_fsdata;
262 * Free up the cache if we read all of the file.
264 if (tf->tf_fp == tf->tf_size && tf->tf_cachesz > 0) {
272 pkg_read(struct open_file *f, void *buf, size_t size, size_t *res)
279 tf = (struct tarfile *)f->f_fsdata;
286 if (tf->tf_cachesz == 0)
293 sz = tf->tf_size - fp;
294 if (fp < tf->tf_cachesz && tf->tf_cachesz < tf->tf_size)
295 sz = tf->tf_cachesz - fp;
301 if (fp < tf->tf_cachesz) {
302 /* Satisfy the request from cache. */
303 memcpy(p, tf->tf_cache + fp, sz);
310 if (get_zipped(tf->tf_pkg, p, sz) == -1) {
323 return ((sz == -1) ? errno : 0);
327 pkg_seek(struct open_file *f, off_t ofs, int whence)
336 tf = (struct tarfile *)f->f_fsdata;
344 delta = ofs - tf->tf_fp;
350 delta = tf->tf_size - tf->tf_fp + ofs;
358 /* seeking backwards - ok if within cache */
359 if (tf->tf_cachesz > 0 && tf->tf_fp <= tf->tf_cachesz) {
360 nofs = tf->tf_fp + delta;
366 DBG(("%s: negative file seek (%jd)\n", __func__,
372 while (delta > 0 && tf->tf_fp < tf->tf_size) {
373 sz = (delta > sizeof(buf)) ? sizeof(buf) : delta;
374 error = pkg_read(f, buf, sz, &res);
386 pkg_stat(struct open_file *f, struct stat *sb)
390 tf = (struct tarfile *)f->f_fsdata;
393 memset(sb, 0, sizeof(*sb));
394 sb->st_mode = get_mode(tf);
395 if ((sb->st_mode & S_IFMT) == 0) {
396 /* tar file bug - assume regular file */
397 sb->st_mode |= S_IFREG;
399 sb->st_size = tf->tf_size;
400 sb->st_blocks = (tf->tf_size + 511) / 512;
401 sb->st_mtime = pkg_atol(tf->tf_hdr.ut_mtime, 12);
402 sb->st_dev = (off_t)((uintptr_t)tf->tf_pkg);
403 sb->st_ino = tf->tf_ofs; /* unique per tf_pkg */
408 pkg_readdir(struct open_file *f, struct dirent *d)
412 tf = (struct tarfile *)f->f_fsdata;
416 tf = scan_tarfile(package, NULL);
421 d->d_reclen = sizeof(*d);
423 memcpy(d->d_name, tf->tf_hdr.ut_name, sizeof(d->d_name));
428 * Low-level support functions.
432 get_byte(struct package *pkg, off_t *op)
436 if (pkg->pkg_zs.avail_in == 0) {
437 c = read(pkg->pkg_fd, pkg->pkg_buf, PKG_BUFSIZE);
440 pkg->pkg_zs.avail_in = c;
441 pkg->pkg_zs.next_in = pkg->pkg_buf;
444 c = *pkg->pkg_zs.next_in;
445 pkg->pkg_zs.next_in++;
446 pkg->pkg_zs.avail_in--;
452 get_zipped(struct package *pkg, void *buf, size_t bufsz)
456 pkg->pkg_zs.next_out = buf;
457 pkg->pkg_zs.avail_out = bufsz;
459 while (pkg->pkg_zs.avail_out) {
460 if (pkg->pkg_zs.avail_in == 0) {
461 c = read(pkg->pkg_fd, pkg->pkg_buf, PKG_BUFSIZE);
466 pkg->pkg_zs.avail_in = c;
467 pkg->pkg_zs.next_in = pkg->pkg_buf;
470 c = inflate(&pkg->pkg_zs, Z_SYNC_FLUSH);
471 if (c != Z_OK && c != Z_STREAM_END) {
477 pkg->pkg_ofs += bufsz;
483 * cache data of a tarfile
489 * If file size > PKG_MAXCACHESZ, cache that much
491 * @return 0, -1 (errno set to error value)
494 cache_data(struct tarfile *tf, int force)
500 DBG(("%s: no file to cache data for?\n", __func__));
507 DBG(("%s: no package associated with file?\n", __func__));
512 if (tf->tf_cachesz > 0) {
513 DBG(("%s: data already cached\n", __func__));
518 if (tf->tf_ofs != pkg->pkg_ofs) {
519 DBG(("%s: caching after force read of file %s?\n",
520 __func__, tf->tf_hdr.ut_name));
525 /* We don't cache everything... */
526 if (tf->tf_size > PKG_MAXCACHESZ && !force) {
531 sz = tf->tf_size < PKG_MAXCACHESZ ? tf->tf_size : PKG_MAXCACHESZ;
532 /* All files are padded to a multiple of 512 bytes. */
533 sz = (sz + 0x1ff) & ~0x1ff;
535 tf->tf_cache = malloc(sz);
536 if (tf->tf_cache == NULL) {
537 DBG(("%s: could not allocate %d bytes\n", __func__, (int)sz));
543 return (get_zipped(pkg, tf->tf_cache, sz));
547 * Note that this implementation does not (and should not!) obey
548 * locale settings; you cannot simply substitute strtol here, since
549 * it does obey locale.
552 pkg_atol8(const char *p, unsigned char_cnt)
554 int64_t l, limit, last_digit_limit;
555 int digit, sign, base;
558 limit = INT64_MAX / base;
559 last_digit_limit = INT64_MAX % base;
561 while (*p == ' ' || *p == '\t')
571 while (digit >= 0 && digit < base && char_cnt-- > 0) {
572 if (l>limit || (l == limit && digit > last_digit_limit)) {
573 l = UINT64_MAX; /* Truncate on overflow. */
576 l = (l * base) + digit;
579 return (sign < 0) ? -l : l;
583 * Parse a base-256 integer. This is just a straight signed binary
584 * value in big-endian order, except that the high-order bit is
585 * ignored. Remember that "int64_t" may or may not be exactly 64
586 * bits; the implementation here tries to avoid making any assumptions
587 * about the actual size of an int64_t. It does assume we're using
588 * twos-complement arithmetic, though.
591 pkg_atol256(const char *_p, unsigned char_cnt)
593 int64_t l, upper_limit, lower_limit;
594 const unsigned char *p = (const unsigned char *)_p;
596 upper_limit = INT64_MAX / 256;
597 lower_limit = INT64_MIN / 256;
599 /* Pad with 1 or 0 bits, depending on sign. */
600 if ((0x40 & *p) == 0x40)
604 l = (l << 6) | (0x3f & *p++);
605 while (--char_cnt > 0) {
606 if (l > upper_limit) {
607 l = INT64_MAX; /* Truncate on overflow */
609 } else if (l < lower_limit) {
613 l = (l << 8) | (0xff & (int64_t)*p++);
619 pkg_atol(const char *p, unsigned char_cnt)
622 * Technically, GNU pkg considers a field to be in base-256
623 * only if the first byte is 0xff or 0x80.
626 return (pkg_atol256(p, char_cnt));
627 return (pkg_atol8(p, char_cnt));
631 get_mode(struct tarfile *tf)
633 return (pkg_atol(tf->tf_hdr.ut_mode, sizeof(tf->tf_hdr.ut_mode)));
637 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
638 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
639 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
640 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
641 #define COMMENT 0x10 /* bit 4 set: file comment present */
642 #define RESERVED 0xE0 /* bits 5..7: reserved */
645 new_package(int fd, struct package **pp)
651 pkg = malloc(sizeof(*pkg));
655 bzero(pkg, sizeof(*pkg));
665 if (get_byte(pkg, &ofs) != 0x1f || get_byte(pkg, &ofs) != 0x8b)
668 if (get_byte(pkg, &ofs) != Z_DEFLATED)
671 flags = get_byte(pkg, &ofs);
672 if (flags & RESERVED)
675 /* Skip time, xflags and OS code. */
676 for (i = 0; i < 6; i++) {
677 if (get_byte(pkg, &ofs) == -1)
681 /* Skip extra field. */
682 if (flags & EXTRA_FIELD) {
683 i = (get_byte(pkg, &ofs) & 0xff) |
684 ((get_byte(pkg, &ofs) << 8) & 0xff);
686 if (get_byte(pkg, &ofs) == -1)
691 /* Skip original file name. */
692 if (flags & ORIG_NAME) {
694 i = get_byte(pkg, &ofs);
695 } while (i != 0 && i != -1);
700 /* Print the comment if it's there. */
701 if (flags & COMMENT) {
703 i = get_byte(pkg, &ofs);
713 if (flags & HEAD_CRC) {
714 if (get_byte(pkg, &ofs) == -1)
716 if (get_byte(pkg, &ofs) == -1)
721 * Done parsing the ZIP header. Spkgt the inflation engine.
723 error = inflateInit2(&pkg->pkg_zs, -15);
735 static struct tarfile *
736 scan_tarfile(struct package *pkg, struct tarfile *last)
743 cur = (last != NULL) ? last->tf_next : pkg->pkg_first;
745 ofs = (last != NULL) ? last->tf_ofs + last->tf_size :
747 ofs = (ofs + 0x1ff) & ~0x1ff;
749 /* Check if we've reached EOF. */
750 if (ofs < pkg->pkg_ofs) {
755 if (ofs != pkg->pkg_ofs) {
756 if (last != NULL && pkg->pkg_ofs == last->tf_ofs) {
757 if (cache_data(last, 0) == -1)
760 sz = ofs - pkg->pkg_ofs;
762 if (sz > sizeof(buf))
764 if (get_zipped(pkg, buf, sz) == -1)
766 sz = ofs - pkg->pkg_ofs;
771 cur = malloc(sizeof(*cur));
774 memset(cur, 0, sizeof(*cur));
778 if (get_zipped(pkg, &cur->tf_hdr,
779 sizeof(cur->tf_hdr)) == -1) {
785 * There are always 2 empty blocks appended to
786 * a PKG. It marks the end of the archive.
788 if (strncmp(cur->tf_hdr.ut_magic, "ustar", 5) != 0) {
794 cur->tf_ofs = pkg->pkg_ofs;
795 cur->tf_size = pkg_atol(cur->tf_hdr.ut_size,
796 sizeof(cur->tf_hdr.ut_size));
798 if (cur->tf_hdr.ut_name[0] != '+')
802 * Skip package meta-files.
804 ofs = cur->tf_ofs + cur->tf_size;
805 ofs = (ofs + 0x1ff) & ~0x1ff;
806 while (pkg->pkg_ofs < ofs) {
807 if (get_zipped(pkg, buf, sizeof(buf)) == -1) {
817 pkg->pkg_first = cur;