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

static sqfs_u8 buffer[4096];

int write_data_from_file(const char *filename, sqfs_block_processor_t *data,
			 sqfs_inode_generic_t **inode, sqfs_file_t *file,
			 int flags)
{
	sqfs_u64 filesz, offset;
	size_t diff;
	int ret;

	ret = sqfs_block_processor_begin_file(data, inode, NULL, flags);
	if (ret) {
		sqfs_perror(filename, "beginning file data blocks", ret);
		return -1;
	}

	filesz = file->get_size(file);

	for (offset = 0; offset < filesz; offset += diff) {
		if (filesz - offset > sizeof(buffer)) {
			diff = sizeof(buffer);
		} else {
			diff = filesz - offset;
		}

		ret = file->read_at(file, offset, buffer, diff);
		if (ret) {
			sqfs_perror(filename, "reading file range", ret);
			return -1;
		}

		ret = sqfs_block_processor_append(data, buffer, diff);
		if (ret) {
			sqfs_perror(filename, "packing file data", ret);
			return -1;
		}
	}

	ret = sqfs_block_processor_end_file(data);
	if (ret) {
		sqfs_perror(filename, "finishing file data", ret);
		return -1;
	}

	return 0;
}