-
Notifications
You must be signed in to change notification settings - Fork 0
/
xattr_db.c
224 lines (180 loc) · 4.8 KB
/
xattr_db.c
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#define _GNU_SOURCE
#include <errno.h>
#include <search.h>
#include <string.h>
#include "debug.h"
#include "xattr_db.h"
#include "zalloc.h"
struct xattr_db_attr_list {
struct xattr_db_attr_list *next;
xattr_db_attr_name name;
struct {
size_t len;
char * buf;
} data;
};
static void xattr_db_attr_list_free(struct xattr_db_attr_list *head)
{
while (head)
{
struct xattr_db_attr_list *next = head->next;
if (head->data.buf) free(head->data.buf);
free(head);
head = next;
}
}
static struct xattr_db_attr_list * xattr_db_attr_list_entry(struct xattr_db_attr_list **head, xattr_db_attr_name name, int create, int replace)
{
struct xattr_db_attr_list *entry;
while (*head)
{
int cmp = strncmp(name, (*head)->name, sizeof(xattr_db_attr_name));
if (cmp == 0)
{
if (!create) return *head;
else
{
errno = EEXIST;
return NULL;
}
}
else if (cmp < 0) break;
else head = &(*head)->next;
}
if (replace)
{
errno = ENODATA;
return NULL;
}
entry = zalloc(sizeof(struct xattr_db_attr_list));
entry->next = *head;
memcpy(entry->name, name, sizeof(xattr_db_attr_name));
*head = entry;
debug_printf("new xattr key \"%s\" added", name);
return entry;
}
int xattr_db_attr_list_remove(struct xattr_db_attr_list **head, xattr_db_attr_name name)
{
struct xattr_db_attr_list *entry;
while (*head)
{
if (strncmp(name, (*head)->name, sizeof(xattr_db_attr_name)) == 0) break;
else head = &(*head)->next;
}
entry = *head;
if (!entry)
{
errno = ENODATA;
return -1;
}
*head = entry->next;
if (entry->data.buf) free(entry->data.buf);
free(entry);
return 0;
}
struct xattr_db_tree_node {
xattr_db_file_id file_id;
struct xattr_db_attr_list *head;
};
static void xattr_db_tree_node_free(struct xattr_db_tree_node *node)
{
if (node->head) xattr_db_attr_list_free(node->head);
free(node);
}
struct xattr_db_ctx {
void *tree;
struct xattr_db_tree_node *scratch;
};
static int xattr_db_file_id_compare(const struct xattr_db_tree_node *a, const struct xattr_db_tree_node *b)
{
return memcmp(&a->file_id, &b->file_id, sizeof(xattr_db_file_id));
}
xattr_db_ctx * xattr_db_init()
{
return zalloc(sizeof(struct xattr_db_ctx));
}
void xattr_db_free(xattr_db_ctx *ctx)
{
tdestroy(ctx->tree, (void (*)(void *)) xattr_db_tree_node_free);
if (ctx->scratch) xattr_db_tree_node_free(ctx->scratch);
free(ctx);
}
static struct xattr_db_tree_node * xattr_db_tree_get(xattr_db_ctx *ctx, xattr_db_file_id file_id)
{
struct xattr_db_tree_node *node;
if (!ctx->scratch) ctx->scratch = zalloc(sizeof (struct xattr_db_tree_node));
ctx->scratch->file_id = file_id;
node = *((struct xattr_db_tree_node **) tsearch(ctx->scratch, &ctx->tree, (int (*)(const void *, const void *)) xattr_db_file_id_compare));
if (node == ctx->scratch)
{
debug_printf("new database entry created for inode %lu on dev %u:%u", node->file_id.ino, node->file_id.dev.major, node->file_id.dev.minor);
ctx->scratch = NULL;
}
else debug_printf("found database entry for inode %lu on dev %u:%u", node->file_id.ino, node->file_id.dev.major, node->file_id.dev.minor);
return node;
}
int xattr_db_set(xattr_db_ctx *ctx, xattr_db_file_id file_id, xattr_db_attr_name name, const char *data, size_t len, int create, int replace)
{
struct xattr_db_tree_node *node;
struct xattr_db_attr_list *entry;
node = xattr_db_tree_get(ctx, file_id);
entry = xattr_db_attr_list_entry(&node->head, name, create, replace);
if (!entry) return -1;
if(entry->data.buf) free(entry->data.buf);
entry->data.len = len;
entry->data.buf = zalloc(len);
memcpy(entry->data.buf, data, len);
return 0;
}
ssize_t xattr_db_get(xattr_db_ctx *ctx, xattr_db_file_id file_id, xattr_db_attr_name name, char *data, size_t size)
{
size_t len;
struct xattr_db_tree_node *node;
struct xattr_db_attr_list *entry;
node = xattr_db_tree_get(ctx, file_id);
entry = xattr_db_attr_list_entry(&node->head, name, 0, 1);
if (!entry) return -1;
len = entry->data.len;
if (len > size)
{
errno = ERANGE;
return len;
}
memcpy(data, entry->data.buf, len);
return len;
}
ssize_t xattr_db_list(xattr_db_ctx *ctx, xattr_db_file_id file_id, char *buf, size_t size)
{
struct xattr_db_tree_node *node;
struct xattr_db_attr_list *head;
size_t total_len = 0;
int _errno;
_errno = 0;
node = xattr_db_tree_get(ctx, file_id);
head = node->head;
while (head)
{
size_t len = strnlen(head->name, sizeof(xattr_db_attr_name) - 1) + 1;
if (len <= size)
{
memcpy(buf, head->name, len);
buf += len;
size -= len;
}
else
{
_errno = ERANGE;
size = 0;
}
total_len += len;
head = head->next;
}
if (_errno) errno = _errno;
return total_len;
}
int xattr_db_remove(xattr_db_ctx *ctx, xattr_db_file_id file_id, xattr_db_attr_name name)
{
struct xattr_db_tree_node *node;
node = xattr_db_tree_get(ctx, file_id);
return xattr_db_attr_list_remove(&node->head, name);
}