aboutsummaryrefslogtreecommitdiff
path: root/lib/tar/src/record_to_memory.c
blob: 43fd44cc61ac33ab32ec3efb82a2f987a4cdef34 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * record_to_memory.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"

#include "tar/tar.h"
#include "internal.h"
#include <stdlib.h>

char *record_to_memory(istream_t *fp, size_t size)
{
	char *buffer = malloc(size + 1);
	int ret;

	if (buffer == NULL)
		goto fail_errno;

	ret = istream_read(fp, buffer, size);
	if (ret < 0)
		goto fail;

	if ((size_t)ret < size) {
		fputs("Reading tar record: unexpected end-of-file.\n", stderr);
		goto fail;
	}

	if (size % 512) {
		if (istream_skip(fp, 512 - (size % 512)))
			goto fail;
	}

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