attr.c 7.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * attr.c - extended attributes (xattr) manipulation
 *
 * This file is part of zsh, the Z shell.
 *
 * Copyright (c) 2009 Mikael Magnusson
 * All rights reserved.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and to distribute modified versions of this software for any
 * purpose, provided that the above copyright notice and the following
 * two paragraphs appear in all copies of this software.
 *
 * In no event shall Mikael Magnusson or the Zsh Development Group be liable
 * to any party for direct, indirect, special, incidental, or consequential
 * damages arising out of the use of this software and its documentation,
 * even if Andrew Main and the Zsh Development Group have been advised of
 * the possibility of such damage.
 *
 * Mikael Magnusson and the Zsh Development Group specifically disclaim any
 * warranties, including, but not limited to, the implied warranties of
 * merchantability and fitness for a particular purpose.  The software
 * provided hereunder is on an "as is" basis, and Mikael Magnusson and the
 * Zsh Development Group have no obligation to provide maintenance,
 * support, updates, enhancements, or modifications.
 *
 */

#include <sys/types.h>
#include <sys/xattr.h>

33 34 35
#include "attr.mdh"
#include "attr.pro"

36 37
static ssize_t
xgetxattr(const char *path, const char *name, void *value, size_t size, int symlink)
38
{
39 40 41 42 43 44
#ifdef XATTR_EXTRA_ARGS
    return getxattr(path, name, value, size, 0, symlink ? XATTR_NOFOLLOW: 0);
#else
    switch (symlink) {
    case 0:
        return getxattr(path, name, value, size);
45
    default:
46 47 48 49 50 51 52 53 54 55 56 57 58 59
        return lgetxattr(path, name, value, size);
    }
#endif
}

static ssize_t
xlistxattr(const char *path, char *list, size_t size, int symlink)
{
#ifdef XATTR_EXTRA_ARGS
    return listxattr(path, list, size, symlink ? XATTR_NOFOLLOW : 0);
#else
    switch (symlink) {
    case 0:
        return listxattr(path, list, size);
60
    default:
61 62 63 64
        return llistxattr(path, list, size);
    }
#endif
}
65

66 67 68 69
static int
xsetxattr(const char *path, const char *name, const void *value,
          size_t size, int flags, int symlink)
{
70
#ifdef XATTR_EXTRA_ARGS
71 72 73 74 75
    return setxattr(path, name, value, size, 0, flags | symlink ? XATTR_NOFOLLOW : 0);
#else
    switch (symlink) {
    case 0:
        return setxattr(path, name, value, size, flags);
76
    default:
77 78
        return lsetxattr(path, name, value, size, flags);
    }
79
#endif
80 81 82 83 84
}

static int
xremovexattr(const char *path, const char *name, int symlink)
{
85
#ifdef XATTR_EXTRA_ARGS
86 87 88 89 90
    return removexattr(path, name, symlink ? XATTR_NOFOLLOW : 0);
#else
    switch (symlink) {
    case 0:
        return removexattr(path, name);
91
    default:
92 93
        return lremovexattr(path, name);
    }
94
#endif
95 96 97 98 99 100
}

static int
bin_getattr(char *nam, char **argv, Options ops, UNUSED(int func))
{
    int ret = 0;
101
    int val_len = 0, attr_len = 0, slen;
102 103 104 105 106
    char *value, *file = argv[0], *attr = argv[1], *param = argv[2];
    int symlink = OPT_ISSET(ops, 'h');

    unmetafy(file, &slen);
    unmetafy(attr, NULL);
107 108 109 110 111 112 113 114 115 116 117
    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';
118
            if (param)
119 120 121
                setsparam(param, metafy(value, attr_len, META_DUP));
            else
                printf("%s\n", value);
122
        }
123
        zfree(value, val_len+1);
124
    }
125
    if (val_len < 0 || attr_len < 0 || attr_len > val_len)  {
126
        zwarnnam(nam, "%s: %e", metafy(file, slen, META_NOALLOC), errno);
127
        ret = 1 + ((val_len > 0 && attr_len > val_len) || attr_len < 0);
128
    }
129 130 131 132
    return ret;
}

