io.c 21.3 KB
Newer Older
1 2 3
/* io.c - Alpine Package Keeper (APK)
 *
 * Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
4
 * Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi>
5 6
 * All rights reserved.
 *
Timo Teräs's avatar
Timo Teräs committed
7
 * This program is free software; you can redistribute it and/or modify it
8 9 10 11
 * 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.
 */

12
#include <errno.h>
13 14
#include <stdio.h>
#include <fcntl.h>
15
#include <endian.h>
16 17
#include <unistd.h>
#include <malloc.h>
18
#include <dirent.h>
19
#include <sys/mman.h>
20
#include <sys/wait.h>
Natanael Copa's avatar
Natanael Copa committed
21
#include <sys/stat.h>
Timo Teräs's avatar
Timo Teräs committed
22
#include <sys/xattr.h>
23 24
#include <pwd.h>
#include <grp.h>
25 26 27

#include "apk_defines.h"
#include "apk_io.h"
28
#include "apk_hash.h"
29

30 31 32 33 34
#if defined(__GLIBC__) || defined(__UCLIBC__)
#define HAVE_FGETPWENT_R
#define HAVE_FGETGRENT_R
#endif

35 36 37
struct apk_fd_istream {
	struct apk_istream is;
	int fd;
38 39
	pid_t pid;
	int (*translate_status)(int status);
40 41
};

42
static ssize_t fdi_read(void *stream, void *ptr, size_t size)
43 44 45
{
	struct apk_fd_istream *fis =
		container_of(stream, struct apk_fd_istream, is);
46
	ssize_t i = 0, r;
47 48 49

	if (ptr == NULL) {
		if (lseek(fis->fd, size, SEEK_CUR) < 0)
50
			return -errno;
51 52 53 54 55 56
		return size;
	}

	while (i < size) {
		r = read(fis->fd, ptr + i, size - i);
		if (r < 0)
57
			return -errno;
58
		if (r == 0)
59
			break;
60 61
		i += r;
	}
62 63 64 65 66 67
	if (i == 0 && fis->pid != 0) {
		int status;
		if (waitpid(fis->pid, &status, 0) == fis->pid)
			i = fis->translate_status(status);
		fis->pid = 0;
	}
68 69 70 71

	return i;
}

Timo Teräs's avatar
Timo Teräs committed
72
static void fdi_close(void *stream)
73 74 75
{
	struct apk_fd_istream *fis =
		container_of(stream, struct apk_fd_istream, is);
76
	int status;
77 78

	close(fis->fd);
79 80
	if (fis->pid != 0)
		waitpid(fis->pid, &status, 0);
81 82 83
	free(fis);
}

84
struct apk_istream *apk_istream_from_fd_pid(int fd, pid_t pid, int (*translate_status)(int))
85 86 87
{
	struct apk_fd_istream *fis;

88
	if (fd < 0) return ERR_PTR(-EBADF);
Timo Teräs's avatar
Timo Teräs committed
89

90
	fis = malloc(sizeof(struct apk_fd_istream));
91 92
	if (fis == NULL) {
		close(fd);
93
		return ERR_PTR(-ENOMEM);
94
	}
95 96

	*fis = (struct apk_fd_istream) {
Timo Teräs's avatar
Timo Teräs committed
97 98
		.is.read = fdi_read,
		.is.close = fdi_close,
99
		.fd = fd,
100 101
		.pid = pid,
		.translate_status = translate_status,
102 103 104 105 106
	};

	return &fis->is;
}

107
struct apk_istream *apk_istream_from_file(int atfd, const char *file)
108 109 110
{
	int fd;

111
	fd = openat(atfd, file, O_RDONLY | O_CLOEXEC);
112
	if (fd < 0) return ERR_PTR(-errno);
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

	return apk_istream_from_fd(fd);
}

size_t apk_istream_skip(struct apk_istream *is, size_t size)
{
	unsigned char buf[2048];
	size_t done = 0, r, togo;

	while (done < size) {
		togo = size - done;
		if (togo > sizeof(buf))
			togo = sizeof(buf);
		r = is->read(is, buf, togo);
		if (r < 0)
			return r;
		done += r;
		if (r != togo)
			break;
	}
	return done;
}

136 137
size_t apk_istream_splice(void *stream, int fd, size_t size,
			  apk_progress_cb cb, void *cb_ctx)
