char: remove qemu_chr_be_generic_open
[qemu/ar7.git] / hw / 9pfs / 9p-xattr.c
blobd05c1a1c1df50629c6d25afaaf96cec71e5928a9
1 /*
2 * 9p xattr callback
4 * Copyright IBM, Corp. 2010
6 * Authors:
7 * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "9p.h"
16 #include "fsdev/file-op-9p.h"
17 #include "9p-xattr.h"
18 #include "9p-util.h"
19 #include "9p-local.h"
22 static XattrOperations *get_xattr_operations(XattrOperations **h,
23 const char *name)
25 XattrOperations *xops;
26 for (xops = *(h)++; xops != NULL; xops = *(h)++) {
27 if (!strncmp(name, xops->name, strlen(xops->name))) {
28 return xops;
31 return NULL;
34 ssize_t v9fs_get_xattr(FsContext *ctx, const char *path,
35 const char *name, void *value, size_t size)
37 XattrOperations *xops = get_xattr_operations(ctx->xops, name);
38 if (xops) {
39 return xops->getxattr(ctx, path, name, value, size);
41 errno = EOPNOTSUPP;
42 return -1;
45 ssize_t pt_listxattr(FsContext *ctx, const char *path,
46 char *name, void *value, size_t size)
48 int name_size = strlen(name) + 1;
49 if (!value) {
50 return name_size;
53 if (size < name_size) {
54 errno = ERANGE;
55 return -1;
58 /* no need for strncpy: name_size is strlen(name)+1 */
59 memcpy(value, name, name_size);
60 return name_size;
63 static ssize_t flistxattrat_nofollow(int dirfd, const char *filename,
64 char *list, size_t size)
66 char *proc_path = g_strdup_printf("/proc/self/fd/%d/%s", dirfd, filename);
67 int ret;
69 ret = llistxattr(proc_path, list, size);
70 g_free(proc_path);
71 return ret;
75 * Get the list and pass to each layer to find out whether
76 * to send the data or not
78 ssize_t v9fs_list_xattr(FsContext *ctx, const char *path,
79 void *value, size_t vsize)
81 ssize_t size = 0;
82 void *ovalue = value;
83 XattrOperations *xops;
84 char *orig_value, *orig_value_start;
85 ssize_t xattr_len, parsed_len = 0, attr_len;
86 char *dirpath, *name;
87 int dirfd;
89 /* Get the actual len */
90 dirpath = g_path_get_dirname(path);
91 dirfd = local_opendir_nofollow(ctx, dirpath);
92 g_free(dirpath);
93 if (dirfd == -1) {
94 return -1;
97 name = g_path_get_basename(path);
98 xattr_len = flistxattrat_nofollow(dirfd, name, value, 0);
99 if (xattr_len <= 0) {
100 g_free(name);
101 close_preserve_errno(dirfd);
102 return xattr_len;
105 /* Now fetch the xattr and find the actual size */
106 orig_value = g_malloc(xattr_len);
107 xattr_len = flistxattrat_nofollow(dirfd, name, orig_value, xattr_len);
108 g_free(name);
109 close_preserve_errno(dirfd);
110 if (xattr_len < 0) {
111 g_free(orig_value);
112 return -1;
115 /* store the orig pointer */
116 orig_value_start = orig_value;
117 while (xattr_len > parsed_len) {
118 xops = get_xattr_operations(ctx->xops, orig_value);
119 if (!xops) {
120 goto next_entry;
123 if (!value) {
124 size += xops->listxattr(ctx, path, orig_value, value, vsize);
125 } else {
126 size = xops->listxattr(ctx, path, orig_value, value, vsize);
127 if (size < 0) {
128 goto err_out;
130 value += size;
131 vsize -= size;
133 next_entry:
134 /* Got the next entry */
135 attr_len = strlen(orig_value) + 1;
136 parsed_len += attr_len;
137 orig_value += attr_len;
139 if (value) {
140 size = value - ovalue;
143 err_out:
144 g_free(orig_value_start);
145 return size;
148 int v9fs_set_xattr(FsContext *ctx, const char *path, const char *name,
149 void *value, size_t size, int flags)
151 XattrOperations *xops = get_xattr_operations(ctx->xops, name);
152 if (xops) {
153 return xops->setxattr(ctx, path, name, value, size, flags);
155 errno = EOPNOTSUPP;
156 return -1;
160 int v9fs_remove_xattr(FsContext *ctx,
161 const char *path, const char *name)
163 XattrOperations *xops = get_xattr_operations(ctx->xops, name);
164 if (xops) {
165 return xops->removexattr(ctx, path, name);
167 errno = EOPNOTSUPP;
168 return -1;
172 ssize_t local_getxattr_nofollow(FsContext *ctx, const char *path,
173 const char *name, void *value, size_t size)
175 char *dirpath = g_path_get_dirname(path);
176 char *filename = g_path_get_basename(path);
177 int dirfd;
178 ssize_t ret = -1;
180 dirfd = local_opendir_nofollow(ctx, dirpath);
181 if (dirfd == -1) {
182 goto out;
185 ret = fgetxattrat_nofollow(dirfd, filename, name, value, size);
186 close_preserve_errno(dirfd);
187 out:
188 g_free(dirpath);
189 g_free(filename);
190 return ret;
193 ssize_t pt_getxattr(FsContext *ctx, const char *path, const char *name,
194 void *value, size_t size)
196 return local_getxattr_nofollow(ctx, path, name, value, size);
199 int fsetxattrat_nofollow(int dirfd, const char *filename, const char *name,
200 void *value, size_t size, int flags)
202 char *proc_path = g_strdup_printf("/proc/self/fd/%d/%s", dirfd, filename);
203 int ret;
205 ret = lsetxattr(proc_path, name, value, size, flags);
206 g_free(proc_path);
207 return ret;
210 ssize_t local_setxattr_nofollow(FsContext *ctx, const char *path,
211 const char *name, void *value, size_t size,
212 int flags)
214 char *dirpath = g_path_get_dirname(path);
215 char *filename = g_path_get_basename(path);
216 int dirfd;
217 ssize_t ret = -1;
219 dirfd = local_opendir_nofollow(ctx, dirpath);
220 if (dirfd == -1) {
221 goto out;
224 ret = fsetxattrat_nofollow(dirfd, filename, name, value, size, flags);
225 close_preserve_errno(dirfd);
226 out:
227 g_free(dirpath);
228 g_free(filename);
229 return ret;
232 int pt_setxattr(FsContext *ctx, const char *path, const char *name, void *value,
233 size_t size, int flags)
235 return local_setxattr_nofollow(ctx, path, name, value, size, flags);
238 static ssize_t fremovexattrat_nofollow(int dirfd, const char *filename,
239 const char *name)
241 char *proc_path = g_strdup_printf("/proc/self/fd/%d/%s", dirfd, filename);
242 int ret;
244 ret = lremovexattr(proc_path, name);
245 g_free(proc_path);
246 return ret;
249 ssize_t local_removexattr_nofollow(FsContext *ctx, const char *path,
250 const char *name)
252 char *dirpath = g_path_get_dirname(path);
253 char *filename = g_path_get_basename(path);
254 int dirfd;
255 ssize_t ret = -1;
257 dirfd = local_opendir_nofollow(ctx, dirpath);
258 if (dirfd == -1) {
259 goto out;
262 ret = fremovexattrat_nofollow(dirfd, filename, name);
263 close_preserve_errno(dirfd);
264 out:
265 g_free(dirpath);
266 g_free(filename);
267 return ret;
270 int pt_removexattr(FsContext *ctx, const char *path, const char *name)
272 return local_removexattr_nofollow(ctx, path, name);
275 ssize_t notsup_getxattr(FsContext *ctx, const char *path, const char *name,
276 void *value, size_t size)
278 errno = ENOTSUP;
279 return -1;
282 int notsup_setxattr(FsContext *ctx, const char *path, const char *name,
283 void *value, size_t size, int flags)
285 errno = ENOTSUP;
286 return -1;
289 ssize_t notsup_listxattr(FsContext *ctx, const char *path, char *name,
290 void *value, size_t size)
292 return 0;
295 int notsup_removexattr(FsContext *ctx, const char *path, const char *name)
297 errno = ENOTSUP;
298 return -1;
301 XattrOperations *mapped_xattr_ops[] = {
302 &mapped_user_xattr,
303 &mapped_pacl_xattr,
304 &mapped_dacl_xattr,
305 NULL,
308 XattrOperations *passthrough_xattr_ops[] = {
309 &passthrough_user_xattr,
310 &passthrough_acl_xattr,
311 NULL,
314 /* for .user none model should be same as passthrough */
315 XattrOperations *none_xattr_ops[] = {
316 &passthrough_user_xattr,
317 &none_acl_xattr,
318 NULL,