aboutsummaryrefslogtreecommitdiff
path: root/lib/tar/read_retry.c
blob: 2a2f82cb2ad5cca6297fd5143f1ae6e0b9470bd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
47
48
49
50
51
52
53
54
55
56
57
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * read_retry.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"

#include "tar.h"
#include "internal.h"

int read_retry(const char *errstr, FILE *fp, void *buffer, size_t size)
{
	size_t ret;

	while (size > 0) {
		if (ferror(fp)) {
			fprintf(stderr, "%s: error reading from file\n",
				errstr);
			return -1;
		}

		if (feof(fp)) {
			fprintf(stderr, "%s: short read\n", errstr);
			return -1;
		}

		ret = fread(buffer, 1, size, fp);
		size -= ret;
		buffer = (char *)buffer + ret;
	}

	return 0;
}

char *record_to_memory(FILE *fp, sqfs_u64 size)
{
	char *buffer = malloc(size + 1);

	if (buffer == NULL)
		goto fail_errno;

	if (read_retry("reading tar record", fp, buffer, size))
		goto fail;

	if (skip_padding(fp, size))
		goto fail;

	buffer[size] = '\0';
	return buffer;
fail_errno:
	perror("reading tar record");
	goto fail;
fail:
	free(buffer);
	return NULL;
}