138
{
139
	static void *splice_buffer = NULL;
140
	struct apk_istream *is = (struct apk_istream *) stream;
141 142
	unsigned char *buf, *mmapbase = MAP_FAILED;
	size_t bufsz, done = 0, r, togo;
143 144

	bufsz = size;
145
	if (size > 128 * 1024) {
146
		if (size != APK_SPLICE_ALL && ftruncate(fd, size) == 0)
147 148 149 150 151
			mmapbase = mmap(NULL, size, PROT_READ | PROT_WRITE,
					MAP_SHARED, fd, 0);
		if (bufsz > 2*1024*1024)
			bufsz = 2*1024*1024;
		buf = mmapbase;
152
	}
153
	if (mmapbase == MAP_FAILED) {
154 155 156
		if (splice_buffer == NULL)
			splice_buffer = malloc(256*1024);
		buf = splice_buffer;
157 158
		if (buf == NULL)
			return -ENOMEM;
159 160
		if (bufsz > 256*1024)
			bufsz = 256*1024;
161
	}
162 163

	while (done < size) {
164 165
		if (cb != NULL)
			cb(cb_ctx, done);
166

167
		togo = size - done;
168 169
		if (togo > bufsz)
			togo = bufsz;
170 171
		r = is->read(is, buf, togo);
		if (r < 0)
172
			goto err;
173 174
		if (r == 0)
			break;
175

176
		if (mmapbase == MAP_FAILED) {
177 178 179 180 181
			if (write(fd, buf, r) != r) {
				if (r < 0)
					r = -errno;
				goto err;
			}
182 183
		} else
			buf += r;
184

185 186 187 188
		done += r;
		if (r != togo)
			break;
	}
189 190
	r = done;
err:
191 192
	if (mmapbase != MAP_FAILED)
		munmap(mmapbase, size);
193
	return r;
194 195 196 197 198
}

struct apk_istream_bstream {
	struct apk_bstream bs;
	struct apk_istream *is;
199 200
	apk_blob_t left;
	char buffer[8*1024];
Timo Teräs's avatar
Timo Teräs committed
201
	size_t size;
202 203
};

204
static apk_blob_t is_bs_read(void *stream, apk_blob_t token)
205 206 207
{
	struct apk_istream_bstream *isbs =
		container_of(stream, struct apk_istream_bstream, bs);
208 209 210 211 212 213 214 215 216 217 218 219
	ssize_t size;
	apk_blob_t ret;

	/* If we have cached stuff, first check if it full fills the request */
	if (isbs->left.len != 0) {
		if (!APK_BLOB_IS_NULL(token)) {
			/* If we have tokenized thingy left, return it */
			if (apk_blob_split(isbs->left, token, &ret, &isbs->left))
				goto ret;
		} else
			goto ret_all;
	}
220

221
	/* If we've exchausted earlier, it's end of stream or error */
222
	if (APK_BLOB_IS_NULL(isbs->left))
223
		return isbs->left;
224

225 226 227 228 229 230 231 232 233 234 235 236 237
	/* We need more data */
	if (isbs->left.len != 0)
		memcpy(isbs->buffer, isbs->left.ptr, isbs->left.len);
	isbs->left.ptr = isbs->buffer;
	size = isbs->is->read(isbs->is, isbs->buffer + isbs->left.len,
			      sizeof(isbs->buffer) - isbs->left.len);
	if (size > 0) {
		isbs->size += size;
		isbs->left.len += size;
	} else if (size == 0) {
		if (isbs->left.len == 0)
			isbs->left = APK_BLOB_NULL;
		goto ret_all;
238 239 240 241
	} else {
		/* cache and return error */
		isbs->left = ret = APK_BLOB_ERROR(size);
		goto ret;
242
	}
243

244 245 246 247 248 249 250 251 252 253 254 255 256
	if (!APK_BLOB_IS_NULL(token)) {
		/* If we have tokenized thingy left, return it */
		if (apk_blob_split(isbs->left, token, &ret, &isbs->left))
			goto ret;
		/* No token found; just return the full buffer */
	}

ret_all:
	/* Return all that is in cache */
	ret = isbs->left;
	isbs->left.len = 0;
ret:
	return ret;
257 258
}

259
static void is_bs_close(void *stream, size_t *size)
260 261 262 263
{
	struct apk_istream_bstream *isbs =
		container_of(stream, struct apk_istream_bstream, bs);

Timo Teräs's avatar
Timo Teräs committed
264 265 266
	if (size != NULL)
		*size = isbs->size;

267 268 269
	isbs->is->close(isbs->is);
	free(isbs);
}
270

271 272 273 274
struct apk_bstream *apk_bstream_from_istream(struct apk_istream *istream)
{
	struct apk_istream_bstream *isbs;

275 276
	if (IS_ERR_OR_NULL(istream)) return ERR_CAST(istream);

277
	isbs = malloc(sizeof(struct apk_istream_bstream));
278
	if (isbs == NULL) return ERR_PTR(-ENOMEM);
279 280 281 282 283 284

	isbs->bs = (struct apk_bstream) {
		.read = is_bs_read,
		.close = is_bs_close,
	};
	isbs->is = istream;
285 286
	isbs->left = APK_BLOB_PTR_LEN(isbs->buffer, 0),
	isbs->size = 0;
287 288 289 290

