aboutsummaryrefslogtreecommitdiff
path: root/extras/mknastyfs.c
blob: 216c7ebf6df9c5125fc8f36b4c93747590868acb (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
#include "sqfs/meta_writer.h"
#include "sqfs/dir_writer.h"
#include "sqfs/compressor.h"
#include "sqfs/id_table.h"
#include "sqfs/inode.h"
#include "sqfs/super.h"
#include "sqfs/io.h"

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

static void add_padding(sqfs_file_t *file)
{
	sqfs_u64 diff, size;

	size = file->get_size(file);
	diff = size % 4096;

	if (diff > 0)
		file->truncate(file, size + (4096 - diff));
}

static sqfs_inode_generic_t *create_file_inode(sqfs_id_table_t *idtbl,
					       unsigned int inode_num)
{
	sqfs_inode_generic_t *inode;

	inode = calloc(1, sizeof(*inode) + sizeof(sqfs_u32));
	inode->base.type = SQFS_INODE_FILE;
	inode->base.mode = SQFS_INODE_MODE_REG | 0644;
	inode->base.inode_number = inode_num;

	sqfs_id_table_id_to_index(idtbl, 0, &inode->base.uid_idx);
	sqfs_id_table_id_to_index(idtbl, 0, &inode->base.gid_idx);

	inode->data.file.file_size = 1337;
	inode->data.file.fragment_index = 0xFFFFFFFF;

	inode->num_file_blocks = 1;
	inode->extra[0] = (1 << 24) | inode->data.file.file_size;
	return inode;
}

int main(void)
{
	sqfs_meta_writer_t *inode_m, *dir_m;
	sqfs_compressor_config_t cfg;
	sqfs_inode_generic_t *inode;
	int status = EXIT_FAILURE;
	sqfs_dir_writer_t *dirwr;
	sqfs_compressor_t *cmp;
	sqfs_id_table_t *idtbl;
	unsigned int inode_num;
	sqfs_u64 block_start;
	sqfs_super_t super;
	sqfs_file_t *file;
	sqfs_u32 offset;

	/* get a file object referring to our destination file */
	file = sqfs_open_file("nasty.sqfs", 0);
	if (file == NULL) {
		fputs("Error opening output file.\n", stderr);
		return EXIT_FAILURE;
	}

	/* initialize the super block with sane values */
	sqfs_super_init(&super, 4096, 0, SQFS_COMP_GZIP);

	if (sqfs_super_write(&super, file)) {
		fputs("Error witing super block.\n", stderr);
		goto out_file;
	}

	/* create a compressor */
	sqfs_compressor_config_init(&cfg, super.compression_id,
				    super.block_size, 0);

	cmp = sqfs_compressor_create(&cfg);
	if (cmp == NULL) {
		fputs("Error creating compressor.\n", stderr);
		goto out_file;
	}

	/* create meta data writers for inodes and directories */
	inode_m = sqfs_meta_writer_create(file, cmp, 0);
	if (inode_m == NULL) {
		fputs("Error creating inode meta data writer.\n", stderr);
		goto out_cmp;
	}

	dir_m = sqfs_meta_writer_create(file, cmp,
					SQFS_META_WRITER_KEEP_IN_MEMORY);
	if (dir_m == NULL) {
		fputs("Error creating directory meta data writer.\n", stderr);
		goto out_im;
	}

	/* create a higher level directory writer on top of the meta writer */
	dirwr = sqfs_dir_writer_create(dir_m, 0);
	if (dirwr == NULL) {
		fputs("Error creating directory writer.\n", stderr);
		goto out_dm;
	}

	/* create an ID table */
	idtbl = sqfs_id_table_create();
	if (idtbl == NULL) {
		fputs("Error creating ID table.\n", stderr);
		goto out_dirwr;
	}

	/* generate inodes and directories */
	super.inode_table_start = file->get_size(file);
	inode_num = 1;

	sqfs_dir_writer_begin(dirwr, 0);

	inode = create_file_inode(idtbl, inode_num++);
	sqfs_meta_writer_get_position(inode_m, &block_start, &offset);
	sqfs_meta_writer_write_inode(inode_m, inode);
	sqfs_dir_writer_add_entry(dirwr, "..", inode->base.inode_number,
				  (block_start << 16) | offset,
				  inode->base.mode);
	free(inode);

	inode = create_file_inode(idtbl, inode_num++);
	sqfs_meta_writer_get_position(inode_m, &block_start, &offset);
	sqfs_meta_writer_write_inode(inode_m, inode);
	sqfs_dir_writer_add_entry(dirwr, "/etc/passwd",
				  inode->base.inode_number,
				  (block_start << 16) | offset,
				  inode->base.mode);
	free(inode);

	sqfs_dir_writer_end(dirwr);

	inode = sqfs_dir_writer_create_inode(dirwr, 0, 0xFFFFFFFF, 0);

	inode->base.mode = SQFS_INODE_MODE_DIR | 0755;
	inode->base.inode_number = inode_num++;
	sqfs_id_table_id_to_index(idtbl, 0, &inode->base.uid_idx);
	sqfs_id_table_id_to_index(idtbl, 0, &inode->base.gid_idx);

	sqfs_meta_writer_get_position(inode_m, &block_start, &offset);
	super.root_inode_ref = (block_start << 16) | offset;
	sqfs_meta_writer_write_inode(inode_m, inode);
	free(inode);

	/* flush the meta data to the file */
	sqfs_meta_writer_flush(inode_m);
	sqfs_meta_writer_flush(dir_m);

	super.directory_table_start = file->get_size(file);
	sqfs_meta_write_write_to_file(dir_m);

	sqfs_id_table_write(idtbl, file, &super, cmp);

	super.inode_count = inode_num - 2;
	super.bytes_used = file->get_size(file);

	if (sqfs_super_write(&super, file)) {
		fputs("Error update the final super block.\n", stderr);
		goto out_file;
	}

	add_padding(file);

	/* cleanup */
	status = EXIT_SUCCESS;
	sqfs_id_table_destroy(idtbl);
out_dirwr:
	sqfs_dir_writer_destroy(dirwr);
out_dm:
	sqfs_meta_writer_destroy(dir_m);
out_im:
	sqfs_meta_writer_destroy(inode_m);
out_cmp:
	cmp->destroy(cmp);
out_file:
	file->destroy(file);
	return status;
}