io.c 16.2 KB
Newer Older
1
2
3
4
5
6
/* io.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.
 *
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
15
16
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <malloc.h>
17
#include <sys/mman.h>
Natanael Copa's avatar
Natanael Copa committed
18
#include <sys/stat.h>
19
20
#include <pwd.h>
#include <grp.h>
21
22
23

#include "apk_defines.h"
#include "apk_io.h"
24
#include "apk_hash.h"
25
26
27
28
29
30

struct apk_fd_istream {
	struct apk_istream is;
	int fd;
};

31
static ssize_t fdi_read(void *stream, void *ptr, size_t size)
32
33
34
{
	struct apk_fd_istream *fis =
		container_of(stream, struct apk_fd_istream, is);
35
	ssize_t i = 0, r;
36
37
38

	if (ptr == NULL) {
		if (lseek(fis->fd, size, SEEK_CUR) < 0)
39
			return -errno;
40
41
42
43
44
45
		return size;
	}

	while (i < size) {
		r = read(fis->fd, ptr + i, size - i);
		if (r < 0)
46
			return -errno;
47
48
49
50
51
52
53
54
		if (r == 0)
			return i;
		i += r;
	}

	return i;
}

Timo Teräs's avatar
Timo Teräs committed
55
static void fdi_close(void *stream)
56
57
58
59
60
61
62
63
64
65
66
67
{
	struct apk_fd_istream *fis =
		container_of(stream, struct apk_fd_istream, is);

	close(fis->fd);
	free(fis);
}

struct apk_istream *apk_istream_from_fd(int fd)
{
	struct apk_fd_istream *fis;

Timo Teräs's avatar
Timo Teräs committed
68
69
70
	if (fd < 0)
		return NULL;

71
	fis = malloc(sizeof(struct apk_fd_istream));
72
73
	if (fis == NULL) {
		close(fd);
74
		return NULL;
75
	}
76
77

	*fis = (struct apk_fd_istream) {
Timo Teräs's avatar
Timo Teräs committed
78
79
		.is.read = fdi_read,
		.is.close = fdi_close,
80
81
82
83
84
85
		.fd = fd,
	};

	return &fis->is;
}

86
struct apk_istream *apk_istream_from_file(int atfd, const char *file)
87
88
89
{
	int fd;

90
	fd = openat(atfd, file, O_RDONLY | O_CLOEXEC);
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
	if (fd < 0)
		return NULL;

	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;
}

116
117
size_t apk_istream_splice(void *stream, int fd, size_t size,
			  apk_progress_cb cb, void *cb_ctx)
118
{
119
	static void *splice_buffer = NULL;
120
	struct apk_istream *is = (struct apk_istream *) stream;
121
122
	unsigned char *buf, *mmapbase = MAP_FAILED;
	size_t bufsz, done = 0, r, togo;
123
124

	bufsz = size;
125
126
	if (size > 128 * 1024) {
		if (ftruncate(fd, size) == 0)
127
128
129
130
131
			mmapbase = mmap(NULL, size, PROT_READ | PROT_WRITE,
					MAP_SHARED, fd, 0);
		if (bufsz > 2*1024*1024)
			bufsz = 2*1024*1024;
		buf = mmapbase;
132
	}
133
	if (mmapbase == MAP_FAILED) {
134
135
136
		if (splice_buffer == NULL)
			splice_buffer = malloc(256*1024);
		buf = splice_buffer;
137
138
		if (buf == NULL)
			return -ENOMEM;
139
140
		if (bufsz > 256*1024)
			bufsz = 256*1024;
141
	}
142
143

	while (done < size) {
144
145
146
		if (done != 0 && cb != NULL)
			cb(cb_ctx, muldiv(APK_PROGRESS_SCALE, done, size));

147
		togo = size - done;
148
149
		if (togo > bufsz)
			togo = bufsz;
150
151
		r = is->read(is, buf, togo);
		if (r < 0)
152
			goto err;
153

154
		if (mmapbase == MAP_FAILED) {
155
156
157
158
159
			if (write(fd, buf, r) != r) {
				if (r < 0)
					r = -errno;
				goto err;
			}
160
161
		} else
			buf += r;
162

163
164
165
166
		done += r;
		if (r != togo)
			break;
	}
167
168
	r = done;
err:
169
170
	if (mmapbase != MAP_FAILED)
		munmap(mmapbase, size);
171
	return r;
172
173
174
175
176
}

struct apk_istream_bstream {
	struct apk_bstream bs;
	struct apk_istream *is;
177
178
	apk_blob_t left;
	char buffer[8*1024];
Timo Teräs's avatar
Timo Teräs committed
179
	size_t size;
180
181
};

182
static apk_blob_t is_bs_read(void *stream, apk_blob_t token)
183
184
185
{
	struct apk_istream_bstream *isbs =
		container_of(stream, struct apk_istream_bstream, bs);
186
187
188
189
190
191
192
193
194
195
196
197
	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;
	}
198

199
200
201
	/* If we've exchausted earlier, it's end of stream */
	if (APK_BLOB_IS_NULL(isbs->left))
		return APK_BLOB_NULL;