static int
133
bin_setattr(char *nam, char **argv, Options ops, UNUSED(int func))
134
{
135 136 137
    int ret = 0, slen, vlen;
    int symlink = OPT_ISSET(ops, 'h');
    char *file = argv[0], *attr = argv[1], *value = argv[2];
138

139 140 141 142 143
    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);
144 145 146 147 148 149
        ret = 1;
    }
    return ret;
}

static int
150
bin_delattr(char *nam, char **argv, Options ops, UNUSED(int func))
151
{
152
    int ret = 0, slen;
153
    int symlink = OPT_ISSET(ops, 'h');
154
    char *file = argv[0], **attr = argv;
155

156 157 158 159 160 161 162 163
    unmetafy(file, &slen);
    while (*++attr) {
        unmetafy(*attr, NULL);
        if (xremovexattr(file, *attr, symlink)) {
            zwarnnam(nam, "%s: %e", metafy(file, slen, META_NOALLOC), errno);
            ret = 1;
            break;
        }
164 165 166
    }
    return ret;
}
167

168
static int
169
bin_listattr(char *nam, char **argv, Options ops, UNUSED(int func))
170 171
{
    int ret = 0;
172
    int val_len, list_len = 0, slen;
173 174
    char *value, *file = argv[0], *param = argv[1];
    int symlink = OPT_ISSET(ops, 'h');
175

176 177 178 179 180 181 182 183 184 185 186
    unmetafy(file, &slen);
    val_len = xlistxattr(file, NULL, 0, symlink);
    if (val_len == 0) {
        if (param)
            unsetparam(param);
        return 0;
    }
    if (val_len > 0) {
        value = (char *)zalloc(val_len+1);
        list_len = xlistxattr(file, value, val_len, symlink);
        if (list_len > 0 && list_len <= val_len) {
187
            char *p = value;
188
            if (param) {
189 190
                int arrlen = 0;
                char **array = NULL, **arrptr = NULL;
191

192 193 194 195 196 197 198 199 200
                while (p < &value[list_len]) {
                    arrlen++;
                    p += strlen(p) + 1;
                }
                arrptr = array = (char **)zshcalloc((arrlen+1) * sizeof(char *));
                p = value;
                while (p < &value[list_len]) {
                    *arrptr++ = metafy(p, -1, META_DUP);
                    p += strlen(p) + 1;
201
                }
202
                setaparam(param, array);
203
            } else while (p < &value[list_len]) {
204 205 206 207
                printf("%s\n", p);
                p += strlen(p) + 1;
            }
        }
208 209
        zfree(value, val_len+1);
    }
210
    if (val_len < 0 || list_len < 0 || list_len > val_len) {
211
        zwarnnam(nam, "%s: %e", metafy(file, slen, META_NOALLOC), errno);
212
        ret = 1 + (list_len > val_len || list_len < 0);
213 214 215 216 217 218 219
    }
    return ret;
}

/* module paraphernalia */

static struct builtin bintab[] = {
220 221 222 223
    BUILTIN("zgetattr", 0, bin_getattr, 2, 3, 0, "h", NULL),
    BUILTIN("zsetattr", 0, bin_setattr, 3, 3, 0, "h", NULL),
    BUILTIN("zdelattr", 0, bin_delattr, 2, -1, 0, "h", NULL),
    BUILTIN("zlistattr", 0, bin_listattr, 1, 2, 0, "h", NULL),
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
};

static struct features module_features = {
    bintab, sizeof(bintab)/sizeof(*bintab),
    NULL, 0,
    NULL, 0,
    NULL, 0,
    0
};

/**/
int
setup_(UNUSED(Module m))
{
    return 0;
}

/**/
int
features_(Module m, char ***features)
{
    *features = featuresarray(m, &module_features);
    return 0;
}

/**/
int
enables_(Module m, int **enables)
{
    return handlefeatures(m, &module_features, enables);
}

/**/
int
boot_(UNUSED(Module m))
{
    return 0;
}

/**/
int
cleanup_(Module m)
{
    return setfeatureenables(m, &module_features, NULL);
}

/**/
int
finish_(UNUSED(Module m))
{
    return 0;
}