199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2007-2014, Juniper Networks, Inc.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int pkg_open(const char *, struct open_file *);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int pkg_read(struct open_file *, void *, size_t, size_t *);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic off_t pkg_seek(struct open_file *, off_t, int);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int pkg_stat(struct open_file *, struct stat *);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int pkg_readdir(struct open_file *, struct dirent *);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Layout of POSIX 'ustar' header.
199767f8919635c4928607450d9e0abb932109ceToomas Soomepkgfs_init(const char *pkgname, struct fs_ops *proto)
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int get_zipped(struct package *, void *, size_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic struct tarfile *scan_tarfile(struct package *, struct tarfile *);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We can only read from a package, so reject request to open
199767f8919635c4928607450d9e0abb932109ceToomas Soome * for write-only or read-write.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Scan the file headers for the named file. We stop scanning
199767f8919635c4928607450d9e0abb932109ceToomas Soome * at the first filename that has the .pkg extension. This is
199767f8919635c4928607450d9e0abb932109ceToomas Soome * a package within a package. We assume we have all the files
199767f8919635c4928607450d9e0abb932109ceToomas Soome * we need up-front and without having to dig within nested
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note that we preserve streaming properties as much as possible.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Allow opening of the root directory for use by readdir()
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to support listing files in the package.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Free up the cache if we read all of the file.
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tf->tf_fp == tf->tf_size && tf->tf_cachesz > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soomepkg_read(struct open_file *f, void *buf, size_t size, size_t *res)
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (size > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp < tf->tf_cachesz && tf->tf_cachesz < tf->tf_size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Satisfy the request from cache. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome tf->tf_cachesz = (sz <= PKG_MAXCACHESZ) ? sz : PKG_MAXCACHESZ;
199767f8919635c4928607450d9e0abb932109ceToomas Soomepkg_seek(struct open_file *f, off_t ofs, int whence)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome DBG(("%s: negative file seek (%jd)\n", __func__,
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sz = (delta > sizeof(buf)) ? sizeof(buf) : delta;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soomepkg_readdir(struct open_file *f, struct dirent *d)
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->d_reclen = sizeof(*d);
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(d->d_name, tf->tf_hdr.ut_name, sizeof(d->d_name));
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Low-level support functions.
199767f8919635c4928607450d9e0abb932109ceToomas Soome c = read(pkg->pkg_fd, pkg->pkg_buf, PKG_BUFSIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soomeget_zipped(struct package *pkg, void *buf, size_t bufsz)
199767f8919635c4928607450d9e0abb932109ceToomas Soome c = read(pkg->pkg_fd, pkg->pkg_buf, PKG_BUFSIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (c <= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome DBG(("%s: no file to cache data for?\n", __func__));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome DBG(("%s: no package associated with file?\n", __func__));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome DBG(("%s: caching after partial read of file %s?\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* We don't cache everything... */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* All files are padded to a multiple of 512 bytes. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome DBG(("%s: could not allocate %d bytes\n", __func__, (int)sz));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note that this implementation does not (and should not!) obey
199767f8919635c4928607450d9e0abb932109ceToomas Soome * locale settings; you cannot simply substitute strtol here, since
199767f8919635c4928607450d9e0abb932109ceToomas Soome * it does obey locale.
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p == '-') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (digit >= 0 && digit < base && char_cnt-- > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (l>limit || (l == limit && digit > last_digit_limit)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (sign < 0) ? -l : l;
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Parse a base-256 integer. This is just a straight signed binary
199767f8919635c4928607450d9e0abb932109ceToomas Soome * value in big-endian order, except that the high-order bit is
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ignored. Remember that "int64_t" may or may not be exactly 64
199767f8919635c4928607450d9e0abb932109ceToomas Soome * bits; the implementation here tries to avoid making any assumptions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * about the actual size of an int64_t. It does assume we're using
199767f8919635c4928607450d9e0abb932109ceToomas Soome * twos-complement arithmetic, though.
199767f8919635c4928607450d9e0abb932109ceToomas Soome const unsigned char *p = (const unsigned char *)_p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Pad with 1 or 0 bits, depending on sign. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (--char_cnt > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (l < lower_limit) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Technically, GNU pkg considers a field to be in base-256
199767f8919635c4928607450d9e0abb932109ceToomas Soome * only if the first byte is 0xff or 0x80.
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p & 0x80)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (pkg_atol(tf->tf_hdr.ut_mode, sizeof(tf->tf_hdr.ut_mode)));
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* GZip flag byte */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define COMMENT 0x10 /* bit 4 set: file comment present */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Parse the header.
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Check megic. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (get_byte(pkg, &ofs) != 0x1f || get_byte(pkg, &ofs) != 0x8b)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Check method. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Check flags. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Skip time, xflags and OS code. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < 6; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Skip extra field. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (i-- > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Skip original file name. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while (i != 0 && i != -1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (i == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Print the comment if it's there. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (i == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Skip the CRC. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Done parsing the ZIP header. Spkgt the inflation engine.
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic struct tarfile *
199767f8919635c4928607450d9e0abb932109ceToomas Soomescan_tarfile(struct package *pkg, struct tarfile *last)
199767f8919635c4928607450d9e0abb932109ceToomas Soome cur = (last != NULL) ? last->tf_next : pkg->pkg_first;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ofs = (last != NULL) ? last->tf_ofs + last->tf_size :
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Check if we've reached EOF. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (last != NULL && pkg->pkg_ofs == last->tf_ofs) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (sz != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome * There are always 2 empty blocks appended to
199767f8919635c4928607450d9e0abb932109ceToomas Soome * a PKG. It marks the end of the archive.
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strncmp(cur->tf_hdr.ut_magic, "ustar", 5) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Skip package meta-files.