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); }
static int bin_setattr(char *nam, char **argv, Options ops, UNUSED(int func)) { int ret = 0, slen, vlen; int symlink = OPT_ISSET(ops, 'h'); char *file = argv[0], *attr = argv[1], *value = argv[2]; unmetafy(file, &slen); unmetafy(attr, NULL); unmetafy(value, &vlen); if (xsetxattr(file, attr, value, vlen, 0, symlink)) { zwarnnam(nam, "%s: %e", metafy(file, slen, META_NOALLOC), errno); ret = 1; } return ret; }