	return &isbs->bs;
}

291 292 293 294 295
struct apk_mmap_bstream {
	struct apk_bstream bs;
	int fd;
	size_t size;
	unsigned char *ptr;
296
	apk_blob_t left;
297 298
};

299
static apk_blob_t mmap_read(void *stream, apk_blob_t token)
300 301 302
{
	struct apk_mmap_bstream *mbs =
		container_of(stream, struct apk_mmap_bstream, bs);
303
	apk_blob_t ret;
304

305 306 307 308
	if (!APK_BLOB_IS_NULL(token) && !APK_BLOB_IS_NULL(mbs->left)) {
		if (apk_blob_split(mbs->left, token, &ret, &mbs->left))
			return ret;
	}
309

310 311
	ret = mbs->left;
	mbs->left = APK_BLOB_NULL;
Timo Teräs's avatar
Timo Teräs committed
312
	mbs->bs.flags |= APK_BSTREAM_EOF;
313

314
	return ret;
315 316
}

317
static void mmap_close(void *stream, size_t *size)
318 319 320 321
{
	struct apk_mmap_bstream *mbs =
		container_of(stream, struct apk_mmap_bstream, bs);

Timo Teräs's avatar
Timo Teräs committed
322 323
	if (size != NULL)
		*size = mbs->size;
324
	munmap(mbs->ptr, mbs->size);
Timo Teräs's avatar
Timo Teräs committed
325
	close(mbs->fd);
326 327 328 329 330 331 332 333 334
	free(mbs);
}

static struct apk_bstream *apk_mmap_bstream_from_fd(int fd)
{
	struct apk_mmap_bstream *mbs;
	struct stat st;
	void *ptr;

335
	if (fstat(fd, &st) < 0) return ERR_PTR(-errno);
336

337
	ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
338
	if (ptr == MAP_FAILED) return ERR_PTR(-errno);
339 340 341 342

	mbs = malloc(sizeof(struct apk_mmap_bstream));
	if (mbs == NULL) {
		munmap(ptr, st.st_size);
343
		return ERR_PTR(-ENOMEM);
344 345 346
	}

	mbs->bs = (struct apk_bstream) {
347
		.flags = APK_BSTREAM_SINGLE_READ,
348 349 350 351 352 353
		.read = mmap_read,
		.close = mmap_close,
	};
	mbs->fd = fd;
	mbs->size = st.st_size;
	mbs->ptr = ptr;
354
	mbs->left = APK_BLOB_PTR_LEN(ptr, mbs->size);
355 356 357 358

	return &mbs->bs;
}

359
struct apk_bstream *apk_bstream_from_fd_pid(int fd, pid_t pid, int (*translate_status)(int))
360
{
361 362
	struct apk_bstream *bs;

363
	if (fd < 0) return ERR_PTR(-EBADF);
Timo Teräs's avatar
Timo Teräs committed
364

365 366
	if (pid == 0) {
		bs = apk_mmap_bstream_from_fd(fd);
367
		if (IS_ERR_OR_NULL(bs)) return ERR_CAST(bs);
368
	}
369

370
	return apk_bstream_from_istream(apk_istream_from_fd_pid(fd, pid, translate_status));
371 372
}

373
struct apk_bstream *apk_bstream_from_file(int atfd, const char *file)
Timo Teräs's avatar
Timo Teräs committed
374 375 376
{
	int fd;

377
	fd = openat(atfd, file, O_RDONLY | O_CLOEXEC);
378
	if (fd < 0) return ERR_PTR(-errno);
Timo Teräs's avatar
Timo Teräs committed
379 380 381 382

	return apk_bstream_from_fd(fd);
}

Timo Teräs's avatar
Timo Teräs committed
383 384 385 386 387 388

struct apk_tee_bstream {
	struct apk_bstream bs;
	struct apk_bstream *inner_bs;
	int fd;
	size_t size;
389 390
	apk_progress_cb cb;
	void *cb_ctx;
Timo Teräs's avatar
Timo Teräs committed
391 392
};

393
static apk_blob_t tee_read(void *stream, apk_blob_t token)
Timo Teräs's avatar
Timo Teräs committed
394 395 396
{
	struct apk_tee_bstream *tbs =
		container_of(stream, struct apk_tee_bstream, bs);
397
	apk_blob_t blob;
Timo Teräs's avatar
Timo Teräs committed
398

399
	blob = tbs->inner_bs->read(tbs->inner_bs, token);
400
	if (!APK_BLOB_IS_NULL(blob)) {
401
		tbs->size += write(tbs->fd, blob.ptr, blob.len);
402 403 404
		if (tbs->cb)
			tbs->cb(tbs->cb_ctx, tbs->size);
	}
Timo Teräs's avatar
Timo Teräs committed
405

406
	return blob;
Timo Teräs's avatar
Timo Teräs committed
407 408
}

