sd/milkymist-memcard: Fix error API violation
[qemu.git] / hw / 9pfs / 9p-xattr.c
blobc696d8f8460f82b25630cba82b6f303182523cd8
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;
64 * Get the list and pass to each layer to find out whether
65 * to send the data or not
67 ssize_t v9fs_list_xattr(FsContext *ctx, const char *path,
68 void *value, size_t vsize)
70 ssize_t size = 0;
71 void *ovalue = value;
72 XattrOperations *xops;
73 char *orig_value, *orig_value_start;
74 ssize_t xattr_len, parsed_len = 0, attr_len;
75 char *dirpath, *name;
76 int dirfd;
78 /* Get the actual len */
79 dirpath = g_path_get_dirname(path);
80 dirfd = local_opendir_nofollow(ctx, dirpath);
81 g_free(dirpath);
82 if (dirfd == -1) {
83 return -1;
86 name = g_path_get_basename(path);
87 xattr_len = flistxattrat_nofollow(dirfd, name, value, 0);
88 if (xattr_len <= 0) {
89 g_free(name);
90 close_preserve_errno(dirfd);
91 return xattr_len;
94 /* Now fetch the xattr and find the actual size */
95 orig_value = g_malloc(xattr_len);
96 xattr_len = flistxattrat_nofollow(dirfd, name, orig_value, xattr_len);
97 g_free(name);
98 close_preserve_errno(dirfd);
99 if (xattr_len < 0) {
100 g_free(orig_value);
101 return -1;
104 /* store the orig pointer */
105 orig_value_start = orig_value;
106 while (xattr_len > parsed_len) {
107 xops = get_xattr_operations(ctx->xops, orig_value);
108 if (!xops) {
109 goto next_entry;
112 if (!value) {
113 size += xops->listxattr(ctx, path, orig_value, value, vsize);
114 } else {
115 size = xops->listxattr(ctx, path, orig_value, value, vsize);
116 if (size < 0) {
117 goto err_out;
119 value += size;
120 vsize -= size;
122 next_entry:
123 /* Got the next entry */
124 attr_len = strlen(orig_value) + 1;
125 parsed_len += attr_len;
126 orig_value += attr_len;
128 if (value) {
129 size = value - ovalue;
132 err_out:
133 g_free(orig_value_start);
134 return size;
137 int v9fs_set_xattr(FsContext *ctx, const char *path, const char *name,
138 void *value, size_t size, int flags)
140 XattrOperations *xops = get_xattr_operations(ctx->xops, name);
141 if (xops) {
142 return xops->setxattr(ctx, path, name, value, size, flags);
144 errno = EOPNOTSUPP;
145 return -1;
149 int v9fs_remove_xattr(FsContext *ctx,
150 const char *path, const char *name)
152 XattrOperations *xops = get_xattr_operations(ctx->xops, name);
153 if (xops) {
154 return xops->removexattr(ctx, path, name);
156 errno = EOPNOTSUPP;
157 return -1;
161 ssize_t local_getxattr_nofollow(FsContext *ctx, const char *path,
162 const char *name, void *value, size_t size)
164 char *dirpath = g_path_get_dirname(path);
165 char *filename = g_path_get_basename(path);
166 int dirfd;
167 ssize_t ret = -1;
169 dirfd = local_opendir_nofollow(ctx, dirpath);
170 if (dirfd == -1) {
171 goto out;
174 ret = fgetxattrat_nofollow(dirfd, filename, name, value, size);
175 close_preserve_errno(dirfd);
176 out:
177 g_free(dirpath);
178 g_free(filename);
179 return ret;
182 ssize_t pt_getxattr(FsContext *ctx, const char *path, const char *name,
183 void *value, size_t size)
185 return local_getxattr_nofollow(ctx, path, name, value, size);
188 ssize_t local_setxattr_nofollow(FsContext *ctx, const char *path,
189 const char *name, void *value, size_t size,
190 int flags)
192 char *dirpath = g_path_get_dirname(path);
193 char *filename = g_path_get_basename(path);
194 int dirfd;
195 ssize_t ret = -1;
197 dirfd = local_opendir_nofollow(ctx, dirpath);
198 if (dirfd == -1) {
199 goto out;
202 ret = fsetxattrat_nofollow(dirfd, filename, name, value, size, flags);
203 close_preserve_errno(dirfd);
204 out:
205 g_free(dirpath);
206 g_free(filename);
207 return ret;
210 int pt_setxattr(FsContext *ctx, const char *path, const char *name, void *value,
211 size_t size, int flags)
213 return local_setxattr_nofollow(ctx, path, name, value, size, flags);
216 ssize_t local_removexattr_nofollow(FsContext *ctx, const char *path,
217 const char *name)
219 char *dirpath = g_path_get_dirname(path);
220 char *filename = g_path_get_basename(path);
221 int dirfd;
222 ssize_t ret = -1;
224 dirfd = local_opendir_nofollow(ctx, dirpath);
225 if (dirfd == -1) {
226 goto out;
229 ret = fremovexattrat_nofollow(dirfd, filename, name);
230 close_preserve_errno(dirfd);
231 out:
232 g_free(dirpath);
233 g_free(filename);
234 return ret;
237 int pt_removexattr(FsContext *ctx, const char *path, const char *name)
239 return local_removexattr_nofollow(ctx, path, name);
242 ssize_t notsup_getxattr(FsContext *ctx, const char *path, const char *name,
243 void *value, size_t size)
245 errno = ENOTSUP;
246 return -1;
249 int notsup_setxattr(FsContext *ctx, const char *path, const char *name,
250 void *value, size_t size, int flags)
252 errno = ENOTSUP;
253 return -1;
256 ssize_t notsup_listxattr(FsContext *ctx, const char *path, char *name,
257 void *value, size_t size)
259 return 0;
262 int notsup_removexattr(FsContext *ctx, const char *path, const char *name)
264 errno = ENOTSUP;
265 return -1;
268 XattrOperations *mapped_xattr_ops[] = {
269 &mapped_user_xattr,
270 &mapped_pacl_xattr,
271 &mapped_dacl_xattr,
272 NULL,
275 XattrOperations *passthrough_xattr_ops[] = {
276 &passthrough_user_xattr,
277 &passthrough_acl_xattr,
278 NULL,
281 /* for .user none model should be same as passthrough */
282 XattrOperations *none_xattr_ops[] = {
283 &passthrough_user_xattr,
284 &none_acl_xattr,
285 NULL,