aboutsummaryrefslogtreecommitdiff
path: root/lib/sqfshelper/data_writer.c
blob: e2a8538c14814b4f4b4ee7ab2bcfff5378cd7a5a (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * data_writer.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"

#include "sqfs/data_writer.h"
#include "data_writer.h"
#include "highlevel.h"
#include "util.h"

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <zlib.h>

struct data_writer_t {
	sqfs_data_writer_t *proc;
	sqfs_compressor_t *cmp;
	sqfs_super_t *super;

	data_writer_stats_t stats;
};

static bool is_zero_block(unsigned char *ptr, size_t size)
{
	return ptr[0] == 0 && memcmp(ptr, ptr + 1, size - 1) == 0;
}

static int add_sentinel_block(data_writer_t *data, sqfs_inode_generic_t *inode,
			      uint32_t flags)
{
	sqfs_block_t *blk = calloc(1, sizeof(*blk));

	if (blk == NULL) {
		perror("creating sentinel block");
		return -1;
	}

	blk->inode = inode;
	blk->flags = flags;

	return sqfs_data_writer_enqueue(data->proc, blk);
}

int write_data_from_file_condensed(data_writer_t *data, sqfs_file_t *file,
				   sqfs_inode_generic_t *inode,
				   const sqfs_sparse_map_t *map, int flags)
{
	uint32_t blk_flags = SQFS_BLK_FIRST_BLOCK;
	uint64_t filesz, offset;
	size_t diff, i = 0;
	sqfs_block_t *blk;
	int ret;

	if (flags & DW_DONT_COMPRESS)
		blk_flags |= SQFS_BLK_DONT_COMPRESS;

	if (flags & DW_ALLIGN_DEVBLK)
		blk_flags |= SQFS_BLK_ALLIGN;

	sqfs_inode_get_file_size(inode, &filesz);

	for (offset = 0; offset < filesz; offset += diff) {
		if (filesz - offset > (uint64_t)data->super->block_size) {
			diff = data->super->block_size;
		} else {
			diff = filesz - offset;
		}

		if (map == NULL) {
			ret = sqfs_file_create_block(file, offset, diff, inode,
						     blk_flags, &blk);
		} else {
			ret = sqfs_file_create_block_dense(file, offset, diff,
							   inode, blk_flags,
							   map, &blk);
		}

		if (ret)
			return -1;

		blk->index = i++;

		if (is_zero_block(blk->data, blk->size)) {
			data->stats.sparse_blocks += 1;

			sqfs_inode_make_extended(inode);
			inode->data.file_ext.sparse += blk->size;
			inode->num_file_blocks += 1;

			inode->block_sizes[blk->index] = 0;
			free(blk);
			continue;
		}

		if (diff < data->super->block_size &&
		    !(flags & DW_DONT_FRAGMENT)) {
			if (!(blk_flags & (SQFS_BLK_FIRST_BLOCK |
					   SQFS_BLK_LAST_BLOCK))) {
				blk_flags |= SQFS_BLK_LAST_BLOCK;

				if (add_sentinel_block(data, inode,
						       blk_flags)) {
					free(blk);
					return -1;
				}
			}

			blk->flags |= SQFS_BLK_IS_FRAGMENT;

			if (sqfs_data_writer_enqueue(data->proc, blk))
				return -1;
		} else {
			if (sqfs_data_writer_enqueue(data->proc, blk))
				return -1;

			blk_flags &= ~SQFS_BLK_FIRST_BLOCK;
			inode->num_file_blocks += 1;
		}
	}

	if (!(blk_flags & (SQFS_BLK_FIRST_BLOCK | SQFS_BLK_LAST_BLOCK))) {
		blk_flags |= SQFS_BLK_LAST_BLOCK;

		if (add_sentinel_block(data, inode, blk_flags))
			return -1;
	}

	sqfs_inode_make_basic(inode);

	data->stats.bytes_read += filesz;
	data->stats.file_count += 1;
	return 0;
}

int write_data_from_file(data_writer_t *data, sqfs_inode_generic_t *inode,
			 sqfs_file_t *file, int flags)
{
	return write_data_from_file_condensed(data, file, inode, NULL, flags);
}

data_writer_t *data_writer_create(sqfs_super_t *super, sqfs_compressor_t *cmp,
				  sqfs_file_t *file, size_t devblksize,
				  unsigned int num_jobs, size_t max_backlog)
{
	data_writer_t *data = calloc(1, sizeof(*data));

	if (data == NULL) {
		perror("creating data writer");
		return NULL;
	}

	data->proc = sqfs_data_writer_create(super->block_size, cmp,
					     num_jobs, max_backlog,
					     devblksize, file);
	if (data->proc == NULL) {
		perror("creating data block processor");
		free(data);
		return NULL;
	}

	data->cmp = cmp;
	data->super = super;
	return data;
}

void data_writer_destroy(data_writer_t *data)
{
	sqfs_data_writer_destroy(data->proc);
	free(data);
}

int data_writer_write_fragment_table(data_writer_t *data)
{
	if (sqfs_data_writer_write_fragment_table(data->proc, data->super)) {
		fputs("error storing fragment table\n", stderr);
		return -1;
	}
	return 0;
}

int data_writer_sync(data_writer_t *data)
{
	return sqfs_data_writer_finish(data->proc);
}

data_writer_stats_t *data_writer_get_stats(data_writer_t *data)
{
	return &data->stats;
}