2 $Id: ea_sys.c,v 1.4 2009-12-04 10:26:10 franklahm Exp $
3 Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
18 #endif /* HAVE_CONFIG_H */
25 #include <sys/types.h>
31 #include <attr/xattr.h>
32 #elif HAVE_SYS_XATTR_H
33 #include <sys/xattr.h>
40 #ifdef HAVE_SYS_EXTATTR_H
41 #include <sys/extattr.h>
44 #include <atalk/adouble.h>
46 #include <atalk/afp.h>
47 #include <atalk/logger.h>
48 #include <atalk/volume.h>
49 #include <atalk/vfs.h>
50 #include <atalk/util.h>
51 #include <atalk/unix.h>
54 #define ENOATTR ENODATA
58 /**********************************************************************************
59 * EA VFS funcs for storing EAs in nativa filesystem EAs
60 **********************************************************************************/
63 * Function: sys_get_easize
65 * Purpose: get size of a native EA
69 * vol (r) current volume
70 * rbuf (w) DSI reply buffer
71 * rbuflen (rw) current length of data in reply buffer
73 * oflag (r) link and create flag
74 * attruname (r) name of attribute
76 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
80 * Copies EA size into rbuf in network order. Increments *rbuflen +4.
82 int sys_get_easize(VFS_FUNC_ARGS_EA_GETSIZE)
87 LOG(log_debug7, logtype_afpd, "sys_getextattr_size(%s): attribute: \"%s\"", uname, attruname);
89 if ((oflag & O_NOFOLLOW) ) {
90 ret = sys_lgetxattr(uname, attruname, rbuf +4, 0);
93 ret = sys_getxattr(uname, attruname, rbuf +4, 0);
101 /* its a symlink and client requested O_NOFOLLOW */
102 LOG(log_debug, logtype_afpd, "sys_getextattr_size(%s): encountered symlink with kXAttrNoFollow", uname);
109 LOG(log_error, logtype_afpd, "sys_getextattr_size: error: %s", strerror(errno));
114 if (ret > MAX_EA_SIZE)
117 /* Start building reply packet */
118 LOG(log_debug7, logtype_afpd, "sys_getextattr_size(%s): attribute: \"%s\", size: %u", uname, attruname, ret);
120 /* length of attribute data */
121 attrsize = htonl((uint32_t)ret);
122 memcpy(rbuf, &attrsize, 4);
130 * Function: sys_get_eacontent
132 * Purpose: copy native EA into rbuf
136 * vol (r) current volume
137 * rbuf (w) DSI reply buffer
138 * rbuflen (rw) current length of data in reply buffer
140 * oflag (r) link and create flag
141 * attruname (r) name of attribute
142 * maxreply (r) maximum EA size as of current specs/real-life
144 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
148 * Copies EA into rbuf. Increments *rbuflen accordingly.
150 int sys_get_eacontent(VFS_FUNC_ARGS_EA_GETCONTENT)
155 /* Start building reply packet */
157 maxreply -= MAX_REPLY_EXTRA_BYTES;
159 if (maxreply > MAX_EA_SIZE)
160 maxreply = MAX_EA_SIZE;
162 LOG(log_debug7, logtype_afpd, "sys_getextattr_content(%s): attribute: \"%s\", size: %u", uname, attruname, maxreply);
164 if ((oflag & O_NOFOLLOW) ) {
165 ret = sys_lgetxattr(uname, attruname, rbuf +4, maxreply);
168 ret = sys_getxattr(uname, attruname, rbuf +4, maxreply);
176 /* its a symlink and client requested O_NOFOLLOW */
177 LOG(log_debug, logtype_afpd, "sys_getextattr_content(%s): encountered symlink with kXAttrNoFollow", uname);
184 LOG(log_error, logtype_afpd, "sys_getextattr_content(%s): error: %s", attruname, strerror(errno));
189 /* remember where we must store length of attribute data in rbuf */
192 attrsize = htonl((uint32_t)ret);
193 memcpy(rbuf, &attrsize, 4);
199 * Function: sys_list_eas
201 * Purpose: copy names of native EAs into attrnamebuf
205 * vol (r) current volume
206 * attrnamebuf (w) store names a consecutive C strings here
207 * buflen (rw) length of names in attrnamebuf
209 * oflag (r) link and create flag
211 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
215 * Copies names of all EAs of uname as consecutive C strings into rbuf.
216 * Increments *rbuflen accordingly.
218 int sys_list_eas(VFS_FUNC_ARGS_EA_LIST)
220 ssize_t attrbuflen = *buflen;
225 buf = malloc(ATTRNAMEBUFSIZ);
229 if ((oflag & O_NOFOLLOW)) {
230 ret = sys_llistxattr(uname, buf, ATTRNAMEBUFSIZ);
233 ret = sys_listxattr(uname, buf, ATTRNAMEBUFSIZ);
236 if (ret == -1) switch(errno) {
238 /* its a symlink and client requested O_NOFOLLOW */
239 ret = AFPERR_BADTYPE;
241 LOG(log_error, logtype_afpd, "sys_list_extattr(%s): error opening atttribute dir: %s", uname, strerror(errno));
249 /* Convert name to CH_UTF8_MAC and directly store in in the reply buffer */
250 if ( 0 >= ( nlen = convert_string(vol->v_volcharset, CH_UTF8_MAC, ptr, len, attrnamebuf + attrbuflen, 256)) ) {
255 LOG(log_debug7, logtype_afpd, "sys_list_extattr(%s): attribute: %s", uname, ptr);
257 attrbuflen += nlen + 1;
258 if (attrbuflen > (ATTRNAMEBUFSIZ - 256)) {
259 /* Next EA name could overflow, so bail out with error.
260 FIXME: evantually malloc/memcpy/realloc whatever.
262 LOG(log_warning, logtype_afpd, "sys_list_extattr(%s): running out of buffer for EA names", uname);
274 *buflen = attrbuflen;
279 * Function: sys_set_ea
281 * Purpose: set a native EA
285 * vol (r) current volume
287 * attruname (r) EA name
288 * ibuf (r) buffer with EA content
289 * attrsize (r) length EA in ibuf
290 * oflag (r) link and create flag
292 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
297 int sys_set_ea(VFS_FUNC_ARGS_EA_SET)
303 if ((oflag & O_CREAT) )
304 attr_flag |= XATTR_CREATE;
306 else if ((oflag & O_TRUNC) )
307 attr_flag |= XATTR_REPLACE;
309 if ((oflag & O_NOFOLLOW) ) {
310 ret = sys_lsetxattr(uname, attruname, ibuf, attrsize,attr_flag);
313 ret = sys_setxattr(uname, attruname, ibuf, attrsize, attr_flag);
319 /* its a symlink and client requested O_NOFOLLOW */
320 LOG(log_debug, logtype_afpd, "sys_set_ea(%s/%s): encountered symlink with kXAttrNoFollow",
324 LOG(log_error, logtype_afpd, "sys_set_ea(%s/%s): error: %s", uname, attruname, strerror(errno));
333 * Function: sys_remove_ea
335 * Purpose: remove a native EA
339 * vol (r) current volume
341 * attruname (r) EA name
342 * oflag (r) link and create flag
344 * Returns: AFP code: AFP_OK on success or appropiate AFP error code
348 * Removes EA attruname from file uname.
350 int sys_remove_ea(VFS_FUNC_ARGS_EA_REMOVE)
354 if ((oflag & O_NOFOLLOW) ) {
355 ret = sys_lremovexattr(uname, attruname);
358 ret = sys_removexattr(uname, attruname);
364 /* its a symlink and client requested O_NOFOLLOW */
365 LOG(log_debug, logtype_afpd, "sys_remove_ea(%s/%s): encountered symlink with kXAttrNoFollow", uname);
368 LOG(log_debug, logtype_afpd, "sys_remove_ea(%s/%s): error: %s", uname, attruname, strerror(errno));
369 return AFPERR_ACCESS;
371 LOG(log_error, logtype_afpd, "sys_remove_ea(%s/%s): error: %s", uname, attruname, strerror(errno));
379 /* ---------------------
382 int sys_ea_copyfile(VFS_FUNC_ARGS_COPYFILE)
386 char *names = NULL, *end_names, *name, *value = NULL;
387 unsigned int setxattr_ENOTSUP = 0;
389 size = sys_listxattr(src, NULL, 0);
391 if (errno != ENOSYS && errno != ENOTSUP) {
396 names = malloc(size+1);
401 size = sys_listxattr(src, names, size);
407 end_names = names + size;
410 for (name = names; name != end_names; name = strchr(name, '\0') + 1) {
413 /* check if this attribute shall be preserved */
417 size = sys_getxattr (src, name, NULL, 0);
422 value = realloc(old_value = value, size);
423 if (size != 0 && value == NULL) {
427 size = sys_getxattr(src, name, value, size);
432 if (sys_setxattr(dst, name, value, size, 0) != 0) {
433 if (errno == ENOTSUP)
436 if (errno == ENOSYS) {
438 /* no hope of getting any further */
446 if (setxattr_ENOTSUP) {
461 LOG(log_debug, logtype_afpd, "sys_ea_copyfile(%s, %s): error: %s", src, dst, strerror(errno));
462 return AFPERR_ACCESS;
464 LOG(log_error, logtype_afpd, "sys_ea_copyfile(%s, %s): error: %s", src, dst, strerror(errno));