aboutsummaryrefslogtreecommitdiff
path: root/lib/util/padd_file.c
blob: 21950b470e5d96741f801ff7230ace8b56951f05 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * padd_file.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"

#include "sqfs/io.h"
#include "util.h"

#include <stdlib.h>
#include <stdio.h>

int padd_file(int outfd, uint64_t size, size_t blocksize)
{
	size_t padd_sz = size % blocksize;
	int status = -1;
	uint8_t *buffer;

	if (padd_sz == 0)
		return 0;

	padd_sz = blocksize - padd_sz;

	buffer = calloc(1, padd_sz);
	if (buffer == NULL)
		goto fail_errno;

	if (write_data("padding output file to block size",
		       outfd, buffer, padd_sz)) {
		goto out;
	}

	status = 0;
out:
	free(buffer);
	return status;
fail_errno:
	perror("padding output file to block size");
	goto out;
}

int padd_sqfs(sqfs_file_t *file, uint64_t size, size_t blocksize)
{
	size_t padd_sz = size % blocksize;
	int status = -1;
	uint8_t *buffer;

	if (padd_sz == 0)
		return 0;

	padd_sz = blocksize - padd_sz;

	buffer = calloc(1, padd_sz);
	if (buffer == NULL)
		goto fail_errno;

	if (file->write_at(file, file->get_size(file),
			   buffer, padd_sz)) {
		goto fail_errno;
	}

	status = 0;
out:
	free(buffer);
	return status;
fail_errno:
	perror("padding output file to block size");
	goto out;
}