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
|
/* $OpenBSD: fuse_subr.c,v 1.5 2013/10/07 18:41:01 syl Exp $ */
/*
* Copyright (c) 2013 Sylvestre Gallon <ccna.syl@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "fuse_private.h"
#include "debug.h"
struct fuse_vnode *
alloc_vn(struct fuse *f, const char *path, ino_t ino, ino_t parent)
{
struct fuse_vnode *vn;
if ((vn = malloc(sizeof(*vn))) == NULL) {
DPERROR("alloc_vn:");
return (NULL);
}
vn->ino = ino;
vn->parent = parent;
strncpy(vn->path, path, NAME_MAX);
vn->path[NAME_MAX - 1] = '\0';
if (ino == (ino_t)-1) {
f->max_ino++;
vn->ino = f->max_ino;
}
return (vn);
}
int
set_vn(struct fuse *f, struct fuse_vnode *v)
{
struct fuse_vn_head *vn_head;
struct fuse_vnode *vn;
DPRINTF("%s: create or update vnode %llu@%llu = %s\n", __func__,
(unsigned long long)v->ino, (unsigned long long)v->parent,
v->path);
if (tree_set(&f->vnode_tree, v->ino, v) == NULL)
return (0);
if (!dict_check(&f->name_tree, v->path)) {
vn_head = malloc(sizeof(*vn_head));
if (vn_head == NULL)
return (0);
SIMPLEQ_INIT(vn_head);
} else {
vn_head = dict_get(&f->name_tree, v->path);
if (vn_head == NULL)
return (0);
}
SIMPLEQ_FOREACH(vn, vn_head, node) {
if (v->parent == vn->parent && v->ino == vn->ino)
return (1);
}
SIMPLEQ_INSERT_TAIL(vn_head, v, node);
dict_set(&f->name_tree, v->path, vn_head);
return (1);
}
struct fuse_vnode *
get_vn_by_name_and_parent(struct fuse *f, uint8_t *xpath, ino_t parent)
{
struct fuse_vn_head *vn_head;
struct fuse_vnode *v = NULL;
const char *path = (const char *)xpath;
vn_head = dict_get(&f->name_tree, path);
if (vn_head == NULL)
return (NULL);
SIMPLEQ_FOREACH(v, vn_head, node)
if (v->parent == parent)
return (v);
return (NULL);
}
char *
build_realname(struct fuse *f, ino_t ino)
{
struct fuse_vnode *vn;
char *name = strdup("/");
char *tmp = NULL;
int firstshot = 0;
vn = tree_get(&f->vnode_tree, ino);
if (!vn || !name) {
free(name);
return (NULL);
}
while (vn->parent != 0) {
if (firstshot++)
asprintf(&tmp, "/%s%s", vn->path, name);
else
asprintf(&tmp, "/%s", vn->path);
if (tmp == NULL) {
free(name);
return (NULL);
}
free(name);
name = tmp;
tmp = NULL;
vn = tree_get(&f->vnode_tree, vn->parent);
if (!vn)
return (NULL);
}
if (ino == (ino_t)0)
DPRINTF("%s: NULL ino\n", __func__);
DPRINTF("realname %s\n", name);
return (name);
}
|