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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
* fstree.c
*
* Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
*/
#include "config.h"
#include "fstree.h"
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <errno.h>
static void free_recursive(tree_node_t *n)
{
tree_node_t *it;
if (S_ISDIR(n->mode)) {
while (n->data.children != NULL) {
it = n->data.children;
n->data.children = it->next;
free_recursive(it);
}
}
free(n);
}
static tree_node_t *child_by_name(tree_node_t *root, const char *name,
size_t len)
{
tree_node_t *n = root->data.children;
while (n != NULL) {
if (strncmp(n->name, name, len) == 0 && n->name[len] == '\0')
break;
n = n->next;
}
return n;
}
static void insert_sorted(tree_node_t *root, tree_node_t *n)
{
tree_node_t *it = root->data.children, *prev = NULL;
while (it != NULL && strcmp(it->name, n->name) < 0) {
prev = it;
it = it->next;
}
n->parent = root;
n->next = it;
if (prev == NULL) {
root->data.children = n;
} else {
prev->next = n;
}
}
static tree_node_t *mknode(tree_node_t *parent, const char *name,
size_t name_len, const char *extra,
const struct stat *sb)
{
tree_node_t *n;
size_t size;
char *ptr;
size = sizeof(tree_node_t) + name_len + 1;
if (extra != NULL)
size += strlen(extra) + 1;
n = calloc(1, size);
if (n == NULL)
return NULL;
n->xattr_idx = 0xFFFFFFFF;
n->uid = sb->st_uid;
n->gid = sb->st_gid;
n->mode = sb->st_mode;
n->mod_time = sb->st_mtime;
n->link_count = 1;
n->name = (char *)n->payload;
memcpy(n->name, name, name_len);
if (extra != NULL) {
ptr = n->name + name_len + 1;
strcpy(ptr, extra);
} else {
ptr = NULL;
}
switch (sb->st_mode & S_IFMT) {
case S_IFREG:
n->data.file.input_file = ptr;
break;
case S_IFLNK:
n->mode = S_IFLNK | 0777;
n->data.target = ptr;
break;
case S_IFBLK:
case S_IFCHR:
n->data.devno = sb->st_rdev;
break;
case S_IFDIR:
n->link_count = 2;
break;
default:
break;
}
if (parent->link_count == 0xFFFFFFFF) {
free(n);
errno = EMLINK;
return NULL;
}
insert_sorted(parent, n);
parent->link_count++;
return n;
}
int fstree_init(fstree_t *fs, const fstree_defaults_t *defaults)
{
memset(fs, 0, sizeof(*fs));
fs->defaults = *defaults;
fs->root = calloc(1, sizeof(tree_node_t) + 1);
if (fs->root == NULL) {
perror("initializing file system tree");
return -1;
}
fs->root->xattr_idx = 0xFFFFFFFF;
fs->root->uid = defaults->uid;
fs->root->gid = defaults->gid;
fs->root->mode = S_IFDIR | (defaults->mode & 07777);
fs->root->mod_time = defaults->mtime;
fs->root->link_count = 2;
fs->root->name = (char *)fs->root->payload;
fs->root->flags |= FLAG_DIR_CREATED_IMPLICITLY;
return 0;
}
void fstree_cleanup(fstree_t *fs)
{
free_recursive(fs->root);
free(fs->inodes);
memset(fs, 0, sizeof(*fs));
}
tree_node_t *fstree_get_node_by_path(fstree_t *fs, tree_node_t *root,
const char *path, bool create_implicitly,
bool stop_at_parent)
{
const char *end;
tree_node_t *n;
size_t len;
while (*path != '\0') {
while (*path == '/')
++path;
if (!S_ISDIR(root->mode)) {
errno = ENOTDIR;
return NULL;
}
end = strchr(path, '/');
if (end == NULL) {
if (stop_at_parent)
break;
len = strlen(path);
} else {
len = end - path;
}
n = child_by_name(root, path, len);
if (n == NULL) {
struct stat sb;
if (!create_implicitly) {
errno = ENOENT;
return NULL;
}
memset(&sb, 0, sizeof(sb));
sb.st_mode = S_IFDIR | (fs->defaults.mode & 07777);
sb.st_uid = fs->defaults.uid;
sb.st_gid = fs->defaults.gid;
sb.st_mtime = fs->defaults.mtime;
n = mknode(root, path, len, NULL, &sb);
if (n == NULL)
return NULL;
n->flags |= FLAG_DIR_CREATED_IMPLICITLY;
}
root = n;
path = path + len;
}
return root;
}
tree_node_t *fstree_add_generic(fstree_t *fs, const char *path,
const struct stat *sb, const char *extra)
{
tree_node_t *child, *parent;
const char *name;
if (S_ISLNK(sb->st_mode) && extra == NULL) {
errno = EINVAL;
return NULL;
}
if (*path == '\0') {
child = fs->root;
assert(child != NULL);
goto out;
}
parent = fstree_get_node_by_path(fs, fs->root, path, true, true);
if (parent == NULL)
return NULL;
name = strrchr(path, '/');
name = (name == NULL ? path : (name + 1));
child = child_by_name(parent, name, strlen(name));
out:
if (child != NULL) {
if (!S_ISDIR(child->mode) || !S_ISDIR(sb->st_mode) ||
!(child->flags & FLAG_DIR_CREATED_IMPLICITLY)) {
errno = EEXIST;
return NULL;
}
child->uid = sb->st_uid;
child->gid = sb->st_gid;
child->mode = sb->st_mode;
child->mod_time = sb->st_mtime;
child->flags &= ~FLAG_DIR_CREATED_IMPLICITLY;
return child;
}
return mknode(parent, name, strlen(name), extra, sb);
}
|