summaryrefslogtreecommitdiffstats
path: root/squashfs-tools/tar_xattr.c
blob: fc9eef57b793364ccfc33fe99d67a93659ab4caf (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
/*
 * Squashfs
 *
 * Copyright (c) 2021, 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.
 *
 * tar_xattr.c
 */

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

#include "squashfs_fs.h"
#include "mksquashfs.h"
#include "mksquashfs_error.h"
#include "tar.h"
#include "xattr.h"

#define TRUE 1
#define FALSE 0

extern regex_t *xattr_exclude_preg;
extern regex_t *xattr_include_preg;


void read_tar_xattr(char *name, char *value, int size, int encoding, struct tar_file *file)
{
	char *data;
	struct xattr_list *xattr;
	int i;

	/* Some tars output both LIBARCHIVE and SCHILY xattrs, which
	 * will lead to multiple definitions of the same xattr.
	 * So check that this xattr hasn't already been defined */
	for(i = 0; i < file->xattrs; i++)
		if(strcmp(name, file->xattr_list[i].full_name) == 0)
			return;

	if(xattr_exclude_preg) {
		int res = regexec(xattr_exclude_preg, name, (size_t) 0, NULL, 0);

		if(res == 0)
			return;
	}

	if(xattr_include_preg) {
		int res = regexec(xattr_include_preg, name, (size_t) 0, NULL, 0);

		if(res)
			return;
	}

	if(encoding == ENCODING_BASE64) {
		data = base64_decode(value, size, &size);
		if(data == NULL) {
			ERROR("Invalid LIBARCHIVE xattr base64 value, ignoring\n");
			return;
		}
	} else {
		data = malloc(size);
		if(data == NULL)
			MEM_ERROR();
		memcpy(data, value, size);
	}

	file->xattr_list = realloc(file->xattr_list, (file->xattrs + 1) *
						sizeof(struct xattr_list));
	if(file->xattr_list == NULL)
		MEM_ERROR();

	xattr = &file->xattr_list[file->xattrs];

	xattr->type = xattr_get_prefix(xattr, name);
	if(xattr->type == -1) {
		ERROR("Unrecognised tar xattr prefix %s, ignoring\n", name);
		free(data);
		return;
	}

	xattr->value = data;
	xattr->vsize = size;
	file->xattrs ++;
}


int read_xattrs_from_tarfile(struct inode_info *inode, struct xattr_list **xattr_list)
{
	if(inode->tar_file) {
		*xattr_list = inode->tar_file->xattr_list;
		return inode->tar_file->xattrs;
	} else
		return 0;
}


void free_tar_xattrs(struct tar_file *file)
{
	int i;

	for(i = 0; i < file->xattrs; i++)
		free(file->xattr_list[i].full_name);

	free(file->xattr_list);
}