409
static void tee_close(void *stream, size_t *size)
Timo Teräs's avatar
Timo Teräs committed
410 411 412 413
{
	struct apk_tee_bstream *tbs =
		container_of(stream, struct apk_tee_bstream, bs);

414
	tbs->inner_bs->close(tbs->inner_bs, NULL);
Timo Teräs's avatar
Timo Teräs committed
415 416 417 418 419 420
	if (size != NULL)
		*size = tbs->size;
	close(tbs->fd);
	free(tbs);
}

421
struct apk_bstream *apk_bstream_tee(struct apk_bstream *from, int atfd, const char *to, apk_progress_cb cb, void *cb_ctx)
Timo Teräs's avatar
Timo Teräs committed
422 423 424 425
{
	struct apk_tee_bstream *tbs;
	int fd;

426
	if (IS_ERR_OR_NULL(from)) return ERR_CAST(from);
Timo Teräs's avatar
Timo Teräs committed
427

428
	fd = openat(atfd, to, O_CREAT | O_RDWR | O_TRUNC | O_CLOEXEC,
429
		    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
Timo Teräs's avatar
Timo Teräs committed
430 431
	if (fd < 0) {
		from->close(from, NULL);
Timo Teräs's avatar
Timo Teräs committed
432
		return NULL;
Timo Teräs's avatar
Timo Teräs committed
433
	}
Timo Teräs's avatar
Timo Teräs committed
434 435

	tbs = malloc(sizeof(struct apk_tee_bstream));
Timo Teräs's avatar
Timo Teräs committed
436
	if (!tbs) {
Timo Teräs's avatar
Timo Teräs committed
437
		close(fd);
Timo Teräs's avatar
Timo Teräs committed
438
		from->close(from, NULL);
Timo Teräs's avatar
Timo Teräs committed
439 440 441 442 443 444 445 446 447
		return NULL;
	}

	tbs->bs = (struct apk_bstream) {
		.read = tee_read,
		.close = tee_close,
	};
	tbs->inner_bs = from;
	tbs->fd = fd;
Timo Teräs's avatar
Timo Teräs committed
448
	tbs->size = 0;
449 450
	tbs->cb = cb;
	tbs->cb_ctx = cb_ctx;
Timo Teräs's avatar
Timo Teräs committed
451 452 453 454

	return &tbs->bs;
}

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
apk_blob_t apk_blob_from_istream(struct apk_istream *is, size_t size)
{
	void *ptr;
	size_t rsize;

	ptr = malloc(size);
	if (ptr == NULL)
		return APK_BLOB_NULL;

	rsize = is->read(is, ptr, size);
	if (rsize < 0) {
		free(ptr);
		return APK_BLOB_NULL;
	}
	if (rsize != size)
		ptr = realloc(ptr, rsize);

	return APK_BLOB_PTR_LEN(ptr, rsize);
}

475
apk_blob_t apk_blob_from_file(int atfd, const char *file)
Timo Teräs's avatar
Timo Teräs committed
476 477 478 479 480
{
	int fd;
	struct stat st;
	char *buf;

481
	fd = openat(atfd, file, O_RDONLY | O_CLOEXEC);
Timo Teräs's avatar
Timo Teräs committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
	if (fd < 0)
		return APK_BLOB_NULL;

	if (fstat(fd, &st) < 0)
		goto err_fd;

	buf = malloc(st.st_size);
	if (buf == NULL)
		goto err_fd;

	if (read(fd, buf, st.st_size) != st.st_size)
		goto err_read;

	close(fd);
	return APK_BLOB_PTR_LEN(buf, st.st_size);
err_read:
	free(buf);
err_fd:
	close(fd);
	return APK_BLOB_NULL;
}

504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
int apk_blob_to_file(int atfd, const char *file, apk_blob_t b, unsigned int flags)
{
	int fd, r, len;

	fd = openat(atfd, file, O_CREAT | O_WRONLY | O_CLOEXEC, 0644);
	if (fd < 0)
		return -errno;

	len = b.len;
	r = write(fd, b.ptr, len);
	if ((r == len) &&
	    (flags & APK_BTF_ADD_EOL) && (b.len == 0 || b.ptr[b.len-1] != '\n')) {
		len = 1;
		r = write(fd, "\n", len);
	}

	if (r < 0)
		r = -errno;
	else if (r != len)
		r = -ENOSPC;
	else
		r = 0;
	close(fd);

	if (r != 0)
		unlinkat(atfd, file, 0);

	return r;
}

534 535 536 537 538 539 540 541 542 543 544 545 546
static int cmp_xattr(const void *p1, const void *p2)
{
	const struct apk_xattr *d1 = p1, *d2 = p2;
	return strcmp(d1->name, d2->name);
}

static void hash_len_data(EVP_MD_CTX *ctx, uint32_t len, const void *ptr)
{
	uint32_t belen = htobe32(len);
	EVP_DigestUpdate(ctx, &belen, sizeof(belen));
	EVP_DigestUpdate(ctx, ptr, len);
}

Timo Teräs's avatar
Timo Teräs committed
547
void apk_fileinfo_hash_xattr_array(struct apk_xattr_array *xattrs, const EVP_MD *md, struct apk_checksum *csum)
548 549 550 551
{
	struct apk_xattr *xattr;
	EVP_MD_CTX mdctx;

Timo Teräs's avatar
Timo Teräs committed
552 553
	if (!xattrs || xattrs->num == 0) {
		csum->type = APK_CHECKSUM_NONE;
554 555 556
		return;
	}

Timo Teräs's avatar
Timo Teräs committed
557
	qsort(xattrs->item, xattrs->num, sizeof(xattrs->item[0]), cmp_xattr);
558 559

	EVP_DigestInit(&mdctx, md);
Timo Teräs's avatar
Timo Teräs committed
560
	foreach_array_item(xattr, xattrs) {
561 562 563
		hash_len_data(&mdctx, strlen(xattr->name), xattr->name);
		hash_len_data(&mdctx, xattr->value.len, xattr->value.ptr);
	}
Timo Teräs's avatar
Timo Teräs committed
564 565 566 567 568 569 570
	csum->type = EVP_MD_CTX_size(&mdctx);
	EVP_DigestFinal(&mdctx, csum->data, NULL);
}

void apk_fileinfo_hash_xattr(struct apk_file_info *fi)
{
	apk_fileinfo_hash_xattr_array(fi->xattrs, apk_checksum_default(), &fi->xattr_csum);
571 572
}

573 574
int apk_fileinfo_get(int atfd, const char *filename, unsigned int flags,
		     struct apk_file_info *fi)
575
{
576
	struct stat64 st;
577
	struct apk_bstream *bs;
Timo Teräs's avatar
Timo Teräs committed
578 579 580
	unsigned int checksum = flags & 0xff;
	unsigned int xattr_checksum = (flags >> 8) & 0xff;
	int atflags = 0;
581

582 583 584 585
	if (flags & APK_FI_NOFOLLOW)
		atflags |= AT_SYMLINK_NOFOLLOW;

	if (fstatat64(atfd, filename, &st, atflags) != 0)
586
		return -errno;
587 588 589 590 591 592 593 594 595 596

	*fi = (struct apk_file_info) {
		.size = st.st_size,
		.uid = st.st_uid,
		.gid = st.st_gid,
		.mode = st.st_mode,
		.mtime = st.st_mtime,
		.device = st.st_dev,
	};

Timo Teräs's avatar
Timo Teräs committed
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
	if (xattr_checksum != APK_CHECKSUM_NONE) {
		ssize_t len, vlen;
		int fd, i, r;
		char val[1024], buf[1024];

		r = 0;
		fd = openat(atfd, filename, O_RDONLY);
		if (fd >= 0) {
			len = flistxattr(fd, buf, sizeof(buf));
			if (len > 0) {
				struct apk_xattr_array *xattrs = NULL;
				apk_xattr_array_init(&xattrs);
				for (i = 0; i < len; i += strlen(&buf[i]) + 1) {
					vlen = fgetxattr(fd, &buf[i], val, sizeof(val));
					if (vlen < 0) {
						r = errno;
						if (r == ENODATA) continue;
						break;
					}
					*apk_xattr_array_add(&xattrs) = (struct apk_xattr) {
						.name = &buf[i],
						.value = *apk_blob_atomize_dup(APK_BLOB_PTR_LEN(val, vlen)),
					};
				}
				apk_fileinfo_hash_xattr_array(xattrs, apk_checksum_evp(xattr_checksum), &fi->xattr_csum);
				apk_xattr_array_free(&xattrs);
623
			} else if (r < 0) r = errno;
Timo Teräs's avatar
Timo Teräs committed
624 625 626 627 628 629 630 631 632 633
			close(fd);
		} else r = errno;

		if (r && r != ENOTSUP) return -r;
	}

	if (checksum == APK_CHECKSUM_NONE)
		return 0;

	if (S_ISDIR(st.st_mode))
Timo Teräs's avatar
Timo Teräs committed
634 635
		return 0;

Timo Teräs's avatar
Timo Teräs committed
636
	/* Checksum file content */
637 638 639 640 641 642 643 644 645 646
	if ((flags & APK_FI_NOFOLLOW) && S_ISLNK(st.st_mode)) {
		char *target = alloca(st.st_size);
		if (target == NULL)
			return -ENOMEM;
		if (readlinkat(atfd, filename, target, st.st_size) < 0)
			return -errno;

		EVP_Digest(target, st.st_size, fi->csum.data, NULL,
			   apk_checksum_evp(checksum), NULL);
		fi->csum.type = checksum;
Timo Teräs's avatar
Timo Teräs committed
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
	} else {
		bs = apk_bstream_from_file(atfd, filename);
		if (!IS_ERR_OR_NULL(bs)) {
			EVP_MD_CTX mdctx;
			apk_blob_t blob;

			EVP_DigestInit(&mdctx, apk_checksum_evp(checksum));
			if (bs->flags & APK_BSTREAM_SINGLE_READ)
				EVP_MD_CTX_set_flags(&mdctx, EVP_MD_CTX_FLAG_ONESHOT);
			while (!APK_BLOB_IS_NULL(blob = bs->read(bs, APK_BLOB_NULL)))
				EVP_DigestUpdate(&mdctx, (void*) blob.ptr, blob.len);
			fi->csum.type = EVP_MD_CTX_size(&mdctx);
			EVP_DigestFinal(&mdctx, fi->csum.data, NULL);

			bs->close(bs, NULL);
		}
663
	}
664 665 666

	return 0;
}
Timo Teräs's avatar
Timo Teräs committed
667

668
void apk_fileinfo_free(struct apk_file_info *fi)
Timo Teräs's avatar
Timo Teräs committed
669 670 671 672
{
	apk_xattr_array_free(&fi->xattrs);
}

673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
int apk_dir_foreach_file(int dirfd, apk_dir_file_cb cb, void *ctx)
{
	struct dirent *de;
	DIR *dir;

	if (dirfd < 0)
		return -1;

	dir = fdopendir(dirfd);
	if (dir == NULL)
		return -1;

	/* We get called here with dup():ed fd. Since they all refer to
	 * same object, we need to rewind so subsequent calls work. */
	rewinddir(dir);

	while ((de = readdir(dir)) != NULL) {
690 691 692 693 694
		if (de->d_name[0] == '.') {
			if (de->d_name[1] == 0 ||
			    (de->d_name[1] == '.' && de->d_name[2] == 0))
				continue;
		}
695
		cb(ctx, dirfd, de->d_name);
696 697 698 699 700 701
	}
	closedir(dir);

	return 0;
}

702
struct apk_istream *apk_istream_from_file_gz(int atfd, const char *file)
Timo Teräs's avatar
Timo Teräs committed
703
{
704
	return apk_bstream_gunzip(apk_bstream_from_file(atfd, file));
Timo Teräs's avatar
Timo Teräs committed
705 706 707 708
}

struct apk_fd_ostream {
	struct apk_ostream os;
709 710 711 712 713
	int fd, rc;

	const char *file, *tmpfile;
	int atfd;

Timo Teräs's avatar
Timo Teräs committed
714 715
	size_t bytes;
	char buffer[1024];
Timo Teräs's avatar
Timo Teräs committed
716 717
};

718
static ssize_t safe_write(int fd, const void *ptr, size_t size)
Timo Teräs's avatar
Timo Teräs committed
719
{
720
	ssize_t i = 0, r;
Timo Teräs's avatar
Timo Teräs committed
721 722

	while (i < size) {
Timo Teräs's avatar
Timo Teräs committed
723
		r = write(fd, ptr + i, size - i);
Timo Teräs's avatar
Timo Teräs committed
724
		if (r < 0)
725
			return -errno;
Timo Teräs's avatar
Timo Teräs committed
726 727 728 729 730 731 732 733
		if (r == 0)
			return i;
		i += r;
	}

	return i;
}

734
static ssize_t fdo_flush(struct apk_fd_ostream *fos)
Timo Teräs's avatar
Timo Teräs committed
735
{
736 737
	ssize_t r;

Timo Teräs's avatar
Timo Teräs committed
738 739 740
	if (fos->bytes == 0)
		return 0;

741 742
	if ((r = safe_write(fos->fd, fos->buffer, fos->bytes)) != fos->bytes) {
		fos->rc = r < 0 ? r : -EIO;
743
		return r;
744
	}
Timo Teräs's avatar
Timo Teräs committed
745 746 747 748 749

	fos->bytes = 0;
	return 0;
}

750
static ssize_t fdo_write(void *stream, const void *ptr, size_t size)
Timo Teräs's avatar
Timo Teräs committed
751 752 753
{
	struct apk_fd_ostream *fos =
		container_of(stream, struct apk_fd_ostream, os);
754
	ssize_t r;
Timo Teräs's avatar
Timo Teräs committed
755 756

	if (size + fos->bytes >= sizeof(fos->buffer)) {
757 758 759
		r = fdo_flush(fos);
		if (r != 0)
			return r;
760 761 762 763 764 765
		if (size >= sizeof(fos->buffer) / 2) {
			r = safe_write(fos->fd, ptr, size);
			if (r != size)
				fos->rc = r < 0 ? r : -EIO;
			return r;
		}
Timo Teräs's avatar
Timo Teräs committed
766 767 768 769 770 771 772 773
	}

	memcpy(&fos->buffer[fos->bytes], ptr, size);
	fos->bytes += size;

	return size;
}

774
static int fdo_close(void *stream)
Timo Teräs's avatar
Timo Teräs committed
775 776 777
{
	struct apk_fd_ostream *fos =
		container_of(stream, struct apk_fd_ostream, os);
778
	int rc = fos->rc;
Timo Teräs's avatar
Timo Teräs committed
779

Timo Teräs's avatar
Timo Teräs committed
780
	fdo_flush(fos);
781 782 783 784 785 786 787 788 789 790 791 792
	if (fos->fd > STDERR_FILENO &&
	    close(fos->fd) < 0)
		rc = -errno;

	if (fos->tmpfile != NULL) {
		if (rc == 0)
			renameat(fos->atfd, fos->tmpfile,
				 fos->atfd, fos->file);
		else
			unlinkat(fos->atfd, fos->tmpfile, 0);
	}

Timo Teräs's avatar
Timo Teräs committed
793
	free(fos);
794 795

	return rc;
Timo Teräs's avatar
Timo Teräs committed
796 797 798 799 800 801
}

struct apk_ostream *apk_ostream_to_fd(int fd)
{
	struct apk_fd_ostream *fos;

802
	if (fd < 0) return ERR_PTR(-EBADF);
Timo Teräs's avatar
Timo Teräs committed
803 804

	fos = malloc(sizeof(struct apk_fd_ostream));
805 806
	if (fos == NULL) {
		close(fd);
807
		return ERR_PTR(-ENOMEM);
808
	}
Timo Teräs's avatar
Timo Teräs committed
809 810 811 812 813 814 815 816 817 818

	*fos = (struct apk_fd_ostream) {
		.os.write = fdo_write,
		.os.close = fdo_close,
		.fd = fd,
	};

	return &fos->os;
}

819 820 821 822
struct apk_ostream *apk_ostream_to_file(int atfd,
					const char *file,
					const char *tmpfile,
					mode_t mode)
Timo Teräs's avatar
Timo Teräs committed
823
{
824
	struct apk_ostream *os;
Timo Teräs's avatar
Timo Teräs committed
825 826
	int fd;

827
	fd = openat(atfd, tmpfile ?: file, O_CREAT | O_RDWR | O_TRUNC | O_CLOEXEC, mode);
828
	if (fd < 0) return ERR_PTR(-errno);
Timo Teräs's avatar
Timo Teräs committed
829 830 831

	fcntl(fd, F_SETFD, FD_CLOEXEC);

832
	os = apk_ostream_to_fd(fd);
833
	if (IS_ERR_OR_NULL(os)) return ERR_CAST(os);
834 835 836 837 838 839 840 841 842

	if (tmpfile != NULL) {
		struct apk_fd_ostream *fos =
			container_of(os, struct apk_fd_ostream, os);
		fos->file = file;
		fos->tmpfile = tmpfile;
		fos->atfd = atfd;
	}
	return os;
Timo Teräs's avatar
Timo Teräs committed
843 844
}

845 846 847 848 849
struct apk_counter_ostream {
	struct apk_ostream os;
	off_t *counter;
};

850
static ssize_t co_write(void *stream, const void *ptr, size_t size)
851 852 853 854 855 856 857 858
{
	struct apk_counter_ostream *cos =
		container_of(stream, struct apk_counter_ostream, os);

	*cos->counter += size;
	return size;
}

859
static int co_close(void *stream)
860 861 862 863 864
{
	struct apk_counter_ostream *cos =
		container_of(stream, struct apk_counter_ostream, os);

	free(cos);
865
	return 0;
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
}

struct apk_ostream *apk_ostream_counter(off_t *counter)
{
	struct apk_counter_ostream *cos;

	cos = malloc(sizeof(struct apk_counter_ostream));
	if (cos == NULL)
		return NULL;

	*cos = (struct apk_counter_ostream) {
		.os.write = co_write,
		.os.close = co_close,
		.counter = counter,
	};

	return &cos->os;
}

885 886 887 888 889 890 891 892 893 894
size_t apk_ostream_write_string(struct apk_ostream *os, const char *string)
{
	size_t len;

	len = strlen(string);
	if (os->write(os, string, len) != len)
		return -1;

	return len;
}
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941

struct cache_item {
	apk_hash_node hash_node;
	unsigned int genid;
	union {
		uid_t uid;
		gid_t gid;
	};
	unsigned short len;
	char name[];
};

static apk_blob_t cache_item_get_key(apk_hash_item item)
{
	struct cache_item *ci = (struct cache_item *) item;
	return APK_BLOB_PTR_LEN(ci->name, ci->len);
}

static const struct apk_hash_ops id_hash_ops = {
	.node_offset = offsetof(struct cache_item, hash_node),
	.get_key = cache_item_get_key,
	.hash_key = apk_blob_hash,
	.compare = apk_blob_compare,
	.delete_item = (apk_hash_delete_f) free,
};

static struct cache_item *resolve_cache_item(struct apk_hash *hash, apk_blob_t name)
{
	struct cache_item *ci;
	unsigned long h;

	h = id_hash_ops.hash_key(name);
	ci = (struct cache_item *) apk_hash_get_hashed(hash, name, h);
	if (ci != NULL)
		return ci;

	ci = calloc(1, sizeof(struct cache_item) + name.len);
	if (ci == NULL)
		return NULL;

	ci->len = name.len;
	memcpy(ci->name, name.ptr, name.len);
	apk_hash_insert_hashed(hash, ci, h);

	return ci;
}

942
void apk_id_cache_init(struct apk_id_cache *idc, int root_fd)
943
{
944 945 946 947
	idc->root_fd = root_fd;
	idc->genid = 1;
	apk_hash_init(&idc->uid_cache, &id_hash_ops, 256);
	apk_hash_init(&idc->gid_cache, &id_hash_ops, 256);
948 949
}

950
void apk_id_cache_free(struct apk_id_cache *idc)
951
{
952 953
	apk_hash_free(&idc->uid_cache);
	apk_hash_free(&idc->gid_cache);
954 955
}

956
void apk_id_cache_reset(struct apk_id_cache *idc)
957
{
958 959 960
	idc->genid++;
	if (idc->genid == 0)
		idc->genid = 1;
961 962
}

963
uid_t apk_resolve_uid(struct apk_id_cache *idc, const char *username, uid_t default_uid)
964
{
965
#ifdef HAVE_FGETPWENT_R
966
	char buf[1024];
967 968 969 970
	struct passwd pwent;
#endif
	struct cache_item *ci;
	struct passwd *pwd;
971
	FILE *in;
972

973
	ci = resolve_cache_item(&idc->uid_cache, APK_BLOB_STR(username));
974 975 976
	if (ci == NULL)
		return default_uid;

977 978 979 980 981 982 983
	if (ci->genid != idc->genid) {
		ci->genid = idc->genid;
		ci->uid = -1;

		in = fdopen(openat(idc->root_fd, "etc/passwd", O_RDONLY|O_CLOEXEC), "r");
		if (in != NULL) {
			do {
984
#ifdef HAVE_FGETPWENT_R
985
				fgetpwent_r(in, &pwent, buf, sizeof(buf), &pwd);
986 987 988
#else
				pwd = fgetpwent(in);
#endif
989 990 991 992 993 994 995 996 997
				if (pwd == NULL)
					break;
				if (strcmp(pwd->pw_name, username) == 0) {
					ci->uid = pwd->pw_uid;
					break;
				}
			} while (1);
			fclose(in);
		}
998 999 1000 1001 1002 1003 1004 1005
	}

	if (ci->uid != -1)
		return ci->uid;

	return default_uid;
}

1006
uid_t apk_resolve_gid(struct apk_id_cache *idc, const char *groupname, uid_t default_gid)
1007
{
1008
#ifdef HAVE_FGETGRENT_R
1009
	char buf[1024];
1010 1011 1012 1013
	struct group grent;
#endif
	struct cache_item *ci;
	struct group *grp;
1014
	FILE *in;
1015

1016
	ci = resolve_cache_item(&idc->gid_cache, APK_BLOB_STR(groupname));
1017 1018 1019
	if (ci == NULL)
		return default_gid;

1020 1021 1022 1023
	if (ci->genid != idc->genid) {
		ci->genid = idc->genid;
		ci->gid = -1;

Timo Teräs's avatar
Timo Teräs committed
1024
		in = fdopen(openat(idc->root_fd, "etc/group", O_RDONLY|O_CLOEXEC), "r");
1025 1026
		if (in != NULL) {
			do {
1027
#ifdef HAVE_FGETGRENT_R
1028
				fgetgrent_r(in, &grent, buf, sizeof(buf), &grp);
1029 1030 1031
#else
				grp = fgetgrent(in);
#endif
1032 1033 1034 1035 1036 1037 1038 1039 1040
				if (grp == NULL)
					break;
				if (strcmp(grp->gr_name, groupname) == 0) {
					ci->gid = grp->gr_gid;
					break;
				}
			} while (1);
			fclose(in);
		}
1041 1042 1043 1044 1045 1046 1047
	}

	if (ci->gid != -1)
		return ci->gid;

	return default_gid;
}