summaryrefslogtreecommitdiff
path: root/lib/tar/skip.c
blob: 69208b24d11f03cc183c63fbf69beb75d9a2cd1d (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include "util.h"
#include "tar.h"

#include <stdio.h>

static int skip_bytes(int fd, uint64_t size)
{
	unsigned char buffer[1024];
	ssize_t ret;
	size_t diff;

	while (size != 0) {
		diff = sizeof(buffer);
		if (diff > size)
			diff = size;

		ret = read_retry(fd, buffer, diff);

		if (ret < 0) {
			perror("reading tar record data");
			return -1;
		}

		if ((size_t)ret < diff) {
			fputs("unexpected end of file\n", stderr);
			return -1;
		}

		size -= diff;
	}

	return 0;
}

int skip_padding(int fd, uint64_t size)
{
	size_t tail = size % 512;

	return tail ? skip_bytes(fd, 512 - tail) : 0;
}

int skip_entry(int fd, uint64_t size)
{
	size_t tail = size % 512;

	return skip_bytes(fd, tail ? (size + 512 - tail) : size);
}