Various fixes.
* src/xattrs.c: Don't mix variable declarations and statements. Use proper data types. Remove improper use of const qualifiers. Use x2nrealloc to reallocate memory buffers.
This commit is contained in:
130
src/xattrs.c
130
src/xattrs.c
@@ -7,7 +7,7 @@
|
||||
|
||||
This program is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU General Public License as published by the
|
||||
Free Software Foundation; either version 3, or (at your option) any later
|
||||
Free Software Foundation; either version 2, or (at your option) any later
|
||||
version.
|
||||
|
||||
This program is distributed in the hope that it will be useful, but
|
||||
@@ -33,8 +33,8 @@
|
||||
struct xattrs_mask_map
|
||||
{
|
||||
const char **masks;
|
||||
int size;
|
||||
int used;
|
||||
size_t size;
|
||||
size_t used;
|
||||
};
|
||||
|
||||
/* list of fnmatch patterns */
|
||||
@@ -136,34 +136,33 @@ perms2acl (int perms)
|
||||
static char *
|
||||
skip_to_ext_fields (char *ptr)
|
||||
{
|
||||
ptr += strcspn (ptr, ":,\n"); /* skip tag name. Ie. user/group/default/mask */
|
||||
/* skip tag name (user/group/default/mask) */
|
||||
ptr += strcspn (ptr, ":,\n");
|
||||
|
||||
if (*ptr != ':')
|
||||
return ptr; /* error? no user/group field */
|
||||
return ptr;
|
||||
++ptr;
|
||||
|
||||
ptr += strcspn (ptr, ":,\n"); /* skip user/group name */
|
||||
|
||||
if (*ptr != ':')
|
||||
return ptr; /* error? no perms field */
|
||||
return ptr;
|
||||
++ptr;
|
||||
|
||||
ptr += strcspn (ptr, ":,\n"); /* skip perms */
|
||||
|
||||
if (*ptr != ':')
|
||||
return ptr; /* no extra fields */
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
/* The POSIX draft allows extra fields after the three main ones. Star
|
||||
uses this to add a fourth field for user/group which is the numeric ID.
|
||||
We just skip all extra fields atm. */
|
||||
static const char *
|
||||
fixup_extra_acl_fields (const char *ptr)
|
||||
This function removes such extra fields by overwriting them with the
|
||||
characters that follow. */
|
||||
static char *
|
||||
fixup_extra_acl_fields (char *ptr)
|
||||
{
|
||||
char *src = (char *) ptr;
|
||||
char *dst = (char *) ptr;
|
||||
char *src = ptr;
|
||||
char *dst = ptr;
|
||||
|
||||
while (*src)
|
||||
{
|
||||
@@ -188,11 +187,12 @@ fixup_extra_acl_fields (const char *ptr)
|
||||
return ptr;
|
||||
}
|
||||
|
||||
/* "system.posix_acl_access" */
|
||||
static void
|
||||
xattrs__acls_set (struct tar_stat_info const *st,
|
||||
char const *file_name, int type,
|
||||
const char *ptr, size_t len, bool def)
|
||||
{ /* "system.posix_acl_access" */
|
||||
char *ptr, size_t len, bool def)
|
||||
{
|
||||
acl_t acl;
|
||||
|
||||
if (ptr)
|
||||
@@ -209,7 +209,7 @@ xattrs__acls_set (struct tar_stat_info const *st,
|
||||
return; /* don't call acl functions unless we first hit an ACL, or
|
||||
--acls was passed explicitly */
|
||||
|
||||
if (acl == (acl_t) NULL)
|
||||
if (!acl)
|
||||
{
|
||||
call_arg_warn ("acl_from_text", file_name);
|
||||
return;
|
||||
@@ -219,7 +219,7 @@ xattrs__acls_set (struct tar_stat_info const *st,
|
||||
/* warn even if filesystem does not support acls */
|
||||
WARNOPT (WARN_XATTR_WRITE,
|
||||
(0, errno,
|
||||
_("acl_set_file_at: Cannot set POSIX ACLs for file '%s'"),
|
||||
_ ("acl_set_file_at: Cannot set POSIX ACLs for file '%s'"),
|
||||
file_name));
|
||||
|
||||
acl_free (acl);
|
||||
@@ -229,13 +229,12 @@ static void
|
||||
xattrs__acls_get_a (int parentfd, const char *file_name,
|
||||
struct tar_stat_info *st,
|
||||
char **ret_ptr, size_t * ret_len)
|
||||
{ /* "system.posix_acl_access" */
|
||||
{
|
||||
char *val = NULL;
|
||||
ssize_t len;
|
||||
acl_t acl;
|
||||
|
||||
if ((acl = acl_get_file_at (parentfd, file_name, ACL_TYPE_ACCESS))
|
||||
== (acl_t) NULL)
|
||||
if (!(acl = acl_get_file_at (parentfd, file_name, ACL_TYPE_ACCESS)))
|
||||
{
|
||||
if (errno != ENOTSUP)
|
||||
call_arg_warn ("acl_get_file_at", file_name);
|
||||
@@ -245,7 +244,7 @@ xattrs__acls_get_a (int parentfd, const char *file_name,
|
||||
val = acl_to_text (acl, &len);
|
||||
acl_free (acl);
|
||||
|
||||
if (val == NULL)
|
||||
if (!val)
|
||||
{
|
||||
call_arg_warn ("acl_to_text", file_name);
|
||||
return;
|
||||
@@ -257,17 +256,17 @@ xattrs__acls_get_a (int parentfd, const char *file_name,
|
||||
acl_free (val);
|
||||
}
|
||||
|
||||
/* "system.posix_acl_default" */
|
||||
static void
|
||||
xattrs__acls_get_d (int parentfd, char const *file_name,
|
||||
struct tar_stat_info *st,
|
||||
char **ret_ptr, size_t * ret_len)
|
||||
{ /* "system.posix_acl_default" */
|
||||
{
|
||||
char *val = NULL;
|
||||
ssize_t len;
|
||||
acl_t acl;
|
||||
|
||||
if ((acl = acl_get_file_at (parentfd, file_name, ACL_TYPE_DEFAULT))
|
||||
== (acl_t) NULL)
|
||||
if (!(acl = acl_get_file_at (parentfd, file_name, ACL_TYPE_DEFAULT)))
|
||||
{
|
||||
if (errno != ENOTSUP)
|
||||
call_arg_warn ("acl_get_file_at", file_name);
|
||||
@@ -277,7 +276,7 @@ xattrs__acls_get_d (int parentfd, char const *file_name,
|
||||
val = acl_to_text (acl, &len);
|
||||
acl_free (acl);
|
||||
|
||||
if (val == NULL)
|
||||
if (!val)
|
||||
{
|
||||
call_arg_warn ("acl_to_text", file_name);
|
||||
return;
|
||||
@@ -296,14 +295,14 @@ acls_one_line (const char *prefix, char delim,
|
||||
{
|
||||
/* support both long and short text representation of posix acls */
|
||||
struct obstack stk;
|
||||
obstack_init (&stk);
|
||||
int pref_len = strlen (prefix);
|
||||
const char *oldstring = aclstring;
|
||||
int pos = 0;
|
||||
|
||||
if (!aclstring || !len)
|
||||
return;
|
||||
|
||||
int pos = 0;
|
||||
obstack_init (&stk);
|
||||
while (pos <= len)
|
||||
{
|
||||
int move = strcspn (aclstring, ",\n");
|
||||
@@ -320,9 +319,8 @@ acls_one_line (const char *prefix, char delim,
|
||||
}
|
||||
|
||||
obstack_1grow (&stk, '\0');
|
||||
const char *toprint = obstack_finish (&stk);
|
||||
|
||||
fprintf (stdlis, "%s", toprint);
|
||||
fprintf (stdlis, "%s", (char *) obstack_finish (&stk));
|
||||
|
||||
obstack_free (&stk, NULL);
|
||||
}
|
||||
@@ -361,7 +359,7 @@ void
|
||||
xattrs_acls_set (struct tar_stat_info const *st,
|
||||
char const *file_name, char typeflag)
|
||||
{
|
||||
if ((acls_option > 0) && (typeflag != SYMTYPE))
|
||||
if (acls_option > 0 && typeflag != SYMTYPE)
|
||||
{
|
||||
#ifndef HAVE_POSIX_ACLS
|
||||
static int done = 0;
|
||||
@@ -371,7 +369,7 @@ xattrs_acls_set (struct tar_stat_info const *st,
|
||||
#else
|
||||
xattrs__acls_set (st, file_name, ACL_TYPE_ACCESS,
|
||||
st->acls_a_ptr, st->acls_a_len, false);
|
||||
if ((typeflag == DIRTYPE) || (typeflag == GNUTYPE_DUMPDIR))
|
||||
if (typeflag == DIRTYPE || typeflag == GNUTYPE_DUMPDIR)
|
||||
xattrs__acls_set (st, file_name, ACL_TYPE_DEFAULT,
|
||||
st->acls_d_ptr, st->acls_d_len, true);
|
||||
#endif
|
||||
@@ -381,26 +379,19 @@ xattrs_acls_set (struct tar_stat_info const *st,
|
||||
static void
|
||||
mask_map_realloc (struct xattrs_mask_map *map)
|
||||
{
|
||||
if (map->used == map->size)
|
||||
{
|
||||
if (map->size == 0)
|
||||
{
|
||||
map->size = 4;
|
||||
map->masks = xmalloc (16 * sizeof (char *));
|
||||
return;
|
||||
}
|
||||
|
||||
if (map->size <= map->used)
|
||||
{
|
||||
map->size *= 2;
|
||||
map->masks = xrealloc (map->masks, map->size * sizeof (char *));
|
||||
return;
|
||||
map->masks = x2nrealloc (map->masks, &map->size, sizeof (map->masks[0]));
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
xattrs_mask_add (const char *mask, bool incl)
|
||||
{
|
||||
struct xattrs_mask_map *mask_map = incl ? &xattrs_setup.incl
|
||||
: &xattrs_setup.excl;
|
||||
struct xattrs_mask_map *mask_map =
|
||||
incl ? &xattrs_setup.incl : &xattrs_setup.excl;
|
||||
/* ensure there is enough space */
|
||||
mask_map_realloc (mask_map);
|
||||
/* just assign pointers -- we silently expect that pointer "mask" is valid
|
||||
@@ -436,12 +427,12 @@ xattrs_xattrs_get (int parentfd, char const *file_name,
|
||||
WARN ((0, 0, _("XATTR support is not available")));
|
||||
done = 1;
|
||||
#else
|
||||
static ssize_t xsz = 1024;
|
||||
static size_t xsz = 1024;
|
||||
static char *xatrs = NULL;
|
||||
ssize_t xret = -1;
|
||||
|
||||
if (!xatrs)
|
||||
xatrs = xmalloc (xsz);
|
||||
xatrs = x2nrealloc (xatrs, &xsz, 1);
|
||||
|
||||
while (((fd == 0) ?
|
||||
((xret =
|
||||
@@ -449,8 +440,7 @@ xattrs_xattrs_get (int parentfd, char const *file_name,
|
||||
((xret = flistxattr (fd, xatrs, xsz)) == -1))
|
||||
&& (errno == ERANGE))
|
||||
{
|
||||
xsz <<= 1;
|
||||
xatrs = xrealloc (xatrs, xsz);
|
||||
xatrs = x2nrealloc (xatrs, &xsz, 1);
|
||||
}
|
||||
|
||||
if (xret == -1)
|
||||
@@ -458,11 +448,11 @@ xattrs_xattrs_get (int parentfd, char const *file_name,
|
||||
else
|
||||
{
|
||||
const char *attr = xatrs;
|
||||
static ssize_t asz = 1024;
|
||||
static size_t asz = 1024;
|
||||
static char *val = NULL;
|
||||
|
||||
if (!val)
|
||||
val = xmalloc (asz);
|
||||
val = x2nrealloc (val, &asz, 1);
|
||||
|
||||
while (xret > 0)
|
||||
{
|
||||
@@ -477,8 +467,7 @@ xattrs_xattrs_get (int parentfd, char const *file_name,
|
||||
: ((aret = fgetxattr (fd, attr, val, asz)) == -1))
|
||||
&& (errno == ERANGE))
|
||||
{
|
||||
asz <<= 1;
|
||||
val = xrealloc (val, asz);
|
||||
val = x2nrealloc (val, &asz, 1);
|
||||
}
|
||||
|
||||
if (aret != -1)
|
||||
@@ -535,8 +524,9 @@ xattrs_selinux_get (int parentfd, char const *file_name,
|
||||
WARN ((0, 0, _("SELinux support is not available")));
|
||||
done = 1;
|
||||
#else
|
||||
int result = (fd ? fgetfilecon (fd, &st->cntx_name)
|
||||
: lgetfileconat (parentfd, file_name, &st->cntx_name));
|
||||
int result = fd ?
|
||||
fgetfilecon (fd, &st->cntx_name)
|
||||
: lgetfileconat (parentfd, file_name, &st->cntx_name);
|
||||
|
||||
if (result == -1 && errno != ENODATA && errno != ENOTSUP)
|
||||
call_arg_warn (fd ? "fgetfilecon" : "lgetfileconat", file_name);
|
||||
@@ -597,27 +587,24 @@ xattrs_matches_mask (const char *kw, struct xattrs_mask_map *mm)
|
||||
return false;
|
||||
}
|
||||
|
||||
#define USER_DOT_PFX "user."
|
||||
|
||||
static bool
|
||||
xattrs_kw_included (const char *kw, bool archiving)
|
||||
{
|
||||
if (xattrs_setup.incl.size)
|
||||
return xattrs_matches_mask (kw, &xattrs_setup.incl);
|
||||
else
|
||||
{
|
||||
if (archiving)
|
||||
else if (archiving)
|
||||
return true;
|
||||
else
|
||||
return strncmp (kw, "user.", strlen ("user.")) == 0;
|
||||
}
|
||||
return strncmp (kw, USER_DOT_PFX, sizeof (USER_DOT_PFX) - 1) == 0;
|
||||
}
|
||||
|
||||
static bool
|
||||
xattrs_kw_excluded (const char *kw, bool archiving)
|
||||
{
|
||||
if (!xattrs_setup.excl.size)
|
||||
return false;
|
||||
|
||||
return xattrs_matches_mask (kw, &xattrs_setup.excl);
|
||||
return xattrs_setup.excl.size ?
|
||||
xattrs_matches_mask (kw, &xattrs_setup.excl) : false;
|
||||
}
|
||||
|
||||
/* Check whether the xattr with keyword KW should be discarded from list of
|
||||
@@ -626,10 +613,8 @@ xattrs_kw_excluded (const char *kw, bool archiving)
|
||||
static bool
|
||||
xattrs_masked_out (const char *kw, bool archiving)
|
||||
{
|
||||
if (!xattrs_kw_included (kw, archiving))
|
||||
return true;
|
||||
|
||||
return xattrs_kw_excluded (kw, archiving);
|
||||
return xattrs_kw_included (kw, archiving) ?
|
||||
xattrs_kw_excluded (kw, archiving) : true;
|
||||
}
|
||||
|
||||
void
|
||||
@@ -691,18 +676,19 @@ xattrs_print_char (struct tar_stat_info const *st, char *output)
|
||||
{
|
||||
/* placeholders */
|
||||
*output = ' ';
|
||||
*(output + 1) = 0;
|
||||
output[1] = 0;
|
||||
}
|
||||
|
||||
if (xattrs_option > 0 && st->xattr_map_size)
|
||||
for (i = 0; i < st->xattr_map_size; ++i)
|
||||
{
|
||||
char *keyword = st->xattr_map[i].xkey + strlen ("SCHILY.xattr.");
|
||||
if (xattrs_masked_out (keyword, false /* like extracting */ ))
|
||||
continue;
|
||||
if (!xattrs_masked_out (keyword, false /* like extracting */ ))
|
||||
{
|
||||
*output = '*';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (selinux_context_option > 0 && st->cntx_name)
|
||||
*output = '.';
|
||||
@@ -734,11 +720,11 @@ xattrs_print (struct tar_stat_info const *st)
|
||||
if (xattrs_option && st->xattr_map_size)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < st->xattr_map_size; ++i)
|
||||
{
|
||||
char *keyword = st->xattr_map[i].xkey + strlen ("SCHILY.xattr.");
|
||||
if (xattrs_masked_out (keyword, false /* like extracting */ ))
|
||||
continue;
|
||||
if (!xattrs_masked_out (keyword, false /* like extracting */ ))
|
||||
fprintf (stdlis, " x: %lu %s\n",
|
||||
(unsigned long) st->xattr_map[i].xval_len, keyword);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user