202

203
204
205
206
207
208
209
210
211
212
213
214
215
216
	/* 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;
	}
217

218
219
220
221
222
223
224
225
226
227
228
229
230
	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;
231
232
}

233
static void is_bs_close(void *stream, size_t *size)
234
235
236
237
{
	struct apk_istream_bstream *isbs =
		container_of(stream, struct apk_istream_bstream, bs);

Timo Teräs's avatar
Timo Teräs committed
238
239
240
	if (size != NULL)
		*size = isbs->size;

241
242
243
	isbs->is->close(isbs->is);
	free(isbs);
}
244

245
246
247
248
249
250
251
252
253
254
255
256
257
struct apk_bstream *apk_bstream_from_istream(struct apk_istream *istream)
{
	struct apk_istream_bstream *isbs;

	isbs = malloc(sizeof(struct apk_istream_bstream));
	if (isbs == NULL)
		return NULL;

	isbs->bs = (struct apk_bstream) {
		.read = is_bs_read,
		.close = is_bs_close,
	};
	isbs->is = istream;
258
259
	isbs->left = APK_BLOB_PTR_LEN(isbs->buffer, 0),
	isbs->size = 0;
260
261
262
263

	return &isbs->bs;
}

264
265
266
267
268
struct apk_mmap_bstream {
	struct apk_bstream bs;
	int fd;
	size_t size;
	unsigned char *ptr;
269
	apk_blob_t left;
270
271
};

272
static apk_blob_t mmap_read(void *stream, apk_blob_t token)
273
274
275
{
	struct apk_mmap_bstream *mbs =
		container_of(stream, struct apk_mmap_bstream, bs);
276
	apk_blob_t ret;
277

278
279
280
281
	if (!APK_BLOB_IS_NULL(token) && !APK_BLOB_IS_NULL(mbs->left)) {
		if (apk_blob_split(mbs->left, token, &ret, &mbs->left))
			return ret;
	}
282

283
284
	ret = mbs->left;
	mbs->left = APK_BLOB_NULL;
Timo Teräs's avatar
Timo Teräs committed
285
	mbs->bs.flags |= APK_BSTREAM_EOF;
286

287
	return ret;
288
289
}

290
static void mmap_close(void *stream, size_t *size)
291
292
293
294
{
	struct apk_mmap_bstream *mbs =
		container_of(stream, struct apk_mmap_bstream, bs);

Timo Teräs's avatar
Timo Teräs committed
295
296
297
	if (size != NULL)
		*size = mbs->size;

298
	munmap(mbs->ptr, mbs->size);
Timo Teräs's avatar
Timo Teräs committed
299
	close(mbs->fd);
300
301
302
303
304
305
306
307
308
309
310
311
	free(mbs);
}

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

	if (fstat(fd, &st) < 0)
		return NULL;

312
	ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
313
314
315
316
317
318
319
320
321
322
	if (ptr == MAP_FAILED)
		return NULL;

	mbs = malloc(sizeof(struct apk_mmap_bstream));
	if (mbs == NULL) {
		munmap(ptr, st.st_size);
		return NULL;
	}

	mbs->bs = (struct apk_bstream) {
323
		.flags = APK_BSTREAM_SINGLE_READ,
324
325
326
327
328
329
		.read = mmap_read,
		.close = mmap_close,
	};
	mbs->fd = fd;
	mbs->size = st.st_size;
	mbs->ptr = ptr;
330
	mbs->left = APK_BLOB_PTR_LEN(ptr, mbs->size);
331
332
333
334

	return &mbs->bs;
}

335
336
struct apk_bstream *apk_bstream_from_fd(int fd)
{
337
338
	struct apk_bstream *bs;

Timo Teräs's avatar
Timo Teräs committed
339
340
341
	if (fd < 0)
		return NULL;

342
343
344
345
	bs = apk_mmap_bstream_from_fd(fd);
	if (bs != NULL)
		return bs;

346
347
348
	return apk_bstream_from_istream(apk_istream_from_fd(fd));
}

349
struct apk_bstream *apk_bstream_from_file(int atfd, const char *file)
Timo Teräs's avatar
Timo Teräs committed
350
351
352
{
	int fd;

353
	fd = openat(atfd, file, O_RDONLY | O_CLOEXEC);
Timo Teräs's avatar
Timo Teräs committed
354
355
356
357
358
359
	if (fd < 0)
		return NULL;

	return apk_bstream_from_fd(fd);
}

Timo Teräs's avatar
Timo Teräs committed
360
361
362
363
364
365
366
367

struct apk_tee_bstream {
	struct apk_bstream bs;
	struct apk_bstream *inner_bs;
	int fd;
	size_t size;
};

368
static apk_blob_t tee_read(void *stream, apk_blob_t token)
Timo Teräs's avatar
Timo Teräs committed
369
370
371
{
	struct apk_tee_bstream *tbs =
		container_of(stream, struct apk_tee_bstream, bs);
372
	apk_blob_t blob;
Timo Teräs's avatar
Timo Teräs committed
373

374
375
376
	blob = tbs->inner_bs->read(tbs->inner_bs, token);
	if (!APK_BLOB_IS_NULL(blob))
		tbs->size += write(tbs->fd, blob.ptr, blob.len);
Timo Teräs's avatar
Timo Teräs committed
377

378
	return blob;
Timo Teräs's avatar
Timo Teräs committed
379
380
}

381
static void tee_close(void *stream, size_t *size)
Timo Teräs's avatar
Timo Teräs committed
382
383
384
385
{
	struct apk_tee_bstream *tbs =
		container_of(stream, struct apk_tee_bstream, bs);

386
	tbs->inner_bs->close(tbs->inner_bs, NULL);
Timo Teräs's avatar
Timo Teräs committed
387
388
389
390
391
392
	if (size != NULL)
		*size = tbs->size;
	close(tbs->fd);
	free(tbs);
}

393
struct apk_bstream *apk_bstream_tee(struct apk_bstream *from, int atfd, const char *to)
Timo Teräs's avatar
Timo Teräs committed
394
395
396
397
{
	struct apk_tee_bstream *tbs;
	int fd;

398
	fd = openat(atfd, to, O_CREAT | O_RDWR | O_TRUNC | O_CLOEXEC,
399
		    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
Timo Teräs's avatar
Timo Teräs committed
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
	if (fd < 0)
		return NULL;

	tbs = malloc(sizeof(struct apk_tee_bstream));
	if (tbs == NULL) {
		close(fd);
		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
415
	tbs->size = 0;
Timo Teräs's avatar
Timo Teräs committed
416
417
418
419

	return &tbs->bs;
}

420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
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);
}

440
apk_blob_t apk_blob_from_file(int atfd, const char *file)
Timo Teräs's avatar
Timo Teräs committed
441
442
443
444
445
{
	int fd;
	struct stat st;
	char *buf;

446
	fd = openat(atfd, file, O_RDONLY | O_CLOEXEC);
Timo Teräs's avatar
Timo Teräs committed
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
	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;
}

469
int apk_file_get_info(int atfd, const char *filename, unsigned int flags,
470
		      struct apk_file_info *fi)
471
{
472
	struct stat64 st;
473
	struct apk_bstream *bs;
474
	int checksum = flags & 0xffff, atflags = 0;
475

476
477
478
479
	if (flags & APK_FI_NOFOLLOW)
		atflags |= AT_SYMLINK_NOFOLLOW;

	if (fstatat64(atfd, filename, &st, atflags) != 0)
480
		return -errno;
481
482
483
484
485
486
487
488
489
490

	*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,
	};

491
	if (checksum == APK_CHECKSUM_NONE || S_ISDIR(st.st_mode))
Timo Teräs's avatar
Timo Teräs committed
492
493
		return 0;

494
495
496
497
498
499
500
501
502
503
504
505
506
	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;
		return 0;
	}

507
	bs = apk_bstream_from_file(atfd, filename);
508
	if (bs != NULL) {
Timo Teräs's avatar
Timo Teräs committed
509
		EVP_MD_CTX mdctx;
510
		apk_blob_t blob;
511

512
		EVP_DigestInit(&mdctx, apk_checksum_evp(checksum));
513
514
		if (bs->flags & APK_BSTREAM_SINGLE_READ)
			EVP_MD_CTX_set_flags(&mdctx, EVP_MD_CTX_FLAG_ONESHOT);
515
		while (!APK_BLOB_IS_NULL(blob = bs->read(bs, APK_BLOB_NULL)))
Timo Teräs's avatar
Timo Teräs committed
516
517
518
			EVP_DigestUpdate(&mdctx, (void*) blob.ptr, blob.len);
		fi->csum.type = EVP_MD_CTX_size(&mdctx);
		EVP_DigestFinal(&mdctx, fi->csum.data, NULL);
519
520
521

		bs->close(bs, NULL);
	}
522
523
524

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

526
struct apk_istream *apk_istream_from_file_gz(int atfd, const char *file)
Timo Teräs's avatar
Timo Teräs committed
527
{
528
	return apk_bstream_gunzip(apk_bstream_from_file(atfd, file));
Timo Teräs's avatar
Timo Teräs committed
529
530
531
532
}

struct apk_fd_ostream {
	struct apk_ostream os;
533
534
535
536
537
	int fd, rc;

	const char *file, *tmpfile;
	int atfd;

Timo Teräs's avatar
Timo Teräs committed
538
539
	size_t bytes;
	char buffer[1024];
Timo Teräs's avatar
Timo Teräs committed
540
541
};

542
static ssize_t safe_write(int fd, const void *ptr, size_t size)
Timo Teräs's avatar
Timo Teräs committed
543
{
544
	ssize_t i = 0, r;
Timo Teräs's avatar
Timo Teräs committed
545
546

	while (i < size) {
Timo Teräs's avatar
Timo Teräs committed
547
		r = write(fd, ptr + i, size - i);
Timo Teräs's avatar
Timo Teräs committed
548
		if (r < 0)
549
			return -errno;
Timo Teräs's avatar
Timo Teräs committed
550
551
552
553
554
555
556
557
		if (r == 0)
			return i;
		i += r;
	}

	return i;
}

558
static ssize_t fdo_flush(struct apk_fd_ostream *fos)
Timo Teräs's avatar
Timo Teräs committed
559
{
560
561
	ssize_t r;

Timo Teräs's avatar
Timo Teräs committed
562
563
564
	if (fos->bytes == 0)
		return 0;

565
566
	if ((r = safe_write(fos->fd, fos->buffer, fos->bytes)) != fos->bytes) {
		fos->rc = r < 0 ? r : -EIO;
567
		return r;
568
	}
Timo Teräs's avatar
Timo Teräs committed
569
570
571
572
573

	fos->bytes = 0;
	return 0;
}

574
static ssize_t fdo_write(void *stream, const void *ptr, size_t size)
Timo Teräs's avatar
Timo Teräs committed
575
576
577
{
	struct apk_fd_ostream *fos =
		container_of(stream, struct apk_fd_ostream, os);
578
	ssize_t r;
Timo Teräs's avatar
Timo Teräs committed
579
580

	if (size + fos->bytes >= sizeof(fos->buffer)) {
581
582
583
		r = fdo_flush(fos);
		if (r != 0)
			return r;
584
585
586
587
588
589
		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
590
591
592
593
594
595
596
597
	}

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

	return size;
}

598
static int fdo_close(void *stream)
Timo Teräs's avatar
Timo Teräs committed
599
600
601
{
	struct apk_fd_ostream *fos =
		container_of(stream, struct apk_fd_ostream, os);
602
	int rc = fos->rc;
Timo Teräs's avatar
Timo Teräs committed
603

Timo Teräs's avatar
Timo Teräs committed
604
	fdo_flush(fos);
605
606
607
608
609
610
611
612
613
614
615
616
	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
617
	free(fos);
618
619

	return rc;
Timo Teräs's avatar
Timo Teräs committed
620
621
622
623
624
625
626
627
628
629
}

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

	if (fd < 0)
		return NULL;

	fos = malloc(sizeof(struct apk_fd_ostream));
630
631
	if (fos == NULL) {
		close(fd);
Timo Teräs's avatar
Timo Teräs committed
632
		return NULL;
633
	}
Timo Teräs's avatar
Timo Teräs committed
634
635
636
637
638
639
640
641
642
643

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

	return &fos->os;
}

644
645
646
647
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
648
{
649
	struct apk_ostream *os;
Timo Teräs's avatar
Timo Teräs committed
650
651
	int fd;

652
	fd = openat(atfd, tmpfile ?: file, O_CREAT | O_RDWR | O_TRUNC | O_CLOEXEC, mode);
Timo Teräs's avatar
Timo Teräs committed
653
654
655
656
657
	if (fd < 0)
		return NULL;

	fcntl(fd, F_SETFD, FD_CLOEXEC);

658
659
660
661
662
663
664
665
666
667
668
669
	os = apk_ostream_to_fd(fd);
	if (os == NULL)
		return NULL;

	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
670
671
}

672
673
674
675
676
struct apk_counter_ostream {
	struct apk_ostream os;
	off_t *counter;
};

677
static ssize_t co_write(void *stream, const void *ptr, size_t size)
678
679
680
681
682
683
684
685
{
	struct apk_counter_ostream *cos =
		container_of(stream, struct apk_counter_ostream, os);

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

686
static int co_close(void *stream)
687
688
689
690
691
{
	struct apk_counter_ostream *cos =
		container_of(stream, struct apk_counter_ostream, os);

	free(cos);
692
	return 0;
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
}

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;
}

712
713
714
715
716
717
718
719
720
721
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;
}
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844

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;
}

static unsigned int id_genid = 0;
static struct apk_hash uid_cache, gid_cache;

void apk_id_cache_init(void)
{
	apk_hash_init(&uid_cache, &id_hash_ops, 256);
	apk_hash_init(&gid_cache, &id_hash_ops, 256);
	id_genid = 1;
}

void apk_id_cache_free(void)
{
	apk_hash_free(&uid_cache);
	apk_hash_free(&gid_cache);
}

void apk_id_cache_reset(void)
{
	id_genid++;
	if (id_genid == 0)
		id_genid = 1;
}

uid_t apk_resolve_uid(const char *username, uid_t default_uid)
{
	struct cache_item *ci;
	struct passwd pwent, *pwd;
	char buf[1024];
	int r;

	ci = resolve_cache_item(&uid_cache, APK_BLOB_STR(username));
	if (ci == NULL)
		return default_uid;

	if (ci->genid != id_genid) {
		r = getpwnam_r(username, &pwent, buf, sizeof(buf), &pwd);
		if (pwd != NULL)
			ci->uid = pwd->pw_uid;
		else
			ci->uid = -1;
		if (r == 0)
			ci->genid = id_genid;
	}

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

	return default_uid;
}

uid_t apk_resolve_gid(const char *groupname, uid_t default_gid)
{
	struct cache_item *ci;
	struct group grent, *grp;
	char buf[1024];
	int r;

	ci = resolve_cache_item(&gid_cache, APK_BLOB_STR(groupname));
	if (ci == NULL)
		return default_gid;

	if (ci->genid != id_genid) {
		r = getgrnam_r(groupname, &grent, buf, sizeof(buf), &grp);
		if (grp != NULL)
			ci->gid = grp->gr_gid;
		else
			ci->gid = -1;
		if (r == 0)
			ci->genid = id_genid;
	}

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

	return default_gid;
}