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>
31 #include <sys/stdint.h>
36 #define DBG(x) printf x
41 static int pkg_open(const char *, struct open_file *);
42 static int pkg_close(struct open_file *);
43 static int pkg_read(struct open_file *, void *, size_t, size_t *);
44 static off_t pkg_seek(struct open_file *, off_t, int);
45 static int pkg_stat(struct open_file *, struct stat *);
46 static int pkg_readdir(struct open_file *, struct dirent *);
47 static off_t pkg_atol(const char *, unsigned);
49 struct fs_ops pkgfs_fsops = {
52 .fo_close = pkg_close,
54 .fo_write = null_write,
57 .fo_readdir = pkg_readdir,
60 #define PKG_BUFSIZE 512
61 #define PKG_MAXCACHESZ (512 * 1024)
63 #define PKG_FILEEXT ".tgz"
66 * Layout of POSIX 'ustar' header.
77 char ut_linkname[100];
78 char ut_magic[6]; /* For POSIX: "ustar\0" */
79 char ut_version[2]; /* For POSIX: "00" */
102 u_char __padding[12];
109 struct package *tf_pkg;
110 struct tarfile *tf_next;
111 struct ustar_hdr tf_hdr;
121 struct package *pkg_chain;
125 struct tarfile *pkg_first;
126 struct tarfile *pkg_last;
127 u_char pkg_buf[PKG_BUFSIZE];
130 static struct package *package = NULL;
132 static int new_package(int, struct package **);
133 static int cache_data(struct tarfile *tf, int);
138 struct package *chain;
139 struct tarfile *tf, *tfn;
141 while (package != NULL) {
142 inflateEnd(&package->pkg_zs);
143 close(package->pkg_fd);
145 tf = package->pkg_first;
148 if (tf->tf_cachesz > 0)
154 chain = package->pkg_chain;
161 pkgfs_init(const char *pkgname, struct fs_ops *proto)
167 if (proto != &pkgfs_fsops)
170 exclusive_file_system = proto;
172 fd = open(pkgname, O_RDONLY);
174 exclusive_file_system = NULL;
179 error = new_package(fd, &pkg);
188 pkg->pkg_chain = package;
190 exclusive_file_system = &pkgfs_fsops;
194 static int get_mode(struct tarfile *);
195 static int get_zipped(struct package *, void *, size_t);
196 static int new_package(int, struct package **);
197 static struct tarfile *scan_tarfile(struct package *, struct tarfile *);
200 pkg_open_follow(const char *fn, struct open_file *f, int lnks)
204 if (fn == NULL || f == NULL)
211 * We can only read from a package, so reject request to open
212 * for write-only or read-write.
214 if (f->f_flags != F_READ)
218 * Scan the file headers for the named file. We stop scanning
219 * at the first filename that has the .pkg extension. This is
220 * a package within a package. We assume we have all the files
221 * we need up-front and without having to dig within nested
224 * Note that we preserve streaming properties as much as possible.
230 * Allow opening of the root directory for use by readdir()
231 * to support listing files in the package.
238 tf = scan_tarfile(package, NULL);
240 if (strcmp(fn, tf->tf_hdr.ut_name) == 0) {
242 tf->tf_fp = 0; /* Reset the file pointer. */
243 DBG(("%s: found %s type %c\n", __func__,
244 fn, tf->tf_hdr.ut_typeflag[0]));
245 if (tf->tf_hdr.ut_typeflag[0] == '2') {
247 * Note: ut_linkname is only 100 chars!
251 return pkg_open_follow(tf->tf_hdr.ut_linkname,
256 tf = scan_tarfile(package, tf);
262 pkg_open(const char *fn, struct open_file *f)
264 return pkg_open_follow(fn, f, 0);
268 pkg_close(struct open_file *f)
272 tf = (struct tarfile *)f->f_fsdata;
277 * Free up the cache if we read all of the file.
279 if (tf->tf_fp == tf->tf_size && tf->tf_cachesz > 0) {
287 pkg_read(struct open_file *f, void *buf, size_t size, size_t *res)
294 tf = (struct tarfile *)f->f_fsdata;
301 if (tf->tf_cachesz == 0)
308 sz = tf->tf_size - fp;
309 if (fp < tf->tf_cachesz && tf->tf_cachesz < tf->tf_size)
310 sz = tf->tf_cachesz - fp;
316 if (fp < tf->tf_cachesz) {
317 /* Satisfy the request from cache. */
318 memcpy(p, tf->tf_cache + fp, sz);
325 if (get_zipped(tf->tf_pkg, p, sz) == -1) {
338 return ((sz == -1) ? errno : 0);
342 pkg_seek(struct open_file *f, off_t ofs, int whence)
351 tf = (struct tarfile *)f->f_fsdata;
359 delta = ofs - tf->tf_fp;
365 delta = tf->tf_size - tf->tf_fp + ofs;
373 /* seeking backwards - ok if within cache */
374 if (tf->tf_cachesz > 0 && tf->tf_fp <= tf->tf_cachesz) {
375 nofs = tf->tf_fp + delta;
381 DBG(("%s: negative file seek (%jd)\n", __func__,
387 while (delta > 0 && tf->tf_fp < tf->tf_size) {
388 sz = (delta > sizeof(buf)) ? sizeof(buf) : delta;
389 error = pkg_read(f, buf, sz, &res);
401 pkg_stat(struct open_file *f, struct stat *sb)
405 tf = (struct tarfile *)f->f_fsdata;
408 memset(sb, 0, sizeof(*sb));
409 sb->st_mode = get_mode(tf);
410 if ((sb->st_mode & S_IFMT) == 0) {
411 /* tar file bug - assume regular file */
412 sb->st_mode |= S_IFREG;
414 sb->st_size = tf->tf_size;
415 sb->st_blocks = (tf->tf_size + 511) / 512;
416 sb->st_mtime = pkg_atol(tf->tf_hdr.ut_mtime, 12);
417 sb->st_dev = (off_t)((uintptr_t)tf->tf_pkg);
418 sb->st_ino = tf->tf_ofs; /* unique per tf_pkg */
423 pkg_readdir(struct open_file *f, struct dirent *d)
427 tf = (struct tarfile *)f->f_fsdata;
431 tf = scan_tarfile(package, NULL);
436 d->d_reclen = sizeof(*d);
438 memcpy(d->d_name, tf->tf_hdr.ut_name, sizeof(d->d_name));
443 * Low-level support functions.
447 get_byte(struct package *pkg, off_t *op)
451 if (pkg->pkg_zs.avail_in == 0) {
452 c = read(pkg->pkg_fd, pkg->pkg_buf, PKG_BUFSIZE);
455 pkg->pkg_zs.avail_in = c;
456 pkg->pkg_zs.next_in = pkg->pkg_buf;
459 c = *pkg->pkg_zs.next_in;
460 pkg->pkg_zs.next_in++;
461 pkg->pkg_zs.avail_in--;
467 get_zipped(struct package *pkg, void *buf, size_t bufsz)
471 pkg->pkg_zs.next_out = buf;
472 pkg->pkg_zs.avail_out = bufsz;
474 while (pkg->pkg_zs.avail_out) {
475 if (pkg->pkg_zs.avail_in == 0) {
476 c = read(pkg->pkg_fd, pkg->pkg_buf, PKG_BUFSIZE);
481 pkg->pkg_zs.avail_in = c;
482 pkg->pkg_zs.next_in = pkg->pkg_buf;
485 c = inflate(&pkg->pkg_zs, Z_SYNC_FLUSH);
486 if (c != Z_OK && c != Z_STREAM_END) {
492 pkg->pkg_ofs += bufsz;
498 * cache data of a tarfile
504 * If file size > PKG_MAXCACHESZ, cache that much
506 * @return 0, -1 (errno set to error value)
509 cache_data(struct tarfile *tf, int force)
515 DBG(("%s: no file to cache data for?\n", __func__));
522 DBG(("%s: no package associated with file?\n", __func__));
527 if (tf->tf_cachesz > 0) {
528 DBG(("%s: data already cached\n", __func__));
533 if (tf->tf_ofs != pkg->pkg_ofs) {
534 DBG(("%s: caching after force read of file %s?\n",
535 __func__, tf->tf_hdr.ut_name));
540 /* We don't cache everything... */
541 if (tf->tf_size > PKG_MAXCACHESZ && !force) {
546 sz = tf->tf_size < PKG_MAXCACHESZ ? tf->tf_size : PKG_MAXCACHESZ;
547 /* All files are padded to a multiple of 512 bytes. */
548 sz = (sz + 0x1ff) & ~0x1ff;
550 tf->tf_cache = malloc(sz);
551 if (tf->tf_cache == NULL) {
552 DBG(("%s: could not allocate %d bytes\n", __func__, (int)sz));
558 return (get_zipped(pkg, tf->tf_cache, sz));
562 * Note that this implementation does not (and should not!) obey
563 * locale settings; you cannot simply substitute strtol here, since
564 * it does obey locale.
567 pkg_atol8(const char *p, unsigned char_cnt)
569 int64_t l, limit, last_digit_limit;
570 int digit, sign, base;
573 limit = INT64_MAX / base;
574 last_digit_limit = INT64_MAX % base;
576 while (*p == ' ' || *p == '\t')
586 while (digit >= 0 && digit < base && char_cnt-- > 0) {
587 if (l>limit || (l == limit && digit > last_digit_limit)) {
588 l = UINT64_MAX; /* Truncate on overflow. */
591 l = (l * base) + digit;
594 return (sign < 0) ? -l : l;
598 * Parse a base-256 integer. This is just a straight signed binary
599 * value in big-endian order, except that the high-order bit is
600 * ignored. Remember that "int64_t" may or may not be exactly 64
601 * bits; the implementation here tries to avoid making any assumptions
602 * about the actual size of an int64_t. It does assume we're using
603 * twos-complement arithmetic, though.
606 pkg_atol256(const char *_p, unsigned char_cnt)
608 int64_t l, upper_limit, lower_limit;
609 const unsigned char *p = (const unsigned char *)_p;
611 upper_limit = INT64_MAX / 256;
612 lower_limit = INT64_MIN / 256;
614 /* Pad with 1 or 0 bits, depending on sign. */
615 if ((0x40 & *p) == 0x40)
619 l = (l << 6) | (0x3f & *p++);
620 while (--char_cnt > 0) {
621 if (l > upper_limit) {
622 l = INT64_MAX; /* Truncate on overflow */
624 } else if (l < lower_limit) {
628 l = (l << 8) | (0xff & (int64_t)*p++);
634 pkg_atol(const char *p, unsigned char_cnt)
637 * Technically, GNU pkg considers a field to be in base-256
638 * only if the first byte is 0xff or 0x80.
641 return (pkg_atol256(p, char_cnt));
642 return (pkg_atol8(p, char_cnt));
646 get_mode(struct tarfile *tf)
648 return (pkg_atol(tf->tf_hdr.ut_mode, sizeof(tf->tf_hdr.ut_mode)));
652 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
653 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
654 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
655 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
656 #define COMMENT 0x10 /* bit 4 set: file comment present */
657 #define RESERVED 0xE0 /* bits 5..7: reserved */
660 new_package(int fd, struct package **pp)
666 pkg = malloc(sizeof(*pkg));
670 bzero(pkg, sizeof(*pkg));
680 if (get_byte(pkg, &ofs) != 0x1f || get_byte(pkg, &ofs) != 0x8b)
683 if (get_byte(pkg, &ofs) != Z_DEFLATED)
686 flags = get_byte(pkg, &ofs);
687 if (flags & RESERVED)
690 /* Skip time, xflags and OS code. */
691 for (i = 0; i < 6; i++) {
692 if (get_byte(pkg, &ofs) == -1)
696 /* Skip extra field. */
697 if (flags & EXTRA_FIELD) {
698 i = (get_byte(pkg, &ofs) & 0xff) |
699 ((get_byte(pkg, &ofs) << 8) & 0xff);
701 if (get_byte(pkg, &ofs) == -1)
706 /* Skip original file name. */
707 if (flags & ORIG_NAME) {
709 i = get_byte(pkg, &ofs);
710 } while (i != 0 && i != -1);
715 /* Print the comment if it's there. */
716 if (flags & COMMENT) {
718 i = get_byte(pkg, &ofs);
728 if (flags & HEAD_CRC) {
729 if (get_byte(pkg, &ofs) == -1)
731 if (get_byte(pkg, &ofs) == -1)
736 * Done parsing the ZIP header. Spkgt the inflation engine.
738 error = inflateInit2(&pkg->pkg_zs, -15);
750 static struct tarfile *
751 scan_tarfile(struct package *pkg, struct tarfile *last)
758 cur = (last != NULL) ? last->tf_next : pkg->pkg_first;
760 ofs = (last != NULL) ? last->tf_ofs + last->tf_size :
762 ofs = (ofs + 0x1ff) & ~0x1ff;
764 /* Check if we've reached EOF. */
765 if (ofs < pkg->pkg_ofs) {
770 if (ofs != pkg->pkg_ofs) {
771 if (last != NULL && pkg->pkg_ofs == last->tf_ofs) {
772 if (cache_data(last, 0) == -1)
775 sz = ofs - pkg->pkg_ofs;
777 if (sz > sizeof(buf))
779 if (get_zipped(pkg, buf, sz) == -1)
781 sz = ofs - pkg->pkg_ofs;
786 cur = malloc(sizeof(*cur));
789 memset(cur, 0, sizeof(*cur));
793 if (get_zipped(pkg, &cur->tf_hdr,
794 sizeof(cur->tf_hdr)) == -1) {
800 * There are always 2 empty blocks appended to
801 * a PKG. It marks the end of the archive.
803 if (strncmp(cur->tf_hdr.ut_magic, "ustar", 5) != 0) {
809 cur->tf_ofs = pkg->pkg_ofs;
810 cur->tf_size = pkg_atol(cur->tf_hdr.ut_size,
811 sizeof(cur->tf_hdr.ut_size));
813 if (cur->tf_hdr.ut_name[0] != '+')
817 * Skip package meta-files.
819 ofs = cur->tf_ofs + cur->tf_size;
820 ofs = (ofs + 0x1ff) & ~0x1ff;
821 while (pkg->pkg_ofs < ofs) {
822 if (get_zipped(pkg, buf, sizeof(buf)) == -1) {
832 pkg->pkg_first = cur;