static int bin_getattr(char *nam, char **argv, Options ops, UNUSED(int func)) { int ret = 0; int val_len = 0, attr_len = 0, slen; char *value, *file = argv[0], *attr = argv[1], *param = argv[2]; int symlink = OPT_ISSET(ops, 'h'); unmetafy(file, &slen); unmetafy(attr, NULL); val_len = xgetxattr(file, attr, NULL, 0, symlink); if (val_len == 0) { if (param) unsetparam(param); return 0; } if (val_len > 0) { value = (char *)zalloc(val_len+1); attr_len = xgetxattr(file, attr, value, val_len, symlink); if (attr_len > 0 && attr_len <= val_len) { value[attr_len] = '\0'; if (param) setsparam(param, metafy(value, attr_len, META_DUP)); else printf("%s\n", value); } zfree(value, val_len+1); } if (val_len < 0 || attr_len < 0 || attr_len > val_len) { zwarnnam(nam, "%s: %e", metafy(file, slen, META_NOALLOC), errno); ret = 1 + ((val_len > 0 && attr_len > val_len) || attr_len < 0); } return ret; }
static void copyxattr(const char *source, const char *target) { ssize_t i, j; /* counters to walk through strings */ ssize_t lsize, xsize; /* size in bytes of the list of xattrs and the values */ char *lxattr; /* string of xattr names */ static char *value = NULL ; /* string of an xattr name's value */ static size_t value_size = 0; /* size of the value string */ lsize = xlistxattr(source, NULL, 0); lxattr = xmalloc(lsize); xlistxattr(source, lxattr, lsize); i = 0; while (1) { while (lxattr[i++] == 0) continue; if (i >= lsize) break; j = 0; while (1) { while (exclude[j++] == 0) continue; if (j >= len_exclude) break; if (!fnmatch(&exclude[j - 1], &lxattr[i - 1], 0)) goto skip; while (exclude[j++] != 0) continue; if (j >= len_exclude) break; } xsize = xgetxattr(source, &lxattr[i-1], 0, 0); if (xsize > value_size) { value_size = xsize; value = xrealloc(value, value_size); } xgetxattr(source, &lxattr[i-1], value, xsize); xsetxattr(target, &lxattr[i-1], value, xsize); skip: while (lxattr[i++] != 0) continue; if (i >= lsize) break; } /* No need to free(value) on return because its static and we */ /* just keep reusing the same allocated memory on each call. */ free(lxattr); }