summaryrefslogtreecommitdiff
path: root/libattr/solaris.c
blob: 966a977c9655bbf9bc5c841cba5a7f3403f2f47b (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Copyright (c) 2012 Igor Pashev <pashev.igor@gmail.com>
 * All Rights Reserved.
 *
 * This program 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.1 of the License, or
 * (at your option) any later version.
 *
 * This program 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 program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#ifndef __EXTENSIONS__
# define __EXTENSIONS__
#endif
#include <string.h>

#include <xattr.h>

ssize_t
fgetxattr (int filedes, const char *name,
           void *value, size_t size)
{
    int xfd;
    ssize_t rv;
    struct stat sb;

    xfd = openat(filedes, name, O_RDONLY | O_XATTR);
    if (xfd == -1) {
        if (errno == ENOENT)
            errno = ENOATTR;
        return (-1);
    }

    if (fstat(xfd, &sb) == -1) {
        rv = -1;
    } else if (sb.st_size > SSIZE_MAX) {
        errno = ERANGE;
        rv = -1;
    } else if (value == NULL || size == 0) {
        rv = sb.st_size;
    } else if (sb.st_size > size) {
        errno = ERANGE;
        rv = -1;
    } else {
        rv = read(xfd, value, size);
    }

    (void) close(xfd);
    return (rv);
}

ssize_t
getxattr (const char *path, const char *name,
           void *value, size_t size)
{
    int fd;
    ssize_t rv;
    
    fd = open(path, O_RDONLY);
    if (fd == -1) {
        return (-1);
    }

    rv = fgetxattr(fd, name, value, size);

    (void) close(fd);
    return (rv);
}

static
ssize_t
__fflistxattr (int xfd, char *list, size_t size)
{
    ssize_t rv;
    DIR *d;
    struct dirent *de;
    size_t l;

    d = fdopendir(xfd);
    if (d == NULL) {
        return (-1);
    }

    rv = 0;
    while ((de = readdir(d)) != NULL) {
        if (
            !strcmp(de->d_name, ".")           ||
            !strcmp(de->d_name, "..")          ||
            !strcmp(de->d_name, "SUNWattr_rw") ||
            !strcmp(de->d_name, "SUNWattr_ro")
            )
        {
            continue;
        }

        /* Define __EXTENSIONS__ for strlcpy() */
        /* http://www.gratisoft.us/todd/papers/strlcpy.html */
        l = strlcpy(list, de->d_name, size);
        if (l >= size) {
            errno = ERANGE;
            rv = -1;
            break;
        }

        rv   += l + 1;
        size -= l + 1;
        list += l + 1;
    }

    (void) closedir(d);
    return (rv);
}

ssize_t
flistxattr (int fd, char *list, size_t size)
{
    int xfd;
    ssize_t rv;

    xfd = openat(fd, ".", O_RDONLY);
    if (xfd == -1) {
        return (-1);
    }
    
    rv = __fflistxattr(xfd, list, size);
    
    (void) close(xfd);
    return (rv);
}

ssize_t
listxattr (const char * path, char *list, size_t size)
{
    int xfd;
    ssize_t rv;

    xfd = attropen(path, ".", O_RDONLY);
    if (xfd == -1) {
        return (-1);
    }
    
    rv = __fflistxattr(xfd, list, size);
    
    (void) close(xfd);
    return (rv);
}

int
fremovexattr (int filedes, const char *name)
{
    int xfd;
    int rv;

    xfd = openat(filedes, ".", O_XATTR, 0644);
    if (xfd == -1) {
        return (-1);
    }

    rv = unlinkat(xfd, name, 0);
    (void) close(xfd);

    if (rv == -1) {
        if (errno == ENOENT)
            errno = ENOATTR;
    }
    return (rv);
}

int
removexattr (const char *path, const char *name)
{
    int fd;
    int rv;

    fd = open(path, O_RDONLY);
    if (fd == -1) {
        return (-1);
    }

    rv = fremovexattr(fd, name);

    (void) close(fd);
    return (rv);
}

int
fsetxattr (int filedes, const char *name,
        const void *value, size_t size, int flags)
{
    int xfd;
    ssize_t w;

    xfd = openat(filedes, name,
         O_XATTR | O_TRUNC |
         ((flags & XATTR_CREATE) ? O_EXCL : 0) |
         ((flags & XATTR_REPLACE) ? O_RDWR : O_WRONLY | O_CREAT),
         0644);

    if (xfd == -1) {
        return (-1);
    }

    while (size > 0) {
        w = write(xfd, value, size);
        if (w == -1) {
            break;
        }
        size  -= w;
        value += w;
    }

    (void) close(xfd);
    return ((w == -1) ? -1 : 0);
}

int
setxattr (const char *path, const char *name,
        const void *value, size_t size, int flags)
{
    int fd;
    int rv;

    fd = open(path, O_RDONLY);
    if (fd == -1) {
        return (-1);
    }

    rv = fsetxattr(fd, name, value, size, flags);

    (void) close(fd);
    return (rv);
}




/*
 * Extended attributes for symlinks are not supported
 *
 */


ssize_t
lgetxattr (const char *path, const char *name,
           void *value, size_t size)
{
    struct stat s;
    if (lstat(path, &s) == -1) {
        return (-1);
    }
    if (S_ISLNK(s.st_mode)) {
        errno = ENOTSUP;
        return (-1);
    }

    return getxattr(path, name, value, size);
}

int
lsetxattr (const char *path, const char *name,
        const void *value, size_t size, int flags)
{
    struct stat s;
    if (lstat(path, &s) == -1) {
        return (-1);
    }
    if (S_ISLNK(s.st_mode)) {
        errno = ENOTSUP;
        return (-1);
    }

    return setxattr(path, name, value, size, flags);
}

int
lremovexattr (const char *path, const char *name)
{
    struct stat s;
    if (lstat(path, &s) == -1) {
        return (-1);
    }
    if (S_ISLNK(s.st_mode)) {
        errno = ENOTSUP;
        return (-1);
    }

    return removexattr(path, name);
}

ssize_t
llistxattr (const char *path, char *list, size_t size)
{
    struct stat s;
    if (lstat(path, &s) == -1) {
        return (-1);
    }
    if (S_ISLNK(s.st_mode)) {
        errno = ENOTSUP;
        return (-1);
    }

    return listxattr(path, list, size);
}