summaryrefslogtreecommitdiffstats
path: root/squashfs-tools/pseudo_xattr.c
blob: 454e8e807c6ffc7b8801df5198e49ca38a56400b (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
/*
 * Create a squashfs filesystem.  This is a highly compressed read only
 * filesystem.
 *
 * Copyright (c) 2022
 * Phillip Lougher <phillip@squashfs.org.uk>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2,
 * or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * pseudo_xattr.c
 */

#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <regex.h>
#include <dirent.h>

#include "pseudo.h"
#include "mksquashfs_error.h"
#include "squashfs_fs.h"
#include "xattr.h"

#define TRUE 1
#define FALSE 0

static void add_xattr(struct pseudo_xattr **xattr, struct xattr_add *entry)
{
	if(*xattr == NULL) {
		*xattr = malloc(sizeof(struct pseudo_xattr));
		if(*xattr == NULL)
			MEM_ERROR();

		(*xattr)->xattr = entry;
		entry->next = NULL;
		(*xattr)->count = 1;
	} else {
		entry->next = (*xattr)->xattr;
		(*xattr)->xattr = entry;
		(*xattr)->count ++;
	}
}


/*
 * Add pseudo xattr to the set of pseudo definitions.
 */
static struct pseudo *add_pseudo_xattr(struct pseudo *pseudo, struct xattr_add *xattr,
	char *target, char *alltarget)
{
	char *targname;
	int i;

	target = get_element(target, &targname);

	if(pseudo == NULL) {
		pseudo = malloc(sizeof(struct pseudo));
		if(pseudo == NULL)
			MEM_ERROR();

		pseudo->names = 0;
		pseudo->count = 0;
		pseudo->name = NULL;
	}

	for(i = 0; i < pseudo->names; i++)
		if(strcmp(pseudo->name[i].name, targname) == 0)
			break;

	if(i == pseudo->names) {
		/* allocate new name entry */
		pseudo->names ++;
		pseudo->name = realloc(pseudo->name, (i + 1) *
			sizeof(struct pseudo_entry));
		if(pseudo->name == NULL)
			MEM_ERROR();
		pseudo->name[i].name = targname;
		pseudo->name[i].pathname = NULL;
		pseudo->name[i].dev = NULL;
		pseudo->name[i].xattr = NULL;

		if(target[0] == '\0') {
			/* at leaf pathname component */
			pseudo->name[i].pathname = strdup(alltarget);
			pseudo->name[i].pseudo = NULL;
			add_xattr(&pseudo->name[i].xattr, xattr);
		} else {
			/* recurse adding child components */
			pseudo->name[i].pseudo = add_pseudo_xattr(NULL, xattr,
				target, alltarget);
		}
	} else {
		/* existing matching entry */

		free(targname);

		if(target[0] == '\0') {
			/* Add xattr to this entry */
			pseudo->name[i].pathname = strdup(alltarget);
			add_xattr(&pseudo->name[i].xattr, xattr);
		} else {
			/* recurse adding child components */
			pseudo->name[i].pseudo = add_pseudo_xattr(pseudo->name[i].pseudo, xattr, target, alltarget);
		}
	}

	return pseudo;
}


static struct pseudo *add_pseudo_xattr_definition(struct pseudo *pseudo,
	struct xattr_add *xattr, char *target, char *alltarget)
{
	/* special case if a root pseudo definition is being added */
	if(strcmp(target, "/") == 0) {
		/* if already have a root pseudo just add xattr */
		if(pseudo && pseudo->names == 1 && strcmp(pseudo->name[0].name, "/") == 0) {
			add_xattr(&pseudo->name[0].xattr, xattr);
			return pseudo;
		} else {
			struct pseudo *new = malloc(sizeof(struct pseudo));
			if(new == NULL)
				MEM_ERROR();

			new->names = 1;
			new->count = 0;
			new->name = malloc(sizeof(struct pseudo_entry));
			if(new->name == NULL)
				MEM_ERROR();

			new->name[0].name = "/";
			new->name[0].pseudo = pseudo;
			new->name[0].pathname = "/";
			new->name[0].dev = NULL;
			new->name[0].xattr = NULL;
			add_xattr(&new->name[0].xattr, xattr);
			return new;
		}
	}

	/* if there's a root pseudo definition, skip it before walking target */
	if(pseudo && pseudo->names == 1 && strcmp(pseudo->name[0].name, "/") == 0) {
		pseudo->name[0].pseudo = add_pseudo_xattr(pseudo->name[0].pseudo, xattr, target, alltarget);
		return pseudo;
	} else
		return add_pseudo_xattr(pseudo, xattr, target, alltarget);
}


int read_pseudo_xattr(char *orig_def, char *filename, char *name, char *def)
{
	struct xattr_add *xattr = xattr_parse(def, "", "pseudo xattr");

	if(xattr == NULL) {
		print_definitions();
		free(filename);
		return FALSE;
	}

	pseudo = add_pseudo_xattr_definition(pseudo, xattr, name, name);

	free(filename);
	return TRUE;
}