/* * Copyright (C) 2006 James Antill * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * email: james@and.org */ /* these functions are wrappers around the generic xattr system calls, to * provide a nice interface (not ERANGE chaecking needed */ #include "xattr_sc.h" #include #if XATTR_SC_CACHE /* do you want to cache the memory */ # define CACHE_DECL static #else # define CACHE_DECL /* nothing */ #endif #define CONF_INIT_VAL_SZ 64 #define CONF_INIT_LIST_SZ (64 << 3) /* enough for 8 keys of length 63+1 */ char *xattr_sc_listxattrs(const char *fname, int fd, unsigned int flags, ssize_t *sz) { CACHE_DECL ssize_t store_sz = CONF_INIT_LIST_SZ; CACHE_DECL char *store_list = NULL; ssize_t ret = -1; ssize_t (*nlistxattr)(const char *, char *, size_t); if (!fname && (fd == -1)) { free(store_list); store_list = NULL; store_sz = CONF_INIT_LIST_SZ; return (NULL); } if (flags & XATTR_FLAG_SC_LISTXATTRS_FOLLOW) nlistxattr = listxattr; else nlistxattr = llistxattr; if (!store_list && !(store_list = malloc(store_sz))) goto mem_fail; while (((fd == -1) ? ((ret = nlistxattr(fname, store_list, store_sz)) == -1) : ((ret = flistxattr(fd, store_list, store_sz)) == -1)) && (errno == ERANGE)) { ssize_t tmp_sz = store_sz << 1; char *tmp_mem = NULL; if (!(tmp_mem = realloc(store_list, tmp_sz))) goto mem_fail; store_list = tmp_mem; store_sz = tmp_sz; } *sz = ret; if ((ret == -1) || (ret == 0)) { if ((ret == -1) && (errno == ENOSYS)) /* pretend it was empty */ *sz = 0; XATTR_SC_RET_FREE(store_list); return (NULL); } return (store_list); mem_fail: XATTR_SC_RET_FREE(store_list); errno = ENOMEM; return (NULL); } char *xattr_sc_getxattrs(const char *fname, int fd, const char *attr, unsigned int flags, ssize_t *sz) { CACHE_DECL ssize_t store_sz = CONF_INIT_VAL_SZ; CACHE_DECL char *store_val = NULL; ssize_t ret = -1; ssize_t (*ngetxattr)(const char *, const char *, void *, size_t); if (!fname && (fd == -1)) { free(store_val); store_val = NULL; store_sz = CONF_INIT_VAL_SZ; return (NULL); } if (flags & XATTR_FLAG_SC_GETXATTRS_FOLLOW) ngetxattr = getxattr; else ngetxattr = lgetxattr; if (!store_val && !(store_val = malloc(store_sz))) goto mem_fail; while (((fd == -1) ? ((ret = ngetxattr(fname, attr, store_val, store_sz)) == -1) : ((ret = fgetxattr(fd, attr, store_val, store_sz)) == -1)) && (errno == ERANGE)) { ssize_t tmp_sz = store_sz << 1; char *tmp_mem = NULL; if (!(tmp_mem = realloc(store_val, tmp_sz))) goto mem_fail; store_val = tmp_mem; store_sz = tmp_sz; } *sz = ret; if (ret == -1) { if (errno == ENODATA) /* pretend it was empty, race */ *sz = 0; XATTR_SC_RET_FREE(store_val); return (NULL); } return (store_val); mem_fail: XATTR_SC_RET_FREE(store_val); errno = ENOMEM; return (NULL); }