archive.c 6.15 KB
Newer Older
1 2 3 4 5 6
/* archive.c - Alpine Package Keeper (APK)
 *
 * Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
 * Copyright (C) 2008 Timo Teräs <timo.teras@iki.fi>
 * All rights reserved.
 *
7
 * This program is free software; you can redistribute it and/or modify it
8 9 10 11 12 13 14 15 16
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation. See http://www.gnu.org/ for details.
 */

#include <stdio.h>

#include <err.h>
#include <errno.h>
#include <fcntl.h>
17
#include <utime.h>
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <malloc.h>
#include <string.h>
#include <unistd.h>
#include <sysexits.h>
#include <sys/wait.h>

#include "apk_defines.h"
#include "apk_archive.h"

struct tar_header {
	/* ustar header, Posix 1003.1 */
	char name[100];     /*   0-99 */
	char mode[8];       /* 100-107 */
	char uid[8];        /* 108-115 */
	char gid[8];        /* 116-123 */
	char size[12];      /* 124-135 */
	char mtime[12];     /* 136-147 */
	char chksum[8];     /* 148-155 */
	char typeflag;      /* 156-156 */
	char linkname[100]; /* 157-256 */
	char magic[8];      /* 257-264 */
	char uname[32];     /* 265-296 */
	char gname[32];     /* 297-328 */
	char devmajor[8];   /* 329-336 */
	char devminor[8];   /* 337-344 */
	char prefix[155];   /* 345-499 */
	char padding[12];   /* 500-512 */
};

47 48 49 50
#define GET_OCTAL(s) get_octal(s, sizeof(s))
static int get_octal(char *s, size_t l)
{
	apk_blob_t b = APK_BLOB_PTR_LEN(s, l);
51
	return apk_blob_pull_uint(&b, 8);
52
}
53

54 55 56 57
struct apk_tar_entry_istream {
	struct apk_istream is;
	struct apk_istream *tar_is;
	size_t bytes_left;
58 59
	csum_ctx_t csum_ctx;
	csum_p csum;
60
};
61

62
static size_t tar_entry_read(void *stream, void *ptr, size_t size)
63
{
64 65 66 67 68 69
	struct apk_tar_entry_istream *teis =
		container_of(stream, struct apk_tar_entry_istream, is);

	if (size > teis->bytes_left)
		size = teis->bytes_left;
	size = teis->tar_is->read(teis->tar_is, ptr, size);
70
	if (size > 0) {
71
		teis->bytes_left -= size;
72 73 74 75
		csum_process(&teis->csum_ctx, ptr, size);
		if (teis->bytes_left == 0)
			csum_finish(&teis->csum_ctx, teis->csum);
	}
76
	return size;
77 78
}

79 80
int apk_parse_tar(struct apk_istream *is, apk_archive_entry_parser parser,
		  void *ctx)
81
{
82
	struct apk_file_info entry;
83 84 85
	struct apk_tar_entry_istream teis = {
		.is.read = tar_entry_read,
		.tar_is = is,
86
		.csum = entry.csum,
87
	};
88 89 90
	struct tar_header buf;
	unsigned long offset = 0;
	int end = 0, r;
91
	size_t toskip;
92 93

	memset(&entry, 0, sizeof(entry));
94
	while ((r = is->read(is, &buf, 512)) == 512) {
95 96
		offset += 512;
		if (buf.name[0] == '\0') {
97 98
			if (end) {
				r = 0;
99
				//break;
100
			}
101 102 103 104
			end++;
			continue;
		}

105
		entry = (struct apk_file_info){
106 107 108
			.size  = GET_OCTAL(buf.size),
			.uid   = GET_OCTAL(buf.uid),
			.gid   = GET_OCTAL(buf.gid),
109
			.mode  = GET_OCTAL(buf.mode) & 07777,
110 111 112 113 114 115 116 117 118 119 120 121 122
			.mtime = GET_OCTAL(buf.mtime),
			.name  = entry.name,
			.uname = buf.uname,
			.gname = buf.gname,
			.device = makedev(GET_OCTAL(buf.devmajor),
					  GET_OCTAL(buf.devminor)),
		};

		switch (buf.typeflag) {
		case 'L':
			if (entry.name != NULL)
				free(entry.name);
			entry.name = malloc(entry.size+1);
123
			is->read(is, entry.name, entry.size);
Timo Teräs's avatar
Timo Teräs committed
124
			entry.name[entry.size] = 0;
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
			offset += entry.size;
			entry.size = 0;
			break;
		case '0':
		case '7': /* regular file */
			entry.mode |= S_IFREG;
			break;
		case '1': /* hard link */
			entry.mode |= S_IFREG;
			entry.link_target = buf.linkname;
			break;
		case '2': /* symbolic link */
			entry.mode |= S_IFLNK;
			entry.link_target = buf.linkname;
			break;
		case '3': /* char device */
			entry.mode |= S_IFCHR;
			break;
Timo Teräs's avatar
Timo Teräs committed
143
		case '4': /* block device */
144 145 146 147 148 149 150 151 152
			entry.mode |= S_IFBLK;
			break;
		case '5': /* directory */
			entry.mode |= S_IFDIR;
			break;
		default:
			break;
		}

153
		teis.bytes_left = entry.size;
154 155 156 157 158
		if (entry.mode & S_IFMT) {
			if (entry.name == NULL)
				entry.name = strdup(buf.name);

			/* callback parser function */
159
			csum_init(&teis.csum_ctx);
160
			r = parser(ctx, &entry, &teis.is);
Timo Teräs's avatar
Timo Teräs committed
161
			free(entry.name);
162 163 164 165 166 167
			if (r != 0)
				return r;

			entry.name = NULL;
		}

168 169 170 171 172 173 174 175
		offset += entry.size - teis.bytes_left;
		toskip = teis.bytes_left;
		if ((offset + toskip) & 511)
			toskip += 512 - ((offset + toskip) & 511);
		offset += toskip;
		if (toskip != 0)
			is->read(is, NULL, toskip);
	}
176

177 178 179
	if (r != 0) {
		apk_error("Bad TAR header (r=%d)", r);
		return -1;
180 181 182 183 184
	}

	return 0;
}

