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

#include "util/util.h"
#include "tar.h"

#include <stdio.h>

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

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

		if (read_retry("reading tar record padding", fd, buffer, diff))
			return -1;

		size -= diff;
	}

	return 0;
}

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

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

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

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