1#ifndef ACTION_H
2#define ACTION_H
3/*
4 * Create a squashfs filesystem.  This is a highly compressed read only
5 * filesystem.
6 *
7 * Copyright (c) 2011, 2012, 2013, 2014
8 * Phillip Lougher <phillip@squashfs.org.uk>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2,
13 * or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 *
24 * action.h
25 */
26
27/*
28 * Lexical analyser definitions
29 */
30#define TOK_OPEN_BRACKET	0
31#define TOK_CLOSE_BRACKET	1
32#define TOK_AND			2
33#define TOK_OR			3
34#define TOK_NOT			4
35#define TOK_COMMA		5
36#define TOK_AT			6
37#define TOK_WHITE_SPACE		7
38#define TOK_STRING		8
39#define TOK_EOF			9
40
41#define TOK_TO_STR(OP, S) ({ \
42	char *s; \
43	switch(OP) { \
44	case TOK_EOF: \
45		s = "EOF"; \
46		break; \
47	case TOK_STRING: \
48		s = S; \
49		break; \
50	default: \
51		s = token_table[OP].string; \
52		break; \
53	} \
54	s; \
55})
56
57
58struct token_entry {
59	char *string;
60	int token;
61	int size;
62};
63
64/*
65 * Expression parser definitions
66 */
67#define OP_TYPE			0
68#define ATOM_TYPE		1
69#define UNARY_TYPE		2
70
71#define SYNTAX_ERROR(S, ARGS...) { \
72	char *src = strdup(source); \
73	src[cur_ptr - source] = '\0'; \
74	fprintf(stderr, "Failed to parse action \"%s\"\n", source); \
75	fprintf(stderr, "Syntax error: "S, ##ARGS); \
76	fprintf(stderr, "Got here \"%s\"\n", src); \
77	free(src); \
78}
79
80#define TEST_SYNTAX_ERROR(TEST, ARG, S, ARGS...) { \
81	char *src = strdup(source); \
82	src[cur_ptr - source] = '\0'; \
83	fprintf(stderr, "Failed to parse action \"%s\"\n", source); \
84	fprintf(stderr, "Syntax error in \"%s()\", arg %d: "S, TEST->name, \
85			 ARG, ##ARGS); \
86	fprintf(stderr, "Got here \"%s\"\n", src); \
87	free(src); \
88}
89
90struct expr;
91
92struct expr_op {
93	struct expr *lhs;
94	struct expr *rhs;
95	int op;
96};
97
98
99struct atom {
100	struct test_entry *test;
101	int args;
102	char **argv;
103	void *data;
104};
105
106
107struct unary_op {
108	struct expr *expr;
109	int op;
110};
111
112
113struct expr {
114	int type;
115	union {
116		struct atom atom;
117		struct expr_op expr_op;
118		struct unary_op unary_op;
119	};
120};
121
122/*
123 * Test operation definitions
124 */
125#define NUM_EQ		1
126#define NUM_LESS	2
127#define NUM_GREATER	3
128
129struct test_number_arg {
130	long long size;
131	int range;
132};
133
134struct test_range_args {
135	long long start;
136	long long end;
137};
138
139struct action;
140struct action_data;
141
142struct test_entry {
143	char *name;
144	int args;
145	int (*fn)(struct atom *, struct action_data *);
146	int (*parse_args)(struct test_entry *, struct atom *);
147	int exclude_ok;
148	int handle_logging;
149};
150
151
152/*
153 * Type test specific definitions
154 */
155struct type_entry {
156	int value;
157	char type;
158};
159
160
161/*
162 * Action definitions
163 */
164#define FRAGMENT_ACTION 0
165#define EXCLUDE_ACTION 1
166#define FRAGMENTS_ACTION 2
167#define NO_FRAGMENTS_ACTION 3
168#define ALWAYS_FRAGS_ACTION 4
169#define NO_ALWAYS_FRAGS_ACTION 5
170#define COMPRESSED_ACTION 6
171#define UNCOMPRESSED_ACTION 7
172#define UID_ACTION 8
173#define GID_ACTION 9
174#define GUID_ACTION 10
175#define MODE_ACTION 11
176#define EMPTY_ACTION 12
177#define MOVE_ACTION 13
178#define PRUNE_ACTION 14
179#define NOOP_ACTION 15
180
181/*
182 * Define what file types each action operates over
183 */
184#define ACTION_DIR 1
185#define ACTION_REG 2
186#define ACTION_ALL_LNK 3
187#define ACTION_ALL 4
188#define ACTION_LNK 5
189
190
191/*
192 * Action logging requested, specified by the various
193 * -action, -true-action, -false-action and -verbose-action
194 * options
195 */
196#define ACTION_LOG_NONE	0
197#define ACTION_LOG_TRUE 1
198#define ACTION_LOG_FALSE 2
199#define ACTION_LOG_VERBOSE ACTION_LOG_TRUE | ACTION_LOG_FALSE
200
201struct action_entry {
202	char *name;
203	int type;
204	int args;
205	int file_types;
206	int (*parse_args)(struct action_entry *, int, char **, void **);
207	void (*run_action)(struct action *, struct dir_ent *);
208};
209
210
211struct action_data {
212	int depth;
213	char *name;
214	char *pathname;
215	char *subpath;
216	struct stat *buf;
217	struct dir_ent *dir_ent;
218	struct dir_info *root;
219};
220
221
222struct action {
223	int type;
224	struct action_entry *action;
225	int args;
226	char **argv;
227	struct expr *expr;
228	void *data;
229	int verbose;
230};
231
232
233/*
234 * Uid/gid action specific definitions
235 */
236struct uid_info {
237	uid_t uid;
238};
239
240struct gid_info {
241	gid_t gid;
242};
243
244struct guid_info {
245	uid_t uid;
246	gid_t gid;
247};
248
249
250/*
251 * Mode action specific definitions
252 */
253#define ACTION_MODE_SET 0
254#define ACTION_MODE_ADD 1
255#define ACTION_MODE_REM 2
256#define ACTION_MODE_OCT 3
257
258struct mode_data {
259	struct mode_data *next;
260	int operation;
261	int mode;
262	unsigned int mask;
263	char X;
264};
265
266
267/*
268 * Empty action specific definitions
269 */
270#define EMPTY_ALL 0
271#define EMPTY_SOURCE 1
272#define EMPTY_EXCLUDED 2
273
274struct empty_data {
275	int val;
276};
277
278
279/*
280 * Move action specific definitions
281 */
282#define ACTION_MOVE_RENAME 1
283#define ACTION_MOVE_MOVE 2
284
285struct move_ent {
286	int ops;
287	struct dir_ent *dir_ent;
288	char *name;
289	struct dir_info *dest;
290	struct move_ent *next;
291};
292
293
294/*
295 * Perm test function specific definitions
296 */
297#define PERM_ALL 1
298#define PERM_ANY 2
299#define PERM_EXACT 3
300
301struct perm_data {
302	int op;
303	int mode;
304};
305
306
307/*
308 * External function definitions
309 */
310extern int parse_action(char *, int verbose);
311extern void dump_actions();
312extern void *eval_frag_actions(struct dir_info *, struct dir_ent *);
313extern void *get_frag_action(void *);
314extern int eval_exclude_actions(char *, char *, char *, struct stat *, int,
315							struct dir_ent *);
316extern void eval_actions(struct dir_info *, struct dir_ent *);
317extern int eval_empty_actions(struct dir_info *, struct dir_ent *dir_ent);
318extern void eval_move_actions(struct dir_info *, struct dir_ent *);
319extern int eval_prune_actions(struct dir_info *, struct dir_ent *);
320extern void do_move_actions();
321extern int read_bytes(int, void *, int);
322extern int actions();
323extern int move_actions();
324extern int empty_actions();
325extern int read_action_file(char *, int);
326extern int exclude_actions();
327extern int prune_actions();
328#endif
329