aboutsummaryrefslogtreecommitdiff
path: root/bin/gensquashfs/src/mkfs.h
blob: 53fb0181e6b163e451f3b4ca49771eed60a2e941 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * mkfs.h
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 * Copyright (C) 2022 Enno Boland <mail@eboland.de>
 */
#ifndef MKFS_H
#define MKFS_H

#include "config.h"

#include "common.h"
#include "fstree.h"
#include "util/util.h"
#include "io/file.h"

#ifdef HAVE_SYS_XATTR_H
#include <sys/xattr.h>

#if defined(__APPLE__) && defined(__MACH__)
#define llistxattr(path, list, size) \
	listxattr(path, list, size, XATTR_NOFOLLOW)

#define lgetxattr(path, name, value, size) \
	getxattr(path, name, value, size, 0, XATTR_NOFOLLOW)
#endif
#endif

#ifdef WITH_SELINUX
#include <selinux/selinux.h>
#include <selinux/label.h>
#endif

#include <getopt.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>

typedef struct {
	sqfs_writer_cfg_t cfg;
	unsigned int dirscan_flags;
	const char *infile;
	const char *selinux;
	const char *xattr_file;
	const char *sortfile;
	bool no_tail_packing;

	/* copied from command line or constructed from infile argument
	   if not specified. Must be free'd. */
	char *packdir;

	unsigned int force_uid_value;
	unsigned int force_gid_value;
	bool force_uid;
	bool force_gid;

	bool scan_xattr;
} options_t;

struct XattrMapEntry {
	char *key;
	sqfs_u8 *value;
	size_t value_len;
	struct XattrMapEntry *next;
};

struct XattrMapPattern {
	char *path;
	struct XattrMapEntry *entries;
	struct XattrMapPattern *next;
};

struct XattrMap {
	struct XattrMapPattern *patterns;
};

void process_command_line(options_t *opt, int argc, char **argv);

int xattrs_from_dir(fstree_t *fs, const char *path, void *selinux_handle,
		    void *xattr_map, sqfs_xattr_writer_t *xwr, bool scan_xattr);

void *xattr_open_map_file(const char *path);

int
xattr_apply_map_file(char *path, void *map, sqfs_xattr_writer_t *xwr);

void xattr_close_map_file(void *xattr_map);

void *selinux_open_context_file(const char *filename);

int selinux_relable_node(void *sehnd, sqfs_xattr_writer_t *xwr,
			 tree_node_t *node, const char *path);

void selinux_close_context_file(void *sehnd);

/*
  Parses the file format accepted by gensquashfs and produce a file system
  tree from it. File input paths are interpreted as relative to the current
  working directory.

  On failure, an error report with filename and line number is written
  to stderr.

  Returns 0 on success.
 */
int fstree_from_file(fstree_t *fs, const char *filename,
		     const char *basepath);

int fstree_from_file_stream(fstree_t *fs, istream_t *file,
			    const char *basepath);

/*
  Recursively scan a directory to build a file system tree.

  Returns 0 on success, prints to stderr on failure.
 */
int fstree_from_dir(fstree_t *fs, tree_node_t *root,
		    const char *path, scan_node_callback cb, void *user,
		    unsigned int flags);

/*
  Same as fstree_from_dir, but scans a sub-directory inside the specified path.

  Returns 0 on success, prints to stderr on failure.
 */
int fstree_from_subdir(fstree_t *fs, tree_node_t *root,
		       const char *path, const char *subdir,
		       scan_node_callback cb, void *user, unsigned int flags);

int fstree_sort_files(fstree_t *fs, istream_t *sortfile);

#endif /* MKFS_H */