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

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

	ent = sqfs_xattr_create((const char *)key->key,
				value->value, value->size);
	if (ent == NULL) {
		perror("creating xattr entry");
		return NULL;
	}

	return ent;
}

int get_xattrs(const char *name, const sqfs_inode_generic_t *inode,
	       sqfs_xattr_t **out)
{
	sqfs_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:
	sqfs_xattr_list_free(list);
	return -1;
}