summaryrefslogtreecommitdiff
path: root/lib/tar/skip.c
blob: 28c02be7e802d400b2a1d8a057db9e3d3561d488 (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
/* 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];
	size_t diff;

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

		if (read_data("reading tar record padding", fd, buffer, diff))
			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);
}