aboutsummaryrefslogtreecommitdiff
path: root/lib/fstree/xattr.c
blob: 4597d9de089d32d10da08af7db790af151d5d833 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include "fstree.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

static void remove_from_list(fstree_t *fs, tree_xattr_t *xattr)
{
	tree_xattr_t *prev = NULL, *it = fs->xattr;

	while (it != xattr) {
		prev = it;
		it = it->next;
	}

	if (prev == NULL) {
		fs->xattr = xattr->next;
	} else {
		prev->next = xattr->next;
	}
}

static tree_xattr_t *grow_xattr_block(tree_xattr_t *xattr)
{
	size_t new_size, old_size = 0, new_count = 4;
	void *new;

	if (xattr != NULL) {
		new_count = xattr->max_attr * 2;
		old_size = sizeof(*xattr) + sizeof(uint64_t) * xattr->max_attr;
	}

	new_size = sizeof(*xattr) + sizeof(uint64_t) * new_count;
	new = realloc(xattr, new_size);

	if (new == NULL) {
		perror("adding extended attributes");
		free(xattr);
		return NULL;
	}

	memset((char *)new + old_size, 0, new_size - old_size);

	xattr = new;
	xattr->max_attr = new_count;
	return xattr;
}

int fstree_add_xattr(fstree_t *fs, tree_node_t *node,
		     const char *key, const char *value)
{
	size_t key_idx, value_idx;
	tree_xattr_t *xattr;

	if (str_table_get_index(&fs->xattr_keys, key, &key_idx))
		return -1;

	if (str_table_get_index(&fs->xattr_values, value, &value_idx))
		return -1;

	if (sizeof(size_t) > sizeof(uint32_t)) {
		if (key_idx > 0xFFFFFFFFUL) {
			fputs("Too many unique xattr keys\n", stderr);
			return -1;
		}

		if (value_idx > 0xFFFFFFFFUL) {
			fputs("Too many unique xattr values\n", stderr);
			return -1;
		}
	}

	xattr = node->xattr;

	if (xattr == NULL || xattr->max_attr == xattr->num_attr) {
		if (xattr != NULL)
			remove_from_list(fs, xattr);

		xattr = grow_xattr_block(xattr);
		if (xattr == NULL)
			return -1;

		node->xattr = xattr;
		xattr->owner = node;
		xattr->next = fs->xattr;
		fs->xattr = xattr;
	}

	xattr->ref[xattr->num_attr]  = (uint64_t)key_idx << 32;
	xattr->ref[xattr->num_attr] |= (uint64_t)value_idx;
	xattr->num_attr += 1;
	return 0;
}

static int cmp_u64(const void *lhs, const void *rhs)
{
	uint64_t l = *((uint64_t *)lhs), r = *((uint64_t *)rhs);

	return l < r ? -1 : (l > r ? 1 : 0);
}

void fstree_xattr_reindex(fstree_t *fs)
{
	uint32_t key_idx, value_idx;
	size_t i, index = 0;
	tree_xattr_t *it;

	str_table_reset_ref_count(&fs->xattr_keys);
	str_table_reset_ref_count(&fs->xattr_values);

	for (it = fs->xattr; it != NULL; it = it->next) {
		it->index = index++;

		for (i = 0; i < it->num_attr; ++i) {
			key_idx = (it->ref[i] >> 32) & 0x00000000FFFFFFFF;
			value_idx = it->ref[i] & 0x00000000FFFFFFFF;

			str_table_add_ref(&fs->xattr_keys, key_idx);
			str_table_add_ref(&fs->xattr_values, value_idx);
		}
	}
}

void fstree_xattr_deduplicate(fstree_t *fs)
{
	tree_xattr_t *it, *it1, *prev;
	int diff;

	for (it = fs->xattr; it != NULL; it = it->next)
		qsort(it->ref, it->num_attr, sizeof(it->ref[0]), cmp_u64);

	prev = NULL;
	it = fs->xattr;

	while (it != NULL) {
		for (it1 = fs->xattr; it1 != it; it1 = it1->next) {
			if (it1->num_attr != it->num_attr)
				continue;

			diff = memcmp(it1->ref, it->ref,
				      it->num_attr * sizeof(it->ref[0]));
			if (diff == 0)
				break;
		}

		if (it1 != it) {
			prev->next = it->next;
			it->owner->xattr = it1;

			free(it);
			it = prev->next;
		} else {
			prev = it;
			it = it->next;
		}
	}

	fstree_xattr_reindex(fs);
}