summaryrefslogtreecommitdiff
path: root/unpack/fill_files.c
blob: 88efd014379e728803cccb7dc530f4365f0bcf20 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include "config.h"
#include "rdsquashfs.h"

static int fill_files(data_reader_t *data, file_info_t *list, int flags)
{
	file_info_t *fi;
	int fd;

	for (fi = list; fi != NULL; fi = fi->next) {
		if (fi->input_file == NULL)
			continue;

		fd = open(fi->input_file, O_WRONLY);
		if (fd < 0) {
			fprintf(stderr, "unpacking %s: %s\n",
				fi->input_file, strerror(errno));
			return -1;
		}

		if (!(flags & UNPACK_QUIET))
			printf("unpacking %s\n", fi->input_file);

		if (data_reader_dump_file(data, fi, fd,
					  (flags & UNPACK_NO_SPARSE) == 0)) {
			close(fd);
			return -1;
		}

		close(fd);
	}

	return 0;
}

static file_info_t *split_list(file_info_t *list, uint64_t threashold)
{
	file_info_t *it, *new = NULL;
	uint64_t size = 0;

	for (it = list; it != NULL; it = it->next) {
		if (it->input_file == NULL)
			continue;

		size += it->size - it->sparse;

		if (size >= threashold) {
			new = it->next;
			it->next = NULL;
			break;
		}
	}

	return new;
}

static uint64_t total_size(file_info_t *list)
{
	uint64_t size = 0;
	file_info_t *it;

	for (it = list; it != NULL; it = it->next) {
		if (it->input_file != NULL)
			size += it->size - it->sparse;
	}

	return size;
}

int fill_unpacked_files(fstree_t *fs, data_reader_t *data, int flags,
			unsigned int num_jobs)
{
	file_info_t *sublists[num_jobs], *it;
	int exitstatus, status = 0;
	uint64_t threshold;
	unsigned int i;
	pid_t pid;

	if (num_jobs <= 1) {
		status = fill_files(data, fs->files, flags);

		for (it = fs->files; it != NULL; it = it->next)
			free(it->input_file);

		return status;
	}

	threshold = total_size(fs->files) / num_jobs;

	for (i = 0; i < num_jobs; ++i) {
		sublists[i] = fs->files;

		fs->files = split_list(fs->files, threshold);
	}

	for (i = 0; i < num_jobs; ++i) {
		pid = fork();

		if (pid == 0) {
			/* Kill the child when the parent process dies */
			prctl(PR_SET_PDEATHSIG, SIGKILL);

			if (fill_files(data, sublists[i], flags))
				exit(EXIT_FAILURE);
			exit(EXIT_SUCCESS);
		}

		if (pid < 0) {
			perror("fork");
			status = -1;
			num_jobs = i;
			break;
		}
	}

	for (i = 0; i < num_jobs; ++i) {
		do {
			pid = waitpid(-1, &exitstatus, 0);
			if (errno == ECHILD)
				goto out;
		} while (pid < 0);

		if (!WIFEXITED(exitstatus) ||
		    WEXITSTATUS(exitstatus) != EXIT_SUCCESS) {
			status = -1;
		}

		for (it = sublists[i]; it != NULL; it = it->next)
			free(it->input_file);
	}
out:
	return status;
}