aboutsummaryrefslogtreecommitdiff
path: root/mkfs/mkfs.c
blob: 019a23377ec3f250d7d207d3b4c106e3f4535250 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include "mkfs.h"
#include "util.h"

static int padd_file(sqfs_info_t *info)
{
	size_t padd_sz = info->super.bytes_used % info->opt.devblksz;
	uint8_t *buffer;
	ssize_t ret;

	if (padd_sz == 0)
		return 0;

	padd_sz = info->opt.devblksz - padd_sz;

	buffer = calloc(1, padd_sz);
	if (buffer == NULL) {
		perror("padding output file to block size");
		return -1;
	}

	ret = write_retry(info->outfd, buffer, padd_sz);

	if (ret < 0) {
		perror("Error padding squashfs image to page size");
		free(buffer);
		return -1;
	}

	if ((size_t)ret < padd_sz) {
		fputs("Truncated write trying to padd squashfs image\n",
		      stderr);
		return -1;
	}

	free(buffer);
	return 0;
}

int main(int argc, char **argv)
{
	int status = EXIT_FAILURE, ret;
	sqfs_info_t info;
	uint64_t start;

	memset(&info, 0, sizeof(info));

	process_command_line(&info.opt, argc, argv);

	if (sqfs_super_init(&info.super, info.opt.blksz, info.opt.def_mtime,
			    info.opt.compressor)) {
		return EXIT_FAILURE;
	}

	if (id_table_init(&info.idtbl))
		return EXIT_FAILURE;

	info.outfd = open(info.opt.outfile, info.opt.outmode, 0644);
	if (info.outfd < 0) {
		perror(info.opt.outfile);
		goto out_idtbl;
	}

	if (sqfs_super_write(&info.super, info.outfd))
		goto out_outfd;

	if (fstree_init(&info.fs, info.opt.blksz, info.opt.def_mtime,
			info.opt.def_mode, info.opt.def_uid,
			info.opt.def_gid)) {
		goto out_outfd;
	}

	if (info.opt.infile != NULL) {
		if (fstree_from_file(&info.fs, info.opt.infile, info.opt.packdir))
			goto out_fstree;
	} else {
		if (fstree_from_dir(&info.fs, info.opt.packdir))
			goto out_fstree;
	}

#ifdef WITH_SELINUX
	if (info.opt.selinux != NULL) {
		if (fstree_relabel_selinux(&info.fs, info.opt.selinux))
			goto out_fstree;
	}
#endif

	fstree_xattr_deduplicate(&info.fs);

	fstree_sort(&info.fs);

	if (fstree_gen_inode_table(&info.fs))
		goto out_fstree;

	info.super.inode_count = info.fs.inode_tbl_size - 2;

	info.cmp = compressor_create(info.super.compression_id, true,
				     info.super.block_size,
				     info.opt.comp_extra);
	if (info.cmp == NULL) {
		fputs("Error creating compressor\n", stderr);
		goto out_outfd;
	}

	ret = info.cmp->write_options(info.cmp, info.outfd);
	if (ret < 0)
		goto out_cmp;

	if (ret > 0) {
		info.super.flags |= SQFS_FLAG_COMPRESSOR_OPTIONS;
		info.super.bytes_used += ret;
	}

	if (write_data_to_image(&info))
		goto out_cmp;

	if (sqfs_serialize_fstree(info.outfd, &info.super, &info.fs,
				  info.cmp, &info.idtbl)) {
		goto out_cmp;
	}

	info.super.fragment_entry_count = info.num_fragments;

	if (sqfs_write_table(info.outfd, &info.super, info.fragments,
			     sizeof(info.fragments[0]), info.num_fragments,
			     &start, info.cmp)) {
		goto out_cmp;
	}

	info.super.fragment_table_start = start;

	if (id_table_write(&info.idtbl, info.outfd, &info.super, info.cmp))
		goto out_cmp;

	if (write_xattr(&info))
		goto out_cmp;

	if (sqfs_super_write(&info.super, info.outfd))
		goto out_cmp;

	if (padd_file(&info))
		goto out_cmp;

	status = EXIT_SUCCESS;
out_cmp:
	free(info.fragments);
	info.cmp->destroy(info.cmp);
out_fstree:
	fstree_cleanup(&info.fs);
out_outfd:
	close(info.outfd);
out_idtbl:
	id_table_cleanup(&info.idtbl);
	return status;
}