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
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include "config.h"
#include "fstree.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
int main(void)
{
tree_node_t *a, *b;
struct stat sb;
fstree_t fs;
char *opts;
opts = strdup("mode=0755,uid=21,gid=42");
assert(fstree_init(&fs, 512, opts) == 0);
free(opts);
memset(&sb, 0, sizeof(sb));
sb.st_mode = S_IFDIR | 0750;
sb.st_uid = 1000;
sb.st_gid = 100;
a = fstree_add_generic(&fs, "dir", &sb, NULL);
assert(a != NULL);
assert(strcmp(a->name, "dir") == 0);
assert(a->mode == sb.st_mode);
assert(a->uid == sb.st_uid);
assert(a->gid == sb.st_gid);
assert(a->parent == fs.root);
assert(a->next == NULL);
assert(fs.root->data.dir->children == a);
assert(!a->data.dir->created_implicitly);
memset(&sb, 0, sizeof(sb));
sb.st_mode = S_IFBLK | 0640;
sb.st_rdev = 1234;
b = fstree_add_generic(&fs, "blkdev", &sb, NULL);
assert(b != NULL);
assert(b != a);
assert(strcmp(b->name, "blkdev") == 0);
assert(b->mode == sb.st_mode);
assert(b->uid == sb.st_uid);
assert(b->gid == sb.st_gid);
assert(b->parent == fs.root);
assert(b->data.devno == sb.st_rdev);
assert(b->next == a);
assert(fs.root->data.dir->children == b);
assert(fstree_add_generic(&fs, "blkdev/foo", &sb, NULL) == NULL);
assert(errno == ENOTDIR);
assert(fstree_add_generic(&fs, "dir", &sb, NULL) == NULL);
assert(errno == EEXIST);
memset(&sb, 0, sizeof(sb));
sb.st_mode = S_IFDIR | 0755;
assert(fstree_add_generic(&fs, "dir", &sb, NULL) == NULL);
assert(errno == EEXIST);
memset(&sb, 0, sizeof(sb));
sb.st_mode = S_IFCHR | 0444;
b = fstree_add_generic(&fs, "dir/chrdev", &sb, NULL);
assert(b != NULL);
assert(b->mode == sb.st_mode);
assert(b->uid == sb.st_uid);
assert(b->gid == sb.st_gid);
assert(b->parent == a);
assert(b->data.devno == sb.st_rdev);
assert(b->next == NULL);
assert(a->data.dir->children == b);
b = fstree_add_generic(&fs, "dir/foo/chrdev", &sb, NULL);
assert(b != NULL);
assert(b->next == NULL);
assert(b->mode == sb.st_mode);
assert(b->uid == sb.st_uid);
assert(b->gid == sb.st_gid);
assert(b->parent != a);
assert(b->parent->parent == a);
assert(b->data.devno == sb.st_rdev);
assert(b->next == NULL);
assert(a->data.dir->children != b);
b = b->parent;
assert(b->data.dir->created_implicitly);
assert(b->mode == (S_IFDIR | 0755));
assert(b->uid == 21);
assert(b->gid == 42);
memset(&sb, 0, sizeof(sb));
sb.st_mode = S_IFDIR | 0750;
sb.st_uid = 1000;
sb.st_gid = 100;
a = fstree_add_generic(&fs, "dir/foo", &sb, NULL);
assert(a != NULL);
assert(a == b);
assert(!a->data.dir->created_implicitly);
assert(a->mode == sb.st_mode);
assert(a->uid == sb.st_uid);
assert(a->gid == sb.st_gid);
assert(fstree_add_generic(&fs, "dir/foo", &sb, NULL) == NULL);
assert(errno == EEXIST);
fstree_cleanup(&fs);
return EXIT_SUCCESS;
}
|