185 186
int apk_parse_tar_gz(struct apk_bstream *bs, apk_archive_entry_parser parser,
		     void *ctx)
187
{
Timo Teräs's avatar
Timo Teräs committed
188 189 190 191 192 193 194 195
	struct apk_istream *is;
	int rc;

	is = apk_bstream_gunzip(bs, FALSE);
	rc = apk_parse_tar(is, parser, ctx);
	is->close(is);

	return rc;
196 197
}

198
int apk_archive_entry_extract(const struct apk_file_info *ae,
199
			      struct apk_istream *is,
200 201
			      const char *fn, apk_progress_cb cb,
			      void *cb_ctx)
202
{
203
	struct utimbuf utb;
204
	int r = -1, fd;
205 206 207 208 209 210 211 212 213

	if (fn == NULL)
		fn = ae->name;

	/* BIG HONKING FIXME */
	unlink(fn);

	switch (ae->mode & S_IFMT) {
	case S_IFDIR:
214
		r = mkdir(fn, ae->mode & 07777);
215 216 217 218 219
		if (r < 0 && errno == EEXIST)
			r = 0;
		break;
	case S_IFREG:
		if (ae->link_target == NULL) {
220 221 222
			fd = open(fn, O_WRONLY | O_CREAT, ae->mode & 07777);
			if (fd < 0) {
				r = -1;
223
				break;
224
			}
225 226
			if (apk_istream_splice(is, fd, ae->size, cb, cb_ctx)
			    == ae->size)
227 228
				r = 0;
			close(fd);
229 230 231 232 233 234 235 236 237 238 239
		} else {
			r = link(ae->link_target, fn);
		}
		break;
	case S_IFLNK:
		r = symlink(ae->link_target, fn);
		break;
	case S_IFSOCK:
	case S_IFBLK:
	case S_IFCHR:
	case S_IFIFO:
240
		r = mknod(fn, ae->mode & 07777, ae->device);
241 242
		break;
	}
243 244 245 246 247
	if (r == 0) {
		if (!S_ISLNK(ae->mode))
			r = chown(fn, ae->uid, ae->gid);
		else
			r = lchown(fn, ae->uid, ae->gid);
248 249 250 251 252 253
		if (r < 0) {
			apk_error("Failed to set ownership on %s: %s",
				  fn, strerror(errno));
			return -errno;
		}

254
		/* chown resets suid bit so we need set it again */
255
		if (ae->mode & 07000) {
256
			r = chmod(fn, ae->mode & 07777);
257 258 259 260 261 262 263 264
			if (r < 0) {
				apk_error("Failed to set file permissions "
					  "on %s: %s",
					  fn, strerror(errno));
				return -errno;
			}
		}

265 266 267 268 269 270 271 272 273
		if (!S_ISLNK(ae->mode)) {
			/* preserve modification time */
			utb.actime = utb.modtime = ae->mtime;
			r = utime(fn, &utb);
			if (r < 0) {
				apk_error("Failed to preserve modification time on %s: %s",
					fn, strerror(errno));
				return -errno;
			}
274
		}
275
	} else {
276 277
		apk_error("Failed to extract %s: %s", ae->name, strerror(errno));
		return -errno;
278
	}
279
	return 0;
280
}