aboutsummaryrefslogtreecommitdiff
path: root/bin/sqfs2tar/src/xattr.c
blob: abec4fb4aff77d36f84419cc2564b565a35de102 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * xattr.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "sqfs2tar.h"

static tar_xattr_t *mkxattr(const sqfs_xattr_entry_t *key,
			    const sqfs_xattr_value_t *value)
{
	tar_xattr_t *ent;

	ent = calloc(1, sizeof(*ent) + strlen((const char *)key->key) +
		     value->size + 2);

	if (ent == NULL) {
		perror("creating xattr entry");
		return NULL;
	}

	ent->key = ent->data;
	ent->value = (sqfs_u8 *)ent->key + strlen((const char *)key->key) + 1;
	ent->value_len = value->size;

	strcpy(ent->key, (const char *)key->key);
	memcpy(ent->value, value->value, value->size + 1);
	return ent;
}

int get_xattrs(const char *name, const sqfs_inode_generic_t *inode,
	       tar_xattr_t **out)
{
	tar_xattr_t *list = NULL, *ent;
	sqfs_xattr_value_t *value;
	sqfs_xattr_entry_t *key;
	sqfs_xattr_id_t desc;
	sqfs_u32 index;
	size_t i;
	int ret;

	if (xr == NULL)
		return 0;

	sqfs_inode_get_xattr_index(inode, &index);
	if (index == 0xFFFFFFFF)
		return 0;

	ret = sqfs_xattr_reader_get_desc(xr, index, &desc);
	if (ret) {
		sqfs_perror(name, "resolving xattr index", ret);
		return -1;
	}

	ret = sqfs_xattr_reader_seek_kv(xr, &desc);
	if (ret) {
		sqfs_perror(name, "locating xattr key-value pairs", ret);
		return -1;
	}

	for (i = 0; i < desc.count; ++i) {
		ret = sqfs_xattr_reader_read_key(xr, &key);
		if (ret) {
			sqfs_perror(name, "reading xattr key", ret);
			goto fail;
		}

		ret = sqfs_xattr_reader_read_value(xr, key, &value);
		if (ret) {
			sqfs_perror(name, "reading xattr value", ret);
			sqfs_free(key);
			goto fail;
		}

		ent = mkxattr(key, value);
		sqfs_free(key);
		sqfs_free(value);

		if (ent == NULL)
			goto fail;

		ent->next = list;
		list = ent;
	}

	*out = list;
	return 0;
fail:
	free_xattr_list(list);
	return -1;
}